1 /* GNU/Linux native-dependent code for debugging multiple forks.
3 Copyright (C) 2005-2017 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"
28 #include "linux-fork.h"
29 #include "linux-nat.h"
30 #include "gdbthread.h"
33 #include "nat/gdb_ptrace.h"
38 struct fork_info
*fork_list
;
39 static int highest_fork_num
;
41 /* Fork list data structure: */
44 struct fork_info
*next
;
47 int num
; /* Convenient handle (GDB fork id). */
48 struct regcache
*savedregs
; /* Convenient for info fork, saves
49 having to actually switch contexts. */
50 int clobber_regs
; /* True if we should restore saved regs. */
51 off_t
*filepos
; /* Set of open file descriptors' offsets. */
55 /* Fork list methods: */
60 return (fork_list
!= NULL
);
63 /* Return the last fork in the list. */
65 static struct fork_info
*
68 struct fork_info
*last
;
70 if (fork_list
== NULL
)
73 for (last
= fork_list
; last
->next
!= NULL
; last
= last
->next
)
78 /* Add a fork to the internal fork list. */
85 if (fork_list
== NULL
&& pid
!= ptid_get_pid (inferior_ptid
))
87 /* Special case -- if this is the first fork in the list
88 (the list is hitherto empty), and if this new fork is
89 NOT the current inferior_ptid, then add inferior_ptid
90 first, as a special zeroeth fork id. */
91 highest_fork_num
= -1;
92 add_fork (ptid_get_pid (inferior_ptid
)); /* safe recursion */
95 fp
= XCNEW (struct fork_info
);
96 fp
->ptid
= ptid_build (pid
, pid
, 0);
97 fp
->num
= ++highest_fork_num
;
99 if (fork_list
== NULL
)
103 struct fork_info
*last
= find_last_fork ();
112 free_fork (struct fork_info
*fp
)
114 /* Notes on step-resume breakpoints: since this is a concern for
115 threads, let's convince ourselves that it's not a concern for
116 forks. There are two ways for a fork_info to be created. First,
117 by the checkpoint command, in which case we're at a gdb prompt
118 and there can't be any step-resume breakpoint. Second, by a fork
119 in the user program, in which case we *may* have stepped into the
120 fork call, but regardless of whether we follow the parent or the
121 child, we will return to the same place and the step-resume
122 breakpoint, if any, will take care of itself as usual. And
123 unlike threads, we do not save a private copy of the step-resume
124 breakpoint -- so we're OK. */
129 delete fp
->savedregs
;
137 delete_fork (ptid_t ptid
)
139 struct fork_info
*fp
, *fpprev
;
143 linux_nat_forget_process (ptid_get_pid (ptid
));
145 for (fp
= fork_list
; fp
; fpprev
= fp
, fp
= fp
->next
)
146 if (ptid_equal (fp
->ptid
, ptid
))
153 fpprev
->next
= fp
->next
;
155 fork_list
= fp
->next
;
159 /* Special case: if there is now only one process in the list,
160 and if it is (hopefully!) the current inferior_ptid, then
161 remove it, leaving the list empty -- we're now down to the
162 default case of debugging a single process. */
163 if (fork_list
!= NULL
&& fork_list
->next
== NULL
&&
164 ptid_equal (fork_list
->ptid
, inferior_ptid
))
166 /* Last fork -- delete from list and handle as solo process
167 (should be a safe recursion). */
168 delete_fork (inferior_ptid
);
172 /* Find a fork_info by matching PTID. */
173 static struct fork_info
*
174 find_fork_ptid (ptid_t ptid
)
176 struct fork_info
*fp
;
178 for (fp
= fork_list
; fp
; fp
= fp
->next
)
179 if (ptid_equal (fp
->ptid
, ptid
))
185 /* Find a fork_info by matching ID. */
186 static struct fork_info
*
187 find_fork_id (int num
)
189 struct fork_info
*fp
;
191 for (fp
= fork_list
; fp
; fp
= fp
->next
)
198 /* Find a fork_info by matching pid. */
199 extern struct fork_info
*
200 find_fork_pid (pid_t pid
)
202 struct fork_info
*fp
;
204 for (fp
= fork_list
; fp
; fp
= fp
->next
)
205 if (pid
== ptid_get_pid (fp
->ptid
))
212 fork_id_to_ptid (int num
)
214 struct fork_info
*fork
= find_fork_id (num
);
218 return pid_to_ptid (-1);
222 init_fork_list (void)
224 struct fork_info
*fp
, *fpnext
;
229 for (fp
= fork_list
; fp
; fp
= fpnext
)
238 /* Fork list <-> gdb interface. */
240 /* Utility function for fork_load/fork_save.
241 Calls lseek in the (current) inferior process. */
244 call_lseek (int fd
, off_t offset
, int whence
)
248 snprintf (&exp
[0], sizeof (exp
), "(long) lseek (%d, %ld, %d)",
249 fd
, (long) offset
, whence
);
250 return (off_t
) parse_and_eval_long (&exp
[0]);
253 /* Load infrun state for the fork PTID. */
256 fork_load_infrun_state (struct fork_info
*fp
)
258 extern void nullify_last_target_wait_ptid ();
261 linux_nat_switch_fork (fp
->ptid
);
263 if (fp
->savedregs
&& fp
->clobber_regs
)
264 regcache_cpy (get_current_regcache (), fp
->savedregs
);
266 registers_changed ();
267 reinit_frame_cache ();
269 stop_pc
= regcache_read_pc (get_current_regcache ());
270 nullify_last_target_wait_ptid ();
272 /* Now restore the file positions of open file descriptors. */
275 for (i
= 0; i
<= fp
->maxfd
; i
++)
276 if (fp
->filepos
[i
] != (off_t
) -1)
277 call_lseek (i
, fp
->filepos
[i
], SEEK_SET
);
278 /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because
279 this is native-only. If it ever has to be cross, we'll have
284 /* Save infrun state for the fork PTID.
285 Exported for use by linux child_follow_fork. */
288 fork_save_infrun_state (struct fork_info
*fp
, int clobber_regs
)
295 delete fp
->savedregs
;
297 fp
->savedregs
= regcache_dup (get_current_regcache ());
298 fp
->clobber_regs
= clobber_regs
;
302 /* Now save the 'state' (file position) of all open file descriptors.
303 Unfortunately fork does not take care of that for us... */
304 snprintf (path
, PATH_MAX
, "/proc/%ld/fd",
305 (long) ptid_get_pid (fp
->ptid
));
306 if ((d
= opendir (path
)) != NULL
)
311 while ((de
= readdir (d
)) != NULL
)
313 /* Count open file descriptors (actually find highest
315 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
319 /* Allocate array of file positions. */
320 fp
->filepos
= XRESIZEVEC (off_t
, fp
->filepos
, fp
->maxfd
+ 1);
322 /* Initialize to -1 (invalid). */
323 for (tmp
= 0; tmp
<= fp
->maxfd
; tmp
++)
324 fp
->filepos
[tmp
] = -1;
326 /* Now find actual file positions. */
328 while ((de
= readdir (d
)) != NULL
)
329 if (isdigit (de
->d_name
[0]))
331 tmp
= strtol (&de
->d_name
[0], NULL
, 10);
332 fp
->filepos
[tmp
] = call_lseek (tmp
, 0, SEEK_CUR
);
339 /* Kill 'em all, let God sort 'em out... */
342 linux_fork_killall (void)
344 /* Walk list and kill every pid. No need to treat the
345 current inferior_ptid as special (we do not return a
346 status for it) -- however any process may be a child
347 or a parent, so may get a SIGCHLD from a previously
348 killed child. Wait them all out. */
349 struct fork_info
*fp
;
353 for (fp
= fork_list
; fp
; fp
= fp
->next
)
355 pid
= ptid_get_pid (fp
->ptid
);
357 /* Use SIGKILL instead of PTRACE_KILL because the former works even
358 if the thread is running, while the later doesn't. */
360 ret
= waitpid (pid
, &status
, 0);
361 /* We might get a SIGCHLD instead of an exit status. This is
362 aggravated by the first kill above - a child has just
363 died. MVS comment cut-and-pasted from linux-nat. */
364 } while (ret
== pid
&& WIFSTOPPED (status
));
366 init_fork_list (); /* Clear list, prepare to start fresh. */
369 /* The current inferior_ptid has exited, but there are other viable
370 forks to debug. Delete the exiting one and context-switch to the
374 linux_fork_mourn_inferior (void)
376 struct fork_info
*last
;
379 /* Wait just one more time to collect the inferior's exit status.
380 Do not check whether this succeeds though, since we may be
381 dealing with a process that we attached to. Such a process will
382 only report its exit status to its original parent. */
383 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
385 /* OK, presumably inferior_ptid is the one who has exited.
386 We need to delete that one from the fork_list, and switch
387 to the next available fork. */
388 delete_fork (inferior_ptid
);
390 /* There should still be a fork - if there's only one left,
391 delete_fork won't remove it, because we haven't updated
392 inferior_ptid yet. */
393 gdb_assert (fork_list
);
395 last
= find_last_fork ();
396 fork_load_infrun_state (last
);
397 printf_filtered (_("[Switching to %s]\n"),
398 target_pid_to_str (inferior_ptid
));
400 /* If there's only one fork, switch back to non-fork mode. */
401 if (fork_list
->next
== NULL
)
402 delete_fork (inferior_ptid
);
405 /* The current inferior_ptid is being detached, but there are other
406 viable forks to debug. Detach and delete it and context-switch to
407 the first available. */
410 linux_fork_detach (const char *args
, int from_tty
)
412 /* OK, inferior_ptid is the one we are detaching from. We need to
413 delete it from the fork_list, and switch to the next available
416 if (ptrace (PTRACE_DETACH
, ptid_get_pid (inferior_ptid
), 0, 0))
417 error (_("Unable to detach %s"), target_pid_to_str (inferior_ptid
));
419 delete_fork (inferior_ptid
);
421 /* There should still be a fork - if there's only one left,
422 delete_fork won't remove it, because we haven't updated
423 inferior_ptid yet. */
424 gdb_assert (fork_list
);
426 fork_load_infrun_state (fork_list
);
429 printf_filtered (_("[Switching to %s]\n"),
430 target_pid_to_str (inferior_ptid
));
432 /* If there's only one fork, switch back to non-fork mode. */
433 if (fork_list
->next
== NULL
)
434 delete_fork (inferior_ptid
);
438 inferior_call_waitpid_cleanup (void *fp
)
440 struct fork_info
*oldfp
= (struct fork_info
*) fp
;
444 /* Switch back to inferior_ptid. */
445 remove_breakpoints ();
446 fork_load_infrun_state (oldfp
);
447 insert_breakpoints ();
452 inferior_call_waitpid (ptid_t pptid
, int pid
)
454 struct objfile
*waitpid_objf
;
455 struct value
*waitpid_fn
= NULL
;
456 struct value
*argv
[4], *retv
;
457 struct gdbarch
*gdbarch
= get_current_arch ();
458 struct fork_info
*oldfp
= NULL
, *newfp
= NULL
;
459 struct cleanup
*old_cleanup
;
462 if (!ptid_equal (pptid
, inferior_ptid
))
464 /* Switch to pptid. */
465 oldfp
= find_fork_ptid (inferior_ptid
);
466 gdb_assert (oldfp
!= NULL
);
467 newfp
= find_fork_ptid (pptid
);
468 gdb_assert (newfp
!= NULL
);
469 fork_save_infrun_state (oldfp
, 1);
470 remove_breakpoints ();
471 fork_load_infrun_state (newfp
);
472 insert_breakpoints ();
475 old_cleanup
= make_cleanup (inferior_call_waitpid_cleanup
, oldfp
);
477 /* Get the waitpid_fn. */
478 if (lookup_minimal_symbol ("waitpid", NULL
, NULL
).minsym
!= NULL
)
479 waitpid_fn
= find_function_in_inferior ("waitpid", &waitpid_objf
);
481 && lookup_minimal_symbol ("_waitpid", NULL
, NULL
).minsym
!= NULL
)
482 waitpid_fn
= find_function_in_inferior ("_waitpid", &waitpid_objf
);
487 argv
[0] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, pid
);
488 argv
[1] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
, 0);
489 argv
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
492 retv
= call_function_by_hand (waitpid_fn
, NULL
, 3, argv
);
493 if (value_as_long (retv
) < 0)
499 do_cleanups (old_cleanup
);
503 /* Fork list <-> user interface. */
506 delete_checkpoint_command (char *args
, int from_tty
)
509 struct fork_info
*fi
;
512 error (_("Requires argument (checkpoint id to delete)"));
514 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
515 if (ptid_equal (ptid
, minus_one_ptid
))
516 error (_("No such checkpoint id, %s"), args
);
518 if (ptid_equal (ptid
, inferior_ptid
))
520 Please switch to another checkpoint before deleting the current one"));
522 if (ptrace (PTRACE_KILL
, ptid_get_pid (ptid
), 0, 0))
523 error (_("Unable to kill pid %s"), target_pid_to_str (ptid
));
525 fi
= find_fork_ptid (ptid
);
527 pptid
= fi
->parent_ptid
;
530 printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid
));
534 /* If fi->parent_ptid is not a part of lwp but it's a part of checkpoint
535 list, waitpid the ptid.
536 If fi->parent_ptid is a part of lwp and it is stoped, waitpid the
538 if ((!find_thread_ptid (pptid
) && find_fork_ptid (pptid
))
539 || (find_thread_ptid (pptid
) && is_stopped (pptid
)))
541 if (inferior_call_waitpid (pptid
, ptid_get_pid (ptid
)))
542 warning (_("Unable to wait pid %s"), target_pid_to_str (ptid
));
547 detach_checkpoint_command (char *args
, int from_tty
)
552 error (_("Requires argument (checkpoint id to detach)"));
554 ptid
= fork_id_to_ptid (parse_and_eval_long (args
));
555 if (ptid_equal (ptid
, minus_one_ptid
))
556 error (_("No such checkpoint id, %s"), args
);
558 if (ptid_equal (ptid
, inferior_ptid
))
560 Please switch to another checkpoint before detaching the current one"));
562 if (ptrace (PTRACE_DETACH
, ptid_get_pid (ptid
), 0, 0))
563 error (_("Unable to detach %s"), target_pid_to_str (ptid
));
566 printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid
));
571 /* Print information about currently known checkpoints. */
574 info_checkpoints_command (char *arg
, int from_tty
)
576 struct gdbarch
*gdbarch
= get_current_arch ();
577 struct symtab_and_line sal
;
578 struct fork_info
*fp
;
581 struct fork_info
*printed
= NULL
;
584 requested
= (int) parse_and_eval_long (arg
);
586 for (fp
= fork_list
; fp
; fp
= fp
->next
)
588 if (requested
> 0 && fp
->num
!= requested
)
592 if (ptid_equal (fp
->ptid
, inferior_ptid
))
594 printf_filtered ("* ");
595 pc
= regcache_read_pc (get_current_regcache ());
599 printf_filtered (" ");
600 pc
= regcache_read_pc (fp
->savedregs
);
602 printf_filtered ("%d %s", fp
->num
, target_pid_to_str (fp
->ptid
));
604 printf_filtered (_(" (main process)"));
605 printf_filtered (_(" at "));
606 fputs_filtered (paddress (gdbarch
, pc
), gdb_stdout
);
608 sal
= find_pc_line (pc
, 0);
610 printf_filtered (_(", file %s"),
611 symtab_to_filename_for_display (sal
.symtab
));
613 printf_filtered (_(", line %d"), sal
.line
);
614 if (!sal
.symtab
&& !sal
.line
)
616 struct bound_minimal_symbol msym
;
618 msym
= lookup_minimal_symbol_by_pc (pc
);
620 printf_filtered (", <%s>", MSYMBOL_LINKAGE_NAME (msym
.minsym
));
623 putchar_filtered ('\n');
628 printf_filtered (_("No checkpoint number %d.\n"), requested
);
630 printf_filtered (_("No checkpoints.\n"));
634 /* The PID of the process we're checkpointing. */
635 static int checkpointing_pid
= 0;
638 linux_fork_checkpointing_p (int pid
)
640 return (checkpointing_pid
== pid
);
643 /* Callback for iterate over threads. Used to check whether
644 the current inferior is multi-threaded. Returns true as soon
645 as it sees the second thread of the current inferior. */
648 inf_has_multiple_thread_cb (struct thread_info
*tp
, void *data
)
650 int *count_p
= (int *) data
;
652 if (current_inferior ()->pid
== ptid_get_pid (tp
->ptid
))
655 /* Stop the iteration if multiple threads have been detected. */
659 /* Return true if the current inferior is multi-threaded. */
662 inf_has_multiple_threads (void)
666 iterate_over_threads (inf_has_multiple_thread_cb
, &count
);
671 checkpoint_command (char *args
, int from_tty
)
673 struct objfile
*fork_objf
;
674 struct gdbarch
*gdbarch
;
675 struct target_waitstatus last_target_waitstatus
;
676 ptid_t last_target_ptid
;
677 struct value
*fork_fn
= NULL
, *ret
;
678 struct fork_info
*fp
;
681 if (!target_has_execution
)
682 error (_("The program is not being run."));
684 /* Ensure that the inferior is not multithreaded. */
685 update_thread_list ();
686 if (inf_has_multiple_threads ())
687 error (_("checkpoint: can't checkpoint multiple threads."));
689 /* Make the inferior fork, record its (and gdb's) state. */
691 if (lookup_minimal_symbol ("fork", NULL
, NULL
).minsym
!= NULL
)
692 fork_fn
= find_function_in_inferior ("fork", &fork_objf
);
694 if (lookup_minimal_symbol ("_fork", NULL
, NULL
).minsym
!= NULL
)
695 fork_fn
= find_function_in_inferior ("fork", &fork_objf
);
697 error (_("checkpoint: can't find fork function in inferior."));
699 gdbarch
= get_objfile_arch (fork_objf
);
700 ret
= value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
702 /* Tell linux-nat.c that we're checkpointing this inferior. */
704 scoped_restore save_pid
705 = make_scoped_restore (&checkpointing_pid
, ptid_get_pid (inferior_ptid
));
707 ret
= call_function_by_hand (fork_fn
, NULL
, 0, &ret
);
710 if (!ret
) /* Probably can't happen. */
711 error (_("checkpoint: call_function_by_hand returned null."));
713 retpid
= value_as_long (ret
);
714 get_last_target_status (&last_target_ptid
, &last_target_waitstatus
);
716 fp
= find_fork_pid (retpid
);
722 printf_filtered (_("checkpoint %d: fork returned pid %ld.\n"),
723 fp
!= NULL
? fp
->num
: -1, (long) retpid
);
726 parent_pid
= ptid_get_lwp (last_target_ptid
);
728 parent_pid
= ptid_get_pid (last_target_ptid
);
729 printf_filtered (_(" gdb says parent = %ld.\n"),
735 error (_("Failed to find new fork"));
736 fork_save_infrun_state (fp
, 1);
737 fp
->parent_ptid
= last_target_ptid
;
741 linux_fork_context (struct fork_info
*newfp
, int from_tty
)
743 /* Now we attempt to switch processes. */
744 struct fork_info
*oldfp
;
746 gdb_assert (newfp
!= NULL
);
748 oldfp
= find_fork_ptid (inferior_ptid
);
749 gdb_assert (oldfp
!= NULL
);
751 fork_save_infrun_state (oldfp
, 1);
752 remove_breakpoints ();
753 fork_load_infrun_state (newfp
);
754 insert_breakpoints ();
756 printf_filtered (_("Switching to %s\n"),
757 target_pid_to_str (inferior_ptid
));
759 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
762 /* Switch inferior process (checkpoint) context, by checkpoint id. */
764 restart_command (char *args
, int from_tty
)
766 struct fork_info
*fp
;
769 error (_("Requires argument (checkpoint id to restart)"));
771 if ((fp
= find_fork_id (parse_and_eval_long (args
))) == NULL
)
772 error (_("Not found: checkpoint id %s"), args
);
774 linux_fork_context (fp
, from_tty
);
778 _initialize_linux_fork (void)
782 /* Checkpoint command: create a fork of the inferior process
783 and set it aside for later debugging. */
785 add_com ("checkpoint", class_obscure
, checkpoint_command
, _("\
786 Fork a duplicate process (experimental)."));
788 /* Restart command: restore the context of a specified checkpoint
791 add_com ("restart", class_obscure
, restart_command
, _("\
792 restart <n>: restore program context from a checkpoint.\n\
793 Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'."));
795 /* Delete checkpoint command: kill the process and remove it from
798 add_cmd ("checkpoint", class_obscure
, delete_checkpoint_command
, _("\
799 Delete a checkpoint (experimental)."),
802 /* Detach checkpoint command: release the process to run independently,
803 and remove it from the fork list. */
805 add_cmd ("checkpoint", class_obscure
, detach_checkpoint_command
, _("\
806 Detach from a checkpoint (experimental)."),
809 /* Info checkpoints command: list all forks/checkpoints
810 currently under gdb's control. */
812 add_info ("checkpoints", info_checkpoints_command
,
813 _("IDs of currently known checkpoints."));