1 /* GNU/Linux native-dependent code for debugging multiple forks.
3 Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_assert.h"
26 #include "gdb_string.h"
27 #include "linux-fork.h"
28 #include "linux-nat.h"
30 #include <sys/ptrace.h>
32 #include <sys/param.h>
33 #include "gdb_dirent.h"
36 struct fork_info
*fork_list
;
37 static int highest_fork_num
;
39 /* Prevent warning from -Wmissing-prototypes. */
40 extern void _initialize_linux_fork (void);
42 int detach_fork
= 1; /* Default behavior is to detach
43 newly forked processes (legacy). */
45 /* Fork list data structure: */
48 struct fork_info
*next
;
50 int num
; /* Convenient handle (GDB fork id) */
51 struct regcache
*savedregs
; /* Convenient for info fork, saves
52 having to actually switch contexts. */
53 int clobber_regs
; /* True if we should restore saved regs. */
54 ULONGEST pc
; /* PC for info fork. */
55 off_t
*filepos
; /* Set of open file descriptors' offsets. */
59 /* Fork list methods: */
64 return (fork_list
!= NULL
);
67 /* Add a fork to internal fork list.
68 Called from linux child_follow_fork. */
70 extern struct fork_info
*
75 if (fork_list
== NULL
&& pid
!= PIDGET (inferior_ptid
))
77 /* Special case -- if this is the first fork in the list
78 (the list is hitherto empty), and if this new fork is
79 NOT the current inferior_ptid, then add inferior_ptid
80 first, as a special zeroeth fork id. */
81 highest_fork_num
= -1;
82 add_fork (PIDGET (inferior_ptid
)); /* safe recursion */
85 fp
= XZALLOC (struct fork_info
);
86 fp
->ptid
= ptid_build (pid
, pid
, 0);
87 fp
->num
= ++highest_fork_num
;
94 free_fork (struct fork_info
*fp
)
96 /* Notes on step-resume breakpoints: since this is a concern for
97 threads, let's convince ourselves that it's not a concern for
98 forks. There are two ways for a fork_info to be created. First,
99 by the checkpoint command, in which case we're at a gdb prompt
100 and there can't be any step-resume breakpoint. Second, by a fork
101 in the user program, in which case we *may* have stepped into the
102 fork call, but regardless of whether we follow the parent or the
103 child, we will return to the same place and the step-resume
104 breakpoint, if any, will take care of itself as usual. And
105 unlike threads, we do not save a private copy of the step-resume
106 breakpoint -- so we're OK. */
111 regcache_xfree (fp
->savedregs
);
119 delete_fork (ptid_t ptid
)
121 struct fork_info
*fp
, *fpprev
;
125 for (fp
= fork_list
; fp
; fpprev
= fp
, fp
= fp
->next
)
126 if (ptid_equal (fp
->ptid
, ptid
))
133 fpprev
->next
= fp
->next
;
135 fork_list
= fp
->next
;
139 /* Special case: if there is now only one process in the list,
140 and if it is (hopefully!) the current inferior_ptid, then
141 remove it, leaving the list empty -- we're now down to the
142 default case of debugging a single process. */
143 if (fork_list
!= NULL
&& fork_list
->next
== NULL
&&
144 ptid_equal (fork_list
->ptid
, inferior_ptid
))
146 /* Last fork -- delete from list and handle as solo process
147 (should be a safe recursion). */
148 delete_fork (inferior_ptid
);
152 /* Find a fork_info by matching PTID. */
153 static struct fork_info
*
154 find_fork_ptid (ptid_t ptid
)
156 struct fork_info
*fp
;
158 for (fp
= fork_list
; fp
; fp
= fp
->next
)
159 if (ptid_equal (fp
->ptid
, ptid
))
165 /* Find a fork_info by matching ID. */
166 static struct fork_info
*
167 find_fork_id (int num
)
169 struct fork_info
*fp
;
171 for (fp
= fork_list
; fp
; fp
= fp
->next
)
178 /* Find a fork_info by matching pid. */
179 extern struct fork_info
*
180 find_fork_pid (pid_t pid
)
182 struct fork_info
*fp
;
184 for (fp
= fork_list
; fp
; fp
= fp
->next
)
185 if (pid
== ptid_get_pid (fp
->ptid
))
192 fork_id_to_ptid (int num
)
194 struct fork_info
*fork
= find_fork_id (num
);
198 return pid_to_ptid (-1);
202 init_fork_list (void)
204 struct fork_info
*fp
, *fpnext
;
209 for (fp
= fork_list
; fp
; fp
= fpnext
)
218 /* Fork list <-> gdb interface. */
220 /* Utility function for fork_load/fork_save.
221 Calls lseek in the (current) inferior process. */
224 call_lseek (int fd
, off_t offset
, int whence
)
228 snprintf (&exp
[0], sizeof (exp
), "lseek (%d, %ld, %d)",
229 fd
, (long) offset
, whence
);
230 return (off_t
) parse_and_eval_long (&exp
[0]);
233 /* Load infrun state for the fork PTID. */
236 fork_load_infrun_state (struct fork_info
*fp
)
238 extern void nullify_last_target_wait_ptid ();
241 inferior_ptid
= fp
->ptid
;
243 linux_nat_switch_fork (inferior_ptid
);
245 if (fp
->savedregs
&& fp
->clobber_regs
)
246 regcache_cpy (get_current_regcache (), fp
->savedregs
);
248 registers_changed ();
249 reinit_frame_cache ();
251 stop_pc
= read_pc ();
252 nullify_last_target_wait_ptid ();
254 /* Now restore the file positions of open file descriptors. */
257 for (i
= 0; i
<= fp
->maxfd
; i
++)
258 if (fp
->filepos
[i
] != (off_t
) -1)
259 call_lseek (i
, fp
->filepos
[i
], SEEK_SET
);
260 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
261 this is native-only. If it ever has to be cross, we'll have
266 /* Save infrun state for the fork PTID.
267 Exported for use by linux child_follow_fork. */
270 fork_save_infrun_state (struct fork_info
*fp
, int clobber_regs
)
272 char path
[MAXPATHLEN
];
277 regcache_xfree (fp
->savedregs
);
279 fp
->savedregs
= regcache_dup (get_current_regcache ());
280 fp
->clobber_regs
= clobber_regs
;
285 /* Now save the 'state' (file position) of all open file descriptors.
286 Unfortunately fork does not take care of that for us... */
287 snprintf (path
, MAXPATHLEN
, "/proc/%ld/fd", (long) PIDGET (fp
->ptid
));
288 if ((d
= opendir (path
)) != NULL
)
293 while ((de
= readdir (d
)) != NULL
)
295 /* Count open file descriptors (actually find highest
297 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
301 /* Allocate array of file positions. */
302 fp
->filepos
= xrealloc (fp
->filepos
,
303 (fp
->maxfd
+ 1) * sizeof (*fp
->filepos
));
305 /* Initialize to -1 (invalid). */
306 for (tmp
= 0; tmp
<= fp
->maxfd
; tmp
++)
307 fp
->filepos
[tmp
] = -1;
309 /* Now find actual file positions. */
311 while ((de
= readdir (d
)) != NULL
)
312 if (isdigit (de
->d_name
[0]))
314 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
315 fp
->filepos
[tmp
] = call_lseek (tmp
, 0, SEEK_CUR
);
322 /* Kill 'em all, let God sort 'em out... */
325 linux_fork_killall (void)
327 /* Walk list and kill every pid. No need to treat the
328 current inferior_ptid as special (we do not return a
329 status for it) -- however any process may be a child
330 or a parent, so may get a SIGCHLD from a previously
331 killed child. Wait them all out. */
332 struct fork_info
*fp
;
336 for (fp
= fork_list
; fp
; fp
= fp
->next
)
338 pid
= PIDGET (fp
->ptid
);
340 /* Use SIGKILL instead of PTRACE_KILL because the former works even
341 if the thread is running, while the later doesn't. */
343 ret
= waitpid (pid
, &status
, 0);
344 /* We might get a SIGCHLD instead of an exit status. This is
345 aggravated by the first kill above - a child has just
346 died. MVS comment cut-and-pasted from linux-nat. */
347 } while (ret
== pid
&& WIFSTOPPED (status
));
349 init_fork_list (); /* Clear list, prepare to start fresh. */
352 /* The current inferior_ptid has exited, but there are other viable
353 forks to debug. Delete the exiting one and context-switch to the
357 linux_fork_mourn_inferior (void)
359 /* Wait just one more time to collect the inferior's exit status.
360 Do not check whether this succeeds though, since we may be
361 dealing with a process that we attached to. Such a process will
362 only report its exit status to its original parent. */
365 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
367 /* OK, presumably inferior_ptid is the one who has exited.
368 We need to delete that one from the fork_list, and switch
369 to the next available fork. */
370 delete_fork (inferior_ptid
);
372 /* There should still be a fork - if there's only one left,
373 delete_fork won't remove it, because we haven't updated
374 inferior_ptid yet. */
375 gdb_assert (fork_list
);
377 fork_load_infrun_state (fork_list
);
378 printf_filtered (_("[Switching to %s]\n"),
379 target_pid_to_str (inferior_ptid
));
381 /* If there's only one fork, switch back to non-fork mode. */
382 if (fork_list
->next
== NULL
)
383 delete_fork (inferior_ptid
);
386 /* Fork list <-> user interface. */
389 delete_fork_command (char *args
, int from_tty
)
394 error (_("Requires argument (fork/checkpoint id to delete)"));
396 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
397 if (ptid_equal (ptid
, minus_one_ptid
))
398 error (_("No such fork/checkpoint id, %s"), args
);
400 if (ptid_equal (ptid
, inferior_ptid
))
401 error (_("Please switch to another fork/checkpoint before deleting the current one"));
403 if (ptrace (PTRACE_KILL
, PIDGET (ptid
), 0, 0))
404 error (_("Unable to kill pid %s"), target_tid_to_str (ptid
));
407 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid
));
413 detach_fork_command (char *args
, int from_tty
)
418 error (_("Requires argument (fork id to detach)"));
420 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
421 if (ptid_equal (ptid
, minus_one_ptid
))
422 error (_("No such fork id, %s"), args
);
424 if (ptid_equal (ptid
, inferior_ptid
))
425 error (_("Please switch to another fork before detaching the current one"));
427 if (ptrace (PTRACE_DETACH
, PIDGET (ptid
), 0, 0))
428 error (_("Unable to detach %s"), target_pid_to_str (ptid
));
431 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid
));
436 /* Print information about currently known forks. */
439 info_forks_command (char *arg
, int from_tty
)
441 struct frame_info
*cur_frame
;
442 struct symtab_and_line sal
;
443 struct symtab
*cur_symtab
;
444 struct fork_info
*fp
;
448 struct fork_info
*printed
= NULL
;
451 requested
= (int) parse_and_eval_long (arg
);
453 for (fp
= fork_list
; fp
; fp
= fp
->next
)
455 if (requested
> 0 && fp
->num
!= requested
)
459 if (ptid_equal (fp
->ptid
, inferior_ptid
))
461 printf_filtered ("* ");
466 printf_filtered (" ");
469 printf_filtered ("%d %s", fp
->num
, target_pid_to_str (fp
->ptid
));
471 printf_filtered (_(" (main process)"));
472 printf_filtered (_(" at "));
473 fputs_filtered (paddress (pc
), gdb_stdout
);
475 sal
= find_pc_line (pc
, 0);
478 char *tmp
= strrchr (sal
.symtab
->filename
, '/');
481 printf_filtered (_(", file %s"), tmp
+ 1);
483 printf_filtered (_(", file %s"), sal
.symtab
->filename
);
486 printf_filtered (_(", line %d"), sal
.line
);
487 if (!sal
.symtab
&& !sal
.line
)
489 struct minimal_symbol
*msym
;
491 msym
= lookup_minimal_symbol_by_pc (pc
);
493 printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym
));
496 putchar_filtered ('\n');
501 printf_filtered (_("No fork number %d.\n"), requested
);
503 printf_filtered (_("No forks.\n"));
507 /* Save/restore mode variable 'detach_fork':
508 We need to temporarily take over this mode variable, while
509 preserving the user-specified state, and make sure that it
510 gets restored in case of error.
512 The int pointer that we use comes from the caller, so we can
513 be called more than once (even though currently we don't need to). */
516 restore_detach_fork (void *arg
)
518 detach_fork
= *(int *) arg
;
521 static struct cleanup
*
522 save_detach_fork (int *saved_val
)
524 *saved_val
= detach_fork
;
525 return make_cleanup (restore_detach_fork
, (void *) saved_val
);
529 checkpoint_command (char *args
, int from_tty
)
531 struct target_waitstatus last_target_waitstatus
;
532 ptid_t last_target_ptid
;
533 struct value
*fork_fn
= NULL
, *ret
;
534 struct fork_info
*fp
;
536 struct cleanup
*old_chain
;
538 /* Make this temp var static, 'cause it's used in the error context. */
539 static int temp_detach_fork
;
541 /* Remove breakpoints, so that they are not inserted
542 in the forked process. */
543 remove_breakpoints ();
545 /* Make the inferior fork, record its (and gdb's) state. */
547 if (lookup_minimal_symbol ("fork", NULL
, NULL
) != NULL
)
548 fork_fn
= find_function_in_inferior ("fork");
550 if (lookup_minimal_symbol ("_fork", NULL
, NULL
) != NULL
)
551 fork_fn
= find_function_in_inferior ("fork");
553 error (_("checkpoint: can't find fork function in inferior."));
555 ret
= value_from_longest (builtin_type_int
, 0);
556 old_chain
= save_detach_fork (&temp_detach_fork
);
558 ret
= call_function_by_hand (fork_fn
, 0, &ret
);
559 do_cleanups (old_chain
);
560 if (!ret
) /* Probably can't happen. */
561 error (_("checkpoint: call_function_by_hand returned null."));
563 retpid
= value_as_long (ret
);
564 get_last_target_status (&last_target_ptid
, &last_target_waitstatus
);
569 printf_filtered (_("checkpoint: fork returned pid %ld.\n"),
573 parent_pid
= ptid_get_lwp (last_target_ptid
);
575 parent_pid
= ptid_get_pid (last_target_ptid
);
576 printf_filtered (_(" gdb says parent = %ld.\n"),
581 fp
= find_fork_pid (retpid
);
583 error (_("Failed to find new fork"));
584 fork_save_infrun_state (fp
, 1);
585 insert_breakpoints ();
589 linux_fork_context (struct fork_info
*newfp
, int from_tty
)
591 /* Now we attempt to switch processes. */
592 struct fork_info
*oldfp
= find_fork_ptid (inferior_ptid
);
597 error (_("No such fork/process"));
600 oldfp
= add_fork (ptid_get_pid (inferior_ptid
));
602 fork_save_infrun_state (oldfp
, 1);
603 remove_breakpoints ();
604 fork_load_infrun_state (newfp
);
605 insert_breakpoints ();
607 printf_filtered (_("Switching to %s\n"),
608 target_pid_to_str (inferior_ptid
));
610 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
613 /* Switch inferior process (fork) context, by process id. */
615 process_command (char *args
, int from_tty
)
617 struct fork_info
*fp
;
620 error (_("Requires argument (process id to switch to)"));
622 if ((fp
= find_fork_pid (parse_and_eval_long (args
))) == NULL
)
623 error (_("Not found: process id %s"), args
);
625 linux_fork_context (fp
, from_tty
);
628 /* Switch inferior process (fork) context, by fork id. */
630 fork_command (char *args
, int from_tty
)
632 struct fork_info
*fp
;
635 error (_("Requires argument (fork id to switch to)"));
637 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
638 error (_("Not found: fork id %s"), args
);
640 linux_fork_context (fp
, from_tty
);
643 /* Switch inferior process (fork) context, by checkpoint id. */
645 restart_command (char *args
, int from_tty
)
647 struct fork_info
*fp
;
650 error (_("Requires argument (checkpoint id to restart)"));
652 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
653 error (_("Not found: checkpoint id %s"), args
);
655 linux_fork_context (fp
, from_tty
);
659 _initialize_linux_fork (void)
663 /* Set/show detach-on-fork: user-settable mode. */
665 add_setshow_boolean_cmd ("detach-on-fork", class_obscure
, &detach_fork
, _("\
666 Set whether gdb will detach the child of a fork."), _("\
667 Show whether gdb will detach the child of a fork."), _("\
668 Tells gdb whether to detach the child of a fork."),
669 NULL
, NULL
, &setlist
, &showlist
);
671 /* Set/show restart-auto-finish: user-settable count. Causes the
672 first "restart" of a fork to do some number of "finish" commands
673 before returning to user.
675 Useful because otherwise the virgin fork process will be stopped
676 somewhere in the un-interesting fork system call. */
678 /* Checkpoint command: create a fork of the inferior process
679 and set it aside for later debugging. */
681 add_com ("checkpoint", class_obscure
, checkpoint_command
, _("\
682 Fork a duplicate process (experimental)."));
684 /* Restart command: restore the context of a specified fork
685 process. May be used for "program forks" as well as for
686 "debugger forks" (checkpoints). */
688 add_com ("restart", class_obscure
, restart_command
, _("\
689 restart <n>: restore program context from a checkpoint.\n\
690 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
692 /* Delete checkpoint command: kill the process and remove it from
695 add_cmd ("checkpoint", class_obscure
, delete_fork_command
, _("\
696 Delete a fork/checkpoint (experimental)."),
699 /* Detach checkpoint command: release the process to run independently,
700 and remove it from the fork list. */
702 add_cmd ("checkpoint", class_obscure
, detach_fork_command
, _("\
703 Detach from a fork/checkpoint (experimental)."),
706 /* Info checkpoints command: list all forks/checkpoints
707 currently under gdb's control. */
709 add_info ("checkpoints", info_forks_command
,
710 _("IDs of currently known forks/checkpoints."));
712 /* Command aliases (let "fork" and "checkpoint" be used
715 add_alias_cmd ("fork", "checkpoint", class_obscure
, 1, &deletelist
);
716 add_alias_cmd ("fork", "checkpoint", class_obscure
, 1, &detachlist
);
717 add_info_alias ("forks", "checkpoints", 0);
719 /* "fork <n>" (by analogy to "thread <n>"). */
720 add_com ("fork", class_obscure
, fork_command
, _("\
721 fork <n>: Switch between forked processes.\n\
722 Argument 'n' is fork ID, as displayed by 'info forks'."));
724 /* "process <proc id>" as opposed to "fork <fork id>". */
725 add_com ("process", class_obscure
, process_command
, _("\
726 process <pid>: Switch between forked processes.\n\
727 Argument 'pid' is process ID, as displayed by 'info forks' or 'shell ps'."));