Blame SOURCES/gdb-rhbz1261564-aarch64-hw-watchpoint-5of5.patch

01917d
http://sourceware.org/ml/gdb-patches/2015-02/msg00734.html
01917d
Subject: [PATCH 7/8] gdbserver/Linux: Use TRAP_BRKPT/TRAP_HWBPT
01917d
01917d
This patch adjusts gdbserver's Linux backend to tell gdbserver core
01917d
(and ultimately GDB) whether a trap was caused by a breakpoint.
01917d
01917d
It teaches the backend to get that information out of the si_code of
01917d
the SIGTRAP siginfo.
01917d
01917d
gdb/gdbserver/ChangeLog:
01917d
2015-02-25  Pedro Alves  <palves@redhat.com>
01917d
01917d
        * linux-low.c (check_stopped_by_breakpoint) [USE_SIGTRAP_SIGINFO]:
01917d
	Decide whether a breakpoint triggered based on the SIGTRAP's
01917d
	siginfo.si_code.
01917d
        (thread_still_has_status_pending_p) [USE_SIGTRAP_SIGINFO]: Don't check whether a
01917d
        breakpoint is inserted if relying on SIGTRAP's siginfo.si_code.
01917d
	(linux_low_filter_event): Check for breakpoints before checking
01917d
	watchpoints.
01917d
	(linux_wait_1): Don't re-increment the PC if relying on SIGTRAP's
01917d
	siginfo.si_code.
01917d
        (linux_stopped_by_sw_breakpoint)
01917d
        (linux_supports_stopped_by_sw_breakpoint)
01917d
        (linux_stopped_by_hw_breakpoint)
01917d
        (linux_supports_stopped_by_hw_breakpoint): New functions.
01917d
        (linux_target_ops): Install new target methods.
01917d
---
01917d
 gdb/gdbserver/linux-low.c | 122 +++++++++++++++++++++++++++++++++++++++++++---
01917d
 1 file changed, 116 insertions(+), 6 deletions(-)
01917d
01917d
Index: gdb-7.6.1/gdb/gdbserver/linux-low.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/linux-low.c	2016-03-13 21:42:34.115589901 +0100
01917d
+++ gdb-7.6.1/gdb/gdbserver/linux-low.c	2016-03-13 21:45:00.625644976 +0100
01917d
@@ -1360,6 +1360,8 @@
01917d
   return (struct lwp_info*) find_inferior (&all_lwps, same_lwp, &ptid);
01917d
 }
01917d
 
01917d
+static void save_stop_reason (struct lwp_info *lwp);
01917d
+
01917d
 static struct lwp_info *
01917d
 linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
01917d
 {
01917d
@@ -1465,6 +1467,10 @@
01917d
 
01917d
 	  current_inferior = saved_inferior;
01917d
 	}
01917d
+
01917d
+      /* RHEL */
01917d
+      if (!child->stopped_by_watchpoint)
01917d
+	save_stop_reason (child);
01917d
     }
01917d
 
01917d
   /* Store the STOP_PC, with adjustment applied.  This depends on the
01917d
@@ -1847,6 +1853,47 @@
01917d
    being stepped.  */
01917d
 ptid_t step_over_bkpt;
01917d
 
01917d
+// gdb/nat/linux-ptrace.h
01917d
+#if defined __i386__ || defined __x86_64__
01917d
+# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL)
01917d
+# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT)
01917d
+#elif defined __powerpc__
01917d
+# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL || (X) == TRAP_BRKPT)
01917d
+# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT)
01917d
+#elif defined __mips__
01917d
+# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == SI_KERNEL)
01917d
+# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == SI_KERNEL)
01917d
+#else
01917d
+# define GDB_ARCH_IS_TRAP_BRKPT(X) ((X) == TRAP_BRKPT)
01917d
+# define GDB_ARCH_IS_TRAP_HWBKPT(X) ((X) == TRAP_HWBKPT)
01917d
+#endif
01917d
+
01917d
+// gdb/nat/linux-ptrace.h
01917d
+#ifndef TRAP_HWBKPT
01917d
+# define TRAP_HWBKPT 4
01917d
+#endif
01917d
+
01917d
+static void
01917d
+save_stop_reason (struct lwp_info *lwp)
01917d
+{
01917d
+  siginfo_t siginfo;
01917d
+
01917d
+  if (ptrace (PTRACE_GETSIGINFO, lwpid_of (lwp),
01917d
+              (PTRACE_ARG3_TYPE) 0, &siginfo) == 0)
01917d
+    {
01917d
+      if (siginfo.si_signo == SIGTRAP)
01917d
+        {
01917d
+          if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
01917d
+	    {
01917d
+              /* This can indicate either a hardware breakpoint or
01917d
+                 hardware watchpoint.  Check debug registers.  */
01917d
+//              if (!check_stopped_by_watchpoint (lwp))
01917d
+		lwp->stopped_by_hw_breakpoint = 1;
01917d
+	    }
01917d
+	}
01917d
+    }
01917d
+}
01917d
+
01917d
 /* Wait for an event from child PID.  If PID is -1, wait for any
01917d
    child.  Store the stop status through the status pointer WSTAT.
01917d
    OPTIONS is passed to the waitpid call.  Return 0 if no child stop
01917d
@@ -3332,6 +3396,7 @@
01917d
   errno = 0;
01917d
   lwp->stopped = 0;
01917d
   lwp->stopped_by_watchpoint = 0;
01917d
+  lwp->stopped_by_hw_breakpoint = 0;
01917d
   lwp->stepping = step;
01917d
   ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (lwp),
01917d
 	  (PTRACE_ARG3_TYPE) 0,
01917d
@@ -4856,6 +4921,26 @@
01917d
     return 1;
01917d
 }
01917d
 
01917d
+/* Implement the to_stopped_by_hw_breakpoint target_ops
01917d
+   method.  */
01917d
+
01917d
+static int
01917d
+linux_stopped_by_hw_breakpoint (void)
01917d
+{
01917d
+  struct lwp_info *lwp = get_thread_lwp (current_inferior);
01917d
+
01917d
+  return lwp->stopped_by_hw_breakpoint;
01917d
+}
01917d
+
01917d
+/* Implement the to_supports_stopped_by_hw_breakpoint target_ops
01917d
+   method.  */
01917d
+
01917d
+static int
01917d
+linux_supports_stopped_by_hw_breakpoint (void)
01917d
+{
01917d
+  return 1; // RHEL
01917d
+}
01917d
+
01917d
 static int
01917d
 linux_stopped_by_watchpoint (void)
01917d
 {
01917d
@@ -5946,6 +6031,8 @@
01917d
   linux_read_auxv,
01917d
   linux_insert_point,
01917d
   linux_remove_point,
01917d
+  linux_stopped_by_hw_breakpoint,
01917d
+  linux_supports_stopped_by_hw_breakpoint,
01917d
   linux_stopped_by_watchpoint,
01917d
   linux_stopped_data_address,
01917d
 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
01917d
Index: gdb-7.6.1/gdb/gdbserver/linux-low.h
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/linux-low.h	2016-03-13 21:45:52.861021138 +0100
01917d
+++ gdb-7.6.1/gdb/gdbserver/linux-low.h	2016-03-13 21:46:00.668077363 +0100
01917d
@@ -223,6 +223,9 @@
01917d
      watchpoint trap.  */
01917d
   int stopped_by_watchpoint;
01917d
 
01917d
+  /* RHEL: TARGET_STOPPED_BY_HW_BREAKPOINT */
01917d
+  int stopped_by_hw_breakpoint;
01917d
+
01917d
   /* On architectures where it is possible to know the data address of
01917d
      a triggered watchpoint, STOPPED_DATA_ADDRESS is non-zero, and
01917d
      contains such data address.  Only valid if STOPPED_BY_WATCHPOINT