diff options
author | Mike Frysinger <vapier@gentoo.org> | 2021-11-15 02:36:29 -0500 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2021-11-15 02:53:31 -0500 |
commit | 852016f92107581dbc9513196c9a74a91b5a5b6e (patch) | |
tree | 0e785165301066a263f61f38c94ee8b272239bc7 /sim | |
parent | e8f20a28b1192d746475f045d77ac84411f164df (diff) | |
download | gdb-852016f92107581dbc9513196c9a74a91b5a5b6e.zip gdb-852016f92107581dbc9513196c9a74a91b5a5b6e.tar.gz gdb-852016f92107581dbc9513196c9a74a91b5a5b6e.tar.bz2 |
sim: run: add --argv0 option to control argv[0]
We default argv[0] to the program we run which is a standard *NIX
convention, but sometimes we want to be able to control the argv[0]
setting independently (especially for programs that inspect argv[0]
to change their behavior or output). Add an option to control it.
Diffstat (limited to 'sim')
-rw-r--r-- | sim/common/nrun.c | 2 | ||||
-rw-r--r-- | sim/common/sim-base.h | 4 | ||||
-rw-r--r-- | sim/common/sim-options.c | 24 | ||||
-rw-r--r-- | sim/common/sim-utils.c | 1 |
4 files changed, 27 insertions, 4 deletions
diff --git a/sim/common/nrun.c b/sim/common/nrun.c index f1fb7d1..b3e48e2 100644 --- a/sim/common/nrun.c +++ b/sim/common/nrun.c @@ -227,7 +227,7 @@ main (int argc, char **argv) static void usage (void) { - fprintf (stderr, "Usage: %s [options] program [program args]\n", myname); + fprintf (stderr, "Usage: %s [options] [--] program [program args]\n", myname); fprintf (stderr, "Run `%s --help' for full list of options.\n", myname); exit (1); } diff --git a/sim/common/sim-base.h b/sim/common/sim-base.h index ff54f1d..c34f6f6 100644 --- a/sim/common/sim-base.h +++ b/sim/common/sim-base.h @@ -161,6 +161,10 @@ struct sim_state { char **prog_argv; #define STATE_PROG_ARGV(sd) ((sd)->prog_argv) + /* Thie is the program's argv[0] override. */ + char *prog_argv0; +#define STATE_PROG_ARGV0(sd) ((sd)->prog_argv0) + /* The program's bfd. */ struct bfd *prog_bfd; #define STATE_PROG_BFD(sd) ((sd)->prog_bfd) diff --git a/sim/common/sim-options.c b/sim/common/sim-options.c index 7e5695d..ee7d11f 100644 --- a/sim/common/sim-options.c +++ b/sim/common/sim-options.c @@ -104,7 +104,8 @@ typedef enum { OPTION_VERSION, OPTION_LOAD_LMA, OPTION_LOAD_VMA, - OPTION_SYSROOT + OPTION_SYSROOT, + OPTION_ARGV0, } STANDARD_OPTIONS; static const OPTION standard_options[] = @@ -179,6 +180,10 @@ static const OPTION standard_options[] = "Root for system calls with absolute file-names and cwd at start", standard_option_handler, NULL }, + { {"argv0", required_argument, NULL, OPTION_ARGV0}, + '\0', "ARGV0", "Set argv[0] to the specified string", + standard_option_handler, NULL }, + { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } }; @@ -420,6 +425,11 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, else simulator_sysroot = ""; break; + + case OPTION_ARGV0: + free (STATE_PROG_ARGV0 (sd)); + STATE_PROG_ARGV0 (sd) = xstrdup (arg); + break; } return SIM_RC_OK; @@ -605,8 +615,16 @@ sim_parse_args (SIM_DESC sd, char * const *argv) { if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) { + char **new_argv = dupargv (argv + optind); + STATE_PROG_FILE (sd) = xstrdup (argv[optind]); - STATE_PROG_ARGV (sd) = dupargv (argv + optind); + if (STATE_PROG_ARGV0 (sd) != NULL) + { + free (new_argv[0]); + new_argv[0] = xstrdup (STATE_PROG_ARGV0 (sd)); + } + freeargv (STATE_PROG_ARGV (sd)); + STATE_PROG_ARGV (sd) = new_argv; } break; } @@ -787,7 +805,7 @@ void sim_print_help (SIM_DESC sd, int is_command) { if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) - sim_io_printf (sd, "Usage: %s [options] program [program args]\n", + sim_io_printf (sd, "Usage: %s [options] [--] program [program args]\n", STATE_MY_NAME (sd)); /* Initialize duplicate argument checker. */ diff --git a/sim/common/sim-utils.c b/sim/common/sim-utils.c index 0e8cd5a..6f147b2 100644 --- a/sim/common/sim-utils.c +++ b/sim/common/sim-utils.c @@ -98,6 +98,7 @@ sim_state_free (SIM_DESC sd) #endif free (STATE_PROG_FILE (sd)); + free (STATE_PROG_ARGV0 (sd)); free (sd); } |