1 /* GNU/Linux native-dependent code for debugging multiple forks.
3 Copyright (C) 2005, 2006, 2007, 2008, 2009 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/>. */
21 #include "arch-utils.h"
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include "linux-fork.h"
30 #include "linux-nat.h"
32 #include <sys/ptrace.h>
34 #include <sys/param.h>
35 #include "gdb_dirent.h"
38 struct fork_info
*fork_list
;
39 static int highest_fork_num
;
41 /* Prevent warning from -Wmissing-prototypes. */
42 extern void _initialize_linux_fork (void);
44 int detach_fork
= 1; /* Default behavior is to detach
45 newly forked processes (legacy). */
47 /* Fork list data structure: */
50 struct fork_info
*next
;
52 int num
; /* Convenient handle (GDB fork id) */
53 struct regcache
*savedregs
; /* Convenient for info fork, saves
54 having to actually switch contexts. */
55 int clobber_regs
; /* True if we should restore saved regs. */
56 ULONGEST pc
; /* PC for info fork. */
57 off_t
*filepos
; /* Set of open file descriptors' offsets. */
61 /* Fork list methods: */
66 return (fork_list
!= NULL
);
69 /* Add a fork to internal fork list.
70 Called from linux child_follow_fork. */
72 extern struct fork_info
*
77 if (fork_list
== NULL
&& pid
!= PIDGET (inferior_ptid
))
79 /* Special case -- if this is the first fork in the list
80 (the list is hitherto empty), and if this new fork is
81 NOT the current inferior_ptid, then add inferior_ptid
82 first, as a special zeroeth fork id. */
83 highest_fork_num
= -1;
84 add_fork (PIDGET (inferior_ptid
)); /* safe recursion */
87 fp
= XZALLOC (struct fork_info
);
88 fp
->ptid
= ptid_build (pid
, pid
, 0);
89 fp
->num
= ++highest_fork_num
;
96 free_fork (struct fork_info
*fp
)
98 /* Notes on step-resume breakpoints: since this is a concern for
99 threads, let's convince ourselves that it's not a concern for
100 forks. There are two ways for a fork_info to be created. First,
101 by the checkpoint command, in which case we're at a gdb prompt
102 and there can't be any step-resume breakpoint. Second, by a fork
103 in the user program, in which case we *may* have stepped into the
104 fork call, but regardless of whether we follow the parent or the
105 child, we will return to the same place and the step-resume
106 breakpoint, if any, will take care of itself as usual. And
107 unlike threads, we do not save a private copy of the step-resume
108 breakpoint -- so we're OK. */
113 regcache_xfree (fp
->savedregs
);
121 delete_fork (ptid_t ptid
)
123 struct fork_info
*fp
, *fpprev
;
127 for (fp
= fork_list
; fp
; fpprev
= fp
, fp
= fp
->next
)
128 if (ptid_equal (fp
->ptid
, ptid
))
135 fpprev
->next
= fp
->next
;
137 fork_list
= fp
->next
;
141 /* Special case: if there is now only one process in the list,
142 and if it is (hopefully!) the current inferior_ptid, then
143 remove it, leaving the list empty -- we're now down to the
144 default case of debugging a single process. */
145 if (fork_list
!= NULL
&& fork_list
->next
== NULL
&&
146 ptid_equal (fork_list
->ptid
, inferior_ptid
))
148 /* Last fork -- delete from list and handle as solo process
149 (should be a safe recursion). */
150 delete_fork (inferior_ptid
);
154 /* Find a fork_info by matching PTID. */
155 static struct fork_info
*
156 find_fork_ptid (ptid_t ptid
)
158 struct fork_info
*fp
;
160 for (fp
= fork_list
; fp
; fp
= fp
->next
)
161 if (ptid_equal (fp
->ptid
, ptid
))
167 /* Find a fork_info by matching ID. */
168 static struct fork_info
*
169 find_fork_id (int num
)
171 struct fork_info
*fp
;
173 for (fp
= fork_list
; fp
; fp
= fp
->next
)
180 /* Find a fork_info by matching pid. */
181 extern struct fork_info
*
182 find_fork_pid (pid_t pid
)
184 struct fork_info
*fp
;
186 for (fp
= fork_list
; fp
; fp
= fp
->next
)
187 if (pid
== ptid_get_pid (fp
->ptid
))
194 fork_id_to_ptid (int num
)
196 struct fork_info
*fork
= find_fork_id (num
);
200 return pid_to_ptid (-1);
204 init_fork_list (void)
206 struct fork_info
*fp
, *fpnext
;
211 for (fp
= fork_list
; fp
; fp
= fpnext
)
214 delete_inferior (ptid_get_pid (fp
->ptid
));
221 /* Fork list <-> gdb interface. */
223 /* Utility function for fork_load/fork_save.
224 Calls lseek in the (current) inferior process. */
227 call_lseek (int fd
, off_t offset
, int whence
)
231 snprintf (&exp
[0], sizeof (exp
), "lseek (%d, %ld, %d)",
232 fd
, (long) offset
, whence
);
233 return (off_t
) parse_and_eval_long (&exp
[0]);
236 /* Load infrun state for the fork PTID. */
239 fork_load_infrun_state (struct fork_info
*fp
)
241 extern void nullify_last_target_wait_ptid ();
244 inferior_ptid
= fp
->ptid
;
246 linux_nat_switch_fork (inferior_ptid
);
248 if (fp
->savedregs
&& fp
->clobber_regs
)
249 regcache_cpy (get_current_regcache (), fp
->savedregs
);
251 registers_changed ();
252 reinit_frame_cache ();
254 stop_pc
= regcache_read_pc (get_current_regcache ());
255 nullify_last_target_wait_ptid ();
257 /* Now restore the file positions of open file descriptors. */
260 for (i
= 0; i
<= fp
->maxfd
; i
++)
261 if (fp
->filepos
[i
] != (off_t
) -1)
262 call_lseek (i
, fp
->filepos
[i
], SEEK_SET
);
263 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
264 this is native-only. If it ever has to be cross, we'll have
269 /* Save infrun state for the fork PTID.
270 Exported for use by linux child_follow_fork. */
273 fork_save_infrun_state (struct fork_info
*fp
, int clobber_regs
)
275 char path
[MAXPATHLEN
];
280 regcache_xfree (fp
->savedregs
);
282 fp
->savedregs
= regcache_dup (get_current_regcache ());
283 fp
->clobber_regs
= clobber_regs
;
284 fp
->pc
= regcache_read_pc (get_current_regcache ());
288 /* Now save the 'state' (file position) of all open file descriptors.
289 Unfortunately fork does not take care of that for us... */
290 snprintf (path
, MAXPATHLEN
, "/proc/%ld/fd", (long) PIDGET (fp
->ptid
));
291 if ((d
= opendir (path
)) != NULL
)
296 while ((de
= readdir (d
)) != NULL
)
298 /* Count open file descriptors (actually find highest
300 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
304 /* Allocate array of file positions. */
305 fp
->filepos
= xrealloc (fp
->filepos
,
306 (fp
->maxfd
+ 1) * sizeof (*fp
->filepos
));
308 /* Initialize to -1 (invalid). */
309 for (tmp
= 0; tmp
<= fp
->maxfd
; tmp
++)
310 fp
->filepos
[tmp
] = -1;
312 /* Now find actual file positions. */
314 while ((de
= readdir (d
)) != NULL
)
315 if (isdigit (de
->d_name
[0]))
317 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
318 fp
->filepos
[tmp
] = call_lseek (tmp
, 0, SEEK_CUR
);
325 /* Kill 'em all, let God sort 'em out... */
328 linux_fork_killall (void)
330 /* Walk list and kill every pid. No need to treat the
331 current inferior_ptid as special (we do not return a
332 status for it) -- however any process may be a child
333 or a parent, so may get a SIGCHLD from a previously
334 killed child. Wait them all out. */
335 struct fork_info
*fp
;
339 for (fp
= fork_list
; fp
; fp
= fp
->next
)
341 pid
= PIDGET (fp
->ptid
);
343 /* Use SIGKILL instead of PTRACE_KILL because the former works even
344 if the thread is running, while the later doesn't. */
346 ret
= waitpid (pid
, &status
, 0);
347 /* We might get a SIGCHLD instead of an exit status. This is
348 aggravated by the first kill above - a child has just
349 died. MVS comment cut-and-pasted from linux-nat. */
350 } while (ret
== pid
&& WIFSTOPPED (status
));
352 init_fork_list (); /* Clear list, prepare to start fresh. */
355 /* The current inferior_ptid has exited, but there are other viable
356 forks to debug. Delete the exiting one and context-switch to the
360 linux_fork_mourn_inferior (void)
362 /* Wait just one more time to collect the inferior's exit status.
363 Do not check whether this succeeds though, since we may be
364 dealing with a process that we attached to. Such a process will
365 only report its exit status to its original parent. */
368 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
370 /* OK, presumably inferior_ptid is the one who has exited.
371 We need to delete that one from the fork_list, and switch
372 to the next available fork. */
373 delete_fork (inferior_ptid
);
374 /* Delete process from GDB's inferior list. */
375 delete_inferior (ptid_get_pid (inferior_ptid
));
377 /* There should still be a fork - if there's only one left,
378 delete_fork won't remove it, because we haven't updated
379 inferior_ptid yet. */
380 gdb_assert (fork_list
);
382 fork_load_infrun_state (fork_list
);
383 printf_filtered (_("[Switching to %s]\n"),
384 target_pid_to_str (inferior_ptid
));
386 /* If there's only one fork, switch back to non-fork mode. */
387 if (fork_list
->next
== NULL
)
388 delete_fork (inferior_ptid
);
391 /* The current inferior_ptid is being detached, but there are other
392 viable forks to debug. Detach and delete it and context-switch to
393 the first available. */
396 linux_fork_detach (char *args
, int from_tty
)
398 /* OK, inferior_ptid is the one we are detaching from. We need to
399 delete it from the fork_list, and switch to the next available
402 if (ptrace (PTRACE_DETACH
, PIDGET (inferior_ptid
), 0, 0))
403 error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid
));
405 delete_fork (inferior_ptid
);
406 /* Delete process from GDB's inferior list. */
407 delete_inferior (ptid_get_pid (inferior_ptid
));
409 /* There should still be a fork - if there's only one left,
410 delete_fork won't remove it, because we haven't updated
411 inferior_ptid yet. */
412 gdb_assert (fork_list
);
414 fork_load_infrun_state (fork_list
);
417 printf_filtered (_("[Switching to %s]\n"),
418 target_pid_to_str (inferior_ptid
));
420 /* If there's only one fork, switch back to non-fork mode. */
421 if (fork_list
->next
== NULL
)
422 delete_fork (inferior_ptid
);
425 /* Fork list <-> user interface. */
428 delete_fork_command (char *args
, int from_tty
)
433 error (_("Requires argument (fork/checkpoint id to delete)"));
435 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
436 if (ptid_equal (ptid
, minus_one_ptid
))
437 error (_("No such fork/checkpoint id, %s"), args
);
439 if (ptid_equal (ptid
, inferior_ptid
))
440 error (_("Please switch to another fork/checkpoint before deleting the current one"));
442 if (ptrace (PTRACE_KILL
, PIDGET (ptid
), 0, 0))
443 error (_("Unable to kill pid %s"), target_pid_to_str (ptid
));
446 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid
));
449 /* Delete process from GDB's inferior list. */
450 delete_inferior (ptid_get_pid (ptid
));
454 detach_fork_command (char *args
, int from_tty
)
459 error (_("Requires argument (fork id to detach)"));
461 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
462 if (ptid_equal (ptid
, minus_one_ptid
))
463 error (_("No such fork id, %s"), args
);
465 if (ptid_equal (ptid
, inferior_ptid
))
466 error (_("Please switch to another fork before detaching the current one"));
468 if (ptrace (PTRACE_DETACH
, PIDGET (ptid
), 0, 0))
469 error (_("Unable to detach %s"), target_pid_to_str (ptid
));
472 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid
));
475 /* Delete process from GDB's process table. */
476 detach_inferior (ptid_get_pid (ptid
));
479 /* Print information about currently known forks. */
482 info_forks_command (char *arg
, int from_tty
)
484 struct gdbarch
*gdbarch
= get_current_arch ();
485 struct frame_info
*cur_frame
;
486 struct symtab_and_line sal
;
487 struct symtab
*cur_symtab
;
488 struct fork_info
*fp
;
492 struct fork_info
*printed
= NULL
;
495 requested
= (int) parse_and_eval_long (arg
);
497 for (fp
= fork_list
; fp
; fp
= fp
->next
)
499 if (requested
> 0 && fp
->num
!= requested
)
503 if (ptid_equal (fp
->ptid
, inferior_ptid
))
505 printf_filtered ("* ");
506 pc
= regcache_read_pc (get_current_regcache ());
510 printf_filtered (" ");
513 printf_filtered ("%d %s", fp
->num
, target_pid_to_str (fp
->ptid
));
515 printf_filtered (_(" (main process)"));
516 printf_filtered (_(" at "));
517 fputs_filtered (paddress (gdbarch
, pc
), gdb_stdout
);
519 sal
= find_pc_line (pc
, 0);
522 char *tmp
= strrchr (sal
.symtab
->filename
, '/');
525 printf_filtered (_(", file %s"), tmp
+ 1);
527 printf_filtered (_(", file %s"), sal
.symtab
->filename
);
530 printf_filtered (_(", line %d"), sal
.line
);
531 if (!sal
.symtab
&& !sal
.line
)
533 struct minimal_symbol
*msym
;
535 msym
= lookup_minimal_symbol_by_pc (pc
);
537 printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym
));
540 putchar_filtered ('\n');
545 printf_filtered (_("No fork number %d.\n"), requested
);
547 printf_filtered (_("No forks.\n"));
551 /* Save/restore mode variable 'detach_fork':
552 We need to temporarily take over this mode variable, while
553 preserving the user-specified state, and make sure that it
554 gets restored in case of error.
556 The int pointer that we use comes from the caller, so we can
557 be called more than once (even though currently we don't need to). */
560 restore_detach_fork (void *arg
)
562 detach_fork
= *(int *) arg
;
565 static struct cleanup
*
566 save_detach_fork (int *saved_val
)
568 *saved_val
= detach_fork
;
569 return make_cleanup (restore_detach_fork
, (void *) saved_val
);
573 checkpoint_command (char *args
, int from_tty
)
575 struct objfile
*fork_objf
;
576 struct gdbarch
*gdbarch
;
577 struct target_waitstatus last_target_waitstatus
;
578 ptid_t last_target_ptid
;
579 struct value
*fork_fn
= NULL
, *ret
;
580 struct fork_info
*fp
;
582 struct cleanup
*old_chain
;
584 /* Make this temp var static, 'cause it's used in the error context. */
585 static int temp_detach_fork
;
587 /* Remove breakpoints, so that they are not inserted
588 in the forked process. */
589 remove_breakpoints ();
591 /* Make the inferior fork, record its (and gdb's) state. */
593 if (lookup_minimal_symbol ("fork", NULL
, NULL
) != NULL
)
594 fork_fn
= find_function_in_inferior ("fork", &fork_objf
);
596 if (lookup_minimal_symbol ("_fork", NULL
, NULL
) != NULL
)
597 fork_fn
= find_function_in_inferior ("fork", &fork_objf
);
599 error (_("checkpoint: can't find fork function in inferior."));
601 gdbarch
= get_objfile_arch (fork_objf
);
602 ret
= value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
603 old_chain
= save_detach_fork (&temp_detach_fork
);
605 ret
= call_function_by_hand (fork_fn
, 0, &ret
);
606 do_cleanups (old_chain
);
607 if (!ret
) /* Probably can't happen. */
608 error (_("checkpoint: call_function_by_hand returned null."));
610 retpid
= value_as_long (ret
);
611 get_last_target_status (&last_target_ptid
, &last_target_waitstatus
);
616 printf_filtered (_("checkpoint: fork returned pid %ld.\n"),
620 parent_pid
= ptid_get_lwp (last_target_ptid
);
622 parent_pid
= ptid_get_pid (last_target_ptid
);
623 printf_filtered (_(" gdb says parent = %ld.\n"),
628 fp
= find_fork_pid (retpid
);
630 error (_("Failed to find new fork"));
631 fork_save_infrun_state (fp
, 1);
632 insert_breakpoints ();
636 linux_fork_context (struct fork_info
*newfp
, int from_tty
)
638 /* Now we attempt to switch processes. */
639 struct fork_info
*oldfp
;
643 gdb_assert (newfp
!= NULL
);
645 oldfp
= find_fork_ptid (inferior_ptid
);
646 gdb_assert (oldfp
!= NULL
);
648 fork_save_infrun_state (oldfp
, 1);
649 remove_breakpoints ();
650 fork_load_infrun_state (newfp
);
651 insert_breakpoints ();
653 printf_filtered (_("Switching to %s\n"),
654 target_pid_to_str (inferior_ptid
));
656 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
659 /* Switch inferior process (fork) context, by process id. */
661 process_command (char *args
, int from_tty
)
663 struct fork_info
*fp
;
666 error (_("Requires argument (process id to switch to)"));
668 if ((fp
= find_fork_pid (parse_and_eval_long (args
))) == NULL
)
669 error (_("Not found: process id %s"), args
);
671 linux_fork_context (fp
, from_tty
);
674 /* Switch inferior process (fork) context, by fork id. */
676 fork_command (char *args
, int from_tty
)
678 struct fork_info
*fp
;
681 error (_("Requires argument (fork id to switch to)"));
683 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
684 error (_("Not found: fork id %s"), args
);
686 linux_fork_context (fp
, from_tty
);
689 /* Switch inferior process (fork) context, by checkpoint id. */
691 restart_command (char *args
, int from_tty
)
693 struct fork_info
*fp
;
696 error (_("Requires argument (checkpoint id to restart)"));
698 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
699 error (_("Not found: checkpoint id %s"), args
);
701 linux_fork_context (fp
, from_tty
);
705 _initialize_linux_fork (void)
709 /* Set/show detach-on-fork: user-settable mode. */
711 add_setshow_boolean_cmd ("detach-on-fork", class_obscure
, &detach_fork
, _("\
712 Set whether gdb will detach the child of a fork."), _("\
713 Show whether gdb will detach the child of a fork."), _("\
714 Tells gdb whether to detach the child of a fork."),
715 NULL
, NULL
, &setlist
, &showlist
);
717 /* Set/show restart-auto-finish: user-settable count. Causes the
718 first "restart" of a fork to do some number of "finish" commands
719 before returning to user.
721 Useful because otherwise the virgin fork process will be stopped
722 somewhere in the un-interesting fork system call. */
724 /* Checkpoint command: create a fork of the inferior process
725 and set it aside for later debugging. */
727 add_com ("checkpoint", class_obscure
, checkpoint_command
, _("\
728 Fork a duplicate process (experimental)."));
730 /* Restart command: restore the context of a specified fork
731 process. May be used for "program forks" as well as for
732 "debugger forks" (checkpoints). */
734 add_com ("restart", class_obscure
, restart_command
, _("\
735 restart <n>: restore program context from a checkpoint.\n\
736 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
738 /* Delete checkpoint command: kill the process and remove it from
741 add_cmd ("checkpoint", class_obscure
, delete_fork_command
, _("\
742 Delete a fork/checkpoint (experimental)."),
745 /* Detach checkpoint command: release the process to run independently,
746 and remove it from the fork list. */
748 add_cmd ("checkpoint", class_obscure
, detach_fork_command
, _("\
749 Detach from a fork/checkpoint (experimental)."),
752 /* Info checkpoints command: list all forks/checkpoints
753 currently under gdb's control. */
755 add_info ("checkpoints", info_forks_command
,
756 _("IDs of currently known forks/checkpoints."));
758 /* Command aliases (let "fork" and "checkpoint" be used
761 add_alias_cmd ("fork", "checkpoint", class_obscure
, 1, &deletelist
);
762 add_alias_cmd ("fork", "checkpoint", class_obscure
, 1, &detachlist
);
763 add_info_alias ("forks", "checkpoints", 0);
765 /* "fork <n>" (by analogy to "thread <n>"). */
766 add_com ("fork", class_obscure
, fork_command
, _("\
767 fork <n>: Switch between forked processes.\n\
768 Argument 'n' is fork ID, as displayed by 'info forks'."));
770 /* "process <proc id>" as opposed to "fork <fork id>". */
771 add_com ("process", class_obscure
, process_command
, _("\
772 process <pid>: Switch between forked processes.\n\
773 Argument 'pid' is process ID, as displayed by 'info forks' or 'shell ps'."));