1 /* Target-vector operations for controlling Unix child processes, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "frame.h" /* required by inferior.h */
29 #include <sys/types.h>
35 # ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h>
40 /* "wait.h" fills in the gaps left by <wait.h> */
44 child_prepare_to_store
PARAMS ((void));
47 static int child_wait
PARAMS ((int, struct target_waitstatus
*));
48 #endif /* CHILD_WAIT */
50 static void child_open
PARAMS ((char *, int));
53 child_files_info
PARAMS ((struct target_ops
*));
56 child_detach
PARAMS ((char *, int));
59 child_attach
PARAMS ((char *, int));
62 ptrace_me
PARAMS ((void));
65 ptrace_him
PARAMS ((int));
67 static void child_create_inferior
PARAMS ((char *, char *, char **));
70 child_mourn_inferior
PARAMS ((void));
73 child_can_run
PARAMS ((void));
76 proc_wait
PARAMS ((int, int*));
79 child_stop
PARAMS ((void));
81 #ifndef CHILD_THREAD_ALIVE
82 static int child_thread_alive
PARAMS ((int));
85 extern char **environ
;
87 /* Forward declaration */
88 extern struct target_ops child_ops
;
90 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
91 be a runnable target. Used by targets
92 that can sit atop inftarg, such as HPUX
95 proc_wait (pid
, status
)
100 return wait (status
);
106 /* Wait for child to do something. Return pid of child, or -1 in case
107 of error; store status through argument pointer OURSTATUS. */
110 child_wait (pid
, ourstatus
)
112 struct target_waitstatus
*ourstatus
;
118 set_sigint_trap(); /* Causes SIGINT to be passed on to the
122 pid
= proc_wait (inferior_pid
, &status
);
131 if (save_errno
== EINTR
)
133 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
134 safe_strerror (save_errno
));
135 /* Claim it exited with unknown signal. */
136 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
137 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
140 } while (pid
!= inferior_pid
); /* Some other child died or stopped */
141 store_waitstatus (ourstatus
, status
);
144 #endif /* CHILD_WAIT */
146 #ifndef CHILD_THREAD_ALIVE
148 /* Check to see if the given thread is alive.
150 FIXME: Is kill() ever the right way to do this? I doubt it, but
151 for now we're going to try and be compatable with the old thread
154 child_thread_alive (pid
)
157 return (kill (pid
, 0) != -1);
162 /* Attach to process PID, then initialize for debugging it. */
165 child_attach (args
, from_tty
)
170 error_no_arg ("process-id to attach");
172 #ifndef ATTACH_DETACH
173 error ("Can't attach to a process on this machine.");
181 if (pid
== getpid()) /* Trying to masturbate? */
182 error ("I refuse to debug myself!");
186 exec_file
= (char *) get_exec_file (0);
189 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
190 target_pid_to_str (pid
));
192 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
194 gdb_flush (gdb_stdout
);
199 push_target (&child_ops
);
201 #endif /* ATTACH_DETACH */
205 /* Take a program previously attached to and detaches it.
206 The program resumes execution and will no longer stop
207 on signals, etc. We'd better not have left any breakpoints
208 in the program or it'll die when it hits one. For this
209 to work, it may be necessary for the process to have been
210 previously attached. It *might* work if the program was
211 started via the normal ptrace (PTRACE_TRACEME). */
214 child_detach (args
, from_tty
)
224 char *exec_file
= get_exec_file (0);
227 printf_unfiltered ("Detaching from program: %s %s\n", exec_file
,
228 target_pid_to_str (inferior_pid
));
229 gdb_flush (gdb_stdout
);
232 siggnal
= atoi (args
);
236 unpush_target (&child_ops
);
239 error ("This version of Unix does not support detaching a process.");
243 /* Get ready to modify the registers array. On machines which store
244 individual registers, this doesn't need to do anything. On machines
245 which store all the registers in one fell swoop, this makes sure
246 that registers contains all the registers from the program being
250 child_prepare_to_store ()
252 #ifdef CHILD_PREPARE_TO_STORE
253 CHILD_PREPARE_TO_STORE ();
257 /* Print status information about what we're accessing. */
260 child_files_info (ignore
)
261 struct target_ops
*ignore
;
263 printf_unfiltered ("\tUsing the running image of %s %s.\n",
264 attach_flag
? "attached": "child", target_pid_to_str (inferior_pid
));
269 child_open (arg
, from_tty
)
273 error ("Use the \"run\" command to start a Unix child process.");
276 /* Stub function which causes the inferior that runs it, to be ptrace-able
277 by its parent process. */
282 /* "Trace me, Dr. Memory!" */
283 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
286 /* Stub function which causes the GDB that runs it, to start ptrace-ing
287 the child process. */
293 push_target (&child_ops
);
295 #ifdef START_INFERIOR_TRAPS_EXPECTED
296 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
298 /* One trap to exec the shell, one to exec the program being debugged. */
299 startup_inferior (2);
305 /* Start an inferior Unix child process and sets inferior_pid to its pid.
306 EXEC_FILE is the file to run.
307 ALLARGS is a string containing the arguments to the program.
308 ENV is the environment vector to pass. Errors reported with error(). */
311 child_create_inferior (exec_file
, allargs
, env
)
316 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
);
317 /* We are at the first instruction we care about. */
318 /* Pedal to the metal... */
319 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
323 child_mourn_inferior ()
325 /* FIXME: Should be in a header file */
326 extern void proc_remove_foreign
PARAMS ((int));
328 unpush_target (&child_ops
);
329 proc_remove_foreign (inferior_pid
);
330 generic_mourn_inferior ();
336 /* This variable is controlled by modules that sit atop inftarg that may layer
337 their own process structure atop that provided here. hpux-thread.c does
338 this because of the Hpux user-mode level thread model. */
340 return !child_suppress_run
;
343 /* Send a SIGINT to the process group. This acts just like the user typed a
344 ^C on the controlling terminal.
346 XXX - This may not be correct for all systems. Some may want to use
347 killpg() instead of kill (-pgrp). */
352 extern pid_t inferior_process_group
;
354 kill (-inferior_process_group
, SIGINT
);
357 struct target_ops child_ops
= {
358 "child", /* to_shortname */
359 "Unix child process", /* to_longname */
360 "Unix child process (started by the \"run\" command).", /* to_doc */
361 child_open
, /* to_open */
363 child_attach
, /* to_attach */
364 NULL
, /* to_post_attach */
365 NULL
, /* to_require_attach */
366 child_detach
, /* to_detach */
367 NULL
, /* to_require_detach */
368 child_resume
, /* to_resume */
369 child_wait
, /* to_wait */
370 NULL
, /* to_post_wait */
371 fetch_inferior_registers
, /* to_fetch_registers */
372 store_inferior_registers
, /* to_store_registers */
373 child_prepare_to_store
, /* to_prepare_to_store */
374 child_xfer_memory
, /* to_xfer_memory */
375 child_files_info
, /* to_files_info */
376 memory_insert_breakpoint
, /* to_insert_breakpoint */
377 memory_remove_breakpoint
, /* to_remove_breakpoint */
378 terminal_init_inferior
, /* to_terminal_init */
379 terminal_inferior
, /* to_terminal_inferior */
380 terminal_ours_for_output
, /* to_terminal_ours_for_output */
381 terminal_ours
, /* to_terminal_ours */
382 child_terminal_info
, /* to_terminal_info */
383 kill_inferior
, /* to_kill */
385 0, /* to_lookup_symbol */
386 child_create_inferior
, /* to_create_inferior */
387 NULL
, /* to_post_startup_inferior */
388 NULL
, /* to_acknowledge_created_inferior */
389 NULL
, /* to_clone_and_follow_inferior */
390 NULL
, /* to_post_follow_inferior_by_clone */
391 NULL
, /* to_insert_fork_catchpoint */
392 NULL
, /* to_remove_fork_catchpoint */
393 NULL
, /* to_insert_vfork_catchpoint */
394 NULL
, /* to_remove_vfork_catchpoint */
395 NULL
, /* to_has_forked */
396 NULL
, /* to_has_vforked */
397 NULL
, /* to_can_follow_vfork_prior_to_exec */
398 NULL
, /* to_post_follow_vfork */
399 NULL
, /* to_insert_exec_catchpoint */
400 NULL
, /* to_remove_exec_catchpoint */
401 NULL
, /* to_has_execd */
402 NULL
, /* to_reported_exec_events_per_exec_call */
403 NULL
, /* to_has_syscall_event */
404 NULL
, /* to_has_exited */
405 child_mourn_inferior
, /* to_mourn_inferior */
406 child_can_run
, /* to_can_run */
407 0, /* to_notice_signals */
408 child_thread_alive
, /* to_thread_alive */
409 child_stop
, /* to_stop */
410 NULL
, /* to_enable_exception_callback */
411 NULL
, /* to_get_current_exception_event */
412 NULL
, /* to_pid_to_exec_file */
413 NULL
, /* to_core_file_to_sym_file */
414 process_stratum
, /* to_stratum */
416 1, /* to_has_all_memory */
417 1, /* to_has_memory */
418 1, /* to_has_stack */
419 1, /* to_has_registers */
420 1, /* to_has_execution */
422 0, /* to_sections_end */
423 OPS_MAGIC
/* to_magic */
427 _initialize_inftarg ()
429 #ifdef HAVE_OPTIONAL_PROC_FS
433 /* If we have an optional /proc filesystem (e.g. under OSF/1),
434 don't add ptrace support if we can access the running GDB via /proc. */
435 #ifndef PROC_NAME_FMT
436 #define PROC_NAME_FMT "/proc/%05d"
438 sprintf (procname
, PROC_NAME_FMT
, getpid ());
439 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
446 add_target (&child_ops
);