2007-07-23 Michael Snyder <msnyder@access-company.com>
[deliverable/binutils-gdb.git] / gdb / linux-fork.c
index 768c7da59574aa2ba90bb54be176a3c40dee00d6..9f0c0d11277eced146b911e91a5c5b61fbdd6b99 100644 (file)
@@ -1,6 +1,6 @@
 /* GNU/Linux native-dependent code for debugging multiple forks.
 
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "regcache.h"
 #include "gdbcmd.h"
 #include "infcall.h"
+#include "gdb_assert.h"
 #include "gdb_string.h"
 #include "linux-fork.h"
+#include "linux-nat.h"
 
 #include <sys/ptrace.h>
 #include <sys/wait.h>
@@ -72,8 +74,7 @@ add_fork (pid_t pid)
 {
   struct fork_info *fp;
 
-  if (fork_list == NULL &&
-      pid != PIDGET (inferior_ptid))
+  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
@@ -84,7 +85,7 @@ add_fork (pid_t pid)
     }
 
   fp = XZALLOC (struct fork_info);
-  fp->ptid = pid_to_ptid (pid);
+  fp->ptid = ptid_build (pid, pid, 0);
   fp->num = ++highest_fork_num;
   fp->next = fork_list;
   fork_list = fp;
@@ -239,9 +240,17 @@ fork_load_infrun_state (struct fork_info *fp)
   extern void nullify_last_target_wait_ptid ();
   int i;
 
+  inferior_ptid = fp->ptid;
+
+  linux_nat_switch_fork (inferior_ptid);
+
   if (fp->savedregs && fp->clobber_regs)
-    regcache_cpy (current_regcache, fp->savedregs);
+    regcache_cpy (get_current_regcache (), fp->savedregs);
 
+  registers_changed ();
+  reinit_frame_cache ();
+
+  stop_pc = read_pc ();
   nullify_last_target_wait_ptid ();
 
   /* Now restore the file positions of open file descriptors.  */
@@ -269,7 +278,7 @@ fork_save_infrun_state (struct fork_info *fp, int clobber_regs)
   if (fp->savedregs)
     regcache_xfree (fp->savedregs);
 
-  fp->savedregs = regcache_dup (current_regcache);
+  fp->savedregs = regcache_dup (get_current_regcache ());
   fp->clobber_regs = clobber_regs;
   fp->pc = read_pc ();
 
@@ -322,17 +331,22 @@ linux_fork_killall (void)
      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.  */
+  struct fork_info *fp;
   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);
+  for (fp = fork_list; fp; fp = fp->next)
+    {
+      pid = PIDGET (fp->ptid);
+      do {
+       ptrace (PT_KILL, pid, 0, 0);
+       ret = waitpid (pid, &status, 0);
+       /* We might get a SIGCHLD instead of an exit status.  This is
+        aggravated by the first kill above - a child has just
+        died.  MVS comment cut-and-pasted from linux-nat.  */
+      } while (ret == pid && WIFSTOPPED (status));
+    }
+  init_fork_list ();   /* Clear list, prepare to start fresh.  */
 }
 
 /* The current inferior_ptid has exited, but there are other viable
@@ -354,12 +368,19 @@ linux_fork_mourn_inferior (void)
      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));
-    }
+
+  /* There should still be a fork - if there's only one left,
+     delete_fork won't remove it, because we haven't updated
+     inferior_ptid yet.  */
+  gdb_assert (fork_list);
+
+  fork_load_infrun_state (fork_list);
+  printf_filtered (_("[Switching to %s]\n"),
+                  target_pid_to_str (inferior_ptid));
+
+  /* If there's only one fork, switch back to non-fork mode.  */
+  if (fork_list->next == NULL)
+    delete_fork (inferior_ptid);
 }
 
 /* Fork list <-> user interface.  */
@@ -423,9 +444,18 @@ info_forks_command (char *arg, int from_tty)
   struct fork_info *fp;
   int cur_line;
   ULONGEST pc;
+  int requested = -1;
+  struct fork_info *printed = NULL;
+
+  if (arg && *arg)
+    requested = (int) parse_and_eval_long (arg);
 
   for (fp = fork_list; fp; fp = fp->next)
     {
+      if (requested > 0 && fp->num != requested)
+       continue;
+
+      printed = fp;
       if (ptid_equal (fp->ptid, inferior_ptid))
        {
          printf_filtered ("* ");
@@ -465,6 +495,13 @@ info_forks_command (char *arg, int from_tty)
 
       putchar_filtered ('\n');
     }
+  if (printed == NULL)
+    {
+      if (requested > 0)
+       printf_filtered (_("No fork number %d.\n"), requested);
+      else
+       printf_filtered (_("No forks.\n"));
+    }
 }
 
 /* Save/restore mode variable 'detach_fork':
@@ -555,17 +592,10 @@ linux_fork_context (struct fork_info *newfp, int from_tty)
     error (_("No such fork/process"));
 
   if (!oldfp)
-    {
-      oldfp = add_fork (ptid_get_pid (inferior_ptid));
-    }
+    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));
@@ -652,17 +682,19 @@ Fork a duplicate process (experimental)."));
 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
+  /* 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)."));
+  add_cmd ("checkpoint", class_obscure, delete_fork_command, _("\
+Delete a fork/checkpoint (experimental)."),
+          &deletelist);
 
-  /* Detach-checkpoint command: release the process to run independantly, 
+  /* Detach checkpoint command: release the process to run independently, 
      and remove it from the fork list.  */
 
-  add_com ("detach-checkpoint", class_obscure, detach_fork_command, _("\
-Detach from a fork/checkpoint (experimental)."));
+  add_cmd ("checkpoint", class_obscure, detach_fork_command, _("\
+Detach from a fork/checkpoint (experimental)."),
+          &detachlist);
 
   /* Info checkpoints command: list all forks/checkpoints 
      currently under gdb's control.  */
@@ -673,8 +705,8 @@ Detach from a fork/checkpoint (experimental)."));
   /* 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_alias_cmd ("fork", "checkpoint", class_obscure, 1, &deletelist);
+  add_alias_cmd ("fork", "checkpoint", class_obscure, 1, &detachlist);
   add_info_alias ("forks", "checkpoints", 0);
 
   /* "fork <n>" (by analogy to "thread <n>").  */
This page took 0.027006 seconds and 4 git commands to generate.