diff options
author | Michael Snyder <msnyder@specifix.com> | 2006-01-04 19:34:58 +0000 |
---|---|---|
committer | Michael Snyder <msnyder@specifix.com> | 2006-01-04 19:34:58 +0000 |
commit | e569df3823bbecc007b21a905875f83ff35c0a01 (patch) | |
tree | 73e14ab526d1602fca7eb4087803c61f17e3cb4e /gdb | |
parent | e3e0c8921135358fc6a18191362775c5b762b07b (diff) | |
download | gdb-e569df3823bbecc007b21a905875f83ff35c0a01.tar.gz |
2006-01-04 Michael Snyder <msnyder@redhat.com>
Checkpoint/Restart for Linux.
* linux-nat.c: Add support for debugging multiple forks.
Add #include for linux-fork.h (interface spec).
(super_mourn_inferior): New function pointer.
(child_mourn_inferior): New function / target method.
(linux_target): Claim to_mourn_inferior method pointer.
(child_follow_fork): Call interface to linux-fork, conditionally
add new fork processes to list of debugged processes.
(kill_inferior): Use interface to linux-fork to kill
multiple processes.
* linux-fork.h: New file.
* linux-fork.c: New file. Support for debugging multiple forks
of the same program. Support for checkpoint and restart commands.
* infrun.c (nullify_last_target_wait_ptid): New function.
* Makefile.in: Add linux-fork.
* config/*/linux.mh: Add linux-fork.
* NEWS: Mention new functionality.
Diffstat (limited to 'gdb')
-rw-r--r-- | gdb/ChangeLog | 23 | ||||
-rw-r--r-- | gdb/Makefile.in | 6 | ||||
-rw-r--r-- | gdb/NEWS | 29 | ||||
-rw-r--r-- | gdb/config/alpha/alpha-linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/arm/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/i386/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/i386/linux64.mh | 2 | ||||
-rw-r--r-- | gdb/config/ia64/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/m32r/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/m68k/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/mips/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/pa/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/powerpc/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/powerpc/ppc64-linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/sparc/linux.mh | 2 | ||||
-rw-r--r-- | gdb/config/sparc/linux64.mh | 2 | ||||
-rw-r--r-- | gdb/infrun.c | 6 | ||||
-rw-r--r-- | gdb/linux-fork.c | 689 | ||||
-rw-r--r-- | gdb/linux-fork.h | 33 | ||||
-rw-r--r-- | gdb/linux-nat.c | 139 |
20 files changed, 901 insertions, 50 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0929ab222c4..d2dbf3a6b90 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,26 @@ +2006-01-04 Michael Snyder <msnyder@redhat.com> + + Checkpoint/Restart for Linux. + * linux-nat.c: Add support for debugging multiple forks. + Add #include for linux-fork.h (interface spec). + (super_mourn_inferior): New function pointer. + (child_mourn_inferior): New function / target method. + (linux_target): Claim to_mourn_inferior method pointer. + (child_follow_fork): Call interface to linux-fork, conditionally + add new fork processes to list of debugged processes. + (kill_inferior): Use interface to linux-fork to kill + multiple processes. + + * linux-fork.h: New file. + * linux-fork.c: New file. Support for debugging multiple forks + of the same program. Support for checkpoint and restart commands. + + * infrun.c (nullify_last_target_wait_ptid): New function. + + * Makefile.in: Add linux-fork. + * config/*/linux.mh: Add linux-fork. + * NEWS: Mention new functionality. + 2006-01-02 Paul Hilfinger <hilfingr@nile.gnat.com> * ada-exp.y (syntax definitions,write_var_or_type,write_name_assoc): diff --git a/gdb/Makefile.in b/gdb/Makefile.in index bd6b96fdad7..9c52d6caa4d 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1377,7 +1377,7 @@ MAKEOVERRIDES= ALLDEPFILES = \ aix-thread.c \ - alpha-nat.c alphabsd-nat.c alpha-linux-nat.c \ + alpha-nat.c alphabsd-nat.c alpha-linux-nat.c linux-fork.c \ alpha-tdep.c alpha-linux-tdep.c alphabsd-tdep.c alphanbsd-tdep.c \ alpha-osf1-tdep.c alphafbsd-tdep.c alpha-mdebug-tdep.c \ amd64-nat.c amd64-tdep.c \ @@ -2180,10 +2180,12 @@ linespec.o: linespec.c $(defs_h) $(symtab_h) $(frame_h) $(command_h) \ $(symfile_h) $(objfiles_h) $(source_h) $(demangle_h) $(value_h) \ $(completer_h) $(cp_abi_h) $(parser_defs_h) $(block_h) \ $(objc_lang_h) $(linespec_h) $(exceptions_h) +linux-fork.o: linux-fork.c linux-fork.h $(defs_h) $(inferior_h) \ + $(regcache_h) $(gdbcmd_h) $(infcall_h) linux-nat.o: linux-nat.c $(defs_h) $(inferior_h) $(target_h) $(gdb_string_h) \ $(gdb_wait_h) $(gdb_assert_h) $(linux_nat_h) $(gdbthread_h) \ $(gdbcmd_h) $(regcache_h) $(inf_ptrace.h) $(auxv.h) $(elf_bfd_h) \ - $(gregset_h) $(gdbcore_h) $(gdbthread_h) $(gdb_stat_h) + $(gregset_h) $(gdbcore_h) $(gdbthread_h) $(gdb_stat_h) linux-fork.h linux-thread-db.o: linux-thread-db.c $(defs_h) $(gdb_assert_h) \ $(gdb_proc_service_h) $(gdb_thread_db_h) $(bfd_h) $(exceptions_h) \ $(gdbthread_h) $(inferior_h) $(symfile_h) $(objfiles_h) $(target_h) \ @@ -8,6 +8,35 @@ init-if-undefined Initialize a convenience variable, but only if it doesn't already have a value. +The following commands are presently only implemented for native GNU/Linux: + +checkpoint Save a snapshot of the program state. + +restart <n> Return the program state to a + previously saved state. + +info checkpoints List currently saved checkpoints. + +delete-checkpoint <n> Delete a previously saved checkpoint. + +set|show detach-on-fork Tell gdb whether to detach from a newly + forked process, or to keep debugging it. + +info forks List forks of the user program that + are available to be debugged. + +fork <n> Switch to debugging one of several + forks of the user program that are + available to be debugged. + +delete-fork <n> Delete a fork from the list of forks + that are available to be debugged (and + kill the forked process). + +detach-fork <n> Delete a fork from the list of forks + that are available to be debugged (and + allow the process to continue). + * New architecture Morpho Technologies ms2 ms1-elf diff --git a/gdb/config/alpha/alpha-linux.mh b/gdb/config/alpha/alpha-linux.mh index 5ee0ec30aa4..28a50c1c16e 100644 --- a/gdb/config/alpha/alpha-linux.mh +++ b/gdb/config/alpha/alpha-linux.mh @@ -2,7 +2,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o corelow.o alpha-nat.o alpha-linux-nat.o \ fork-child.o proc-service.o linux-thread-db.o gcore.o \ - linux-nat.o + linux-nat.o linux-fork.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/config/arm/linux.mh b/gdb/config/arm/linux.mh index 34a168956a6..32181a431aa 100644 --- a/gdb/config/arm/linux.mh +++ b/gdb/config/arm/linux.mh @@ -3,7 +3,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o corelow.o \ core-regset.o arm-linux-nat.o gcore.o \ - proc-service.o linux-thread-db.o linux-nat.o + proc-service.o linux-thread-db.o linux-nat.o linux-fork.o LOADLIBES= -ldl -rdynamic diff --git a/gdb/config/i386/linux.mh b/gdb/config/i386/linux.mh index ec058a5daf7..62fff994b30 100644 --- a/gdb/config/i386/linux.mh +++ b/gdb/config/i386/linux.mh @@ -4,7 +4,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o corelow.o \ core-aout.o i386-nat.o i386-linux-nat.o \ proc-service.o linux-thread-db.o gcore.o \ - linux-nat.o + linux-nat.o linux-fork.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/config/i386/linux64.mh b/gdb/config/i386/linux64.mh index b764ca5d2c3..1c03d7172c6 100644 --- a/gdb/config/i386/linux64.mh +++ b/gdb/config/i386/linux64.mh @@ -1,7 +1,7 @@ # Host: GNU/Linux x86-64 NATDEPFILES= inf-ptrace.o fork-child.o \ i386-nat.o amd64-nat.o amd64-linux-nat.o linux-nat.o \ - proc-service.o linux-thread-db.o gcore.o + proc-service.o linux-thread-db.o gcore.o linux-fork.o NAT_FILE= nm-linux64.h # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/ia64/linux.mh b/gdb/config/ia64/linux.mh index 2e43f333ae5..3ae22b26298 100644 --- a/gdb/config/ia64/linux.mh +++ b/gdb/config/ia64/linux.mh @@ -3,6 +3,6 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o corelow.o gcore.o \ core-aout.o core-regset.o ia64-linux-nat.o \ - proc-service.o linux-thread-db.o linux-nat.o + proc-service.o linux-thread-db.o linux-nat.o linux-fork.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/m32r/linux.mh b/gdb/config/m32r/linux.mh index 0986bab39bd..0f94bd8d9ed 100644 --- a/gdb/config/m32r/linux.mh +++ b/gdb/config/m32r/linux.mh @@ -3,6 +3,6 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o corelow.o \ m32r-linux-nat.o gcore.o proc-service.o linux-thread-db.o \ - linux-nat.o + linux-nat.o linux-fork.o LOADLIBES= -ldl -rdynamic diff --git a/gdb/config/m68k/linux.mh b/gdb/config/m68k/linux.mh index e867396811e..871632da8d7 100644 --- a/gdb/config/m68k/linux.mh +++ b/gdb/config/m68k/linux.mh @@ -3,7 +3,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ corelow.o core-aout.o m68klinux-nat.o gcore.o \ - proc-service.o linux-thread-db.o linux-nat.o + proc-service.o linux-thread-db.o linux-nat.o linux-fork.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/config/mips/linux.mh b/gdb/config/mips/linux.mh index 56f4f9cb704..4f5e01ff380 100644 --- a/gdb/config/mips/linux.mh +++ b/gdb/config/mips/linux.mh @@ -2,6 +2,6 @@ NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o mips-linux-nat.o \ linux-thread-db.o proc-service.o gcore.o \ - linux-nat.o + linux-nat.o linux-fork.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/pa/linux.mh b/gdb/config/pa/linux.mh index 59b4c58d2bd..b0362e49cc1 100644 --- a/gdb/config/pa/linux.mh +++ b/gdb/config/pa/linux.mh @@ -3,6 +3,6 @@ XDEPFILES= NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o corelow.o gcore.o \ core-regset.o hppa-linux-nat.o \ - proc-service.o linux-thread-db.o linux-nat.o + proc-service.o linux-thread-db.o linux-nat.o linux-fork.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/powerpc/linux.mh b/gdb/config/powerpc/linux.mh index 8b3cbbbd8c1..9a6a2a49a9a 100644 --- a/gdb/config/powerpc/linux.mh +++ b/gdb/config/powerpc/linux.mh @@ -5,6 +5,6 @@ XM_CLIBS= NAT_FILE= nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ ppc-linux-nat.o proc-service.o linux-thread-db.o \ - gcore.o linux-nat.o + gcore.o linux-nat.o linux-fork.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/powerpc/ppc64-linux.mh b/gdb/config/powerpc/ppc64-linux.mh index 0a4a4abb9dc..6cee59098ba 100644 --- a/gdb/config/powerpc/ppc64-linux.mh +++ b/gdb/config/powerpc/ppc64-linux.mh @@ -5,7 +5,7 @@ XM_CLIBS= NAT_FILE= nm-ppc64-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ ppc-linux-nat.o proc-service.o linux-thread-db.o \ - gcore.o linux-nat.o + gcore.o linux-nat.o linux-fork.o # The PowerPC has severe limitations on TOC size, and uses them even # for non-PIC code. GDB overflows those tables when compiling with diff --git a/gdb/config/sparc/linux.mh b/gdb/config/sparc/linux.mh index bc52fb7b4a9..04943814902 100644 --- a/gdb/config/sparc/linux.mh +++ b/gdb/config/sparc/linux.mh @@ -3,7 +3,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= sparc-nat.o sparc-sol2-nat.o sparc-linux-nat.o \ corelow.o core-regset.o fork-child.o inf-ptrace.o \ proc-service.o linux-thread-db.o \ - gcore.o linux-nat.o + gcore.o linux-nat.o linux-fork.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/config/sparc/linux64.mh b/gdb/config/sparc/linux64.mh index 26514cfd4f4..59dfbf0ac72 100644 --- a/gdb/config/sparc/linux64.mh +++ b/gdb/config/sparc/linux64.mh @@ -4,7 +4,7 @@ NATDEPFILES= sparc-nat.o sparc64-nat.o sparc-sol2-nat.o sparc64-linux-nat.o \ corelow.o core-regset.o \ fork-child.o inf-ptrace.o \ proc-service.o linux-thread-db.o \ - gcore.o linux-nat.o + gcore.o linux-nat.o linux-fork.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/infrun.c b/gdb/infrun.c index 84c920f62d5..47fbe09ed72 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -1102,6 +1102,12 @@ get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status) *status = target_last_waitstatus; } +void +nullify_last_target_wait_ptid (void) +{ + target_last_wait_ptid = minus_one_ptid; +} + /* Switch thread contexts, maintaining "infrun state". */ static void diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c new file mode 100644 index 00000000000..46b0d904c09 --- /dev/null +++ b/gdb/linux-fork.c @@ -0,0 +1,689 @@ +/* GNU/Linux native-dependent code for debugging multiple forks. + + Copyright (C) 2005 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ + +#include "defs.h" +#include "inferior.h" +#include "regcache.h" +#include "gdbcmd.h" +#include "infcall.h" +#include "gdb_string.h" +#include "linux-fork.h" + +#include <sys/ptrace.h> +#include <sys/wait.h> +#include <sys/param.h> +#include <dirent.h> +#include <ctype.h> + +struct fork_info *fork_list; +static int highest_fork_num; + +/* Prevent warning from -Wmissing-prototypes. */ +extern void _initialize_linux_fork (void); + +int detach_fork = 1; /* Default behavior is to detach + newly forked processes (legacy). */ + +/* Fork list data structure: */ +struct fork_info +{ + struct fork_info *next; + ptid_t ptid; + int num; /* Convenient handle (GDB fork id) */ + struct regcache *savedregs; /* Convenient for info fork, saves + having to actually switch contexts. */ + int clobber_regs; /* True if we should restore saved regs. */ + ULONGEST pc; /* PC for info fork. */ + off_t *filepos; /* Set of open file descriptors' offsets. */ + int maxfd; +}; + +/* Fork list methods: */ + +extern int +forks_exist_p (void) +{ + return (fork_list != NULL); +} + +/* Add a fork to internal fork list. + Called from linux child_follow_fork. */ + +extern struct fork_info * +add_fork (pid_t pid) +{ + struct fork_info *fp; + + if (fork_list == NULL && + pid != PIDGET (inferior_ptid)) + { + /* Special case -- if this is the first fork in the list + (the list is hitherto empty), and if this new fork is + NOT the current inferior_ptid, then add inferior_ptid + first, as a special zeroeth fork id. */ + highest_fork_num = -1; + add_fork (PIDGET (inferior_ptid)); /* safe recursion */ + } + + fp = XZALLOC (struct fork_info); + fp->ptid = pid_to_ptid (pid); + fp->num = ++highest_fork_num; + fp->next = fork_list; + fork_list = fp; + return fp; +} + +static void +free_fork (struct fork_info *fp) +{ + /* Notes on step-resume breakpoints: since this is a concern for + threads, let's convince ourselves that it's not a concern for + forks. There are two ways for a fork_info to be created. First, + by the checkpoint command, in which case we're at a gdb prompt + and there can't be any step-resume breakpoint. Second, by a fork + in the user program, in which case we *may* have stepped into the + fork call, but regardless of whether we follow the parent or the + child, we will return to the same place and the step-resume + breakpoint, if any, will take care of itself as usual. And + unlike threads, we do not save a private copy of the step-resume + breakpoint -- so we're OK. */ + + if (fp) + { + if (fp->savedregs) + regcache_xfree (fp->savedregs); + if (fp->filepos) + xfree (fp->filepos); + xfree (fp); + } +} + +static void +delete_fork (ptid_t ptid) +{ + struct fork_info *fp, *fpprev; + + fpprev = NULL; + + for (fp = fork_list; fp; fpprev = fp, fp = fp->next) + if (ptid_equal (fp->ptid, ptid)) + break; + + if (!fp) + return; + + if (fpprev) + fpprev->next = fp->next; + else + fork_list = fp->next; + + free_fork (fp); + + /* Special case: if there is now only one process in the list, + and if it is (hopefully!) the current inferior_ptid, then + remove it, leaving the list empty -- we're now down to the + default case of debugging a single process. */ + if (fork_list != NULL && fork_list->next == NULL && + ptid_equal (fork_list->ptid, inferior_ptid)) + { + /* Last fork -- delete from list and handle as solo process + (should be a safe recursion). */ + delete_fork (inferior_ptid); + } +} + +/* Find a fork_info by matching PTID. */ +static struct fork_info * +find_fork_ptid (ptid_t ptid) +{ + struct fork_info *fp; + + for (fp = fork_list; fp; fp = fp->next) + if (ptid_equal (fp->ptid, ptid)) + return fp; + + return NULL; +} + +/* Find a fork_info by matching ID. */ +static struct fork_info * +find_fork_id (int num) +{ + struct fork_info *fp; + + for (fp = fork_list; fp; fp = fp->next) + if (fp->num == num) + return fp; + + return NULL; +} + +/* Find a fork_info by matching pid. */ +extern struct fork_info * +find_fork_pid (pid_t pid) +{ + struct fork_info *fp; + + for (fp = fork_list; fp; fp = fp->next) + if (pid == ptid_get_pid (fp->ptid)) + return fp; + + return NULL; +} + +static ptid_t +fork_id_to_ptid (int num) +{ + struct fork_info *fork = find_fork_id (num); + if (fork) + return fork->ptid; + else + return pid_to_ptid (-1); +} + +static void +init_fork_list (void) +{ + struct fork_info *fp, *fpnext; + + if (!fork_list) + return; + + for (fp = fork_list; fp; fp = fpnext) + { + fpnext = fp->next; + free_fork (fp); + } + + fork_list = NULL; +} + +/* Fork list <-> gdb interface. */ + +/* Utility function for fork_load/fork_save. + Calls lseek in the (current) inferior process. */ + +static off_t +call_lseek (int fd, off_t offset, int whence) +{ + char exp[80]; + + snprintf (&exp[0], sizeof (exp), "lseek (%d, %ld, %d)", + fd, (long) offset, whence); + return (off_t) parse_and_eval_long (&exp[0]); +} + +/* Load infrun state for the fork PTID. */ + +static void +fork_load_infrun_state (struct fork_info *fp) +{ + extern void nullify_last_target_wait_ptid (); + int i; + + if (fp->savedregs && fp->clobber_regs) + regcache_cpy (current_regcache, fp->savedregs); + + nullify_last_target_wait_ptid (); + + /* Now restore the file positions of open file descriptors. */ + if (fp->filepos) + { + for (i = 0; i <= fp->maxfd; i++) + if (fp->filepos[i] != (off_t) -1) + call_lseek (i, fp->filepos[i], SEEK_SET); + /* NOTE: I can get away with using SEEK_SET and SEEK_CUR because + this is native-only. If it ever has to be cross, we'll have + to rethink this. */ + } +} + +/* Save infrun state for the fork PTID. + Exported for use by linux child_follow_fork. */ + +extern void +fork_save_infrun_state (struct fork_info *fp, int clobber_regs) +{ + char path[MAXPATHLEN]; + struct dirent *de; + DIR *d; + + if (fp->savedregs) + regcache_xfree (fp->savedregs); + + fp->savedregs = regcache_dup (current_regcache); + fp->clobber_regs = clobber_regs; + fp->pc = read_pc (); + + if (clobber_regs) + { + /* Now save the 'state' (file position) of all open file descriptors. + Unfortunately fork does not take care of that for us... */ + snprintf (path, MAXPATHLEN, "/proc/%ld/fd", (long) PIDGET (fp->ptid)); + if ((d = opendir (path)) != NULL) + { + long tmp; + + fp->maxfd = 0; + while ((de = readdir (d)) != NULL) + { + /* Count open file descriptors (actually find highest + numbered). */ + tmp = strtol (&de->d_name[0], NULL, 10); + if (fp->maxfd < tmp) + fp->maxfd = tmp; + } + /* Allocate array of file positions. */ + fp->filepos = xrealloc (fp->filepos, + (fp->maxfd + 1) * sizeof (*fp->filepos)); + + /* Initialize to -1 (invalid). */ + for (tmp = 0; tmp <= fp->maxfd; tmp++) + fp->filepos[tmp] = -1; + + /* Now find actual file positions. */ + rewinddir (d); + while ((de = readdir (d)) != NULL) + if (isdigit (de->d_name[0])) + { + tmp = strtol (&de->d_name[0], NULL, 10); + fp->filepos[tmp] = call_lseek (tmp, 0, SEEK_CUR); + } + closedir (d); + } + } +} + +/* Kill 'em all, let God sort 'em out... */ + +extern void +linux_fork_killall (void) +{ + /* Walk list and kill every pid. No need to treat the + current inferior_ptid as special (we do not return a + status for it) -- however any process may be a child + or a parent, so may get a SIGCHLD from a previously + killed child. Wait them all out. */ + pid_t pid, ret; + int status; + + do { + pid = PIDGET (fork_list->ptid); + do { + ptrace (PT_KILL, pid, 0, 0); + ret = waitpid (pid, &status, 0); + } while (ret == pid && WIFSTOPPED (status)); + delete_fork (fork_list->ptid); + } while (fork_list != NULL); +} + +/* The current inferior_ptid has exited, but there are other viable + forks to debug. Delete the exiting one and context-switch to the + first available. */ + +extern void +linux_fork_mourn_inferior (void) +{ + /* Wait just one more time to collect the inferior's exit status. + Do not check whether this succeeds though, since we may be + dealing with a process that we attached to. Such a process will + only report its exit status to its original parent. */ + int status; + + waitpid (ptid_get_pid (inferior_ptid), &status, 0); + + /* OK, presumably inferior_ptid is the one who has exited. + We need to delete that one from the fork_list, and switch + to the next available fork. */ + delete_fork (inferior_ptid); + if (fork_list) /* Paranoia, shouldn't happen. */ + { + inferior_ptid = fork_list[0].ptid; + printf_filtered (_("[Switching to %s]\n"), + target_pid_to_str (inferior_ptid)); + } +} + +/* Fork list <-> user interface. */ + +static void +delete_fork_command (char *args, int from_tty) +{ + ptid_t ptid; + + if (!args || !*args) + error (_("Requires argument (fork/checkpoint id to delete)")); + + ptid = fork_id_to_ptid (parse_and_eval_long (args)); + if (ptid_equal (ptid, minus_one_ptid)) + error (_("No such fork/checkpoint id, %s"), args); + + if (ptid_equal (ptid, inferior_ptid)) + error (_("Please switch to another fork/checkpoint before deleting the current one")); + + if (ptrace (PTRACE_KILL, ptid, 0, 0)) + error (_("Unable to kill pid %s"), target_tid_to_str (ptid)); + + if (from_tty) + printf_filtered (_("Killed %s\n"), target_pid_to_str (ptid)); + + delete_fork (ptid); +} + +static void +detach_fork_command (char *args, int from_tty) +{ + ptid_t ptid; + + if (!args || !*args) + error (_("Requires argument (fork id to detach)")); + + ptid = fork_id_to_ptid (parse_and_eval_long (args)); + if (ptid_equal (ptid, minus_one_ptid)) + error (_("No such fork id, %s"), args); + + if (ptid_equal (ptid, inferior_ptid)) + error (_("Please switch to another fork before detaching the current one")); + + if (ptrace (PTRACE_DETACH, ptid, 0, 0)) + error (_("Unable to detach %s"), target_pid_to_str (ptid)); + + if (from_tty) + printf_filtered (_("Detached %s\n"), target_pid_to_str (ptid)); + + delete_fork (ptid); +} + +/* Print information about currently known forks. */ + +static void +info_forks_command (char *arg, int from_tty) +{ + struct frame_info *cur_frame; + struct symtab_and_line sal; + struct symtab *cur_symtab; + struct fork_info *fp; + int cur_line; + ULONGEST pc; + + for (fp = fork_list; fp; fp = fp->next) + { + if (ptid_equal (fp->ptid, inferior_ptid)) + { + printf_filtered ("* "); + pc = read_pc (); + } + else + { + printf_filtered (" "); + pc = fp->pc; + } + printf_filtered ("%d %s", fp->num, target_pid_to_str (fp->ptid)); + if (fp->num == 0) + printf_filtered (_(" (main process)")); + printf_filtered (_(" at ")); + deprecated_print_address_numeric (pc, 1, gdb_stdout); + + sal = find_pc_line (pc, 0); + if (sal.symtab) + { + char *tmp = strrchr (sal.symtab->filename, '/'); + + if (tmp) + printf_filtered (_(", file %s"), tmp + 1); + else + printf_filtered (_(", file %s"), sal.symtab->filename); + } + if (sal.line) + printf_filtered (_(", line %d"), sal.line); + if (!sal.symtab && !sal.line) + { + struct minimal_symbol *msym; + + msym = lookup_minimal_symbol_by_pc (pc); + if (msym) + printf_filtered (", <%s>", SYMBOL_LINKAGE_NAME (msym)); + } + + putchar_filtered ('\n'); + } +} + +/* Save/restore mode variable 'detach_fork': + We need to temporarily take over this mode variable, while + preserving the user-specified state, and make sure that it + gets restored in case of error. + + The int pointer that we use comes from the caller, so we can + be called more than once (even though currently we don't need to). */ + +static void +restore_detach_fork (void *arg) +{ + detach_fork = *(int *) arg; +} + +static struct cleanup * +save_detach_fork (int *saved_val) +{ + *saved_val = detach_fork; + return make_cleanup (restore_detach_fork, (void *) saved_val); +} + +static void +checkpoint_command (char *args, int from_tty) +{ + struct target_waitstatus last_target_waitstatus; + ptid_t last_target_ptid; + struct value *fork_fn = NULL, *ret; + struct fork_info *fp; + pid_t retpid; + struct cleanup *old_chain; + long i; + /* Make this temp var static, 'cause it's used in the error context. */ + static int temp_detach_fork; + + /* Make the inferior fork, record its (and gdb's) state. */ + + if (lookup_minimal_symbol ("fork", NULL, NULL) != NULL) + fork_fn = find_function_in_inferior ("fork"); + if (!fork_fn) + if (lookup_minimal_symbol ("_fork", NULL, NULL) != NULL) + fork_fn = find_function_in_inferior ("fork"); + if (!fork_fn) + error (_("checkpoint: can't find fork function in inferior.")); + + ret = value_from_longest (builtin_type_int, 0); + old_chain = save_detach_fork (&temp_detach_fork); + detach_fork = 0; + ret = call_function_by_hand (fork_fn, 0, &ret); + do_cleanups (old_chain); + if (!ret) /* Probably can't happen. */ + error (_("checkpoint: call_function_by_hand returned null.")); + + retpid = value_as_long (ret); + get_last_target_status (&last_target_ptid, &last_target_waitstatus); + if (from_tty) + { + int parent_pid; + + printf_filtered (_("checkpoint: fork returned pid %ld.\n"), + (long) retpid); + if (info_verbose) + { + parent_pid = ptid_get_lwp (last_target_ptid); + if (parent_pid == 0) + parent_pid = ptid_get_pid (last_target_ptid); + printf_filtered (_(" gdb says parent = %ld.\n"), + (long) parent_pid); + } + } + + fp = find_fork_pid (retpid); + if (!fp) + error (_("Failed to find new fork")); + fork_save_infrun_state (fp, 1); +} + +static void +linux_fork_context (struct fork_info *newfp, int from_tty) +{ + /* Now we attempt to switch processes. */ + struct fork_info *oldfp = find_fork_ptid (inferior_ptid); + ptid_t ptid; + int id, i; + + if (!newfp) + error (_("No such fork/process")); + + if (!oldfp) + { + oldfp = add_fork (ptid_get_pid (inferior_ptid)); + } + + fork_save_infrun_state (oldfp, 1); + inferior_ptid = newfp->ptid; + fork_load_infrun_state (newfp); + registers_changed (); + reinit_frame_cache (); + stop_pc = read_pc (); + select_frame (get_current_frame ()); + + printf_filtered (_("Switching to %s\n"), + target_pid_to_str (inferior_ptid)); + + print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); +} + +/* Switch inferior process (fork) context, by process id. */ +static void +process_command (char *args, int from_tty) +{ + struct fork_info *fp; + + if (!args || !*args) + error (_("Requires argument (process id to switch to)")); + + if ((fp = find_fork_pid (parse_and_eval_long (args))) == NULL) + error (_("Not found: process id %s"), args); + + linux_fork_context (fp, from_tty); +} + +/* Switch inferior process (fork) context, by fork id. */ +static void +fork_command (char *args, int from_tty) +{ + struct fork_info *fp; + + if (!args || !*args) + error (_("Requires argument (fork id to switch to)")); + + if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) + error (_("Not found: fork id %s"), args); + + linux_fork_context (fp, from_tty); +} + +/* Switch inferior process (fork) context, by checkpoint id. */ +static void +restart_command (char *args, int from_tty) +{ + struct fork_info *fp; + + if (!args || !*args) + error (_("Requires argument (checkpoint id to restart)")); + + if ((fp = find_fork_id (parse_and_eval_long (args))) == NULL) + error (_("Not found: checkpoint id %s"), args); + + linux_fork_context (fp, from_tty); +} + +void +_initialize_linux_fork (void) +{ + init_fork_list (); + + /* Set/show detach-on-fork: user-settable mode. */ + + add_setshow_boolean_cmd ("detach-on-fork", class_obscure, &detach_fork, _("\ +Set whether gdb will detach the child of a fork."), _("\ +Show whether gdb will detach the child of a fork."), _("\ +Tells gdb whether to detach the child of a fork."), + NULL, NULL, &setlist, &showlist); + + /* Set/show restart-auto-finish: user-settable count. Causes the + first "restart" of a fork to do some number of "finish" commands + before returning to user. + + Useful because otherwise the virgin fork process will be stopped + somewhere in the un-interesting fork system call. */ + + /* Checkpoint command: create a fork of the inferior process + and set it aside for later debugging. */ + + add_com ("checkpoint", class_obscure, checkpoint_command, _("\ +Fork a duplicate process (experimental).")); + + /* Restart command: restore the context of a specified fork + process. May be used for "program forks" as well as for + "debugger forks" (checkpoints). */ + + add_com ("restart", class_obscure, restart_command, _("\ +restart <n>: restore program context from a checkpoint.\n\ +Argument 'n' is checkpoint ID, as displayed by 'info checkpoints'.")); + + /* Delete-checkpoint command: kill the process and remove it from + fork list. */ + + add_com ("delete-checkpoint", class_obscure, delete_fork_command, _("\ +Delete a fork/checkpoint (experimental).")); + + /* Detach-checkpoint command: release the process to run independantly, + and remove it from the fork list. */ + + add_com ("detach-checkpoint", class_obscure, detach_fork_command, _("\ +Detach from a fork/checkpoint (experimental).")); + + /* Info checkpoints command: list all forks/checkpoints + currently under gdb's control. */ + + add_info ("checkpoints", info_forks_command, + _("IDs of currently known forks/checkpoints.")); + + /* Command aliases (let "fork" and "checkpoint" be used + interchangeably). */ + + add_com_alias ("delete-fork", "delete-checkpoint", class_obscure, 1); + add_com_alias ("detach-fork", "detach-checkpoint", class_obscure, 1); + add_info_alias ("forks", "checkpoints", 0); + + /* "fork <n>" (by analogy to "thread <n>"). */ + add_com ("fork", class_obscure, fork_command, _("\ +fork <n>: Switch between forked processes.\n\ +Argument 'n' is fork ID, as displayed by 'info forks'.")); + + /* "process <proc id>" as opposed to "fork <fork id>". */ + add_com ("process", class_obscure, process_command, _("\ +process <pid>: Switch between forked processes.\n\ +Argument 'pid' is process ID, as displayed by 'info forks' or 'shell ps'.")); +} diff --git a/gdb/linux-fork.h b/gdb/linux-fork.h new file mode 100644 index 00000000000..9b154038061 --- /dev/null +++ b/gdb/linux-fork.h @@ -0,0 +1,33 @@ +/* GNU/Linux native-dependent code for debugging multiple forks. + + Copyright 2005 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +struct fork_info; +extern struct fork_info *add_fork (pid_t); +extern struct fork_info *find_fork_pid (pid_t); +extern void fork_save_infrun_state (struct fork_info *, int); +extern void linux_fork_killall (void); +extern void linux_fork_mourn_inferior (void); +extern int forks_exist_p (void); + +struct fork_info *fork_list; + +extern int detach_fork; + diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 786e549c035..7b4bed8c927 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -31,6 +31,7 @@ #endif #include <sys/ptrace.h> #include "linux-nat.h" +#include "linux-fork.h" #include "gdbthread.h" #include "gdbcmd.h" #include "regcache.h" @@ -87,12 +88,18 @@ the use of the multi-threaded target. */ static struct target_ops *linux_ops; -/* The saved to_xfer_partial method, inherited from inf-ptrace.c. Called - by our to_xfer_partial. */ -static LONGEST (*super_xfer_partial) (struct target_ops *, enum target_object, - const char *, gdb_byte *, const gdb_byte *, +/* The saved to_xfer_partial method, inherited from inf-ptrace.c. + Called by our to_xfer_partial. */ +static LONGEST (*super_xfer_partial) (struct target_ops *, + enum target_object, + const char *, gdb_byte *, + const gdb_byte *, ULONGEST, LONGEST); +/* The saved to_mourn_inferior method, inherited from inf-ptrace.c. + Called by our to_mourn_inferior. */ +static void (*super_mourn_inferior) (void); + static int debug_linux_nat; static void show_debug_linux_nat (struct ui_file *file, int from_tty, @@ -363,15 +370,28 @@ child_follow_fork (struct target_ops *ops, int follow_child) also, but they'll be reinserted below. */ detach_breakpoints (child_pid); - if (debug_linux_nat) + /* Detach new forked process? */ + if (detach_fork) { - target_terminal_ours (); - fprintf_unfiltered (gdb_stdlog, - "Detaching after fork from child process %d.\n", - child_pid); - } + if (debug_linux_nat) + { + target_terminal_ours (); + fprintf_filtered (gdb_stdlog, + "Detaching after fork from child process %d.\n", + child_pid); + } - ptrace (PTRACE_DETACH, child_pid, 0, 0); + ptrace (PTRACE_DETACH, child_pid, 0, 0); + } + else + { + struct fork_info *fp; + /* Retain child fork in ptrace (stopped) state. */ + fp = find_fork_pid (child_pid); + if (!fp) + fp = add_fork (child_pid); + fork_save_infrun_state (fp, 0); + } if (has_vforked) { @@ -441,9 +461,9 @@ child_follow_fork (struct target_ops *ops, int follow_child) if (debug_linux_nat) { target_terminal_ours (); - fprintf_unfiltered (gdb_stdlog, - "Attaching after fork to child process %d.\n", - child_pid); + fprintf_filtered (gdb_stdlog, + "Attaching after fork to child process %d.\n", + child_pid); } /* If we're vforking, we may want to hold on to the parent until @@ -466,13 +486,25 @@ child_follow_fork (struct target_ops *ops, int follow_child) if (has_vforked) linux_parent_pid = parent_pid; + else if (!detach_fork) + { + struct fork_info *fp; + /* Retain parent fork in ptrace (stopped) state. */ + fp = find_fork_pid (parent_pid); + if (!fp) + fp = add_fork (parent_pid); + fork_save_infrun_state (fp, 0); + } else - target_detach (NULL, 0); + { + target_detach (NULL, 0); + } inferior_ptid = pid_to_ptid (child_pid); /* Reinstall ourselves, since we might have been removed in target_detach (which does other necessary cleanup). */ + push_target (ops); /* Reset breakpoints in the child as appropriate. */ @@ -579,33 +611,42 @@ kill_inferior (void) if (pid == 0) return; - /* If we're stopped while forking and we haven't followed yet, kill the - other task. We need to do this first because the parent will be - sleeping if this is a vfork. */ - - get_last_target_status (&last_ptid, &last); - - if (last.kind == TARGET_WAITKIND_FORKED - || last.kind == TARGET_WAITKIND_VFORKED) + /* First cut -- let's crudely do everything inline. */ + if (forks_exist_p ()) { - ptrace (PT_KILL, last.value.related_pid, 0, 0); - wait (&status); + linux_fork_killall (); + pop_target (); + generic_mourn_inferior (); } + else + { + /* If we're stopped while forking and we haven't followed yet, + kill the other task. We need to do this first because the + parent will be sleeping if this is a vfork. */ - /* Kill the current process. */ - ptrace (PT_KILL, pid, 0, 0); - ret = wait (&status); + get_last_target_status (&last_ptid, &last); - /* We might get a SIGCHLD instead of an exit status. This is - aggravated by the first kill above - a child has just died. */ + if (last.kind == TARGET_WAITKIND_FORKED + || last.kind == TARGET_WAITKIND_VFORKED) + { + ptrace (PT_KILL, last.value.related_pid, 0, 0); + wait (&status); + } - while (ret == pid && WIFSTOPPED (status)) - { + /* Kill the current process. */ ptrace (PT_KILL, pid, 0, 0); ret = wait (&status); - } - target_mourn_inferior (); + /* We might get a SIGCHLD instead of an exit status. This is + aggravated by the first kill above - a child has just died. */ + + while (ret == pid && WIFSTOPPED (status)) + { + ptrace (PT_KILL, pid, 0, 0); + ret = wait (&status); + } + target_mourn_inferior (); + } } /* On GNU/Linux there are no real LWP's. The closest thing to LWP's @@ -1677,7 +1718,7 @@ select_event_lwp (struct lwp_info **orig_lp, int *status) int random_selector; struct lwp_info *event_lp; - /* Record the wait status for the origional LWP. */ + /* Record the wait status for the original LWP. */ (*orig_lp)->status = *status; /* Give preference to any LWP that is being single-stepped. */ @@ -1729,6 +1770,30 @@ resumed_callback (struct lwp_info *lp, void *data) return lp->resumed; } +/* Local mourn_inferior -- we need to override mourn_inferior + so that we can do something clever if one of several forks + has exited. */ + +static void +child_mourn_inferior (void) +{ + int status; + + if (! forks_exist_p ()) + { + /* Normal case, no other forks available. */ + super_mourn_inferior (); + return; + } + else + { + /* Multi-fork case. The current inferior_ptid has exited, but + there are other viable forks to debug. Delete the exiting + one and context-switch to the first available. */ + linux_fork_mourn_inferior (); + } +} + /* We need to override child_wait to support attaching to cloned processes, since a normal wait (as done by the default version) ignores those processes. */ @@ -3201,6 +3266,9 @@ linux_target (void) super_xfer_partial = t->to_xfer_partial; t->to_xfer_partial = linux_xfer_partial; + super_mourn_inferior = t->to_mourn_inferior; + t->to_mourn_inferior = child_mourn_inferior; + linux_ops = t; return t; } @@ -3312,3 +3380,4 @@ lin_thread_get_thread_signals (sigset_t *set) /* ... except during a sigsuspend. */ sigdelset (&suspend_mask, cancel); } + |