public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] sim: split program path out of argv vector
@ 2021-11-15  8:01 Mike Frysinger
  2021-11-15  8:01 ` [PATCH 2/3] sim: run: add --argv0 option to control argv[0] Mike Frysinger
  2021-11-15  8:01 ` [PATCH 3/3] sim: cris: replace custom "dest" test field with new --argv0 Mike Frysinger
  0 siblings, 2 replies; 3+ messages in thread
From: Mike Frysinger @ 2021-11-15  8:01 UTC (permalink / raw)
  To: gdb-patches

We use the program argv to both find the program to run (argv[0]) and
to hold the arguments to the program.  Most of the time this is fine,
but if we want to let programs specify argv[0] independently (which is
possible in standard *NIX programs), this double duty doesn't work.

So let's split the path to the program to run out into a separate
field by itself.  This simplifies the various sim_open funcs too.

By itself, this code is more of a logical cleanup than something that
is super useful.  But it will open up customization of argv[0] in a
follow up commit.  Split the changes to make it easier to review.
---
 sim/aarch64/interp.c         |  5 +----
 sim/arm/wrapper.c            |  5 +----
 sim/avr/interp.c             |  5 +----
 sim/bfin/interp.c            |  5 +----
 sim/bpf/sim-if.c             |  5 +----
 sim/common/nrun.c            |  2 +-
 sim/common/sim-base.h        |  5 +++++
 sim/common/sim-options.c     |  5 ++++-
 sim/common/sim-utils.c       |  1 +
 sim/cr16/interp.c            |  5 +----
 sim/cris/sim-if.c            | 10 +++-------
 sim/d10v/interp.c            |  5 +----
 sim/example-synacor/interp.c |  5 +----
 sim/frv/sim-if.c             |  6 +-----
 sim/ft32/interp.c            |  5 +----
 sim/h8300/compile.c          |  5 +----
 sim/iq2000/sim-if.c          |  6 +-----
 sim/lm32/sim-if.c            |  5 +----
 sim/m32r/sim-if.c            |  6 +-----
 sim/m68hc11/interp.c         |  5 +----
 sim/mcore/interp.c           |  5 +----
 sim/microblaze/interp.c      |  5 +----
 sim/mips/interp.c            |  6 +-----
 sim/mn10300/interp.c         |  6 +-----
 sim/moxie/interp.c           |  5 +----
 sim/msp430/msp430-sim.c      |  5 +----
 sim/or1k/sim-if.c            |  5 +----
 sim/pru/interp.c             |  5 +----
 sim/riscv/interp.c           |  5 +----
 sim/sh/interp.c              |  5 +----
 sim/v850/interp.c            |  6 +-----
 31 files changed, 40 insertions(+), 119 deletions(-)

diff --git a/sim/aarch64/interp.c b/sim/aarch64/interp.c
index 18c2fc0eacbd..999b949ed88b 100644
--- a/sim/aarch64/interp.c
+++ b/sim/aarch64/interp.c
@@ -339,10 +339,7 @@ sim_open (SIM_OPEN_KIND                  kind,
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK
       || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
       || sim_parse_args (sd, argv) != SIM_RC_OK
-      || sim_analyze_program (sd,
-			      (STATE_PROG_ARGV (sd) != NULL
-			       ? *STATE_PROG_ARGV (sd)
-			       : NULL), abfd) != SIM_RC_OK
+      || sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK
       || sim_config (sd) != SIM_RC_OK
       || sim_post_argv_init (sd) != SIM_RC_OK)
     {
diff --git a/sim/arm/wrapper.c b/sim/arm/wrapper.c
index e697d55a6b5e..12b974a95f82 100644
--- a/sim/arm/wrapper.c
+++ b/sim/arm/wrapper.c
@@ -825,10 +825,7 @@ sim_open (SIM_OPEN_KIND kind,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/avr/interp.c b/sim/avr/interp.c
index df7177ef23f5..8ec07c98f8d2 100644
--- a/sim/avr/interp.c
+++ b/sim/avr/interp.c
@@ -1710,10 +1710,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/bfin/interp.c b/sim/bfin/interp.c
index fab4df7aaf8b..088b55937878 100644
--- a/sim/bfin/interp.c
+++ b/sim/bfin/interp.c
@@ -740,10 +740,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/bpf/sim-if.c b/sim/bpf/sim-if.c
index aba191df7ec4..a8d944115671 100644
--- a/sim/bpf/sim-if.c
+++ b/sim/bpf/sim-if.c
@@ -147,10 +147,7 @@ sim_open (SIM_OPEN_KIND kind,
   if (sim_parse_args (sd, argv) != SIM_RC_OK)
     goto error;
 
-  if (sim_analyze_program (sd,
-                           (STATE_PROG_ARGV (sd) != NULL
-                            ? *STATE_PROG_ARGV (sd)
-                            : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     goto error;
 
   if (sim_config (sd) != SIM_RC_OK)
diff --git a/sim/common/nrun.c b/sim/common/nrun.c
index 3fd78346f957..f1fb7d12ebaa 100644
--- a/sim/common/nrun.c
+++ b/sim/common/nrun.c
@@ -103,7 +103,7 @@ main (int argc, char **argv)
   if (prog_argv == NULL || *prog_argv == NULL)
     usage ();
 
-  name = *prog_argv;
+  name = STATE_PROG_FILE (sd);
 
   /* For simulators that don't open prog during sim_open() */
   if (prog_bfd == NULL)
diff --git a/sim/common/sim-base.h b/sim/common/sim-base.h
index 674b2d423084..ff54f1d1e4c4 100644
--- a/sim/common/sim-base.h
+++ b/sim/common/sim-base.h
@@ -151,6 +151,11 @@ struct sim_state {
   const char *model_name;
 #define STATE_MODEL_NAME(sd) ((sd)->model_name)
 
+  /* In standalone simulator, this is the program to run.  Not to be confused
+     with argv which are the strings passed to the program itself.  */
+  char *prog_file;
+#define STATE_PROG_FILE(sd) ((sd)->prog_file)
+
   /* In standalone simulator, this is the program's arguments passed
      on the command line.  */
   char **prog_argv;
diff --git a/sim/common/sim-options.c b/sim/common/sim-options.c
index e82ac33fe764..7e5695d8cc78 100644
--- a/sim/common/sim-options.c
+++ b/sim/common/sim-options.c
@@ -604,7 +604,10 @@ sim_parse_args (SIM_DESC sd, char * const *argv)
       if (optc == -1)
 	{
 	  if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
-	    STATE_PROG_ARGV (sd) = dupargv (argv + optind);
+	    {
+	      STATE_PROG_FILE (sd) = xstrdup (argv[optind]);
+	      STATE_PROG_ARGV (sd) = dupargv (argv + optind);
+	    }
 	  break;
 	}
       if (optc == '?')
diff --git a/sim/common/sim-utils.c b/sim/common/sim-utils.c
index 88fd20e88767..0e8cd5afc2f4 100644
--- a/sim/common/sim-utils.c
+++ b/sim/common/sim-utils.c
@@ -97,6 +97,7 @@ sim_state_free (SIM_DESC sd)
   SIM_STATE_FREE (sd);
 #endif
 
+  free (STATE_PROG_FILE (sd));
   free (sd);
 }
 
diff --git a/sim/cr16/interp.c b/sim/cr16/interp.c
index e2aef013d160..7bbcf3cde7c9 100644
--- a/sim/cr16/interp.c
+++ b/sim/cr16/interp.c
@@ -423,10 +423,7 @@ sim_open (SIM_OPEN_KIND kind, struct host_callback_struct *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/cris/sim-if.c b/sim/cris/sim-if.c
index 2d7e9221505a..500941a27d7b 100644
--- a/sim/cris/sim-if.c
+++ b/sim/cris/sim-if.c
@@ -690,11 +690,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     }
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       /* When there's an error, sim_analyze_program has already output
 	 a message.  Let's just clarify it, as "not an object file"
@@ -717,9 +713,9 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 
   if (abfd != NULL && bfd_get_arch (abfd) == bfd_arch_unknown)
     {
-      if (STATE_PROG_ARGV (sd) != NULL)
+      if (STATE_PROG_FILE (sd) != NULL)
 	sim_io_eprintf (sd, "%s: `%s' is not a CRIS program\n",
-			STATE_MY_NAME (sd), *STATE_PROG_ARGV (sd));
+			STATE_MY_NAME (sd), STATE_PROG_FILE (sd));
       else
 	sim_io_eprintf (sd, "%s: program to be run is not a CRIS program\n",
 			STATE_MY_NAME (sd));
diff --git a/sim/d10v/interp.c b/sim/d10v/interp.c
index 228ca3ea6e49..33baea79d794 100644
--- a/sim/d10v/interp.c
+++ b/sim/d10v/interp.c
@@ -780,10 +780,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/example-synacor/interp.c b/sim/example-synacor/interp.c
index 57b1bee7cd9d..d1a82e509d18 100644
--- a/sim/example-synacor/interp.c
+++ b/sim/example-synacor/interp.c
@@ -111,10 +111,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/frv/sim-if.c b/sim/frv/sim-if.c
index 6d22aaddc090..d0361537c61a 100644
--- a/sim/frv/sim-if.c
+++ b/sim/frv/sim-if.c
@@ -92,11 +92,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, bfd *abfd,
     sim_do_commandf (sd, "memory region 0,0x%x", FRV_DEFAULT_MEM_SIZE);
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/ft32/interp.c b/sim/ft32/interp.c
index 7da6ecd2937a..a1cc9abe9117 100644
--- a/sim/ft32/interp.c
+++ b/sim/ft32/interp.c
@@ -842,10 +842,7 @@ sim_open (SIM_OPEN_KIND kind,
     }
 
   /* Check for/establish the reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c
index 1f3316e20ad9..e729c520e4ef 100644
--- a/sim/h8300/compile.c
+++ b/sim/h8300/compile.c
@@ -4755,10 +4755,7 @@ sim_open (SIM_OPEN_KIND kind,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/iq2000/sim-if.c b/sim/iq2000/sim-if.c
index 8cfc1e0ca3e6..21ed8217bb07 100644
--- a/sim/iq2000/sim-if.c
+++ b/sim/iq2000/sim-if.c
@@ -93,11 +93,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
   sim_do_commandf (sd, "memory region 0x%x,0x%x", IQ2000_DATA_VALUE, IQ2000_DATA_MEM_SIZE); 
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/lm32/sim-if.c b/sim/lm32/sim-if.c
index 2f8b3449917a..ce9ab5ac9676 100644
--- a/sim/lm32/sim-if.c
+++ b/sim/lm32/sim-if.c
@@ -133,10 +133,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 #endif
 
   /* check for/establish the reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/m32r/sim-if.c b/sim/m32r/sim-if.c
index e05b1630ace7..b2f7b4636e0d 100644
--- a/sim/m32r/sim-if.c
+++ b/sim/m32r/sim-if.c
@@ -98,11 +98,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     sim_do_commandf (sd, "memory region 0,0x%x", M32R_DEFAULT_MEM_SIZE);
 
   /* check for/establish the reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/m68hc11/interp.c b/sim/m68hc11/interp.c
index 7d28f40d71dd..977c207bfa01 100644
--- a/sim/m68hc11/interp.c
+++ b/sim/m68hc11/interp.c
@@ -436,10 +436,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/mcore/interp.c b/sim/mcore/interp.c
index 138dcccea4d3..e8a452022192 100644
--- a/sim/mcore/interp.c
+++ b/sim/mcore/interp.c
@@ -1371,10 +1371,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/microblaze/interp.c b/sim/microblaze/interp.c
index d2bd9e907e26..d80959617445 100644
--- a/sim/microblaze/interp.c
+++ b/sim/microblaze/interp.c
@@ -429,10 +429,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index 4ab908d4f7a0..30d417bab569 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -633,11 +633,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* check for/establish the a reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_module_uninstall (sd);
       return 0;
diff --git a/sim/mn10300/interp.c b/sim/mn10300/interp.c
index f16a756c818d..551d17630082 100644
--- a/sim/mn10300/interp.c
+++ b/sim/mn10300/interp.c
@@ -268,11 +268,7 @@ sim_open (SIM_OPEN_KIND kind,
   
 
   /* check for/establish the a reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_module_uninstall (sd);
       return 0;
diff --git a/sim/moxie/interp.c b/sim/moxie/interp.c
index 447f52e6e402..3aa6a3b10e0a 100644
--- a/sim/moxie/interp.c
+++ b/sim/moxie/interp.c
@@ -1226,10 +1226,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
   sim_do_command(sd," memory region 0xE0000000,0x10000") ; 
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/msp430/msp430-sim.c b/sim/msp430/msp430-sim.c
index 6f1c14f759ee..119d2b95ba34 100644
--- a/sim/msp430/msp430-sim.c
+++ b/sim/msp430/msp430-sim.c
@@ -152,10 +152,7 @@ sim_open (SIM_OPEN_KIND kind,
     sim_do_commandf (sd, "memory-region 0x90000,0x70000"); /* HIGH ROM.  */
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_state_free (sd);
       return 0;
diff --git a/sim/or1k/sim-if.c b/sim/or1k/sim-if.c
index 3f47ce5e41b6..2957587afdf9 100644
--- a/sim/or1k/sim-if.c
+++ b/sim/or1k/sim-if.c
@@ -201,10 +201,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
     }
 
   /* Check for/establish the reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/pru/interp.c b/sim/pru/interp.c
index fc8bbe5c4c85..ca640f440bcd 100644
--- a/sim/pru/interp.c
+++ b/sim/pru/interp.c
@@ -772,10 +772,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/riscv/interp.c b/sim/riscv/interp.c
index 202412ab8ca0..f94a8428d690 100644
--- a/sim/riscv/interp.c
+++ b/sim/riscv/interp.c
@@ -94,10 +94,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/sh/interp.c b/sim/sh/interp.c
index 559b39a63226..1b2b09f95100 100644
--- a/sim/sh/interp.c
+++ b/sim/sh/interp.c
@@ -2367,10 +2367,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* Check for/establish the a reference program image.  */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL), abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
diff --git a/sim/v850/interp.c b/sim/v850/interp.c
index 92c80a814c69..0d3136580551 100644
--- a/sim/v850/interp.c
+++ b/sim/v850/interp.c
@@ -239,11 +239,7 @@ sim_open (SIM_OPEN_KIND    kind,
     }
 
   /* check for/establish the a reference program image */
-  if (sim_analyze_program (sd,
-			   (STATE_PROG_ARGV (sd) != NULL
-			    ? *STATE_PROG_ARGV (sd)
-			    : NULL),
-			   abfd) != SIM_RC_OK)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       sim_module_uninstall (sd);
       return 0;
-- 
2.33.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] sim: run: add --argv0 option to control argv[0]
  2021-11-15  8:01 [PATCH 1/3] sim: split program path out of argv vector Mike Frysinger
@ 2021-11-15  8:01 ` Mike Frysinger
  2021-11-15  8:01 ` [PATCH 3/3] sim: cris: replace custom "dest" test field with new --argv0 Mike Frysinger
  1 sibling, 0 replies; 3+ messages in thread
From: Mike Frysinger @ 2021-11-15  8:01 UTC (permalink / raw)
  To: gdb-patches

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.
---
 sim/common/nrun.c        |  2 +-
 sim/common/sim-base.h    |  4 ++++
 sim/common/sim-options.c | 24 +++++++++++++++++++++---
 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 f1fb7d12ebaa..b3e48e214ed3 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 ff54f1d1e4c4..c34f6f66a913 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 7e5695d8cc78..ee7d11fb6925 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 0e8cd5afc2f4..6f147b2d703b 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);
 }
 
-- 
2.33.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] sim: cris: replace custom "dest" test field with new --argv0
  2021-11-15  8:01 [PATCH 1/3] sim: split program path out of argv vector Mike Frysinger
  2021-11-15  8:01 ` [PATCH 2/3] sim: run: add --argv0 option to control argv[0] Mike Frysinger
@ 2021-11-15  8:01 ` Mike Frysinger
  1 sibling, 0 replies; 3+ messages in thread
From: Mike Frysinger @ 2021-11-15  8:01 UTC (permalink / raw)
  To: gdb-patches

The #dest field used in the cris testsuite is a bit of hack to set the
argv[0] for the tests to read out later on.  Now that the sim has an
option to set argv[0] explicitly, we don't need this custom field, so
let's drop it to harmonize the testsuites a little.
---
 sim/testsuite/cris/c/c.exp        | 6 ++----
 sim/testsuite/cris/c/readlink11.c | 3 +--
 sim/testsuite/cris/c/readlink5.c  | 2 +-
 sim/testsuite/cris/c/readlink6.c  | 2 +-
 4 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/sim/testsuite/cris/c/c.exp b/sim/testsuite/cris/c/c.exp
index 26084682dc95..ec89c5c1de20 100644
--- a/sim/testsuite/cris/c/c.exp
+++ b/sim/testsuite/cris/c/c.exp
@@ -101,7 +101,6 @@ foreach src [lsort [glob -nocomplain $srcdir/$subdir/*.c]] {
     set opts(timeout) ""
     set opts(mach) ""
     set opts(xerror) "no"
-    set opts(dest) "$testname.x"
     set opts(simenv) ""
     set opts(kfail) ""
     set opts(xfail) ""
@@ -188,8 +187,7 @@ foreach src [lsort [glob -nocomplain $srcdir/$subdir/*.c]] {
 
     verbose -log "Compiling $src with $opts(cc)"
 
-    set dest "$opts(dest)"
-    if { [target_compile $src $dest "executable" "$opts(cc)" ] != "" } {
+    if { [target_compile $src "$testname.x" "executable" "$opts(cc)" ] != "" } {
 	unresolved $testname
 	continue
     }
@@ -212,7 +210,7 @@ foreach src [lsort [glob -nocomplain $srcdir/$subdir/*.c]] {
 	eval setup_kfail $opts(kfail)
     }
 
-    set result [sim_run $dest "$opts(sim,$mach)" "$opts(progoptions)" \
+    set result [sim_run "$testname.x" "$opts(sim,$mach)" "$opts(progoptions)" \
 	"" "$opts(simenv)"]
     set return_code [lindex $result 0]
     set output [lindex $result 1]
diff --git a/sim/testsuite/cris/c/readlink11.c b/sim/testsuite/cris/c/readlink11.c
index 05a332f65ec2..9d84e7d95fac 100644
--- a/sim/testsuite/cris/c/readlink11.c
+++ b/sim/testsuite/cris/c/readlink11.c
@@ -1,7 +1,6 @@
 /* As readlink5.c (sic), but specifying silent ENOSYS.
 #notarget: cris*-*-elf
-#dest: ./readlink11.c.x
-#sim: --cris-unknown-syscall=enosys-quiet
+#sim: --cris-unknown-syscall=enosys-quiet --argv0 ./readlink11.c.x
 #output: ENOSYS\n
 #output: xyzzy\n
 */
diff --git a/sim/testsuite/cris/c/readlink5.c b/sim/testsuite/cris/c/readlink5.c
index 80f20dadd888..4bfb7ba3a1d3 100644
--- a/sim/testsuite/cris/c/readlink5.c
+++ b/sim/testsuite/cris/c/readlink5.c
@@ -1,6 +1,6 @@
 /* Check that unsupported readlink calls don't cause the simulator to abort.
 #notarget: cris*-*-elf
-#dest: ./readlink5.c.x
+#sim: --argv0 ./readlink5.c.x
 #xerror:
 #output: Unimplemented readlink syscall (*)\n
 #output: program stopped with signal 4 (*).\n
diff --git a/sim/testsuite/cris/c/readlink6.c b/sim/testsuite/cris/c/readlink6.c
index 4bac20d81905..52a26f496392 100644
--- a/sim/testsuite/cris/c/readlink6.c
+++ b/sim/testsuite/cris/c/readlink6.c
@@ -1,5 +1,5 @@
 /* Check that rare readlink calls don't cause the simulator to abort.
 #notarget: cris*-*-elf
-#dest: @exedir@/readlink6.c.x
+#sim: --argv0 @exedir@/readlink6.c.x
 */
 #include "readlink2.c"
-- 
2.33.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-11-15  8:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-15  8:01 [PATCH 1/3] sim: split program path out of argv vector Mike Frysinger
2021-11-15  8:01 ` [PATCH 2/3] sim: run: add --argv0 option to control argv[0] Mike Frysinger
2021-11-15  8:01 ` [PATCH 3/3] sim: cris: replace custom "dest" test field with new --argv0 Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).