xref: /dflybsd-src/contrib/gdb-7/gdb/fork-child.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Fork a Unix child process, and set up to debug it, for GDB.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4*cf7f2e2dSJohn Marino    2001, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5*cf7f2e2dSJohn Marino    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    Contributed by Cygnus Support.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This file is part of GDB.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
125796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
135796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
145796c8dcSSimon Schubert    (at your option) any later version.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
175796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
185796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
195796c8dcSSimon Schubert    GNU General Public License for more details.
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
225796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert #include "defs.h"
255796c8dcSSimon Schubert #include "gdb_string.h"
265796c8dcSSimon Schubert #include "inferior.h"
275796c8dcSSimon Schubert #include "terminal.h"
285796c8dcSSimon Schubert #include "target.h"
295796c8dcSSimon Schubert #include "gdb_wait.h"
305796c8dcSSimon Schubert #include "gdb_vfork.h"
315796c8dcSSimon Schubert #include "gdbcore.h"
325796c8dcSSimon Schubert #include "terminal.h"
335796c8dcSSimon Schubert #include "gdbthread.h"
345796c8dcSSimon Schubert #include "command.h" /* for dont_repeat () */
355796c8dcSSimon Schubert #include "gdbcmd.h"
365796c8dcSSimon Schubert #include "solib.h"
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #include <signal.h>
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert /* This just gets used as a default if we can't find SHELL.  */
415796c8dcSSimon Schubert #define SHELL_FILE "/bin/sh"
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert extern char **environ;
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert static char *exec_wrapper;
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert /* Break up SCRATCH into an argument vector suitable for passing to
485796c8dcSSimon Schubert    execvp and store it in ARGV.  E.g., on "run a b c d" this routine
495796c8dcSSimon Schubert    would get as input the string "a b c d", and as output it would
505796c8dcSSimon Schubert    fill in ARGV with the four arguments "a", "b", "c", "d".  */
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert static void
535796c8dcSSimon Schubert breakup_args (char *scratch, char **argv)
545796c8dcSSimon Schubert {
555796c8dcSSimon Schubert   char *cp = scratch;
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert   for (;;)
585796c8dcSSimon Schubert     {
595796c8dcSSimon Schubert       /* Scan past leading separators */
605796c8dcSSimon Schubert       while (*cp == ' ' || *cp == '\t' || *cp == '\n')
615796c8dcSSimon Schubert 	cp++;
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert       /* Break if at end of string.  */
645796c8dcSSimon Schubert       if (*cp == '\0')
655796c8dcSSimon Schubert 	break;
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert       /* Take an arg.  */
685796c8dcSSimon Schubert       *argv++ = cp;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert       /* Scan for next arg separator.  */
715796c8dcSSimon Schubert       cp = strchr (cp, ' ');
725796c8dcSSimon Schubert       if (cp == NULL)
735796c8dcSSimon Schubert 	cp = strchr (cp, '\t');
745796c8dcSSimon Schubert       if (cp == NULL)
755796c8dcSSimon Schubert 	cp = strchr (cp, '\n');
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert       /* No separators => end of string => break.  */
785796c8dcSSimon Schubert       if (cp == NULL)
795796c8dcSSimon Schubert 	break;
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert       /* Replace the separator with a terminator.  */
825796c8dcSSimon Schubert       *cp++ = '\0';
835796c8dcSSimon Schubert     }
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert   /* Null-terminate the vector.  */
865796c8dcSSimon Schubert   *argv = NULL;
875796c8dcSSimon Schubert }
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert /* When executing a command under the given shell, return non-zero if
905796c8dcSSimon Schubert    the '!' character should be escaped when embedded in a quoted
915796c8dcSSimon Schubert    command-line argument.  */
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert static int
945796c8dcSSimon Schubert escape_bang_in_quoted_argument (const char *shell_file)
955796c8dcSSimon Schubert {
965796c8dcSSimon Schubert   const int shell_file_len = strlen (shell_file);
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert   /* Bang should be escaped only in C Shells.  For now, simply check
995796c8dcSSimon Schubert      that the shell name ends with 'csh', which covers at least csh
1005796c8dcSSimon Schubert      and tcsh.  This should be good enough for now.  */
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert   if (shell_file_len < 3)
1035796c8dcSSimon Schubert     return 0;
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert   if (shell_file[shell_file_len - 3] == 'c'
1065796c8dcSSimon Schubert       && shell_file[shell_file_len - 2] == 's'
1075796c8dcSSimon Schubert       && shell_file[shell_file_len - 1] == 'h')
1085796c8dcSSimon Schubert     return 1;
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert   return 0;
1115796c8dcSSimon Schubert }
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert /* Start an inferior Unix child process and sets inferior_ptid to its
1145796c8dcSSimon Schubert    pid.  EXEC_FILE is the file to run.  ALLARGS is a string containing
1155796c8dcSSimon Schubert    the arguments to the program.  ENV is the environment vector to
1165796c8dcSSimon Schubert    pass.  SHELL_FILE is the shell file, or NULL if we should pick
1175796c8dcSSimon Schubert    one.  */
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert /* This function is NOT reentrant.  Some of the variables have been
1205796c8dcSSimon Schubert    made static to ensure that they survive the vfork call.  */
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert int
1235796c8dcSSimon Schubert fork_inferior (char *exec_file_arg, char *allargs, char **env,
1245796c8dcSSimon Schubert 	       void (*traceme_fun) (void), void (*init_trace_fun) (int),
1255796c8dcSSimon Schubert 	       void (*pre_trace_fun) (void), char *shell_file_arg)
1265796c8dcSSimon Schubert {
1275796c8dcSSimon Schubert   int pid;
1285796c8dcSSimon Schubert   char *shell_command;
1295796c8dcSSimon Schubert   static char default_shell_file[] = SHELL_FILE;
1305796c8dcSSimon Schubert   int len;
1315796c8dcSSimon Schubert   /* Set debug_fork then attach to the child while it sleeps, to debug. */
1325796c8dcSSimon Schubert   static int debug_fork = 0;
1335796c8dcSSimon Schubert   /* This is set to the result of setpgrp, which if vforked, will be visible
1345796c8dcSSimon Schubert      to you in the parent process.  It's only used by humans for debugging.  */
1355796c8dcSSimon Schubert   static int debug_setpgrp = 657473;
1365796c8dcSSimon Schubert   static char *shell_file;
1375796c8dcSSimon Schubert   static char *exec_file;
1385796c8dcSSimon Schubert   char **save_our_env;
1395796c8dcSSimon Schubert   int shell = 0;
1405796c8dcSSimon Schubert   static char **argv;
1415796c8dcSSimon Schubert   const char *inferior_io_terminal = get_inferior_io_terminal ();
142*cf7f2e2dSJohn Marino   struct inferior *inf;
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert   /* If no exec file handed to us, get it from the exec-file command
1455796c8dcSSimon Schubert      -- with a good, common error message if none is specified.  */
1465796c8dcSSimon Schubert   exec_file = exec_file_arg;
1475796c8dcSSimon Schubert   if (exec_file == 0)
1485796c8dcSSimon Schubert     exec_file = get_exec_file (1);
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   /* STARTUP_WITH_SHELL is defined in inferior.h.  If 0,e we'll just
1515796c8dcSSimon Schubert     do a fork/exec, no shell, so don't bother figuring out what
1525796c8dcSSimon Schubert     shell.  */
1535796c8dcSSimon Schubert   shell_file = shell_file_arg;
1545796c8dcSSimon Schubert   if (STARTUP_WITH_SHELL)
1555796c8dcSSimon Schubert     {
1565796c8dcSSimon Schubert       /* Figure out what shell to start up the user program under.  */
1575796c8dcSSimon Schubert       if (shell_file == NULL)
1585796c8dcSSimon Schubert 	shell_file = getenv ("SHELL");
1595796c8dcSSimon Schubert       if (shell_file == NULL)
1605796c8dcSSimon Schubert 	shell_file = default_shell_file;
1615796c8dcSSimon Schubert       shell = 1;
1625796c8dcSSimon Schubert     }
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert   /* Multiplying the length of exec_file by 4 is to account for the
1655796c8dcSSimon Schubert      fact that it may expand when quoted; it is a worst-case number
1665796c8dcSSimon Schubert      based on every character being '.  */
1675796c8dcSSimon Schubert   len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
1685796c8dcSSimon Schubert   if (exec_wrapper)
1695796c8dcSSimon Schubert     len += strlen (exec_wrapper) + 1;
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert   shell_command = (char *) alloca (len);
1725796c8dcSSimon Schubert   shell_command[0] = '\0';
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert   if (!shell)
1755796c8dcSSimon Schubert     {
1765796c8dcSSimon Schubert       /* We're going to call execvp.  Create argument vector.
1775796c8dcSSimon Schubert 	 Calculate an upper bound on the length of the vector by
1785796c8dcSSimon Schubert 	 assuming that every other character is a separate
1795796c8dcSSimon Schubert 	 argument.  */
1805796c8dcSSimon Schubert       int argc = (strlen (allargs) + 1) / 2 + 2;
181*cf7f2e2dSJohn Marino 
1825796c8dcSSimon Schubert       argv = (char **) xmalloc (argc * sizeof (*argv));
1835796c8dcSSimon Schubert       argv[0] = exec_file;
1845796c8dcSSimon Schubert       breakup_args (allargs, &argv[1]);
1855796c8dcSSimon Schubert     }
1865796c8dcSSimon Schubert   else
1875796c8dcSSimon Schubert     {
1885796c8dcSSimon Schubert       /* We're going to call a shell.  */
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert       char *p;
1915796c8dcSSimon Schubert       int need_to_quote;
1925796c8dcSSimon Schubert       const int escape_bang = escape_bang_in_quoted_argument (shell_file);
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert       strcat (shell_command, "exec ");
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert       /* Add any exec wrapper.  That may be a program name with arguments, so
1975796c8dcSSimon Schubert 	 the user must handle quoting.  */
1985796c8dcSSimon Schubert       if (exec_wrapper)
1995796c8dcSSimon Schubert 	{
2005796c8dcSSimon Schubert 	  strcat (shell_command, exec_wrapper);
2015796c8dcSSimon Schubert 	  strcat (shell_command, " ");
2025796c8dcSSimon Schubert 	}
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert       /* Now add exec_file, quoting as necessary.  */
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert       /* Quoting in this style is said to work with all shells.  But
2075796c8dcSSimon Schubert          csh on IRIX 4.0.1 can't deal with it.  So we only quote it if
2085796c8dcSSimon Schubert          we need to.  */
2095796c8dcSSimon Schubert       p = exec_file;
2105796c8dcSSimon Schubert       while (1)
2115796c8dcSSimon Schubert 	{
2125796c8dcSSimon Schubert 	  switch (*p)
2135796c8dcSSimon Schubert 	    {
2145796c8dcSSimon Schubert 	    case '\'':
2155796c8dcSSimon Schubert 	    case '!':
2165796c8dcSSimon Schubert 	    case '"':
2175796c8dcSSimon Schubert 	    case '(':
2185796c8dcSSimon Schubert 	    case ')':
2195796c8dcSSimon Schubert 	    case '$':
2205796c8dcSSimon Schubert 	    case '&':
2215796c8dcSSimon Schubert 	    case ';':
2225796c8dcSSimon Schubert 	    case '<':
2235796c8dcSSimon Schubert 	    case '>':
2245796c8dcSSimon Schubert 	    case ' ':
2255796c8dcSSimon Schubert 	    case '\n':
2265796c8dcSSimon Schubert 	    case '\t':
2275796c8dcSSimon Schubert 	      need_to_quote = 1;
2285796c8dcSSimon Schubert 	      goto end_scan;
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert 	    case '\0':
2315796c8dcSSimon Schubert 	      need_to_quote = 0;
2325796c8dcSSimon Schubert 	      goto end_scan;
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert 	    default:
2355796c8dcSSimon Schubert 	      break;
2365796c8dcSSimon Schubert 	    }
2375796c8dcSSimon Schubert 	  ++p;
2385796c8dcSSimon Schubert 	}
2395796c8dcSSimon Schubert     end_scan:
2405796c8dcSSimon Schubert       if (need_to_quote)
2415796c8dcSSimon Schubert 	{
2425796c8dcSSimon Schubert 	  strcat (shell_command, "'");
2435796c8dcSSimon Schubert 	  for (p = exec_file; *p != '\0'; ++p)
2445796c8dcSSimon Schubert 	    {
2455796c8dcSSimon Schubert 	      if (*p == '\'')
2465796c8dcSSimon Schubert 		strcat (shell_command, "'\\''");
2475796c8dcSSimon Schubert 	      else if (*p == '!' && escape_bang)
2485796c8dcSSimon Schubert 		strcat (shell_command, "\\!");
2495796c8dcSSimon Schubert 	      else
2505796c8dcSSimon Schubert 		strncat (shell_command, p, 1);
2515796c8dcSSimon Schubert 	    }
2525796c8dcSSimon Schubert 	  strcat (shell_command, "'");
2535796c8dcSSimon Schubert 	}
2545796c8dcSSimon Schubert       else
2555796c8dcSSimon Schubert 	strcat (shell_command, exec_file);
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert       strcat (shell_command, " ");
2585796c8dcSSimon Schubert       strcat (shell_command, allargs);
2595796c8dcSSimon Schubert     }
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert   /* On some systems an exec will fail if the executable is open.  */
2625796c8dcSSimon Schubert   close_exec_file ();
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert   /* Retain a copy of our environment variables, since the child will
2655796c8dcSSimon Schubert      replace the value of environ and if we're vforked, we have to
2665796c8dcSSimon Schubert      restore it.  */
2675796c8dcSSimon Schubert   save_our_env = environ;
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert   /* Tell the terminal handling subsystem what tty we plan to run on;
2705796c8dcSSimon Schubert      it will just record the information for later.  */
2715796c8dcSSimon Schubert   new_tty_prefork (inferior_io_terminal);
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert   /* It is generally good practice to flush any possible pending stdio
2745796c8dcSSimon Schubert      output prior to doing a fork, to avoid the possibility of both
2755796c8dcSSimon Schubert      the parent and child flushing the same data after the fork. */
2765796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
2775796c8dcSSimon Schubert   gdb_flush (gdb_stderr);
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert   /* If there's any initialization of the target layers that must
2805796c8dcSSimon Schubert      happen to prepare to handle the child we're about fork, do it
2815796c8dcSSimon Schubert      now...  */
2825796c8dcSSimon Schubert   if (pre_trace_fun != NULL)
2835796c8dcSSimon Schubert     (*pre_trace_fun) ();
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert   /* Create the child process.  Since the child process is going to
2865796c8dcSSimon Schubert      exec(3) shortly afterwards, try to reduce the overhead by
2875796c8dcSSimon Schubert      calling vfork(2).  However, if PRE_TRACE_FUN is non-null, it's
2885796c8dcSSimon Schubert      likely that this optimization won't work since there's too much
2895796c8dcSSimon Schubert      work to do between the vfork(2) and the exec(3).  This is known
2905796c8dcSSimon Schubert      to be the case on ttrace(2)-based HP-UX, where some handshaking
2915796c8dcSSimon Schubert      between parent and child needs to happen between fork(2) and
2925796c8dcSSimon Schubert      exec(2).  However, since the parent is suspended in the vforked
2935796c8dcSSimon Schubert      state, this doesn't work.  Also note that the vfork(2) call might
2945796c8dcSSimon Schubert      actually be a call to fork(2) due to the fact that autoconf will
2955796c8dcSSimon Schubert      ``#define vfork fork'' on certain platforms.  */
2965796c8dcSSimon Schubert   if (pre_trace_fun || debug_fork)
2975796c8dcSSimon Schubert     pid = fork ();
2985796c8dcSSimon Schubert   else
2995796c8dcSSimon Schubert     pid = vfork ();
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert   if (pid < 0)
3025796c8dcSSimon Schubert     perror_with_name (("vfork"));
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert   if (pid == 0)
3055796c8dcSSimon Schubert     {
3065796c8dcSSimon Schubert       if (debug_fork)
3075796c8dcSSimon Schubert 	sleep (debug_fork);
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert       /* Create a new session for the inferior process, if necessary.
3105796c8dcSSimon Schubert          It will also place the inferior in a separate process group.  */
3115796c8dcSSimon Schubert       if (create_tty_session () <= 0)
3125796c8dcSSimon Schubert 	{
3135796c8dcSSimon Schubert 	  /* No session was created, but we still want to run the inferior
3145796c8dcSSimon Schubert 	     in a separate process group.  */
3155796c8dcSSimon Schubert 	  debug_setpgrp = gdb_setpgid ();
3165796c8dcSSimon Schubert 	  if (debug_setpgrp == -1)
3175796c8dcSSimon Schubert 	    perror ("setpgrp failed in child");
3185796c8dcSSimon Schubert 	}
3195796c8dcSSimon Schubert 
3205796c8dcSSimon Schubert       /* Ask the tty subsystem to switch to the one we specified
3215796c8dcSSimon Schubert          earlier (or to share the current terminal, if none was
3225796c8dcSSimon Schubert          specified).  */
3235796c8dcSSimon Schubert       new_tty ();
3245796c8dcSSimon Schubert 
3255796c8dcSSimon Schubert       /* Changing the signal handlers for the inferior after
3265796c8dcSSimon Schubert          a vfork can also change them for the superior, so we don't mess
3275796c8dcSSimon Schubert          with signals here.  See comments in
3285796c8dcSSimon Schubert          initialize_signals for how we get the right signal handlers
3295796c8dcSSimon Schubert          for the inferior.  */
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert       /* "Trace me, Dr. Memory!" */
3325796c8dcSSimon Schubert       (*traceme_fun) ();
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert       /* The call above set this process (the "child") as debuggable
3355796c8dcSSimon Schubert         by the original gdb process (the "parent").  Since processes
3365796c8dcSSimon Schubert         (unlike people) can have only one parent, if you are debugging
3375796c8dcSSimon Schubert         gdb itself (and your debugger is thus _already_ the
3385796c8dcSSimon Schubert         controller/parent for this child), code from here on out is
3395796c8dcSSimon Schubert         undebuggable.  Indeed, you probably got an error message
3405796c8dcSSimon Schubert         saying "not parent".  Sorry; you'll have to use print
3415796c8dcSSimon Schubert         statements!  */
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert       /* There is no execlpe call, so we have to set the environment
3445796c8dcSSimon Schubert          for our child in the global variable.  If we've vforked, this
3455796c8dcSSimon Schubert          clobbers the parent, but environ is restored a few lines down
3465796c8dcSSimon Schubert          in the parent.  By the way, yes we do need to look down the
3475796c8dcSSimon Schubert          path to find $SHELL.  Rich Pixley says so, and I agree.  */
3485796c8dcSSimon Schubert       environ = env;
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert       /* If we decided above to start up with a shell, we exec the
3515796c8dcSSimon Schubert 	 shell, "-c" says to interpret the next arg as a shell command
3525796c8dcSSimon Schubert 	 to execute, and this command is "exec <target-program>
3535796c8dcSSimon Schubert 	 <args>".  */
3545796c8dcSSimon Schubert       if (shell)
3555796c8dcSSimon Schubert 	{
3565796c8dcSSimon Schubert 	  execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert 	  /* If we get here, it's an error.  */
3595796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
3605796c8dcSSimon Schubert 			      safe_strerror (errno));
3615796c8dcSSimon Schubert 	  gdb_flush (gdb_stderr);
3625796c8dcSSimon Schubert 	  _exit (0177);
3635796c8dcSSimon Schubert 	}
3645796c8dcSSimon Schubert       else
3655796c8dcSSimon Schubert 	{
3665796c8dcSSimon Schubert 	  /* Otherwise, we directly exec the target program with
3675796c8dcSSimon Schubert 	     execvp.  */
3685796c8dcSSimon Schubert 	  int i;
3695796c8dcSSimon Schubert 	  char *errstring;
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert 	  execvp (exec_file, argv);
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert 	  /* If we get here, it's an error.  */
3745796c8dcSSimon Schubert 	  errstring = safe_strerror (errno);
3755796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert 	  i = 1;
3785796c8dcSSimon Schubert 	  while (argv[i] != NULL)
3795796c8dcSSimon Schubert 	    {
3805796c8dcSSimon Schubert 	      if (i != 1)
3815796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stderr, " ");
3825796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
3835796c8dcSSimon Schubert 	      i++;
3845796c8dcSSimon Schubert 	    }
3855796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, ".\n");
3865796c8dcSSimon Schubert #if 0
3875796c8dcSSimon Schubert 	  /* This extra info seems to be useless.  */
3885796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
3895796c8dcSSimon Schubert #endif
3905796c8dcSSimon Schubert 	  gdb_flush (gdb_stderr);
3915796c8dcSSimon Schubert 	  _exit (0177);
3925796c8dcSSimon Schubert 	}
3935796c8dcSSimon Schubert     }
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert   /* Restore our environment in case a vforked child clob'd it.  */
3965796c8dcSSimon Schubert   environ = save_our_env;
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert   if (!have_inferiors ())
3995796c8dcSSimon Schubert     init_thread_list ();
4005796c8dcSSimon Schubert 
401*cf7f2e2dSJohn Marino   inf = current_inferior ();
402*cf7f2e2dSJohn Marino 
403*cf7f2e2dSJohn Marino   inferior_appeared (inf, pid);
4045796c8dcSSimon Schubert 
4055796c8dcSSimon Schubert   /* Needed for wait_for_inferior stuff below.  */
4065796c8dcSSimon Schubert   inferior_ptid = pid_to_ptid (pid);
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   new_tty_postfork ();
4095796c8dcSSimon Schubert 
4105796c8dcSSimon Schubert   /* We have something that executes now.  We'll be running through
4115796c8dcSSimon Schubert      the shell at this point, but the pid shouldn't change.  Targets
4125796c8dcSSimon Schubert      supporting MT should fill this task's ptid with more data as soon
4135796c8dcSSimon Schubert      as they can.  */
4145796c8dcSSimon Schubert   add_thread_silent (inferior_ptid);
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert   /* Now that we have a child process, make it our target, and
4175796c8dcSSimon Schubert      initialize anything target-vector-specific that needs
4185796c8dcSSimon Schubert      initializing.  */
4195796c8dcSSimon Schubert   if (init_trace_fun)
4205796c8dcSSimon Schubert     (*init_trace_fun) (pid);
4215796c8dcSSimon Schubert 
4225796c8dcSSimon Schubert   /* We are now in the child process of interest, having exec'd the
4235796c8dcSSimon Schubert      correct program, and are poised at the first instruction of the
4245796c8dcSSimon Schubert      new program.  */
4255796c8dcSSimon Schubert   return pid;
4265796c8dcSSimon Schubert }
4275796c8dcSSimon Schubert 
4285796c8dcSSimon Schubert /* Accept NTRAPS traps from the inferior.  */
4295796c8dcSSimon Schubert 
4305796c8dcSSimon Schubert void
4315796c8dcSSimon Schubert startup_inferior (int ntraps)
4325796c8dcSSimon Schubert {
4335796c8dcSSimon Schubert   int pending_execs = ntraps;
4345796c8dcSSimon Schubert   int terminal_initted = 0;
4355796c8dcSSimon Schubert   ptid_t resume_ptid;
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert   if (target_supports_multi_process ())
4385796c8dcSSimon Schubert     resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
4395796c8dcSSimon Schubert   else
4405796c8dcSSimon Schubert     resume_ptid = minus_one_ptid;
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert   /* The process was started by the fork that created it, but it will
4435796c8dcSSimon Schubert      have stopped one instruction after execing the shell.  Here we
4445796c8dcSSimon Schubert      must get it up to actual execution of the real program.  */
4455796c8dcSSimon Schubert 
4465796c8dcSSimon Schubert   if (exec_wrapper)
4475796c8dcSSimon Schubert     pending_execs++;
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert   while (1)
4505796c8dcSSimon Schubert     {
4515796c8dcSSimon Schubert       int resume_signal = TARGET_SIGNAL_0;
4525796c8dcSSimon Schubert       ptid_t event_ptid;
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert       struct target_waitstatus ws;
4555796c8dcSSimon Schubert       memset (&ws, 0, sizeof (ws));
4565796c8dcSSimon Schubert       event_ptid = target_wait (resume_ptid, &ws, 0);
4575796c8dcSSimon Schubert 
4585796c8dcSSimon Schubert       if (ws.kind == TARGET_WAITKIND_IGNORE)
4595796c8dcSSimon Schubert 	/* The inferior didn't really stop, keep waiting.  */
4605796c8dcSSimon Schubert 	continue;
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert       switch (ws.kind)
4635796c8dcSSimon Schubert 	{
4645796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SPURIOUS:
4655796c8dcSSimon Schubert 	  case TARGET_WAITKIND_LOADED:
4665796c8dcSSimon Schubert 	  case TARGET_WAITKIND_FORKED:
4675796c8dcSSimon Schubert 	  case TARGET_WAITKIND_VFORKED:
4685796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SYSCALL_ENTRY:
4695796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SYSCALL_RETURN:
4705796c8dcSSimon Schubert 	    /* Ignore gracefully during startup of the inferior.  */
4715796c8dcSSimon Schubert 	    switch_to_thread (event_ptid);
4725796c8dcSSimon Schubert 	    break;
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SIGNALLED:
4755796c8dcSSimon Schubert 	    target_terminal_ours ();
4765796c8dcSSimon Schubert 	    target_mourn_inferior ();
4775796c8dcSSimon Schubert 	    error (_("During startup program terminated with signal %s, %s."),
4785796c8dcSSimon Schubert 		   target_signal_to_name (ws.value.sig),
4795796c8dcSSimon Schubert 		   target_signal_to_string (ws.value.sig));
4805796c8dcSSimon Schubert 	    return;
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert 	  case TARGET_WAITKIND_EXITED:
4835796c8dcSSimon Schubert 	    target_terminal_ours ();
4845796c8dcSSimon Schubert 	    target_mourn_inferior ();
4855796c8dcSSimon Schubert 	    if (ws.value.integer)
4865796c8dcSSimon Schubert 	      error (_("During startup program exited with code %d."),
4875796c8dcSSimon Schubert 		     ws.value.integer);
4885796c8dcSSimon Schubert 	    else
4895796c8dcSSimon Schubert 	      error (_("During startup program exited normally."));
4905796c8dcSSimon Schubert 	    return;
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert 	  case TARGET_WAITKIND_EXECD:
4935796c8dcSSimon Schubert 	    /* Handle EXEC signals as if they were SIGTRAP signals.  */
4945796c8dcSSimon Schubert 	    xfree (ws.value.execd_pathname);
4955796c8dcSSimon Schubert 	    resume_signal = TARGET_SIGNAL_TRAP;
4965796c8dcSSimon Schubert 	    switch_to_thread (event_ptid);
4975796c8dcSSimon Schubert 	    break;
4985796c8dcSSimon Schubert 
4995796c8dcSSimon Schubert 	  case TARGET_WAITKIND_STOPPED:
5005796c8dcSSimon Schubert 	    resume_signal = ws.value.sig;
5015796c8dcSSimon Schubert 	    switch_to_thread (event_ptid);
5025796c8dcSSimon Schubert 	    break;
5035796c8dcSSimon Schubert 	}
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert       if (resume_signal != TARGET_SIGNAL_TRAP)
5065796c8dcSSimon Schubert 	{
5075796c8dcSSimon Schubert 	  /* Let shell child handle its own signals in its own way.  */
5085796c8dcSSimon Schubert 	  target_resume (resume_ptid, 0, resume_signal);
5095796c8dcSSimon Schubert 	}
5105796c8dcSSimon Schubert       else
5115796c8dcSSimon Schubert 	{
5125796c8dcSSimon Schubert 	  /* We handle SIGTRAP, however; it means child did an exec.  */
5135796c8dcSSimon Schubert 	  if (!terminal_initted)
5145796c8dcSSimon Schubert 	    {
5155796c8dcSSimon Schubert 	      /* Now that the child has exec'd we know it has already
5165796c8dcSSimon Schubert 	         set its process group.  On POSIX systems, tcsetpgrp
5175796c8dcSSimon Schubert 	         will fail with EPERM if we try it before the child's
5185796c8dcSSimon Schubert 	         setpgid.  */
5195796c8dcSSimon Schubert 
5205796c8dcSSimon Schubert 	      /* Set up the "saved terminal modes" of the inferior
5215796c8dcSSimon Schubert 	         based on what modes we are starting it with.  */
5225796c8dcSSimon Schubert 	      target_terminal_init ();
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert 	      /* Install inferior's terminal modes.  */
5255796c8dcSSimon Schubert 	      target_terminal_inferior ();
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert 	      terminal_initted = 1;
5285796c8dcSSimon Schubert 	    }
5295796c8dcSSimon Schubert 
5305796c8dcSSimon Schubert 	  if (--pending_execs == 0)
5315796c8dcSSimon Schubert 	    break;
5325796c8dcSSimon Schubert 
5335796c8dcSSimon Schubert 	  /* Just make it go on.  */
5345796c8dcSSimon Schubert 	  target_resume (resume_ptid, 0, TARGET_SIGNAL_0);
5355796c8dcSSimon Schubert 	}
5365796c8dcSSimon Schubert     }
5375796c8dcSSimon Schubert 
5385796c8dcSSimon Schubert   /* Mark all threads non-executing.  */
5395796c8dcSSimon Schubert   set_executing (resume_ptid, 0);
5405796c8dcSSimon Schubert }
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert /* Implement the "unset exec-wrapper" command.  */
5435796c8dcSSimon Schubert 
5445796c8dcSSimon Schubert static void
5455796c8dcSSimon Schubert unset_exec_wrapper_command (char *args, int from_tty)
5465796c8dcSSimon Schubert {
5475796c8dcSSimon Schubert   xfree (exec_wrapper);
5485796c8dcSSimon Schubert   exec_wrapper = NULL;
5495796c8dcSSimon Schubert }
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
5525796c8dcSSimon Schubert extern initialize_file_ftype _initialize_fork_child;
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert void
5555796c8dcSSimon Schubert _initialize_fork_child (void)
5565796c8dcSSimon Schubert {
5575796c8dcSSimon Schubert   add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
5585796c8dcSSimon Schubert Set a wrapper for running programs.\n\
5595796c8dcSSimon Schubert The wrapper prepares the system and environment for the new program."),
5605796c8dcSSimon Schubert 			    _("\
5615796c8dcSSimon Schubert Show the wrapper for running programs."), NULL,
5625796c8dcSSimon Schubert 			    NULL, NULL,
5635796c8dcSSimon Schubert 			    &setlist, &showlist);
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert   add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
5665796c8dcSSimon Schubert            _("Disable use of an execution wrapper."),
5675796c8dcSSimon Schubert            &unsetlist);
5685796c8dcSSimon Schubert }
569