1 /* GNU/Linux native-dependent code for debugging multiple forks.
3 Copyright (C) 2005, 2006 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
27 #include "gdb_string.h"
28 #include "linux-fork.h"
30 #include <sys/ptrace.h>
32 #include <sys/param.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
= pid_to_ptid (pid
);
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 if (fp
->savedregs
&& fp
->clobber_regs
)
242 regcache_cpy (current_regcache
, fp
->savedregs
);
244 nullify_last_target_wait_ptid ();
246 /* Now restore the file positions of open file descriptors. */
249 for (i
= 0; i
<= fp
->maxfd
; i
++)
250 if (fp
->filepos
[i
] != (off_t
) -1)
251 call_lseek (i
, fp
->filepos
[i
], SEEK_SET
);
252 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
253 this is native-only. If it ever has to be cross, we'll have
258 /* Save infrun state for the fork PTID.
259 Exported for use by linux child_follow_fork. */
262 fork_save_infrun_state (struct fork_info
*fp
, int clobber_regs
)
264 char path
[MAXPATHLEN
];
269 regcache_xfree (fp
->savedregs
);
271 fp
->savedregs
= regcache_dup (current_regcache
);
272 fp
->clobber_regs
= clobber_regs
;
277 /* Now save the 'state' (file position) of all open file descriptors.
278 Unfortunately fork does not take care of that for us... */
279 snprintf (path
, MAXPATHLEN
, "/proc/%ld/fd", (long) PIDGET (fp
->ptid
));
280 if ((d
= opendir (path
)) != NULL
)
285 while ((de
= readdir (d
)) != NULL
)
287 /* Count open file descriptors (actually find highest
289 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
293 /* Allocate array of file positions. */
294 fp
->filepos
= xrealloc (fp
->filepos
,
295 (fp
->maxfd
+ 1) * sizeof (*fp
->filepos
));
297 /* Initialize to -1 (invalid). */
298 for (tmp
= 0; tmp
<= fp
->maxfd
; tmp
++)
299 fp
->filepos
[tmp
] = -1;
301 /* Now find actual file positions. */
303 while ((de
= readdir (d
)) != NULL
)
304 if (isdigit (de
->d_name
[0]))
306 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
307 fp
->filepos
[tmp
] = call_lseek (tmp
, 0, SEEK_CUR
);
314 /* Kill 'em all, let God sort 'em out... */
317 linux_fork_killall (void)
319 /* Walk list and kill every pid. No need to treat the
320 current inferior_ptid as special (we do not return a
321 status for it) -- however any process may be a child
322 or a parent, so may get a SIGCHLD from a previously
323 killed child. Wait them all out. */
324 struct fork_info
*fp
;
328 for (fp
= fork_list
; fp
; fp
= fp
->next
)
330 pid
= PIDGET (fp
->ptid
);
332 ptrace (PT_KILL
, pid
, 0, 0);
333 ret
= waitpid (pid
, &status
, 0);
334 /* We might get a SIGCHLD instead of an exit status. This is
335 aggravated by the first kill above - a child has just
336 died. MVS comment cut-and-pasted from linux-nat. */
337 } while (ret
== pid
&& WIFSTOPPED (status
));
339 init_fork_list (); /* Clear list, prepare to start fresh. */
342 /* The current inferior_ptid has exited, but there are other viable
343 forks to debug. Delete the exiting one and context-switch to the
347 linux_fork_mourn_inferior (void)
349 /* Wait just one more time to collect the inferior's exit status.
350 Do not check whether this succeeds though, since we may be
351 dealing with a process that we attached to. Such a process will
352 only report its exit status to its original parent. */
355 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
357 /* OK, presumably inferior_ptid is the one who has exited.
358 We need to delete that one from the fork_list, and switch
359 to the next available fork. */
360 delete_fork (inferior_ptid
);
361 if (fork_list
) /* Paranoia, shouldn't happen. */
363 inferior_ptid
= fork_list
[0].ptid
;
364 printf_filtered (_("[Switching to %s]\n"),
365 target_pid_to_str (inferior_ptid
));
369 /* Fork list <-> user interface. */
372 delete_fork_command (char *args
, int from_tty
)
377 error (_("Requires argument (fork/checkpoint id to delete)"));
379 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
380 if (ptid_equal (ptid
, minus_one_ptid
))
381 error (_("No such fork/checkpoint id, %s"), args
);
383 if (ptid_equal (ptid
, inferior_ptid
))
384 error (_("Please switch to another fork/checkpoint before deleting the current one"));
386 if (ptrace (PTRACE_KILL
, PIDGET (ptid
), 0, 0))
387 error (_("Unable to kill pid %s"), target_tid_to_str (ptid
));
390 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid
));
396 detach_fork_command (char *args
, int from_tty
)
401 error (_("Requires argument (fork id to detach)"));
403 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
404 if (ptid_equal (ptid
, minus_one_ptid
))
405 error (_("No such fork id, %s"), args
);
407 if (ptid_equal (ptid
, inferior_ptid
))
408 error (_("Please switch to another fork before detaching the current one"));
410 if (ptrace (PTRACE_DETACH
, PIDGET (ptid
), 0, 0))
411 error (_("Unable to detach %s"), target_pid_to_str (ptid
));
414 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid
));
419 /* Print information about currently known forks. */
422 info_forks_command (char *arg
, int from_tty
)
424 struct frame_info
*cur_frame
;
425 struct symtab_and_line sal
;
426 struct symtab
*cur_symtab
;
427 struct fork_info
*fp
;
431 for (fp
= fork_list
; fp
; fp
= fp
->next
)
433 if (ptid_equal (fp
->ptid
, inferior_ptid
))
435 printf_filtered ("* ");
440 printf_filtered (" ");
443 printf_filtered ("%d %s", fp
->num
, target_pid_to_str (fp
->ptid
));
445 printf_filtered (_(" (main process)"));
446 printf_filtered (_(" at "));
447 deprecated_print_address_numeric (pc
, 1, gdb_stdout
);
449 sal
= find_pc_line (pc
, 0);
452 char *tmp
= strrchr (sal
.symtab
->filename
, '/');
455 printf_filtered (_(", file %s"), tmp
+ 1);
457 printf_filtered (_(", file %s"), sal
.symtab
->filename
);
460 printf_filtered (_(", line %d"), sal
.line
);
461 if (!sal
.symtab
&& !sal
.line
)
463 struct minimal_symbol
*msym
;
465 msym
= lookup_minimal_symbol_by_pc (pc
);
467 printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym
));
470 putchar_filtered ('\n');
474 /* Save/restore mode variable 'detach_fork':
475 We need to temporarily take over this mode variable, while
476 preserving the user-specified state, and make sure that it
477 gets restored in case of error.
479 The int pointer that we use comes from the caller, so we can
480 be called more than once (even though currently we don't need to). */
483 restore_detach_fork (void *arg
)
485 detach_fork
= *(int *) arg
;
488 static struct cleanup
*
489 save_detach_fork (int *saved_val
)
491 *saved_val
= detach_fork
;
492 return make_cleanup (restore_detach_fork
, (void *) saved_val
);
496 checkpoint_command (char *args
, int from_tty
)
498 struct target_waitstatus last_target_waitstatus
;
499 ptid_t last_target_ptid
;
500 struct value
*fork_fn
= NULL
, *ret
;
501 struct fork_info
*fp
;
503 struct cleanup
*old_chain
;
505 /* Make this temp var static, 'cause it's used in the error context. */
506 static int temp_detach_fork
;
508 /* Make the inferior fork, record its (and gdb's) state. */
510 if (lookup_minimal_symbol ("fork", NULL
, NULL
) != NULL
)
511 fork_fn
= find_function_in_inferior ("fork");
513 if (lookup_minimal_symbol ("_fork", NULL
, NULL
) != NULL
)
514 fork_fn
= find_function_in_inferior ("fork");
516 error (_("checkpoint: can't find fork function in inferior."));
518 ret
= value_from_longest (builtin_type_int
, 0);
519 old_chain
= save_detach_fork (&temp_detach_fork
);
521 ret
= call_function_by_hand (fork_fn
, 0, &ret
);
522 do_cleanups (old_chain
);
523 if (!ret
) /* Probably can't happen. */
524 error (_("checkpoint: call_function_by_hand returned null."));
526 retpid
= value_as_long (ret
);
527 get_last_target_status (&last_target_ptid
, &last_target_waitstatus
);
532 printf_filtered (_("checkpoint: fork returned pid %ld.\n"),
536 parent_pid
= ptid_get_lwp (last_target_ptid
);
538 parent_pid
= ptid_get_pid (last_target_ptid
);
539 printf_filtered (_(" gdb says parent = %ld.\n"),
544 fp
= find_fork_pid (retpid
);
546 error (_("Failed to find new fork"));
547 fork_save_infrun_state (fp
, 1);
551 linux_fork_context (struct fork_info
*newfp
, int from_tty
)
553 /* Now we attempt to switch processes. */
554 struct fork_info
*oldfp
= find_fork_ptid (inferior_ptid
);
559 error (_("No such fork/process"));
563 oldfp
= add_fork (ptid_get_pid (inferior_ptid
));
566 fork_save_infrun_state (oldfp
, 1);
567 inferior_ptid
= newfp
->ptid
;
568 fork_load_infrun_state (newfp
);
569 registers_changed ();
570 reinit_frame_cache ();
571 stop_pc
= read_pc ();
572 select_frame (get_current_frame ());
574 printf_filtered (_("Switching to %s\n"),
575 target_pid_to_str (inferior_ptid
));
577 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
580 /* Switch inferior process (fork) context, by process id. */
582 process_command (char *args
, int from_tty
)
584 struct fork_info
*fp
;
587 error (_("Requires argument (process id to switch to)"));
589 if ((fp
= find_fork_pid (parse_and_eval_long (args
))) == NULL
)
590 error (_("Not found: process id %s"), args
);
592 linux_fork_context (fp
, from_tty
);
595 /* Switch inferior process (fork) context, by fork id. */
597 fork_command (char *args
, int from_tty
)
599 struct fork_info
*fp
;
602 error (_("Requires argument (fork id to switch to)"));
604 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
605 error (_("Not found: fork id %s"), args
);
607 linux_fork_context (fp
, from_tty
);
610 /* Switch inferior process (fork) context, by checkpoint id. */
612 restart_command (char *args
, int from_tty
)
614 struct fork_info
*fp
;
617 error (_("Requires argument (checkpoint id to restart)"));
619 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
620 error (_("Not found: checkpoint id %s"), args
);
622 linux_fork_context (fp
, from_tty
);
626 _initialize_linux_fork (void)
630 /* Set/show detach-on-fork: user-settable mode. */
632 add_setshow_boolean_cmd ("detach-on-fork", class_obscure
, &detach_fork
, _("\
633 Set whether gdb will detach the child of a fork."), _("\
634 Show whether gdb will detach the child of a fork."), _("\
635 Tells gdb whether to detach the child of a fork."),
636 NULL
, NULL
, &setlist
, &showlist
);
638 /* Set/show restart-auto-finish: user-settable count. Causes the
639 first "restart" of a fork to do some number of "finish" commands
640 before returning to user.
642 Useful because otherwise the virgin fork process will be stopped
643 somewhere in the un-interesting fork system call. */
645 /* Checkpoint command: create a fork of the inferior process
646 and set it aside for later debugging. */
648 add_com ("checkpoint", class_obscure
, checkpoint_command
, _("\
649 Fork a duplicate process (experimental)."));
651 /* Restart command: restore the context of a specified fork
652 process. May be used for "program forks" as well as for
653 "debugger forks" (checkpoints). */
655 add_com ("restart", class_obscure
, restart_command
, _("\
656 restart <n>: restore program context from a checkpoint.\n\
657 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
659 /* Delete-checkpoint command: kill the process and remove it from
662 add_com ("delete-checkpoint", class_obscure
, delete_fork_command
, _("\
663 Delete a fork/checkpoint (experimental)."));
665 /* Detach-checkpoint command: release the process to run independantly,
666 and remove it from the fork list. */
668 add_com ("detach-checkpoint", class_obscure
, detach_fork_command
, _("\
669 Detach from a fork/checkpoint (experimental)."));
671 /* Info checkpoints command: list all forks/checkpoints
672 currently under gdb's control. */
674 add_info ("checkpoints", info_forks_command
,
675 _("IDs of currently known forks/checkpoints."));
677 /* Command aliases (let "fork" and "checkpoint" be used
680 add_com_alias ("delete-fork", "delete-checkpoint", class_obscure
, 1);
681 add_com_alias ("detach-fork", "detach-checkpoint", class_obscure
, 1);
682 add_info_alias ("forks", "checkpoints", 0);
684 /* "fork <n>" (by analogy to "thread <n>"). */
685 add_com ("fork", class_obscure
, fork_command
, _("\
686 fork <n>: Switch between forked processes.\n\
687 Argument 'n' is fork ID, as displayed by 'info forks'."));
689 /* "process <proc id>" as opposed to "fork <fork id>". */
690 add_com ("process", class_obscure
, process_command
, _("\
691 process <pid>: Switch between forked processes.\n\
692 Argument 'pid' is process ID, as displayed by 'info forks' or 'shell ps'."));