2002-11-13 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / lin-lwp.c
index 22d9d74796c58a57350f60a91a6b284f3a5fbfb1..4b9d57d03203359b99b4d0bbd9f94db1fb983929 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
@@ -293,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
@@ -352,6 +353,14 @@ 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));
 
@@ -368,7 +377,7 @@ lin_lwp_attach_lwp (ptid_t ptid, int verbose)
 
       if (ptrace (PTRACE_ATTACH, GET_LWP (ptid), 0, 0) < 0)
        error ("Can't attach %s: %s", target_pid_to_str (ptid),
-              strerror (errno));
+              safe_strerror (errno));
 
       pid = waitpid (GET_LWP (ptid), &status, 0);
       if (pid == -1 && errno == ECHILD)
@@ -383,6 +392,16 @@ lin_lwp_attach_lwp (ptid_t ptid, int verbose)
 
       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;
+    }
 }
 
 static void
@@ -436,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;
@@ -453,7 +472,7 @@ detach_callback (struct lwp_info *lp, void *data)
       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);
     }
@@ -560,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);
@@ -951,11 +967,11 @@ child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
       clear_sigio_trap ();
       clear_sigint_trap ();
     }
-  while (pid == -1 && errno == EINTR);
+  while (pid == -1 && save_errno == EINTR);
 
   if (pid == -1)
     {
-      warning ("Child process unexpectedly missing: %s", strerror (errno));
+      warning ("Child process unexpectedly missing: %s", safe_strerror (errno));
 
       /* Claim it exited with unknown signal.  */
       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
@@ -1458,7 +1474,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);
@@ -1466,7 +1482,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
@@ -1510,10 +1527,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.027469 seconds and 4 git commands to generate.