C++-fy and prepare for sharing fork_inferior
authorSergio Durigan Junior <sergiodj@redhat.com>
Thu, 23 Mar 2017 01:54:49 +0000 (21:54 -0400)
committerSergio Durigan Junior <sergiodj@redhat.com>
Wed, 12 Apr 2017 05:02:03 +0000 (01:02 -0400)
As a preparation for the next patch, which will move fork_inferior
from GDB to common/ (and therefore share it with gdbserver), it is
interesting to convert a few functions to C++.

This patch touches functions related to parsing command-line arguments
to the inferior (see gdb/fork-child.c:breakup_args), the way the
arguments are stored on fork_inferior (using std::vector instead of
char **), and the code responsible for dealing with argv also on
gdbserver.

I've taken this opportunity and decided to constify a few arguments to
fork_inferior/create_inferior as well, in order to make the code
cleaner.  And now, on gdbserver, we're using xstrdup everywhere and
aren't checking for memory allocation failures anymore, as requested
by Pedro:

  <https://sourceware.org/ml/gdb-patches/2017-03/msg00191.html>
  Message-Id: <025ebdb9-90d9-d54a-c055-57ed2406b812@redhat.com>

  Pedro Alves wrote:

  > On the "== NULL" check: IIUC, the old NULL check was there to
  > handle strdup returning NULL due to out-of-memory.
  > See NULL checks and comments further above in this function.
  > Now that you're using a std::vector, that doesn't work or make
  > sense any longer, since if push_back fails to allocate space for
  > its internal buffer (with operator new), our operator new replacement
  > (common/new-op.c) calls malloc_failure, which aborts gdbserver.
  >
  > Not sure it makes sense to handle out-of-memory specially in
  > the gdb/rsp-facing functions nowadays (maybe git blame/log/patch
  > submission for that code shows some guidelines).  Maybe (or, probably)
  > it's OK to stop caring about it, but then we should consistently remove
  > left over code, by using xstrdup instead and remove the NULL checks.

IMO this refactoring was very good to increase the readability of the
code as well, because some parts of the argument handling were
unnecessarily confusing before.

gdb/ChangeLog:
2017-04-12  Sergio Durigan Junior  <sergiodj@redhat.com>

* common/common-utils.c (free_vector_argv): New function.
* common/common-utils.h: Include <vector>.
(free_vector_argv): New prototype.
* darwin-nat.c (darwin_create_inferior): Rewrite function
prototype in order to constify "exec_file" and accept a
"std::string" for "allargs".
* fork-child.c: Include <vector>.
(breakup_args): Rewrite function, using C++.
(fork_inferior): Rewrite function header, constify "exec_file_arg"
and accept "std::string" for "allargs".  Update the code to
calculate "argv" based on "allargs".  Update calls to "exec_fun"
and "execvp".
* gnu-nat.c (gnu_create_inferior): Rewrite function prototype in
order to constify "exec_file" and accept a "std::string" for
"allargs".
* go32-nat.c (go32_create_inferior): Likewise.
* inf-ptrace.c (inf_ptrace_create_inferior): Likewise.
* infcmd.c (run_command_1): Constify "exec_file".  Use
"std::string" for inferior arguments.
* inferior.h (fork_inferior): Update prototype.
* linux-nat.c (linux_nat_create_inferior): Rewrite function
prototype in order to constify "exec_file" and accept a
"std::string" for "allargs".
* nto-procfs.c (procfs_create_inferior): Likewise.
* procfs.c (procfs_create_inferior): Likewise.
* remote-sim.c (gdbsim_create_inferior): Likewise.
* remote.c (extended_remote_run): Update code to accept
"std::string" as argument.
(extended_remote_create_inferior): Rewrite function prototype in
order to constify "exec_file" and accept a "std::string" for
"allargs".
* rs6000-nat.c (super_create_inferior): Likewise.
(rs6000_create_inferior): Likewise.
* target.h (struct target_ops) <to_create_inferior>: Likewise.
* windows-nat.c (windows_create_inferior): Likewise.

gdb/gdbserver/ChangeLog:
2017-04-12  Sergio Durigan Junior  <sergiodj@redhat.com>

* server.c: Include <vector>.
<program_argv, wrapper_argv>: Convert to std::vector.
(start_inferior): Rewrite function to use C++.
(handle_v_run): Likewise.  Update code that calculates the argv
based on the vRun packet; use C++.
(captured_main): Likewise.

20 files changed:
gdb/ChangeLog
gdb/common/common-utils.c
gdb/common/common-utils.h
gdb/darwin-nat.c
gdb/fork-child.c
gdb/gdbserver/ChangeLog
gdb/gdbserver/server.c
gdb/gnu-nat.c
gdb/go32-nat.c
gdb/inf-ptrace.c
gdb/infcmd.c
gdb/inferior.h
gdb/linux-nat.c
gdb/nto-procfs.c
gdb/procfs.c
gdb/remote-sim.c
gdb/remote.c
gdb/rs6000-nat.c
gdb/target.h
gdb/windows-nat.c

index a5d07e64e96cc615250c2758f9bdb1e5b085b9c0..c8f1bcfbc53d7ce131689d14d210b55fd9cb7b7d 100644 (file)
@@ -1,3 +1,41 @@
+2017-04-12  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       * common/common-utils.c (free_vector_argv): New function.
+       * common/common-utils.h: Include <vector>.
+       (free_vector_argv): New prototype.
+       * darwin-nat.c (darwin_create_inferior): Rewrite function
+       prototype in order to constify "exec_file" and accept a
+       "std::string" for "allargs".
+       * fork-child.c: Include <vector>.
+       (breakup_args): Rewrite function, using C++.
+       (fork_inferior): Rewrite function header, constify "exec_file_arg"
+       and accept "std::string" for "allargs".  Update the code to
+       calculate "argv" based on "allargs".  Update calls to "exec_fun"
+       and "execvp".
+       * gnu-nat.c (gnu_create_inferior): Rewrite function prototype in
+       order to constify "exec_file" and accept a "std::string" for
+       "allargs".
+       * go32-nat.c (go32_create_inferior): Likewise.
+       * inf-ptrace.c (inf_ptrace_create_inferior): Likewise.
+       * infcmd.c (run_command_1): Constify "exec_file".  Use
+       "std::string" for inferior arguments.
+       * inferior.h (fork_inferior): Update prototype.
+       * linux-nat.c (linux_nat_create_inferior): Rewrite function
+       prototype in order to constify "exec_file" and accept a
+       "std::string" for "allargs".
+       * nto-procfs.c (procfs_create_inferior): Likewise.
+       * procfs.c (procfs_create_inferior): Likewise.
+       * remote-sim.c (gdbsim_create_inferior): Likewise.
+       * remote.c (extended_remote_run): Update code to accept
+       "std::string" as argument.
+       (extended_remote_create_inferior): Rewrite function prototype in
+       order to constify "exec_file" and accept a "std::string" for
+       "allargs".
+       * rs6000-nat.c (super_create_inferior): Likewise.
+       (rs6000_create_inferior): Likewise.
+       * target.h (struct target_ops) <to_create_inferior>: Likewise.
+       * windows-nat.c (windows_create_inferior): Likewise.
+
 2017-04-11  Pedro Alves  <palves@redhat.com>
 
        * thread.c: Fix whitespace throughout.
index afc0af9641c5fd747b1538df0c8c310bf3c058e8..e94fdc4cb3631bc6f422506de70a5ff3541834ac 100644 (file)
@@ -317,3 +317,14 @@ skip_to_space_const (const char *chp)
     chp++;
   return chp;
 }
+
+/* See common/common-utils.h.  */
+
+void
+free_vector_argv (std::vector<char *> &v)
+{
+  for (char *el : v)
+    xfree (el);
+
+  v.clear ();
+}
index 618d2663fab881aa8636318f855fad1fd84ef304..c331f0d62ff4419cb3fcf4da23a028536264be4f 100644 (file)
@@ -21,6 +21,7 @@
 #define COMMON_UTILS_H
 
 #include <string>
+#include <vector>
 
 /* If possible, define FUNCTION_NAME, a macro containing the name of
    the function being defined.  Since this macro may not always be
@@ -103,4 +104,8 @@ extern const char *skip_spaces_const (const char *inp);
 
 extern const char *skip_to_space_const (const char *inp);
 
+/* Assumes that V is an argv for a program, and iterates through
+   freeing all the elements.  */
+extern void free_vector_argv (std::vector<char *> &v);
+
 #endif
index e5092401f8746ce0cb3a17d8cd36fb6a9d9a14e6..cba84caec3b9589fed9262dbabadffb5a6bdf68a 100644 (file)
@@ -102,8 +102,10 @@ static void darwin_ptrace_me (void);
 
 static void darwin_ptrace_him (int pid);
 
-static void darwin_create_inferior (struct target_ops *ops, char *exec_file,
-                                   char *allargs, char **env, int from_tty);
+static void darwin_create_inferior (struct target_ops *ops,
+                                   const char *exec_file,
+                                   const std::string &allargs,
+                                   char **env, int from_tty);
 
 static void darwin_files_info (struct target_ops *ops);
 
@@ -1826,8 +1828,10 @@ darwin_execvp (const char *file, char * const argv[], char * const env[])
 }
 
 static void
-darwin_create_inferior (struct target_ops *ops, char *exec_file,
-                       char *allargs, char **env, int from_tty)
+darwin_create_inferior (struct target_ops *ops,
+                       const char *exec_file,
+                       const std::string &allargs,
+                       char **env, int from_tty)
 {
   /* Do the hard work.  */
   fork_inferior (exec_file, allargs, env, darwin_ptrace_me, darwin_ptrace_him,
index 04d2cdfbfac3f992c1b673612e7857dca7a19b0f..6b7386e07e25592b990c7698b3d3a910b8a0d5ad 100644 (file)
@@ -34,6 +34,7 @@
 #include "top.h"
 #include "signals-state-save-restore.h"
 #include <signal.h>
+#include <vector>
 
 /* This just gets used as a default if we can't find SHELL.  */
 #define SHELL_FILE "/bin/sh"
@@ -48,41 +49,32 @@ static char *exec_wrapper;
    fill in ARGV with the four arguments "a", "b", "c", "d".  */
 
 static void
-breakup_args (char *scratch, char **argv)
+breakup_args (const std::string &scratch, std::vector<char *> &argv)
 {
-  char *cp = scratch, *tmp;
-
-  for (;;)
+  for (size_t cur_pos = 0; cur_pos < scratch.size ();)
     {
-      /* Scan past leading separators */
-      while (*cp == ' ' || *cp == '\t' || *cp == '\n')
-       cp++;
-
-      /* Break if at end of string.  */
-      if (*cp == '\0')
-       break;
-
-      /* Take an arg.  */
-      *argv++ = cp;
-
-      /* Scan for next arg separator.  */
-      tmp = strchr (cp, ' ');
-      if (tmp == NULL)
-       tmp = strchr (cp, '\t');
-      if (tmp == NULL)
-       tmp = strchr (cp, '\n');
-
-      /* No separators => end of string => break.  */
-      if (tmp == NULL)
-       break;
-      cp = tmp;
-
-      /* Replace the separator with a terminator.  */
-      *cp++ = '\0';
+      /* Skip whitespace-like chars.  */
+      std::size_t pos = scratch.find_first_not_of (" \t\n", cur_pos);
+
+      if (pos != std::string::npos)
+       cur_pos = pos;
+
+      /* Find the position of the next separator.  */
+      std::size_t next_sep = scratch.find_first_of (" \t\n", cur_pos);
+
+      /* No separator found, which means this is the last
+        argument.  */
+      if (next_sep == std::string::npos)
+       next_sep = scratch.size ();
+
+      char *arg = savestring (scratch.c_str () + cur_pos, next_sep - cur_pos);
+      argv.push_back (arg);
+
+      cur_pos = next_sep;
     }
 
-  /* Null-terminate the vector.  */
-  *argv = NULL;
+  /* NULL-terminate the vector.  */
+  argv.push_back (NULL);
 }
 
 /* When executing a command under the given shell, return non-zero if
@@ -145,9 +137,10 @@ trace_start_error_with_name (const char *string)
    made static to ensure that they survive the vfork call.  */
 
 int
-fork_inferior (char *exec_file_arg, char *allargs, char **env,
-              void (*traceme_fun) (void), void (*init_trace_fun) (int),
-              void (*pre_trace_fun) (void), char *shell_file_arg,
+fork_inferior (const char *exec_file_arg, const std::string &allargs,
+              char **env, void (*traceme_fun) (void),
+              void (*init_trace_fun) (int), void (*pre_trace_fun) (void),
+              char *shell_file_arg,
                void (*exec_fun)(const char *file, char * const *argv,
                                 char * const *env))
 {
@@ -159,10 +152,10 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
      to you in the parent process.  It's only used by humans for debugging.  */
   static int debug_setpgrp = 657473;
   static char *shell_file;
-  static char *exec_file;
+  static const char *exec_file;
   char **save_our_env;
   int shell = 0;
-  static char **argv;
+  std::vector<char *> argv;
   const char *inferior_io_terminal = get_inferior_io_terminal ();
   struct inferior *inf;
   int i;
@@ -171,9 +164,10 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
 
   /* If no exec file handed to us, get it from the exec-file command
      -- with a good, common error message if none is specified.  */
-  exec_file = exec_file_arg;
-  if (exec_file == 0)
+  if (exec_file_arg == NULL)
     exec_file = get_exec_file (1);
+  else
+    exec_file = exec_file_arg;
 
   /* 'startup_with_shell' is declared in inferior.h and bound to the
      "set startup-with-shell" option.  If 0, we'll just do a
@@ -191,43 +185,26 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
 
   if (!shell)
     {
-      /* We're going to call execvp.  Create argument vector.
-        Calculate an upper bound on the length of the vector by
-        assuming that every other character is a separate
-        argument.  */
-      int argc = (strlen (allargs) + 1) / 2 + 2;
-
-      argv = XALLOCAVEC (char *, argc);
-      argv[0] = exec_file;
-      breakup_args (allargs, &argv[1]);
+      /* We're going to call execvp.  Create argument vector.  */
+      argv.push_back (xstrdup (exec_file));
+      breakup_args (allargs, argv);
     }
   else
     {
       /* We're going to call a shell.  */
-      char *shell_command;
-      int len;
-      char *p;
+      std::string shell_command;
+      const char *p;
       int need_to_quote;
       const int escape_bang = escape_bang_in_quoted_argument (shell_file);
 
-      /* Multiplying the length of exec_file by 4 is to account for the
-         fact that it may expand when quoted; it is a worst-case number
-         based on every character being '.  */
-      len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
-      if (exec_wrapper)
-        len += strlen (exec_wrapper) + 1;
-
-      shell_command = (char *) alloca (len);
-      shell_command[0] = '\0';
-
-      strcat (shell_command, "exec ");
+      shell_command = std::string ("exec ");
 
       /* Add any exec wrapper.  That may be a program name with arguments, so
         the user must handle quoting.  */
       if (exec_wrapper)
        {
-         strcat (shell_command, exec_wrapper);
-         strcat (shell_command, " ");
+         shell_command += exec_wrapper;
+         shell_command += ' ';
        }
 
       /* Now add exec_file, quoting as necessary.  */
@@ -268,33 +245,32 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
     end_scan:
       if (need_to_quote)
        {
-         strcat (shell_command, "'");
+         shell_command += '\'';
          for (p = exec_file; *p != '\0'; ++p)
            {
              if (*p == '\'')
-               strcat (shell_command, "'\\''");
+               shell_command += "'\\''";
              else if (*p == '!' && escape_bang)
-               strcat (shell_command, "\\!");
+               shell_command += "\\!";
              else
-               strncat (shell_command, p, 1);
+               shell_command += *p;
            }
-         strcat (shell_command, "'");
+         shell_command += '\'';
        }
       else
-       strcat (shell_command, exec_file);
+       shell_command += exec_file;
 
-      strcat (shell_command, " ");
-      strcat (shell_command, allargs);
+      shell_command += " " + allargs;
 
       /* If we decided above to start up with a shell, we exec the
         shell, "-c" says to interpret the next arg as a shell command
         to execute, and this command is "exec <target-program>
-        <args>".  */
-      argv = (char **) alloca (4 * sizeof (char *));
-      argv[0] = shell_file;
-      argv[1] = (char *) "-c";
-      argv[2] = shell_command;
-      argv[3] = (char *) 0;
+        <args>".  We xstrdup all the strings here because they will
+        be free'd later in the code.  */
+      argv.push_back (xstrdup (shell_file));
+      argv.push_back (xstrdup ("-c"));
+      argv.push_back (xstrdup (shell_command.c_str ()));
+      argv.push_back (NULL);
     }
 
   /* Retain a copy of our environment variables, since the child will
@@ -401,9 +377,9 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
       environ = env;
 
       if (exec_fun != NULL)
-        (*exec_fun) (argv[0], argv, env);
+        (*exec_fun) (argv[0], &argv[0], env);
       else
-        execvp (argv[0], argv);
+        execvp (argv[0], &argv[0]);
 
       /* If we get here, it's an error.  */
       save_errno = errno;
@@ -417,6 +393,8 @@ fork_inferior (char *exec_file_arg, char *allargs, char **env,
       _exit (0177);
     }
 
+  free_vector_argv (argv);
+
   /* Restore our environment in case a vforked child clob'd it.  */
   environ = save_our_env;
 
index 8b1adfe27096a4ed1418fe55605f173e7547b31e..1f8c5938b79ca8d3084fccae769536cc84413d01 100644 (file)
@@ -1,3 +1,12 @@
+2017-04-12  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       * server.c: Include <vector>.
+       <program_argv, wrapper_argv>: Convert to std::vector.
+       (start_inferior): Rewrite function to use C++.
+       (handle_v_run): Likewise.  Update code that calculates the argv
+       based on the vRun packet; use C++.
+       (captured_main): Likewise.
+
 2017-04-06  Simon Marchi  <simon.marchi@ericsson.com>
 
        * server.c (handle_v_cont): Initialize thread_resume::thread
index 9cc6145b051643883a4c1ff8e6f82ee6ce372a2c..69fcab128b4d0cc38a65f691c497ad177f21429b 100644 (file)
@@ -35,6 +35,7 @@
 #include "tracepoint.h"
 #include "dll.h"
 #include "hostio.h"
+#include <vector>
 
 /* The thread set with an `Hc' packet.  `Hc' is deprecated in favor of
    `vCont'.  Note the multi-process extensions made `vCont' a
@@ -78,7 +79,8 @@ static int vCont_supported;
    space randomization feature before starting an inferior.  */
 int disable_randomization = 1;
 
-static char **program_argv, **wrapper_argv;
+static std::vector<char *> program_argv;
+static std::vector<char *> wrapper_argv;
 
 int pass_signals[GDB_SIGNAL_LAST];
 int program_signals[GDB_SIGNAL_LAST];
@@ -239,29 +241,21 @@ target_running (void)
 static int
 start_inferior (char **argv)
 {
-  char **new_argv = argv;
+  std::vector<char *> new_argv;
 
-  if (wrapper_argv != NULL)
-    {
-      int i, count = 1;
-
-      for (i = 0; wrapper_argv[i] != NULL; i++)
-       count++;
-      for (i = 0; argv[i] != NULL; i++)
-       count++;
-      new_argv = XALLOCAVEC (char *, count);
-      count = 0;
-      for (i = 0; wrapper_argv[i] != NULL; i++)
-       new_argv[count++] = wrapper_argv[i];
-      for (i = 0; argv[i] != NULL; i++)
-       new_argv[count++] = argv[i];
-      new_argv[count] = NULL;
-    }
+  if (!wrapper_argv.empty ())
+    new_argv.insert (new_argv.begin (),
+                    wrapper_argv.begin (),
+                    wrapper_argv.end ());
+
+  for (int i = 0; argv[i] != NULL; ++i)
+    new_argv.push_back (argv[i]);
+
+  new_argv.push_back (NULL);
 
   if (debug_threads)
     {
-      int i;
-      for (i = 0; new_argv[i]; ++i)
+      for (int i = 0; i < new_argv.size (); ++i)
        debug_printf ("new_argv[%d] = \"%s\"\n", i, new_argv[i]);
       debug_flush ();
     }
@@ -271,7 +265,7 @@ start_inferior (char **argv)
   signal (SIGTTIN, SIG_DFL);
 #endif
 
-  signal_pid = create_inferior (new_argv[0], new_argv);
+  signal_pid = create_inferior (new_argv[0], &new_argv[0]);
 
   /* FIXME: we don't actually know at this point that the create
      actually succeeded.  We won't know that until we wait.  */
@@ -288,7 +282,7 @@ start_inferior (char **argv)
   atexit (restore_old_foreground_pgrp);
 #endif
 
-  if (wrapper_argv != NULL)
+  if (!wrapper_argv.empty ())
     {
       ptid_t ptid = pid_to_ptid (signal_pid);
 
@@ -2852,7 +2846,8 @@ handle_v_attach (char *own_buf)
 static int
 handle_v_run (char *own_buf)
 {
-  char *p, *next_p, **new_argv;
+  char *p, *next_p;
+  std::vector<char *> new_argv;
   int i, new_argc;
 
   new_argc = 0;
@@ -2862,62 +2857,51 @@ handle_v_run (char *own_buf)
       new_argc++;
     }
 
-  new_argv = (char **) calloc (new_argc + 2, sizeof (char *));
-  if (new_argv == NULL)
-    {
-      write_enn (own_buf);
-      return 0;
-    }
-
-  i = 0;
-  for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
+  for (i = 0, p = own_buf + strlen ("vRun;"); *p; p = next_p, ++i)
     {
       next_p = strchr (p, ';');
       if (next_p == NULL)
        next_p = p + strlen (p);
 
       if (i == 0 && p == next_p)
-       new_argv[i] = NULL;
+       {
+         /* No program specified.  */
+         new_argv.push_back (NULL);
+       }
       else
        {
-         /* FIXME: Fail request if out of memory instead of dying.  */
-         new_argv[i] = (char *) xmalloc (1 + (next_p - p) / 2);
-         hex2bin (p, (gdb_byte *) new_argv[i], (next_p - p) / 2);
-         new_argv[i][(next_p - p) / 2] = '\0';
+         size_t len = (next_p - p) / 2;
+         char *arg = (char *) xmalloc (len + 1);
+
+         hex2bin (p, (gdb_byte *) arg, len);
+         arg[len] = '\0';
+         new_argv.push_back (arg);
        }
 
       if (*next_p)
        next_p++;
-      i++;
     }
-  new_argv[i] = NULL;
+  new_argv.push_back (NULL);
 
   if (new_argv[0] == NULL)
     {
       /* GDB didn't specify a program to run.  Use the program from the
         last run with the new argument list.  */
-
-      if (program_argv == NULL)
+      if (program_argv.empty ())
        {
          write_enn (own_buf);
-         freeargv (new_argv);
+         free_vector_argv (new_argv);
          return 0;
        }
 
-      new_argv[0] = strdup (program_argv[0]);
-      if (new_argv[0] == NULL)
-       {
-         write_enn (own_buf);
-         freeargv (new_argv);
-         return 0;
-       }
+      new_argv.push_back (xstrdup (program_argv[0]));
     }
 
   /* Free the old argv and install the new one.  */
-  freeargv (program_argv);
+  free_vector_argv (program_argv);
   program_argv = new_argv;
 
-  start_inferior (program_argv);
+  start_inferior (&program_argv[0]);
   if (last_status.kind == TARGET_WAITKIND_STOPPED)
     {
       prepare_resume_reply (own_buf, last_ptid, &last_status);
@@ -3536,13 +3520,18 @@ captured_main (int argc, char *argv[])
        multi_mode = 1;
       else if (strcmp (*next_arg, "--wrapper") == 0)
        {
+         char **tmp;
+
          next_arg++;
 
-         wrapper_argv = next_arg;
+         tmp = next_arg;
          while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
-           next_arg++;
+           {
+             wrapper_argv.push_back (*next_arg);
+             next_arg++;
+           }
 
-         if (next_arg == wrapper_argv || *next_arg == NULL)
+         if (next_arg == tmp || *next_arg == NULL)
            {
              gdbserver_usage (stderr);
              exit (1);
@@ -3692,13 +3681,12 @@ captured_main (int argc, char *argv[])
       int i, n;
 
       n = argc - (next_arg - argv);
-      program_argv = XNEWVEC (char *, n + 1);
       for (i = 0; i < n; i++)
-       program_argv[i] = xstrdup (next_arg[i]);
-      program_argv[i] = NULL;
+       program_argv.push_back (xstrdup (next_arg[i]));
+      program_argv.push_back (NULL);
 
       /* Wait till we are at first instruction in program.  */
-      start_inferior (program_argv);
+      start_inferior (&program_argv[0]);
 
       /* We are now (hopefully) stopped at the first instruction of
         the target process.  This assumes that the target process was
@@ -4313,9 +4301,9 @@ process_serial_event (void)
          fprintf (stderr, "GDBserver restarting\n");
 
          /* Wait till we are at 1st instruction in prog.  */
-         if (program_argv != NULL)
+         if (!program_argv.empty ())
            {
-             start_inferior (program_argv);
+             start_inferior (&program_argv[0]);
              if (last_status.kind == TARGET_WAITKIND_STOPPED)
                {
                  /* Stopped at the first instruction of the target
index b2a975dead75d3e712817f7c6a77fd1610ccfb03..6298103e45e7375745f5ec2afc2204498d662ee1 100644 (file)
@@ -2131,7 +2131,8 @@ gnu_ptrace_me (void)
 
 static void
 gnu_create_inferior (struct target_ops *ops, 
-                    char *exec_file, char *allargs, char **env,
+                    const char *exec_file, const std::string &allargs,
+                    char **env,
                     int from_tty)
 {
   struct inf *inf = cur_inf ();
index a20067e7ef7ae052c49cc4ba451a4025d630f452..4e609a87e7a7e58a0f170f258461b452bee0d8fb 100644 (file)
@@ -631,8 +631,9 @@ go32_kill_inferior (struct target_ops *ops)
 }
 
 static void
-go32_create_inferior (struct target_ops *ops, char *exec_file,
-                     char *args, char **env, int from_tty)
+go32_create_inferior (struct target_ops *ops,
+                     const char *exec_file,
+                     const std::string &allargs, char **env, int from_tty)
 {
   extern char **environ;
   jmp_buf start_state;
@@ -641,6 +642,7 @@ go32_create_inferior (struct target_ops *ops, char *exec_file,
   size_t cmdlen;
   struct inferior *inf;
   int result;
+  const char *args = allargs.c_str ();
 
   /* If no exec file handed to us, get it from the exec-file command -- with
      a good, common error message if none is specified.  */
index c91100134b108def656d25b876687c315f0b68b5..b19aaf9e29b9f8b410f7a28746fee00b462a7f03 100644 (file)
@@ -90,8 +90,8 @@ inf_ptrace_me (void)
 
 static void
 inf_ptrace_create_inferior (struct target_ops *ops,
-                           char *exec_file, char *allargs, char **env,
-                           int from_tty)
+                           const char *exec_file, const std::string &allargs,
+                           char **env, int from_tty)
 {
   int pid;
 
index f6ffb9ba18631fdc76eead426022a21afb7182fc..22b2c7aadecc18b81f7da4a8e4f1a4b8d45434d1 100644 (file)
@@ -526,7 +526,7 @@ prepare_execution_command (struct target_ops *target, int background)
 static void
 run_command_1 (char *args, int from_tty, int tbreak_at_main)
 {
-  char *exec_file;
+  const char *exec_file;
   struct cleanup *old_chain;
   ptid_t ptid;
   struct ui_out *uiout = current_uiout;
@@ -574,7 +574,7 @@ run_command_1 (char *args, int from_tty, int tbreak_at_main)
   if (tbreak_at_main)
     tbreak_command (main_name (), 0);
 
-  exec_file = (char *) get_exec_file (0);
+  exec_file = get_exec_file (0);
 
   /* We keep symbols from add-symbol-file, on the grounds that the
      user might want to add some symbols before running the program
@@ -607,7 +607,8 @@ run_command_1 (char *args, int from_tty, int tbreak_at_main)
 
   /* We call get_inferior_args() because we might need to compute
      the value now.  */
-  run_target->to_create_inferior (run_target, exec_file, get_inferior_args (),
+  run_target->to_create_inferior (run_target, exec_file,
+                                 std::string (get_inferior_args ()),
                                  environ_vector (current_inferior ()->environment),
                                  from_tty);
   /* to_create_inferior should push the target, so after this point we
index 7c0ddf37f1e0bdea33833916fd2718ce3eb3076b..bf06ac1c7ef4efeaa07ccec2b8d464a09689b458 100644 (file)
@@ -146,7 +146,7 @@ extern void trace_start_error (const char *fmt, ...)
 extern void trace_start_error_with_name (const char *string)
   ATTRIBUTE_NORETURN;
 
-extern int fork_inferior (char *, char *, char **,
+extern int fork_inferior (const char *, const std::string &, char **,
                          void (*)(void),
                          void (*)(int), void (*)(void), char *,
                           void (*)(const char *,
index 8dececf2c52d206867c26cb08af4ee356bb3d593..b008df0ac06cf8e3d81d22ce251182f0576a876f 100644 (file)
@@ -1105,8 +1105,8 @@ linux_nat_post_attach_wait (ptid_t ptid, int first, int *signalled)
 
 static void
 linux_nat_create_inferior (struct target_ops *ops, 
-                          char *exec_file, char *allargs, char **env,
-                          int from_tty)
+                          const char *exec_file, const std::string &allargs,
+                          char **env, int from_tty)
 {
   struct cleanup *restore_personality
     = maybe_disable_address_space_randomization (disable_randomization);
index 75f92dcf7805c4c30b96d458469e7ae2de2c6d29..7fb7095745fda2d15a32ce5e424018a357fa6fa7 100644 (file)
@@ -1163,8 +1163,9 @@ breakup_args (char *scratch, char **argv)
 }
 
 static void
-procfs_create_inferior (struct target_ops *ops, char *exec_file,
-                       char *allargs, char **env, int from_tty)
+procfs_create_inferior (struct target_ops *ops, const char *exec_file,
+                       const std::string &allargs,
+                       char **env, int from_tty)
 {
   struct inheritance inherit;
   pid_t pid;
@@ -1176,7 +1177,7 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
   const char *inferior_io_terminal = get_inferior_io_terminal ();
   struct inferior *inf;
 
-  argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
+  argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
                  sizeof (*argv));
   argv[0] = get_exec_file (1);
   if (!argv[0])
@@ -1187,7 +1188,7 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
        return;
     }
 
-  args = xstrdup (allargs);
+  args = xstrdup (allargs.c_str ());
   breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
 
   argv = nto_parse_redirection (argv, &in, &out, &err);
index 87c317f375866db9a4d9da225078aace538c29e6..5d940ddbae235e4bf6dc526d6390984ae4e6e39f 100644 (file)
@@ -4526,8 +4526,8 @@ procfs_set_exec_trap (void)
    inf-ptrace?  */
 
 static void
-procfs_create_inferior (struct target_ops *ops, char *exec_file,
-                       char *allargs, char **env, int from_tty)
+procfs_create_inferior (struct target_ops *ops, const char *exec_file,
+                       const std::string &allargs, char **env, int from_tty)
 {
   char *shell_file = getenv ("SHELL");
   char *tryname;
index 4396d4bcfe4dec20d03e68549b3503a719dd5133..fd1bc66642389a4879171af8eeee30a5df7cbaec 100644 (file)
@@ -607,13 +607,14 @@ gdbsim_load (struct target_ops *self, const char *args, int fromtty)
    user types "run" after having attached.  */
 
 static void
-gdbsim_create_inferior (struct target_ops *target, char *exec_file, char *args,
-                       char **env, int from_tty)
+gdbsim_create_inferior (struct target_ops *target, const char *exec_file,
+                       const std::string &allargs, char **env, int from_tty)
 {
   struct sim_inferior_data *sim_data
     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
   int len;
   char *arg_buf, **argv;
+  const char *args = allargs.c_str ();
 
   if (exec_file == 0 || exec_bfd == 0)
     warning (_("No executable file specified."));
@@ -633,7 +634,7 @@ gdbsim_create_inferior (struct target_ops *target, char *exec_file, char *args,
 
   if (exec_file != NULL)
     {
-      len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10;
+      len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10;
       arg_buf = (char *) alloca (len);
       arg_buf[0] = '\0';
       strcat (arg_buf, exec_file);
index a61469c987258e3149543a547ec1e4e2117ca2e8..7c5fe87da8ab167ea5b4111ec42cbe71a6ad5ea9 100644 (file)
@@ -9526,7 +9526,7 @@ extended_remote_disable_randomization (int val)
 }
 
 static int
-extended_remote_run (char *args)
+extended_remote_run (const std::string &args)
 {
   struct remote_state *rs = get_remote_state ();
   int len;
@@ -9545,14 +9545,13 @@ extended_remote_run (char *args)
   len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
                      strlen (remote_exec_file));
 
-  gdb_assert (args != NULL);
-  if (*args)
+  if (!args.empty ())
     {
       struct cleanup *back_to;
       int i;
       char **argv;
 
-      argv = gdb_buildargv (args);
+      argv = gdb_buildargv (args.c_str ());
       back_to = make_cleanup_freeargv (argv);
       for (i = 0; argv[i] != NULL; i++)
        {
@@ -9597,7 +9596,8 @@ extended_remote_run (char *args)
 
 static void
 extended_remote_create_inferior (struct target_ops *ops,
-                                char *exec_file, char *args,
+                                const char *exec_file,
+                                const std::string &args,
                                 char **env, int from_tty)
 {
   int run_worked;
@@ -9622,7 +9622,7 @@ extended_remote_create_inferior (struct target_ops *ops,
         user requested.  */
       if (remote_exec_file[0])
        error (_("Remote target does not support \"set remote exec-file\""));
-      if (args[0])
+      if (!args.empty ())
        error (_("Remote target does not support \"set args\" or run <ARGS>"));
 
       /* Fall back to "R".  */
index 8f5d25cdb3093a4b66296361b948a7e9594da4f7..83e0693d9834cf97863b038e4778405692220265 100644 (file)
@@ -524,11 +524,13 @@ rs6000_wait (struct target_ops *ops,
 /* Set the current architecture from the host running GDB.  Called when
    starting a child process.  */
 
-static void (*super_create_inferior) (struct target_ops *,char *exec_file, 
-                                     char *allargs, char **env, int from_tty);
+static void (*super_create_inferior) (struct target_ops *,
+                                     const char *exec_file,
+                                     const std::string &allargs,
+                                     char **env, int from_tty);
 static void
-rs6000_create_inferior (struct target_ops * ops, char *exec_file,
-                       char *allargs, char **env, int from_tty)
+rs6000_create_inferior (struct target_ops * ops, const char *exec_file,
+                       const std::string &allargs, char **env, int from_tty)
 {
   enum bfd_architecture arch;
   unsigned long mach;
index fac9d51c017e41ecbf267e8bb89154fdc0604a9b..a971adf8c6cff4d866a649d170ed5c9ea41b6c1b 100644 (file)
@@ -591,7 +591,8 @@ struct target_ops
        ENV is the environment vector to pass.  Errors reported with error().
        On VxWorks and various standalone systems, we ignore exec_file.  */
     void (*to_create_inferior) (struct target_ops *, 
-                               char *, char *, char **, int);
+                               const char *, const std::string &,
+                               char **, int);
     void (*to_post_startup_inferior) (struct target_ops *, ptid_t)
       TARGET_DEFAULT_IGNORE ();
     int (*to_insert_fork_catchpoint) (struct target_ops *, int)
index 21b5ebefd8df017d0cc66dfaa11e3c28afb6e5ae..dd5cfe4cad453b4b0d761324dde0fff8fcd7d4d8 100644 (file)
@@ -2412,8 +2412,9 @@ redirect_inferior_handles (const char *cmd_orig, char *cmd,
    ENV is the environment vector to pass.  Errors reported with error().  */
 
 static void
-windows_create_inferior (struct target_ops *ops, char *exec_file,
-                      char *allargs, char **in_env, int from_tty)
+windows_create_inferior (struct target_ops *ops, const char *exec_file,
+                        const std::string &origallargs, char **in_env,
+                        int from_tty)
 {
   STARTUPINFO si;
 #ifdef __CYGWIN__
@@ -2432,6 +2433,7 @@ windows_create_inferior (struct target_ops *ops, char *exec_file,
   char real_path[__PMAX];
   char shell[__PMAX]; /* Path to shell */
   char *toexec;
+  const char *allargs = origallargs.c_str ();
   char *args, *allargs_copy;
   size_t args_len, allargs_len;
   int fd_inp = -1, fd_out = -1, fd_err = -1;
This page took 0.070155 seconds and 4 git commands to generate.