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