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