Commit | Line | Data |
---|---|---|
3fbdd536 JG |
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" | |
c18b613a | 27 | #include "terminal.h" |
3fbdd536 JG |
28 | |
29 | #include <signal.h> | |
30 | ||
31 | #ifdef SET_STACK_LIMIT_HUGE | |
32 | #include <sys/time.h> | |
33 | #include <sys/resource.h> | |
34 | ||
35 | extern int original_stack_limit; | |
36 | #endif /* SET_STACK_LIMIT_HUGE */ | |
37 | ||
38 | extern char **environ; | |
39 | ||
40 | /* Start an inferior Unix child process and sets inferior_pid to its pid. | |
41 | EXEC_FILE is the file to run. | |
42 | ALLARGS is a string containing the arguments to the program. | |
43 | ENV is the environment vector to pass. Errors reported with error(). */ | |
44 | ||
45 | #ifndef SHELL_FILE | |
46 | #define SHELL_FILE "/bin/sh" | |
47 | #endif | |
48 | ||
49 | void | |
50 | fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun) | |
51 | char *exec_file; | |
52 | char *allargs; | |
53 | char **env; | |
54 | void (*traceme_fun) PARAMS ((void)); | |
55 | void (*init_trace_fun) PARAMS ((int)); | |
56 | { | |
57 | int pid; | |
58 | char *shell_command; | |
59 | char *shell_file; | |
60 | static char default_shell_file[] = SHELL_FILE; | |
61 | int len; | |
62 | int pending_execs; | |
01dfd415 | 63 | int terminal_initted; |
3fbdd536 JG |
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; | |
3768398d JK |
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; | |
3fbdd536 JG |
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 "); | |
3768398d | 99 | |
38bbfd37 | 100 | /* Now add exec_file, quoting as necessary. */ |
3768398d JK |
101 | { |
102 | char *p; | |
38bbfd37 | 103 | int need_to_quote; |
3768398d | 104 | |
38bbfd37 JK |
105 | /* Quoting in this style is said to work with all shells. But csh |
106 | on IRIX 4.0.1 can't deal with it. So we only quote it if we need | |
107 | to. */ | |
108 | p = exec_file; | |
109 | while (1) | |
3768398d | 110 | { |
38bbfd37 JK |
111 | switch (*p) |
112 | { | |
113 | case '\'': | |
114 | case '"': | |
115 | case '(': | |
116 | case ')': | |
117 | case '$': | |
118 | case '&': | |
119 | case ';': | |
120 | case '<': | |
121 | case '>': | |
122 | case ' ': | |
123 | case '\n': | |
124 | case '\t': | |
125 | need_to_quote = 1; | |
126 | goto end_scan; | |
127 | ||
128 | case '\0': | |
129 | need_to_quote = 0; | |
130 | goto end_scan; | |
131 | ||
132 | default: | |
133 | break; | |
134 | } | |
135 | ++p; | |
3768398d | 136 | } |
38bbfd37 JK |
137 | end_scan: |
138 | if (need_to_quote) | |
139 | { | |
140 | strcat (shell_command, "'"); | |
141 | for (p = exec_file; *p != '\0'; ++p) | |
142 | { | |
143 | if (*p == '\'') | |
144 | strcat (shell_command, "'\\''"); | |
145 | else | |
146 | strncat (shell_command, p, 1); | |
147 | } | |
148 | strcat (shell_command, "'"); | |
149 | } | |
150 | else | |
151 | strcat (shell_command, exec_file); | |
3768398d JK |
152 | } |
153 | ||
3fbdd536 JG |
154 | strcat (shell_command, " "); |
155 | strcat (shell_command, allargs); | |
156 | ||
157 | /* exec is said to fail if the executable is open. */ | |
158 | close_exec_file (); | |
159 | ||
160 | /* Retain a copy of our environment variables, since the child will | |
161 | replace the value of environ and if we're vforked, we have to | |
162 | restore it. */ | |
163 | save_our_env = environ; | |
164 | ||
165 | /* Tell the terminal handling subsystem what tty we plan to run on; | |
166 | it will just record the information for later. */ | |
167 | ||
168 | new_tty_prefork (inferior_io_terminal); | |
169 | ||
170 | /* It is generally good practice to flush any possible pending stdio | |
171 | output prior to doing a fork, to avoid the possibility of both the | |
172 | parent and child flushing the same data after the fork. */ | |
173 | ||
174 | fflush (stdout); | |
175 | fflush (stderr); | |
176 | ||
177 | #if defined(USG) && !defined(HAVE_VFORK) | |
178 | pid = fork (); | |
179 | #else | |
180 | if (debug_fork) | |
181 | pid = fork (); | |
182 | else | |
183 | pid = vfork (); | |
184 | #endif | |
185 | ||
186 | if (pid < 0) | |
187 | perror_with_name ("vfork"); | |
188 | ||
189 | if (pid == 0) | |
190 | { | |
191 | if (debug_fork) | |
192 | sleep (debug_fork); | |
193 | ||
3fbdd536 | 194 | /* Run inferior in a separate process group. */ |
c2e247c4 | 195 | debug_setpgrp = gdb_setpgid (); |
3fbdd536 JG |
196 | if (debug_setpgrp == -1) |
197 | perror("setpgrp failed in child"); | |
3fbdd536 JG |
198 | |
199 | #ifdef SET_STACK_LIMIT_HUGE | |
200 | /* Reset the stack limit back to what it was. */ | |
201 | { | |
202 | struct rlimit rlim; | |
203 | ||
204 | getrlimit (RLIMIT_STACK, &rlim); | |
205 | rlim.rlim_cur = original_stack_limit; | |
206 | setrlimit (RLIMIT_STACK, &rlim); | |
207 | } | |
208 | #endif /* SET_STACK_LIMIT_HUGE */ | |
209 | ||
210 | /* Ask the tty subsystem to switch to the one we specified earlier | |
211 | (or to share the current terminal, if none was specified). */ | |
212 | ||
213 | new_tty (); | |
214 | ||
215 | /* Changing the signal handlers for the inferior after | |
216 | a vfork can also change them for the superior, so we don't mess | |
217 | with signals here. See comments in | |
218 | initialize_signals for how we get the right signal handlers | |
219 | for the inferior. */ | |
220 | ||
221 | /* "Trace me, Dr. Memory!" */ | |
222 | (*traceme_fun) (); | |
223 | ||
224 | /* There is no execlpe call, so we have to set the environment | |
225 | for our child in the global variable. If we've vforked, this | |
226 | clobbers the parent, but environ is restored a few lines down | |
227 | in the parent. By the way, yes we do need to look down the | |
228 | path to find $SHELL. Rich Pixley says so, and I agree. */ | |
229 | environ = env; | |
230 | execlp (shell_file, shell_file, "-c", shell_command, (char *)0); | |
231 | ||
232 | fprintf (stderr, "Cannot exec %s: %s.\n", shell_file, | |
233 | safe_strerror (errno)); | |
234 | fflush (stderr); | |
235 | _exit (0177); | |
236 | } | |
237 | ||
238 | /* Restore our environment in case a vforked child clob'd it. */ | |
239 | environ = save_our_env; | |
240 | ||
de43d7d0 SG |
241 | init_thread_list(); |
242 | ||
3fbdd536 JG |
243 | /* Now that we have a child process, make it our target, and |
244 | initialize anything target-vector-specific that needs initializing. */ | |
245 | (*init_trace_fun)(pid); | |
246 | ||
5d76c8e6 JK |
247 | /* The process was started by the fork that created it, |
248 | but it will have stopped one instruction after execing the shell. | |
249 | Here we must get it up to actual execution of the real program. */ | |
3fbdd536 JG |
250 | |
251 | inferior_pid = pid; /* Needed for wait_for_inferior stuff below */ | |
252 | ||
253 | clear_proceed_status (); | |
254 | ||
255 | /* We will get a trace trap after one instruction. | |
256 | Continue it automatically. Eventually (after shell does an exec) | |
257 | it will get another trace trap. Then insert breakpoints and continue. */ | |
258 | ||
259 | #ifdef START_INFERIOR_TRAPS_EXPECTED | |
260 | pending_execs = START_INFERIOR_TRAPS_EXPECTED; | |
261 | #else | |
262 | pending_execs = 2; | |
263 | #endif | |
264 | ||
265 | init_wait_for_inferior (); | |
266 | ||
01dfd415 | 267 | terminal_initted = 0; |
3fbdd536 JG |
268 | |
269 | while (1) | |
270 | { | |
271 | stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */ | |
272 | wait_for_inferior (); | |
273 | if (stop_signal != SIGTRAP) | |
274 | { | |
275 | /* Let shell child handle its own signals in its own way */ | |
276 | /* FIXME, what if child has exit()ed? Must exit loop somehow */ | |
277 | resume (0, stop_signal); | |
278 | } | |
279 | else | |
280 | { | |
281 | /* We handle SIGTRAP, however; it means child did an exec. */ | |
01dfd415 JK |
282 | if (!terminal_initted) |
283 | { | |
284 | /* Now that the child has exec'd we know it has already set its | |
285 | process group. On POSIX systems, tcsetpgrp will fail with | |
286 | EPERM if we try it before the child's setpgid. */ | |
287 | ||
288 | /* Set up the "saved terminal modes" of the inferior | |
289 | based on what modes we are starting it with. */ | |
290 | target_terminal_init (); | |
291 | ||
292 | /* Install inferior's terminal modes. */ | |
293 | target_terminal_inferior (); | |
294 | ||
295 | terminal_initted = 1; | |
296 | } | |
3fbdd536 JG |
297 | if (0 == --pending_execs) |
298 | break; | |
299 | resume (0, 0); /* Just make it go on */ | |
300 | } | |
301 | } | |
302 | stop_soon_quietly = 0; | |
303 | ||
304 | /* We are now in the child process of interest, having exec'd the | |
305 | correct program, and are poised at the first instruction of the | |
306 | new program. */ | |
307 | #ifdef SOLIB_CREATE_INFERIOR_HOOK | |
308 | SOLIB_CREATE_INFERIOR_HOOK (pid); | |
309 | #endif | |
310 | } |