1 /* GNU/Linux native-dependent code for debugging multiple forks.
3 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "linux-fork.h"
31 #include "linux-nat.h"
33 #include <sys/ptrace.h>
35 #include <sys/param.h>
36 #include "gdb_dirent.h"
39 struct fork_info
*fork_list
;
40 static int highest_fork_num
;
42 /* Prevent warning from -Wmissing-prototypes. */
43 extern void _initialize_linux_fork (void);
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 off_t
*filepos
; /* Set of open file descriptors' offsets. */
58 /* Fork list methods: */
63 return (fork_list
!= NULL
);
66 /* Add a fork to the internal fork list. */
73 if (fork_list
== NULL
&& pid
!= PIDGET (inferior_ptid
))
75 /* Special case -- if this is the first fork in the list
76 (the list is hitherto empty), and if this new fork is
77 NOT the current inferior_ptid, then add inferior_ptid
78 first, as a special zeroeth fork id. */
79 highest_fork_num
= -1;
80 add_fork (PIDGET (inferior_ptid
)); /* safe recursion */
83 fp
= XZALLOC (struct fork_info
);
84 fp
->ptid
= ptid_build (pid
, pid
, 0);
85 fp
->num
= ++highest_fork_num
;
92 free_fork (struct fork_info
*fp
)
94 /* Notes on step-resume breakpoints: since this is a concern for
95 threads, let's convince ourselves that it's not a concern for
96 forks. There are two ways for a fork_info to be created. First,
97 by the checkpoint command, in which case we're at a gdb prompt
98 and there can't be any step-resume breakpoint. Second, by a fork
99 in the user program, in which case we *may* have stepped into the
100 fork call, but regardless of whether we follow the parent or the
101 child, we will return to the same place and the step-resume
102 breakpoint, if any, will take care of itself as usual. And
103 unlike threads, we do not save a private copy of the step-resume
104 breakpoint -- so we're OK. */
109 regcache_xfree (fp
->savedregs
);
117 delete_fork (ptid_t ptid
)
119 struct fork_info
*fp
, *fpprev
;
123 for (fp
= fork_list
; fp
; fpprev
= fp
, fp
= fp
->next
)
124 if (ptid_equal (fp
->ptid
, ptid
))
131 fpprev
->next
= fp
->next
;
133 fork_list
= fp
->next
;
137 /* Special case: if there is now only one process in the list,
138 and if it is (hopefully!) the current inferior_ptid, then
139 remove it, leaving the list empty -- we're now down to the
140 default case of debugging a single process. */
141 if (fork_list
!= NULL
&& fork_list
->next
== NULL
&&
142 ptid_equal (fork_list
->ptid
, inferior_ptid
))
144 /* Last fork -- delete from list and handle as solo process
145 (should be a safe recursion). */
146 delete_fork (inferior_ptid
);
150 /* Find a fork_info by matching PTID. */
151 static struct fork_info
*
152 find_fork_ptid (ptid_t ptid
)
154 struct fork_info
*fp
;
156 for (fp
= fork_list
; fp
; fp
= fp
->next
)
157 if (ptid_equal (fp
->ptid
, ptid
))
163 /* Find a fork_info by matching ID. */
164 static struct fork_info
*
165 find_fork_id (int num
)
167 struct fork_info
*fp
;
169 for (fp
= fork_list
; fp
; fp
= fp
->next
)
176 /* Find a fork_info by matching pid. */
177 extern struct fork_info
*
178 find_fork_pid (pid_t pid
)
180 struct fork_info
*fp
;
182 for (fp
= fork_list
; fp
; fp
= fp
->next
)
183 if (pid
== ptid_get_pid (fp
->ptid
))
190 fork_id_to_ptid (int num
)
192 struct fork_info
*fork
= find_fork_id (num
);
196 return pid_to_ptid (-1);
200 init_fork_list (void)
202 struct fork_info
*fp
, *fpnext
;
207 for (fp
= fork_list
; fp
; fp
= fpnext
)
216 /* Fork list <-> gdb interface. */
218 /* Utility function for fork_load/fork_save.
219 Calls lseek in the (current) inferior process. */
222 call_lseek (int fd
, off_t offset
, int whence
)
226 snprintf (&exp
[0], sizeof (exp
), "lseek (%d, %ld, %d)",
227 fd
, (long) offset
, whence
);
228 return (off_t
) parse_and_eval_long (&exp
[0]);
231 /* Load infrun state for the fork PTID. */
234 fork_load_infrun_state (struct fork_info
*fp
)
236 extern void nullify_last_target_wait_ptid ();
239 linux_nat_switch_fork (fp
->ptid
);
241 if (fp
->savedregs
&& fp
->clobber_regs
)
242 regcache_cpy (get_current_regcache (), fp
->savedregs
);
244 registers_changed ();
245 reinit_frame_cache ();
247 stop_pc
= regcache_read_pc (get_current_regcache ());
248 nullify_last_target_wait_ptid ();
250 /* Now restore the file positions of open file descriptors. */
253 for (i
= 0; i
<= fp
->maxfd
; i
++)
254 if (fp
->filepos
[i
] != (off_t
) -1)
255 call_lseek (i
, fp
->filepos
[i
], SEEK_SET
);
256 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
257 this is native-only. If it ever has to be cross, we'll have
262 /* Save infrun state for the fork PTID.
263 Exported for use by linux child_follow_fork. */
266 fork_save_infrun_state (struct fork_info
*fp
, int clobber_regs
)
268 char path
[MAXPATHLEN
];
273 regcache_xfree (fp
->savedregs
);
275 fp
->savedregs
= regcache_dup (get_current_regcache ());
276 fp
->clobber_regs
= clobber_regs
;
280 /* Now save the 'state' (file position) of all open file descriptors.
281 Unfortunately fork does not take care of that for us... */
282 snprintf (path
, MAXPATHLEN
, "/proc/%ld/fd", (long) PIDGET (fp
->ptid
));
283 if ((d
= opendir (path
)) != NULL
)
288 while ((de
= readdir (d
)) != NULL
)
290 /* Count open file descriptors (actually find highest
292 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
296 /* Allocate array of file positions. */
297 fp
->filepos
= xrealloc (fp
->filepos
,
298 (fp
->maxfd
+ 1) * sizeof (*fp
->filepos
));
300 /* Initialize to -1 (invalid). */
301 for (tmp
= 0; tmp
<= fp
->maxfd
; tmp
++)
302 fp
->filepos
[tmp
] = -1;
304 /* Now find actual file positions. */
306 while ((de
= readdir (d
)) != NULL
)
307 if (isdigit (de
->d_name
[0]))
309 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
310 fp
->filepos
[tmp
] = call_lseek (tmp
, 0, SEEK_CUR
);
317 /* Kill 'em all, let God sort 'em out... */
320 linux_fork_killall (void)
322 /* Walk list and kill every pid. No need to treat the
323 current inferior_ptid as special (we do not return a
324 status for it) -- however any process may be a child
325 or a parent, so may get a SIGCHLD from a previously
326 killed child. Wait them all out. */
327 struct fork_info
*fp
;
331 for (fp
= fork_list
; fp
; fp
= fp
->next
)
333 pid
= PIDGET (fp
->ptid
);
335 /* Use SIGKILL instead of PTRACE_KILL because the former works even
336 if the thread is running, while the later doesn't. */
338 ret
= waitpid (pid
, &status
, 0);
339 /* We might get a SIGCHLD instead of an exit status. This is
340 aggravated by the first kill above - a child has just
341 died. MVS comment cut-and-pasted from linux-nat. */
342 } while (ret
== pid
&& WIFSTOPPED (status
));
344 init_fork_list (); /* Clear list, prepare to start fresh. */
347 /* The current inferior_ptid has exited, but there are other viable
348 forks to debug. Delete the exiting one and context-switch to the
352 linux_fork_mourn_inferior (void)
354 /* Wait just one more time to collect the inferior's exit status.
355 Do not check whether this succeeds though, since we may be
356 dealing with a process that we attached to. Such a process will
357 only report its exit status to its original parent. */
360 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
362 /* OK, presumably inferior_ptid is the one who has exited.
363 We need to delete that one from the fork_list, and switch
364 to the next available fork. */
365 delete_fork (inferior_ptid
);
367 /* There should still be a fork - if there's only one left,
368 delete_fork won't remove it, because we haven't updated
369 inferior_ptid yet. */
370 gdb_assert (fork_list
);
372 fork_load_infrun_state (fork_list
);
373 printf_filtered (_("[Switching to %s]\n"),
374 target_pid_to_str (inferior_ptid
));
376 /* If there's only one fork, switch back to non-fork mode. */
377 if (fork_list
->next
== NULL
)
378 delete_fork (inferior_ptid
);
381 /* The current inferior_ptid is being detached, but there are other
382 viable forks to debug. Detach and delete it and context-switch to
383 the first available. */
386 linux_fork_detach (char *args
, int from_tty
)
388 /* OK, inferior_ptid is the one we are detaching from. We need to
389 delete it from the fork_list, and switch to the next available
392 if (ptrace (PTRACE_DETACH
, PIDGET (inferior_ptid
), 0, 0))
393 error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid
));
395 delete_fork (inferior_ptid
);
397 /* There should still be a fork - if there's only one left,
398 delete_fork won't remove it, because we haven't updated
399 inferior_ptid yet. */
400 gdb_assert (fork_list
);
402 fork_load_infrun_state (fork_list
);
405 printf_filtered (_("[Switching to %s]\n"),
406 target_pid_to_str (inferior_ptid
));
408 /* If there's only one fork, switch back to non-fork mode. */
409 if (fork_list
->next
== NULL
)
410 delete_fork (inferior_ptid
);
413 /* Fork list <-> user interface. */
416 delete_checkpoint_command (char *args
, int from_tty
)
421 error (_("Requires argument (checkpoint id to delete)"));
423 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
424 if (ptid_equal (ptid
, minus_one_ptid
))
425 error (_("No such checkpoint id, %s"), args
);
427 if (ptid_equal (ptid
, inferior_ptid
))
429 Please switch to another checkpoint before deleting the current one"));
431 if (ptrace (PTRACE_KILL
, PIDGET (ptid
), 0, 0))
432 error (_("Unable to kill pid %s"), target_pid_to_str (ptid
));
435 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid
));
441 detach_checkpoint_command (char *args
, int from_tty
)
446 error (_("Requires argument (checkpoint id to detach)"));
448 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
449 if (ptid_equal (ptid
, minus_one_ptid
))
450 error (_("No such checkpoint id, %s"), args
);
452 if (ptid_equal (ptid
, inferior_ptid
))
454 Please switch to another checkpoint before detaching the current one"));
456 if (ptrace (PTRACE_DETACH
, PIDGET (ptid
), 0, 0))
457 error (_("Unable to detach %s"), target_pid_to_str (ptid
));
460 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid
));
465 /* Print information about currently known checkpoints. */
468 info_checkpoints_command (char *arg
, int from_tty
)
470 struct gdbarch
*gdbarch
= get_current_arch ();
471 struct frame_info
*cur_frame
;
472 struct symtab_and_line sal
;
473 struct symtab
*cur_symtab
;
474 struct fork_info
*fp
;
478 struct fork_info
*printed
= NULL
;
481 requested
= (int) parse_and_eval_long (arg
);
483 for (fp
= fork_list
; fp
; fp
= fp
->next
)
485 if (requested
> 0 && fp
->num
!= requested
)
489 if (ptid_equal (fp
->ptid
, inferior_ptid
))
491 printf_filtered ("* ");
492 pc
= regcache_read_pc (get_current_regcache ());
496 printf_filtered (" ");
497 pc
= regcache_read_pc (fp
->savedregs
);
499 printf_filtered ("%d %s", fp
->num
, target_pid_to_str (fp
->ptid
));
501 printf_filtered (_(" (main process)"));
502 printf_filtered (_(" at "));
503 fputs_filtered (paddress (gdbarch
, pc
), gdb_stdout
);
505 sal
= find_pc_line (pc
, 0);
508 char *tmp
= strrchr (sal
.symtab
->filename
, '/');
511 printf_filtered (_(", file %s"), tmp
+ 1);
513 printf_filtered (_(", file %s"), sal
.symtab
->filename
);
516 printf_filtered (_(", line %d"), sal
.line
);
517 if (!sal
.symtab
&& !sal
.line
)
519 struct minimal_symbol
*msym
;
521 msym
= lookup_minimal_symbol_by_pc (pc
);
523 printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym
));
526 putchar_filtered ('\n');
531 printf_filtered (_("No checkpoint number %d.\n"), requested
);
533 printf_filtered (_("No checkpoints.\n"));
537 /* The PID of the process we're checkpointing. */
538 static int checkpointing_pid
= 0;
541 linux_fork_checkpointing_p (int pid
)
543 return (checkpointing_pid
== pid
);
547 checkpoint_command (char *args
, int from_tty
)
549 struct objfile
*fork_objf
;
550 struct gdbarch
*gdbarch
;
551 struct target_waitstatus last_target_waitstatus
;
552 ptid_t last_target_ptid
;
553 struct value
*fork_fn
= NULL
, *ret
;
554 struct fork_info
*fp
;
556 struct cleanup
*old_chain
;
559 /* Make the inferior fork, record its (and gdb's) state. */
561 if (lookup_minimal_symbol ("fork", NULL
, NULL
) != NULL
)
562 fork_fn
= find_function_in_inferior ("fork", &fork_objf
);
564 if (lookup_minimal_symbol ("_fork", NULL
, NULL
) != NULL
)
565 fork_fn
= find_function_in_inferior ("fork", &fork_objf
);
567 error (_("checkpoint: can't find fork function in inferior."));
569 gdbarch
= get_objfile_arch (fork_objf
);
570 ret
= value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
572 /* Tell linux-nat.c that we're checkpointing this inferior. */
573 old_chain
= make_cleanup_restore_integer (&checkpointing_pid
);
574 checkpointing_pid
= PIDGET (inferior_ptid
);
576 ret
= call_function_by_hand (fork_fn
, 0, &ret
);
577 do_cleanups (old_chain
);
578 if (!ret
) /* Probably can't happen. */
579 error (_("checkpoint: call_function_by_hand returned null."));
581 retpid
= value_as_long (ret
);
582 get_last_target_status (&last_target_ptid
, &last_target_waitstatus
);
587 printf_filtered (_("checkpoint: fork returned pid %ld.\n"),
591 parent_pid
= ptid_get_lwp (last_target_ptid
);
593 parent_pid
= ptid_get_pid (last_target_ptid
);
594 printf_filtered (_(" gdb says parent = %ld.\n"),
599 fp
= find_fork_pid (retpid
);
601 error (_("Failed to find new fork"));
602 fork_save_infrun_state (fp
, 1);
606 linux_fork_context (struct fork_info
*newfp
, int from_tty
)
608 /* Now we attempt to switch processes. */
609 struct fork_info
*oldfp
;
613 gdb_assert (newfp
!= NULL
);
615 oldfp
= find_fork_ptid (inferior_ptid
);
616 gdb_assert (oldfp
!= NULL
);
618 fork_save_infrun_state (oldfp
, 1);
619 remove_breakpoints ();
620 fork_load_infrun_state (newfp
);
621 insert_breakpoints ();
623 printf_filtered (_("Switching to %s\n"),
624 target_pid_to_str (inferior_ptid
));
626 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
629 /* Switch inferior process (checkpoint) context, by checkpoint id. */
631 restart_command (char *args
, int from_tty
)
633 struct fork_info
*fp
;
636 error (_("Requires argument (checkpoint id to restart)"));
638 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
639 error (_("Not found: checkpoint id %s"), args
);
641 linux_fork_context (fp
, from_tty
);
645 _initialize_linux_fork (void)
649 /* Checkpoint command: create a fork of the inferior process
650 and set it aside for later debugging. */
652 add_com ("checkpoint", class_obscure
, checkpoint_command
, _("\
653 Fork a duplicate process (experimental)."));
655 /* Restart command: restore the context of a specified checkpoint
658 add_com ("restart", class_obscure
, restart_command
, _("\
659 restart <n>: restore program context from a checkpoint.\n\
660 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
662 /* Delete checkpoint command: kill the process and remove it from
665 add_cmd ("checkpoint", class_obscure
, delete_checkpoint_command
, _("\
666 Delete a checkpoint (experimental)."),
669 /* Detach checkpoint command: release the process to run independently,
670 and remove it from the fork list. */
672 add_cmd ("checkpoint", class_obscure
, detach_checkpoint_command
, _("\
673 Detach from a checkpoint (experimental)."),
676 /* Info checkpoints command: list all forks/checkpoints
677 currently under gdb's control. */
679 add_info ("checkpoints", info_checkpoints_command
,
680 _("IDs of currently known checkpoints."));