xref: /dflybsd-src/contrib/gdb-7/gdb/fork-child.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Fork a Unix child process, and set up to debug it, for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4*5796c8dcSSimon Schubert    2001, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    Contributed by Cygnus Support.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This file is part of GDB.
9*5796c8dcSSimon Schubert 
10*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
11*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
12*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
13*5796c8dcSSimon Schubert    (at your option) any later version.
14*5796c8dcSSimon Schubert 
15*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
16*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
17*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*5796c8dcSSimon Schubert    GNU General Public License for more details.
19*5796c8dcSSimon Schubert 
20*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
21*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22*5796c8dcSSimon Schubert 
23*5796c8dcSSimon Schubert #include "defs.h"
24*5796c8dcSSimon Schubert #include "gdb_string.h"
25*5796c8dcSSimon Schubert #include "inferior.h"
26*5796c8dcSSimon Schubert #include "terminal.h"
27*5796c8dcSSimon Schubert #include "target.h"
28*5796c8dcSSimon Schubert #include "gdb_wait.h"
29*5796c8dcSSimon Schubert #include "gdb_vfork.h"
30*5796c8dcSSimon Schubert #include "gdbcore.h"
31*5796c8dcSSimon Schubert #include "terminal.h"
32*5796c8dcSSimon Schubert #include "gdbthread.h"
33*5796c8dcSSimon Schubert #include "command.h" /* for dont_repeat () */
34*5796c8dcSSimon Schubert #include "gdbcmd.h"
35*5796c8dcSSimon Schubert #include "solib.h"
36*5796c8dcSSimon Schubert 
37*5796c8dcSSimon Schubert #include <signal.h>
38*5796c8dcSSimon Schubert 
39*5796c8dcSSimon Schubert /* This just gets used as a default if we can't find SHELL.  */
40*5796c8dcSSimon Schubert #define SHELL_FILE "/bin/sh"
41*5796c8dcSSimon Schubert 
42*5796c8dcSSimon Schubert extern char **environ;
43*5796c8dcSSimon Schubert 
44*5796c8dcSSimon Schubert static char *exec_wrapper;
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert /* Break up SCRATCH into an argument vector suitable for passing to
47*5796c8dcSSimon Schubert    execvp and store it in ARGV.  E.g., on "run a b c d" this routine
48*5796c8dcSSimon Schubert    would get as input the string "a b c d", and as output it would
49*5796c8dcSSimon Schubert    fill in ARGV with the four arguments "a", "b", "c", "d".  */
50*5796c8dcSSimon Schubert 
51*5796c8dcSSimon Schubert static void
52*5796c8dcSSimon Schubert breakup_args (char *scratch, char **argv)
53*5796c8dcSSimon Schubert {
54*5796c8dcSSimon Schubert   char *cp = scratch;
55*5796c8dcSSimon Schubert 
56*5796c8dcSSimon Schubert   for (;;)
57*5796c8dcSSimon Schubert     {
58*5796c8dcSSimon Schubert       /* Scan past leading separators */
59*5796c8dcSSimon Schubert       while (*cp == ' ' || *cp == '\t' || *cp == '\n')
60*5796c8dcSSimon Schubert 	cp++;
61*5796c8dcSSimon Schubert 
62*5796c8dcSSimon Schubert       /* Break if at end of string.  */
63*5796c8dcSSimon Schubert       if (*cp == '\0')
64*5796c8dcSSimon Schubert 	break;
65*5796c8dcSSimon Schubert 
66*5796c8dcSSimon Schubert       /* Take an arg.  */
67*5796c8dcSSimon Schubert       *argv++ = cp;
68*5796c8dcSSimon Schubert 
69*5796c8dcSSimon Schubert       /* Scan for next arg separator.  */
70*5796c8dcSSimon Schubert       cp = strchr (cp, ' ');
71*5796c8dcSSimon Schubert       if (cp == NULL)
72*5796c8dcSSimon Schubert 	cp = strchr (cp, '\t');
73*5796c8dcSSimon Schubert       if (cp == NULL)
74*5796c8dcSSimon Schubert 	cp = strchr (cp, '\n');
75*5796c8dcSSimon Schubert 
76*5796c8dcSSimon Schubert       /* No separators => end of string => break.  */
77*5796c8dcSSimon Schubert       if (cp == NULL)
78*5796c8dcSSimon Schubert 	break;
79*5796c8dcSSimon Schubert 
80*5796c8dcSSimon Schubert       /* Replace the separator with a terminator.  */
81*5796c8dcSSimon Schubert       *cp++ = '\0';
82*5796c8dcSSimon Schubert     }
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert   /* Null-terminate the vector.  */
85*5796c8dcSSimon Schubert   *argv = NULL;
86*5796c8dcSSimon Schubert }
87*5796c8dcSSimon Schubert 
88*5796c8dcSSimon Schubert /* When executing a command under the given shell, return non-zero if
89*5796c8dcSSimon Schubert    the '!' character should be escaped when embedded in a quoted
90*5796c8dcSSimon Schubert    command-line argument.  */
91*5796c8dcSSimon Schubert 
92*5796c8dcSSimon Schubert static int
93*5796c8dcSSimon Schubert escape_bang_in_quoted_argument (const char *shell_file)
94*5796c8dcSSimon Schubert {
95*5796c8dcSSimon Schubert   const int shell_file_len = strlen (shell_file);
96*5796c8dcSSimon Schubert 
97*5796c8dcSSimon Schubert   /* Bang should be escaped only in C Shells.  For now, simply check
98*5796c8dcSSimon Schubert      that the shell name ends with 'csh', which covers at least csh
99*5796c8dcSSimon Schubert      and tcsh.  This should be good enough for now.  */
100*5796c8dcSSimon Schubert 
101*5796c8dcSSimon Schubert   if (shell_file_len < 3)
102*5796c8dcSSimon Schubert     return 0;
103*5796c8dcSSimon Schubert 
104*5796c8dcSSimon Schubert   if (shell_file[shell_file_len - 3] == 'c'
105*5796c8dcSSimon Schubert       && shell_file[shell_file_len - 2] == 's'
106*5796c8dcSSimon Schubert       && shell_file[shell_file_len - 1] == 'h')
107*5796c8dcSSimon Schubert     return 1;
108*5796c8dcSSimon Schubert 
109*5796c8dcSSimon Schubert   return 0;
110*5796c8dcSSimon Schubert }
111*5796c8dcSSimon Schubert 
112*5796c8dcSSimon Schubert /* Start an inferior Unix child process and sets inferior_ptid to its
113*5796c8dcSSimon Schubert    pid.  EXEC_FILE is the file to run.  ALLARGS is a string containing
114*5796c8dcSSimon Schubert    the arguments to the program.  ENV is the environment vector to
115*5796c8dcSSimon Schubert    pass.  SHELL_FILE is the shell file, or NULL if we should pick
116*5796c8dcSSimon Schubert    one.  */
117*5796c8dcSSimon Schubert 
118*5796c8dcSSimon Schubert /* This function is NOT reentrant.  Some of the variables have been
119*5796c8dcSSimon Schubert    made static to ensure that they survive the vfork call.  */
120*5796c8dcSSimon Schubert 
121*5796c8dcSSimon Schubert int
122*5796c8dcSSimon Schubert fork_inferior (char *exec_file_arg, char *allargs, char **env,
123*5796c8dcSSimon Schubert 	       void (*traceme_fun) (void), void (*init_trace_fun) (int),
124*5796c8dcSSimon Schubert 	       void (*pre_trace_fun) (void), char *shell_file_arg)
125*5796c8dcSSimon Schubert {
126*5796c8dcSSimon Schubert   int pid;
127*5796c8dcSSimon Schubert   char *shell_command;
128*5796c8dcSSimon Schubert   static char default_shell_file[] = SHELL_FILE;
129*5796c8dcSSimon Schubert   int len;
130*5796c8dcSSimon Schubert   /* Set debug_fork then attach to the child while it sleeps, to debug. */
131*5796c8dcSSimon Schubert   static int debug_fork = 0;
132*5796c8dcSSimon Schubert   /* This is set to the result of setpgrp, which if vforked, will be visible
133*5796c8dcSSimon Schubert      to you in the parent process.  It's only used by humans for debugging.  */
134*5796c8dcSSimon Schubert   static int debug_setpgrp = 657473;
135*5796c8dcSSimon Schubert   static char *shell_file;
136*5796c8dcSSimon Schubert   static char *exec_file;
137*5796c8dcSSimon Schubert   char **save_our_env;
138*5796c8dcSSimon Schubert   int shell = 0;
139*5796c8dcSSimon Schubert   static char **argv;
140*5796c8dcSSimon Schubert   const char *inferior_io_terminal = get_inferior_io_terminal ();
141*5796c8dcSSimon Schubert 
142*5796c8dcSSimon Schubert   /* If no exec file handed to us, get it from the exec-file command
143*5796c8dcSSimon Schubert      -- with a good, common error message if none is specified.  */
144*5796c8dcSSimon Schubert   exec_file = exec_file_arg;
145*5796c8dcSSimon Schubert   if (exec_file == 0)
146*5796c8dcSSimon Schubert     exec_file = get_exec_file (1);
147*5796c8dcSSimon Schubert 
148*5796c8dcSSimon Schubert   /* STARTUP_WITH_SHELL is defined in inferior.h.  If 0,e we'll just
149*5796c8dcSSimon Schubert     do a fork/exec, no shell, so don't bother figuring out what
150*5796c8dcSSimon Schubert     shell.  */
151*5796c8dcSSimon Schubert   shell_file = shell_file_arg;
152*5796c8dcSSimon Schubert   if (STARTUP_WITH_SHELL)
153*5796c8dcSSimon Schubert     {
154*5796c8dcSSimon Schubert       /* Figure out what shell to start up the user program under.  */
155*5796c8dcSSimon Schubert       if (shell_file == NULL)
156*5796c8dcSSimon Schubert 	shell_file = getenv ("SHELL");
157*5796c8dcSSimon Schubert       if (shell_file == NULL)
158*5796c8dcSSimon Schubert 	shell_file = default_shell_file;
159*5796c8dcSSimon Schubert       shell = 1;
160*5796c8dcSSimon Schubert     }
161*5796c8dcSSimon Schubert 
162*5796c8dcSSimon Schubert   /* Multiplying the length of exec_file by 4 is to account for the
163*5796c8dcSSimon Schubert      fact that it may expand when quoted; it is a worst-case number
164*5796c8dcSSimon Schubert      based on every character being '.  */
165*5796c8dcSSimon Schubert   len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
166*5796c8dcSSimon Schubert   if (exec_wrapper)
167*5796c8dcSSimon Schubert     len += strlen (exec_wrapper) + 1;
168*5796c8dcSSimon Schubert 
169*5796c8dcSSimon Schubert   shell_command = (char *) alloca (len);
170*5796c8dcSSimon Schubert   shell_command[0] = '\0';
171*5796c8dcSSimon Schubert 
172*5796c8dcSSimon Schubert   if (!shell)
173*5796c8dcSSimon Schubert     {
174*5796c8dcSSimon Schubert       /* We're going to call execvp.  Create argument vector.
175*5796c8dcSSimon Schubert 	 Calculate an upper bound on the length of the vector by
176*5796c8dcSSimon Schubert 	 assuming that every other character is a separate
177*5796c8dcSSimon Schubert 	 argument.  */
178*5796c8dcSSimon Schubert       int argc = (strlen (allargs) + 1) / 2 + 2;
179*5796c8dcSSimon Schubert       argv = (char **) xmalloc (argc * sizeof (*argv));
180*5796c8dcSSimon Schubert       argv[0] = exec_file;
181*5796c8dcSSimon Schubert       breakup_args (allargs, &argv[1]);
182*5796c8dcSSimon Schubert     }
183*5796c8dcSSimon Schubert   else
184*5796c8dcSSimon Schubert     {
185*5796c8dcSSimon Schubert       /* We're going to call a shell.  */
186*5796c8dcSSimon Schubert 
187*5796c8dcSSimon Schubert       char *p;
188*5796c8dcSSimon Schubert       int need_to_quote;
189*5796c8dcSSimon Schubert       const int escape_bang = escape_bang_in_quoted_argument (shell_file);
190*5796c8dcSSimon Schubert 
191*5796c8dcSSimon Schubert       strcat (shell_command, "exec ");
192*5796c8dcSSimon Schubert 
193*5796c8dcSSimon Schubert       /* Add any exec wrapper.  That may be a program name with arguments, so
194*5796c8dcSSimon Schubert 	 the user must handle quoting.  */
195*5796c8dcSSimon Schubert       if (exec_wrapper)
196*5796c8dcSSimon Schubert 	{
197*5796c8dcSSimon Schubert 	  strcat (shell_command, exec_wrapper);
198*5796c8dcSSimon Schubert 	  strcat (shell_command, " ");
199*5796c8dcSSimon Schubert 	}
200*5796c8dcSSimon Schubert 
201*5796c8dcSSimon Schubert       /* Now add exec_file, quoting as necessary.  */
202*5796c8dcSSimon Schubert 
203*5796c8dcSSimon Schubert       /* Quoting in this style is said to work with all shells.  But
204*5796c8dcSSimon Schubert          csh on IRIX 4.0.1 can't deal with it.  So we only quote it if
205*5796c8dcSSimon Schubert          we need to.  */
206*5796c8dcSSimon Schubert       p = exec_file;
207*5796c8dcSSimon Schubert       while (1)
208*5796c8dcSSimon Schubert 	{
209*5796c8dcSSimon Schubert 	  switch (*p)
210*5796c8dcSSimon Schubert 	    {
211*5796c8dcSSimon Schubert 	    case '\'':
212*5796c8dcSSimon Schubert 	    case '!':
213*5796c8dcSSimon Schubert 	    case '"':
214*5796c8dcSSimon Schubert 	    case '(':
215*5796c8dcSSimon Schubert 	    case ')':
216*5796c8dcSSimon Schubert 	    case '$':
217*5796c8dcSSimon Schubert 	    case '&':
218*5796c8dcSSimon Schubert 	    case ';':
219*5796c8dcSSimon Schubert 	    case '<':
220*5796c8dcSSimon Schubert 	    case '>':
221*5796c8dcSSimon Schubert 	    case ' ':
222*5796c8dcSSimon Schubert 	    case '\n':
223*5796c8dcSSimon Schubert 	    case '\t':
224*5796c8dcSSimon Schubert 	      need_to_quote = 1;
225*5796c8dcSSimon Schubert 	      goto end_scan;
226*5796c8dcSSimon Schubert 
227*5796c8dcSSimon Schubert 	    case '\0':
228*5796c8dcSSimon Schubert 	      need_to_quote = 0;
229*5796c8dcSSimon Schubert 	      goto end_scan;
230*5796c8dcSSimon Schubert 
231*5796c8dcSSimon Schubert 	    default:
232*5796c8dcSSimon Schubert 	      break;
233*5796c8dcSSimon Schubert 	    }
234*5796c8dcSSimon Schubert 	  ++p;
235*5796c8dcSSimon Schubert 	}
236*5796c8dcSSimon Schubert     end_scan:
237*5796c8dcSSimon Schubert       if (need_to_quote)
238*5796c8dcSSimon Schubert 	{
239*5796c8dcSSimon Schubert 	  strcat (shell_command, "'");
240*5796c8dcSSimon Schubert 	  for (p = exec_file; *p != '\0'; ++p)
241*5796c8dcSSimon Schubert 	    {
242*5796c8dcSSimon Schubert 	      if (*p == '\'')
243*5796c8dcSSimon Schubert 		strcat (shell_command, "'\\''");
244*5796c8dcSSimon Schubert 	      else if (*p == '!' && escape_bang)
245*5796c8dcSSimon Schubert 		strcat (shell_command, "\\!");
246*5796c8dcSSimon Schubert 	      else
247*5796c8dcSSimon Schubert 		strncat (shell_command, p, 1);
248*5796c8dcSSimon Schubert 	    }
249*5796c8dcSSimon Schubert 	  strcat (shell_command, "'");
250*5796c8dcSSimon Schubert 	}
251*5796c8dcSSimon Schubert       else
252*5796c8dcSSimon Schubert 	strcat (shell_command, exec_file);
253*5796c8dcSSimon Schubert 
254*5796c8dcSSimon Schubert       strcat (shell_command, " ");
255*5796c8dcSSimon Schubert       strcat (shell_command, allargs);
256*5796c8dcSSimon Schubert     }
257*5796c8dcSSimon Schubert 
258*5796c8dcSSimon Schubert   /* On some systems an exec will fail if the executable is open.  */
259*5796c8dcSSimon Schubert   close_exec_file ();
260*5796c8dcSSimon Schubert 
261*5796c8dcSSimon Schubert   /* Retain a copy of our environment variables, since the child will
262*5796c8dcSSimon Schubert      replace the value of environ and if we're vforked, we have to
263*5796c8dcSSimon Schubert      restore it.  */
264*5796c8dcSSimon Schubert   save_our_env = environ;
265*5796c8dcSSimon Schubert 
266*5796c8dcSSimon Schubert   /* Tell the terminal handling subsystem what tty we plan to run on;
267*5796c8dcSSimon Schubert      it will just record the information for later.  */
268*5796c8dcSSimon Schubert   new_tty_prefork (inferior_io_terminal);
269*5796c8dcSSimon Schubert 
270*5796c8dcSSimon Schubert   /* It is generally good practice to flush any possible pending stdio
271*5796c8dcSSimon Schubert      output prior to doing a fork, to avoid the possibility of both
272*5796c8dcSSimon Schubert      the parent and child flushing the same data after the fork. */
273*5796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
274*5796c8dcSSimon Schubert   gdb_flush (gdb_stderr);
275*5796c8dcSSimon Schubert 
276*5796c8dcSSimon Schubert   /* If there's any initialization of the target layers that must
277*5796c8dcSSimon Schubert      happen to prepare to handle the child we're about fork, do it
278*5796c8dcSSimon Schubert      now...  */
279*5796c8dcSSimon Schubert   if (pre_trace_fun != NULL)
280*5796c8dcSSimon Schubert     (*pre_trace_fun) ();
281*5796c8dcSSimon Schubert 
282*5796c8dcSSimon Schubert   /* Create the child process.  Since the child process is going to
283*5796c8dcSSimon Schubert      exec(3) shortly afterwards, try to reduce the overhead by
284*5796c8dcSSimon Schubert      calling vfork(2).  However, if PRE_TRACE_FUN is non-null, it's
285*5796c8dcSSimon Schubert      likely that this optimization won't work since there's too much
286*5796c8dcSSimon Schubert      work to do between the vfork(2) and the exec(3).  This is known
287*5796c8dcSSimon Schubert      to be the case on ttrace(2)-based HP-UX, where some handshaking
288*5796c8dcSSimon Schubert      between parent and child needs to happen between fork(2) and
289*5796c8dcSSimon Schubert      exec(2).  However, since the parent is suspended in the vforked
290*5796c8dcSSimon Schubert      state, this doesn't work.  Also note that the vfork(2) call might
291*5796c8dcSSimon Schubert      actually be a call to fork(2) due to the fact that autoconf will
292*5796c8dcSSimon Schubert      ``#define vfork fork'' on certain platforms.  */
293*5796c8dcSSimon Schubert   if (pre_trace_fun || debug_fork)
294*5796c8dcSSimon Schubert     pid = fork ();
295*5796c8dcSSimon Schubert   else
296*5796c8dcSSimon Schubert     pid = vfork ();
297*5796c8dcSSimon Schubert 
298*5796c8dcSSimon Schubert   if (pid < 0)
299*5796c8dcSSimon Schubert     perror_with_name (("vfork"));
300*5796c8dcSSimon Schubert 
301*5796c8dcSSimon Schubert   if (pid == 0)
302*5796c8dcSSimon Schubert     {
303*5796c8dcSSimon Schubert       if (debug_fork)
304*5796c8dcSSimon Schubert 	sleep (debug_fork);
305*5796c8dcSSimon Schubert 
306*5796c8dcSSimon Schubert       /* Create a new session for the inferior process, if necessary.
307*5796c8dcSSimon Schubert          It will also place the inferior in a separate process group.  */
308*5796c8dcSSimon Schubert       if (create_tty_session () <= 0)
309*5796c8dcSSimon Schubert 	{
310*5796c8dcSSimon Schubert 	  /* No session was created, but we still want to run the inferior
311*5796c8dcSSimon Schubert 	     in a separate process group.  */
312*5796c8dcSSimon Schubert 	  debug_setpgrp = gdb_setpgid ();
313*5796c8dcSSimon Schubert 	  if (debug_setpgrp == -1)
314*5796c8dcSSimon Schubert 	    perror ("setpgrp failed in child");
315*5796c8dcSSimon Schubert 	}
316*5796c8dcSSimon Schubert 
317*5796c8dcSSimon Schubert       /* Ask the tty subsystem to switch to the one we specified
318*5796c8dcSSimon Schubert          earlier (or to share the current terminal, if none was
319*5796c8dcSSimon Schubert          specified).  */
320*5796c8dcSSimon Schubert       new_tty ();
321*5796c8dcSSimon Schubert 
322*5796c8dcSSimon Schubert       /* Changing the signal handlers for the inferior after
323*5796c8dcSSimon Schubert          a vfork can also change them for the superior, so we don't mess
324*5796c8dcSSimon Schubert          with signals here.  See comments in
325*5796c8dcSSimon Schubert          initialize_signals for how we get the right signal handlers
326*5796c8dcSSimon Schubert          for the inferior.  */
327*5796c8dcSSimon Schubert 
328*5796c8dcSSimon Schubert       /* "Trace me, Dr. Memory!" */
329*5796c8dcSSimon Schubert       (*traceme_fun) ();
330*5796c8dcSSimon Schubert 
331*5796c8dcSSimon Schubert       /* The call above set this process (the "child") as debuggable
332*5796c8dcSSimon Schubert         by the original gdb process (the "parent").  Since processes
333*5796c8dcSSimon Schubert         (unlike people) can have only one parent, if you are debugging
334*5796c8dcSSimon Schubert         gdb itself (and your debugger is thus _already_ the
335*5796c8dcSSimon Schubert         controller/parent for this child), code from here on out is
336*5796c8dcSSimon Schubert         undebuggable.  Indeed, you probably got an error message
337*5796c8dcSSimon Schubert         saying "not parent".  Sorry; you'll have to use print
338*5796c8dcSSimon Schubert         statements!  */
339*5796c8dcSSimon Schubert 
340*5796c8dcSSimon Schubert       /* There is no execlpe call, so we have to set the environment
341*5796c8dcSSimon Schubert          for our child in the global variable.  If we've vforked, this
342*5796c8dcSSimon Schubert          clobbers the parent, but environ is restored a few lines down
343*5796c8dcSSimon Schubert          in the parent.  By the way, yes we do need to look down the
344*5796c8dcSSimon Schubert          path to find $SHELL.  Rich Pixley says so, and I agree.  */
345*5796c8dcSSimon Schubert       environ = env;
346*5796c8dcSSimon Schubert 
347*5796c8dcSSimon Schubert       /* If we decided above to start up with a shell, we exec the
348*5796c8dcSSimon Schubert 	 shell, "-c" says to interpret the next arg as a shell command
349*5796c8dcSSimon Schubert 	 to execute, and this command is "exec <target-program>
350*5796c8dcSSimon Schubert 	 <args>".  */
351*5796c8dcSSimon Schubert       if (shell)
352*5796c8dcSSimon Schubert 	{
353*5796c8dcSSimon Schubert 	  execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
354*5796c8dcSSimon Schubert 
355*5796c8dcSSimon Schubert 	  /* If we get here, it's an error.  */
356*5796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
357*5796c8dcSSimon Schubert 			      safe_strerror (errno));
358*5796c8dcSSimon Schubert 	  gdb_flush (gdb_stderr);
359*5796c8dcSSimon Schubert 	  _exit (0177);
360*5796c8dcSSimon Schubert 	}
361*5796c8dcSSimon Schubert       else
362*5796c8dcSSimon Schubert 	{
363*5796c8dcSSimon Schubert 	  /* Otherwise, we directly exec the target program with
364*5796c8dcSSimon Schubert 	     execvp.  */
365*5796c8dcSSimon Schubert 	  int i;
366*5796c8dcSSimon Schubert 	  char *errstring;
367*5796c8dcSSimon Schubert 
368*5796c8dcSSimon Schubert 	  execvp (exec_file, argv);
369*5796c8dcSSimon Schubert 
370*5796c8dcSSimon Schubert 	  /* If we get here, it's an error.  */
371*5796c8dcSSimon Schubert 	  errstring = safe_strerror (errno);
372*5796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert 	  i = 1;
375*5796c8dcSSimon Schubert 	  while (argv[i] != NULL)
376*5796c8dcSSimon Schubert 	    {
377*5796c8dcSSimon Schubert 	      if (i != 1)
378*5796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stderr, " ");
379*5796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
380*5796c8dcSSimon Schubert 	      i++;
381*5796c8dcSSimon Schubert 	    }
382*5796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, ".\n");
383*5796c8dcSSimon Schubert #if 0
384*5796c8dcSSimon Schubert 	  /* This extra info seems to be useless.  */
385*5796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
386*5796c8dcSSimon Schubert #endif
387*5796c8dcSSimon Schubert 	  gdb_flush (gdb_stderr);
388*5796c8dcSSimon Schubert 	  _exit (0177);
389*5796c8dcSSimon Schubert 	}
390*5796c8dcSSimon Schubert     }
391*5796c8dcSSimon Schubert 
392*5796c8dcSSimon Schubert   /* Restore our environment in case a vforked child clob'd it.  */
393*5796c8dcSSimon Schubert   environ = save_our_env;
394*5796c8dcSSimon Schubert 
395*5796c8dcSSimon Schubert   if (!have_inferiors ())
396*5796c8dcSSimon Schubert     init_thread_list ();
397*5796c8dcSSimon Schubert 
398*5796c8dcSSimon Schubert   add_inferior (pid);
399*5796c8dcSSimon Schubert 
400*5796c8dcSSimon Schubert   /* Needed for wait_for_inferior stuff below.  */
401*5796c8dcSSimon Schubert   inferior_ptid = pid_to_ptid (pid);
402*5796c8dcSSimon Schubert 
403*5796c8dcSSimon Schubert   new_tty_postfork ();
404*5796c8dcSSimon Schubert 
405*5796c8dcSSimon Schubert   /* We have something that executes now.  We'll be running through
406*5796c8dcSSimon Schubert      the shell at this point, but the pid shouldn't change.  Targets
407*5796c8dcSSimon Schubert      supporting MT should fill this task's ptid with more data as soon
408*5796c8dcSSimon Schubert      as they can.  */
409*5796c8dcSSimon Schubert   add_thread_silent (inferior_ptid);
410*5796c8dcSSimon Schubert 
411*5796c8dcSSimon Schubert   /* Now that we have a child process, make it our target, and
412*5796c8dcSSimon Schubert      initialize anything target-vector-specific that needs
413*5796c8dcSSimon Schubert      initializing.  */
414*5796c8dcSSimon Schubert   if (init_trace_fun)
415*5796c8dcSSimon Schubert     (*init_trace_fun) (pid);
416*5796c8dcSSimon Schubert 
417*5796c8dcSSimon Schubert   /* We are now in the child process of interest, having exec'd the
418*5796c8dcSSimon Schubert      correct program, and are poised at the first instruction of the
419*5796c8dcSSimon Schubert      new program.  */
420*5796c8dcSSimon Schubert   return pid;
421*5796c8dcSSimon Schubert }
422*5796c8dcSSimon Schubert 
423*5796c8dcSSimon Schubert /* Accept NTRAPS traps from the inferior.  */
424*5796c8dcSSimon Schubert 
425*5796c8dcSSimon Schubert void
426*5796c8dcSSimon Schubert startup_inferior (int ntraps)
427*5796c8dcSSimon Schubert {
428*5796c8dcSSimon Schubert   int pending_execs = ntraps;
429*5796c8dcSSimon Schubert   int terminal_initted = 0;
430*5796c8dcSSimon Schubert   ptid_t resume_ptid;
431*5796c8dcSSimon Schubert 
432*5796c8dcSSimon Schubert   if (target_supports_multi_process ())
433*5796c8dcSSimon Schubert     resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
434*5796c8dcSSimon Schubert   else
435*5796c8dcSSimon Schubert     resume_ptid = minus_one_ptid;
436*5796c8dcSSimon Schubert 
437*5796c8dcSSimon Schubert   /* The process was started by the fork that created it, but it will
438*5796c8dcSSimon Schubert      have stopped one instruction after execing the shell.  Here we
439*5796c8dcSSimon Schubert      must get it up to actual execution of the real program.  */
440*5796c8dcSSimon Schubert 
441*5796c8dcSSimon Schubert   if (exec_wrapper)
442*5796c8dcSSimon Schubert     pending_execs++;
443*5796c8dcSSimon Schubert 
444*5796c8dcSSimon Schubert   while (1)
445*5796c8dcSSimon Schubert     {
446*5796c8dcSSimon Schubert       int resume_signal = TARGET_SIGNAL_0;
447*5796c8dcSSimon Schubert       ptid_t event_ptid;
448*5796c8dcSSimon Schubert 
449*5796c8dcSSimon Schubert       struct target_waitstatus ws;
450*5796c8dcSSimon Schubert       memset (&ws, 0, sizeof (ws));
451*5796c8dcSSimon Schubert       event_ptid = target_wait (resume_ptid, &ws, 0);
452*5796c8dcSSimon Schubert 
453*5796c8dcSSimon Schubert       if (ws.kind == TARGET_WAITKIND_IGNORE)
454*5796c8dcSSimon Schubert 	/* The inferior didn't really stop, keep waiting.  */
455*5796c8dcSSimon Schubert 	continue;
456*5796c8dcSSimon Schubert 
457*5796c8dcSSimon Schubert       switch (ws.kind)
458*5796c8dcSSimon Schubert 	{
459*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SPURIOUS:
460*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_LOADED:
461*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_FORKED:
462*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_VFORKED:
463*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SYSCALL_ENTRY:
464*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SYSCALL_RETURN:
465*5796c8dcSSimon Schubert 	    /* Ignore gracefully during startup of the inferior.  */
466*5796c8dcSSimon Schubert 	    switch_to_thread (event_ptid);
467*5796c8dcSSimon Schubert 	    break;
468*5796c8dcSSimon Schubert 
469*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_SIGNALLED:
470*5796c8dcSSimon Schubert 	    target_terminal_ours ();
471*5796c8dcSSimon Schubert 	    target_mourn_inferior ();
472*5796c8dcSSimon Schubert 	    error (_("During startup program terminated with signal %s, %s."),
473*5796c8dcSSimon Schubert 		   target_signal_to_name (ws.value.sig),
474*5796c8dcSSimon Schubert 		   target_signal_to_string (ws.value.sig));
475*5796c8dcSSimon Schubert 	    return;
476*5796c8dcSSimon Schubert 
477*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_EXITED:
478*5796c8dcSSimon Schubert 	    target_terminal_ours ();
479*5796c8dcSSimon Schubert 	    target_mourn_inferior ();
480*5796c8dcSSimon Schubert 	    if (ws.value.integer)
481*5796c8dcSSimon Schubert 	      error (_("During startup program exited with code %d."),
482*5796c8dcSSimon Schubert 		     ws.value.integer);
483*5796c8dcSSimon Schubert 	    else
484*5796c8dcSSimon Schubert 	      error (_("During startup program exited normally."));
485*5796c8dcSSimon Schubert 	    return;
486*5796c8dcSSimon Schubert 
487*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_EXECD:
488*5796c8dcSSimon Schubert 	    /* Handle EXEC signals as if they were SIGTRAP signals.  */
489*5796c8dcSSimon Schubert 	    xfree (ws.value.execd_pathname);
490*5796c8dcSSimon Schubert 	    resume_signal = TARGET_SIGNAL_TRAP;
491*5796c8dcSSimon Schubert 	    switch_to_thread (event_ptid);
492*5796c8dcSSimon Schubert 	    break;
493*5796c8dcSSimon Schubert 
494*5796c8dcSSimon Schubert 	  case TARGET_WAITKIND_STOPPED:
495*5796c8dcSSimon Schubert 	    resume_signal = ws.value.sig;
496*5796c8dcSSimon Schubert 	    switch_to_thread (event_ptid);
497*5796c8dcSSimon Schubert 	    break;
498*5796c8dcSSimon Schubert 	}
499*5796c8dcSSimon Schubert 
500*5796c8dcSSimon Schubert       if (resume_signal != TARGET_SIGNAL_TRAP)
501*5796c8dcSSimon Schubert 	{
502*5796c8dcSSimon Schubert 	  /* Let shell child handle its own signals in its own way.  */
503*5796c8dcSSimon Schubert 	  target_resume (resume_ptid, 0, resume_signal);
504*5796c8dcSSimon Schubert 	}
505*5796c8dcSSimon Schubert       else
506*5796c8dcSSimon Schubert 	{
507*5796c8dcSSimon Schubert 	  /* We handle SIGTRAP, however; it means child did an exec.  */
508*5796c8dcSSimon Schubert 	  if (!terminal_initted)
509*5796c8dcSSimon Schubert 	    {
510*5796c8dcSSimon Schubert 	      /* Now that the child has exec'd we know it has already
511*5796c8dcSSimon Schubert 	         set its process group.  On POSIX systems, tcsetpgrp
512*5796c8dcSSimon Schubert 	         will fail with EPERM if we try it before the child's
513*5796c8dcSSimon Schubert 	         setpgid.  */
514*5796c8dcSSimon Schubert 
515*5796c8dcSSimon Schubert 	      /* Set up the "saved terminal modes" of the inferior
516*5796c8dcSSimon Schubert 	         based on what modes we are starting it with.  */
517*5796c8dcSSimon Schubert 	      target_terminal_init ();
518*5796c8dcSSimon Schubert 
519*5796c8dcSSimon Schubert 	      /* Install inferior's terminal modes.  */
520*5796c8dcSSimon Schubert 	      target_terminal_inferior ();
521*5796c8dcSSimon Schubert 
522*5796c8dcSSimon Schubert 	      terminal_initted = 1;
523*5796c8dcSSimon Schubert 	    }
524*5796c8dcSSimon Schubert 
525*5796c8dcSSimon Schubert 	  if (--pending_execs == 0)
526*5796c8dcSSimon Schubert 	    break;
527*5796c8dcSSimon Schubert 
528*5796c8dcSSimon Schubert 	  /* Just make it go on.  */
529*5796c8dcSSimon Schubert 	  target_resume (resume_ptid, 0, TARGET_SIGNAL_0);
530*5796c8dcSSimon Schubert 	}
531*5796c8dcSSimon Schubert     }
532*5796c8dcSSimon Schubert 
533*5796c8dcSSimon Schubert   /* Mark all threads non-executing.  */
534*5796c8dcSSimon Schubert   set_executing (resume_ptid, 0);
535*5796c8dcSSimon Schubert }
536*5796c8dcSSimon Schubert 
537*5796c8dcSSimon Schubert /* Implement the "unset exec-wrapper" command.  */
538*5796c8dcSSimon Schubert 
539*5796c8dcSSimon Schubert static void
540*5796c8dcSSimon Schubert unset_exec_wrapper_command (char *args, int from_tty)
541*5796c8dcSSimon Schubert {
542*5796c8dcSSimon Schubert   xfree (exec_wrapper);
543*5796c8dcSSimon Schubert   exec_wrapper = NULL;
544*5796c8dcSSimon Schubert }
545*5796c8dcSSimon Schubert 
546*5796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
547*5796c8dcSSimon Schubert extern initialize_file_ftype _initialize_fork_child;
548*5796c8dcSSimon Schubert 
549*5796c8dcSSimon Schubert void
550*5796c8dcSSimon Schubert _initialize_fork_child (void)
551*5796c8dcSSimon Schubert {
552*5796c8dcSSimon Schubert   add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
553*5796c8dcSSimon Schubert Set a wrapper for running programs.\n\
554*5796c8dcSSimon Schubert The wrapper prepares the system and environment for the new program."),
555*5796c8dcSSimon Schubert 			    _("\
556*5796c8dcSSimon Schubert Show the wrapper for running programs."), NULL,
557*5796c8dcSSimon Schubert 			    NULL, NULL,
558*5796c8dcSSimon Schubert 			    &setlist, &showlist);
559*5796c8dcSSimon Schubert 
560*5796c8dcSSimon Schubert   add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
561*5796c8dcSSimon Schubert            _("Disable use of an execution wrapper."),
562*5796c8dcSSimon Schubert            &unsetlist);
563*5796c8dcSSimon Schubert }
564