Fix race exposed by gdb.threads/killed.exp
[deliverable/binutils-gdb.git] / gdb / linux-nat.c
index 5f2c176b044799f9f734b51b53b40e9c765e63aa..8b620417a40d0b886ab56bf11daba98fdb7bb3e7 100644 (file)
@@ -32,6 +32,7 @@
 #include "linux-nat.h"
 #include "nat/linux-ptrace.h"
 #include "nat/linux-procfs.h"
+#include "nat/linux-personality.h"
 #include "linux-fork.h"
 #include "gdbthread.h"
 #include "gdbcmd.h"
 #define SPUFS_MAGIC 0x23c9b64e
 #endif
 
-#ifdef HAVE_PERSONALITY
-# include <sys/personality.h>
-# if !HAVE_DECL_ADDR_NO_RANDOMIZE
-#  define ADDR_NO_RANDOMIZE 0x0040000
-# endif
-#endif /* HAVE_PERSONALITY */
-
 /* This comment documents high-level logic of this file.
 
 Waiting for events in sync mode
@@ -225,6 +219,9 @@ struct simple_pid_list *stopped_pids;
    event loop.  */
 static int linux_nat_event_pipe[2] = { -1, -1 };
 
+/* True if we're currently in async mode.  */
+#define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
+
 /* Flush the event pipe.  */
 
 static void
@@ -267,6 +264,7 @@ async_file_mark (void)
 static int kill_lwp (int lwpid, int signo);
 
 static int stop_callback (struct lwp_info *lp, void *data);
+static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
 
 static void block_child_signals (sigset_t *prev_mask);
 static void restore_child_signals_mask (sigset_t *prev_mask);
@@ -389,20 +387,19 @@ linux_child_follow_fork (struct target_ops *ops, int follow_child,
       int status = W_STOPCODE (0);
       struct cleanup *old_chain;
       int has_vforked;
+      ptid_t parent_ptid, child_ptid;
       int parent_pid, child_pid;
 
       has_vforked = (inferior_thread ()->pending_follow.kind
                     == TARGET_WAITKIND_VFORKED);
-      parent_pid = ptid_get_lwp (inferior_ptid);
-      if (parent_pid == 0)
-       parent_pid = ptid_get_pid (inferior_ptid);
-      child_pid
-       = ptid_get_pid (inferior_thread ()->pending_follow.value.related_pid);
-
+      parent_ptid = inferior_ptid;
+      child_ptid = inferior_thread ()->pending_follow.value.related_pid;
+      parent_pid = ptid_get_lwp (parent_ptid);
+      child_pid = ptid_get_lwp (child_ptid);
 
       /* We're already attached to the parent, by default.  */
       old_chain = save_inferior_ptid ();
-      inferior_ptid = ptid_build (child_pid, child_pid, 0);
+      inferior_ptid = child_ptid;
       child_lp = add_lwp (inferior_ptid);
       child_lp->stopped = 1;
       child_lp->last_resume_kind = resume_stop;
@@ -459,7 +456,7 @@ linux_child_follow_fork (struct target_ops *ops, int follow_child,
        {
          struct lwp_info *parent_lp;
 
-         parent_lp = find_lwp_pid (pid_to_ptid (parent_pid));
+         parent_lp = find_lwp_pid (parent_ptid);
          gdb_assert (linux_supports_tracefork () >= 0);
 
          if (linux_supports_tracevforkdone ())
@@ -523,7 +520,7 @@ linux_child_follow_fork (struct target_ops *ops, int follow_child,
 
              /* If we're in async mode, need to tell the event loop
                 there's something here to process.  */
-             if (target_can_async_p ())
+             if (target_is_async_p ())
                async_file_mark ();
            }
        }
@@ -694,6 +691,7 @@ linux_nat_pass_signals (struct target_ops *self,
 static int stop_wait_callback (struct lwp_info *lp, void *data);
 static int linux_thread_alive (ptid_t ptid);
 static char *linux_child_pid_to_exec_file (struct target_ops *self, int pid);
+static int resume_stopped_resumed_lwps (struct lwp_info *lp, void *data);
 
 \f
 
@@ -996,13 +994,12 @@ lin_lwp_attach_lwp (ptid_t ptid)
   lp = find_lwp_pid (ptid);
   lwpid = ptid_get_lwp (ptid);
 
-  /* We assume that we're already attached to any LWP that has an id
-     equal to the overall process id, and to any LWP that is already
+  /* We assume that we're already attached to any LWP that is already
      in our list of LWPs.  If we're not seeing exit events from threads
      and we've had PID wraparound since we last tried to stop all threads,
      this assumption might be wrong; fortunately, this is very unlikely
      to happen.  */
-  if (lwpid != ptid_get_pid (ptid) && lp == NULL)
+  if (lp == NULL)
     {
       int status, cloned = 0, signalled = 0;
 
@@ -1020,23 +1017,50 @@ lin_lwp_attach_lwp (ptid_t ptid)
                  /* We've already seen this thread stop, but we
                     haven't seen the PTRACE_EVENT_CLONE extended
                     event yet.  */
-                 return 0;
+                 if (debug_linux_nat)
+                   fprintf_unfiltered (gdb_stdlog,
+                                       "LLAL: attach failed, but already seen "
+                                       "this thread %s stop\n",
+                                       target_pid_to_str (ptid));
+                 return 1;
                }
              else
                {
                  int new_pid;
                  int status;
 
-                 /* See if we've got a stop for this new child
-                    pending.  If so, we're already attached.  */
+                 if (debug_linux_nat)
+                   fprintf_unfiltered (gdb_stdlog,
+                                       "LLAL: attach failed, and haven't seen "
+                                       "this thread %s stop yet\n",
+                                       target_pid_to_str (ptid));
+
+                 /* We may or may not be attached to the LWP already.
+                    Try waitpid on it.  If that errors, we're not
+                    attached to the LWP yet.  Otherwise, we're
+                    already attached.  */
                  gdb_assert (lwpid > 0);
                  new_pid = my_waitpid (lwpid, &status, WNOHANG);
                  if (new_pid == -1 && errno == ECHILD)
                    new_pid = my_waitpid (lwpid, &status, __WCLONE | WNOHANG);
                  if (new_pid != -1)
                    {
-                     if (WIFSTOPPED (status))
-                       add_to_pid_list (&stopped_pids, lwpid, status);
+                     if (new_pid == 0)
+                       {
+                         /* The child hasn't stopped for its initial
+                            SIGSTOP stop yet.  */
+                         if (debug_linux_nat)
+                           fprintf_unfiltered (gdb_stdlog,
+                                               "LLAL: child hasn't "
+                                               "stopped yet\n");
+                       }
+                     else if (WIFSTOPPED (status))
+                       {
+                         if (debug_linux_nat)
+                           fprintf_unfiltered (gdb_stdlog,
+                                               "LLAL: adding to stopped_pids\n");
+                         add_to_pid_list (&stopped_pids, lwpid, status);
+                       }
                      return 1;
                    }
                }
@@ -1063,6 +1087,7 @@ lin_lwp_attach_lwp (ptid_t ptid)
 
       lp = add_lwp (ptid);
       lp->stopped = 1;
+      lp->last_resume_kind = resume_stop;
       lp->cloned = cloned;
       lp->signalled = signalled;
       if (WSTOPSIG (status) != SIGSTOP)
@@ -1081,20 +1106,7 @@ lin_lwp_attach_lwp (ptid_t ptid)
                              status_to_str (status));
        }
     }
-  else
-    {
-      /* We assume that the LWP representing the original process is
-         already stopped.  Mark it as stopped in the data structure
-         that the GNU/linux ptrace 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.  */
-      if (lp == NULL)
-       lp = add_lwp (ptid);
-      lp->stopped = 1;
-    }
 
-  lp->last_resume_kind = resume_stop;
   return 0;
 }
 
@@ -1103,45 +1115,18 @@ linux_nat_create_inferior (struct target_ops *ops,
                           char *exec_file, char *allargs, char **env,
                           int from_tty)
 {
-#ifdef HAVE_PERSONALITY
-  int personality_orig = 0, personality_set = 0;
-#endif /* HAVE_PERSONALITY */
+  struct cleanup *restore_personality
+    = maybe_disable_address_space_randomization (disable_randomization);
 
   /* The fork_child mechanism is synchronous and calls target_wait, so
      we have to mask the async mode.  */
 
-#ifdef HAVE_PERSONALITY
-  if (disable_randomization)
-    {
-      errno = 0;
-      personality_orig = personality (0xffffffff);
-      if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
-       {
-         personality_set = 1;
-         personality (personality_orig | ADDR_NO_RANDOMIZE);
-       }
-      if (errno != 0 || (personality_set
-                        && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
-       warning (_("Error disabling address space randomization: %s"),
-                safe_strerror (errno));
-    }
-#endif /* HAVE_PERSONALITY */
-
   /* Make sure we report all signals during startup.  */
   linux_nat_pass_signals (ops, 0, NULL);
 
   linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
 
-#ifdef HAVE_PERSONALITY
-  if (personality_set)
-    {
-      errno = 0;
-      personality (personality_orig);
-      if (errno != 0)
-       warning (_("Error restoring address space randomization: %s"),
-                safe_strerror (errno));
-    }
-#endif /* HAVE_PERSONALITY */
+  do_cleanups (restore_personality);
 }
 
 /* Callback for linux_proc_attach_tgid_threads.  Attach to PTID if not
@@ -1181,7 +1166,7 @@ attach_proc_task_lwp_callback (ptid_t ptid)
            }
          else
            {
-             warning (_("Cannot attach to lwp %d: %s\n"),
+             warning (_("Cannot attach to lwp %d: %s"),
                       lwpid,
                       linux_ptrace_attach_fail_reason_string (ptid,
                                                               err));
@@ -1217,16 +1202,15 @@ linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
   struct lwp_info *lp;
   int status;
   ptid_t ptid;
-  volatile struct gdb_exception ex;
 
   /* Make sure we report all signals during attach.  */
   linux_nat_pass_signals (ops, 0, NULL);
 
-  TRY_CATCH (ex, RETURN_MASK_ERROR)
+  TRY
     {
       linux_ops->to_attach (ops, args, from_tty);
     }
-  if (ex.reason < 0)
+  CATCH (ex, RETURN_MASK_ERROR)
     {
       pid_t pid = parse_pid_to_attach (args);
       struct buffer buffer;
@@ -1247,6 +1231,7 @@ linux_nat_attach (struct target_ops *ops, const char *args, int from_tty)
       else
        throw_error (ex.error, "%s", message);
     }
+  END_CATCH
 
   /* The ptrace base target adds the main thread with (pid,0,0)
      format.  Decorate it with lwp info.  */
@@ -1518,10 +1503,9 @@ linux_nat_detach (struct target_ops *ops, const char *args, int from_tty)
    single-step it.  If SIGNAL is nonzero, give it that signal.  */
 
 static void
-linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
+linux_resume_one_lwp_throw (struct lwp_info *lp, int step,
+                           enum gdb_signal signo)
 {
-  ptid_t ptid;
-
   lp->step = step;
 
   /* stop_pc doubles as the PC the LWP had when it was last resumed.
@@ -1538,14 +1522,69 @@ linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
 
   if (linux_nat_prepare_to_resume != NULL)
     linux_nat_prepare_to_resume (lp);
-  /* Convert to something the lower layer understands.  */
-  ptid = pid_to_ptid (ptid_get_lwp (lp->ptid));
-  linux_ops->to_resume (linux_ops, ptid, step, signo);
-  lp->stop_reason = LWP_STOPPED_BY_NO_REASON;
+  linux_ops->to_resume (linux_ops, lp->ptid, step, signo);
+
+  /* Successfully resumed.  Clear state that no longer makes sense,
+     and mark the LWP as running.  Must not do this before resuming
+     otherwise if that fails other code will be confused.  E.g., we'd
+     later try to stop the LWP and hang forever waiting for a stop
+     status.  Note that we must not throw after this is cleared,
+     otherwise handle_zombie_lwp_error would get confused.  */
   lp->stopped = 0;
+  lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
   registers_changed_ptid (lp->ptid);
 }
 
+/* Called when we try to resume a stopped LWP and that errors out.  If
+   the LWP is no longer in ptrace-stopped state (meaning it's zombie,
+   or about to become), discard the error, clear any pending status
+   the LWP may have, and return true (we'll collect the exit status
+   soon enough).  Otherwise, return false.  */
+
+static int
+check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
+{
+  /* If we get an error after resuming the LWP successfully, we'd
+     confuse !T state for the LWP being gone.  */
+  gdb_assert (lp->stopped);
+
+  /* We can't just check whether the LWP is in 'Z (Zombie)' state,
+     because even if ptrace failed with ESRCH, the tracee may be "not
+     yet fully dead", but already refusing ptrace requests.  In that
+     case the tracee has 'R (Running)' state for a little bit
+     (observed in Linux 3.18).  See also the note on ESRCH in the
+     ptrace(2) man page.  Instead, check whether the LWP has any state
+     other than ptrace-stopped.  */
+
+  /* Don't assume anything if /proc/PID/status can't be read.  */
+  if (linux_proc_pid_is_trace_stopped_nowarn (ptid_get_lwp (lp->ptid)) == 0)
+    {
+      lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
+      lp->status = 0;
+      lp->waitstatus.kind = TARGET_WAITKIND_IGNORE;
+      return 1;
+    }
+  return 0;
+}
+
+/* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
+   disappears while we try to resume it.  */
+
+static void
+linux_resume_one_lwp (struct lwp_info *lp, int step, enum gdb_signal signo)
+{
+  TRY
+    {
+      linux_resume_one_lwp_throw (lp, step, signo);
+    }
+  CATCH (ex, RETURN_MASK_ERROR)
+    {
+      if (!check_ptrace_stopped_lwp_gone (lp))
+       throw_exception (ex);
+    }
+  END_CATCH
+}
+
 /* Resume LP.  */
 
 static void
@@ -2018,34 +2057,24 @@ linux_handle_extended_wait (struct lwp_info *lp, int status,
                status = 0;
            }
 
-         if (non_stop)
+         /* If the thread_db layer is active, let it record the user
+            level thread id and status, and add the thread to GDB's
+            list.  */
+         if (!thread_db_notice_clone (lp->ptid, new_lp->ptid))
            {
-             /* Add the new thread to GDB's lists as soon as possible
-                so that:
-
-                1) the frontend doesn't have to wait for a stop to
-                display them, and,
-
-                2) we tag it with the correct running state.  */
-
-             /* If the thread_db layer is active, let it know about
-                this new thread, and add it to GDB's list.  */
-             if (!thread_db_attach_lwp (new_lp->ptid))
-               {
-                 /* We're not using thread_db.  Add it to GDB's
-                    list.  */
-                 target_post_attach (ptid_get_lwp (new_lp->ptid));
-                 add_thread (new_lp->ptid);
-               }
+             /* The process is not using thread_db.  Add the LWP to
+                GDB's list.  */
+             target_post_attach (ptid_get_lwp (new_lp->ptid));
+             add_thread (new_lp->ptid);
+           }
 
-             if (!stopping)
-               {
-                 set_running (new_lp->ptid, 1);
-                 set_executing (new_lp->ptid, 1);
-                 /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
-                    resume_stop.  */
-                 new_lp->last_resume_kind = resume_continue;
-               }
+         if (!stopping)
+           {
+             set_running (new_lp->ptid, 1);
+             set_executing (new_lp->ptid, 1);
+             /* thread_db_attach_lwp -> lin_lwp_attach_lwp forced
+                resume_stop.  */
+             new_lp->last_resume_kind = resume_continue;
            }
 
          if (status != 0)
@@ -2063,28 +2092,7 @@ linux_handle_extended_wait (struct lwp_info *lp, int status,
              new_lp->status = status;
            }
 
-         /* Note the need to use the low target ops to resume, to
-            handle resuming with PT_SYSCALL if we have syscall
-            catchpoints.  */
-         if (!stopping)
-           {
-             new_lp->resumed = 1;
-
-             if (status == 0)
-               {
-                 gdb_assert (new_lp->last_resume_kind == resume_continue);
-                 if (debug_linux_nat)
-                   fprintf_unfiltered (gdb_stdlog,
-                                       "LHEW: resuming new LWP %ld\n",
-                                       ptid_get_lwp (new_lp->ptid));
-                 linux_resume_one_lwp (new_lp, 0, GDB_SIGNAL_0);
-               }
-           }
-
-         if (debug_linux_nat)
-           fprintf_unfiltered (gdb_stdlog,
-                               "LHEW: resuming parent LWP %d\n", pid);
-         linux_resume_one_lwp (lp, 0, GDB_SIGNAL_0);
+         new_lp->resumed = !stopping;
          return 1;
        }
 
@@ -2126,9 +2134,8 @@ linux_handle_extended_wait (struct lwp_info *lp, int status,
       if (debug_linux_nat)
        fprintf_unfiltered (gdb_stdlog,
                            "LHEW: Got PTRACE_EVENT_VFORK_DONE "
-                           "from LWP %ld: resuming\n",
+                           "from LWP %ld: ignoring\n",
                            ptid_get_lwp (lp->ptid));
-      ptrace (PTRACE_CONT, ptid_get_lwp (lp->ptid), 0, 0);
       return 1;
     }
 
@@ -2275,8 +2282,8 @@ wait_lwp (struct lwp_info *lp)
        fprintf_unfiltered (gdb_stdlog,
                            "WL: Handling extended status 0x%06x\n",
                            status);
-      if (linux_handle_extended_wait (lp, status, 1))
-       return wait_lwp (lp);
+      linux_handle_extended_wait (lp, status, 1);
+      return 0;
     }
 
   return status;
@@ -2322,6 +2329,28 @@ linux_stop_lwp (struct lwp_info *lwp)
   stop_callback (lwp, NULL);
 }
 
+/* See linux-nat.h  */
+
+void
+linux_stop_and_wait_all_lwps (void)
+{
+  /* Stop all LWP's ...  */
+  iterate_over_lwps (minus_one_ptid, stop_callback, NULL);
+
+  /* ... and wait until all of them have reported back that
+     they're no longer running.  */
+  iterate_over_lwps (minus_one_ptid, stop_wait_callback, NULL);
+}
+
+/* See linux-nat.h  */
+
+void
+linux_unstop_all_lwps (void)
+{
+  iterate_over_lwps (minus_one_ptid,
+                    resume_stopped_resumed_lwps, &minus_one_ptid);
+}
+
 /* Return non-zero if LWP PID has a pending SIGINT.  */
 
 static int
@@ -2404,7 +2433,7 @@ check_stopped_by_watchpoint (struct lwp_info *lp)
 
   if (linux_ops->to_stopped_by_watchpoint (linux_ops))
     {
-      lp->stop_reason = LWP_STOPPED_BY_WATCHPOINT;
+      lp->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
 
       if (linux_ops->to_stopped_data_address != NULL)
        lp->stopped_data_address_p =
@@ -2416,7 +2445,7 @@ check_stopped_by_watchpoint (struct lwp_info *lp)
 
   do_cleanups (old_chain);
 
-  return lp->stop_reason == LWP_STOPPED_BY_WATCHPOINT;
+  return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
 }
 
 /* Called when the LWP stopped for a trap that could be explained by a
@@ -2425,14 +2454,22 @@ check_stopped_by_watchpoint (struct lwp_info *lp)
 static void
 save_sigtrap (struct lwp_info *lp)
 {
-  gdb_assert (lp->stop_reason == LWP_STOPPED_BY_NO_REASON);
+  gdb_assert (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON);
   gdb_assert (lp->status != 0);
 
-  if (check_stopped_by_watchpoint (lp))
-    return;
-
+  /* Check first if this was a SW/HW breakpoint before checking
+     watchpoints, because at least s390 can't tell the data address of
+     hardware watchpoint hits, and the kernel returns
+     stopped-by-watchpoint as long as there's a watchpoint set.  */
   if (linux_nat_status_is_event (lp->status))
     check_stopped_by_breakpoint (lp);
+
+  /* Note that TRAP_HWBKPT can indicate either a hardware breakpoint
+     or hardware watchpoint.  Check which is which if we got
+     TARGET_STOPPED_BY_HW_BREAKPOINT.  */
+  if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON
+      || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
+    check_stopped_by_watchpoint (lp);
 }
 
 /* Returns true if the LWP had stopped for a watchpoint.  */
@@ -2444,7 +2481,7 @@ linux_nat_stopped_by_watchpoint (struct target_ops *ops)
 
   gdb_assert (lp != NULL);
 
-  return lp->stop_reason == LWP_STOPPED_BY_WATCHPOINT;
+  return lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
 }
 
 static int
@@ -2564,16 +2601,17 @@ status_callback (struct lwp_info *lp, void *data)
   if (!lp->resumed)
     return 0;
 
-  if (lp->stop_reason == LWP_STOPPED_BY_SW_BREAKPOINT
-      || lp->stop_reason == LWP_STOPPED_BY_HW_BREAKPOINT)
+  if (!lwp_status_pending_p (lp))
+    return 0;
+
+  if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
+      || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
     {
       struct regcache *regcache = get_thread_regcache (lp->ptid);
       struct gdbarch *gdbarch = get_regcache_arch (regcache);
       CORE_ADDR pc;
       int discard = 0;
 
-      gdb_assert (lp->status != 0);
-
       pc = regcache_read_pc (regcache);
 
       if (pc != lp->stop_pc)
@@ -2586,6 +2624,8 @@ status_callback (struct lwp_info *lp, void *data)
                                paddress (target_gdbarch (), pc));
          discard = 1;
        }
+
+#if !USE_SIGTRAP_SIGINFO
       else if (!breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
        {
          if (debug_linux_nat)
@@ -2596,6 +2636,7 @@ status_callback (struct lwp_info *lp, void *data)
 
          discard = 1;
        }
+#endif
 
       if (discard)
        {
@@ -2608,10 +2649,9 @@ status_callback (struct lwp_info *lp, void *data)
          linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
          return 0;
        }
-      return 1;
     }
 
-  return lwp_status_pending_p (lp);
+  return 1;
 }
 
 /* Return non-zero if LP isn't stopped.  */
@@ -2662,7 +2702,7 @@ lwp_status_pending_p (struct lwp_info *lp)
   return lp->status != 0 || lp->waitstatus.kind != TARGET_WAITKIND_IGNORE;
 }
 
-/* Select the Nth LWP that has had a SIGTRAP event.  */
+/* Select the Nth LWP that has had an event.  */
 
 static int
 select_event_lwp_callback (struct lwp_info *lp, void *data)
@@ -2698,10 +2738,49 @@ check_stopped_by_breakpoint (struct lwp_info *lp)
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   CORE_ADDR pc;
   CORE_ADDR sw_bp_pc;
+#if USE_SIGTRAP_SIGINFO
+  siginfo_t siginfo;
+#endif
 
   pc = regcache_read_pc (regcache);
-  sw_bp_pc = pc - target_decr_pc_after_break (gdbarch);
+  sw_bp_pc = pc - gdbarch_decr_pc_after_break (gdbarch);
+
+#if USE_SIGTRAP_SIGINFO
+  if (linux_nat_get_siginfo (lp->ptid, &siginfo))
+    {
+      if (siginfo.si_signo == SIGTRAP)
+       {
+         if (siginfo.si_code == GDB_ARCH_TRAP_BRKPT)
+           {
+             if (debug_linux_nat)
+               fprintf_unfiltered (gdb_stdlog,
+                                   "CSBB: Push back software "
+                                   "breakpoint for %s\n",
+                                   target_pid_to_str (lp->ptid));
 
+             /* Back up the PC if necessary.  */
+             if (pc != sw_bp_pc)
+               regcache_write_pc (regcache, sw_bp_pc);
+
+             lp->stop_pc = sw_bp_pc;
+             lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
+             return 1;
+           }
+         else if (siginfo.si_code == TRAP_HWBKPT)
+           {
+             if (debug_linux_nat)
+               fprintf_unfiltered (gdb_stdlog,
+                                   "CSBB: Push back hardware "
+                                   "breakpoint/watchpoint for %s\n",
+                                   target_pid_to_str (lp->ptid));
+
+             lp->stop_pc = pc;
+             lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
+             return 1;
+           }
+       }
+    }
+#else
   if ((!lp->step || lp->stop_pc == sw_bp_pc)
       && software_breakpoint_inserted_here_p (get_regcache_aspace (regcache),
                                              sw_bp_pc))
@@ -2718,7 +2797,7 @@ check_stopped_by_breakpoint (struct lwp_info *lp)
        regcache_write_pc (regcache, sw_bp_pc);
 
       lp->stop_pc = sw_bp_pc;
-      lp->stop_reason = LWP_STOPPED_BY_SW_BREAKPOINT;
+      lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
       return 1;
     }
 
@@ -2730,13 +2809,56 @@ check_stopped_by_breakpoint (struct lwp_info *lp)
                            target_pid_to_str (lp->ptid));
 
       lp->stop_pc = pc;
-      lp->stop_reason = LWP_STOPPED_BY_HW_BREAKPOINT;
+      lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
       return 1;
     }
+#endif
 
   return 0;
 }
 
+
+/* Returns true if the LWP had stopped for a software breakpoint.  */
+
+static int
+linux_nat_stopped_by_sw_breakpoint (struct target_ops *ops)
+{
+  struct lwp_info *lp = find_lwp_pid (inferior_ptid);
+
+  gdb_assert (lp != NULL);
+
+  return lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
+}
+
+/* Implement the supports_stopped_by_sw_breakpoint method.  */
+
+static int
+linux_nat_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
+{
+  return USE_SIGTRAP_SIGINFO;
+}
+
+/* Returns true if the LWP had stopped for a hardware
+   breakpoint/watchpoint.  */
+
+static int
+linux_nat_stopped_by_hw_breakpoint (struct target_ops *ops)
+{
+  struct lwp_info *lp = find_lwp_pid (inferior_ptid);
+
+  gdb_assert (lp != NULL);
+
+  return lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
+}
+
+/* Implement the supports_stopped_by_hw_breakpoint method.  */
+
+static int
+linux_nat_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
+{
+  return USE_SIGTRAP_SIGINFO;
+}
+
 /* Select one LWP out of those that have events pending.  */
 
 static void
@@ -2777,6 +2899,7 @@ select_event_lwp (ptid_t filter, struct lwp_info **orig_lp, int *status)
 
       /* First see how many events we have.  */
       iterate_over_lwps (filter, count_events_callback, &num_events);
+      gdb_assert (num_events > 0);
 
       /* Now randomly pick a LWP out of those that have had
         events.  */
@@ -2906,6 +3029,10 @@ linux_nat_filter_event (int lwpid, int status)
 
   if (WIFSTOPPED (status) && !lp)
     {
+      if (debug_linux_nat)
+       fprintf_unfiltered (gdb_stdlog,
+                           "LHEW: saving LWP %ld status %s in stopped_pids list\n",
+                           (long) lwpid, status_to_str (status));
       add_to_pid_list (&stopped_pids, lwpid, status);
       return NULL;
     }
@@ -3121,9 +3248,11 @@ linux_nat_filter_event (int lwpid, int status)
        }
 
       /* When using hardware single-step, we need to report every signal.
-        Otherwise, signals in pass_mask may be short-circuited.  */
+        Otherwise, signals in pass_mask may be short-circuited
+        except signals that might be caused by a breakpoint.  */
       if (!lp->step
-         && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status)))
+         && WSTOPSIG (status) && sigismember (&pass_mask, WSTOPSIG (status))
+         && !linux_wstatus_maybe_breakpoint (status))
        {
          linux_resume_one_lwp (lp, lp->step, signo);
          if (debug_linux_nat)
@@ -3252,7 +3381,7 @@ linux_nat_wait_1 (struct target_ops *ops,
                            target_pid_to_str (lp->ptid));
     }
 
-  if (!target_can_async_p ())
+  if (!target_is_async_p ())
     {
       /* Causes SIGINT to be passed on to the attached process.  */
       set_sigint_trap ();
@@ -3304,8 +3433,13 @@ linux_nat_wait_1 (struct target_ops *ops,
          continue;
        }
 
-      /* Now that we've pulled all events out of the kernel, check if
-        there's any LWP with a status to report to the core.  */
+      /* Now that we've pulled all events out of the kernel, resume
+        LWPs that don't have an interesting event to report.  */
+      iterate_over_lwps (minus_one_ptid,
+                        resume_stopped_resumed_lwps, &minus_one_ptid);
+
+      /* ... and find an LWP with a status to report to the core, if
+        any.  */
       lp = iterate_over_lwps (ptid, status_callback, NULL);
       if (lp != NULL)
        break;
@@ -3323,7 +3457,7 @@ linux_nat_wait_1 (struct target_ops *ops,
 
          ourstatus->kind = TARGET_WAITKIND_NO_RESUMED;
 
-         if (!target_can_async_p ())
+         if (!target_is_async_p ())
            clear_sigint_trap ();
 
          restore_child_signals_mask (&prev_mask);
@@ -3351,7 +3485,7 @@ linux_nat_wait_1 (struct target_ops *ops,
       sigsuspend (&suspend_mask);
     }
 
-  if (!target_can_async_p ())
+  if (!target_is_async_p ())
     clear_sigint_trap ();
 
   gdb_assert (lp);
@@ -3378,12 +3512,14 @@ linux_nat_wait_1 (struct target_ops *ops,
   gdb_assert (lp != NULL);
 
   /* Now that we've selected our final event LWP, un-adjust its PC if
-     it was a software breakpoint.  */
-  if (lp->stop_reason == LWP_STOPPED_BY_SW_BREAKPOINT)
+     it was a software breakpoint, and we can't reliably support the
+     "stopped by software breakpoint" stop reason.  */
+  if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
+      && !USE_SIGTRAP_SIGINFO)
     {
       struct regcache *regcache = get_thread_regcache (lp->ptid);
       struct gdbarch *gdbarch = get_regcache_arch (regcache);
-      int decr_pc = target_decr_pc_after_break (gdbarch);
+      int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
 
       if (decr_pc != 0)
        {
@@ -3464,26 +3600,39 @@ resume_stopped_resumed_lwps (struct lwp_info *lp, void *data)
     {
       struct regcache *regcache = get_thread_regcache (lp->ptid);
       struct gdbarch *gdbarch = get_regcache_arch (regcache);
-      CORE_ADDR pc = regcache_read_pc (regcache);
-
-      gdb_assert (is_executing (lp->ptid));
 
-      /* Don't bother if there's a breakpoint at PC that we'd hit
-        immediately, and we're not waiting for this LWP.  */
-      if (!ptid_match (lp->ptid, *wait_ptid_p))
+      TRY
        {
-         if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
-           return 0;
-       }
+         CORE_ADDR pc = regcache_read_pc (regcache);
+         int leave_stopped = 0;
 
-      if (debug_linux_nat)
-       fprintf_unfiltered (gdb_stdlog,
-                           "RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
-                           target_pid_to_str (lp->ptid),
-                           paddress (gdbarch, pc),
-                           lp->step);
+         /* Don't bother if there's a breakpoint at PC that we'd hit
+            immediately, and we're not waiting for this LWP.  */
+         if (!ptid_match (lp->ptid, *wait_ptid_p))
+           {
+             if (breakpoint_inserted_here_p (get_regcache_aspace (regcache), pc))
+               leave_stopped = 1;
+           }
 
-      linux_resume_one_lwp (lp, lp->step, GDB_SIGNAL_0);
+         if (!leave_stopped)
+           {
+             if (debug_linux_nat)
+               fprintf_unfiltered (gdb_stdlog,
+                                   "RSRL: resuming stopped-resumed LWP %s at "
+                                   "%s: step=%d\n",
+                                   target_pid_to_str (lp->ptid),
+                                   paddress (gdbarch, pc),
+                                   lp->step);
+
+             linux_resume_one_lwp_throw (lp, lp->step, GDB_SIGNAL_0);
+           }
+       }
+      CATCH (ex, RETURN_MASK_ERROR)
+       {
+         if (!check_ptrace_stopped_lwp_gone (lp))
+           throw_exception (ex);
+       }
+      END_CATCH
     }
 
   return 0;
@@ -3509,7 +3658,7 @@ linux_nat_wait (struct target_ops *ops,
     }
 
   /* Flush the async file first.  */
-  if (target_can_async_p ())
+  if (target_is_async_p ())
     async_file_flush ();
 
   /* Resume LWPs that are currently stopped without any pending status
@@ -3527,16 +3676,12 @@ linux_nat_wait (struct target_ops *ops,
   /* If we requested any event, and something came out, assume there
      may be more.  If we requested a specific lwp or process, also
      assume there may be more.  */
-  if (target_can_async_p ()
+  if (target_is_async_p ()
       && ((ourstatus->kind != TARGET_WAITKIND_IGNORE
           && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
          || !ptid_equal (ptid, minus_one_ptid)))
     async_file_mark ();
 
-  /* Get ready for the next event.  */
-  if (target_can_async_p ())
-    target_async (inferior_event_handler, 0);
-
   return event_ptid;
 }
 
@@ -4144,13 +4289,13 @@ linux_proc_pending_signals (int pid, sigset_t *pending,
         Unfortunately some Red Hat kernels include the shared pending
         queue but not the ShdPnd status field.  */
 
-      if (strncmp (buffer, "SigPnd:\t", 8) == 0)
+      if (startswith (buffer, "SigPnd:\t"))
        add_line_to_sigset (buffer + 8, pending);
-      else if (strncmp (buffer, "ShdPnd:\t", 8) == 0)
+      else if (startswith (buffer, "ShdPnd:\t"))
        add_line_to_sigset (buffer + 8, pending);
-      else if (strncmp (buffer, "SigBlk:\t", 8) == 0)
+      else if (startswith (buffer, "SigBlk:\t"))
        add_line_to_sigset (buffer + 8, blocked);
-      else if (strncmp (buffer, "SigIgn:\t", 8) == 0)
+      else if (startswith (buffer, "SigIgn:\t"))
        add_line_to_sigset (buffer + 8, ignored);
     }
 
@@ -4335,10 +4480,7 @@ linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
 static int
 linux_nat_is_async_p (struct target_ops *ops)
 {
-  /* NOTE: palves 2008-03-21: We're only async when the user requests
-     it explicitly with the "set target-async" command.
-     Someday, linux will always be async.  */
-  return target_async_permitted;
+  return linux_is_async_p ();
 }
 
 /* target_can_async_p implementation.  */
@@ -4388,7 +4530,11 @@ static int async_terminal_is_ours = 1;
 static void
 linux_nat_terminal_inferior (struct target_ops *self)
 {
-  if (!target_is_async_p ())
+  /* Like target_terminal_inferior, use target_can_async_p, not
+     target_is_async_p, since at this point the target is not async
+     yet.  If it can async, then we know it will become async prior to
+     resume.  */
+  if (!target_can_async_p ())
     {
       /* Async mode is disabled.  */
       child_terminal_inferior (self);
@@ -4418,13 +4564,6 @@ linux_nat_terminal_inferior (struct target_ops *self)
 static void
 linux_nat_terminal_ours (struct target_ops *self)
 {
-  if (!target_is_async_p ())
-    {
-      /* Async mode is disabled.  */
-      child_terminal_ours (self);
-      return;
-    }
-
   /* GDB should never give the terminal to the inferior if the
      inferior is running in the background (run&, continue&, etc.),
      but claiming it sure should.  */
@@ -4477,7 +4616,7 @@ handle_target_event (int error, gdb_client_data client_data)
 static int
 linux_async_pipe (int enable)
 {
-  int previous = (linux_nat_event_pipe[0] != -1);
+  int previous = linux_is_async_p ();
 
   if (previous != enable)
     {
@@ -4681,6 +4820,10 @@ linux_nat_add_target (struct target_ops *t)
   t->to_thread_address_space = linux_nat_thread_address_space;
   t->to_stopped_by_watchpoint = linux_nat_stopped_by_watchpoint;
   t->to_stopped_data_address = linux_nat_stopped_data_address;
+  t->to_stopped_by_sw_breakpoint = linux_nat_stopped_by_sw_breakpoint;
+  t->to_supports_stopped_by_sw_breakpoint = linux_nat_supports_stopped_by_sw_breakpoint;
+  t->to_stopped_by_hw_breakpoint = linux_nat_stopped_by_hw_breakpoint;
+  t->to_supports_stopped_by_hw_breakpoint = linux_nat_supports_stopped_by_hw_breakpoint;
 
   t->to_can_async_p = linux_nat_can_async_p;
   t->to_is_async_p = linux_nat_is_async_p;
This page took 0.040505 seconds and 4 git commands to generate.