1 /* Target-vector operations for controlling Unix child processes, for GDB.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 2000, 2002, 2003 Free Software Foundation, Inc.
6 Contributed by Cygnus Support.
8 ## Contains temporary hacks..
10 This file is part of GDB.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. */
28 #include "frame.h" /* required by inferior.h */
35 #include <sys/types.h>
40 extern struct symtab_and_line *child_enable_exception_callback (enum
44 extern struct exception_event_record
45 *child_get_current_exception_event (void);
47 extern void _initialize_inftarg (void);
49 static void child_prepare_to_store (void);
52 static ptid_t child_wait (ptid_t, struct target_waitstatus *);
53 #endif /* CHILD_WAIT */
55 #if !defined(CHILD_POST_WAIT)
56 void child_post_wait (ptid_t, int);
59 static void child_open (char *, int);
61 static void child_files_info (struct target_ops *);
63 static void child_detach (char *, int);
65 static void child_attach (char *, int);
67 #if !defined(CHILD_POST_ATTACH)
68 extern void child_post_attach (int);
71 static void ptrace_me (void);
73 static void ptrace_him (int);
75 static void child_create_inferior (char *, char *, char **);
77 static void child_mourn_inferior (void);
79 static int child_can_run (void);
81 static void child_stop (void);
83 #ifndef CHILD_THREAD_ALIVE
84 int child_thread_alive (ptid_t);
87 static void init_child_ops (void);
89 extern char **environ;
91 struct target_ops child_ops;
93 int child_suppress_run = 0; /* Non-zero if inftarg should pretend not to
94 be a runnable target. Used by targets
95 that can sit atop inftarg, such as HPUX
100 /* Wait for child to do something. Return pid of child, or -1 in case
101 of error; store status through argument pointer OURSTATUS. */
104 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
108 char *execd_pathname = NULL;
112 enum target_waitkind kind;
117 set_sigint_trap (); /* Causes SIGINT to be passed on to the
121 pid = ptrace_wait (inferior_ptid, &status);
127 clear_sigint_trap ();
131 if (save_errno == EINTR)
134 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
135 safe_strerror (save_errno));
137 /* Claim it exited with unknown signal. */
138 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
139 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
140 return pid_to_ptid (-1);
145 if (target_has_exited (pid, status, &exit_status))
147 /* ??rehrauer: For now, ignore this. */
151 if (!target_thread_alive (pid_to_ptid (pid)))
153 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
154 return pid_to_ptid (pid);
156 } while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */
158 store_waitstatus (ourstatus, status);
159 return pid_to_ptid (pid);
161 #endif /* CHILD_WAIT */
163 #if !defined(CHILD_POST_WAIT)
165 child_post_wait (ptid_t ptid, int wait_status)
167 /* This version of Unix doesn't require a meaningful "post wait"
174 #ifndef CHILD_THREAD_ALIVE
176 /* Check to see if the given thread is alive.
178 FIXME: Is kill() ever the right way to do this? I doubt it, but
179 for now we're going to try and be compatable with the old thread
182 child_thread_alive (ptid_t ptid)
184 pid_t pid = PIDGET (ptid);
186 return (kill (pid, 0) != -1);
191 /* Attach to process PID, then initialize for debugging it. */
194 child_attach (char *args, int from_tty)
197 error_no_arg ("process-id to attach");
199 #ifndef ATTACH_DETACH
200 error ("Can't attach to a process on this machine.");
208 pid = strtol (args, &dummy, 0);
209 /* Some targets don't set errno on errors, grrr! */
210 if ((pid == 0) && (args == dummy))
211 error ("Illegal process-id: %s\n", args);
213 if (pid == getpid ()) /* Trying to masturbate? */
214 error ("I refuse to debug myself!");
218 exec_file = (char *) get_exec_file (0);
221 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
222 target_pid_to_str (pid_to_ptid (pid)));
224 printf_unfiltered ("Attaching to %s\n",
225 target_pid_to_str (pid_to_ptid (pid)));
227 gdb_flush (gdb_stdout);
232 inferior_ptid = pid_to_ptid (pid);
233 push_target (&child_ops);
235 #endif /* ATTACH_DETACH */
238 #if !defined(CHILD_POST_ATTACH)
240 child_post_attach (int pid)
242 /* This version of Unix doesn't require a meaningful "post attach"
243 operation by a debugger. */
247 /* Take a program previously attached to and detaches it.
248 The program resumes execution and will no longer stop
249 on signals, etc. We'd better not have left any breakpoints
250 in the program or it'll die when it hits one. For this
251 to work, it may be necessary for the process to have been
252 previously attached. It *might* work if the program was
253 started via the normal ptrace (PTRACE_TRACEME). */
256 child_detach (char *args, int from_tty)
261 int pid = PIDGET (inferior_ptid);
265 char *exec_file = get_exec_file (0);
268 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
269 target_pid_to_str (pid_to_ptid (pid)));
270 gdb_flush (gdb_stdout);
273 siggnal = atoi (args);
277 inferior_ptid = null_ptid;
278 unpush_target (&child_ops);
281 error ("This version of Unix does not support detaching a process.");
285 /* Get ready to modify the registers array. On machines which store
286 individual registers, this doesn't need to do anything. On machines
287 which store all the registers in one fell swoop, this makes sure
288 that registers contains all the registers from the program being
292 child_prepare_to_store (void)
294 #ifdef CHILD_PREPARE_TO_STORE
295 CHILD_PREPARE_TO_STORE ();
299 /* Print status information about what we're accessing. */
302 child_files_info (struct target_ops *ignore)
304 printf_unfiltered ("\tUsing the running image of %s %s.\n",
305 attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
310 child_open (char *arg, int from_tty)
312 error ("Use the \"run\" command to start a Unix child process.");
315 /* Stub function which causes the inferior that runs it, to be ptrace-able
316 by its parent process. */
321 /* "Trace me, Dr. Memory!" */
322 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
325 /* Stub function which causes the GDB that runs it, to start ptrace-ing
326 the child process. */
331 push_target (&child_ops);
333 /* On some targets, there must be some explicit synchronization
334 between the parent and child processes after the debugger
335 forks, and before the child execs the debuggee program. This
336 call basically gives permission for the child to exec.
339 target_acknowledge_created_inferior (pid);
341 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
342 * and will be 1 or 2 depending on whether we're starting
343 * without or with a shell.
345 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
347 /* On some targets, there must be some explicit actions taken after
348 the inferior has been started up.
350 target_post_startup_inferior (pid_to_ptid (pid));
353 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
354 EXEC_FILE is the file to run.
355 ALLARGS is a string containing the arguments to the program.
356 ENV is the environment vector to pass. Errors reported with error(). */
359 child_create_inferior (char *exec_file, char *allargs, char **env)
362 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, pre_fork_inferior, NULL);
364 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL, NULL);
366 /* We are at the first instruction we care about. */
367 /* Pedal to the metal... */
368 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
371 #if !defined(CHILD_POST_STARTUP_INFERIOR)
373 child_post_startup_inferior (ptid_t ptid)
375 /* This version of Unix doesn't require a meaningful "post startup inferior"
376 operation by a debugger.
381 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
383 child_acknowledge_created_inferior (int pid)
385 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
386 operation by a debugger.
392 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
394 child_insert_fork_catchpoint (int pid)
396 /* This version of Unix doesn't support notification of fork events. */
401 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
403 child_remove_fork_catchpoint (int pid)
405 /* This version of Unix doesn't support notification of fork events. */
410 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
412 child_insert_vfork_catchpoint (int pid)
414 /* This version of Unix doesn't support notification of vfork events. */
419 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
421 child_remove_vfork_catchpoint (int pid)
423 /* This version of Unix doesn't support notification of vfork events. */
428 #if !defined(CHILD_FOLLOW_FORK)
430 child_follow_fork (int follow_child)
432 /* This version of Unix doesn't support following fork or vfork events. */
437 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
439 child_insert_exec_catchpoint (int pid)
441 /* This version of Unix doesn't support notification of exec events. */
446 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
448 child_remove_exec_catchpoint (int pid)
450 /* This version of Unix doesn't support notification of exec events. */
455 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
457 child_reported_exec_events_per_exec_call (void)
459 /* This version of Unix doesn't support notification of exec events.
465 #if !defined(CHILD_HAS_EXITED)
467 child_has_exited (int pid, int wait_status, int *exit_status)
469 if (WIFEXITED (wait_status))
471 *exit_status = WEXITSTATUS (wait_status);
475 if (WIFSIGNALED (wait_status))
477 *exit_status = 0; /* ?? Don't know what else to say here. */
481 /* ?? Do we really need to consult the event state, too? Assume the
482 wait_state alone suffices.
490 child_mourn_inferior (void)
492 unpush_target (&child_ops);
493 generic_mourn_inferior ();
499 /* This variable is controlled by modules that sit atop inftarg that may layer
500 their own process structure atop that provided here. hpux-thread.c does
501 this because of the Hpux user-mode level thread model. */
503 return !child_suppress_run;
506 /* Send a SIGINT to the process group. This acts just like the user typed a
507 ^C on the controlling terminal.
509 XXX - This may not be correct for all systems. Some may want to use
510 killpg() instead of kill (-pgrp). */
515 extern pid_t inferior_process_group;
517 kill (-inferior_process_group, SIGINT);
520 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
521 struct symtab_and_line *
522 child_enable_exception_callback (enum exception_event_kind kind, int enable)
524 return (struct symtab_and_line *) NULL;
528 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
529 struct exception_event_record *
530 child_get_current_exception_event (void)
532 return (struct exception_event_record *) NULL;
537 #if !defined(CHILD_PID_TO_EXEC_FILE)
539 child_pid_to_exec_file (int pid)
541 /* This version of Unix doesn't support translation of a process ID
542 to the filename of the executable file.
549 child_core_file_to_sym_file (char *core)
551 /* The target stratum for a running executable need not support
558 #if !defined(CHILD_PID_TO_STR)
560 child_pid_to_str (ptid_t ptid)
562 return normal_pid_to_str (ptid);
567 init_child_ops (void)
569 child_ops.to_shortname = "child";
570 child_ops.to_longname = "Unix child process";
571 child_ops.to_doc = "Unix child process (started by the \"run\" command).";
572 child_ops.to_open = child_open;
573 child_ops.to_attach = child_attach;
574 child_ops.to_post_attach = child_post_attach;
575 child_ops.to_detach = child_detach;
576 child_ops.to_resume = child_resume;
577 child_ops.to_wait = child_wait;
578 child_ops.to_post_wait = child_post_wait;
579 child_ops.to_fetch_registers = fetch_inferior_registers;
580 child_ops.to_store_registers = store_inferior_registers;
581 child_ops.to_prepare_to_store = child_prepare_to_store;
582 child_ops.to_xfer_memory = child_xfer_memory;
583 child_ops.to_files_info = child_files_info;
584 child_ops.to_insert_breakpoint = memory_insert_breakpoint;
585 child_ops.to_remove_breakpoint = memory_remove_breakpoint;
586 child_ops.to_terminal_init = terminal_init_inferior;
587 child_ops.to_terminal_inferior = terminal_inferior;
588 child_ops.to_terminal_ours_for_output = terminal_ours_for_output;
589 child_ops.to_terminal_save_ours = terminal_save_ours;
590 child_ops.to_terminal_ours = terminal_ours;
591 child_ops.to_terminal_info = child_terminal_info;
592 child_ops.to_kill = kill_inferior;
593 child_ops.to_create_inferior = child_create_inferior;
594 child_ops.to_post_startup_inferior = child_post_startup_inferior;
595 child_ops.to_acknowledge_created_inferior = child_acknowledge_created_inferior;
596 child_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
597 child_ops.to_remove_fork_catchpoint = child_remove_fork_catchpoint;
598 child_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
599 child_ops.to_remove_vfork_catchpoint = child_remove_vfork_catchpoint;
600 child_ops.to_follow_fork = child_follow_fork;
601 child_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
602 child_ops.to_remove_exec_catchpoint = child_remove_exec_catchpoint;
603 child_ops.to_reported_exec_events_per_exec_call = child_reported_exec_events_per_exec_call;
604 child_ops.to_has_exited = child_has_exited;
605 child_ops.to_mourn_inferior = child_mourn_inferior;
606 child_ops.to_can_run = child_can_run;
607 child_ops.to_thread_alive = child_thread_alive;
608 child_ops.to_pid_to_str = child_pid_to_str;
609 child_ops.to_stop = child_stop;
610 child_ops.to_enable_exception_callback = child_enable_exception_callback;
611 child_ops.to_get_current_exception_event = child_get_current_exception_event;
612 child_ops.to_pid_to_exec_file = child_pid_to_exec_file;
613 child_ops.to_stratum = process_stratum;
614 child_ops.to_has_all_memory = 1;
615 child_ops.to_has_memory = 1;
616 child_ops.to_has_stack = 1;
617 child_ops.to_has_registers = 1;
618 child_ops.to_has_execution = 1;
619 child_ops.to_magic = OPS_MAGIC;
622 /* Take over the 'find_mapped_memory' vector from inftarg.c. */
624 inftarg_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR,
630 child_ops.to_find_memory_regions = func;
633 /* Take over the 'make_corefile_notes' vector from inftarg.c. */
635 inftarg_set_make_corefile_notes (char * (*func) (bfd *, int *))
637 child_ops.to_make_corefile_notes = func;
641 _initialize_inftarg (void)
643 #ifdef HAVE_OPTIONAL_PROC_FS
647 /* If we have an optional /proc filesystem (e.g. under OSF/1),
648 don't add ptrace support if we can access the running GDB via /proc. */
649 #ifndef PROC_NAME_FMT
650 #define PROC_NAME_FMT "/proc/%05d"
652 sprintf (procname, PROC_NAME_FMT, getpid ());
653 fd = open (procname, O_RDONLY);
662 add_target (&child_ops);