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_assert.h"
28 #include "gdb_string.h"
29 #include "linux-fork.h"
31 #include <sys/ptrace.h>
33 #include <sys/param.h>
37 struct fork_info
*fork_list
;
38 static int highest_fork_num
;
40 /* Prevent warning from -Wmissing-prototypes. */
41 extern void _initialize_linux_fork (void);
43 int detach_fork
= 1; /* Default behavior is to detach
44 newly forked processes (legacy). */
46 /* Fork list data structure: */
49 struct fork_info
*next
;
51 int num
; /* Convenient handle (GDB fork id) */
52 struct regcache
*savedregs
; /* Convenient for info fork, saves
53 having to actually switch contexts. */
54 int clobber_regs
; /* True if we should restore saved regs. */
55 ULONGEST pc
; /* PC for info fork. */
56 off_t
*filepos
; /* Set of open file descriptors' offsets. */
60 /* Fork list methods: */
65 return (fork_list
!= NULL
);
68 /* Add a fork to internal fork list.
69 Called from linux child_follow_fork. */
71 extern struct fork_info
*
76 if (fork_list
== NULL
&& pid
!= PIDGET (inferior_ptid
))
78 /* Special case -- if this is the first fork in the list
79 (the list is hitherto empty), and if this new fork is
80 NOT the current inferior_ptid, then add inferior_ptid
81 first, as a special zeroeth fork id. */
82 highest_fork_num
= -1;
83 add_fork (PIDGET (inferior_ptid
)); /* safe recursion */
86 fp
= XZALLOC (struct fork_info
);
87 fp
->ptid
= pid_to_ptid (pid
);
88 fp
->num
= ++highest_fork_num
;
95 free_fork (struct fork_info
*fp
)
97 /* Notes on step-resume breakpoints: since this is a concern for
98 threads, let's convince ourselves that it's not a concern for
99 forks. There are two ways for a fork_info to be created. First,
100 by the checkpoint command, in which case we're at a gdb prompt
101 and there can't be any step-resume breakpoint. Second, by a fork
102 in the user program, in which case we *may* have stepped into the
103 fork call, but regardless of whether we follow the parent or the
104 child, we will return to the same place and the step-resume
105 breakpoint, if any, will take care of itself as usual. And
106 unlike threads, we do not save a private copy of the step-resume
107 breakpoint -- so we're OK. */
112 regcache_xfree (fp
->savedregs
);
120 delete_fork (ptid_t ptid
)
122 struct fork_info
*fp
, *fpprev
;
126 for (fp
= fork_list
; fp
; fpprev
= fp
, fp
= fp
->next
)
127 if (ptid_equal (fp
->ptid
, ptid
))
134 fpprev
->next
= fp
->next
;
136 fork_list
= fp
->next
;
140 /* Special case: if there is now only one process in the list,
141 and if it is (hopefully!) the current inferior_ptid, then
142 remove it, leaving the list empty -- we're now down to the
143 default case of debugging a single process. */
144 if (fork_list
!= NULL
&& fork_list
->next
== NULL
&&
145 ptid_equal (fork_list
->ptid
, inferior_ptid
))
147 /* Last fork -- delete from list and handle as solo process
148 (should be a safe recursion). */
149 delete_fork (inferior_ptid
);
153 /* Find a fork_info by matching PTID. */
154 static struct fork_info
*
155 find_fork_ptid (ptid_t ptid
)
157 struct fork_info
*fp
;
159 for (fp
= fork_list
; fp
; fp
= fp
->next
)
160 if (ptid_equal (fp
->ptid
, ptid
))
166 /* Find a fork_info by matching ID. */
167 static struct fork_info
*
168 find_fork_id (int num
)
170 struct fork_info
*fp
;
172 for (fp
= fork_list
; fp
; fp
= fp
->next
)
179 /* Find a fork_info by matching pid. */
180 extern struct fork_info
*
181 find_fork_pid (pid_t pid
)
183 struct fork_info
*fp
;
185 for (fp
= fork_list
; fp
; fp
= fp
->next
)
186 if (pid
== ptid_get_pid (fp
->ptid
))
193 fork_id_to_ptid (int num
)
195 struct fork_info
*fork
= find_fork_id (num
);
199 return pid_to_ptid (-1);
203 init_fork_list (void)
205 struct fork_info
*fp
, *fpnext
;
210 for (fp
= fork_list
; fp
; fp
= fpnext
)
219 /* Fork list <-> gdb interface. */
221 /* Utility function for fork_load/fork_save.
222 Calls lseek in the (current) inferior process. */
225 call_lseek (int fd
, off_t offset
, int whence
)
229 snprintf (&exp
[0], sizeof (exp
), "lseek (%d, %ld, %d)",
230 fd
, (long) offset
, whence
);
231 return (off_t
) parse_and_eval_long (&exp
[0]);
234 /* Load infrun state for the fork PTID. */
237 fork_load_infrun_state (struct fork_info
*fp
)
239 extern void nullify_last_target_wait_ptid ();
242 inferior_ptid
= fp
->ptid
;
244 if (fp
->savedregs
&& fp
->clobber_regs
)
245 regcache_cpy (current_regcache
, fp
->savedregs
);
247 registers_changed ();
248 reinit_frame_cache ();
250 /* We must select a new frame before making any inferior calls to
252 select_frame (get_current_frame ());
254 stop_pc
= read_pc ();
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 (current_regcache
);
283 fp
->clobber_regs
= clobber_regs
;
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 ptrace (PT_KILL
, pid
, 0, 0);
344 ret
= waitpid (pid
, &status
, 0);
345 /* We might get a SIGCHLD instead of an exit status. This is
346 aggravated by the first kill above - a child has just
347 died. MVS comment cut-and-pasted from linux-nat. */
348 } while (ret
== pid
&& WIFSTOPPED (status
));
350 init_fork_list (); /* Clear list, prepare to start fresh. */
353 /* The current inferior_ptid has exited, but there are other viable
354 forks to debug. Delete the exiting one and context-switch to the
358 linux_fork_mourn_inferior (void)
360 /* Wait just one more time to collect the inferior's exit status.
361 Do not check whether this succeeds though, since we may be
362 dealing with a process that we attached to. Such a process will
363 only report its exit status to its original parent. */
366 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
368 /* OK, presumably inferior_ptid is the one who has exited.
369 We need to delete that one from the fork_list, and switch
370 to the next available fork. */
371 delete_fork (inferior_ptid
);
373 /* There should still be a fork - if there's only one left,
374 delete_fork won't remove it, because we haven't updated
375 inferior_ptid yet. */
376 gdb_assert (fork_list
);
378 fork_load_infrun_state (fork_list
);
379 printf_filtered (_("[Switching to %s]\n"),
380 target_pid_to_str (inferior_ptid
));
382 /* If there's only one fork, switch back to non-fork mode. */
383 if (fork_list
->next
== NULL
)
384 delete_fork (inferior_ptid
);
387 /* Fork list <-> user interface. */
390 delete_fork_command (char *args
, int from_tty
)
395 error (_("Requires argument (fork/checkpoint id to delete)"));
397 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
398 if (ptid_equal (ptid
, minus_one_ptid
))
399 error (_("No such fork/checkpoint id, %s"), args
);
401 if (ptid_equal (ptid
, inferior_ptid
))
402 error (_("Please switch to another fork/checkpoint before deleting the current one"));
404 if (ptrace (PTRACE_KILL
, PIDGET (ptid
), 0, 0))
405 error (_("Unable to kill pid %s"), target_tid_to_str (ptid
));
408 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid
));
414 detach_fork_command (char *args
, int from_tty
)
419 error (_("Requires argument (fork id to detach)"));
421 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
422 if (ptid_equal (ptid
, minus_one_ptid
))
423 error (_("No such fork id, %s"), args
);
425 if (ptid_equal (ptid
, inferior_ptid
))
426 error (_("Please switch to another fork before detaching the current one"));
428 if (ptrace (PTRACE_DETACH
, PIDGET (ptid
), 0, 0))
429 error (_("Unable to detach %s"), target_pid_to_str (ptid
));
432 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid
));
437 /* Print information about currently known forks. */
440 info_forks_command (char *arg
, int from_tty
)
442 struct frame_info
*cur_frame
;
443 struct symtab_and_line sal
;
444 struct symtab
*cur_symtab
;
445 struct fork_info
*fp
;
449 for (fp
= fork_list
; fp
; fp
= fp
->next
)
451 if (ptid_equal (fp
->ptid
, inferior_ptid
))
453 printf_filtered ("* ");
458 printf_filtered (" ");
461 printf_filtered ("%d %s", fp
->num
, target_pid_to_str (fp
->ptid
));
463 printf_filtered (_(" (main process)"));
464 printf_filtered (_(" at "));
465 deprecated_print_address_numeric (pc
, 1, gdb_stdout
);
467 sal
= find_pc_line (pc
, 0);
470 char *tmp
= strrchr (sal
.symtab
->filename
, '/');
473 printf_filtered (_(", file %s"), tmp
+ 1);
475 printf_filtered (_(", file %s"), sal
.symtab
->filename
);
478 printf_filtered (_(", line %d"), sal
.line
);
479 if (!sal
.symtab
&& !sal
.line
)
481 struct minimal_symbol
*msym
;
483 msym
= lookup_minimal_symbol_by_pc (pc
);
485 printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym
));
488 putchar_filtered ('\n');
492 /* Save/restore mode variable 'detach_fork':
493 We need to temporarily take over this mode variable, while
494 preserving the user-specified state, and make sure that it
495 gets restored in case of error.
497 The int pointer that we use comes from the caller, so we can
498 be called more than once (even though currently we don't need to). */
501 restore_detach_fork (void *arg
)
503 detach_fork
= *(int *) arg
;
506 static struct cleanup
*
507 save_detach_fork (int *saved_val
)
509 *saved_val
= detach_fork
;
510 return make_cleanup (restore_detach_fork
, (void *) saved_val
);
514 checkpoint_command (char *args
, int from_tty
)
516 struct target_waitstatus last_target_waitstatus
;
517 ptid_t last_target_ptid
;
518 struct value
*fork_fn
= NULL
, *ret
;
519 struct fork_info
*fp
;
521 struct cleanup
*old_chain
;
523 /* Make this temp var static, 'cause it's used in the error context. */
524 static int temp_detach_fork
;
526 /* Make the inferior fork, record its (and gdb's) state. */
528 if (lookup_minimal_symbol ("fork", NULL
, NULL
) != NULL
)
529 fork_fn
= find_function_in_inferior ("fork");
531 if (lookup_minimal_symbol ("_fork", NULL
, NULL
) != NULL
)
532 fork_fn
= find_function_in_inferior ("fork");
534 error (_("checkpoint: can't find fork function in inferior."));
536 ret
= value_from_longest (builtin_type_int
, 0);
537 old_chain
= save_detach_fork (&temp_detach_fork
);
539 ret
= call_function_by_hand (fork_fn
, 0, &ret
);
540 do_cleanups (old_chain
);
541 if (!ret
) /* Probably can't happen. */
542 error (_("checkpoint: call_function_by_hand returned null."));
544 retpid
= value_as_long (ret
);
545 get_last_target_status (&last_target_ptid
, &last_target_waitstatus
);
550 printf_filtered (_("checkpoint: fork returned pid %ld.\n"),
554 parent_pid
= ptid_get_lwp (last_target_ptid
);
556 parent_pid
= ptid_get_pid (last_target_ptid
);
557 printf_filtered (_(" gdb says parent = %ld.\n"),
562 fp
= find_fork_pid (retpid
);
564 error (_("Failed to find new fork"));
565 fork_save_infrun_state (fp
, 1);
569 linux_fork_context (struct fork_info
*newfp
, int from_tty
)
571 /* Now we attempt to switch processes. */
572 struct fork_info
*oldfp
= find_fork_ptid (inferior_ptid
);
577 error (_("No such fork/process"));
580 oldfp
= add_fork (ptid_get_pid (inferior_ptid
));
582 fork_save_infrun_state (oldfp
, 1);
583 fork_load_infrun_state (newfp
);
585 printf_filtered (_("Switching to %s\n"),
586 target_pid_to_str (inferior_ptid
));
588 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
591 /* Switch inferior process (fork) context, by process id. */
593 process_command (char *args
, int from_tty
)
595 struct fork_info
*fp
;
598 error (_("Requires argument (process id to switch to)"));
600 if ((fp
= find_fork_pid (parse_and_eval_long (args
))) == NULL
)
601 error (_("Not found: process id %s"), args
);
603 linux_fork_context (fp
, from_tty
);
606 /* Switch inferior process (fork) context, by fork id. */
608 fork_command (char *args
, int from_tty
)
610 struct fork_info
*fp
;
613 error (_("Requires argument (fork id to switch to)"));
615 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
616 error (_("Not found: fork id %s"), args
);
618 linux_fork_context (fp
, from_tty
);
621 /* Switch inferior process (fork) context, by checkpoint id. */
623 restart_command (char *args
, int from_tty
)
625 struct fork_info
*fp
;
628 error (_("Requires argument (checkpoint id to restart)"));
630 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
631 error (_("Not found: checkpoint id %s"), args
);
633 linux_fork_context (fp
, from_tty
);
637 _initialize_linux_fork (void)
641 /* Set/show detach-on-fork: user-settable mode. */
643 add_setshow_boolean_cmd ("detach-on-fork", class_obscure
, &detach_fork
, _("\
644 Set whether gdb will detach the child of a fork."), _("\
645 Show whether gdb will detach the child of a fork."), _("\
646 Tells gdb whether to detach the child of a fork."),
647 NULL
, NULL
, &setlist
, &showlist
);
649 /* Set/show restart-auto-finish: user-settable count. Causes the
650 first "restart" of a fork to do some number of "finish" commands
651 before returning to user.
653 Useful because otherwise the virgin fork process will be stopped
654 somewhere in the un-interesting fork system call. */
656 /* Checkpoint command: create a fork of the inferior process
657 and set it aside for later debugging. */
659 add_com ("checkpoint", class_obscure
, checkpoint_command
, _("\
660 Fork a duplicate process (experimental)."));
662 /* Restart command: restore the context of a specified fork
663 process. May be used for "program forks" as well as for
664 "debugger forks" (checkpoints). */
666 add_com ("restart", class_obscure
, restart_command
, _("\
667 restart <n>: restore program context from a checkpoint.\n\
668 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
670 /* Delete-checkpoint command: kill the process and remove it from
673 add_com ("delete-checkpoint", class_obscure
, delete_fork_command
, _("\
674 Delete a fork/checkpoint (experimental)."));
676 /* Detach-checkpoint command: release the process to run independantly,
677 and remove it from the fork list. */
679 add_com ("detach-checkpoint", class_obscure
, detach_fork_command
, _("\
680 Detach from a fork/checkpoint (experimental)."));
682 /* Info checkpoints command: list all forks/checkpoints
683 currently under gdb's control. */
685 add_info ("checkpoints", info_forks_command
,
686 _("IDs of currently known forks/checkpoints."));
688 /* Command aliases (let "fork" and "checkpoint" be used
691 add_com_alias ("delete-fork", "delete-checkpoint", class_obscure
, 1);
692 add_com_alias ("detach-fork", "detach-checkpoint", class_obscure
, 1);
693 add_info_alias ("forks", "checkpoints", 0);
695 /* "fork <n>" (by analogy to "thread <n>"). */
696 add_com ("fork", class_obscure
, fork_command
, _("\
697 fork <n>: Switch between forked processes.\n\
698 Argument 'n' is fork ID, as displayed by 'info forks'."));
700 /* "process <proc id>" as opposed to "fork <fork id>". */
701 add_com ("process", class_obscure
, process_command
, _("\
702 process <pid>: Switch between forked processes.\n\
703 Argument 'pid' is process ID, as displayed by 'info forks' or 'shell ps'."));