* inftarg.c: Remove unused include of terminal.h.
[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 "serial.h" /* For job_control.  */
28 #include "terminal.h"           /* For new_tty */
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   int pending_execs;
64   /* Set debug_fork then attach to the child while it sleeps, to debug. */
65   static int debug_fork = 0;
66   /* This is set to the result of setpgrp, which if vforked, will be visible
67      to you in the parent process.  It's only used by humans for debugging.  */
68   static int debug_setpgrp = 657473;
69   char **save_our_env;
70
71   /* If no exec file handed to us, get it from the exec-file command -- with
72      a good, common error message if none is specified.  */
73   if (exec_file == 0)
74     exec_file = get_exec_file(1);
75
76   /* The user might want tilde-expansion, and in general probably wants
77      the program to behave the same way as if run from
78      his/her favorite shell.  So we let the shell run it for us.
79      FIXME, this should probably search the local environment (as
80      modified by the setenv command), not the env gdb inherited.  */
81   shell_file = getenv ("SHELL");
82   if (shell_file == NULL)
83     shell_file = default_shell_file;
84
85   /* Multiplying the length of exec_file by 4 is to account for the fact
86      that it may expand when quoted; it is a worst-case number based on
87      every character being '.  */
88   len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 12;
89   /* If desired, concat something onto the front of ALLARGS.
90      SHELL_COMMAND is the result.  */
91 #ifdef SHELL_COMMAND_CONCAT
92   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
93   strcpy (shell_command, SHELL_COMMAND_CONCAT);
94 #else
95   shell_command = (char *) alloca (len);
96   shell_command[0] = '\0';
97 #endif
98   strcat (shell_command, "exec ");
99
100   /* Now add exec_file, quoting as necessary.  Quoting in this style is
101      said to work with all shells.  */
102   {
103     char *p;
104
105     strcat (shell_command, "'");
106     for (p = exec_file; *p != '\0'; ++p)
107       {
108         if (*p == '\'')
109           strcat (shell_command, "'\\''");
110         else
111           strncat (shell_command, p, 1);
112       }
113     strcat (shell_command, "'");
114   }
115
116   strcat (shell_command, " ");
117   strcat (shell_command, allargs);
118
119   /* exec is said to fail if the executable is open.  */
120   close_exec_file ();
121
122   /* Retain a copy of our environment variables, since the child will
123      replace the value of  environ  and if we're vforked, we have to 
124      restore it.  */
125   save_our_env = environ;
126
127   /* Tell the terminal handling subsystem what tty we plan to run on;
128      it will just record the information for later.  */
129
130   new_tty_prefork (inferior_io_terminal);
131
132   /* It is generally good practice to flush any possible pending stdio
133      output prior to doing a fork, to avoid the possibility of both the
134      parent and child flushing the same data after the fork. */
135
136   fflush (stdout);
137   fflush (stderr);
138
139 #if defined(USG) && !defined(HAVE_VFORK)
140   pid = fork ();
141 #else
142   if (debug_fork)
143     pid = fork ();
144   else
145     pid = vfork ();
146 #endif
147
148   if (pid < 0)
149     perror_with_name ("vfork");
150
151   if (pid == 0)
152     {
153       if (debug_fork) 
154         sleep (debug_fork);
155
156       /* Run inferior in a separate process group.  */
157       debug_setpgrp = gdb_setpgid ();
158       if (debug_setpgrp == -1)
159          perror("setpgrp failed in child");
160
161 #ifdef SET_STACK_LIMIT_HUGE
162       /* Reset the stack limit back to what it was.  */
163       {
164         struct rlimit rlim;
165
166         getrlimit (RLIMIT_STACK, &rlim);
167         rlim.rlim_cur = original_stack_limit;
168         setrlimit (RLIMIT_STACK, &rlim);
169       }
170 #endif /* SET_STACK_LIMIT_HUGE */
171
172       /* Ask the tty subsystem to switch to the one we specified earlier
173          (or to share the current terminal, if none was specified).  */
174
175       new_tty ();
176
177       /* Changing the signal handlers for the inferior after
178          a vfork can also change them for the superior, so we don't mess
179          with signals here.  See comments in
180          initialize_signals for how we get the right signal handlers
181          for the inferior.  */
182
183       /* "Trace me, Dr. Memory!" */
184       (*traceme_fun) ();
185
186       /* There is no execlpe call, so we have to set the environment
187          for our child in the global variable.  If we've vforked, this
188          clobbers the parent, but environ is restored a few lines down
189          in the parent.  By the way, yes we do need to look down the
190          path to find $SHELL.  Rich Pixley says so, and I agree.  */
191       environ = env;
192       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
193
194       fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
195                safe_strerror (errno));
196       fflush (stderr);
197       _exit (0177);
198     }
199
200   /* Restore our environment in case a vforked child clob'd it.  */
201   environ = save_our_env;
202
203   /* Now that we have a child process, make it our target, and
204      initialize anything target-vector-specific that needs initializing.  */
205   (*init_trace_fun)(pid);
206
207 #ifdef CREATE_INFERIOR_HOOK
208   CREATE_INFERIOR_HOOK (pid);
209 #endif  
210
211 /* The process was started by the fork that created it,
212    but it will have stopped one instruction after execing the shell.
213    Here we must get it up to actual execution of the real program.  */
214
215   inferior_pid = pid;           /* Needed for wait_for_inferior stuff below */
216
217   clear_proceed_status ();
218
219   /* We will get a trace trap after one instruction.
220      Continue it automatically.  Eventually (after shell does an exec)
221      it will get another trace trap.  Then insert breakpoints and continue.  */
222
223 #ifdef START_INFERIOR_TRAPS_EXPECTED
224   pending_execs = START_INFERIOR_TRAPS_EXPECTED;
225 #else
226   pending_execs = 2;
227 #endif
228
229   init_wait_for_inferior ();
230
231   /* Set up the "saved terminal modes" of the inferior
232      based on what modes we are starting it with.  */
233   target_terminal_init ();
234
235   /* Install inferior's terminal modes.  */
236   target_terminal_inferior ();
237
238   while (1)
239     {
240       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
241       wait_for_inferior ();
242       if (stop_signal != SIGTRAP)
243         {
244           /* Let shell child handle its own signals in its own way */
245           /* FIXME, what if child has exit()ed?  Must exit loop somehow */
246           resume (0, stop_signal);
247         }
248       else
249         {
250           /* We handle SIGTRAP, however; it means child did an exec.  */
251           if (0 == --pending_execs)
252             break;
253           resume (0, 0);                /* Just make it go on */
254         }
255     }
256   stop_soon_quietly = 0;
257
258   /* We are now in the child process of interest, having exec'd the
259      correct program, and are poised at the first instruction of the
260      new program.  */
261 #ifdef SOLIB_CREATE_INFERIOR_HOOK
262   SOLIB_CREATE_INFERIOR_HOOK (pid);
263 #endif
264 }