* breakpoint.c (bpstat_stop_status): Call inferior_has_forked,
[deliverable/binutils-gdb.git] / gdb / inftarg.c
1 /* Target-vector operations for controlling Unix child processes, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
3 2000, 2002
4 Free Software Foundation, Inc.
5 Contributed by Cygnus Support.
6
7 ## Contains temporary hacks..
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
25
26 #include "defs.h"
27 #include "frame.h" /* required by inferior.h */
28 #include "inferior.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "command.h"
32 #include "gdb_stat.h"
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36
37 #include "gdb_wait.h"
38
39 extern struct symtab_and_line *child_enable_exception_callback (enum
40 exception_event_kind,
41 int);
42
43 extern struct exception_event_record
44 *child_get_current_exception_event (void);
45
46 extern void _initialize_inftarg (void);
47
48 static void child_prepare_to_store (void);
49
50 #ifndef CHILD_WAIT
51 static ptid_t child_wait (ptid_t, struct target_waitstatus *);
52 #endif /* CHILD_WAIT */
53
54 #if !defined(CHILD_POST_WAIT)
55 void child_post_wait (ptid_t, int);
56 #endif
57
58 static void child_open (char *, int);
59
60 static void child_files_info (struct target_ops *);
61
62 static void child_detach (char *, int);
63
64 static void child_detach_from_process (int, char *, int, int);
65
66 static void child_attach (char *, int);
67
68 static void child_attach_to_process (char *, int, int);
69
70 #if !defined(CHILD_POST_ATTACH)
71 extern void child_post_attach (int);
72 #endif
73
74 static void child_require_attach (char *, int);
75
76 static void child_require_detach (int, char *, int);
77
78 static void ptrace_me (void);
79
80 static void ptrace_him (int);
81
82 static void child_create_inferior (char *, char *, char **);
83
84 static void child_mourn_inferior (void);
85
86 static int child_can_run (void);
87
88 static void child_stop (void);
89
90 #ifndef CHILD_THREAD_ALIVE
91 int child_thread_alive (ptid_t);
92 #endif
93
94 static void init_child_ops (void);
95
96 extern char **environ;
97
98 struct target_ops child_ops;
99
100 int child_suppress_run = 0; /* Non-zero if inftarg should pretend not to
101 be a runnable target. Used by targets
102 that can sit atop inftarg, such as HPUX
103 thread support. */
104
105 #ifndef CHILD_WAIT
106
107 /* Wait for child to do something. Return pid of child, or -1 in case
108 of error; store status through argument pointer OURSTATUS. */
109
110 static ptid_t
111 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
112 {
113 int save_errno;
114 int status;
115 char *execd_pathname = NULL;
116 int exit_status;
117 int related_pid;
118 int syscall_id;
119 enum target_waitkind kind;
120 int pid;
121
122 do
123 {
124 set_sigint_trap (); /* Causes SIGINT to be passed on to the
125 attached process. */
126 set_sigio_trap ();
127
128 pid = ptrace_wait (inferior_ptid, &status);
129
130 save_errno = errno;
131
132 clear_sigio_trap ();
133
134 clear_sigint_trap ();
135
136 if (pid == -1)
137 {
138 if (save_errno == EINTR)
139 continue;
140
141 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
142 safe_strerror (save_errno));
143
144 /* Claim it exited with unknown signal. */
145 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
146 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
147 return pid_to_ptid (-1);
148 }
149
150 /* Did it exit?
151 */
152 if (target_has_exited (pid, status, &exit_status))
153 {
154 /* ??rehrauer: For now, ignore this. */
155 continue;
156 }
157
158 if (!target_thread_alive (pid_to_ptid (pid)))
159 {
160 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
161 return pid_to_ptid (pid);
162 }
163 } while (pid != PIDGET (inferior_ptid)); /* Some other child died or stopped */
164
165 store_waitstatus (ourstatus, status);
166 return pid_to_ptid (pid);
167 }
168 #endif /* CHILD_WAIT */
169
170 #if !defined(CHILD_POST_WAIT)
171 void
172 child_post_wait (ptid_t ptid, int wait_status)
173 {
174 /* This version of Unix doesn't require a meaningful "post wait"
175 operation.
176 */
177 }
178 #endif
179
180
181 #ifndef CHILD_THREAD_ALIVE
182
183 /* Check to see if the given thread is alive.
184
185 FIXME: Is kill() ever the right way to do this? I doubt it, but
186 for now we're going to try and be compatable with the old thread
187 code. */
188 int
189 child_thread_alive (ptid_t ptid)
190 {
191 pid_t pid = PIDGET (ptid);
192
193 return (kill (pid, 0) != -1);
194 }
195
196 #endif
197
198 static void
199 child_attach_to_process (char *args, int from_tty, int after_fork)
200 {
201 if (!args)
202 error_no_arg ("process-id to attach");
203
204 #ifndef ATTACH_DETACH
205 error ("Can't attach to a process on this machine.");
206 #else
207 {
208 char *exec_file;
209 int pid;
210 char *dummy;
211
212 dummy = args;
213 pid = strtol (args, &dummy, 0);
214 /* Some targets don't set errno on errors, grrr! */
215 if ((pid == 0) && (args == dummy))
216 error ("Illegal process-id: %s\n", args);
217
218 if (pid == getpid ()) /* Trying to masturbate? */
219 error ("I refuse to debug myself!");
220
221 if (from_tty)
222 {
223 exec_file = (char *) get_exec_file (0);
224
225 if (after_fork)
226 printf_unfiltered ("Attaching after fork to %s\n",
227 target_pid_to_str (pid_to_ptid (pid)));
228 else if (exec_file)
229 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
230 target_pid_to_str (pid_to_ptid (pid)));
231 else
232 printf_unfiltered ("Attaching to %s\n",
233 target_pid_to_str (pid_to_ptid (pid)));
234
235 gdb_flush (gdb_stdout);
236 }
237
238 if (!after_fork)
239 attach (pid);
240 else
241 REQUIRE_ATTACH (pid);
242
243 inferior_ptid = pid_to_ptid (pid);
244 push_target (&child_ops);
245 }
246 #endif /* ATTACH_DETACH */
247 }
248
249
250 /* Attach to process PID, then initialize for debugging it. */
251
252 static void
253 child_attach (char *args, int from_tty)
254 {
255 child_attach_to_process (args, from_tty, 0);
256 }
257
258 #if !defined(CHILD_POST_ATTACH)
259 void
260 child_post_attach (int pid)
261 {
262 /* This version of Unix doesn't require a meaningful "post attach"
263 operation by a debugger. */
264 }
265 #endif
266
267 static void
268 child_require_attach (char *args, int from_tty)
269 {
270 child_attach_to_process (args, from_tty, 1);
271 }
272
273 static void
274 child_detach_from_process (int pid, char *args, int from_tty, int after_fork)
275 {
276 #ifdef ATTACH_DETACH
277 {
278 int siggnal = 0;
279
280 if (from_tty)
281 {
282 char *exec_file = get_exec_file (0);
283 if (exec_file == 0)
284 exec_file = "";
285 if (after_fork)
286 printf_unfiltered ("Detaching after fork from %s\n",
287 target_pid_to_str (pid_to_ptid (pid)));
288 else
289 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
290 target_pid_to_str (pid_to_ptid (pid)));
291 gdb_flush (gdb_stdout);
292 }
293 if (args)
294 siggnal = atoi (args);
295
296 if (!after_fork)
297 detach (siggnal);
298 else
299 REQUIRE_DETACH (pid, siggnal);
300 }
301 #else
302 error ("This version of Unix does not support detaching a process.");
303 #endif
304 }
305
306 /* Take a program previously attached to and detaches it.
307 The program resumes execution and will no longer stop
308 on signals, etc. We'd better not have left any breakpoints
309 in the program or it'll die when it hits one. For this
310 to work, it may be necessary for the process to have been
311 previously attached. It *might* work if the program was
312 started via the normal ptrace (PTRACE_TRACEME). */
313
314 static void
315 child_detach (char *args, int from_tty)
316 {
317 child_detach_from_process (PIDGET (inferior_ptid), args, from_tty, 0);
318 inferior_ptid = null_ptid;
319 unpush_target (&child_ops);
320 }
321
322 static void
323 child_require_detach (int pid, char *args, int from_tty)
324 {
325 child_detach_from_process (pid, args, from_tty, 1);
326 }
327
328
329 /* Get ready to modify the registers array. On machines which store
330 individual registers, this doesn't need to do anything. On machines
331 which store all the registers in one fell swoop, this makes sure
332 that registers contains all the registers from the program being
333 debugged. */
334
335 static void
336 child_prepare_to_store (void)
337 {
338 #ifdef CHILD_PREPARE_TO_STORE
339 CHILD_PREPARE_TO_STORE ();
340 #endif
341 }
342
343 /* Print status information about what we're accessing. */
344
345 static void
346 child_files_info (struct target_ops *ignore)
347 {
348 printf_unfiltered ("\tUsing the running image of %s %s.\n",
349 attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
350 }
351
352 /* ARGSUSED */
353 static void
354 child_open (char *arg, int from_tty)
355 {
356 error ("Use the \"run\" command to start a Unix child process.");
357 }
358
359 /* Stub function which causes the inferior that runs it, to be ptrace-able
360 by its parent process. */
361
362 static void
363 ptrace_me (void)
364 {
365 /* "Trace me, Dr. Memory!" */
366 call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
367 }
368
369 /* Stub function which causes the GDB that runs it, to start ptrace-ing
370 the child process. */
371
372 static void
373 ptrace_him (int pid)
374 {
375 push_target (&child_ops);
376
377 /* On some targets, there must be some explicit synchronization
378 between the parent and child processes after the debugger
379 forks, and before the child execs the debuggee program. This
380 call basically gives permission for the child to exec.
381 */
382
383 target_acknowledge_created_inferior (pid);
384
385 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
386 * and will be 1 or 2 depending on whether we're starting
387 * without or with a shell.
388 */
389 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
390
391 /* On some targets, there must be some explicit actions taken after
392 the inferior has been started up.
393 */
394 target_post_startup_inferior (pid_to_ptid (pid));
395 }
396
397 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
398 EXEC_FILE is the file to run.
399 ALLARGS is a string containing the arguments to the program.
400 ENV is the environment vector to pass. Errors reported with error(). */
401
402 static void
403 child_create_inferior (char *exec_file, char *allargs, char **env)
404 {
405 #ifdef HPUXHPPA
406 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, pre_fork_inferior, NULL);
407 #else
408 fork_inferior (exec_file, allargs, env, ptrace_me, ptrace_him, NULL, NULL);
409 #endif
410 /* We are at the first instruction we care about. */
411 /* Pedal to the metal... */
412 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
413 }
414
415 #if !defined(CHILD_POST_STARTUP_INFERIOR)
416 void
417 child_post_startup_inferior (ptid_t ptid)
418 {
419 /* This version of Unix doesn't require a meaningful "post startup inferior"
420 operation by a debugger.
421 */
422 }
423 #endif
424
425 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
426 void
427 child_acknowledge_created_inferior (int pid)
428 {
429 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
430 operation by a debugger.
431 */
432 }
433 #endif
434
435
436 void
437 child_clone_and_follow_inferior (int child_pid, int *followed_child)
438 {
439 clone_and_follow_inferior (child_pid, followed_child);
440
441 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
442 the decision gets made elsewhere how to continue it.
443 */
444 }
445
446
447 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
448 void
449 child_post_follow_inferior_by_clone (void)
450 {
451 /* This version of Unix doesn't require a meaningful "post follow inferior"
452 operation by a clone debugger.
453 */
454 }
455 #endif
456
457 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
458 int
459 child_insert_fork_catchpoint (int pid)
460 {
461 /* This version of Unix doesn't support notification of fork events. */
462 return 0;
463 }
464 #endif
465
466 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
467 int
468 child_remove_fork_catchpoint (int pid)
469 {
470 /* This version of Unix doesn't support notification of fork events. */
471 return 0;
472 }
473 #endif
474
475 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
476 int
477 child_insert_vfork_catchpoint (int pid)
478 {
479 /* This version of Unix doesn't support notification of vfork events. */
480 return 0;
481 }
482 #endif
483
484 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
485 int
486 child_remove_vfork_catchpoint (int pid)
487 {
488 /* This version of Unix doesn't support notification of vfork events. */
489 return 0;
490 }
491 #endif
492
493 #if !defined(CHILD_POST_FOLLOW_VFORK)
494 void
495 child_post_follow_vfork (int parent_pid, int followed_parent, int child_pid,
496 int followed_child)
497 {
498 /* This version of Unix doesn't require a meaningful "post follow vfork"
499 operation by a clone debugger.
500 */
501 }
502 #endif
503
504 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
505 int
506 child_insert_exec_catchpoint (int pid)
507 {
508 /* This version of Unix doesn't support notification of exec events. */
509 return 0;
510 }
511 #endif
512
513 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
514 int
515 child_remove_exec_catchpoint (int pid)
516 {
517 /* This version of Unix doesn't support notification of exec events. */
518 return 0;
519 }
520 #endif
521
522 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
523 int
524 child_reported_exec_events_per_exec_call (void)
525 {
526 /* This version of Unix doesn't support notification of exec events.
527 */
528 return 1;
529 }
530 #endif
531
532 #if !defined(CHILD_HAS_EXITED)
533 int
534 child_has_exited (int pid, int wait_status, int *exit_status)
535 {
536 if (WIFEXITED (wait_status))
537 {
538 *exit_status = WEXITSTATUS (wait_status);
539 return 1;
540 }
541
542 if (WIFSIGNALED (wait_status))
543 {
544 *exit_status = 0; /* ?? Don't know what else to say here. */
545 return 1;
546 }
547
548 /* ?? Do we really need to consult the event state, too? Assume the
549 wait_state alone suffices.
550 */
551 return 0;
552 }
553 #endif
554
555
556 static void
557 child_mourn_inferior (void)
558 {
559 unpush_target (&child_ops);
560 generic_mourn_inferior ();
561 }
562
563 static int
564 child_can_run (void)
565 {
566 /* This variable is controlled by modules that sit atop inftarg that may layer
567 their own process structure atop that provided here. hpux-thread.c does
568 this because of the Hpux user-mode level thread model. */
569
570 return !child_suppress_run;
571 }
572
573 /* Send a SIGINT to the process group. This acts just like the user typed a
574 ^C on the controlling terminal.
575
576 XXX - This may not be correct for all systems. Some may want to use
577 killpg() instead of kill (-pgrp). */
578
579 static void
580 child_stop (void)
581 {
582 extern pid_t inferior_process_group;
583
584 kill (-inferior_process_group, SIGINT);
585 }
586
587 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
588 struct symtab_and_line *
589 child_enable_exception_callback (enum exception_event_kind kind, int enable)
590 {
591 return (struct symtab_and_line *) NULL;
592 }
593 #endif
594
595 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
596 struct exception_event_record *
597 child_get_current_exception_event (void)
598 {
599 return (struct exception_event_record *) NULL;
600 }
601 #endif
602
603
604 #if !defined(CHILD_PID_TO_EXEC_FILE)
605 char *
606 child_pid_to_exec_file (int pid)
607 {
608 /* This version of Unix doesn't support translation of a process ID
609 to the filename of the executable file.
610 */
611 return NULL;
612 }
613 #endif
614
615 char *
616 child_core_file_to_sym_file (char *core)
617 {
618 /* The target stratum for a running executable need not support
619 this operation.
620 */
621 return NULL;
622 }
623 \f
624
625 #if !defined(CHILD_PID_TO_STR)
626 char *
627 child_pid_to_str (ptid_t ptid)
628 {
629 return normal_pid_to_str (ptid);
630 }
631 #endif
632
633 static void
634 init_child_ops (void)
635 {
636 child_ops.to_shortname = "child";
637 child_ops.to_longname = "Unix child process";
638 child_ops.to_doc = "Unix child process (started by the \"run\" command).";
639 child_ops.to_open = child_open;
640 child_ops.to_attach = child_attach;
641 child_ops.to_post_attach = child_post_attach;
642 child_ops.to_require_attach = child_require_attach;
643 child_ops.to_detach = child_detach;
644 child_ops.to_require_detach = child_require_detach;
645 child_ops.to_resume = child_resume;
646 child_ops.to_wait = child_wait;
647 child_ops.to_post_wait = child_post_wait;
648 child_ops.to_fetch_registers = fetch_inferior_registers;
649 child_ops.to_store_registers = store_inferior_registers;
650 child_ops.to_prepare_to_store = child_prepare_to_store;
651 child_ops.to_xfer_memory = child_xfer_memory;
652 child_ops.to_files_info = child_files_info;
653 child_ops.to_insert_breakpoint = memory_insert_breakpoint;
654 child_ops.to_remove_breakpoint = memory_remove_breakpoint;
655 child_ops.to_terminal_init = terminal_init_inferior;
656 child_ops.to_terminal_inferior = terminal_inferior;
657 child_ops.to_terminal_ours_for_output = terminal_ours_for_output;
658 child_ops.to_terminal_save_ours = terminal_save_ours;
659 child_ops.to_terminal_ours = terminal_ours;
660 child_ops.to_terminal_info = child_terminal_info;
661 child_ops.to_kill = kill_inferior;
662 child_ops.to_create_inferior = child_create_inferior;
663 child_ops.to_post_startup_inferior = child_post_startup_inferior;
664 child_ops.to_acknowledge_created_inferior = child_acknowledge_created_inferior;
665 child_ops.to_clone_and_follow_inferior = child_clone_and_follow_inferior;
666 child_ops.to_post_follow_inferior_by_clone = child_post_follow_inferior_by_clone;
667 child_ops.to_insert_fork_catchpoint = child_insert_fork_catchpoint;
668 child_ops.to_remove_fork_catchpoint = child_remove_fork_catchpoint;
669 child_ops.to_insert_vfork_catchpoint = child_insert_vfork_catchpoint;
670 child_ops.to_remove_vfork_catchpoint = child_remove_vfork_catchpoint;
671 child_ops.to_post_follow_vfork = child_post_follow_vfork;
672 child_ops.to_insert_exec_catchpoint = child_insert_exec_catchpoint;
673 child_ops.to_remove_exec_catchpoint = child_remove_exec_catchpoint;
674 child_ops.to_reported_exec_events_per_exec_call = child_reported_exec_events_per_exec_call;
675 child_ops.to_has_exited = child_has_exited;
676 child_ops.to_mourn_inferior = child_mourn_inferior;
677 child_ops.to_can_run = child_can_run;
678 child_ops.to_thread_alive = child_thread_alive;
679 child_ops.to_pid_to_str = child_pid_to_str;
680 child_ops.to_stop = child_stop;
681 child_ops.to_enable_exception_callback = child_enable_exception_callback;
682 child_ops.to_get_current_exception_event = child_get_current_exception_event;
683 child_ops.to_pid_to_exec_file = child_pid_to_exec_file;
684 child_ops.to_stratum = process_stratum;
685 child_ops.to_has_all_memory = 1;
686 child_ops.to_has_memory = 1;
687 child_ops.to_has_stack = 1;
688 child_ops.to_has_registers = 1;
689 child_ops.to_has_execution = 1;
690 child_ops.to_magic = OPS_MAGIC;
691 }
692
693 /* Take over the 'find_mapped_memory' vector from inftarg.c. */
694 extern void
695 inftarg_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR,
696 unsigned long,
697 int, int, int,
698 void *),
699 void *))
700 {
701 child_ops.to_find_memory_regions = func;
702 }
703
704 /* Take over the 'make_corefile_notes' vector from inftarg.c. */
705 extern void
706 inftarg_set_make_corefile_notes (char * (*func) (bfd *, int *))
707 {
708 child_ops.to_make_corefile_notes = func;
709 }
710
711 void
712 _initialize_inftarg (void)
713 {
714 #ifdef HAVE_OPTIONAL_PROC_FS
715 char procname[32];
716 int fd;
717
718 /* If we have an optional /proc filesystem (e.g. under OSF/1),
719 don't add ptrace support if we can access the running GDB via /proc. */
720 #ifndef PROC_NAME_FMT
721 #define PROC_NAME_FMT "/proc/%05d"
722 #endif
723 sprintf (procname, PROC_NAME_FMT, getpid ());
724 if ((fd = open (procname, O_RDONLY)) >= 0)
725 {
726 close (fd);
727 return;
728 }
729 #endif
730
731 init_child_ops ();
732 add_target (&child_ops);
733 }
This page took 0.044447 seconds and 5 git commands to generate.