1 /* Target-vector operations for controlling Unix child processes, for GDB.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 2000, 2002, 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, 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 static void child_open (char *, int);
57 static void child_files_info (struct target_ops
*);
59 static void child_detach (char *, int);
61 static void child_attach (char *, int);
63 #if !defined(CHILD_POST_ATTACH)
64 extern void child_post_attach (int);
67 static void ptrace_me (void);
69 static void ptrace_him (int);
71 static void child_create_inferior (char *, char *, char **, int);
73 static void child_mourn_inferior (void);
75 static int child_can_run (void);
77 static void child_stop (void);
79 #ifndef CHILD_THREAD_ALIVE
80 int child_thread_alive (ptid_t
);
83 static void init_child_ops (void);
85 extern char **environ
;
87 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
88 be a runnable target. Used by targets
89 that can sit atop inftarg, such as HPUX
94 /* Wait for child to do something. Return pid of child, or -1 in case
95 of error; store status through argument pointer OURSTATUS. */
98 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
102 char *execd_pathname
= NULL
;
105 enum target_waitkind kind
;
110 set_sigint_trap (); /* Causes SIGINT to be passed on to the
114 pid
= wait (&status
);
120 clear_sigint_trap ();
124 if (save_errno
== EINTR
)
127 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
128 safe_strerror (save_errno
));
130 /* Claim it exited with unknown signal. */
131 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
132 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
133 return pid_to_ptid (-1);
138 if (target_has_exited (pid
, status
, &exit_status
))
140 /* ??rehrauer: For now, ignore this. */
144 if (!target_thread_alive (pid_to_ptid (pid
)))
146 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
147 return pid_to_ptid (pid
);
149 } while (pid
!= PIDGET (inferior_ptid
)); /* Some other child died or stopped */
151 store_waitstatus (ourstatus
, status
);
152 return pid_to_ptid (pid
);
154 #endif /* CHILD_WAIT */
156 #ifndef CHILD_THREAD_ALIVE
158 /* Check to see if the given thread is alive.
160 FIXME: Is kill() ever the right way to do this? I doubt it, but
161 for now we're going to try and be compatable with the old thread
164 child_thread_alive (ptid_t ptid
)
166 pid_t pid
= PIDGET (ptid
);
168 return (kill (pid
, 0) != -1);
173 /* Attach to process PID, then initialize for debugging it. */
176 child_attach (char *args
, int from_tty
)
183 error_no_arg (_("process-id to attach"));
186 pid
= strtol (args
, &dummy
, 0);
187 /* Some targets don't set errno on errors, grrr! */
188 if ((pid
== 0) && (args
== dummy
))
189 error (_("Illegal process-id: %s."), args
);
191 if (pid
== getpid ()) /* Trying to masturbate? */
192 error (_("I refuse to debug myself!"));
196 exec_file
= (char *) get_exec_file (0);
199 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
200 target_pid_to_str (pid_to_ptid (pid
)));
202 printf_unfiltered (_("Attaching to %s\n"),
203 target_pid_to_str (pid_to_ptid (pid
)));
205 gdb_flush (gdb_stdout
);
210 inferior_ptid
= pid_to_ptid (pid
);
211 push_target (&deprecated_child_ops
);
214 #if !defined(CHILD_POST_ATTACH)
216 child_post_attach (int pid
)
218 /* This version of Unix doesn't require a meaningful "post attach"
219 operation by a debugger. */
223 /* Take a program previously attached to and detaches it.
224 The program resumes execution and will no longer stop
225 on signals, etc. We'd better not have left any breakpoints
226 in the program or it'll die when it hits one. For this
227 to work, it may be necessary for the process to have been
228 previously attached. It *might* work if the program was
229 started via the normal ptrace (PTRACE_TRACEME). */
232 child_detach (char *args
, int from_tty
)
235 int pid
= PIDGET (inferior_ptid
);
239 char *exec_file
= get_exec_file (0);
242 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
243 target_pid_to_str (pid_to_ptid (pid
)));
244 gdb_flush (gdb_stdout
);
247 siggnal
= atoi (args
);
251 inferior_ptid
= null_ptid
;
252 unpush_target (&deprecated_child_ops
);
255 /* Get ready to modify the registers array. On machines which store
256 individual registers, this doesn't need to do anything. On machines
257 which store all the registers in one fell swoop, this makes sure
258 that registers contains all the registers from the program being
262 child_prepare_to_store (void)
264 #ifdef CHILD_PREPARE_TO_STORE
265 CHILD_PREPARE_TO_STORE ();
269 /* Print status information about what we're accessing. */
272 child_files_info (struct target_ops
*ignore
)
274 printf_unfiltered (_("\tUsing the running image of %s %s.\n"),
275 attach_flag
? "attached" : "child", target_pid_to_str (inferior_ptid
));
279 child_open (char *arg
, int from_tty
)
281 error (_("Use the \"run\" command to start a Unix child process."));
284 /* Stub function which causes the inferior that runs it, to be ptrace-able
285 by its parent process. */
290 /* "Trace me, Dr. Memory!" */
291 call_ptrace (0, 0, (PTRACE_TYPE_ARG3
) 0, 0);
294 /* Stub function which causes the GDB that runs it, to start ptrace-ing
295 the child process. */
300 push_target (&deprecated_child_ops
);
302 /* On some targets, there must be some explicit synchronization
303 between the parent and child processes after the debugger
304 forks, and before the child execs the debuggee program. This
305 call basically gives permission for the child to exec.
308 target_acknowledge_created_inferior (pid
);
310 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
311 * and will be 1 or 2 depending on whether we're starting
312 * without or with a shell.
314 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
316 /* On some targets, there must be some explicit actions taken after
317 the inferior has been started up.
319 target_post_startup_inferior (pid_to_ptid (pid
));
322 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
323 EXEC_FILE is the file to run.
324 ALLARGS is a string containing the arguments to the program.
325 ENV is the environment vector to pass. Errors reported with error(). */
328 child_create_inferior (char *exec_file
, char *allargs
, char **env
,
331 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
334 #if !defined(CHILD_POST_STARTUP_INFERIOR)
336 child_post_startup_inferior (ptid_t ptid
)
338 /* This version of Unix doesn't require a meaningful "post startup inferior"
339 operation by a debugger.
344 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
346 child_acknowledge_created_inferior (int pid
)
348 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
349 operation by a debugger.
355 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
357 child_insert_fork_catchpoint (int pid
)
359 /* This version of Unix doesn't support notification of fork
364 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
366 child_remove_fork_catchpoint (int pid
)
368 /* This version of Unix doesn't support notification of fork events. */
373 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
375 child_insert_vfork_catchpoint (int pid
)
377 /* This version of Unix doesn't support notification of vfork
382 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
384 child_remove_vfork_catchpoint (int pid
)
386 /* This version of Unix doesn't support notification of vfork events. */
391 #if !defined(CHILD_FOLLOW_FORK)
393 child_follow_fork (struct target_ops
*ops
, int follow_child
)
395 /* This version of Unix doesn't support following fork or vfork events. */
400 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
402 child_insert_exec_catchpoint (int pid
)
404 /* This version of Unix doesn't support notification of exec
409 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
411 child_remove_exec_catchpoint (int pid
)
413 /* This version of Unix doesn't support notification of exec events. */
418 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
420 child_reported_exec_events_per_exec_call (void)
422 /* This version of Unix doesn't support notification of exec events.
428 #if !defined(CHILD_HAS_EXITED)
430 child_has_exited (int pid
, int wait_status
, int *exit_status
)
432 if (WIFEXITED (wait_status
))
434 *exit_status
= WEXITSTATUS (wait_status
);
438 if (WIFSIGNALED (wait_status
))
440 *exit_status
= 0; /* ?? Don't know what else to say here. */
444 /* ?? Do we really need to consult the event state, too? Assume the
445 wait_state alone suffices.
453 child_mourn_inferior (void)
455 unpush_target (&deprecated_child_ops
);
456 generic_mourn_inferior ();
462 /* This variable is controlled by modules that sit atop inftarg that may layer
463 their own process structure atop that provided here. hpux-thread.c does
464 this because of the Hpux user-mode level thread model. */
466 return !child_suppress_run
;
469 /* Send a SIGINT to the process group. This acts just like the user typed a
470 ^C on the controlling terminal.
472 XXX - This may not be correct for all systems. Some may want to use
473 killpg() instead of kill (-pgrp). */
478 kill (-inferior_process_group
, SIGINT
);
481 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
482 struct symtab_and_line
*
483 child_enable_exception_callback (enum exception_event_kind kind
, int enable
)
485 return (struct symtab_and_line
*) NULL
;
489 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
490 struct exception_event_record
*
491 child_get_current_exception_event (void)
493 return (struct exception_event_record
*) NULL
;
498 #if !defined(CHILD_PID_TO_EXEC_FILE)
500 child_pid_to_exec_file (int pid
)
502 /* This version of Unix doesn't support translation of a process ID
503 to the filename of the executable file.
510 child_core_file_to_sym_file (char *core
)
512 /* The target stratum for a running executable need not support
518 /* Perform a partial transfer to/from the specified object. For
519 memory transfers, fall back to the old memory xfer functions. */
522 child_xfer_partial (struct target_ops
*ops
, enum target_object object
,
523 const char *annex
, gdb_byte
*readbuf
,
524 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
528 case TARGET_OBJECT_MEMORY
:
530 return child_xfer_memory (offset
, readbuf
, len
, 0/*write*/,
533 return child_xfer_memory (offset
, (void *) writebuf
, len
, 1/*write*/,
537 case TARGET_OBJECT_UNWIND_TABLE
:
538 #ifndef NATIVE_XFER_UNWIND_TABLE
539 #define NATIVE_XFER_UNWIND_TABLE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
541 return NATIVE_XFER_UNWIND_TABLE (ops
, object
, annex
, readbuf
, writebuf
,
544 case TARGET_OBJECT_AUXV
:
545 #ifndef NATIVE_XFER_AUXV
546 #define NATIVE_XFER_AUXV(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
548 return NATIVE_XFER_AUXV (ops
, object
, annex
, readbuf
, writebuf
,
556 #if !defined(CHILD_PID_TO_STR)
558 child_pid_to_str (ptid_t ptid
)
560 return normal_pid_to_str (ptid
);
565 init_child_ops (void)
567 deprecated_child_ops
.to_shortname
= "child";
568 deprecated_child_ops
.to_longname
= "Unix child process";
569 deprecated_child_ops
.to_doc
= "Unix child process (started by the \"run\" command).";
570 deprecated_child_ops
.to_open
= child_open
;
571 deprecated_child_ops
.to_attach
= child_attach
;
572 deprecated_child_ops
.to_post_attach
= child_post_attach
;
573 deprecated_child_ops
.to_detach
= child_detach
;
574 deprecated_child_ops
.to_resume
= child_resume
;
575 deprecated_child_ops
.to_wait
= child_wait
;
576 deprecated_child_ops
.to_fetch_registers
= fetch_inferior_registers
;
577 deprecated_child_ops
.to_store_registers
= store_inferior_registers
;
578 deprecated_child_ops
.to_prepare_to_store
= child_prepare_to_store
;
579 deprecated_child_ops
.deprecated_xfer_memory
= child_xfer_memory
;
580 deprecated_child_ops
.to_xfer_partial
= child_xfer_partial
;
581 deprecated_child_ops
.to_files_info
= child_files_info
;
582 deprecated_child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
583 deprecated_child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
584 deprecated_child_ops
.to_terminal_init
= terminal_init_inferior
;
585 deprecated_child_ops
.to_terminal_inferior
= terminal_inferior
;
586 deprecated_child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
587 deprecated_child_ops
.to_terminal_save_ours
= terminal_save_ours
;
588 deprecated_child_ops
.to_terminal_ours
= terminal_ours
;
589 deprecated_child_ops
.to_terminal_info
= child_terminal_info
;
590 deprecated_child_ops
.to_kill
= kill_inferior
;
591 deprecated_child_ops
.to_create_inferior
= child_create_inferior
;
592 deprecated_child_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
593 deprecated_child_ops
.to_acknowledge_created_inferior
= child_acknowledge_created_inferior
;
594 deprecated_child_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
595 deprecated_child_ops
.to_remove_fork_catchpoint
= child_remove_fork_catchpoint
;
596 deprecated_child_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
597 deprecated_child_ops
.to_remove_vfork_catchpoint
= child_remove_vfork_catchpoint
;
598 deprecated_child_ops
.to_follow_fork
= child_follow_fork
;
599 deprecated_child_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
600 deprecated_child_ops
.to_remove_exec_catchpoint
= child_remove_exec_catchpoint
;
601 deprecated_child_ops
.to_reported_exec_events_per_exec_call
= child_reported_exec_events_per_exec_call
;
602 deprecated_child_ops
.to_has_exited
= child_has_exited
;
603 deprecated_child_ops
.to_mourn_inferior
= child_mourn_inferior
;
604 deprecated_child_ops
.to_can_run
= child_can_run
;
605 deprecated_child_ops
.to_thread_alive
= child_thread_alive
;
606 deprecated_child_ops
.to_pid_to_str
= child_pid_to_str
;
607 deprecated_child_ops
.to_stop
= child_stop
;
608 deprecated_child_ops
.to_enable_exception_callback
= child_enable_exception_callback
;
609 deprecated_child_ops
.to_get_current_exception_event
= child_get_current_exception_event
;
610 deprecated_child_ops
.to_pid_to_exec_file
= child_pid_to_exec_file
;
611 deprecated_child_ops
.to_stratum
= process_stratum
;
612 deprecated_child_ops
.to_has_all_memory
= 1;
613 deprecated_child_ops
.to_has_memory
= 1;
614 deprecated_child_ops
.to_has_stack
= 1;
615 deprecated_child_ops
.to_has_registers
= 1;
616 deprecated_child_ops
.to_has_execution
= 1;
617 deprecated_child_ops
.to_magic
= OPS_MAGIC
;
621 _initialize_inftarg (void)
623 #ifdef HAVE_OPTIONAL_PROC_FS
627 /* If we have an optional /proc filesystem (e.g. under OSF/1),
628 don't add ptrace support if we can access the running GDB via /proc. */
629 #ifndef PROC_NAME_FMT
630 #define PROC_NAME_FMT "/proc/%05d"
632 sprintf (procname
, PROC_NAME_FMT
, getpid ());
633 fd
= open (procname
, O_RDONLY
);
642 add_target (&deprecated_child_ops
);