]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Fork a Unix child process, and set up to debug it, for GDB. |
cce74817 | 2 | Copyright 1990, 91, 92, 93, 94, 1996, 1999 Free Software Foundation, Inc. |
c906108c SS |
3 | Contributed by Cygnus Support. |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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. | |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b JM |
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., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "gdb_string.h" | |
24 | #include "frame.h" /* required by inferior.h */ | |
25 | #include "inferior.h" | |
26 | #include "target.h" | |
03f2053f | 27 | #include "gdb_wait.h" |
c906108c SS |
28 | #include "gdbcore.h" |
29 | #include "terminal.h" | |
30 | #include "gdbthread.h" | |
c2d11a7d | 31 | #include "command.h" /* for dont_repeat () */ |
c906108c SS |
32 | |
33 | #include <signal.h> | |
c906108c | 34 | |
c906108c SS |
35 | /* This just gets used as a default if we can't find SHELL */ |
36 | #ifndef SHELL_FILE | |
37 | #define SHELL_FILE "/bin/sh" | |
38 | #endif | |
39 | ||
40 | extern char **environ; | |
41 | ||
42 | /* This function breaks up an argument string into an argument | |
43 | * vector suitable for passing to execvp(). | |
44 | * E.g., on "run a b c d" this routine would get as input | |
45 | * the string "a b c d", and as output it would fill in argv with | |
46 | * the four arguments "a", "b", "c", "d". | |
47 | */ | |
48 | static void | |
49 | breakup_args ( | |
50 | scratch, | |
51 | argv) | |
52 | char *scratch; | |
53 | char **argv; | |
54 | { | |
55 | char *cp = scratch; | |
56 | ||
c906108c SS |
57 | for (;;) |
58 | { | |
59 | ||
60 | /* Scan past leading separators */ | |
61 | while (*cp == ' ' || *cp == '\t' || *cp == '\n') | |
62 | { | |
63 | cp++; | |
64 | } | |
65 | ||
66 | /* Break if at end of string */ | |
67 | if (*cp == '\0') | |
68 | break; | |
69 | ||
70 | /* Take an arg */ | |
71 | *argv++ = cp; | |
72 | ||
73 | /* Scan for next arg separator */ | |
74 | cp = strchr (cp, ' '); | |
75 | if (cp == NULL) | |
76 | cp = strchr (cp, '\t'); | |
77 | if (cp == NULL) | |
78 | cp = strchr (cp, '\n'); | |
79 | ||
80 | /* No separators => end of string => break */ | |
81 | if (cp == NULL) | |
82 | break; | |
83 | ||
84 | /* Replace the separator with a terminator */ | |
85 | *cp++ = '\0'; | |
86 | } | |
87 | ||
88 | /* execv requires a null-terminated arg vector */ | |
89 | *argv = NULL; | |
90 | ||
91 | } | |
92 | ||
93 | ||
94 | /* Start an inferior Unix child process and sets inferior_pid to its pid. | |
95 | EXEC_FILE is the file to run. | |
96 | ALLARGS is a string containing the arguments to the program. | |
97 | ENV is the environment vector to pass. SHELL_FILE is the shell file, | |
98 | or NULL if we should pick one. Errors reported with error(). */ | |
99 | ||
100 | void | |
101 | fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun, | |
102 | pre_trace_fun, shell_file) | |
103 | char *exec_file; | |
104 | char *allargs; | |
105 | char **env; | |
106 | void (*traceme_fun) PARAMS ((void)); | |
107 | void (*init_trace_fun) PARAMS ((int)); | |
108 | void (*pre_trace_fun) PARAMS ((void)); | |
109 | char *shell_file; | |
110 | { | |
111 | int pid; | |
112 | char *shell_command; | |
113 | static char default_shell_file[] = SHELL_FILE; | |
114 | int len; | |
115 | /* Set debug_fork then attach to the child while it sleeps, to debug. */ | |
116 | static int debug_fork = 0; | |
117 | /* This is set to the result of setpgrp, which if vforked, will be visible | |
118 | to you in the parent process. It's only used by humans for debugging. */ | |
119 | static int debug_setpgrp = 657473; | |
120 | char **save_our_env; | |
121 | int shell = 0; | |
122 | char **argv; | |
c906108c SS |
123 | |
124 | /* If no exec file handed to us, get it from the exec-file command -- with | |
125 | a good, common error message if none is specified. */ | |
126 | if (exec_file == 0) | |
127 | exec_file = get_exec_file (1); | |
128 | ||
129 | /* STARTUP_WITH_SHELL is defined in inferior.h. | |
130 | * If 0, we'll just do a fork/exec, no shell, so don't | |
131 | * bother figuring out what shell. | |
132 | */ | |
133 | if (STARTUP_WITH_SHELL) | |
134 | { | |
135 | /* Figure out what shell to start up the user program under. */ | |
136 | if (shell_file == NULL) | |
137 | shell_file = getenv ("SHELL"); | |
138 | if (shell_file == NULL) | |
139 | shell_file = default_shell_file; | |
140 | shell = 1; | |
141 | } | |
142 | ||
c906108c SS |
143 | /* Multiplying the length of exec_file by 4 is to account for the fact |
144 | that it may expand when quoted; it is a worst-case number based on | |
145 | every character being '. */ | |
c5aa993b | 146 | len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12; |
c906108c SS |
147 | /* If desired, concat something onto the front of ALLARGS. |
148 | SHELL_COMMAND is the result. */ | |
149 | #ifdef SHELL_COMMAND_CONCAT | |
150 | shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len); | |
151 | strcpy (shell_command, SHELL_COMMAND_CONCAT); | |
152 | #else | |
153 | shell_command = (char *) alloca (len); | |
154 | shell_command[0] = '\0'; | |
155 | #endif | |
156 | ||
157 | if (!shell) | |
158 | { | |
159 | /* We're going to call execvp. Create argv */ | |
160 | /* Largest case: every other character is a separate arg */ | |
c906108c SS |
161 | argv = (char **) xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) * sizeof (*argv)); |
162 | argv[0] = exec_file; | |
163 | breakup_args (allargs, &argv[1]); | |
164 | ||
165 | } | |
166 | else | |
167 | { | |
168 | ||
169 | /* We're going to call a shell */ | |
170 | ||
171 | /* Now add exec_file, quoting as necessary. */ | |
172 | ||
173 | char *p; | |
174 | int need_to_quote; | |
175 | ||
176 | strcat (shell_command, "exec "); | |
177 | ||
178 | /* Quoting in this style is said to work with all shells. But csh | |
c5aa993b JM |
179 | on IRIX 4.0.1 can't deal with it. So we only quote it if we need |
180 | to. */ | |
c906108c SS |
181 | p = exec_file; |
182 | while (1) | |
183 | { | |
184 | switch (*p) | |
185 | { | |
186 | case '\'': | |
187 | case '"': | |
188 | case '(': | |
189 | case ')': | |
190 | case '$': | |
191 | case '&': | |
192 | case ';': | |
193 | case '<': | |
194 | case '>': | |
195 | case ' ': | |
196 | case '\n': | |
197 | case '\t': | |
198 | need_to_quote = 1; | |
199 | goto end_scan; | |
200 | ||
201 | case '\0': | |
202 | need_to_quote = 0; | |
203 | goto end_scan; | |
204 | ||
205 | default: | |
206 | break; | |
207 | } | |
208 | ++p; | |
209 | } | |
210 | end_scan: | |
211 | if (need_to_quote) | |
212 | { | |
213 | strcat (shell_command, "'"); | |
214 | for (p = exec_file; *p != '\0'; ++p) | |
215 | { | |
216 | if (*p == '\'') | |
217 | strcat (shell_command, "'\\''"); | |
218 | else | |
219 | strncat (shell_command, p, 1); | |
220 | } | |
221 | strcat (shell_command, "'"); | |
222 | } | |
223 | else | |
224 | strcat (shell_command, exec_file); | |
225 | ||
226 | strcat (shell_command, " "); | |
227 | strcat (shell_command, allargs); | |
228 | ||
229 | } | |
230 | ||
231 | /* exec is said to fail if the executable is open. */ | |
232 | close_exec_file (); | |
233 | ||
234 | /* Retain a copy of our environment variables, since the child will | |
235 | replace the value of environ and if we're vforked, we have to | |
236 | restore it. */ | |
237 | save_our_env = environ; | |
238 | ||
239 | /* Tell the terminal handling subsystem what tty we plan to run on; | |
240 | it will just record the information for later. */ | |
241 | ||
242 | new_tty_prefork (inferior_io_terminal); | |
243 | ||
244 | /* It is generally good practice to flush any possible pending stdio | |
245 | output prior to doing a fork, to avoid the possibility of both the | |
246 | parent and child flushing the same data after the fork. */ | |
247 | ||
248 | gdb_flush (gdb_stdout); | |
249 | gdb_flush (gdb_stderr); | |
250 | ||
251 | /* If there's any initialization of the target layers that must happen | |
c5aa993b | 252 | to prepare to handle the child we're about fork, do it now... |
c906108c SS |
253 | */ |
254 | if (pre_trace_fun != NULL) | |
255 | (*pre_trace_fun) (); | |
256 | ||
257 | #if defined(USG) && !defined(HAVE_VFORK) | |
258 | pid = fork (); | |
259 | #else | |
260 | if (debug_fork) | |
261 | pid = fork (); | |
262 | else | |
263 | pid = vfork (); | |
264 | #endif | |
265 | ||
266 | if (pid < 0) | |
267 | perror_with_name ("vfork"); | |
268 | ||
269 | if (pid == 0) | |
270 | { | |
271 | if (debug_fork) | |
272 | sleep (debug_fork); | |
273 | ||
274 | /* Run inferior in a separate process group. */ | |
275 | debug_setpgrp = gdb_setpgid (); | |
276 | if (debug_setpgrp == -1) | |
277 | perror ("setpgrp failed in child"); | |
278 | ||
279 | /* Ask the tty subsystem to switch to the one we specified earlier | |
c5aa993b | 280 | (or to share the current terminal, if none was specified). */ |
c906108c SS |
281 | |
282 | new_tty (); | |
283 | ||
284 | /* Changing the signal handlers for the inferior after | |
c5aa993b JM |
285 | a vfork can also change them for the superior, so we don't mess |
286 | with signals here. See comments in | |
287 | initialize_signals for how we get the right signal handlers | |
288 | for the inferior. */ | |
c906108c SS |
289 | |
290 | /* "Trace me, Dr. Memory!" */ | |
291 | (*traceme_fun) (); | |
292 | /* The call above set this process (the "child") as debuggable | |
293 | * by the original gdb process (the "parent"). Since processes | |
294 | * (unlike people) can have only one parent, if you are | |
295 | * debugging gdb itself (and your debugger is thus _already_ the | |
296 | * controller/parent for this child), code from here on out | |
297 | * is undebuggable. Indeed, you probably got an error message | |
298 | * saying "not parent". Sorry--you'll have to use print statements! | |
299 | */ | |
300 | ||
301 | /* There is no execlpe call, so we have to set the environment | |
c5aa993b JM |
302 | for our child in the global variable. If we've vforked, this |
303 | clobbers the parent, but environ is restored a few lines down | |
304 | in the parent. By the way, yes we do need to look down the | |
305 | path to find $SHELL. Rich Pixley says so, and I agree. */ | |
c906108c SS |
306 | environ = env; |
307 | ||
308 | /* If we decided above to start up with a shell, | |
309 | * we exec the shell, | |
310 | * "-c" says to interpret the next arg as a shell command | |
311 | * to execute, and this command is "exec <target-program> <args>". | |
312 | * "-f" means "fast startup" to the c-shell, which means | |
313 | * don't do .cshrc file. Doing .cshrc may cause fork/exec | |
314 | * events which will confuse debugger start-up code. | |
315 | */ | |
316 | if (shell) | |
317 | { | |
c906108c | 318 | execlp (shell_file, shell_file, "-c", shell_command, (char *) 0); |
c906108c SS |
319 | |
320 | /* If we get here, it's an error */ | |
321 | fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file, | |
322 | safe_strerror (errno)); | |
323 | gdb_flush (gdb_stderr); | |
324 | _exit (0177); | |
325 | } | |
326 | else | |
327 | { | |
328 | /* Otherwise, we directly exec the target program with execvp. */ | |
329 | int i; | |
330 | char *errstring; | |
cce74817 | 331 | |
c906108c SS |
332 | execvp (exec_file, argv); |
333 | ||
334 | /* If we get here, it's an error */ | |
335 | errstring = safe_strerror (errno); | |
336 | fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file); | |
337 | ||
338 | i = 1; | |
339 | while (argv[i] != NULL) | |
340 | { | |
341 | if (i != 1) | |
342 | fprintf_unfiltered (gdb_stderr, " "); | |
343 | fprintf_unfiltered (gdb_stderr, "%s", argv[i]); | |
344 | i++; | |
345 | } | |
346 | fprintf_unfiltered (gdb_stderr, ".\n"); | |
347 | /* This extra info seems to be useless | |
c5aa993b JM |
348 | fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring); |
349 | */ | |
c906108c SS |
350 | gdb_flush (gdb_stderr); |
351 | _exit (0177); | |
352 | } | |
353 | } | |
354 | ||
355 | /* Restore our environment in case a vforked child clob'd it. */ | |
356 | environ = save_our_env; | |
357 | ||
358 | init_thread_list (); | |
359 | ||
360 | inferior_pid = pid; /* Needed for wait_for_inferior stuff below */ | |
361 | ||
362 | /* Now that we have a child process, make it our target, and | |
363 | initialize anything target-vector-specific that needs initializing. */ | |
364 | ||
365 | (*init_trace_fun) (pid); | |
366 | ||
367 | /* We are now in the child process of interest, having exec'd the | |
368 | correct program, and are poised at the first instruction of the | |
369 | new program. */ | |
370 | ||
371 | /* Allow target dependant code to play with the new process. This might be | |
372 | used to have target-specific code initialize a variable in the new process | |
373 | prior to executing the first instruction. */ | |
374 | TARGET_CREATE_INFERIOR_HOOK (pid); | |
375 | ||
376 | #ifdef SOLIB_CREATE_INFERIOR_HOOK | |
377 | SOLIB_CREATE_INFERIOR_HOOK (pid); | |
378 | #endif | |
379 | } | |
380 | ||
381 | /* An inferior Unix process CHILD_PID has been created by a call to | |
382 | fork() (or variants like vfork). It is presently stopped, and waiting | |
383 | to be resumed. clone_and_follow_inferior will fork the debugger, | |
384 | and that clone will "follow" (attach to) CHILD_PID. The original copy | |
385 | of the debugger will not touch CHILD_PID again. | |
386 | ||
387 | Also, the original debugger will set FOLLOWED_CHILD FALSE, while the | |
388 | clone will set it TRUE. | |
c5aa993b | 389 | */ |
c906108c SS |
390 | void |
391 | clone_and_follow_inferior (child_pid, followed_child) | |
392 | int child_pid; | |
393 | int *followed_child; | |
394 | { | |
395 | extern int auto_solib_add; | |
396 | ||
397 | int debugger_pid; | |
398 | int status; | |
399 | char pid_spelling[100]; /* Arbitrary but sufficient length. */ | |
400 | ||
401 | /* This semaphore is used to coordinate the two debuggers' handoff | |
402 | of CHILD_PID. The original debugger will detach from CHILD_PID, | |
403 | and then the clone debugger will attach to it. (It must be done | |
404 | this way because on some targets, only one process at a time can | |
405 | trace another. Thus, the original debugger must relinquish its | |
406 | tracing rights before the clone can pick them up.) | |
c5aa993b | 407 | */ |
c906108c SS |
408 | #define SEM_TALK (1) |
409 | #define SEM_LISTEN (0) | |
410 | int handoff_semaphore[2]; /* Original "talks" to [1], clone "listens" to [0] */ | |
411 | int talk_value = 99; | |
412 | int listen_value; | |
413 | ||
414 | /* Set debug_fork then attach to the child while it sleeps, to debug. */ | |
415 | static int debug_fork = 0; | |
416 | ||
417 | /* It is generally good practice to flush any possible pending stdio | |
418 | output prior to doing a fork, to avoid the possibility of both the | |
419 | parent and child flushing the same data after the fork. */ | |
420 | ||
421 | gdb_flush (gdb_stdout); | |
422 | gdb_flush (gdb_stderr); | |
423 | ||
424 | /* Open the semaphore pipes. | |
c5aa993b | 425 | */ |
c906108c SS |
426 | status = pipe (handoff_semaphore); |
427 | if (status < 0) | |
428 | error ("error getting pipe for handoff semaphore"); | |
429 | ||
430 | /* Clone the debugger. */ | |
431 | #if defined(USG) && !defined(HAVE_VFORK) | |
432 | debugger_pid = fork (); | |
433 | #else | |
434 | if (debug_fork) | |
435 | debugger_pid = fork (); | |
436 | else | |
437 | debugger_pid = vfork (); | |
438 | #endif | |
439 | ||
440 | if (debugger_pid < 0) | |
441 | perror_with_name ("fork"); | |
442 | ||
443 | /* Are we the original debugger? If so, we must relinquish all claims | |
c5aa993b | 444 | to CHILD_PID. */ |
c906108c SS |
445 | if (debugger_pid != 0) |
446 | { | |
c5aa993b | 447 | char signal_spelling[100]; /* Arbitrary but sufficient length */ |
c906108c SS |
448 | |
449 | /* Detach from CHILD_PID. Deliver a "stop" signal when we do, though, | |
c5aa993b JM |
450 | so that it remains stopped until the clone debugger can attach |
451 | to it. | |
c906108c SS |
452 | */ |
453 | detach_breakpoints (child_pid); | |
454 | ||
455 | sprintf (signal_spelling, "%d", target_signal_to_host (TARGET_SIGNAL_STOP)); | |
456 | target_require_detach (child_pid, signal_spelling, 1); | |
457 | ||
458 | /* Notify the clone debugger that it should attach to CHILD_PID. */ | |
459 | write (handoff_semaphore[SEM_TALK], &talk_value, sizeof (talk_value)); | |
460 | ||
461 | *followed_child = 0; | |
462 | } | |
463 | ||
464 | /* We're the child. */ | |
465 | else | |
466 | { | |
467 | if (debug_fork) | |
468 | sleep (debug_fork); | |
469 | ||
470 | /* The child (i.e., the cloned debugger) must now attach to | |
471 | CHILD_PID. inferior_pid is presently set to the parent process | |
472 | of the fork, while CHILD_PID should be the child process of the | |
473 | fork. | |
474 | ||
475 | Wait until the original debugger relinquishes control of CHILD_PID, | |
476 | though. | |
c5aa993b | 477 | */ |
c906108c SS |
478 | read (handoff_semaphore[SEM_LISTEN], &listen_value, sizeof (listen_value)); |
479 | ||
480 | /* Note that we DON'T want to actually detach from inferior_pid, | |
481 | because that would allow it to run free. The original | |
482 | debugger wants to retain control of the process. So, we | |
483 | just reset inferior_pid to CHILD_PID, and then ensure that all | |
484 | breakpoints are really set in CHILD_PID. | |
c5aa993b | 485 | */ |
c906108c SS |
486 | target_mourn_inferior (); |
487 | ||
488 | /* Ask the tty subsystem to switch to the one we specified earlier | |
489 | (or to share the current terminal, if none was specified). */ | |
490 | ||
491 | new_tty (); | |
492 | ||
493 | dont_repeat (); | |
494 | sprintf (pid_spelling, "%d", child_pid); | |
495 | target_require_attach (pid_spelling, 1); | |
496 | ||
497 | /* Perform any necessary cleanup, after attachment. (This form | |
498 | of attaching can behave differently on some targets than the | |
499 | standard method, where a process formerly not under debugger | |
500 | control was suddenly attached to..) | |
c5aa993b | 501 | */ |
c906108c SS |
502 | target_post_follow_inferior_by_clone (); |
503 | ||
504 | *followed_child = 1; | |
505 | } | |
506 | ||
507 | /* Discard the handoff sempahore. */ | |
508 | (void) close (handoff_semaphore[SEM_LISTEN]); | |
509 | (void) close (handoff_semaphore[SEM_TALK]); | |
510 | } | |
511 | ||
512 | /* Accept NTRAPS traps from the inferior. */ | |
513 | ||
514 | void | |
515 | startup_inferior (ntraps) | |
516 | int ntraps; | |
517 | { | |
518 | int pending_execs = ntraps; | |
519 | int terminal_initted; | |
520 | ||
521 | /* The process was started by the fork that created it, | |
522 | but it will have stopped one instruction after execing the shell. | |
523 | Here we must get it up to actual execution of the real program. */ | |
524 | ||
525 | clear_proceed_status (); | |
526 | ||
527 | init_wait_for_inferior (); | |
528 | ||
529 | terminal_initted = 0; | |
530 | ||
531 | if (STARTUP_WITH_SHELL) | |
532 | inferior_ignoring_startup_exec_events = ntraps; | |
533 | else | |
534 | inferior_ignoring_startup_exec_events = 0; | |
535 | inferior_ignoring_leading_exec_events = | |
536 | target_reported_exec_events_per_exec_call () - 1; | |
537 | ||
538 | #ifdef STARTUP_INFERIOR | |
539 | STARTUP_INFERIOR (pending_execs); | |
540 | #else | |
541 | while (1) | |
542 | { | |
543 | stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */ | |
544 | wait_for_inferior (); | |
545 | if (stop_signal != TARGET_SIGNAL_TRAP) | |
546 | { | |
547 | /* Let shell child handle its own signals in its own way */ | |
548 | /* FIXME, what if child has exit()ed? Must exit loop somehow */ | |
549 | resume (0, stop_signal); | |
550 | } | |
551 | else | |
552 | { | |
553 | /* We handle SIGTRAP, however; it means child did an exec. */ | |
554 | if (!terminal_initted) | |
555 | { | |
556 | /* Now that the child has exec'd we know it has already set its | |
c5aa993b JM |
557 | process group. On POSIX systems, tcsetpgrp will fail with |
558 | EPERM if we try it before the child's setpgid. */ | |
c906108c SS |
559 | |
560 | /* Set up the "saved terminal modes" of the inferior | |
c5aa993b | 561 | based on what modes we are starting it with. */ |
c906108c SS |
562 | target_terminal_init (); |
563 | ||
564 | /* Install inferior's terminal modes. */ | |
565 | target_terminal_inferior (); | |
566 | ||
567 | terminal_initted = 1; | |
568 | } | |
569 | ||
570 | pending_execs = pending_execs - 1; | |
571 | if (0 == pending_execs) | |
572 | break; | |
573 | ||
574 | resume (0, TARGET_SIGNAL_0); /* Just make it go on */ | |
575 | } | |
576 | } | |
577 | #endif /* STARTUP_INFERIOR */ | |
578 | stop_soon_quietly = 0; | |
579 | } |