2002-12-03 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / lin-lwp.c
index 61228a0385d3ebd631dbe5a562d16271fecf486d..444c221009ad52746382bef8461625d4f005c03b 100644 (file)
@@ -1,4 +1,4 @@
-/* Multi-threaded debugging support for Linux (LWP layer).
+/* Multi-threaded debugging support for GNU/Linux (LWP layer).
    Copyright 2000, 2001 Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -21,6 +21,7 @@
 #include "defs.h"
 
 #include "gdb_assert.h"
+#include "gdb_string.h"
 #include <errno.h>
 #include <signal.h>
 #include <sys/ptrace.h>
 static int debug_lin_lwp;
 extern const char *strsignal (int sig);
 
-/* On Linux there are no real LWP's.  The closest thing to LWP's are
-   processes sharing the same VM space.  A multi-threaded process is
-   basically a group of such processes.  However, such a grouping is
-   almost entirely a user-space issue; the kernel doesn't enforce such
-   a grouping at all (this might change in the future).  In general,
-   we'll rely on the threads library (i.e. the LinuxThreads library)
-   to provide such a grouping.
+/* On GNU/Linux there are no real LWP's.  The closest thing to LWP's
+   are processes sharing the same VM space.  A multi-threaded process
+   is basically a group of such processes.  However, such a grouping
+   is almost entirely a user-space issue; the kernel doesn't enforce
+   such a grouping at all (this might change in the future).  In
+   general, we'll rely on the threads library (i.e. the GNU/Linux
+   Threads library) to provide such a grouping.
 
    It is perfectly well possible to write a multi-threaded application
    without the assistance of a threads library, by using the clone
    system call directly.  This module should be able to give some
    rudimentary support for debugging such applications if developers
    specify the CLONE_PTRACE flag in the clone system call, and are
-   using Linux 2.4 or above.
+   using the Linux kernel 2.4 or above.
 
-   Note that there are some peculiarities in Linux that affect this
-   code:
+   Note that there are some peculiarities in GNU/Linux that affect
+   this code:
 
    - In general one should specify the __WCLONE flag to waitpid in
      order to make it report events for any of the cloned processes
      (and leave it out for the initial process).  However, if a cloned
      process has exited the exit status is only reported if the
-     __WCLONE flag is absent.  Linux 2.4 has a __WALL flag, but we
-     cannot use it since GDB must work on older systems too.
+     __WCLONE flag is absent.  Linux kernel 2.4 has a __WALL flag, but
+     we cannot use it since GDB must work on older systems too.
 
    - When a traced, cloned process exits and is waited for by the
      debugger, the kernel reassigns it to the original parent and
-     keeps it around as a "zombie".  Somehow, the LinuxThreads library
-     doesn't notice this, which leads to the "zombie problem": When
-     debugged a multi-threaded process that spawns a lot of threads
-     will run out of processes, even if the threads exit, because the
-     "zombies" stay around.  */
+     keeps it around as a "zombie".  Somehow, the GNU/Linux Threads
+     library doesn't notice this, which leads to the "zombie problem":
+     When debugged a multi-threaded process that spawns a lot of
+     threads will run out of processes, even if the threads exit,
+     because the "zombies" stay around.  */
 
 /* Structure describing a LWP.  */
 struct lwp_info
@@ -75,6 +76,11 @@ struct lwp_info
      and overall process id.  */
   ptid_t ptid;
 
+  /* Non-zero if this LWP is cloned.  In this context "cloned" means
+     that the LWP is reporting to its parent using a signal other than
+     SIGCHLD.  */
+  int cloned;
+
   /* Non-zero if we sent this LWP a SIGSTOP (but the LWP didn't report
      it back yet).  */
   int signalled;
@@ -115,8 +121,6 @@ static int threaded;
 #define is_lwp(ptid)           (GET_LWP (ptid) != 0)
 #define BUILD_LWP(lwp, pid)    ptid_build (pid, lwp, 0)
 
-#define is_cloned(pid) (GET_LWP (pid) != GET_PID (pid))
-
 /* If the last reported event was a SIGTRAP, this variable is set to
    the process id of the LWP/thread that got it.  */
 ptid_t trap_ptid;
@@ -290,7 +294,7 @@ iterate_over_lwps (int (*callback) (struct lwp_info *, void *), void *data)
 }
 \f
 
-/* Implementation of the PREPARE_TO_PROCEED hook for the Linux LWP
+/* Implementation of the PREPARE_TO_PROCEED hook for the GNU/Linux LWP
    layer.
 
    Note that this implementation is potentially redundant now that
@@ -349,21 +353,54 @@ lin_lwp_attach_lwp (ptid_t ptid, int verbose)
 
   gdb_assert (is_lwp (ptid));
 
+  /* Make sure SIGCHLD is blocked.  We don't want SIGCHLD events
+     to interrupt either the ptrace() or waitpid() calls below.  */
+  if (! sigismember (&blocked_mask, SIGCHLD))
+    {
+      sigaddset (&blocked_mask, SIGCHLD);
+      sigprocmask (SIG_BLOCK, &blocked_mask, NULL);
+    }
+
   if (verbose)
     printf_filtered ("[New %s]\n", target_pid_to_str (ptid));
 
-  /* We assume that we're already tracing the initial process.  */
-  if (is_cloned (ptid) && ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
-    error ("Can't attach %s: %s", target_pid_to_str (ptid), strerror (errno));
-
   lp = find_lwp_pid (ptid);
   if (lp == NULL)
     lp = add_lwp (ptid);
 
-  if (is_cloned (ptid))
+  /* We assume that we're already attached to any LWP that has an
+     id equal to the overall process id.  */
+  if (GET_LWP (ptid) != GET_PID (ptid))
     {
-      lp->signalled = 1;
-      stop_wait_callback (lp, NULL);
+      pid_t pid;
+      int status;
+
+      if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
+       error ("Can't attach %s: %s", target_pid_to_str (ptid),
+              safe_strerror (errno));
+
+      pid = waitpid (GET_LWP (ptid), &status, 0);
+      if (pid == -1 && errno == ECHILD)
+       {
+         /* Try again with __WCLONE to check cloned processes.  */
+         pid = waitpid (GET_LWP (ptid), &status, __WCLONE);
+         lp->cloned = 1;
+       }
+
+      gdb_assert (pid == GET_LWP (ptid)
+                 && WIFSTOPPED (status) && WSTOPSIG (status));
+
+      lp->stopped = 1;
+    }
+  else
+    {
+      /* We assume that the LWP representing the original process
+        is already stopped.  Mark it as stopped in the data structure
+        that the lin-lwp layer uses to keep track of threads.  Note
+        that this won't have already been done since the main thread
+        will have, we assume, been stopped by an attach from a
+        different layer.  */
+      lp->stopped = 1;
     }
 }
 
@@ -371,20 +408,33 @@ static void
 lin_lwp_attach (char *args, int from_tty)
 {
   struct lwp_info *lp;
+  pid_t pid;
+  int status;
 
   /* FIXME: We should probably accept a list of process id's, and
      attach all of them.  */
   child_ops.to_attach (args, from_tty);
 
   /* Add the initial process as the first LWP to the list.  */
-  lp = add_lwp (BUILD_LWP (PIDGET (inferior_ptid), PIDGET (inferior_ptid)));
+  lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid), GET_PID (inferior_ptid)));
 
   /* Make sure the initial process is stopped.  The user-level threads
      layer might want to poke around in the inferior, and that won't
      work if things haven't stabilized yet.  */
-  lp->signalled = 1;
-  stop_wait_callback (lp, NULL);
-  gdb_assert (lp->status == 0);
+  pid = waitpid (GET_PID (inferior_ptid), &status, 0);
+  if (pid == -1 && errno == ECHILD)
+    {
+      warning ("%s is a cloned process", target_pid_to_str (inferior_ptid));
+
+      /* Try again with __WCLONE to check cloned processes.  */
+      pid = waitpid (GET_PID (inferior_ptid), &status, __WCLONE);
+      lp->cloned = 1;
+    }
+
+  gdb_assert (pid == GET_PID (inferior_ptid)
+             && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
+
+  lp->stopped = 1;
 
   /* Fake the SIGSTOP that core GDB expects.  */
   lp->status = W_STOPCODE (SIGSTOP);
@@ -405,7 +455,7 @@ detach_callback (struct lwp_info *lp, void *data)
       if (ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0,
                  WSTOPSIG (lp->status)) < 0)
        error ("Can't continue %s: %s", target_pid_to_str (lp->ptid),
-              strerror (errno));
+              safe_strerror (errno));
 
       lp->stopped = 0;
       lp->signalled = 0;
@@ -415,12 +465,14 @@ detach_callback (struct lwp_info *lp, void *data)
       gdb_assert (lp->status == 0 || WIFSTOPPED (lp->status));
     }
 
-  if (is_cloned (lp->ptid))
+  /* We don't actually detach from the LWP that has an id equal to the
+     overall process id just yet.  */
+  if (GET_LWP (lp->ptid) != GET_PID (lp->ptid))
     {
       if (ptrace (PTRACE_DETACH, GET_LWP (lp->ptid), 0,
                  WSTOPSIG (lp->status)) < 0)
        error ("Can't detach %s: %s", target_pid_to_str (lp->ptid),
-              strerror (errno));
+              safe_strerror (errno));
 
       delete_lwp (lp->ptid);
     }
@@ -433,7 +485,7 @@ lin_lwp_detach (char *args, int from_tty)
 {
   iterate_over_lwps (detach_callback, NULL);
 
-  /* Only the initial (uncloned) process should be left right now.  */
+  /* Only the initial process should be left right now.  */
   gdb_assert (num_lwps == 1);
 
   trap_ptid = null_ptid;
@@ -527,11 +579,8 @@ lin_lwp_resume (ptid_t ptid, int step, enum target_signal signo)
   struct lwp_info *lp;
   int resume_all;
 
-  /* Apparently the interpretation of PID is dependent on STEP: If
-     STEP is non-zero, a specific PID means `step only this process
-     id'.  But if STEP is zero, then PID means `continue *all*
-     processes, but give the signal only to this one'.  */
-  resume_all = (PIDGET (ptid) == -1) || !step;
+  /* A specific PTID means `step only this process id'.  */
+  resume_all = (PIDGET (ptid) == -1);
 
   if (resume_all)
     iterate_over_lwps (resume_set_callback, NULL);
@@ -610,8 +659,7 @@ stop_wait_callback (struct lwp_info *lp, void *data)
 
       gdb_assert (lp->status == 0);
 
-      pid = waitpid (GET_LWP (lp->ptid), &status,
-                    is_cloned (lp->ptid) ? __WCLONE : 0);
+      pid = waitpid (GET_LWP (lp->ptid), &status, lp->cloned ? __WCLONE : 0);
       if (pid == -1 && errno == ECHILD)
        /* OK, the proccess has disappeared.  We'll catch the actual
           exit event in lin_lwp_wait.  */
@@ -888,6 +936,55 @@ resumed_callback (struct lwp_info *lp, void *data)
   return lp->resumed;
 }
 
+#ifdef CHILD_WAIT
+
+/* 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.  */
+
+/* Wait for child PTID to do something.  Return id of the child,
+   minus_one_ptid in case of error; store status into *OURSTATUS.  */
+
+ptid_t
+child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
+{
+  int save_errno;
+  int status;
+  pid_t pid;
+
+  do
+    {
+      set_sigint_trap ();      /* Causes SIGINT to be passed on to the
+                                  attached process.  */
+      set_sigio_trap ();
+
+      pid = waitpid (GET_PID (ptid), &status, 0);
+      if (pid == -1 && errno == ECHILD)
+       /* Try again with __WCLONE to check cloned processes.  */
+       pid = waitpid (GET_PID (ptid), &status, __WCLONE);
+      save_errno = errno;
+
+      clear_sigio_trap ();
+      clear_sigint_trap ();
+    }
+  while (pid == -1 && save_errno == EINTR);
+
+  if (pid == -1)
+    {
+      warning ("Child process unexpectedly missing: %s", safe_strerror (errno));
+
+      /* Claim it exited with unknown signal.  */
+      ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
+      ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
+      return minus_one_ptid;
+    }
+
+  store_waitstatus (ourstatus, status);
+  return pid_to_ptid (pid);
+}
+
+#endif
+
 static ptid_t
 lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
 {
@@ -908,8 +1005,9 @@ lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
 
  retry:
 
-  /* Make sure there is at least one thread that has been resumed.  */
-  gdb_assert (iterate_over_lwps (resumed_callback, NULL));
+  /* Make sure there is at least one LWP that has been resumed, at
+     least if there are any LWPs at all.  */
+  gdb_assert (num_lwps == 0 || iterate_over_lwps (resumed_callback, NULL));
 
   /* First check if there is a LWP with a wait status pending.  */
   if (pid == -1)
@@ -953,7 +1051,7 @@ lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
       /* If we have to wait, take into account whether PID is a cloned
          process or not.  And we have to convert it to something that
          the layer beneath us can understand.  */
-      options = is_cloned (lp->ptid) ? __WCLONE : 0;
+      options = lp->cloned ? __WCLONE : 0;
       pid = GET_LWP (ptid);
     }
 
@@ -996,6 +1094,9 @@ lin_lwp_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
          if (! lp)
            {
              lp = add_lwp (BUILD_LWP (lwpid, GET_PID (inferior_ptid)));
+             if (options & __WCLONE)
+               lp->cloned = 1;
+
              if (threaded)
                {
                  gdb_assert (WIFSTOPPED (status)
@@ -1188,7 +1289,7 @@ kill_wait_callback (struct lwp_info *lp, void *data)
   /* For cloned processes we must check both with __WCLONE and
      without, since the exit status of a cloned process isn't reported
      with __WCLONE.  */
-  if (is_cloned (lp->ptid))
+  if (lp->cloned)
     {
       do
        {
@@ -1242,32 +1343,6 @@ lin_lwp_mourn_inferior (void)
   child_ops.to_mourn_inferior ();
 }
 
-static void
-lin_lwp_fetch_registers (int regno)
-{
-  struct cleanup *old_chain = save_inferior_ptid ();
-
-  if (is_lwp (inferior_ptid))
-    inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
-
-  fetch_inferior_registers (regno);
-
-  do_cleanups (old_chain);
-}
-
-static void
-lin_lwp_store_registers (int regno)
-{
-  struct cleanup *old_chain = save_inferior_ptid ();
-
-  if (is_lwp (inferior_ptid))
-    inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
-
-  store_inferior_registers (regno);
-
-  do_cleanups (old_chain);
-}
-
 static int
 lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
                     struct mem_attrib *attrib,
@@ -1279,7 +1354,9 @@ lin_lwp_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
   if (is_lwp (inferior_ptid))
     inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
 
-  xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
+  xfer = linux_proc_xfer_memory (memaddr, myaddr, len, write, attrib, target);
+  if (xfer == 0)
+    xfer = child_xfer_memory (memaddr, myaddr, len, write, attrib, target);
 
   do_cleanups (old_chain);
   return xfer;
@@ -1325,8 +1402,10 @@ init_lin_lwp_ops (void)
   lin_lwp_ops.to_detach = lin_lwp_detach;
   lin_lwp_ops.to_resume = lin_lwp_resume;
   lin_lwp_ops.to_wait = lin_lwp_wait;
-  lin_lwp_ops.to_fetch_registers = lin_lwp_fetch_registers;
-  lin_lwp_ops.to_store_registers = lin_lwp_store_registers;
+  /* fetch_inferior_registers and store_inferior_registers will
+     honor the LWP id, so we can use them directly.  */
+  lin_lwp_ops.to_fetch_registers = fetch_inferior_registers;
+  lin_lwp_ops.to_store_registers = store_inferior_registers;
   lin_lwp_ops.to_xfer_memory = lin_lwp_xfer_memory;
   lin_lwp_ops.to_kill = lin_lwp_kill;
   lin_lwp_ops.to_create_inferior = lin_lwp_create_inferior;
@@ -1373,7 +1452,7 @@ _initialize_lin_lwp (void)
 
   add_show_from_set (add_set_cmd ("lin-lwp", no_class, var_zinteger,
                                  (char *) &debug_lin_lwp, 
-                                 "Set debugging of linux lwp module.\n\
+                                 "Set debugging of GNU/Linux lwp module.\n\
 Enables printf debugging output.\n",
                                      &setdebuglist),
                     &showdebuglist);
@@ -1381,7 +1460,8 @@ Enables printf debugging output.\n",
 \f
 
 /* FIXME: kettenis/2000-08-26: The stuff on this page is specific to
-   the LinuxThreads library and therefore doesn't really belong here.  */
+   the GNU/Linux Threads library and therefore doesn't really belong
+   here.  */
 
 /* Read variable NAME in the target and return its value if found.
    Otherwise return zero.  It is assumed that the type of the variable
@@ -1425,10 +1505,11 @@ lin_thread_get_thread_signals (sigset_t *set)
   sigaddset (set, restart);
   sigaddset (set, cancel);
 
-  /* The LinuxThreads library makes terminating threads send a special
-     "cancel" signal instead of SIGCHLD.  Make sure we catch those (to
-     prevent them from terminating GDB itself, which is likely to be
-     their default action) and treat them the same way as SIGCHLD.  */
+  /* The GNU/Linux Threads library makes terminating threads send a
+     special "cancel" signal instead of SIGCHLD.  Make sure we catch
+     those (to prevent them from terminating GDB itself, which is
+     likely to be their default action) and treat them the same way as
+     SIGCHLD.  */
 
   action.sa_handler = sigchld_handler;
   sigemptyset (&action.sa_mask);
This page took 0.030691 seconds and 4 git commands to generate.