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

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