Blame SOURCES/gdb-rhbz1854784-powerpc-remove-region-limit-dawr-4of7.patch

599b31
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
599b31
From: Pedro Franco de Carvalho <pedromfc@linux.ibm.com>
599b31
Date: Wed, 7 Jul 2021 18:49:42 -0400
599b31
Subject: gdb-rhbz1854784-powerpc-remove-region-limit-dawr-4of7.patch
599b31
599b31
;; Backport "[PowerPC] Fix debug register issues in ppc-linux-nat"
599b31
;; (Pedro Franco de Carvalho, RH BZ 1854784)
599b31
599b31
This patch fixes some issues with debug register handling for the powerpc
599b31
linux native target.
599b31
599b31
Currently, the target methods for installing and removing hardware
599b31
breakpoints and watchpoints in ppc-linux-nat.c affect all threads known to
599b31
linux-nat, including threads of different processes.
599b31
599b31
This patch changes ppc-linux-nat.c so that only the process of
599b31
inferior_ptid is affected by these target methods, as GDB expects.
599b31
599b31
This is done in the same way as various other architectures.  The
599b31
install/remove target methods only register a hardware breakpoint or
599b31
watchpoint, and then send a stop signal to the threads.  The debug
599b31
registers are only changed with ptrace right before each thread is next
599b31
resumed, using low_prepare_to_resume.
599b31
599b31
There are two interfaces to modify debug registers for linux running on
599b31
powerpc, with different sets of ptrace requests:
599b31
599b31
- PPC_PTRACE_GETHWDBGINFO, PPC_PTRACE_SETHWDEBUG, and
599b31
  PPC_PTRACE_DELHWDEBUG.
599b31
599b31
   Or
599b31
599b31
- PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG
599b31
599b31
The first set (HWDEBUG) is the more flexible one and allows setting
599b31
watchpoints with a variable watched region length and, for certain
599b31
embedded processors, multiple types of debug registers (e.g. hardware
599b31
breakpoints and hardware-assisted conditions for watchpoints).
599b31
Currently, server processors only provide one watchpoint.  The second one
599b31
(DEBUGREG) only allows setting one debug register, a watchpoint, so we
599b31
only use it if the first one is not available.
599b31
599b31
The HWDEBUG interface handles debug registers with slot numbers.  Once a
599b31
hardware watchpoint or breakpoint is installed (with
599b31
PPC_PTRACE_SETHWDEBUG), ptrace returns a slot number.  This slot number
599b31
can then be used to remove the watchpoint or breakpoint from the inferior
599b31
(with PPC_PTRACE_DELHWDEBUG).  The first interface also provides a
599b31
bitmask of available debug register features, which can be obtained with
599b31
PPC_PTRACE_GETHWDBGINFO.
599b31
599b31
When GDB first tries to use debug registers, we try the first interface
599b31
with a ptrace call, and if it isn't available, we fall back to the second
599b31
one, if available.  We use EIO as an indicator that an interface is not
599b31
available in the kernel.  For simplicity, with any other error we
599b31
immediately assume no interface is available.  Unfortunately this means
599b31
that if a process is killed by a signal right before we try to detect the
599b31
interface, we might get an ESRCH, which would prevent debug registers to
599b31
be used in the GDB session.  However, it isn't clear that we can safely
599b31
raise an exception and try again in the future in all the contexts where
599b31
we try to detect the interface.
599b31
599b31
If the HWDEBUG interface works but provides no feature bits, the target
599b31
falls back to the DEBUGREG interface.  When the kernel is configured
599b31
without CONFIG_HW_BREAKPOINTS (selected by CONFIG_PERF_EVENTS), there is
599b31
a bug that causes watchpoints installed with the HWDEBUG interface not to
599b31
trigger.  When this is the case, the feature bits will be zero, which is
599b31
used as the indicator to fall back to the DEBUGREG interface.  This isn't
599b31
ideal, but has always been the behavior of GDB before this patch, so I
599b31
decided not to change it.
599b31
599b31
A flag indicates for each thread if its debug registers need to be
599b31
updated the next time it is resumed.  The flag is set whenever the upper
599b31
layers request or remove a hardware watchpoint or breakpoint, or when a
599b31
new thread is detected.  Because some kernel configurations disable
599b31
watchpoints after they are hit, we also use the last stop reason of the
599b31
LWP to determine whether we should update the debug registers.  It isn't
599b31
clear that this is also true of BookE hardware breakpoints, but we also
599b31
check their stop reason to be on the safe side, since it doesn't hurt.
599b31
599b31
A map from process numbers to hardware watchpoint or breakpoint objects
599b31
keeps track of what has been requested by the upper layers of GDB, since
599b31
for GDB installing a hardware watchpoint or breakpoint means doing so for
599b31
the whole process.
599b31
599b31
When using the HWDEBUG interface we also have to keep track of which
599b31
slots were last installed in each thread with a map from threads to the
599b31
slots, so that they can be removed when needed.  When resuming a thread,
599b31
we remove all the slots using this map, then we install all the hardware
599b31
watchpoints and breakpoints from the per-process map of requests, and
599b31
then update the per-thread map accordingly.
599b31
599b31
This per-thread state is also used for copying the debug register state
599b31
after a fork or a clone is detected.  The kernel might do this depending
599b31
on the configuration.  Recent kernels running on server processors that
599b31
were configured with CONFIG_PERF_EVENTS (and therefore
599b31
CONFIG_HW_BREAKPOINTS) don't copy debug registers across forks and
599b31
clones.  Recent kernels without CONFIG_HW_BREAKPOINTS copy this state.  I
599b31
believe that on embedded processors (e.g. a ppc440) the debug register
599b31
state is copied, but I haven't been able to test this.  To handle both
599b31
cases, the per-thread state is always copied when forks and clones are
599b31
detected, and when we resume the thread and delete the debug register
599b31
slots before updating them, we ignore ENOENT errors.
599b31
599b31
We don't need to handle this when using the DEBUGREG interface since it
599b31
only allows one hardware watchpoint and doesn't return slot numbers, we
599b31
just set or clear this watchpoint when needed.
599b31
599b31
Since we signal running threads to stop after a request is processed, so
599b31
that we can update their debug registers when they are next resumed,
599b31
there will be a time between signalling the threads and their stop during
599b31
which the debug registers haven't been updated, even if the target
599b31
methods completed.
599b31
599b31
The tests in gdb.threads/watchpoint-fork.exp no longer fail with this
599b31
patch.
599b31
599b31
gdb/ChangeLog:
599b31
2020-03-30  Pedro Franco de Carvalho  <pedromfc@linux.ibm.com>
599b31
599b31
	* ppc-linux-nat.c: Include <algorithm>, <unordered_map>, and
599b31
	<list>.  Remove inclusion of observable.h.
599b31
	(PPC_DEBUG_CURRENT_VERSION): Move up define.
599b31
	(struct arch_lwp_info): New struct.
599b31
	(class ppc_linux_dreg_interface): New class.
599b31
	(struct ppc_linux_process_info): New struct.
599b31
	(struct ppc_linux_nat_target) <low_delete_thread, low_new_fork>
599b31
	<low_new_clone, low_forget_process, low_prepare_to_resume>
599b31
	<copy_thread_dreg_state, mark_thread_stale>
599b31
	<mark_debug_registers_changed, register_hw_breakpoint>
599b31
	<clear_hw_breakpoint, register_wp, clear_wp>
599b31
	<can_use_watchpoint_cond_accel, calculate_dvc, check_condition>
599b31
	<num_memory_accesses, get_trigger_type>
599b31
	<create_watchpoint_request, hwdebug_point_cmp>
599b31
	<init_arch_lwp_info, get_arch_lwp_info>
599b31
	<low_stopped_by_watchpoint, low_stopped_data_address>: Declare as
599b31
	methods.
599b31
	<struct ptid_hash>: New inner struct.
599b31
	<m_dreg_interface, m_process_info, m_installed_hw_bps>: Declare
599b31
	members.
599b31
	(saved_dabr_value, hwdebug_info, max_slots_number)
599b31
	(struct hw_break_tuple, struct thread_points, ppc_threads)
599b31
	(have_ptrace_hwdebug_interface)
599b31
	(hwdebug_find_thread_points_by_tid)
599b31
	(hwdebug_insert_point, hwdebug_remove_point): Remove.
599b31
	(ppc_linux_nat_target::can_use_hw_breakpoint): Use
599b31
	m_dreg_interface, remove call to PTRACE_SET_DEBUGREG.
599b31
	(ppc_linux_nat_target::region_ok_for_hw_watchpoint): Add comment,
599b31
	use m_dreg_interface.
599b31
	(hwdebug_point_cmp): Change to...
599b31
	(ppc_linux_nat_target::hwdebug_point_cmp): ...this method.  Use
599b31
	reference arguments instead of pointers.
599b31
	(ppc_linux_nat_target::ranged_break_num_registers): Use
599b31
	m_dreg_interface.
599b31
	(ppc_linux_nat_target::insert_hw_breakpoint): Add comment, use
599b31
	m_dreg_interface.  Call register_hw_breakpoint.
599b31
	(ppc_linux_nat_target::remove_hw_breakpoint): Add comment, use
599b31
	m_dreg_interface.  Call clear_hw_breakpoint.
599b31
	(get_trigger_type): Change to...
599b31
	(ppc_linux_nat_target::get_trigger_type): ...this method.  Add
599b31
	comment.
599b31
	(ppc_linux_nat_target::insert_mask_watchpoint): Update comment,
599b31
	use m_dreg_interface.  Call register_hw_breakpoint.
599b31
	(ppc_linux_nat_target::remove_mask_watchpoint): Update comment,
599b31
	use m_dreg_interface.  Call clear_hw_breakpoint.
599b31
	(can_use_watchpoint_cond_accel): Change to...
599b31
	(ppc_linux_nat_target::can_use_watchpoint_cond_accel): ...this
599b31
	method.  Update comment, use m_dreg_interface and
599b31
	m_process_info.
599b31
	(calculate_dvc): Change to...
599b31
	(ppc_linux_nat_target::calculate_dvc): ...this method.  Use
599b31
	m_dreg_interface.
599b31
	(num_memory_accesses): Change to...
599b31
	(ppc_linux_nat_target::num_memory_accesses): ...this method.
599b31
	(check_condition): Change to...
599b31
	(ppc_linux_nat_target::check_condition): ...this method.
599b31
	(ppc_linux_nat_target::can_accel_watchpoint_condition): Update
599b31
	comment, use m_dreg_interface.
599b31
	(create_watchpoint_request): Change to...
599b31
	(ppc_linux_nat_target::create_watchpoint_request): ...this
599b31
	method.  Use m_dreg_interface.
599b31
	(ppc_linux_nat_target::insert_watchpoint): Add comment, use
599b31
	m_dreg_interface.  Call register_hw_breakpoint or register_wp.
599b31
	(ppc_linux_nat_target::remove_watchpoint): Add comment, use
599b31
	m_dreg_interface.  Call clear_hw_breakpoint or clear_wp.
599b31
	(ppc_linux_nat_target::low_forget_process)
599b31
	(ppc_linux_nat_target::low_new_fork)
599b31
	(ppc_linux_nat_target::low_new_clone)
599b31
	(ppc_linux_nat_target::low_delete_thread)
599b31
	(ppc_linux_nat_target::low_prepare_to_resume): New methods.
599b31
	(ppc_linux_nat_target::low_new_thread): Remove previous logic,
599b31
	only call mark_thread_stale.
599b31
	(ppc_linux_thread_exit): Remove.
599b31
	(ppc_linux_nat_target::stopped_data_address): Change to...
599b31
	(ppc_linux_nat_target::low_stopped_data_address): This. Add
599b31
	comment, use m_dreg_interface and m_thread_hw_breakpoints.
599b31
	(ppc_linux_nat_target::stopped_by_watchpoint): Change to...
599b31
	(ppc_linux_nat_target::stopped_by_watchpoint): This.  Add
599b31
	comment.  Call low_stopped_data_address.
599b31
	(ppc_linux_nat_target::watchpoint_addr_within_range): Use
599b31
	m_dreg_interface.
599b31
	(ppc_linux_nat_target::masked_watch_num_registers): Use
599b31
	m_dreg_interface.
599b31
	(ppc_linux_nat_target::copy_thread_dreg_state)
599b31
	(ppc_linux_nat_target::mark_thread_stale)
599b31
	(ppc_linux_nat_target::mark_debug_registers_changed)
599b31
	(ppc_linux_nat_target::register_hw_breakpoint)
599b31
	(ppc_linux_nat_target::clear_hw_breakpoint)
599b31
	(ppc_linux_nat_target::register_wp)
599b31
	(ppc_linux_nat_target::clear_wp)
599b31
	(ppc_linux_nat_target::init_arch_lwp_info)
599b31
	(ppc_linux_nat_target::get_arch_lwp_info): New methods.
599b31
	(_initialize_ppc_linux_nat): Remove observer callback.
599b31
599b31
diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c
599b31
--- a/gdb/aarch64-linux-nat.c
599b31
+++ b/gdb/aarch64-linux-nat.c
599b31
@@ -64,14 +64,14 @@ public:
599b31
   int can_use_hw_breakpoint (enum bptype, int, int) override;
599b31
   int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
599b31
   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
599b31
   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
599b31
 			 struct expression *) override;
599b31
   int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
599b31
 			 struct expression *) override;
599b31
   bool stopped_by_watchpoint () override;
599b31
   bool stopped_data_address (CORE_ADDR *) override;
599b31
-  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, LONGEST) override;
599b31
+  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
599b31
 
599b31
   int can_do_single_step () override;
599b31
 
599b31
@@ -808,7 +808,7 @@ aarch64_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
599b31
 /* Implement the "region_ok_for_hw_watchpoint" target_ops method.  */
599b31
 
599b31
 int
599b31
-aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+aarch64_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   return aarch64_linux_region_ok_for_watchpoint (addr, len);
599b31
 }
599b31
@@ -888,7 +888,7 @@ aarch64_linux_nat_target::stopped_by_watchpoint ()
599b31
 bool
599b31
 aarch64_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
599b31
 							CORE_ADDR start,
599b31
-							LONGEST length)
599b31
+							int length)
599b31
 {
599b31
   return start <= addr && start + length - 1 >= addr;
599b31
 }
599b31
diff --git a/gdb/arm-linux-nat.c b/gdb/arm-linux-nat.c
599b31
--- a/gdb/arm-linux-nat.c
599b31
+++ b/gdb/arm-linux-nat.c
599b31
@@ -80,7 +80,7 @@ public:
599b31
 
599b31
   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
599b31
 
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
599b31
 
599b31
   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
599b31
 			 struct expression *) override;
599b31
@@ -91,7 +91,7 @@ public:
599b31
 
599b31
   bool stopped_data_address (CORE_ADDR *) override;
599b31
 
599b31
-  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, LONGEST) override;
599b31
+  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
599b31
 
599b31
   const struct target_desc *read_description () override;
599b31
 
599b31
@@ -1093,7 +1093,7 @@ arm_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
599b31
 /* Are we able to use a hardware watchpoint for the LEN bytes starting at 
599b31
    ADDR?  */
599b31
 int
599b31
-arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+arm_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   const struct arm_linux_hwbp_cap *cap = arm_linux_get_hwbp_cap ();
599b31
   CORE_ADDR max_wp_length, aligned_addr;
599b31
@@ -1202,7 +1202,7 @@ arm_linux_nat_target::stopped_by_watchpoint ()
599b31
 bool
599b31
 arm_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
599b31
 						    CORE_ADDR start,
599b31
-						    LONGEST length)
599b31
+						    int length)
599b31
 {
599b31
   return start <= addr && start + length - 1 >= addr;
599b31
 }
599b31
diff --git a/gdb/mips-linux-nat.c b/gdb/mips-linux-nat.c
599b31
--- a/gdb/mips-linux-nat.c
599b31
+++ b/gdb/mips-linux-nat.c
599b31
@@ -614,7 +614,7 @@ mips_linux_nat_target::stopped_data_address (CORE_ADDR *paddr)
599b31
    the specified region can be covered by the watch registers.  */
599b31
 
599b31
 int
599b31
-mips_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+mips_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   struct pt_watch_regs dummy_regs;
599b31
   int i;
599b31
diff --git a/gdb/ppc-linux-nat.c b/gdb/ppc-linux-nat.c
599b31
--- a/gdb/ppc-linux-nat.c
599b31
+++ b/gdb/ppc-linux-nat.c
599b31
@@ -18,7 +18,6 @@
599b31
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
599b31
 
599b31
 #include "defs.h"
599b31
-#include "observable.h"
599b31
 #include "frame.h"
599b31
 #include "inferior.h"
599b31
 #include "gdbthread.h"
599b31
@@ -37,6 +36,9 @@
599b31
 #include <sys/procfs.h>
599b31
 #include "nat/gdb_ptrace.h"
599b31
 #include "inf-ptrace.h"
599b31
+#include <algorithm>
599b31
+#include <unordered_map>
599b31
+#include <list>
599b31
 
599b31
 /* Prototypes for supply_gregset etc.  */
599b31
 #include "gregset.h"
599b31
@@ -52,6 +54,13 @@
599b31
 #include "nat/ppc-linux.h"
599b31
 #include "linux-tdep.h"
599b31
 
599b31
+/* Function type for the CALLBACK argument of iterate_over_lwps_new.  */
599b31
+typedef int (iterate_over_lwps_new_ftype) (struct lwp_info *lwp);
599b31
+
599b31
+static struct lwp_info *iterate_over_lwps_new
599b31
+    (ptid_t filter,
599b31
+     gdb::function_view<iterate_over_lwps_new_ftype> callback);
599b31
+
599b31
 /* Similarly for the hardware watchpoint support.  These requests are used
599b31
    when the PowerPC HWDEBUG ptrace interface is not available.  */
599b31
 #ifndef PTRACE_GET_DEBUGREG
599b31
@@ -135,6 +144,10 @@ struct ppc_hw_breakpoint
599b31
 #define PPC_DEBUG_FEATURE_DATA_BP_DAWR	0x10
599b31
 #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */
599b31
 
599b31
+/* The version of the PowerPC HWDEBUG kernel interface that we will use, if
599b31
+   available.  */
599b31
+#define PPC_DEBUG_CURRENT_VERSION 1
599b31
+
599b31
 /* Similarly for the general-purpose (gp0 -- gp31)
599b31
    and floating-point registers (fp0 -- fp31).  */
599b31
 #ifndef PTRACE_GETREGS
599b31
@@ -269,6 +282,214 @@ int have_ptrace_getsetregs = 1;
599b31
    them and gotten an error.  */
599b31
 int have_ptrace_getsetfpregs = 1;
599b31
 
599b31
+/* Private arch info associated with each thread lwp_info object, used
599b31
+   for debug register handling.  */
599b31
+
599b31
+struct arch_lwp_info
599b31
+{
599b31
+  /* When true, indicates that the debug registers installed in the
599b31
+     thread no longer correspond to the watchpoints and breakpoints
599b31
+     requested by GDB.  */
599b31
+  bool debug_regs_stale;
599b31
+
599b31
+  /* We need a back-reference to the PTID of the thread so that we can
599b31
+     cleanup the debug register state of the thread in
599b31
+     low_delete_thread.  */
599b31
+  ptid_t lwp_ptid;
599b31
+};
599b31
+
599b31
+/* Class used to detect which set of ptrace requests that
599b31
+   ppc_linux_nat_target will use to install and remove hardware
599b31
+   breakpoints and watchpoints.
599b31
+
599b31
+   The interface is only detected once, testing the ptrace calls.  The
599b31
+   result can indicate that no interface is available.
599b31
+
599b31
+   The Linux kernel provides two different sets of ptrace requests to
599b31
+   handle hardware watchpoints and breakpoints for Power:
599b31
+
599b31
+   - PPC_PTRACE_GETHWDBGINFO, PPC_PTRACE_SETHWDEBUG, and
599b31
+     PPC_PTRACE_DELHWDEBUG.
599b31
+
599b31
+   Or
599b31
+
599b31
+   - PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG
599b31
+
599b31
+   The first set is the more flexible one and allows setting watchpoints
599b31
+   with a variable watched region length and, for BookE processors,
599b31
+   multiple types of debug registers (e.g. hardware breakpoints and
599b31
+   hardware-assisted conditions for watchpoints).  The second one only
599b31
+   allows setting one debug register, a watchpoint, so we only use it if
599b31
+   the first one is not available.  */
599b31
+
599b31
+class ppc_linux_dreg_interface
599b31
+{
599b31
+public:
599b31
+
599b31
+  ppc_linux_dreg_interface ()
599b31
+    : m_interface (), m_hwdebug_info ()
599b31
+  {
599b31
+  };
599b31
+
599b31
+  DISABLE_COPY_AND_ASSIGN (ppc_linux_dreg_interface);
599b31
+
599b31
+  /* One and only one of these three functions returns true, indicating
599b31
+     whether the corresponding interface is the one we detected.  The
599b31
+     interface must already have been detected as a precontidion.  */
599b31
+
599b31
+  bool hwdebug_p ()
599b31
+  {
599b31
+    gdb_assert (detected_p ());
599b31
+    return *m_interface == HWDEBUG;
599b31
+  }
599b31
+
599b31
+  bool debugreg_p ()
599b31
+  {
599b31
+    gdb_assert (detected_p ());
599b31
+    return *m_interface == DEBUGREG;
599b31
+  }
599b31
+
599b31
+  bool unavailable_p ()
599b31
+  {
599b31
+    gdb_assert (detected_p ());
599b31
+    return *m_interface == UNAVAILABLE;
599b31
+  }
599b31
+
599b31
+  /* Returns the debug register capabilities of the target.  Should only
599b31
+     be called if the interface is HWDEBUG.  */
599b31
+  const struct ppc_debug_info &hwdebug_info ()
599b31
+  {
599b31
+    gdb_assert (hwdebug_p ());
599b31
+
599b31
+    return m_hwdebug_info;
599b31
+  }
599b31
+
599b31
+  /* Returns true if the interface has already been detected.  This is
599b31
+     useful for cases when we know there is no work to be done if the
599b31
+     interface hasn't been detected yet.  */
599b31
+  bool detected_p ()
599b31
+  {
599b31
+    return m_interface.has_value ();
599b31
+  }
599b31
+
599b31
+  /* Detect the available interface, if any, if it hasn't been detected
599b31
+     before, using PTID for the necessary ptrace calls.  */
599b31
+
599b31
+  void detect (const ptid_t &ptid)
599b31
+  {
599b31
+    if (m_interface.has_value ())
599b31
+      return;
599b31
+
599b31
+    gdb_assert (ptid.lwp_p ());
599b31
+
599b31
+    bool no_features = false;
599b31
+
599b31
+    if (ptrace (PPC_PTRACE_GETHWDBGINFO, ptid.lwp (), 0, &m_hwdebug_info)
599b31
+	!= -1)
599b31
+      {
599b31
+	/* If there are no advertised features, we don't use the
599b31
+	   HWDEBUG interface and try the DEBUGREG interface instead.
599b31
+	   It shouldn't be necessary to do this, however, when the
599b31
+	   kernel is configured without CONFIG_HW_BREAKPOINTS (selected
599b31
+	   by CONFIG_PERF_EVENTS), there is a bug that causes
599b31
+	   watchpoints installed with the HWDEBUG interface not to
599b31
+	   trigger.  When this is the case, features will be zero,
599b31
+	   which we use as an indicator to fall back to the DEBUGREG
599b31
+	   interface.  */
599b31
+	if (m_hwdebug_info.features != 0)
599b31
+	  {
599b31
+	    m_interface.emplace (HWDEBUG);
599b31
+	    return;
599b31
+	  }
599b31
+	else
599b31
+	  no_features = true;
599b31
+      }
599b31
+
599b31
+    /* EIO indicates that the request is invalid, so we try DEBUGREG
599b31
+       next.  Technically, it can also indicate other failures, but we
599b31
+       can't differentiate those.
599b31
+
599b31
+       Other errors could happen for various reasons.  We could get an
599b31
+       ESRCH if the traced thread was killed by a signal.  Trying to
599b31
+       detect the interface with another thread in the future would be
599b31
+       complicated, as callers would have to handle an "unknown
599b31
+       interface" case.  It's also unclear if raising an exception
599b31
+       here would be safe.
599b31
+
599b31
+       Other errors, such as ENODEV, could be more permanent and cause
599b31
+       a failure for any thread.
599b31
+
599b31
+       For simplicity, with all errors other than EIO, we set the
599b31
+       interface to UNAVAILABLE and don't try DEBUGREG.  If DEBUGREG
599b31
+       fails too, we'll also set the interface to UNAVAILABLE.  It's
599b31
+       unlikely that trying the DEBUGREG interface with this same thread
599b31
+       would work, for errors other than EIO.  This means that these
599b31
+       errors will cause hardware watchpoints and breakpoints to become
599b31
+       unavailable throughout a GDB session.  */
599b31
+
599b31
+    if (no_features || errno == EIO)
599b31
+      {
599b31
+	unsigned long wp;
599b31
+
599b31
+	if (ptrace (PTRACE_GET_DEBUGREG, ptid.lwp (), 0, &wp) != -1)
599b31
+	  {
599b31
+	    m_interface.emplace (DEBUGREG);
599b31
+	    return;
599b31
+	  }
599b31
+      }
599b31
+
599b31
+    if (errno != EIO)
599b31
+      warning (_("Error when detecting the debug register interface. "
599b31
+		 "Debug registers will be unavailable."));
599b31
+
599b31
+    m_interface.emplace (UNAVAILABLE);
599b31
+    return;
599b31
+  }
599b31
+
599b31
+private:
599b31
+
599b31
+  /* HWDEBUG represents the set of calls PPC_PTRACE_GETHWDBGINFO,
599b31
+     PPC_PTRACE_SETHWDEBUG and PPC_PTRACE_DELHWDEBUG.
599b31
+
599b31
+     DEBUGREG represents the set of calls PTRACE_SET_DEBUGREG and
599b31
+     PTRACE_GET_DEBUGREG.
599b31
+
599b31
+     UNAVAILABLE can indicate that the kernel doesn't support any of the
599b31
+     two sets of requests or that there was an error when we tried to
599b31
+     detect wich interface is available.  */
599b31
+
599b31
+  enum debug_reg_interface
599b31
+    {
599b31
+     UNAVAILABLE,
599b31
+     HWDEBUG,
599b31
+     DEBUGREG
599b31
+    };
599b31
+
599b31
+  /* The interface option.  Initialized if has_value () returns true.  */
599b31
+  gdb::optional<enum debug_reg_interface> m_interface;
599b31
+
599b31
+  /* The info returned by the kernel with PPC_PTRACE_GETHWDBGINFO.  Only
599b31
+     valid if we determined that the interface is HWDEBUG.  */
599b31
+  struct ppc_debug_info m_hwdebug_info;
599b31
+};
599b31
+
599b31
+/* Per-process information.  This includes the hardware watchpoints and
599b31
+   breakpoints that GDB requested to this target.  */
599b31
+
599b31
+struct ppc_linux_process_info
599b31
+{
599b31
+  /* The list of hardware watchpoints and breakpoints that GDB requested
599b31
+     for this process.
599b31
+
599b31
+     Only used when the interface is HWDEBUG.  */
599b31
+  std::list<struct ppc_hw_breakpoint> requested_hw_bps;
599b31
+
599b31
+  /* The watchpoint value that GDB requested for this process.
599b31
+
599b31
+     Only used when the interface is DEBUGREG.  */
599b31
+  gdb::optional<long> requested_wp_val;
599b31
+};
599b31
+
599b31
 struct ppc_linux_nat_target final : public linux_nat_target
599b31
 {
599b31
   /* Add our register access methods.  */
599b31
@@ -284,7 +505,7 @@ struct ppc_linux_nat_target final : public linux_nat_target
599b31
   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
599b31
     override;
599b31
 
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
599b31
 
599b31
   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
599b31
 			 struct expression *) override;
599b31
@@ -298,13 +519,9 @@ struct ppc_linux_nat_target final : public linux_nat_target
599b31
   int remove_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
599b31
     override;
599b31
 
599b31
-  bool stopped_by_watchpoint () override;
599b31
-
599b31
-  bool stopped_data_address (CORE_ADDR *) override;
599b31
+  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
599b31
 
599b31
-  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, LONGEST) override;
599b31
-
599b31
-  bool can_accel_watchpoint_condition (CORE_ADDR, LONGEST, int, struct expression *)
599b31
+  bool can_accel_watchpoint_condition (CORE_ADDR, int, int, struct expression *)
599b31
     override;
599b31
 
599b31
   int masked_watch_num_registers (CORE_ADDR, CORE_ADDR) override;
599b31
@@ -318,7 +535,95 @@ struct ppc_linux_nat_target final : public linux_nat_target
599b31
     override;
599b31
 
599b31
   /* Override linux_nat_target low methods.  */
599b31
+  bool low_stopped_by_watchpoint () override;
599b31
+
599b31
+  bool low_stopped_data_address (CORE_ADDR *) override;
599b31
+
599b31
   void low_new_thread (struct lwp_info *lp) override;
599b31
+
599b31
+  void low_delete_thread (arch_lwp_info *) override;
599b31
+
599b31
+  void low_new_fork (struct lwp_info *, pid_t) override;
599b31
+
599b31
+  void low_new_clone (struct lwp_info *, pid_t) override;
599b31
+
599b31
+  void low_forget_process (pid_t pid) override;
599b31
+
599b31
+  void low_prepare_to_resume (struct lwp_info *) override;
599b31
+
599b31
+private:
599b31
+
599b31
+  void copy_thread_dreg_state (const ptid_t &parent_ptid,
599b31
+			       const ptid_t &child_ptid);
599b31
+
599b31
+  void mark_thread_stale (struct lwp_info *lp);
599b31
+
599b31
+  void mark_debug_registers_changed (pid_t pid);
599b31
+
599b31
+  void register_hw_breakpoint (pid_t pid,
599b31
+			       const struct ppc_hw_breakpoint &bp);
599b31
+
599b31
+  void clear_hw_breakpoint (pid_t pid,
599b31
+			    const struct ppc_hw_breakpoint &a);
599b31
+
599b31
+  void register_wp (pid_t pid, long wp_value);
599b31
+
599b31
+  void clear_wp (pid_t pid);
599b31
+
599b31
+  bool can_use_watchpoint_cond_accel (void);
599b31
+
599b31
+  void calculate_dvc (CORE_ADDR addr, int len,
599b31
+		      CORE_ADDR data_value,
599b31
+		      uint32_t *condition_mode,
599b31
+		      uint64_t *condition_value);
599b31
+
599b31
+  int check_condition (CORE_ADDR watch_addr,
599b31
+		       struct expression *cond,
599b31
+		       CORE_ADDR *data_value, int *len);
599b31
+
599b31
+  int num_memory_accesses (const std::vector<value_ref_ptr> &chain);
599b31
+
599b31
+  int get_trigger_type (enum target_hw_bp_type type);
599b31
+
599b31
+  void create_watchpoint_request (struct ppc_hw_breakpoint *p,
599b31
+				  CORE_ADDR addr,
599b31
+				  int len,
599b31
+				  enum target_hw_bp_type type,
599b31
+				  struct expression *cond,
599b31
+				  int insert);
599b31
+
599b31
+  bool hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
599b31
+			  const struct ppc_hw_breakpoint &b);
599b31
+
599b31
+  void init_arch_lwp_info (struct lwp_info *lp);
599b31
+
599b31
+  arch_lwp_info *get_arch_lwp_info (struct lwp_info *lp);
599b31
+
599b31
+  /* The ptrace interface we'll use to install hardware watchpoints and
599b31
+     breakpoints (debug registers).  */
599b31
+  ppc_linux_dreg_interface m_dreg_interface;
599b31
+
599b31
+  /* A map from pids to structs containing info specific to each
599b31
+     process.  */
599b31
+  std::unordered_map<pid_t, ppc_linux_process_info> m_process_info;
599b31
+
599b31
+  /* Callable object to hash ptids by their lwp number.  */
599b31
+  struct ptid_hash
599b31
+  {
599b31
+    std::size_t operator() (const ptid_t &ptid) const
599b31
+    {
599b31
+      return std::hash<long>{} (ptid.lwp ());
599b31
+    }
599b31
+  };
599b31
+
599b31
+  /* A map from ptid_t objects to a list of pairs of slots and hardware
599b31
+     breakpoint objects.  This keeps track of which hardware breakpoints
599b31
+     and watchpoints were last installed in each slot of each thread.
599b31
+
599b31
+     Only used when the interface is HWDEBUG.  */
599b31
+  std::unordered_map 
599b31
+		      std::list<std::pair<long, ppc_hw_breakpoint>>,
599b31
+		      ptid_hash> m_installed_hw_bps;
599b31
 };
599b31
 
599b31
 static ppc_linux_nat_target the_ppc_linux_nat_target;
599b31
@@ -1725,102 +2030,50 @@ ppc_linux_nat_target::read_description ()
599b31
   return ppc_linux_match_description (features);
599b31
 }
599b31
 
599b31
-/* The cached DABR value, to install in new threads.
599b31
-   This variable is used when the PowerPC HWDEBUG ptrace
599b31
-   interface is not available.  */
599b31
-static long saved_dabr_value;
599b31
-
599b31
-/* Global structure that will store information about the available
599b31
-   features provided by the PowerPC HWDEBUG ptrace interface.  */
599b31
-static struct ppc_debug_info hwdebug_info;
599b31
-
599b31
-/* Global variable that holds the maximum number of slots that the
599b31
-   kernel will use.  This is only used when PowerPC HWDEBUG ptrace interface
599b31
-   is available.  */
599b31
-static size_t max_slots_number = 0;
599b31
-
599b31
-struct hw_break_tuple
599b31
-{
599b31
-  long slot;
599b31
-  struct ppc_hw_breakpoint *hw_break;
599b31
-};
599b31
-
599b31
-/* This is an internal VEC created to store information about *points inserted
599b31
-   for each thread.  This is used when PowerPC HWDEBUG ptrace interface is
599b31
-   available.  */
599b31
-typedef struct thread_points
599b31
-  {
599b31
-    /* The TID to which this *point relates.  */
599b31
-    int tid;
599b31
-    /* Information about the *point, such as its address, type, etc.
599b31
-
599b31
-       Each element inside this vector corresponds to a hardware
599b31
-       breakpoint or watchpoint in the thread represented by TID.  The maximum
599b31
-       size of these vector is MAX_SLOTS_NUMBER.  If the hw_break element of
599b31
-       the tuple is NULL, then the position in the vector is free.  */
599b31
-    struct hw_break_tuple *hw_breaks;
599b31
-  } *thread_points_p;
599b31
-DEF_VEC_P (thread_points_p);
599b31
-
599b31
-VEC(thread_points_p) *ppc_threads = NULL;
599b31
-
599b31
-/* The version of the PowerPC HWDEBUG kernel interface that we will use, if
599b31
-   available.  */
599b31
-#define PPC_DEBUG_CURRENT_VERSION 1
599b31
-
599b31
-/* Returns non-zero if we support the PowerPC HWDEBUG ptrace interface.  */
599b31
-static int
599b31
-have_ptrace_hwdebug_interface (void)
599b31
-{
599b31
-  static int have_ptrace_hwdebug_interface = -1;
599b31
-
599b31
-  if (have_ptrace_hwdebug_interface == -1)
599b31
-    {
599b31
-      int tid;
599b31
-
599b31
-      tid = inferior_ptid.lwp ();
599b31
-      if (tid == 0)
599b31
-	tid = inferior_ptid.pid ();
599b31
-
599b31
-      /* Check for kernel support for PowerPC HWDEBUG ptrace interface.  */
599b31
-      if (ptrace (PPC_PTRACE_GETHWDBGINFO, tid, 0, &hwdebug_info) >= 0)
599b31
-	{
599b31
-	  /* Check whether PowerPC HWDEBUG ptrace interface is functional and
599b31
-	     provides any supported feature.  */
599b31
-	  if (hwdebug_info.features != 0)
599b31
-	    {
599b31
-	      have_ptrace_hwdebug_interface = 1;
599b31
-	      max_slots_number = hwdebug_info.num_instruction_bps
599b31
-	        + hwdebug_info.num_data_bps
599b31
-	        + hwdebug_info.num_condition_regs;
599b31
-	      return have_ptrace_hwdebug_interface;
599b31
-	    }
599b31
-	}
599b31
-      /* Old school interface and no PowerPC HWDEBUG ptrace support.  */
599b31
-      have_ptrace_hwdebug_interface = 0;
599b31
-      memset (&hwdebug_info, 0, sizeof (struct ppc_debug_info));
599b31
-    }
599b31
-
599b31
-  return have_ptrace_hwdebug_interface;
599b31
-}
599b31
+/* Routines for installing hardware watchpoints and breakpoints.  When
599b31
+   GDB requests a hardware watchpoint or breakpoint to be installed, we
599b31
+   register the request for the pid of inferior_ptid in a map with one
599b31
+   entry per process.  We then issue a stop request to all the threads of
599b31
+   this process, and mark a per-thread flag indicating that their debug
599b31
+   registers should be updated.  Right before they are next resumed, we
599b31
+   remove all previously installed debug registers and install all the
599b31
+   ones GDB requested.  We then update a map with one entry per thread
599b31
+   that keeps track of what debug registers were last installed in each
599b31
+   thread.
599b31
+
599b31
+   We use this second map to remove installed registers before installing
599b31
+   the ones requested by GDB, and to copy the debug register state after
599b31
+   a thread clones or forks, since depending on the kernel configuration,
599b31
+   debug registers can be inherited.  */
599b31
+
599b31
+/* Check if we support and have enough resources to install a hardware
599b31
+   watchpoint or breakpoint.  See the description in target.h.  */
599b31
 
599b31
 int
599b31
-ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt, int ot)
599b31
+ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt,
599b31
+					     int ot)
599b31
 {
599b31
   int total_hw_wp, total_hw_bp;
599b31
 
599b31
-  if (have_ptrace_hwdebug_interface ())
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  if (m_dreg_interface.unavailable_p ())
599b31
+    return 0;
599b31
+
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
     {
599b31
       /* When PowerPC HWDEBUG ptrace interface is available, the number of
599b31
 	 available hardware watchpoints and breakpoints is stored at the
599b31
 	 hwdebug_info struct.  */
599b31
-      total_hw_bp = hwdebug_info.num_instruction_bps;
599b31
-      total_hw_wp = hwdebug_info.num_data_bps;
599b31
+      total_hw_bp = m_dreg_interface.hwdebug_info ().num_instruction_bps;
599b31
+      total_hw_wp = m_dreg_interface.hwdebug_info ().num_data_bps;
599b31
     }
599b31
   else
599b31
     {
599b31
-      /* When we do not have PowerPC HWDEBUG ptrace interface, we should
599b31
-	 consider having 1 hardware watchpoint and no hardware breakpoints.  */
599b31
+      gdb_assert (m_dreg_interface.debugreg_p ());
599b31
+
599b31
+      /* With the DEBUGREG ptrace interface, we should consider having 1
599b31
+	 hardware watchpoint and no hardware breakpoints.  */
599b31
       total_hw_bp = 0;
599b31
       total_hw_wp = 1;
599b31
     }
599b31
@@ -1828,53 +2081,50 @@ ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt, int ot)
599b31
   if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
599b31
       || type == bp_access_watchpoint || type == bp_watchpoint)
599b31
     {
599b31
-      if (cnt + ot > total_hw_wp)
599b31
+      if (total_hw_wp == 0)
599b31
+	return 0;
599b31
+      else if (cnt + ot > total_hw_wp)
599b31
 	return -1;
599b31
+      else
599b31
+	return 1;
599b31
     }
599b31
   else if (type == bp_hardware_breakpoint)
599b31
     {
599b31
       if (total_hw_bp == 0)
599b31
-	{
599b31
-	  /* No hardware breakpoint support. */
599b31
-	  return 0;
599b31
-	}
599b31
-      if (cnt > total_hw_bp)
599b31
-	return -1;
599b31
-    }
599b31
-
599b31
-  if (!have_ptrace_hwdebug_interface ())
599b31
-    {
599b31
-      int tid;
599b31
-      ptid_t ptid = inferior_ptid;
599b31
-
599b31
-      /* We need to know whether ptrace supports PTRACE_SET_DEBUGREG
599b31
-	 and whether the target has DABR.  If either answer is no, the
599b31
-	 ptrace call will return -1.  Fail in that case.  */
599b31
-      tid = ptid.lwp ();
599b31
-      if (tid == 0)
599b31
-	tid = ptid.pid ();
599b31
-
599b31
-      if (ptrace (PTRACE_SET_DEBUGREG, tid, 0, 0) == -1)
599b31
 	return 0;
599b31
+      else if (cnt > total_hw_bp)
599b31
+	return -1;
599b31
+      else
599b31
+	return 1;
599b31
     }
599b31
 
599b31
-  return 1;
599b31
+  return 0;
599b31
 }
599b31
 
599b31
+/* Returns 1 if we can watch LEN bytes at address ADDR, 0 otherwise.  */
599b31
+
599b31
 int
599b31
-ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   /* Handle sub-8-byte quantities.  */
599b31
   if (len <= 0)
599b31
     return 0;
599b31
 
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  if (m_dreg_interface.unavailable_p ())
599b31
+    return 0;
599b31
+
599b31
   /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
599b31
      restrictions for watchpoints in the processors.  In that case, we use that
599b31
      information to determine the hardcoded watchable region for
599b31
      watchpoints.  */
599b31
-  if (have_ptrace_hwdebug_interface ())
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
     {
599b31
       int region_size;
599b31
+      const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
599b31
+						   .hwdebug_info ());
599b31
+
599b31
       /* Embedded DAC-based processors, like the PowerPC 440 have ranged
599b31
 	 watchpoints and can watch any access within an arbitrary memory
599b31
 	 region. This is useful to watch arrays and structs, for instance.  It
599b31
@@ -1901,121 +2151,32 @@ ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
      ptrace interface, DAC-based processors (i.e., embedded processors) will
599b31
      use addresses aligned to 4-bytes due to the way the read/write flags are
599b31
      passed in the old ptrace interface.  */
599b31
-  else if (((linux_get_hwcap (current_top_target ()) & PPC_FEATURE_BOOKE)
599b31
-	   && (addr + len) > (addr & ~3) + 4)
599b31
-	   || (addr + len) > (addr & ~7) + 8)
599b31
-    return 0;
599b31
-
599b31
-  return 1;
599b31
-}
599b31
-
599b31
-/* This function compares two ppc_hw_breakpoint structs field-by-field.  */
599b31
-static int
599b31
-hwdebug_point_cmp (struct ppc_hw_breakpoint *a, struct ppc_hw_breakpoint *b)
599b31
-{
599b31
-  return (a->trigger_type == b->trigger_type
599b31
-	  && a->addr_mode == b->addr_mode
599b31
-	  && a->condition_mode == b->condition_mode
599b31
-	  && a->addr == b->addr
599b31
-	  && a->addr2 == b->addr2
599b31
-	  && a->condition_value == b->condition_value);
599b31
-}
599b31
-
599b31
-/* This function can be used to retrieve a thread_points by the TID of the
599b31
-   related process/thread.  If nothing has been found, and ALLOC_NEW is 0,
599b31
-   it returns NULL.  If ALLOC_NEW is non-zero, a new thread_points for the
599b31
-   provided TID will be created and returned.  */
599b31
-static struct thread_points *
599b31
-hwdebug_find_thread_points_by_tid (int tid, int alloc_new)
599b31
-{
599b31
-  int i;
599b31
-  struct thread_points *t;
599b31
-
599b31
-  for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, t); i++)
599b31
-    if (t->tid == tid)
599b31
-      return t;
599b31
-
599b31
-  t = NULL;
599b31
-
599b31
-  /* Do we need to allocate a new point_item
599b31
-     if the wanted one does not exist?  */
599b31
-  if (alloc_new)
599b31
+  else
599b31
     {
599b31
-      t = XNEW (struct thread_points);
599b31
-      t->hw_breaks = XCNEWVEC (struct hw_break_tuple, max_slots_number);
599b31
-      t->tid = tid;
599b31
-      VEC_safe_push (thread_points_p, ppc_threads, t);
599b31
-    }
599b31
+      gdb_assert (m_dreg_interface.debugreg_p ());
599b31
 
599b31
-  return t;
599b31
-}
599b31
-
599b31
-/* This function is a generic wrapper that is responsible for inserting a
599b31
-   *point (i.e., calling `ptrace' in order to issue the request to the
599b31
-   kernel) and registering it internally in GDB.  */
599b31
-static void
599b31
-hwdebug_insert_point (struct ppc_hw_breakpoint *b, int tid)
599b31
-{
599b31
-  int i;
599b31
-  long slot;
599b31
-  gdb::unique_xmalloc_ptr<ppc_hw_breakpoint> p (XDUP (ppc_hw_breakpoint, b));
599b31
-  struct hw_break_tuple *hw_breaks;
599b31
-  struct thread_points *t;
599b31
-  struct hw_break_tuple *tuple;
599b31
-
599b31
-  errno = 0;
599b31
-  slot = ptrace (PPC_PTRACE_SETHWDEBUG, tid, 0, p.get ());
599b31
-  if (slot < 0)
599b31
-    perror_with_name (_("Unexpected error setting breakpoint or watchpoint"));
599b31
-
599b31
-  /* Everything went fine, so we have to register this *point.  */
599b31
-  t = hwdebug_find_thread_points_by_tid (tid, 1);
599b31
-  gdb_assert (t != NULL);
599b31
-  hw_breaks = t->hw_breaks;
599b31
-
599b31
-  /* Find a free element in the hw_breaks vector.  */
599b31
-  for (i = 0; i < max_slots_number; i++)
599b31
-    if (hw_breaks[i].hw_break == NULL)
599b31
-      {
599b31
-	hw_breaks[i].slot = slot;
599b31
-	hw_breaks[i].hw_break = p.release ();
599b31
-	break;
599b31
-      }
599b31
+      if (((linux_get_hwcap (current_top_target ()) & PPC_FEATURE_BOOKE)
599b31
+	   && (addr + len) > (addr & ~3) + 4)
599b31
+	  || (addr + len) > (addr & ~7) + 8)
599b31
+	return 0;
599b31
+    }
599b31
 
599b31
-  gdb_assert (i != max_slots_number);
599b31
+  return 1;
599b31
 }
599b31
 
599b31
-/* This function is a generic wrapper that is responsible for removing a
599b31
-   *point (i.e., calling `ptrace' in order to issue the request to the
599b31
-   kernel), and unregistering it internally at GDB.  */
599b31
-static void
599b31
-hwdebug_remove_point (struct ppc_hw_breakpoint *b, int tid)
599b31
-{
599b31
-  int i;
599b31
-  struct hw_break_tuple *hw_breaks;
599b31
-  struct thread_points *t;
599b31
-
599b31
-  t = hwdebug_find_thread_points_by_tid (tid, 0);
599b31
-  gdb_assert (t != NULL);
599b31
-  hw_breaks = t->hw_breaks;
599b31
-
599b31
-  for (i = 0; i < max_slots_number; i++)
599b31
-    if (hw_breaks[i].hw_break && hwdebug_point_cmp (hw_breaks[i].hw_break, b))
599b31
-      break;
599b31
+/* This function compares two ppc_hw_breakpoint structs
599b31
+   field-by-field.  */
599b31
 
599b31
-  gdb_assert (i != max_slots_number);
599b31
-
599b31
-  /* We have to ignore ENOENT errors because the kernel implements hardware
599b31
-     breakpoints/watchpoints as "one-shot", that is, they are automatically
599b31
-     deleted when hit.  */
599b31
-  errno = 0;
599b31
-  if (ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot) < 0)
599b31
-    if (errno != ENOENT)
599b31
-      perror_with_name (_("Unexpected error deleting "
599b31
-			  "breakpoint or watchpoint"));
599b31
-
599b31
-  xfree (hw_breaks[i].hw_break);
599b31
-  hw_breaks[i].hw_break = NULL;
599b31
+bool
599b31
+ppc_linux_nat_target::hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
599b31
+					 const struct ppc_hw_breakpoint &b)
599b31
+{
599b31
+  return (a.trigger_type == b.trigger_type
599b31
+	  && a.addr_mode == b.addr_mode
599b31
+	  && a.condition_mode == b.condition_mode
599b31
+	  && a.addr == b.addr
599b31
+	  && a.addr2 == b.addr2
599b31
+	  && a.condition_value == b.condition_value);
599b31
 }
599b31
 
599b31
 /* Return the number of registers needed for a ranged breakpoint.  */
599b31
@@ -2023,22 +2184,28 @@ hwdebug_remove_point (struct ppc_hw_breakpoint *b, int tid)
599b31
 int
599b31
 ppc_linux_nat_target::ranged_break_num_registers ()
599b31
 {
599b31
-  return ((have_ptrace_hwdebug_interface ()
599b31
-	   && hwdebug_info.features & PPC_DEBUG_FEATURE_INSN_BP_RANGE)?
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  return ((m_dreg_interface.hwdebug_p ()
599b31
+	   && (m_dreg_interface.hwdebug_info ().features
599b31
+	       & PPC_DEBUG_FEATURE_INSN_BP_RANGE))?
599b31
 	  2 : -1);
599b31
 }
599b31
 
599b31
-/* Insert the hardware breakpoint described by BP_TGT.  Returns 0 for
599b31
-   success, 1 if hardware breakpoints are not supported or -1 for failure.  */
599b31
+/* Register the hardware breakpoint described by BP_TGT, to be inserted
599b31
+   when the threads of inferior_ptid are resumed.  Returns 0 for success,
599b31
+   or -1 if the HWDEBUG interface that we need for hardware breakpoints
599b31
+   is not available.  */
599b31
 
599b31
 int
599b31
 ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
599b31
 					    struct bp_target_info *bp_tgt)
599b31
 {
599b31
-  struct lwp_info *lp;
599b31
   struct ppc_hw_breakpoint p;
599b31
 
599b31
-  if (!have_ptrace_hwdebug_interface ())
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  if (!m_dreg_interface.hwdebug_p ())
599b31
     return -1;
599b31
 
599b31
   p.version = PPC_DEBUG_CURRENT_VERSION;
599b31
@@ -2061,20 +2228,25 @@ ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
599b31
       p.addr2 = 0;
599b31
     }
599b31
 
599b31
-  ALL_LWPS (lp)
599b31
-    hwdebug_insert_point (&p, lp->ptid.lwp ());
599b31
+  register_hw_breakpoint (inferior_ptid.pid (), p);
599b31
 
599b31
   return 0;
599b31
 }
599b31
 
599b31
+/* Clear a registration for the hardware breakpoint given by type BP_TGT.
599b31
+   It will be removed from the threads of inferior_ptid when they are
599b31
+   next resumed.  Returns 0 for success, or -1 if the HWDEBUG interface
599b31
+   that we need for hardware breakpoints is not available.  */
599b31
+
599b31
 int
599b31
 ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
599b31
 					    struct bp_target_info *bp_tgt)
599b31
 {
599b31
-  struct lwp_info *lp;
599b31
   struct ppc_hw_breakpoint p;
599b31
 
599b31
-  if (!have_ptrace_hwdebug_interface ())
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  if (!m_dreg_interface.hwdebug_p ())
599b31
     return -1;
599b31
 
599b31
   p.version = PPC_DEBUG_CURRENT_VERSION;
599b31
@@ -2097,14 +2269,16 @@ ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
599b31
       p.addr2 = 0;
599b31
     }
599b31
 
599b31
-  ALL_LWPS (lp)
599b31
-    hwdebug_remove_point (&p, lp->ptid.lwp ());
599b31
+  clear_hw_breakpoint (inferior_ptid.pid (), p);
599b31
 
599b31
   return 0;
599b31
 }
599b31
 
599b31
-static int
599b31
-get_trigger_type (enum target_hw_bp_type type)
599b31
+/* Return the trigger value to set in a ppc_hw_breakpoint object for a
599b31
+   given hardware watchpoint TYPE.  We assume type is not hw_execute.  */
599b31
+
599b31
+int
599b31
+ppc_linux_nat_target::get_trigger_type (enum target_hw_bp_type type)
599b31
 {
599b31
   int t;
599b31
 
599b31
@@ -2118,19 +2292,18 @@ get_trigger_type (enum target_hw_bp_type type)
599b31
   return t;
599b31
 }
599b31
 
599b31
-/* Insert a new masked watchpoint at ADDR using the mask MASK.
599b31
-   RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
599b31
-   or hw_access for an access watchpoint.  Returns 0 on success and throws
599b31
-   an error on failure.  */
599b31
+/* Register a new masked watchpoint at ADDR using the mask MASK, to be
599b31
+   inserted when the threads of inferior_ptid are resumed.  RW may be
599b31
+   hw_read for a read watchpoint, hw_write for a write watchpoint or
599b31
+   hw_access for an access watchpoint.  */
599b31
 
599b31
 int
599b31
 ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr,  CORE_ADDR mask,
599b31
 					      target_hw_bp_type rw)
599b31
 {
599b31
-  struct lwp_info *lp;
599b31
   struct ppc_hw_breakpoint p;
599b31
 
599b31
-  gdb_assert (have_ptrace_hwdebug_interface ());
599b31
+  gdb_assert (m_dreg_interface.hwdebug_p ());
599b31
 
599b31
   p.version = PPC_DEBUG_CURRENT_VERSION;
599b31
   p.trigger_type = get_trigger_type (rw);
599b31
@@ -2140,25 +2313,23 @@ ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr,  CORE_ADDR mask,
599b31
   p.addr2 = mask;
599b31
   p.condition_value = 0;
599b31
 
599b31
-  ALL_LWPS (lp)
599b31
-    hwdebug_insert_point (&p, lp->ptid.lwp ());
599b31
+  register_hw_breakpoint (inferior_ptid.pid (), p);
599b31
 
599b31
   return 0;
599b31
 }
599b31
 
599b31
-/* Remove a masked watchpoint at ADDR with the mask MASK.
599b31
-   RW may be hw_read for a read watchpoint, hw_write for a write watchpoint
599b31
-   or hw_access for an access watchpoint.  Returns 0 on success and throws
599b31
-   an error on failure.  */
599b31
+/* Clear a registration for a masked watchpoint at ADDR with the mask
599b31
+   MASK.  It will be removed from the threads of inferior_ptid when they
599b31
+   are next resumed.  RW may be hw_read for a read watchpoint, hw_write
599b31
+   for a write watchpoint or hw_access for an access watchpoint.  */
599b31
 
599b31
 int
599b31
 ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
599b31
 					      target_hw_bp_type rw)
599b31
 {
599b31
-  struct lwp_info *lp;
599b31
   struct ppc_hw_breakpoint p;
599b31
 
599b31
-  gdb_assert (have_ptrace_hwdebug_interface ());
599b31
+  gdb_assert (m_dreg_interface.hwdebug_p ());
599b31
 
599b31
   p.version = PPC_DEBUG_CURRENT_VERSION;
599b31
   p.trigger_type = get_trigger_type (rw);
599b31
@@ -2168,40 +2339,42 @@ ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
599b31
   p.addr2 = mask;
599b31
   p.condition_value = 0;
599b31
 
599b31
-  ALL_LWPS (lp)
599b31
-    hwdebug_remove_point (&p, lp->ptid.lwp ());
599b31
+  clear_hw_breakpoint (inferior_ptid.pid (), p);
599b31
 
599b31
   return 0;
599b31
 }
599b31
 
599b31
-/* Check whether we have at least one free DVC register.  */
599b31
-static int
599b31
-can_use_watchpoint_cond_accel (void)
599b31
+/* Check whether we have at least one free DVC register for the threads
599b31
+   of the pid of inferior_ptid.  */
599b31
+
599b31
+bool
599b31
+ppc_linux_nat_target::can_use_watchpoint_cond_accel (void)
599b31
 {
599b31
-  struct thread_points *p;
599b31
-  int tid = inferior_ptid.lwp ();
599b31
-  int cnt = hwdebug_info.num_condition_regs, i;
599b31
-  CORE_ADDR tmp_value;
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
 
599b31
-  if (!have_ptrace_hwdebug_interface () || cnt == 0)
599b31
-    return 0;
599b31
+  if (!m_dreg_interface.hwdebug_p ())
599b31
+    return false;
599b31
 
599b31
-  p = hwdebug_find_thread_points_by_tid (tid, 0);
599b31
+  int cnt = m_dreg_interface.hwdebug_info ().num_condition_regs;
599b31
 
599b31
-  if (p)
599b31
-    {
599b31
-      for (i = 0; i < max_slots_number; i++)
599b31
-	if (p->hw_breaks[i].hw_break != NULL
599b31
-	    && (p->hw_breaks[i].hw_break->condition_mode
599b31
-		!= PPC_BREAKPOINT_CONDITION_NONE))
599b31
-	  cnt--;
599b31
+  if (cnt == 0)
599b31
+    return false;
599b31
 
599b31
-      /* There are no available slots now.  */
599b31
-      if (cnt <= 0)
599b31
-	return 0;
599b31
-    }
599b31
+  auto process_it = m_process_info.find (inferior_ptid.pid ());
599b31
 
599b31
-  return 1;
599b31
+  /* No breakpoints or watchpoints have been requested for this process,
599b31
+     we have at least one free DVC register.  */
599b31
+  if (process_it == m_process_info.end ())
599b31
+    return true;
599b31
+
599b31
+  for (const ppc_hw_breakpoint &bp : process_it->second.requested_hw_bps)
599b31
+    if (bp.condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
599b31
+      cnt--;
599b31
+
599b31
+  if (cnt <= 0)
599b31
+    return false;
599b31
+
599b31
+  return true;
599b31
 }
599b31
 
599b31
 /* Calculate the enable bits and the contents of the Data Value Compare
599b31
@@ -2212,10 +2385,16 @@ can_use_watchpoint_cond_accel (void)
599b31
    On exit, CONDITION_MODE will hold the enable bits for the DVC, and
599b31
    CONDITION_VALUE will hold the value which should be put in the
599b31
    DVC register.  */
599b31
-static void
599b31
-calculate_dvc (CORE_ADDR addr, LONGEST len, CORE_ADDR data_value,
599b31
-	       uint32_t *condition_mode, uint64_t *condition_value)
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::calculate_dvc (CORE_ADDR addr, int len,
599b31
+				     CORE_ADDR data_value,
599b31
+				     uint32_t *condition_mode,
599b31
+				     uint64_t *condition_value)
599b31
 {
599b31
+  const struct ppc_debug_info &hwdebug_info = (m_dreg_interface.
599b31
+					       hwdebug_info ());
599b31
+
599b31
   LONGEST i, num_byte_enable;
599b31
   int align_offset, num_bytes_off_dvc, rightmost_enabled_byte;
599b31
   CORE_ADDR addr_end_data, addr_end_dvc;
599b31
@@ -2254,8 +2433,10 @@ calculate_dvc (CORE_ADDR addr, LONGEST len, CORE_ADDR data_value,
599b31
    Returns -1 if there's any register access involved, or if there are
599b31
    other kinds of values which are not acceptable in a condition
599b31
    expression (e.g., lval_computed or lval_internalvar).  */
599b31
-static int
599b31
-num_memory_accesses (const std::vector<value_ref_ptr> &chain)
599b31
+
599b31
+int
599b31
+ppc_linux_nat_target::num_memory_accesses (const std::vector<value_ref_ptr>
599b31
+					   &chain)
599b31
 {
599b31
   int found_memory_cnt = 0;
599b31
 
599b31
@@ -2303,9 +2484,11 @@ num_memory_accesses (const std::vector<value_ref_ptr> &chain)
599b31
    If the function returns 1, DATA_VALUE will contain the constant against
599b31
    which the watch value should be compared and LEN will contain the size
599b31
    of the constant.  */
599b31
-static int
599b31
-check_condition (CORE_ADDR watch_addr, struct expression *cond,
599b31
-		 CORE_ADDR *data_value, LONGEST *len)
599b31
+
599b31
+int
599b31
+ppc_linux_nat_target::check_condition (CORE_ADDR watch_addr,
599b31
+				       struct expression *cond,
599b31
+				       CORE_ADDR *data_value, int *len)
599b31
 {
599b31
   int pc = 1, num_accesses_left, num_accesses_right;
599b31
   struct value *left_val, *right_val;
599b31
@@ -2352,19 +2535,21 @@ check_condition (CORE_ADDR watch_addr, struct expression *cond,
599b31
   return 1;
599b31
 }
599b31
 
599b31
-/* Return non-zero if the target is capable of using hardware to evaluate
599b31
-   the condition expression, thus only triggering the watchpoint when it is
599b31
+/* Return true if the target is capable of using hardware to evaluate the
599b31
+   condition expression, thus only triggering the watchpoint when it is
599b31
    true.  */
599b31
+
599b31
 bool
599b31
 ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr,
599b31
-						      LONGEST len,
599b31
-						      int rw,
599b31
+						      int len, int rw,
599b31
 						      struct expression *cond)
599b31
 {
599b31
   CORE_ADDR data_value;
599b31
 
599b31
-  return (have_ptrace_hwdebug_interface ()
599b31
-	  && hwdebug_info.num_condition_regs > 0
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  return (m_dreg_interface.hwdebug_p ()
599b31
+	  && (m_dreg_interface.hwdebug_info ().num_condition_regs > 0)
599b31
 	  && check_condition (addr, cond, &data_value, &len));
599b31
 }
599b31
 
599b31
@@ -2373,11 +2558,16 @@ ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr,
599b31
    evaluated by hardware.  INSERT tells if we are creating a request for
599b31
    inserting or removing the watchpoint.  */
599b31
 
599b31
-static void
599b31
-create_watchpoint_request (struct ppc_hw_breakpoint *p, CORE_ADDR addr,
599b31
-			   LONGEST len, enum target_hw_bp_type type,
599b31
-			   struct expression *cond, int insert)
599b31
+void
599b31
+ppc_linux_nat_target::create_watchpoint_request (struct ppc_hw_breakpoint *p,
599b31
+						 CORE_ADDR addr, int len,
599b31
+						 enum target_hw_bp_type type,
599b31
+						 struct expression *cond,
599b31
+						 int insert)
599b31
 {
599b31
+  const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
599b31
+					       .hwdebug_info ());
599b31
+
599b31
   if (len == 1
599b31
       || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
599b31
     {
599b31
@@ -2419,28 +2609,33 @@ create_watchpoint_request (struct ppc_hw_breakpoint *p, CORE_ADDR addr,
599b31
   p->addr = (uint64_t) addr;
599b31
 }
599b31
 
599b31
+/* Register a watchpoint, to be inserted when the threads of the group of
599b31
+   inferior_ptid are next resumed.  Returns 0 on success, and -1 if there
599b31
+   is no ptrace interface available to install the watchpoint.  */
599b31
+
599b31
 int
599b31
 ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
599b31
 					 enum target_hw_bp_type type,
599b31
 					 struct expression *cond)
599b31
 {
599b31
-  struct lwp_info *lp;
599b31
-  int ret = -1;
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  if (m_dreg_interface.unavailable_p ())
599b31
+    return -1;
599b31
 
599b31
-  if (have_ptrace_hwdebug_interface ())
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
     {
599b31
       struct ppc_hw_breakpoint p;
599b31
 
599b31
       create_watchpoint_request (&p, addr, len, type, cond, 1);
599b31
 
599b31
-      ALL_LWPS (lp)
599b31
-	hwdebug_insert_point (&p, lp->ptid.lwp ());
599b31
-
599b31
-      ret = 0;
599b31
+      register_hw_breakpoint (inferior_ptid.pid (), p);
599b31
     }
599b31
   else
599b31
     {
599b31
-      long dabr_value;
599b31
+      gdb_assert (m_dreg_interface.debugreg_p ());
599b31
+
599b31
+      long wp_value;
599b31
       long read_mode, write_mode;
599b31
 
599b31
       if (linux_get_hwcap (current_top_target ()) & PPC_FEATURE_BOOKE)
599b31
@@ -2458,142 +2653,300 @@ ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
599b31
 	  write_mode = 6;
599b31
 	}
599b31
 
599b31
-      dabr_value = addr & ~(read_mode | write_mode);
599b31
+      wp_value = addr & ~(read_mode | write_mode);
599b31
       switch (type)
599b31
 	{
599b31
 	  case hw_read:
599b31
 	    /* Set read and translate bits.  */
599b31
-	    dabr_value |= read_mode;
599b31
+	    wp_value |= read_mode;
599b31
 	    break;
599b31
 	  case hw_write:
599b31
 	    /* Set write and translate bits.  */
599b31
-	    dabr_value |= write_mode;
599b31
+	    wp_value |= write_mode;
599b31
 	    break;
599b31
 	  case hw_access:
599b31
 	    /* Set read, write and translate bits.  */
599b31
-	    dabr_value |= read_mode | write_mode;
599b31
+	    wp_value |= read_mode | write_mode;
599b31
 	    break;
599b31
 	}
599b31
 
599b31
-      saved_dabr_value = dabr_value;
599b31
-
599b31
-      ALL_LWPS (lp)
599b31
-	if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0,
599b31
-		    saved_dabr_value) < 0)
599b31
-	  return -1;
599b31
-
599b31
-      ret = 0;
599b31
+      register_wp (inferior_ptid.pid (), wp_value);
599b31
     }
599b31
 
599b31
-  return ret;
599b31
+  return 0;
599b31
 }
599b31
 
599b31
+/* Clear a registration for a hardware watchpoint.  It will be removed
599b31
+   from the threads of the group of inferior_ptid when they are next
599b31
+   resumed.  */
599b31
+
599b31
 int
599b31
 ppc_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
599b31
 					 enum target_hw_bp_type type,
599b31
 					 struct expression *cond)
599b31
 {
599b31
-  struct lwp_info *lp;
599b31
-  int ret = -1;
599b31
+  gdb_assert (!m_dreg_interface.unavailable_p ());
599b31
 
599b31
-  if (have_ptrace_hwdebug_interface ())
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
     {
599b31
       struct ppc_hw_breakpoint p;
599b31
 
599b31
       create_watchpoint_request (&p, addr, len, type, cond, 0);
599b31
 
599b31
-      ALL_LWPS (lp)
599b31
-	hwdebug_remove_point (&p, lp->ptid.lwp ());
599b31
-
599b31
-      ret = 0;
599b31
+      clear_hw_breakpoint (inferior_ptid.pid (), p);
599b31
     }
599b31
   else
599b31
     {
599b31
-      saved_dabr_value = 0;
599b31
-      ALL_LWPS (lp)
599b31
-	if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0,
599b31
-		    saved_dabr_value) < 0)
599b31
-	  return -1;
599b31
+      gdb_assert (m_dreg_interface.debugreg_p ());
599b31
 
599b31
-      ret = 0;
599b31
+      clear_wp (inferior_ptid.pid ());
599b31
     }
599b31
 
599b31
-  return ret;
599b31
+  return 0;
599b31
 }
599b31
 
599b31
+/* Clean up the per-process info associated with PID.  When using the
599b31
+   HWDEBUG interface, we also erase the per-thread state of installed
599b31
+   debug registers for all the threads that belong to the group of PID.
599b31
+
599b31
+   Usually the thread state is cleaned up by low_delete_thread.  We also
599b31
+   do it here because low_new_thread is not called for the initial LWP,
599b31
+   so low_delete_thread won't be able to clean up this state.  */
599b31
+
599b31
 void
599b31
-ppc_linux_nat_target::low_new_thread (struct lwp_info *lp)
599b31
+ppc_linux_nat_target::low_forget_process (pid_t pid)
599b31
 {
599b31
-  int tid = lp->ptid.lwp ();
599b31
+  if ((!m_dreg_interface.detected_p ())
599b31
+      || (m_dreg_interface.unavailable_p ()))
599b31
+    return;
599b31
+
599b31
+  ptid_t pid_ptid (pid, 0, 0);
599b31
 
599b31
-  if (have_ptrace_hwdebug_interface ())
599b31
+  m_process_info.erase (pid);
599b31
+
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
     {
599b31
-      int i;
599b31
-      struct thread_points *p;
599b31
-      struct hw_break_tuple *hw_breaks;
599b31
+      for (auto it = m_installed_hw_bps.begin ();
599b31
+	   it != m_installed_hw_bps.end ();)
599b31
+	{
599b31
+	  if (it->first.matches (pid_ptid))
599b31
+	    it = m_installed_hw_bps.erase (it);
599b31
+	  else
599b31
+	    it++;
599b31
+	}
599b31
+    }
599b31
+}
599b31
 
599b31
-      if (VEC_empty (thread_points_p, ppc_threads))
599b31
-	return;
599b31
+/* Copy the per-process state associated with the pid of PARENT to the
599b31
+   sate of CHILD_PID.  GDB expects that a forked process will have the
599b31
+   same hardware breakpoints and watchpoints as the parent.
599b31
 
599b31
-      /* Get a list of breakpoints from any thread.  */
599b31
-      p = VEC_last (thread_points_p, ppc_threads);
599b31
-      hw_breaks = p->hw_breaks;
599b31
+   If we're using the HWDEBUG interface, also copy the thread debug
599b31
+   register state for the ptid of PARENT to the state for CHILD_PID.
599b31
 
599b31
-      /* Copy that thread's breakpoints and watchpoints to the new thread.  */
599b31
-      for (i = 0; i < max_slots_number; i++)
599b31
-	if (hw_breaks[i].hw_break)
599b31
-	  {
599b31
-	    /* Older kernels did not make new threads inherit their parent
599b31
-	       thread's debug state, so we always clear the slot and replicate
599b31
-	       the debug state ourselves, ensuring compatibility with all
599b31
-	       kernels.  */
599b31
+   Like for clone events, we assume the kernel will copy the debug
599b31
+   registers from the parent thread to the child. The
599b31
+   low_prepare_to_resume function is made to work even if it doesn't.
599b31
 
599b31
-	    /* The ppc debug resource accounting is done through "slots".
599b31
-	       Ask the kernel the deallocate this specific *point's slot.  */
599b31
-	    ptrace (PPC_PTRACE_DELHWDEBUG, tid, 0, hw_breaks[i].slot);
599b31
+   We copy the thread state here and not in low_new_thread since we don't
599b31
+   have the pid of the parent in low_new_thread.  Even if we did,
599b31
+   low_new_thread might not be called immediately after the fork event is
599b31
+   detected.  For instance, with the checkpointing system (see
599b31
+   linux-fork.c), the thread won't be added until GDB decides to switch
599b31
+   to a new checkpointed process.  At that point, the debug register
599b31
+   state of the parent thread is unlikely to correspond to the state it
599b31
+   had at the point when it forked.  */
599b31
 
599b31
-	    hwdebug_insert_point (hw_breaks[i].hw_break, tid);
599b31
-	  }
599b31
+void
599b31
+ppc_linux_nat_target::low_new_fork (struct lwp_info *parent,
599b31
+				    pid_t child_pid)
599b31
+{
599b31
+  if ((!m_dreg_interface.detected_p ())
599b31
+      || (m_dreg_interface.unavailable_p ()))
599b31
+    return;
599b31
+
599b31
+  auto process_it = m_process_info.find (parent->ptid.pid ());
599b31
+
599b31
+  if (process_it != m_process_info.end ())
599b31
+    m_process_info[child_pid] = m_process_info[parent->ptid.pid ()];
599b31
+
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
+    {
599b31
+      ptid_t child_ptid (child_pid, child_pid, 0);
599b31
+
599b31
+      copy_thread_dreg_state (parent->ptid, child_ptid);
599b31
     }
599b31
-  else
599b31
-    ptrace (PTRACE_SET_DEBUGREG, tid, 0, saved_dabr_value);
599b31
 }
599b31
 
599b31
-static void
599b31
-ppc_linux_thread_exit (struct thread_info *tp, int silent)
599b31
+/* Copy the thread debug register state from the PARENT thread to the the
599b31
+   state for CHILD_LWP, if we're using the HWDEBUG interface.  We assume
599b31
+   the kernel copies the debug registers from one thread to another after
599b31
+   a clone event.  The low_prepare_to_resume function is made to work
599b31
+   even if it doesn't.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::low_new_clone (struct lwp_info *parent,
599b31
+				     pid_t child_lwp)
599b31
 {
599b31
-  int i;
599b31
-  int tid = tp->ptid.lwp ();
599b31
-  struct hw_break_tuple *hw_breaks;
599b31
-  struct thread_points *t = NULL, *p;
599b31
+  if ((!m_dreg_interface.detected_p ())
599b31
+      || (m_dreg_interface.unavailable_p ()))
599b31
+    return;
599b31
+
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
+    {
599b31
+      ptid_t child_ptid (parent->ptid.pid (), child_lwp, 0);
599b31
+
599b31
+      copy_thread_dreg_state (parent->ptid, child_ptid);
599b31
+    }
599b31
+}
599b31
+
599b31
+/* Initialize the arch-specific thread state for LP so that it contains
599b31
+   the ptid for lp, so that we can use it in low_delete_thread.  Mark the
599b31
+   new thread LP as stale so that we update its debug registers before
599b31
+   resuming it.  This is not called for the initial thread.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::low_new_thread (struct lwp_info *lp)
599b31
+{
599b31
+  init_arch_lwp_info (lp);
599b31
+
599b31
+  mark_thread_stale (lp);
599b31
+}
599b31
+
599b31
+/* Delete the per-thread debug register stale flag.  */
599b31
 
599b31
-  if (!have_ptrace_hwdebug_interface ())
599b31
+void
599b31
+ppc_linux_nat_target::low_delete_thread (struct arch_lwp_info
599b31
+					 *lp_arch_info)
599b31
+{
599b31
+  if (lp_arch_info != NULL)
599b31
+    {
599b31
+      if (m_dreg_interface.detected_p ()
599b31
+	  && m_dreg_interface.hwdebug_p ())
599b31
+	m_installed_hw_bps.erase (lp_arch_info->lwp_ptid);
599b31
+
599b31
+      xfree (lp_arch_info);
599b31
+    }
599b31
+}
599b31
+
599b31
+/* Install or delete debug registers in thread LP so that it matches what
599b31
+   GDB requested before it is resumed.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
599b31
+{
599b31
+  if ((!m_dreg_interface.detected_p ())
599b31
+      || (m_dreg_interface.unavailable_p ()))
599b31
     return;
599b31
 
599b31
-  for (i = 0; VEC_iterate (thread_points_p, ppc_threads, i, p); i++)
599b31
-    if (p->tid == tid)
599b31
-      {
599b31
-	t = p;
599b31
-	break;
599b31
-      }
599b31
+  /* We have to re-install or clear the debug registers if we set the
599b31
+     stale flag.
599b31
+
599b31
+     In addition, some kernels configurations can disable a hardware
599b31
+     watchpoint after it is hit.  Usually, GDB will remove and re-install
599b31
+     a hardware watchpoint when the thread stops if "breakpoint
599b31
+     always-inserted" is off, or to single-step a watchpoint.  But so
599b31
+     that we don't rely on this behavior, if we stop due to a hardware
599b31
+     breakpoint or watchpoint, we also refresh our debug registers.  */
599b31
 
599b31
-  if (t == NULL)
599b31
+  arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
599b31
+
599b31
+  bool stale_dregs = (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
599b31
+		      || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
599b31
+		      || lp_arch_info->debug_regs_stale);
599b31
+
599b31
+  if (!stale_dregs)
599b31
     return;
599b31
 
599b31
-  VEC_unordered_remove (thread_points_p, ppc_threads, i);
599b31
+  gdb_assert (lp->ptid.lwp_p ());
599b31
+
599b31
+  auto process_it = m_process_info.find (lp->ptid.pid ());
599b31
+
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
+    {
599b31
+      /* First, delete any hardware watchpoint or breakpoint installed in
599b31
+	 the inferior and update the thread state.  */
599b31
+      auto installed_it = m_installed_hw_bps.find (lp->ptid);
599b31
+
599b31
+      if (installed_it != m_installed_hw_bps.end ())
599b31
+	{
599b31
+	  auto &bp_list = installed_it->second;
599b31
+
599b31
+	  for (auto bp_it = bp_list.begin (); bp_it != bp_list.end ();)
599b31
+	    {
599b31
+	      /* We ignore ENOENT to account for various possible kernel
599b31
+		 behaviors, e.g. the kernel might or might not copy debug
599b31
+		 registers across forks and clones, and we always copy
599b31
+		 the debug register state when fork and clone events are
599b31
+		 detected.  */
599b31
+	      if (ptrace (PPC_PTRACE_DELHWDEBUG, lp->ptid.lwp (), 0,
599b31
+			  bp_it->first) == -1)
599b31
+		if (errno != ENOENT)
599b31
+		  perror_with_name (_("Error deleting hardware "
599b31
+				      "breakpoint or watchpoint"));
599b31
+
599b31
+	      /* We erase the entries one at a time after successfuly
599b31
+		 removing the corresponding slot form the thread so that
599b31
+		 if we throw an exception above in a future iteration the
599b31
+		 map remains consistent.  */
599b31
+	      bp_it = bp_list.erase (bp_it);
599b31
+	    }
599b31
 
599b31
-  hw_breaks = t->hw_breaks;
599b31
+	  gdb_assert (bp_list.empty ());
599b31
+	}
599b31
 
599b31
-  for (i = 0; i < max_slots_number; i++)
599b31
-    if (hw_breaks[i].hw_break)
599b31
-      xfree (hw_breaks[i].hw_break);
599b31
+      /* Now we install all the requested hardware breakpoints and
599b31
+	 watchpoints and update the thread state.  */
599b31
 
599b31
-  xfree (t->hw_breaks);
599b31
-  xfree (t);
599b31
+      if (process_it != m_process_info.end ())
599b31
+	{
599b31
+	  auto &bp_list = m_installed_hw_bps[lp->ptid];
599b31
+
599b31
+	  for (ppc_hw_breakpoint bp
599b31
+		 : process_it->second.requested_hw_bps)
599b31
+	    {
599b31
+	      long slot = ptrace (PPC_PTRACE_SETHWDEBUG, lp->ptid.lwp (),
599b31
+				  0, &bp);
599b31
+
599b31
+	      if (slot < 0)
599b31
+		perror_with_name (_("Error setting hardware "
599b31
+				    "breakpoint or watchpoint"));
599b31
+
599b31
+	      /* Keep track of which slots we installed in this
599b31
+		 thread.  */
599b31
+	      bp_list.emplace (bp_list.begin (), slot, bp);
599b31
+	    }
599b31
+	}
599b31
+    }
599b31
+  else
599b31
+    {
599b31
+      gdb_assert (m_dreg_interface.debugreg_p ());
599b31
+
599b31
+      /* Passing 0 to PTRACE_SET_DEBUGREG will clear the
599b31
+	 watchpoint.  */
599b31
+      long wp = 0;
599b31
+
599b31
+      /* GDB requested a watchpoint to be installed.  */
599b31
+      if (process_it != m_process_info.end ()
599b31
+	  && process_it->second.requested_wp_val.has_value ())
599b31
+	wp = *(process_it->second.requested_wp_val);
599b31
+
599b31
+      long ret = ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (),
599b31
+			 0, wp);
599b31
+
599b31
+      if (ret == -1)
599b31
+	perror_with_name (_("Error setting hardware watchpoint"));
599b31
+    }
599b31
+
599b31
+  lp_arch_info->debug_regs_stale = false;
599b31
 }
599b31
 
599b31
+/* Return true if INFERIOR_PTID is known to have been stopped by a
599b31
+   hardware watchpoint, false otherwise.  If true is returned, write the
599b31
+   address that the kernel reported as causing the SIGTRAP in ADDR_P.  */
599b31
+
599b31
 bool
599b31
-ppc_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
599b31
+ppc_linux_nat_target::low_stopped_data_address (CORE_ADDR *addr_p)
599b31
 {
599b31
   siginfo_t siginfo;
599b31
 
599b31
@@ -2604,48 +2957,57 @@ ppc_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
599b31
       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
599b31
     return false;
599b31
 
599b31
-  if (have_ptrace_hwdebug_interface ())
599b31
+  gdb_assert (!m_dreg_interface.unavailable_p ());
599b31
+
599b31
+  /* Check if this signal corresponds to a hardware breakpoint.  We only
599b31
+     need to check this if we're using the HWDEBUG interface, since the
599b31
+     DEBUGREG interface only allows setting one hardware watchpoint.  */
599b31
+  if (m_dreg_interface.hwdebug_p ())
599b31
     {
599b31
-      int i;
599b31
-      struct thread_points *t;
599b31
-      struct hw_break_tuple *hw_breaks;
599b31
-      /* The index (or slot) of the *point is passed in the si_errno field.  */
599b31
+      /* The index (or slot) of the *point is passed in the si_errno
599b31
+	 field.  Currently, this is only the case if the kernel was
599b31
+	 configured with CONFIG_PPC_ADV_DEBUG_REGS.  If not, we assume
599b31
+	 the kernel will set si_errno to a value that doesn't correspond
599b31
+	 to any real slot.  */
599b31
       int slot = siginfo.si_errno;
599b31
 
599b31
-      t = hwdebug_find_thread_points_by_tid (inferior_ptid.lwp (), 0);
599b31
+      auto installed_it = m_installed_hw_bps.find (inferior_ptid);
599b31
 
599b31
-      /* Find out if this *point is a hardware breakpoint.
599b31
-	 If so, we should return 0.  */
599b31
-      if (t)
599b31
-	{
599b31
-	  hw_breaks = t->hw_breaks;
599b31
-	  for (i = 0; i < max_slots_number; i++)
599b31
-	   if (hw_breaks[i].hw_break && hw_breaks[i].slot == slot
599b31
-	       && hw_breaks[i].hw_break->trigger_type
599b31
-		    == PPC_BREAKPOINT_TRIGGER_EXECUTE)
599b31
-	     return false;
599b31
-	}
599b31
+      /* We must have installed slots for the thread if it got a
599b31
+	 TRAP_HWBKPT signal.  */
599b31
+      gdb_assert (installed_it != m_installed_hw_bps.end ());
599b31
+
599b31
+      for (const auto & slot_bp_pair : installed_it->second)
599b31
+	if (slot_bp_pair.first == slot
599b31
+	    && (slot_bp_pair.second.trigger_type
599b31
+		== PPC_BREAKPOINT_TRIGGER_EXECUTE))
599b31
+	  return false;
599b31
     }
599b31
 
599b31
   *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
599b31
   return true;
599b31
 }
599b31
 
599b31
+/* Return true if INFERIOR_PTID is known to have been stopped by a
599b31
+   hardware watchpoint, false otherwise.  */
599b31
+
599b31
 bool
599b31
-ppc_linux_nat_target::stopped_by_watchpoint ()
599b31
+ppc_linux_nat_target::low_stopped_by_watchpoint ()
599b31
 {
599b31
   CORE_ADDR addr;
599b31
-  return stopped_data_address (&addr);
599b31
+  return low_stopped_data_address (&addr);
599b31
 }
599b31
 
599b31
 bool
599b31
 ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
599b31
 						    CORE_ADDR start,
599b31
-						    LONGEST length)
599b31
+						    int length)
599b31
 {
599b31
+  gdb_assert (!m_dreg_interface.unavailable_p ());
599b31
+
599b31
   int mask;
599b31
 
599b31
-  if (have_ptrace_hwdebug_interface ()
599b31
+  if (m_dreg_interface.hwdebug_p ()
599b31
       && linux_get_hwcap (current_top_target ()) & PPC_FEATURE_BOOKE)
599b31
     return start <= addr && start + length >= addr;
599b31
   else if (linux_get_hwcap (current_top_target ()) & PPC_FEATURE_BOOKE)
599b31
@@ -2662,10 +3024,14 @@ ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
599b31
 /* Return the number of registers needed for a masked hardware watchpoint.  */
599b31
 
599b31
 int
599b31
-ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
599b31
+ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr,
599b31
+						  CORE_ADDR mask)
599b31
 {
599b31
-  if (!have_ptrace_hwdebug_interface ()
599b31
-	   || (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
599b31
+  m_dreg_interface.detect (inferior_ptid);
599b31
+
599b31
+  if (!m_dreg_interface.hwdebug_p ()
599b31
+      || (m_dreg_interface.hwdebug_info ().features
599b31
+	  & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
599b31
     return -1;
599b31
   else if ((mask & 0xC0000000) != 0xC0000000)
599b31
     {
599b31
@@ -2678,14 +3044,204 @@ ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask
599b31
     return 2;
599b31
 }
599b31
 
599b31
+/* Copy the per-thread debug register state, if any, from thread
599b31
+   PARENT_PTID to thread CHILD_PTID, if the debug register being used is
599b31
+   HWDEBUG.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::copy_thread_dreg_state (const ptid_t &parent_ptid,
599b31
+					      const ptid_t &child_ptid)
599b31
+{
599b31
+  gdb_assert (m_dreg_interface.hwdebug_p ());
599b31
+
599b31
+  auto installed_it = m_installed_hw_bps.find (parent_ptid);
599b31
+
599b31
+  if (installed_it != m_installed_hw_bps.end ())
599b31
+    m_installed_hw_bps[child_ptid] = m_installed_hw_bps[parent_ptid];
599b31
+}
599b31
+
599b31
+/* Mark the debug register stale flag for the new thread, if we have
599b31
+   already detected which debug register interface we use.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::mark_thread_stale (struct lwp_info *lp)
599b31
+{
599b31
+  if ((!m_dreg_interface.detected_p ())
599b31
+      || (m_dreg_interface.unavailable_p ()))
599b31
+    return;
599b31
+
599b31
+  arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
599b31
+
599b31
+  lp_arch_info->debug_regs_stale = true;
599b31
+}
599b31
+
599b31
+/* Mark all the threads of the group of PID as stale with respect to
599b31
+   debug registers and issue a stop request to each such thread that
599b31
+   isn't already stopped.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::mark_debug_registers_changed (pid_t pid)
599b31
+{
599b31
+  /* We do this in two passes to make sure all threads are marked even if
599b31
+     we get an exception when stopping one of them.  */
599b31
+
599b31
+  iterate_over_lwps_new (ptid_t (pid),
599b31
+		     [this] (struct lwp_info *lp) -> int {
599b31
+		       this->mark_thread_stale (lp);
599b31
+		       return 0;
599b31
+		     });
599b31
+
599b31
+  iterate_over_lwps_new (ptid_t (pid),
599b31
+		     [] (struct lwp_info *lp) -> int {
599b31
+		       if (!lwp_is_stopped (lp))
599b31
+			 linux_stop_lwp (lp);
599b31
+		       return 0;
599b31
+		     });
599b31
+}
599b31
+
599b31
+/* Register a hardware breakpoint or watchpoint BP for the pid PID, then
599b31
+   mark the stale flag for all threads of the group of PID, and issue a
599b31
+   stop request for them.  The breakpoint or watchpoint will be installed
599b31
+   the next time each thread is resumed.  Should only be used if the
599b31
+   debug register interface is HWDEBUG.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::register_hw_breakpoint (pid_t pid,
599b31
+					      const struct
599b31
+					      ppc_hw_breakpoint &bp)
599b31
+{
599b31
+  gdb_assert (m_dreg_interface.hwdebug_p ());
599b31
+
599b31
+  m_process_info[pid].requested_hw_bps.push_back (bp);
599b31
+
599b31
+  mark_debug_registers_changed (pid);
599b31
+}
599b31
+
599b31
+/* Clear a registration for a hardware breakpoint or watchpoint BP for
599b31
+   the pid PID, then mark the stale flag for all threads of the group of
599b31
+   PID, and issue a stop request for them.  The breakpoint or watchpoint
599b31
+   will be removed the next time each thread is resumed.  Should only be
599b31
+   used if the debug register interface is HWDEBUG.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::clear_hw_breakpoint (pid_t pid,
599b31
+					   const struct ppc_hw_breakpoint &bp)
599b31
+{
599b31
+  gdb_assert (m_dreg_interface.hwdebug_p ());
599b31
+
599b31
+  auto process_it = m_process_info.find (pid);
599b31
+
599b31
+  gdb_assert (process_it != m_process_info.end ());
599b31
+
599b31
+  auto bp_it = std::find_if (process_it->second.requested_hw_bps.begin (),
599b31
+			     process_it->second.requested_hw_bps.end (),
599b31
+			     [&bp, this]
599b31
+			     (const struct ppc_hw_breakpoint &curr)
599b31
+			     { return hwdebug_point_cmp (bp, curr); }
599b31
+			     );
599b31
+
599b31
+  /* If GDB is removing a watchpoint, it must have been inserted.  */
599b31
+  gdb_assert (bp_it != process_it->second.requested_hw_bps.end ());
599b31
+
599b31
+  process_it->second.requested_hw_bps.erase (bp_it);
599b31
+
599b31
+  mark_debug_registers_changed (pid);
599b31
+}
599b31
+
599b31
+/* Register the hardware watchpoint value WP_VALUE for the pid PID,
599b31
+   then mark the stale flag for all threads of the group of PID, and
599b31
+   issue a stop request for them.  The breakpoint or watchpoint will be
599b31
+   installed the next time each thread is resumed.  Should only be used
599b31
+   if the debug register interface is DEBUGREG.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::register_wp (pid_t pid, long wp_value)
599b31
+{
599b31
+  gdb_assert (m_dreg_interface.debugreg_p ());
599b31
+
599b31
+  /* Our other functions should have told GDB that we only have one
599b31
+     hardware watchpoint with this interface.  */
599b31
+  gdb_assert (!m_process_info[pid].requested_wp_val.has_value ());
599b31
+
599b31
+  m_process_info[pid].requested_wp_val.emplace (wp_value);
599b31
+
599b31
+  mark_debug_registers_changed (pid);
599b31
+}
599b31
+
599b31
+/* Clear the hardware watchpoint registration for the pid PID, then mark
599b31
+   the stale flag for all threads of the group of PID, and issue a stop
599b31
+   request for them.  The breakpoint or watchpoint will be installed the
599b31
+   next time each thread is resumed.  Should only be used if the debug
599b31
+   register interface is DEBUGREG.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::clear_wp (pid_t pid)
599b31
+{
599b31
+  gdb_assert (m_dreg_interface.debugreg_p ());
599b31
+
599b31
+  auto process_it = m_process_info.find (pid);
599b31
+
599b31
+  gdb_assert (process_it != m_process_info.end ());
599b31
+  gdb_assert (process_it->second.requested_wp_val.has_value ());
599b31
+
599b31
+  process_it->second.requested_wp_val.reset ();
599b31
+
599b31
+  mark_debug_registers_changed (pid);
599b31
+}
599b31
+
599b31
+/* Initialize the arch-specific thread state for LWP, if it not already
599b31
+   created.  */
599b31
+
599b31
+void
599b31
+ppc_linux_nat_target::init_arch_lwp_info (struct lwp_info *lp)
599b31
+{
599b31
+  if (lwp_arch_private_info (lp) == NULL)
599b31
+    {
599b31
+      lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
599b31
+      lwp_arch_private_info (lp)->debug_regs_stale = false;
599b31
+      lwp_arch_private_info (lp)->lwp_ptid = lp->ptid;
599b31
+    }
599b31
+}
599b31
+
599b31
+/* Get the arch-specific thread state for LWP, creating it if
599b31
+   necessary.  */
599b31
+
599b31
+arch_lwp_info *
599b31
+ppc_linux_nat_target::get_arch_lwp_info (struct lwp_info *lp)
599b31
+{
599b31
+  init_arch_lwp_info (lp);
599b31
+
599b31
+  return lwp_arch_private_info (lp);
599b31
+}
599b31
+
599b31
+/* The post-gdb-8 version of iterate_over_lwps.  */
599b31
+
599b31
+static struct lwp_info *
599b31
+iterate_over_lwps_new (ptid_t filter,
599b31
+                       gdb::function_view<iterate_over_lwps_new_ftype> callback)
599b31
+{
599b31
+  struct lwp_info *lp, *lpnext;
599b31
+
599b31
+  for (lp = lwp_list; lp; lp = lpnext)
599b31
+    {
599b31
+      lpnext = lp->next;
599b31
+
599b31
+      if (lp->ptid.matches (filter))
599b31
+        {
599b31
+          if (callback (lp) != 0)
599b31
+            return lp;
599b31
+        }
599b31
+    }
599b31
+
599b31
+  return NULL;
599b31
+}
599b31
+
599b31
 void _initialize_ppc_linux_nat ();
599b31
 void
599b31
 _initialize_ppc_linux_nat (void)
599b31
 {
599b31
   linux_target = &the_ppc_linux_nat_target;
599b31
 
599b31
-  gdb::observers::thread_exit.attach (ppc_linux_thread_exit);
599b31
-
599b31
   /* Register the target.  */
599b31
   add_inf_child_target (linux_target);
599b31
 }
599b31
diff --git a/gdb/procfs.c b/gdb/procfs.c
599b31
--- a/gdb/procfs.c
599b31
+++ b/gdb/procfs.c
599b31
@@ -3358,7 +3358,7 @@ procfs_target::remove_watchpoint (CORE_ADDR addr, int len,
599b31
 }
599b31
 
599b31
 int
599b31
-procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   /* The man page for proc(4) on Solaris 2.6 and up says that the
599b31
      system can support "thousands" of hardware watchpoints, but gives
599b31
diff --git a/gdb/remote.c b/gdb/remote.c
599b31
--- a/gdb/remote.c
599b31
+++ b/gdb/remote.c
599b31
@@ -454,7 +454,7 @@ public:
599b31
 
599b31
   bool stopped_data_address (CORE_ADDR *) override;
599b31
 
599b31
-  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, LONGEST) override;
599b31
+  bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
599b31
 
599b31
   int can_use_hw_breakpoint (enum bptype, int, int) override;
599b31
 
599b31
@@ -462,7 +462,7 @@ public:
599b31
 
599b31
   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
599b31
 
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
599b31
 
599b31
   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
599b31
 			 struct expression *) override;
599b31
@@ -10362,7 +10362,7 @@ remote_target::insert_watchpoint (CORE_ADDR addr, int len,
599b31
 
599b31
 bool
599b31
 remote_target::watchpoint_addr_within_range (CORE_ADDR addr,
599b31
-					     CORE_ADDR start, LONGEST length)
599b31
+					     CORE_ADDR start, int length)
599b31
 {
599b31
   CORE_ADDR diff = remote_address_masked (addr - start);
599b31
 
599b31
@@ -10413,7 +10413,7 @@ int remote_hw_watchpoint_length_limit = -1;
599b31
 int remote_hw_breakpoint_limit = -1;
599b31
 
599b31
 int
599b31
-remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   if (remote_hw_watchpoint_length_limit == 0)
599b31
     return 0;
599b31
diff --git a/gdb/s390-linux-nat.c b/gdb/s390-linux-nat.c
599b31
--- a/gdb/s390-linux-nat.c
599b31
+++ b/gdb/s390-linux-nat.c
599b31
@@ -122,7 +122,7 @@ public:
599b31
     override;
599b31
   int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
599b31
     override;
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
599b31
   bool have_continuable_watchpoint () override { return true; }
599b31
   bool stopped_by_watchpoint () override;
599b31
   int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
599b31
@@ -954,7 +954,7 @@ s390_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
599b31
 
599b31
 int
599b31
 s390_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr,
599b31
-						    LONGEST cnt)
599b31
+						    int cnt)
599b31
 {
599b31
   return 1;
599b31
 }
599b31
diff --git a/gdb/spu-multiarch.c b/gdb/spu-multiarch.c
599b31
--- a/gdb/spu-multiarch.c
599b31
+++ b/gdb/spu-multiarch.c
599b31
@@ -66,7 +66,7 @@ struct spu_multiarch_target final : public target_ops
599b31
 		     const gdb_byte *pattern, ULONGEST pattern_len,
599b31
 		     CORE_ADDR *found_addrp) override;
599b31
 
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
599b31
 
599b31
   struct gdbarch *thread_architecture (ptid_t) override;
599b31
 };
599b31
@@ -163,7 +163,7 @@ spu_multiarch_target::thread_architecture (ptid_t ptid)
599b31
 /* Override the to_region_ok_for_hw_watchpoint routine.  */
599b31
 
599b31
 int
599b31
-spu_multiarch_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+spu_multiarch_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   /* We cannot watch SPU local store.  */
599b31
   if (SPUADDR_SPU (addr) != -1)
599b31
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
599b31
--- a/gdb/target-delegates.c
599b31
+++ b/gdb/target-delegates.c
599b31
@@ -38,9 +38,9 @@ struct dummy_target : public target_ops
599b31
   int have_steppable_watchpoint () override;
599b31
   bool have_continuable_watchpoint () override;
599b31
   bool stopped_data_address (CORE_ADDR *arg0) override;
599b31
-  bool watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, LONGEST arg2) override;
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR arg0, LONGEST arg1) override;
599b31
-  bool can_accel_watchpoint_condition (CORE_ADDR arg0, LONGEST arg1, int arg2, struct expression *arg3) override;
599b31
+  bool watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, int arg2) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR arg0, int arg1) override;
599b31
+  bool can_accel_watchpoint_condition (CORE_ADDR arg0, int arg1, int arg2, struct expression *arg3) override;
599b31
   int masked_watch_num_registers (CORE_ADDR arg0, CORE_ADDR arg1) override;
599b31
   int can_do_single_step () override;
599b31
   bool supports_terminal_ours () override;
599b31
@@ -206,9 +206,9 @@ struct debug_target : public target_ops
599b31
   int have_steppable_watchpoint () override;
599b31
   bool have_continuable_watchpoint () override;
599b31
   bool stopped_data_address (CORE_ADDR *arg0) override;
599b31
-  bool watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, LONGEST arg2) override;
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR arg0, LONGEST arg1) override;
599b31
-  bool can_accel_watchpoint_condition (CORE_ADDR arg0, LONGEST arg1, int arg2, struct expression *arg3) override;
599b31
+  bool watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, int arg2) override;
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR arg0, int arg1) override;
599b31
+  bool can_accel_watchpoint_condition (CORE_ADDR arg0, int arg1, int arg2, struct expression *arg3) override;
599b31
   int masked_watch_num_registers (CORE_ADDR arg0, CORE_ADDR arg1) override;
599b31
   int can_do_single_step () override;
599b31
   bool supports_terminal_ours () override;
599b31
@@ -1068,19 +1068,19 @@ debug_target::stopped_data_address (CORE_ADDR *arg0)
599b31
 }
599b31
 
599b31
 bool
599b31
-target_ops::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, LONGEST arg2)
599b31
+target_ops::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, int arg2)
599b31
 {
599b31
   return this->beneath ()->watchpoint_addr_within_range (arg0, arg1, arg2);
599b31
 }
599b31
 
599b31
 bool
599b31
-dummy_target::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, LONGEST arg2)
599b31
+dummy_target::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, int arg2)
599b31
 {
599b31
   return default_watchpoint_addr_within_range (this, arg0, arg1, arg2);
599b31
 }
599b31
 
599b31
 bool
599b31
-debug_target::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, LONGEST arg2)
599b31
+debug_target::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, int arg2)
599b31
 {
599b31
   bool result;
599b31
   fprintf_unfiltered (gdb_stdlog, "-> %s->watchpoint_addr_within_range (...)\n", this->beneath ()->shortname ());
599b31
@@ -1098,19 +1098,19 @@ debug_target::watchpoint_addr_within_range (CORE_ADDR arg0, CORE_ADDR arg1, LONG
599b31
 }
599b31
 
599b31
 int
599b31
-target_ops::region_ok_for_hw_watchpoint (CORE_ADDR arg0, LONGEST arg1)
599b31
+target_ops::region_ok_for_hw_watchpoint (CORE_ADDR arg0, int arg1)
599b31
 {
599b31
   return this->beneath ()->region_ok_for_hw_watchpoint (arg0, arg1);
599b31
 }
599b31
 
599b31
 int
599b31
-dummy_target::region_ok_for_hw_watchpoint (CORE_ADDR arg0, LONGEST arg1)
599b31
+dummy_target::region_ok_for_hw_watchpoint (CORE_ADDR arg0, int arg1)
599b31
 {
599b31
   return default_region_ok_for_hw_watchpoint (this, arg0, arg1);
599b31
 }
599b31
 
599b31
 int
599b31
-debug_target::region_ok_for_hw_watchpoint (CORE_ADDR arg0, LONGEST arg1)
599b31
+debug_target::region_ok_for_hw_watchpoint (CORE_ADDR arg0, int arg1)
599b31
 {
599b31
   int result;
599b31
   fprintf_unfiltered (gdb_stdlog, "-> %s->region_ok_for_hw_watchpoint (...)\n", this->beneath ()->shortname ());
599b31
@@ -1126,19 +1126,19 @@ debug_target::region_ok_for_hw_watchpoint (CORE_ADDR arg0, LONGEST arg1)
599b31
 }
599b31
 
599b31
 bool
599b31
-target_ops::can_accel_watchpoint_condition (CORE_ADDR arg0, LONGEST arg1, int arg2, struct expression *arg3)
599b31
+target_ops::can_accel_watchpoint_condition (CORE_ADDR arg0, int arg1, int arg2, struct expression *arg3)
599b31
 {
599b31
   return this->beneath ()->can_accel_watchpoint_condition (arg0, arg1, arg2, arg3);
599b31
 }
599b31
 
599b31
 bool
599b31
-dummy_target::can_accel_watchpoint_condition (CORE_ADDR arg0, LONGEST arg1, int arg2, struct expression *arg3)
599b31
+dummy_target::can_accel_watchpoint_condition (CORE_ADDR arg0, int arg1, int arg2, struct expression *arg3)
599b31
 {
599b31
   return false;
599b31
 }
599b31
 
599b31
 bool
599b31
-debug_target::can_accel_watchpoint_condition (CORE_ADDR arg0, LONGEST arg1, int arg2, struct expression *arg3)
599b31
+debug_target::can_accel_watchpoint_condition (CORE_ADDR arg0, int arg1, int arg2, struct expression *arg3)
599b31
 {
599b31
   bool result;
599b31
   fprintf_unfiltered (gdb_stdlog, "-> %s->can_accel_watchpoint_condition (...)\n", this->beneath ()->shortname ());
599b31
diff --git a/gdb/target.h b/gdb/target.h
599b31
--- a/gdb/target.h
599b31
+++ b/gdb/target.h
599b31
@@ -557,15 +557,15 @@ struct target_ops
599b31
       TARGET_DEFAULT_RETURN (false);
599b31
     virtual bool stopped_data_address (CORE_ADDR *)
599b31
       TARGET_DEFAULT_RETURN (false);
599b31
-    virtual bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, LONGEST)
599b31
+    virtual bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int)
599b31
       TARGET_DEFAULT_FUNC (default_watchpoint_addr_within_range);
599b31
 
599b31
     /* Documentation of this routine is provided with the corresponding
599b31
        target_* macro.  */
599b31
-    virtual int region_ok_for_hw_watchpoint (CORE_ADDR, LONGEST)
599b31
+    virtual int region_ok_for_hw_watchpoint (CORE_ADDR, int)
599b31
       TARGET_DEFAULT_FUNC (default_region_ok_for_hw_watchpoint);
599b31
 
599b31
-    virtual bool can_accel_watchpoint_condition (CORE_ADDR, LONGEST, int,
599b31
+    virtual bool can_accel_watchpoint_condition (CORE_ADDR, int, int,
599b31
 						 struct expression *)
599b31
       TARGET_DEFAULT_RETURN (false);
599b31
     virtual int masked_watch_num_registers (CORE_ADDR, CORE_ADDR)
599b31
diff --git a/gdb/x86-nat.c b/gdb/x86-nat.c
599b31
--- a/gdb/x86-nat.c
599b31
+++ b/gdb/x86-nat.c
599b31
@@ -173,7 +173,7 @@ x86_remove_watchpoint (CORE_ADDR addr, int len,
599b31
    address ADDR and whose length is LEN bytes.  */
599b31
 
599b31
 int
599b31
-x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len)
599b31
+x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
599b31
 {
599b31
   struct x86_debug_reg_state *state
599b31
     = x86_debug_reg_state (inferior_ptid.pid ());
599b31
diff --git a/gdb/x86-nat.h b/gdb/x86-nat.h
599b31
--- a/gdb/x86-nat.h
599b31
+++ b/gdb/x86-nat.h
599b31
@@ -49,7 +49,7 @@ extern void x86_forget_process (pid_t pid);
599b31
    definitions.  */
599b31
 
599b31
 extern int x86_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype);
599b31
-extern int x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len);
599b31
+extern int x86_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len);
599b31
 extern int x86_stopped_by_watchpoint ();
599b31
 extern int x86_stopped_data_address (CORE_ADDR *addr_p);
599b31
 extern int x86_insert_watchpoint (CORE_ADDR addr, int len,
599b31
@@ -82,7 +82,7 @@ struct x86_nat_target : public BaseTarget
599b31
   int can_use_hw_breakpoint (enum bptype type, int cnt, int othertype) override
599b31
   { return x86_can_use_hw_breakpoint (type, cnt, othertype); }
599b31
 
599b31
-  int region_ok_for_hw_watchpoint (CORE_ADDR addr, LONGEST len) override
599b31
+  int region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) override
599b31
   { return x86_region_ok_for_hw_watchpoint (addr, len); }
599b31
 
599b31
   int insert_watchpoint (CORE_ADDR addr, int len,