1 /* This file inplements the host independent child process statum.
3 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "frame.h" /* required by inferior.h */
28 #include "ieee-float.h" /* Required by REGISTER_CONVERT_TO_XXX */
29 #include "terminal.h" /* For #ifdef TIOCGPGRP and new_tty */
33 #ifdef SET_STACK_LIMIT_HUGE
35 #include <sys/resource.h>
37 extern int original_stack_limit;
38 #endif /* SET_STACK_LIMIT_HUGE */
41 child_prepare_to_store PARAMS ((void));
44 child_wait PARAMS ((int *));
47 child_open PARAMS ((char *, int));
50 child_files_info PARAMS ((struct target_ops *));
53 child_detach PARAMS ((char *, int));
56 child_attach PARAMS ((char *, int));
59 child_create_inferior PARAMS ((char *, char *, char **));
62 child_mourn_inferior PARAMS ((void));
64 extern char **environ;
66 /* Forward declaration */
67 extern struct target_ops child_ops;
69 /* Wait for child to do something. Return pid of child, or -1 in case
70 of error; store status through argument pointer STATUS. */
80 pid = proc_wait (status);
84 if (pid == -1) /* No more children to wait for */
86 fprintf (stderr, "Child process unexpectedly missing.\n");
87 *status = 42; /* Claim it exited with signal 42 */
90 } while (pid != inferior_pid); /* Some other child died or stopped */
95 /* Attach to process PID, then initialize for debugging it
96 and wait for the trace-trap that results from attaching. */
99 child_attach (args, from_tty)
109 error_no_arg ("process-id to attach");
111 #ifndef ATTACH_DETACH
112 error ("Can't attach to a process on this machine.");
116 if (pid == getpid()) /* Trying to masturbate? */
117 error ("I refuse to debug myself!");
119 if (target_has_execution)
121 if (query ("A program is being debugged already. Kill it? "))
124 error ("Inferior not killed.");
129 exec_file = (char *) get_exec_file (0);
132 printf ("Attaching program `%s', pid %d\n", exec_file, pid);
134 printf ("Attaching pid %d\n", pid);
141 push_target (&child_ops);
143 mark_breakpoints_out ();
144 target_terminal_init ();
145 clear_proceed_status ();
146 stop_soon_quietly = 1;
147 /*proceed (-1, 0, -2);*/
148 target_terminal_inferior ();
149 wait_for_inferior ();
151 SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
154 #endif /* ATTACH_DETACH */
159 * takes a program previously attached to and detaches it.
160 * The program resumes execution and will no longer stop
161 * on signals, etc. We better not have left any breakpoints
162 * in the program or it'll die when it hits one. For this
163 * to work, it may be necessary for the process to have been
164 * previously attached. It *might* work if the program was
165 * started via the normal ptrace (PTRACE_TRACEME).
169 child_detach (args, from_tty)
178 char *exec_file = get_exec_file (0);
181 printf ("Detaching program: %s pid %d\n",
182 exec_file, inferior_pid);
186 siggnal = atoi (args);
190 unpush_target (&child_ops); /* Pop out of handling an inferior */
192 error ("This version of Unix does not support detaching a process.");
196 /* Get ready to modify the registers array. On machines which store
197 individual registers, this doesn't need to do anything. On machines
198 which store all the registers in one fell swoop, this makes sure
199 that registers contains all the registers from the program being
203 child_prepare_to_store ()
205 #ifdef CHILD_PREPARE_TO_STORE
206 CHILD_PREPARE_TO_STORE ();
210 /* Print status information about what we're accessing. */
213 child_files_info (ignore)
214 struct target_ops *ignore;
216 printf ("\tUsing the running image of %s process %d.\n",
217 attach_flag? "attached": "child", inferior_pid);
222 child_open (arg, from_tty)
226 error ("Use the \"run\" command to start a Unix child process.");
229 /* Start an inferior Unix child process and sets inferior_pid to its pid.
230 EXEC_FILE is the file to run.
231 ALLARGS is a string containing the arguments to the program.
232 ENV is the environment vector to pass. Errors reported with error(). */
235 #define SHELL_FILE "/bin/sh"
239 child_create_inferior (exec_file, allargs, env)
247 static char default_shell_file[] = SHELL_FILE;
250 /* Set debug_fork then attach to the child while it sleeps, to debug. */
251 static int debug_fork = 0;
252 /* This is set to the result of setpgrp, which if vforked, will be visible
253 to you in the parent process. It's only used by humans for debugging. */
254 static int debug_setpgrp = 657473;
257 /* If no exec file handed to us, get it from the exec-file command -- with
258 a good, common error message if none is specified. */
260 exec_file = get_exec_file(1);
262 /* The user might want tilde-expansion, and in general probably wants
263 the program to behave the same way as if run from
264 his/her favorite shell. So we let the shell run it for us.
265 FIXME, this should probably search the local environment (as
266 modified by the setenv command), not the env gdb inherited. */
267 shell_file = getenv ("SHELL");
268 if (shell_file == NULL)
269 shell_file = default_shell_file;
271 len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
272 /* If desired, concat something onto the front of ALLARGS.
273 SHELL_COMMAND is the result. */
274 #ifdef SHELL_COMMAND_CONCAT
275 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
276 strcpy (shell_command, SHELL_COMMAND_CONCAT);
278 shell_command = (char *) alloca (len);
279 shell_command[0] = '\0';
281 strcat (shell_command, "exec ");
282 strcat (shell_command, exec_file);
283 strcat (shell_command, " ");
284 strcat (shell_command, allargs);
286 /* exec is said to fail if the executable is open. */
289 /* Retain a copy of our environment variables, since the child will
290 replace the value of environ and if we're vforked, we have to
292 save_our_env = environ;
294 /* Tell the terminal handling subsystem what tty we plan to run on;
295 it will just record the information for later. */
297 new_tty_prefork (inferior_io_terminal);
299 /* It is generally good practice to flush any possible pending stdio
300 output prior to doing a fork, to avoid the possibility of both the
301 parent and child flushing the same data after the fork. */
306 #if defined(USG) && !defined(HAVE_VFORK)
316 perror_with_name ("vfork");
324 /* Run inferior in a separate process group. */
325 #ifdef NEED_POSIX_SETPGID
326 debug_setpgrp = setpgid (0, 0);
328 #if defined(USG) && !defined(SETPGRP_ARGS)
329 debug_setpgrp = setpgrp ();
331 debug_setpgrp = setpgrp (getpid (), getpid ());
333 #endif /* NEED_POSIX_SETPGID */
334 if (debug_setpgrp == -1)
335 perror("setpgrp failed in child");
336 #endif /* TIOCGPGRP */
338 #ifdef SET_STACK_LIMIT_HUGE
339 /* Reset the stack limit back to what it was. */
343 getrlimit (RLIMIT_STACK, &rlim);
344 rlim.rlim_cur = original_stack_limit;
345 setrlimit (RLIMIT_STACK, &rlim);
347 #endif /* SET_STACK_LIMIT_HUGE */
349 /* Ask the tty subsystem to switch to the one we specified earlier
350 (or to share the current terminal, if none was specified). */
354 /* Changing the signal handlers for the inferior after
355 a vfork can also change them for the superior, so we don't mess
356 with signals here. See comments in
357 initialize_signals for how we get the right signal handlers
361 /* Use SVR4 /proc interface */
362 proc_set_exec_trap ();
364 /* "Trace me, Dr. Memory!" */
365 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
368 /* There is no execlpe call, so we have to set the environment
369 for our child in the global variable. If we've vforked, this
370 clobbers the parent, but environ is restored a few lines down
371 in the parent. By the way, yes we do need to look down the
372 path to find $SHELL. Rich Pixley says so, and I agree. */
374 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
376 fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
377 safe_strerror (errno));
382 /* Restore our environment in case a vforked child clob'd it. */
383 environ = save_our_env;
385 /* Now that we have a child process, make it our target. */
386 push_target (&child_ops);
388 #ifdef CREATE_INFERIOR_HOOK
389 CREATE_INFERIOR_HOOK (pid);
392 /* The process was started by the fork that created it,
393 but it will have stopped one instruction after execing the shell.
394 Here we must get it up to actual execution of the real program. */
396 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
398 clear_proceed_status ();
400 /* We will get a trace trap after one instruction.
401 Continue it automatically. Eventually (after shell does an exec)
402 it will get another trace trap. Then insert breakpoints and continue. */
404 #ifdef START_INFERIOR_TRAPS_EXPECTED
405 pending_execs = START_INFERIOR_TRAPS_EXPECTED;
410 init_wait_for_inferior ();
412 /* Set up the "saved terminal modes" of the inferior
413 based on what modes we are starting it with. */
414 target_terminal_init ();
416 /* Install inferior's terminal modes. */
417 target_terminal_inferior ();
421 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
422 wait_for_inferior ();
423 if (stop_signal != SIGTRAP)
425 /* Let shell child handle its own signals in its own way */
426 /* FIXME, what if child has exit()ed? Must exit loop somehow */
427 resume (0, stop_signal);
431 /* We handle SIGTRAP, however; it means child did an exec. */
432 if (0 == --pending_execs)
434 resume (0, 0); /* Just make it go on */
437 stop_soon_quietly = 0;
439 /* We are now in the child process of interest, having exec'd the
440 correct program, and are poised at the first instruction of the
442 #ifdef SOLIB_CREATE_INFERIOR_HOOK
443 SOLIB_CREATE_INFERIOR_HOOK (pid);
446 /* Pedal to the metal. Away we go. */
447 proceed ((CORE_ADDR) -1, 0, 0);
451 child_mourn_inferior ()
453 unpush_target (&child_ops);
454 generic_mourn_inferior ();
463 struct target_ops child_ops = {
464 "child", /* to_shortname */
465 "Unix child process", /* to_longname */
466 "Unix child process (started by the \"run\" command).", /* to_doc */
467 child_open, /* to_open */
469 child_attach, /* to_attach */
470 child_detach, /* to_detach */
471 child_resume, /* to_resume */
472 child_wait, /* to_wait */
473 fetch_inferior_registers, /* to_fetch_registers */
474 store_inferior_registers, /* to_store_registers */
475 child_prepare_to_store, /* to_prepare_to_store */
476 child_xfer_memory, /* to_xfer_memory */
477 child_files_info, /* to_files_info */
478 memory_insert_breakpoint, /* to_insert_breakpoint */
479 memory_remove_breakpoint, /* to_remove_breakpoint */
480 terminal_init_inferior, /* to_terminal_init */
481 terminal_inferior, /* to_terminal_inferior */
482 terminal_ours_for_output, /* to_terminal_ours_for_output */
483 terminal_ours, /* to_terminal_ours */
484 child_terminal_info, /* to_terminal_info */
485 kill_inferior, /* to_kill */
487 0, /* to_lookup_symbol */
488 child_create_inferior, /* to_create_inferior */
489 child_mourn_inferior, /* to_mourn_inferior */
490 child_can_run, /* to_can_run */
491 process_stratum, /* to_stratum */
493 1, /* to_has_all_memory */
494 1, /* to_has_memory */
495 1, /* to_has_stack */
496 1, /* to_has_registers */
497 1, /* to_has_execution */
499 0, /* sections_end */
500 OPS_MAGIC /* to_magic */
504 _initialize_inftarg ()
506 add_target (&child_ops);