* target.h: Add enum target_waitkind, enum target_signal, and
[platform/upstream/binutils.git] / gdb / fork-child.c
1 /* Fork a Unix child process, and set up to debug it, for GDB.
2    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "defs.h"
22 #include "frame.h"  /* required by inferior.h */
23 #include "inferior.h"
24 #include "target.h"
25 #include "wait.h"
26 #include "gdbcore.h"
27 #include "terminal.h"
28 #include "thread.h"
29
30 #include <signal.h>
31
32 #ifdef SET_STACK_LIMIT_HUGE
33 #include <sys/time.h>
34 #include <sys/resource.h>
35
36 extern int original_stack_limit;
37 #endif /* SET_STACK_LIMIT_HUGE */
38
39 extern char **environ;
40
41 /* Start an inferior Unix child process and sets inferior_pid to its pid.
42    EXEC_FILE is the file to run.
43    ALLARGS is a string containing the arguments to the program.
44    ENV is the environment vector to pass.  Errors reported with error().  */
45
46 #ifndef SHELL_FILE
47 #define SHELL_FILE "/bin/sh"
48 #endif
49
50 void
51 fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun)
52      char *exec_file;
53      char *allargs;
54      char **env;
55      void (*traceme_fun) PARAMS ((void));
56      void (*init_trace_fun) PARAMS ((int));
57 {
58   int pid;
59   char *shell_command;
60   char *shell_file;
61   static char default_shell_file[] = SHELL_FILE;
62   int len;
63   /* Set debug_fork then attach to the child while it sleeps, to debug. */
64   static int debug_fork = 0;
65   /* This is set to the result of setpgrp, which if vforked, will be visible
66      to you in the parent process.  It's only used by humans for debugging.  */
67   static int debug_setpgrp = 657473;
68   char **save_our_env;
69
70   /* If no exec file handed to us, get it from the exec-file command -- with
71      a good, common error message if none is specified.  */
72   if (exec_file == 0)
73     exec_file = get_exec_file(1);
74
75   /* The user might want tilde-expansion, and in general probably wants
76      the program to behave the same way as if run from
77      his/her favorite shell.  So we let the shell run it for us.
78      FIXME-maybe, we might want a "set shell" command so the user can change
79      the shell from within GDB.  */
80   shell_file = getenv ("SHELL");
81   if (shell_file == NULL)
82     shell_file = default_shell_file;
83
84   /* Multiplying the length of exec_file by 4 is to account for the fact
85      that it may expand when quoted; it is a worst-case number based on
86      every character being '.  */
87   len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 12;
88   /* If desired, concat something onto the front of ALLARGS.
89      SHELL_COMMAND is the result.  */
90 #ifdef SHELL_COMMAND_CONCAT
91   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
92   strcpy (shell_command, SHELL_COMMAND_CONCAT);
93 #else
94   shell_command = (char *) alloca (len);
95   shell_command[0] = '\0';
96 #endif
97   strcat (shell_command, "exec ");
98
99   /* Now add exec_file, quoting as necessary.  */
100   {
101     char *p;
102     int need_to_quote;
103
104     /* Quoting in this style is said to work with all shells.  But csh
105        on IRIX 4.0.1 can't deal with it.  So we only quote it if we need
106        to.  */
107     p = exec_file;
108     while (1)
109       {
110         switch (*p)
111           {
112           case '\'':
113           case '"':
114           case '(':
115           case ')':
116           case '$':
117           case '&':
118           case ';':
119           case '<':
120           case '>':
121           case ' ':
122           case '\n':
123           case '\t':
124             need_to_quote = 1;
125             goto end_scan;
126
127           case '\0':
128             need_to_quote = 0;
129             goto end_scan;
130
131           default:
132             break;
133           }
134         ++p;
135       }
136   end_scan:
137     if (need_to_quote)
138       {
139         strcat (shell_command, "'");
140         for (p = exec_file; *p != '\0'; ++p)
141           {
142             if (*p == '\'')
143               strcat (shell_command, "'\\''");
144             else
145               strncat (shell_command, p, 1);
146           }
147         strcat (shell_command, "'");
148       }
149     else
150       strcat (shell_command, exec_file);
151   }
152
153   strcat (shell_command, " ");
154   strcat (shell_command, allargs);
155
156   /* exec is said to fail if the executable is open.  */
157   close_exec_file ();
158
159   /* Retain a copy of our environment variables, since the child will
160      replace the value of  environ  and if we're vforked, we have to 
161      restore it.  */
162   save_our_env = environ;
163
164   /* Tell the terminal handling subsystem what tty we plan to run on;
165      it will just record the information for later.  */
166
167   new_tty_prefork (inferior_io_terminal);
168
169   /* It is generally good practice to flush any possible pending stdio
170      output prior to doing a fork, to avoid the possibility of both the
171      parent and child flushing the same data after the fork. */
172
173   gdb_flush (gdb_stdout);
174   gdb_flush (gdb_stderr);
175
176 #if defined(USG) && !defined(HAVE_VFORK)
177   pid = fork ();
178 #else
179   if (debug_fork)
180     pid = fork ();
181   else
182     pid = vfork ();
183 #endif
184
185   if (pid < 0)
186     perror_with_name ("vfork");
187
188   if (pid == 0)
189     {
190       if (debug_fork) 
191         sleep (debug_fork);
192
193       /* Run inferior in a separate process group.  */
194       debug_setpgrp = gdb_setpgid ();
195       if (debug_setpgrp == -1)
196          perror("setpgrp failed in child");
197
198 #ifdef SET_STACK_LIMIT_HUGE
199       /* Reset the stack limit back to what it was.  */
200       {
201         struct rlimit rlim;
202
203         getrlimit (RLIMIT_STACK, &rlim);
204         rlim.rlim_cur = original_stack_limit;
205         setrlimit (RLIMIT_STACK, &rlim);
206       }
207 #endif /* SET_STACK_LIMIT_HUGE */
208
209       /* Ask the tty subsystem to switch to the one we specified earlier
210          (or to share the current terminal, if none was specified).  */
211
212       new_tty ();
213
214       /* Changing the signal handlers for the inferior after
215          a vfork can also change them for the superior, so we don't mess
216          with signals here.  See comments in
217          initialize_signals for how we get the right signal handlers
218          for the inferior.  */
219
220       /* "Trace me, Dr. Memory!" */
221       (*traceme_fun) ();
222
223       /* There is no execlpe call, so we have to set the environment
224          for our child in the global variable.  If we've vforked, this
225          clobbers the parent, but environ is restored a few lines down
226          in the parent.  By the way, yes we do need to look down the
227          path to find $SHELL.  Rich Pixley says so, and I agree.  */
228       environ = env;
229       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
230
231       fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
232                safe_strerror (errno));
233       gdb_flush (gdb_stderr);
234       _exit (0177);
235     }
236
237   /* Restore our environment in case a vforked child clob'd it.  */
238   environ = save_our_env;
239
240   init_thread_list();
241
242   inferior_pid = pid;           /* Needed for wait_for_inferior stuff below */
243
244   /* Now that we have a child process, make it our target, and
245      initialize anything target-vector-specific that needs initializing.  */
246   (*init_trace_fun)(pid);
247
248   /* We are now in the child process of interest, having exec'd the
249      correct program, and are poised at the first instruction of the
250      new program.  */
251 #ifdef SOLIB_CREATE_INFERIOR_HOOK
252   SOLIB_CREATE_INFERIOR_HOOK (pid);
253 #endif
254 }
255
256 /* Accept NTRAPS traps from the inferior.  */
257
258 void
259 startup_inferior (ntraps)
260      int ntraps;
261 {
262   int pending_execs = ntraps;
263   int terminal_initted;
264
265   /* The process was started by the fork that created it,
266      but it will have stopped one instruction after execing the shell.
267      Here we must get it up to actual execution of the real program.  */
268
269   clear_proceed_status ();
270
271   init_wait_for_inferior ();
272
273   terminal_initted = 0;
274
275   while (1)
276     {
277       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
278       wait_for_inferior ();
279       if (stop_signal != TARGET_SIGNAL_TRAP)
280         {
281           /* Let shell child handle its own signals in its own way */
282           /* FIXME, what if child has exit()ed?  Must exit loop somehow */
283           resume (0, stop_signal);
284         }
285       else
286         {
287           /* We handle SIGTRAP, however; it means child did an exec.  */
288           if (!terminal_initted)
289             {
290               /* Now that the child has exec'd we know it has already set its
291                  process group.  On POSIX systems, tcsetpgrp will fail with
292                  EPERM if we try it before the child's setpgid.  */
293
294               /* Set up the "saved terminal modes" of the inferior
295                  based on what modes we are starting it with.  */
296               target_terminal_init ();
297
298               /* Install inferior's terminal modes.  */
299               target_terminal_inferior ();
300
301               terminal_initted = 1;
302             }
303           if (0 == --pending_execs)
304             break;
305           resume (0, TARGET_SIGNAL_0);          /* Just make it go on */
306         }
307     }
308   stop_soon_quietly = 0;
309 }