2003-01-28 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / inftarg.c
CommitLineData
c906108c 1/* Target-vector operations for controlling Unix child processes, for GDB.
1bac305b
AC
2
3 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 2000, 2002, 2003 Free Software Foundation, Inc.
5
c906108c
SS
6 Contributed by Cygnus Support.
7
c5aa993b 8 ## Contains temporary hacks..
c906108c 9
c5aa993b 10 This file is part of GDB.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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.
c906108c 21
c5aa993b
JM
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. */
c906108c
SS
26
27#include "defs.h"
c5aa993b 28#include "frame.h" /* required by inferior.h */
c906108c
SS
29#include "inferior.h"
30#include "target.h"
31#include "gdbcore.h"
32#include "command.h"
33#include "gdb_stat.h"
34#include <signal.h>
35#include <sys/types.h>
36#include <fcntl.h>
37
03f2053f 38#include "gdb_wait.h"
c906108c 39
a14ed312
KB
40extern struct symtab_and_line *child_enable_exception_callback (enum
41 exception_event_kind,
42 int);
c906108c 43
a14ed312
KB
44extern struct exception_event_record
45 *child_get_current_exception_event (void);
c906108c 46
a14ed312 47extern void _initialize_inftarg (void);
c906108c 48
a14ed312 49static void child_prepare_to_store (void);
c906108c
SS
50
51#ifndef CHILD_WAIT
39f77062 52static ptid_t child_wait (ptid_t, struct target_waitstatus *);
c906108c
SS
53#endif /* CHILD_WAIT */
54
55#if !defined(CHILD_POST_WAIT)
39f77062 56void child_post_wait (ptid_t, int);
c906108c
SS
57#endif
58
a14ed312 59static void child_open (char *, int);
c906108c 60
a14ed312 61static void child_files_info (struct target_ops *);
c906108c 62
a14ed312 63static void child_detach (char *, int);
c906108c 64
a14ed312 65static void child_attach (char *, int);
c906108c 66
c906108c 67#if !defined(CHILD_POST_ATTACH)
a14ed312 68extern void child_post_attach (int);
c906108c
SS
69#endif
70
a14ed312 71static void ptrace_me (void);
c906108c 72
a14ed312 73static void ptrace_him (int);
c906108c 74
a14ed312 75static void child_create_inferior (char *, char *, char **);
c906108c 76
a14ed312 77static void child_mourn_inferior (void);
c906108c 78
a14ed312 79static int child_can_run (void);
c906108c 80
a14ed312 81static void child_stop (void);
c906108c
SS
82
83#ifndef CHILD_THREAD_ALIVE
39f77062 84int child_thread_alive (ptid_t);
c906108c
SS
85#endif
86
a14ed312 87static void init_child_ops (void);
c906108c
SS
88
89extern char **environ;
90
91struct target_ops child_ops;
92
93int 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
96 thread support. */
97
98#ifndef CHILD_WAIT
99
c906108c
SS
100/* Wait for child to do something. Return pid of child, or -1 in case
101 of error; store status through argument pointer OURSTATUS. */
102
39f77062
KB
103static ptid_t
104child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
c906108c
SS
105{
106 int save_errno;
107 int status;
7a292a7a 108 char *execd_pathname = NULL;
c5aa993b
JM
109 int exit_status;
110 int related_pid;
111 int syscall_id;
112 enum target_waitkind kind;
39f77062 113 int pid;
c906108c 114
c5aa993b
JM
115 do
116 {
117 set_sigint_trap (); /* Causes SIGINT to be passed on to the
118 attached process. */
119 set_sigio_trap ();
c906108c 120
39f77062 121 pid = ptrace_wait (inferior_ptid, &status);
c906108c 122
c5aa993b 123 save_errno = errno;
c906108c 124
c5aa993b 125 clear_sigio_trap ();
c906108c 126
c5aa993b 127 clear_sigint_trap ();
c906108c 128
c5aa993b
JM
129 if (pid == -1)
130 {
131 if (save_errno == EINTR)
132 continue;
c906108c 133
c5aa993b
JM
134 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
135 safe_strerror (save_errno));
c906108c 136
c5aa993b
JM
137 /* Claim it exited with unknown signal. */
138 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
139 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
39f77062 140 return pid_to_ptid (-1);
c5aa993b 141 }
c906108c 142
c5aa993b 143 /* Did it exit?
c906108c 144 */
c5aa993b
JM
145 if (target_has_exited (pid, status, &exit_status))
146 {
147 /* ??rehrauer: For now, ignore this. */
148 continue;
149 }
150
39f77062 151 if (!target_thread_alive (pid_to_ptid (pid)))
c5aa993b
JM
152 {
153 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 154 return pid_to_ptid (pid);
c5aa993b 155 }
47932f85 156 } while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */
c906108c
SS
157
158 store_waitstatus (ourstatus, status);
39f77062 159 return pid_to_ptid (pid);
c906108c
SS
160}
161#endif /* CHILD_WAIT */
162
163#if !defined(CHILD_POST_WAIT)
164void
39f77062 165child_post_wait (ptid_t ptid, int wait_status)
c906108c
SS
166{
167 /* This version of Unix doesn't require a meaningful "post wait"
168 operation.
c5aa993b 169 */
c906108c
SS
170}
171#endif
c5aa993b 172
c906108c
SS
173
174#ifndef CHILD_THREAD_ALIVE
175
176/* Check to see if the given thread is alive.
177
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
180 code. */
181int
39f77062 182child_thread_alive (ptid_t ptid)
c906108c 183{
39f77062
KB
184 pid_t pid = PIDGET (ptid);
185
c906108c
SS
186 return (kill (pid, 0) != -1);
187}
188
189#endif
190
4c9ba7e0
DJ
191/* Attach to process PID, then initialize for debugging it. */
192
c906108c 193static void
4c9ba7e0 194child_attach (char *args, int from_tty)
c906108c
SS
195{
196 if (!args)
197 error_no_arg ("process-id to attach");
198
199#ifndef ATTACH_DETACH
200 error ("Can't attach to a process on this machine.");
201#else
202 {
203 char *exec_file;
204 int pid;
205 char *dummy;
206
207 dummy = args;
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);
212
c5aa993b 213 if (pid == getpid ()) /* Trying to masturbate? */
c906108c
SS
214 error ("I refuse to debug myself!");
215
216 if (from_tty)
217 {
218 exec_file = (char *) get_exec_file (0);
219
4c9ba7e0 220 if (exec_file)
c906108c 221 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
39f77062 222 target_pid_to_str (pid_to_ptid (pid)));
c5aa993b 223 else
39f77062
KB
224 printf_unfiltered ("Attaching to %s\n",
225 target_pid_to_str (pid_to_ptid (pid)));
c906108c
SS
226
227 gdb_flush (gdb_stdout);
228 }
229
4c9ba7e0 230 attach (pid);
c906108c 231
39f77062 232 inferior_ptid = pid_to_ptid (pid);
c906108c
SS
233 push_target (&child_ops);
234 }
c5aa993b 235#endif /* ATTACH_DETACH */
c906108c
SS
236}
237
c906108c
SS
238#if !defined(CHILD_POST_ATTACH)
239void
fba45db2 240child_post_attach (int pid)
c906108c
SS
241{
242 /* This version of Unix doesn't require a meaningful "post attach"
243 operation by a debugger. */
244}
245#endif
246
4c9ba7e0
DJ
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). */
c906108c
SS
254
255static void
4c9ba7e0 256child_detach (char *args, int from_tty)
c906108c
SS
257{
258#ifdef ATTACH_DETACH
259 {
260 int siggnal = 0;
4c9ba7e0 261 int pid = PIDGET (inferior_ptid);
c906108c
SS
262
263 if (from_tty)
264 {
265 char *exec_file = get_exec_file (0);
266 if (exec_file == 0)
267 exec_file = "";
4c9ba7e0
DJ
268 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
269 target_pid_to_str (pid_to_ptid (pid)));
c906108c
SS
270 gdb_flush (gdb_stdout);
271 }
272 if (args)
273 siggnal = atoi (args);
274
4c9ba7e0
DJ
275 detach (siggnal);
276
277 inferior_ptid = null_ptid;
278 unpush_target (&child_ops);
c906108c
SS
279 }
280#else
281 error ("This version of Unix does not support detaching a process.");
282#endif
283}
284
c906108c
SS
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
289 debugged. */
290
291static void
fba45db2 292child_prepare_to_store (void)
c906108c
SS
293{
294#ifdef CHILD_PREPARE_TO_STORE
295 CHILD_PREPARE_TO_STORE ();
296#endif
297}
298
299/* Print status information about what we're accessing. */
300
301static void
fba45db2 302child_files_info (struct target_ops *ignore)
c906108c
SS
303{
304 printf_unfiltered ("\tUsing the running image of %s %s.\n",
39f77062 305 attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
c906108c
SS
306}
307
308/* ARGSUSED */
309static void
fba45db2 310child_open (char *arg, int from_tty)
c906108c
SS
311{
312 error ("Use the \"run\" command to start a Unix child process.");
313}
314
315/* Stub function which causes the inferior that runs it, to be ptrace-able
316 by its parent process. */
317
318static void
fba45db2 319ptrace_me (void)
c906108c
SS
320{
321 /* "Trace me, Dr. Memory!" */
322 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
323}
324
325/* Stub function which causes the GDB that runs it, to start ptrace-ing
326 the child process. */
327
c5aa993b 328static void
fba45db2 329ptrace_him (int pid)
c906108c
SS
330{
331 push_target (&child_ops);
332
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.
c5aa993b 337 */
c906108c
SS
338
339 target_acknowledge_created_inferior (pid);
340
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.
344 */
345 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
346
347 /* On some targets, there must be some explicit actions taken after
348 the inferior has been started up.
c5aa993b 349 */
39f77062 350 target_post_startup_inferior (pid_to_ptid (pid));
c906108c
SS
351}
352
39f77062 353/* Start an inferior Unix child process and sets inferior_ptid to its pid.
c906108c
SS
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(). */
357
358static void
fba45db2 359child_create_inferior (char *exec_file, char *allargs, char **env)
c906108c 360{
c906108c 361#ifdef HPUXHPPA
c906108c
SS
362 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, pre_fork_inferior, NULL);
363#else
c5aa993b 364 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL, NULL);
c906108c
SS
365#endif
366 /* We are at the first instruction we care about. */
367 /* Pedal to the metal... */
2acceee2 368 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
c906108c
SS
369}
370
371#if !defined(CHILD_POST_STARTUP_INFERIOR)
372void
39f77062 373child_post_startup_inferior (ptid_t ptid)
c906108c
SS
374{
375 /* This version of Unix doesn't require a meaningful "post startup inferior"
376 operation by a debugger.
c5aa993b 377 */
c906108c
SS
378}
379#endif
380
381#if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
382void
fba45db2 383child_acknowledge_created_inferior (int pid)
c906108c
SS
384{
385 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
386 operation by a debugger.
c5aa993b 387 */
c906108c
SS
388}
389#endif
390
391
c906108c
SS
392#if !defined(CHILD_INSERT_FORK_CATCHPOINT)
393int
fba45db2 394child_insert_fork_catchpoint (int pid)
c906108c
SS
395{
396 /* This version of Unix doesn't support notification of fork events. */
397 return 0;
398}
399#endif
400
401#if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
402int
fba45db2 403child_remove_fork_catchpoint (int pid)
c906108c
SS
404{
405 /* This version of Unix doesn't support notification of fork events. */
406 return 0;
407}
408#endif
409
410#if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
411int
fba45db2 412child_insert_vfork_catchpoint (int pid)
c906108c
SS
413{
414 /* This version of Unix doesn't support notification of vfork events. */
415 return 0;
416}
417#endif
418
419#if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
420int
fba45db2 421child_remove_vfork_catchpoint (int pid)
c906108c
SS
422{
423 /* This version of Unix doesn't support notification of vfork events. */
424 return 0;
425}
426#endif
427
6604731b
DJ
428#if !defined(CHILD_FOLLOW_FORK)
429int
430child_follow_fork (int follow_child)
c906108c 431{
6604731b
DJ
432 /* This version of Unix doesn't support following fork or vfork events. */
433 return 0;
c906108c
SS
434}
435#endif
436
437#if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
438int
fba45db2 439child_insert_exec_catchpoint (int pid)
c906108c
SS
440{
441 /* This version of Unix doesn't support notification of exec events. */
442 return 0;
443}
444#endif
445
446#if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
447int
fba45db2 448child_remove_exec_catchpoint (int pid)
c906108c
SS
449{
450 /* This version of Unix doesn't support notification of exec events. */
451 return 0;
452}
453#endif
454
c906108c
SS
455#if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
456int
fba45db2 457child_reported_exec_events_per_exec_call (void)
c906108c
SS
458{
459 /* This version of Unix doesn't support notification of exec events.
c5aa993b 460 */
c906108c
SS
461 return 1;
462}
463#endif
464
c906108c
SS
465#if !defined(CHILD_HAS_EXITED)
466int
fba45db2 467child_has_exited (int pid, int wait_status, int *exit_status)
c906108c
SS
468{
469 if (WIFEXITED (wait_status))
470 {
471 *exit_status = WEXITSTATUS (wait_status);
472 return 1;
473 }
474
475 if (WIFSIGNALED (wait_status))
476 {
c5aa993b 477 *exit_status = 0; /* ?? Don't know what else to say here. */
c906108c
SS
478 return 1;
479 }
480
481 /* ?? Do we really need to consult the event state, too? Assume the
c5aa993b 482 wait_state alone suffices.
c906108c
SS
483 */
484 return 0;
485}
486#endif
487
488
489static void
fba45db2 490child_mourn_inferior (void)
c906108c 491{
c906108c 492 unpush_target (&child_ops);
c906108c
SS
493 generic_mourn_inferior ();
494}
495
496static int
fba45db2 497child_can_run (void)
c906108c
SS
498{
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. */
502
503 return !child_suppress_run;
504}
505
506/* Send a SIGINT to the process group. This acts just like the user typed a
507 ^C on the controlling terminal.
508
509 XXX - This may not be correct for all systems. Some may want to use
510 killpg() instead of kill (-pgrp). */
511
512static void
fba45db2 513child_stop (void)
c906108c
SS
514{
515 extern pid_t inferior_process_group;
516
517 kill (-inferior_process_group, SIGINT);
518}
519
520#if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
521struct symtab_and_line *
fba45db2 522child_enable_exception_callback (enum exception_event_kind kind, int enable)
c906108c
SS
523{
524 return (struct symtab_and_line *) NULL;
525}
526#endif
527
528#if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
529struct exception_event_record *
fba45db2 530child_get_current_exception_event (void)
c906108c
SS
531{
532 return (struct exception_event_record *) NULL;
533}
534#endif
535
536
537#if !defined(CHILD_PID_TO_EXEC_FILE)
538char *
fba45db2 539child_pid_to_exec_file (int pid)
c906108c
SS
540{
541 /* This version of Unix doesn't support translation of a process ID
542 to the filename of the executable file.
c5aa993b 543 */
c906108c
SS
544 return NULL;
545}
546#endif
547
548char *
fba45db2 549child_core_file_to_sym_file (char *core)
c906108c
SS
550{
551 /* The target stratum for a running executable need not support
552 this operation.
c5aa993b 553 */
c906108c
SS
554 return NULL;
555}
c5aa993b 556\f
c906108c 557
ed9a39eb
JM
558#if !defined(CHILD_PID_TO_STR)
559char *
39f77062 560child_pid_to_str (ptid_t ptid)
ed9a39eb 561{
39f77062 562 return normal_pid_to_str (ptid);
ed9a39eb
JM
563}
564#endif
c906108c 565
c906108c 566static void
fba45db2 567init_child_ops (void)
c906108c
SS
568{
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;
c906108c 575 child_ops.to_detach = child_detach;
c906108c
SS
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;
a790ad35 589 child_ops.to_terminal_save_ours = terminal_save_ours;
c906108c
SS
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;
c906108c
SS
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;
6604731b 600 child_ops.to_follow_fork = child_follow_fork;
c906108c
SS
601 child_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
602 child_ops.to_remove_exec_catchpoint = child_remove_exec_catchpoint;
c906108c 603 child_ops.to_reported_exec_events_per_exec_call = child_reported_exec_events_per_exec_call;
c906108c
SS
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;
ed9a39eb 608 child_ops.to_pid_to_str = child_pid_to_str;
c906108c
SS
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;
c906108c
SS
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;
620}
621
be4d1333
MS
622/* Take over the 'find_mapped_memory' vector from inftarg.c. */
623extern void
624inftarg_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR,
625 unsigned long,
626 int, int, int,
627 void *),
628 void *))
629{
630 child_ops.to_find_memory_regions = func;
631}
632
633/* Take over the 'make_corefile_notes' vector from inftarg.c. */
634extern void
635inftarg_set_make_corefile_notes (char * (*func) (bfd *, int *))
636{
637 child_ops.to_make_corefile_notes = func;
638}
639
c906108c 640void
fba45db2 641_initialize_inftarg (void)
c906108c
SS
642{
643#ifdef HAVE_OPTIONAL_PROC_FS
644 char procname[32];
645 int fd;
646
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"
651#endif
652 sprintf (procname, PROC_NAME_FMT, getpid ());
bde58177
AC
653 fd = open (procname, O_RDONLY);
654 if (fd >= 0)
c906108c
SS
655 {
656 close (fd);
657 return;
658 }
659#endif
660
661 init_child_ops ();
662 add_target (&child_ops);
663}
This page took 0.32166 seconds and 4 git commands to generate.