1 /* Target-vector operations for controlling Unix child processes, for GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB.
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.
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.
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. */
22 #include "frame.h" /* required by inferior.h */
27 #include "ieee-float.h" /* Required by REGISTER_CONVERT_TO_XXX */
28 #include "terminal.h" /* For #ifdef TIOCGPGRP and new_tty */
32 #ifdef SET_STACK_LIMIT_HUGE
34 #include <sys/resource.h>
36 extern int original_stack_limit;
37 #endif /* SET_STACK_LIMIT_HUGE */
40 child_prepare_to_store PARAMS ((void));
43 child_wait PARAMS ((int *));
46 child_open PARAMS ((char *, int));
49 child_files_info PARAMS ((struct target_ops *));
52 child_detach PARAMS ((char *, int));
55 child_attach PARAMS ((char *, int));
58 child_create_inferior PARAMS ((char *, char *, char **));
61 child_mourn_inferior PARAMS ((void));
64 child_can_run PARAMS ((void));
66 extern char **environ;
68 /* Forward declaration */
69 extern struct target_ops child_ops;
71 /* Wait for child to do something. Return pid of child, or -1 in case
72 of error; store status through argument pointer STATUS. */
82 pid = proc_wait (status);
86 if (pid == -1) /* No more children to wait for */
88 fprintf (stderr, "Child process unexpectedly missing.\n");
89 *status = 42; /* Claim it exited with signal 42 */
92 } while (pid != inferior_pid); /* Some other child died or stopped */
97 /* Attach to process PID, then initialize for debugging it. */
100 child_attach (args, from_tty)
108 error_no_arg ("process-id to attach");
110 #ifndef ATTACH_DETACH
111 error ("Can't attach to a process on this machine.");
115 if (pid == getpid()) /* Trying to masturbate? */
116 error ("I refuse to debug myself!");
120 exec_file = (char *) get_exec_file (0);
123 printf ("Attaching program `%s', pid %d\n", exec_file, pid);
125 printf ("Attaching pid %d\n", pid);
132 push_target (&child_ops);
133 #endif /* ATTACH_DETACH */
137 /* Take a program previously attached to and detaches it.
138 The program resumes execution and will no longer stop
139 on signals, etc. We'd better not have left any breakpoints
140 in the program or it'll die when it hits one. For this
141 to work, it may be necessary for the process to have been
142 previously attached. It *might* work if the program was
143 started via the normal ptrace (PTRACE_TRACEME). */
146 child_detach (args, from_tty)
155 char *exec_file = get_exec_file (0);
158 printf ("Detaching program: %s pid %d\n",
159 exec_file, inferior_pid);
163 siggnal = atoi (args);
167 unpush_target (&child_ops); /* Pop out of handling an inferior */
169 error ("This version of Unix does not support detaching a process.");
173 /* Get ready to modify the registers array. On machines which store
174 individual registers, this doesn't need to do anything. On machines
175 which store all the registers in one fell swoop, this makes sure
176 that registers contains all the registers from the program being
180 child_prepare_to_store ()
182 #ifdef CHILD_PREPARE_TO_STORE
183 CHILD_PREPARE_TO_STORE ();
187 /* Print status information about what we're accessing. */
190 child_files_info (ignore)
191 struct target_ops *ignore;
193 printf ("\tUsing the running image of %s process %d.\n",
194 attach_flag? "attached": "child", inferior_pid);
199 child_open (arg, from_tty)
203 error ("Use the \"run\" command to start a Unix child process.");
206 /* Start an inferior Unix child process and sets inferior_pid to its pid.
207 EXEC_FILE is the file to run.
208 ALLARGS is a string containing the arguments to the program.
209 ENV is the environment vector to pass. Errors reported with error(). */
212 #define SHELL_FILE "/bin/sh"
216 child_create_inferior (exec_file, allargs, env)
224 static char default_shell_file[] = SHELL_FILE;
227 /* Set debug_fork then attach to the child while it sleeps, to debug. */
228 static int debug_fork = 0;
229 /* This is set to the result of setpgrp, which if vforked, will be visible
230 to you in the parent process. It's only used by humans for debugging. */
231 static int debug_setpgrp = 657473;
234 /* If no exec file handed to us, get it from the exec-file command -- with
235 a good, common error message if none is specified. */
237 exec_file = get_exec_file(1);
239 /* The user might want tilde-expansion, and in general probably wants
240 the program to behave the same way as if run from
241 his/her favorite shell. So we let the shell run it for us.
242 FIXME, this should probably search the local environment (as
243 modified by the setenv command), not the env gdb inherited. */
244 shell_file = getenv ("SHELL");
245 if (shell_file == NULL)
246 shell_file = default_shell_file;
248 len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
249 /* If desired, concat something onto the front of ALLARGS.
250 SHELL_COMMAND is the result. */
251 #ifdef SHELL_COMMAND_CONCAT
252 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
253 strcpy (shell_command, SHELL_COMMAND_CONCAT);
255 shell_command = (char *) alloca (len);
256 shell_command[0] = '\0';
258 strcat (shell_command, "exec ");
259 strcat (shell_command, exec_file);
260 strcat (shell_command, " ");
261 strcat (shell_command, allargs);
263 /* exec is said to fail if the executable is open. */
266 /* Retain a copy of our environment variables, since the child will
267 replace the value of environ and if we're vforked, we have to
269 save_our_env = environ;
271 /* Tell the terminal handling subsystem what tty we plan to run on;
272 it will just record the information for later. */
274 new_tty_prefork (inferior_io_terminal);
276 /* It is generally good practice to flush any possible pending stdio
277 output prior to doing a fork, to avoid the possibility of both the
278 parent and child flushing the same data after the fork. */
283 #if defined(USG) && !defined(HAVE_VFORK)
293 perror_with_name ("vfork");
301 /* Run inferior in a separate process group. */
302 #ifdef NEED_POSIX_SETPGID
303 debug_setpgrp = setpgid (0, 0);
305 #if defined(USG) && !defined(SETPGRP_ARGS)
306 debug_setpgrp = setpgrp ();
308 debug_setpgrp = setpgrp (getpid (), getpid ());
310 #endif /* NEED_POSIX_SETPGID */
311 if (debug_setpgrp == -1)
312 perror("setpgrp failed in child");
313 #endif /* TIOCGPGRP */
315 #ifdef SET_STACK_LIMIT_HUGE
316 /* Reset the stack limit back to what it was. */
320 getrlimit (RLIMIT_STACK, &rlim);
321 rlim.rlim_cur = original_stack_limit;
322 setrlimit (RLIMIT_STACK, &rlim);
324 #endif /* SET_STACK_LIMIT_HUGE */
326 /* Ask the tty subsystem to switch to the one we specified earlier
327 (or to share the current terminal, if none was specified). */
331 /* Changing the signal handlers for the inferior after
332 a vfork can also change them for the superior, so we don't mess
333 with signals here. See comments in
334 initialize_signals for how we get the right signal handlers
338 /* Use SVR4 /proc interface */
339 proc_set_exec_trap ();
341 /* "Trace me, Dr. Memory!" */
342 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
345 /* There is no execlpe call, so we have to set the environment
346 for our child in the global variable. If we've vforked, this
347 clobbers the parent, but environ is restored a few lines down
348 in the parent. By the way, yes we do need to look down the
349 path to find $SHELL. Rich Pixley says so, and I agree. */
351 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
353 fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
354 safe_strerror (errno));
359 /* Restore our environment in case a vforked child clob'd it. */
360 environ = save_our_env;
362 /* Now that we have a child process, make it our target. */
363 push_target (&child_ops);
365 #ifdef CREATE_INFERIOR_HOOK
366 CREATE_INFERIOR_HOOK (pid);
369 /* The process was started by the fork that created it,
370 but it will have stopped one instruction after execing the shell.
371 Here we must get it up to actual execution of the real program. */
373 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
375 clear_proceed_status ();
377 /* We will get a trace trap after one instruction.
378 Continue it automatically. Eventually (after shell does an exec)
379 it will get another trace trap. Then insert breakpoints and continue. */
381 #ifdef START_INFERIOR_TRAPS_EXPECTED
382 pending_execs = START_INFERIOR_TRAPS_EXPECTED;
387 init_wait_for_inferior ();
389 /* Set up the "saved terminal modes" of the inferior
390 based on what modes we are starting it with. */
391 target_terminal_init ();
393 /* Install inferior's terminal modes. */
394 target_terminal_inferior ();
398 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
399 wait_for_inferior ();
400 if (stop_signal != SIGTRAP)
402 /* Let shell child handle its own signals in its own way */
403 /* FIXME, what if child has exit()ed? Must exit loop somehow */
404 resume (0, stop_signal);
408 /* We handle SIGTRAP, however; it means child did an exec. */
409 if (0 == --pending_execs)
411 resume (0, 0); /* Just make it go on */
414 stop_soon_quietly = 0;
416 /* We are now in the child process of interest, having exec'd the
417 correct program, and are poised at the first instruction of the
419 #ifdef SOLIB_CREATE_INFERIOR_HOOK
420 SOLIB_CREATE_INFERIOR_HOOK (pid);
423 /* Pedal to the metal. Away we go. */
424 proceed ((CORE_ADDR) -1, 0, 0);
428 child_mourn_inferior ()
430 unpush_target (&child_ops);
431 generic_mourn_inferior ();
440 struct target_ops child_ops = {
441 "child", /* to_shortname */
442 "Unix child process", /* to_longname */
443 "Unix child process (started by the \"run\" command).", /* to_doc */
444 child_open, /* to_open */
446 child_attach, /* to_attach */
447 child_detach, /* to_detach */
448 child_resume, /* to_resume */
449 child_wait, /* to_wait */
450 fetch_inferior_registers, /* to_fetch_registers */
451 store_inferior_registers, /* to_store_registers */
452 child_prepare_to_store, /* to_prepare_to_store */
453 child_xfer_memory, /* to_xfer_memory */
454 child_files_info, /* to_files_info */
455 memory_insert_breakpoint, /* to_insert_breakpoint */
456 memory_remove_breakpoint, /* to_remove_breakpoint */
457 terminal_init_inferior, /* to_terminal_init */
458 terminal_inferior, /* to_terminal_inferior */
459 terminal_ours_for_output, /* to_terminal_ours_for_output */
460 terminal_ours, /* to_terminal_ours */
461 child_terminal_info, /* to_terminal_info */
462 kill_inferior, /* to_kill */
464 0, /* to_lookup_symbol */
465 child_create_inferior, /* to_create_inferior */
466 child_mourn_inferior, /* to_mourn_inferior */
467 child_can_run, /* to_can_run */
468 process_stratum, /* to_stratum */
470 1, /* to_has_all_memory */
471 1, /* to_has_memory */
472 1, /* to_has_stack */
473 1, /* to_has_registers */
474 1, /* to_has_execution */
476 0, /* sections_end */
477 OPS_MAGIC /* to_magic */
481 _initialize_inftarg ()
483 add_target (&child_ops);