From: Mike Frysinger Date: Mon, 15 Nov 2021 07:36:29 +0000 (-0500) Subject: sim: run: add --argv0 option to control argv[0] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=852016f92107581dbc9513196c9a74a91b5a5b6e;p=binutils-gdb.git 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. --- diff --git a/sim/common/nrun.c b/sim/common/nrun.c index f1fb7d12eba..b3e48e214ed 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 ff54f1d1e4c..c34f6f66a91 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 7e5695d8cc7..ee7d11fb692 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 0e8cd5afc2f..6f147b2d703 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); }