This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 3/3] [GDBserver] Make Zx/zx packet handling idempotent.


This patch fixes hardware breakpoint regressions exposed by my fix for
"PR breakpoints/7143 - Watchpoint does not trigger when first set", at
https://sourceware.org/ml/gdb-patches/2014-03/msg00167.html

The testsuite caught them on Linux/x86_64, at least.  gdb.sum:

gdb.sum:

 FAIL: gdb.base/hbreak2.exp: next over recursive call
 FAIL: gdb.base/hbreak2.exp: backtrace from factorial(5.1)
 FAIL: gdb.base/hbreak2.exp: continue until exit at recursive next test

gdb.log:

 (gdb) next

 Program received signal SIGTRAP, Trace/breakpoint trap.
 factorial (value=4) at ../../../src/gdb/testsuite/gdb.base/break.c:113
 113       if (value > 1) {  /* set breakpoint 7 here */
 (gdb) FAIL: gdb.base/hbreak2.exp: next over recursive call

Actually, that patch just exposed a latent issue to "breakpoints
always-inserted off" mode, not really caused it.  After that patch,
GDB no longer removes breakpoints at each internal event, thus making
some scenarios behave like breakpoint always-inserted on.  The bug is
easy to trigger with always-inserted on.

The issue is that since the target-side breakpoint conditions support,
if the stub/server supports evaluating breakpoint conditions on the
target side, then GDB is sending duplicate Zx packets to the target
without removing them before, and GDBserver is not really expecting
that for Z packets other than Z0/z0.  E.g., with "set breakpoint
always-inserted on" and "set debug remote 1":

 (gdb) b main
 Sending packet: $m410943,1#ff...Packet received: 48
 Breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028.
 Sending packet: $Z0,410943,1#48...Packet received: OK
                 ^^^^^^^^^^^^
 (gdb) b main
 Note: breakpoint 4 also set at pc 0x410943.
 Sending packet: $m410943,1#ff...Packet received: 48
 Breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028.
 Sending packet: $Z0,410943,1#48...Packet received: OK
                 ^^^^^^^^^^^^
 (gdb) b main
 Note: breakpoints 4 and 5 also set at pc 0x410943.
 Sending packet: $m410943,1#ff...Packet received: 48
 Breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028.
 Sending packet: $Z0,410943,1#48...Packet received: OK
                 ^^^^^^^^^^^^
 (gdb) del
 Delete all breakpoints? (y or n) y
 Sending packet: $Z0,410943,1#48...Packet received: OK
 Sending packet: $Z0,410943,1#48...Packet received: OK
 Sending packet: $z0,410943,1#68...Packet received: OK

And for Z1, similarly:

 (gdb) hbreak main
 Sending packet: $m410943,1#ff...Packet received: 48
 Hardware assisted breakpoint 4 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028.
 Sending packet: $Z1,410943,1#49...Packet received: OK
                 ^^^^^^^^^^^^
 Packet Z1 (hardware-breakpoint) is supported
 (gdb) hbreak main
 Note: breakpoint 4 also set at pc 0x410943.
 Sending packet: $m410943,1#ff...Packet received: 48
 Hardware assisted breakpoint 5 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028.
 Sending packet: $Z1,410943,1#49...Packet received: OK
                 ^^^^^^^^^^^^
 (gdb) hbreak main
 Note: breakpoints 4 and 5 also set at pc 0x410943.
 Sending packet: $m410943,1#ff...Packet received: 48
 Hardware assisted breakpoint 6 at 0x410943: file ../../../src/gdb/gdbserver/server.c, line 3028.
 Sending packet: $Z1,410943,1#49...Packet received: OK
                 ^^^^^^^^^^^^
 (gdb) del
 Delete all breakpoints? (y or n) y
 Sending packet: $Z1,410943,1#49...Packet received: OK
                 ^^^^^^^^^^^^
 Sending packet: $Z1,410943,1#49...Packet received: OK
                 ^^^^^^^^^^^^
 Sending packet: $z1,410943,1#69...Packet received: OK
                 ^^^^^^^^^^^^

So GDB sent a bunch of Z1 packets, and then when finally removing the
breakpoint, only one z1 packet was sent.  On the GDBserver side (with
monitor set debug-hw-points 1), in the Z1 case, we see:

 $ ./gdbserver :9999 ./gdbserver
 Process ./gdbserver created; pid = 8629
 Listening on port 9999
 Remote debugging from host 127.0.0.1
 insert_watchpoint (addr=410943, len=1, type=instruction-execute):
	 CONTROL (DR7): 00000101          STATUS (DR6): 00000000
	 DR0: addr=0x410943, ref.count=1  DR1: addr=0x0, ref.count=0
	 DR2: addr=0x0, ref.count=0  DR3: addr=0x0, ref.count=0
 insert_watchpoint (addr=410943, len=1, type=instruction-execute):
	 CONTROL (DR7): 00000101          STATUS (DR6): 00000000
	 DR0: addr=0x410943, ref.count=2  DR1: addr=0x0, ref.count=0
	 DR2: addr=0x0, ref.count=0  DR3: addr=0x0, ref.count=0
 insert_watchpoint (addr=410943, len=1, type=instruction-execute):
	 CONTROL (DR7): 00000101          STATUS (DR6): 00000000
	 DR0: addr=0x410943, ref.count=3  DR1: addr=0x0, ref.count=0
	 DR2: addr=0x0, ref.count=0  DR3: addr=0x0, ref.count=0
 insert_watchpoint (addr=410943, len=1, type=instruction-execute):
	 CONTROL (DR7): 00000101          STATUS (DR6): 00000000
	 DR0: addr=0x410943, ref.count=4  DR1: addr=0x0, ref.count=0
	 DR2: addr=0x0, ref.count=0  DR3: addr=0x0, ref.count=0
 insert_watchpoint (addr=410943, len=1, type=instruction-execute):
	 CONTROL (DR7): 00000101          STATUS (DR6): 00000000
	 DR0: addr=0x410943, ref.count=5  DR1: addr=0x0, ref.count=0
	 DR2: addr=0x0, ref.count=0  DR3: addr=0x0, ref.count=0
 remove_watchpoint (addr=410943, len=1, type=instruction-execute):
	 CONTROL (DR7): 00000101          STATUS (DR6): 00000000
	 DR0: addr=0x410943, ref.count=4  DR1: addr=0x0, ref.count=0
	 DR2: addr=0x0, ref.count=0  DR3: addr=0x0, ref.count=0

That's one insert_watchpoint call for each Z1 packet, and then one
remove_watchpoint call for the z1 packet.  Notice how ref.count
increased for each insert_watchpoint call, and then in the end, after
GDB told GDBserver to forget about the hardware breakpoint, GDBserver
ends with the the first debug register still with ref.count=4!  IOW,
the hardware breakpoint is left armed on the target, while on the GDB
end it's gone.  If the program happens to execute 0x410943 afterwards,
then the CPU traps, GDBserver reports the trap to GDB, and GDB not
having a breakpoint set at that address anymore, reports to the user a
spurious SIGTRAP.

This is exactly what is happening in the hbreak2.exp test, though in
that case, it's a shared library event that triggers a
breakpoint_re_set, when breakpoints are still inserted (because
nowadays GDB doesn't remove breakpoints while handling internal
events), and that recreates breakpoint locations, which likewise
forces breakpoint reinsertion and Zx packet resends...

That is a lot of bogus Zx duplication that should possibly be
addressed on the GDB side.  GDB resends Zx packets because the way to
change the target-side condition, is to resend the breakpoint to the
server with the new condition.  (That's an option in the packet: e.g.,
"Z1,410943,1;X3,220027" for "hbreak main if 0".  The packets in the
examples above are shorter because the breakpoints don't have
conditions attached).  GDB doesn't remove the breakpoint first before
reinserting it because that'd be bad for non-stop, as it'd open a
window where the inferior could miss the breakpoint.  The conditions
actually haven't changed between the resends, but GDB isn't smart
enough to realize that.

(TBC, if the target doesn't support target-side conditions, then GDB
doesn't trigger these resends (init_bp_location calls
mark_breakpoint_location_modified, and that does nothing if condition
evaluation is on the host side.  The resends are caused by the
'loc->condition_changed = condition_modified.'  line.)

But, even if GDB was made smarter, GDBserver should really still
handle the resends anyway.  So target-side conditions also aren't
really to blame.  The documentation of the Z/z packets says:

 "To avoid potential problems with duplicate packets, the operations
 should be implemented in an idempotent way."

As such, we may want to fix GDB, but we should definitely fix
GDBserver.  The fix is a prerequisite for target-side conditions on
hardware breakpoints anyway (and while at it, on watchpoints too).

GDBserver indeed already treats duplicate Z0 packets in an idempotent
way.  mem-break.c has the concept of high-level and low-level
breakpoints, somewhat similar to GDB's split of breakpoints vs
breakpoint locations, and keeps track of multiple breakpoints
referencing the same address/location, for the case of an internal
GDBserver breakpoint or a tracepoint being set at the same address as
a GDB breakpoint.  But, it only allows GDB to ever contribute one
reference to a software breakpoint location.  IOW, if gdbserver sees a
Z0 packet for the same address where it already had a GDB breakpoint
set, then GDBserver won't create another high-level GDB breakpoint.

However, mem-break.c only tracks GDB Z0 breakpoints.  The same logic
should apply to all kinds of Zx packets.  Currently, gdbserver passes
down each duplicate Zx (other than Z0) request directly to the
target->insert_point routine.  The x86 watchpoint support itself
refcounts watchpoint / hw breakpoint requests, to handle overlapping
watchpoints, and save debug registers.  But that code doesn't (and
really shouldn't) handle the duplicate requests, assuming that for
each insert there will be a corresponding remove.

So the fix is to generalize mem-break.c to track all kinds of Zx
breakpoints, and filter out duplicates.  As mentioned, this ends up
adding support for target-side conditions on hardware breakpoints and
watchpoints too (though GDB itself doesn't support the latter yet).

Probably the least obvious change in the patch is that it kind of
turns the breakpoint insert/remove APIs inside out.  Before, the
target methods were only called for GDB breakpoints.  The internal
breakpoint set/delete methods inserted memory breakpoints directly
bypassing the insert/remove target methods.  That's not good when the
target should use a debug API to set software breakpoints, instead of
relying on GDBserver patching memory with breakpoint instructions, as
is the case of NTO.

Now removal/insertion of all kinds of breakpoints/watchpoints, either
internal, or from GDB, always go through the target methods.  The
insert_point/remove_point methods no longer get passed a Z packet
type, but an internal/raw breakpoint type.  They're also passed a
pointer to the raw breakpoint itself (note that's still opaque outside
mem-break.c), so that insert_memory_breakpoint /
remove_memory_breakpoint have access to the breakpoint's shadow
buffer.  I first tried passing down a new structure based on GDB's
"struct bp_target_info" (actually with that name exactly), but then
decided against it as unnecessary complication.

As software/memory breakpoints work by poking at memory, when setting
a GDB Z0 breakpoint (but not internal breakpoints, as those can assume
the conditions are already right), we need to tell the target to
prepare to access memory (which on Linux means stop threads).  If that
operation fails, we need to return error to GDB.  Seeing an error, if
this is the first breakpoint of that type that GDB tries to insert,
GDB would then assume the breakpoint type is supported, but it may
actually not be.  So we need to check whether the type is supported at
all before preparing to access memory.  And to solve that, the patch
adds a new target->supports_z_point_type method that is called before
actually trying to insert the breakpoint.

Other than that, hopefully the change is more or less obvious.

New test added that exercises the hbreak2.exp regression in a more
direct way, without relying on a breakpoint re-set happening before
main is reached.

Tested by building GDBserver for:

 aarch64-linux-gnu
 arm-linux-gnueabihf
 i686-pc-linux-gnu
 i686-w64-mingw32
 m68k-linux-gnu
 mips-linux-gnu
 mips-uclinux
 nios2-linux-gnu
 powerpc-linux-gnu
 sh-linux-gnu
 tilegx-unknown-linux-gnu
 x86_64-redhat-linux
 x86_64-w64-mingw32

And also regression tested on x86_64 Fedora 17.

gdb/gdbserver/
2014-04-23  Pedro Alves  <palves@redhat.com>

	* linux-aarch64-low.c (aarch64_insert_point)
	(aarch64_remove_point): No longer check whether the type is
	supported here.  Adjust to new interface.
	(the_low_target): Install aarch64_supports_z_point_type as
	supports_z_point_type method.
	* linux-arm-low.c (raw_bkpt_type_to_arm_hwbp_type): New function.
	(arm_linux_hw_point_initialize): Take an enum raw_bkpt_type
	instead of a Z packet char.  Adjust.
	(arm_supports_z_point_type): New function.
	(arm_insert_point, arm_remove_point): Adjust to new interface.
	(the_low_target): Install arm_supports_z_point_type.
	* linux-crisv32-low.c (cris_supports_z_point_type): New function.
	(cris_insert_point, cris_remove_point): Adjust to new interface.
	Don't check whether the type is supported here.
	(the_low_target): Install cris_supports_z_point_type.
	* linux-low.c (linux_supports_z_point_type): New function.
	(linux_insert_point, linux_remove_point): Adjust to new interface.
	* linux-low.h (struct linux_target_ops) <insert_point,
	remove_point>: Take an enum raw_bkpt_type instead of a char.  Add
	raw_breakpoint pointer parameter.
	<supports_z_point_type>: New method.
	* linux-mips-low.c (mips_supports_z_point_type): New function.
	(mips_insert_point, mips_remove_point): Adjust to new interface.
	Use mips_supports_z_point_type.
	(the_low_target): Install mips_supports_z_point_type.
	* linux-ppc-low.c (the_low_target): Install NULL as
	supports_z_point_type method.
	* linux-s390-low.c (the_low_target): Install NULL as
	supports_z_point_type method.
	* linux-sparc-low.c (the_low_target): Install NULL as
	supports_z_point_type method.
	* linux-x86-low.c (x86_supports_z_point_type): New function.
	(x86_insert_point): Adjust to new insert_point interface.  Use
	insert_memory_breakpoint.  Adjust to new
	i386_low_insert_watchpoint interface.
	(x86_remove_point): Adjust to remove_point interface.  Use
	remove_memory_breakpoint.  Adjust to new
	i386_low_remove_watchpoint interface.
	(the_low_target): Install x86_supports_z_point_type.
	* lynx-low.c (lynx_target_ops): Install NULL as
	supports_z_point_type callback.
	* nto-low.c (nto_supports_z_point_type): New.
	(nto_insert_point, nto_remove_point): Adjust to new interface.
	(nto_target_ops): Install nto_supports_z_point_type.
	* mem-break.c: Adjust intro comment.
	(struct raw_breakpoint) <raw_type, size>: New fields.
	<inserted>: Update comment.
	<shlib_disabled>: Delete field.
	(enum bkpt_type) <gdb_breakpoint>: Delete value.
	<gdb_breakpoint_Z0, gdb_breakpoint_Z1, gdb_breakpoint_Z2,
	gdb_breakpoint_Z3, gdb_breakpoint_Z4>: New values.
	(raw_bkpt_type_to_target_hw_bp_type): New function.
	(find_enabled_raw_code_breakpoint_at): New function.
	(find_raw_breakpoint_at): New type and size parameters.  Use them.
	(insert_memory_breakpoint): New function, based off
	set_raw_breakpoint_at.
	(remove_memory_breakpoint): New function.
	(set_raw_breakpoint_at): Reimplement.
	(set_breakpoint): New, based on set_breakpoint_at.
	(set_breakpoint_at): Reimplement.
	(delete_raw_breakpoint): Go through the_target->remove_point
	instead of assuming memory breakpoints.
	(find_gdb_breakpoint_at): Delete.
	(Z_packet_to_bkpt_type, Z_packet_to_raw_bkpt_type): New functions.
	(find_gdb_breakpoint): New function.
	(set_gdb_breakpoint_at): Delete.
	(z_type_supported): New function.
	(set_gdb_breakpoint_1): New function, loosely based off
	set_gdb_breakpoint_at.
	(check_gdb_bp_preconditions, set_gdb_breakpoint): New functions.
	(delete_gdb_breakpoint_at): Delete.
	(delete_gdb_breakpoint_1): New function, loosely based off
	delete_gdb_breakpoint_at.
	(delete_gdb_breakpoint): New function.
	(clear_gdb_breakpoint_conditions): Rename to ...
	(clear_breakpoint_conditions): ... this.  Don't handle a NULL
	breakpoint.
	(add_condition_to_breakpoint): Make static.
	(add_breakpoint_condition): Take a struct breakpoint pointer
	instead of an address.  Adjust.
	(gdb_condition_true_at_breakpoint): Rename to ...
	(gdb_condition_true_at_breakpoint_z_type): ... this, and add
	z_type parameter.
	(gdb_condition_true_at_breakpoint): Reimplement.
	(add_breakpoint_commands): Take a struct breakpoint pointer
	instead of an address.  Adjust.
	(gdb_no_commands_at_breakpoint): Rename to ...
	(gdb_no_commands_at_breakpoint_z_type): ... this.  Add z_type
	parameter.  Return true if no breakpoint was found.  Change debug
	output.
	(gdb_no_commands_at_breakpoint): Reimplement.
	(run_breakpoint_commands): Rename to ...
	(run_breakpoint_commands_z_type): ... this.  Add z_type parameter,
	and change return type to boolean.
	(run_breakpoint_commands): New function.
	(gdb_breakpoint_here): Also check for Z1 breakpoints.
	(uninsert_raw_breakpoint): Don't try to reinsert a disabled
	breakpoint.  Go through the_target->remove_point instead of
	assuming memory breakpoint.
	(uninsert_breakpoints_at, uninsert_all_breakpoints): Uninsert
	software and hardware breakpoints.
	(reinsert_raw_breakpoint): Go through the_target->insert_point
	instead of assuming memory breakpoint.
	(reinsert_breakpoints_at, reinsert_all_breakpoints): Reinsert
	software and hardware breakpoints.
	(check_breakpoints, breakpoint_here, breakpoint_inserted_here):
	Check both software and hardware breakpoints.
	(validate_inserted_breakpoint): Assert the breakpoint is a
	software breakpoint.  Set the inserted flag to -1 instead of
	setting shlib_disabled.
	(delete_disabled_breakpoints): Adjust.
	(validate_breakpoints): Only validate software breakpoints.
	Adjust to inserted flag change.
	(check_mem_read, check_mem_write): Skip breakpoint types other
	than software breakpoints.  Adjust to inserted flag change.
	* mem-break.h (enum raw_bkpt_type): New enum.
	(raw_breakpoint, struct process_info): Forward declare.
	(Z_packet_to_target_hw_bp_type): Delete declaration.
	(raw_bkpt_type_to_target_hw_bp_type, Z_packet_to_raw_bkpt_type)
	(set_gdb_breakpoint, delete_gdb_breakpoint)
	(clear_breakpoint_conditions): New declarations.
	(set_gdb_breakpoint_at, clear_gdb_breakpoint_conditions): Delete.
	(breakpoint_inserted_here): Update comment.
	(add_breakpoint_condition, add_breakpoint_commands): Replace
	address parameter with a breakpoint pointer parameter.
	(gdb_breakpoint_here): Update comment.
	(delete_gdb_breakpoint_at): Delete.
	(insert_memory_breakpoint, remove_memory_breakpoint): Declare.
	* server.c (process_point_options): Take a struct breakpoint
	pointer instead of an address.  Adjust.
	(process_serial_event) <Z/z packets>: Use set_gdb_breakpoint and
	delete_gdb_breakpoint.
	* spu-low.c (spu_target_ops): Install NULL as
	supports_z_point_type method.
	* target.h: Include mem-break.h.
	(struct target_ops) <prepare_to_access_memory>: Update comment.
	<supports_z_point_type>: New field.
	<insert_point, remove_point>: Take an enum raw_bkpt_type argument
	instead of a char.  Also take a raw breakpoint pointer.
	* win32-arm-low.c (the_low_target): Install NULL as
	supports_z_point_type.
	* win32-i386-low.c (i386_supports_z_point_type): New function.
	(i386_insert_point, i386_remove_point): Adjust to new interface.
	(the_low_target): Install i386_supports_z_point_type.
	* win32-low.c (win32_supports_z_point_type): New function.
	(win32_insert_point, win32_remove_point): Adjust to new interface.
	(win32_target_ops): Install win32_supports_z_point_type.
	* win32-low.h (struct win32_target_ops):
	<supports_z_point_type>: New method.
	<insert_point, remove_point>: Take an enum raw_bkpt_type argument
	instead of a char.  Also take a raw breakpoint pointer.

gdb/testsuite/
	* gdb.base/break-idempotent.c: New file.
	* gdb.base/break-idempotent.exp: New file.
---
 gdb/gdbserver/linux-aarch64-low.c           |  21 +-
 gdb/gdbserver/linux-arm-low.c               |  73 +--
 gdb/gdbserver/linux-crisv32-low.c           |  63 ++-
 gdb/gdbserver/linux-low.c                   |  18 +-
 gdb/gdbserver/linux-low.h                   |   8 +-
 gdb/gdbserver/linux-mips-low.c              |  51 +-
 gdb/gdbserver/linux-ppc-low.c               |   1 +
 gdb/gdbserver/linux-s390-low.c              |   1 +
 gdb/gdbserver/linux-sparc-low.c             |   1 +
 gdb/gdbserver/linux-x86-low.c               |  76 +--
 gdb/gdbserver/lynx-low.c                    |   1 +
 gdb/gdbserver/mem-break.c                   | 728 +++++++++++++++++++++-------
 gdb/gdbserver/mem-break.h                   |  79 ++-
 gdb/gdbserver/nto-low.c                     |  49 +-
 gdb/gdbserver/server.c                      |  57 +--
 gdb/gdbserver/spu-low.c                     |   1 +
 gdb/gdbserver/target.h                      |  23 +-
 gdb/gdbserver/win32-arm-low.c               |   1 +
 gdb/gdbserver/win32-i386-low.c              |  42 +-
 gdb/gdbserver/win32-low.c                   |  18 +-
 gdb/gdbserver/win32-low.h                   |   7 +-
 gdb/testsuite/gdb.base/break-idempotent.c   |  52 ++
 gdb/testsuite/gdb.base/break-idempotent.exp | 179 +++++++
 23 files changed, 1139 insertions(+), 411 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/break-idempotent.c
 create mode 100644 gdb/testsuite/gdb.base/break-idempotent.exp

diff --git a/gdb/gdbserver/linux-aarch64-low.c b/gdb/gdbserver/linux-aarch64-low.c
index 7e425c9..033e79e 100644
--- a/gdb/gdbserver/linux-aarch64-low.c
+++ b/gdb/gdbserver/linux-aarch64-low.c
@@ -966,20 +966,18 @@ aarch64_supports_z_point_type (char z_type)
    Return -1 if an error occurs.  */
 
 static int
-aarch64_insert_point (char type, CORE_ADDR addr, int len)
+aarch64_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		      int len, struct raw_breakpoint *bp)
 {
   int ret;
   enum target_hw_bp_type targ_type;
 
-  if (!aarch64_supports_z_point_type (type))
-    return 1;
-
   if (debug_hw_points)
     fprintf (stderr, "insert_point on entry (addr=0x%08lx, len=%d)\n",
 	     (unsigned long) addr, len);
 
-  /* Determine the type from the packet.  */
-  targ_type = Z_packet_to_target_hw_bp_type (type);
+  /* Determine the type from the raw breakpoint type.  */
+  targ_type = raw_bkpt_type_to_target_hw_bp_type (type);
 
   if (targ_type != hw_execute)
     ret =
@@ -1004,20 +1002,18 @@ aarch64_insert_point (char type, CORE_ADDR addr, int len)
    Return -1 if an error occurs.  */
 
 static int
-aarch64_remove_point (char type, CORE_ADDR addr, int len)
+aarch64_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		      int len, struct raw_breakpoint *bp)
 {
   int ret;
   enum target_hw_bp_type targ_type;
 
-  if (!aarch64_supports_z_point_type (type))
-    return 1;
-
   if (debug_hw_points)
     fprintf (stderr, "remove_point on entry (addr=0x%08lx, len=%d)\n",
 	     (unsigned long) addr, len);
 
-  /* Determine the type from the packet.  */
-  targ_type = Z_packet_to_target_hw_bp_type (type);
+  /* Determine the type from the raw breakpoint type.  */
+  targ_type = raw_bkpt_type_to_target_hw_bp_type (type);
 
   /* Set up state pointers.  */
   if (targ_type != hw_execute)
@@ -1289,6 +1285,7 @@ struct linux_target_ops the_low_target =
   NULL,
   0,
   aarch64_breakpoint_at,
+  aarch64_supports_z_point_type,
   aarch64_insert_point,
   aarch64_remove_point,
   aarch64_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/linux-arm-low.c b/gdb/gdbserver/linux-arm-low.c
index d7813ab..c4cfbd4 100644
--- a/gdb/gdbserver/linux-arm-low.c
+++ b/gdb/gdbserver/linux-arm-low.c
@@ -439,43 +439,39 @@ arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
   return p1->address == p2->address && p1->control == p2->control;
 }
 
+/* Convert a raw breakpoint type to an enum arm_hwbp_type.  */
+
+static int
+raw_bkpt_type_to_arm_hwbp_type (enum raw_bkpt_type raw_type)
+{
+  switch (raw_type)
+    {
+    case raw_bkpt_type_hw:
+      return arm_hwbp_break;
+    case raw_bkpt_type_write_wp:
+      return arm_hwbp_store;
+    case raw_bkpt_type_read_wp:
+      return arm_hwbp_load;
+    case raw_bkpt_type_access_wp:
+      return arm_hwbp_access;
+    default:
+      gdb_assert_not_reached ("unhandled raw type");
+    }
+}
+
 /* Initialize the hardware breakpoint structure P for a breakpoint or
    watchpoint at ADDR to LEN.  The type of watchpoint is given in TYPE.
    Returns -1 if TYPE is unsupported, or -2 if the particular combination
    of ADDR and LEN cannot be implemented.  Otherwise, returns 0 if TYPE
    represents a breakpoint and 1 if type represents a watchpoint.  */
 static int
-arm_linux_hw_point_initialize (char type, CORE_ADDR addr, int len,
-			       struct arm_linux_hw_breakpoint *p)
+arm_linux_hw_point_initialize (enum raw_bkpt_type raw_type, CORE_ADDR addr,
+			       int len, struct arm_linux_hw_breakpoint *p)
 {
   arm_hwbp_type hwbp_type;
   unsigned mask;
 
-  /* Breakpoint/watchpoint types (GDB terminology):
-     0 = memory breakpoint for instructions
-     (not supported; done via memory write instead)
-     1 = hardware breakpoint for instructions (supported)
-     2 = write watchpoint (supported)
-     3 = read watchpoint (supported)
-     4 = access watchpoint (supported).  */
-  switch (type)
-    {
-    case '1':
-      hwbp_type = arm_hwbp_break;
-      break;
-    case '2':
-      hwbp_type = arm_hwbp_store;
-      break;
-    case '3':
-      hwbp_type = arm_hwbp_load;
-      break;
-    case '4':
-      hwbp_type = arm_hwbp_access;
-      break;
-    default:
-      /* Unsupported.  */
-      return -1;
-    }
+  hwbp_type = raw_bkpt_type_to_arm_hwbp_type (raw_type);
 
   if (hwbp_type == arm_hwbp_break)
     {
@@ -559,9 +555,26 @@ update_registers_callback (struct inferior_list_entry *entry, void *arg)
   return 0;
 }
 
+static int
+arm_supports_z_point_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_HW_BP:
+    case Z_PACKET_WRITE_WP:
+    case Z_PACKET_READ_WP:
+    case Z_PACKET_ACCESS_WP:
+      return 1;
+    default:
+      /* Leave the handling of sw breakpoints with the gdb client.  */
+      return 0;
+    }
+}
+
 /* Insert hardware break-/watchpoint.  */
 static int
-arm_insert_point (char type, CORE_ADDR addr, int len)
+arm_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		  int len, struct raw_breakpoint *bp)
 {
   struct process_info *proc = current_process ();
   struct arm_linux_hw_breakpoint p, *pts;
@@ -600,7 +613,8 @@ arm_insert_point (char type, CORE_ADDR addr, int len)
 
 /* Remove hardware break-/watchpoint.  */
 static int
-arm_remove_point (char type, CORE_ADDR addr, int len)
+arm_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		  int len, struct raw_breakpoint *bp)
 {
   struct process_info *proc = current_process ();
   struct arm_linux_hw_breakpoint p, *pts;
@@ -896,6 +910,7 @@ struct linux_target_ops the_low_target = {
   arm_reinsert_addr,
   0,
   arm_breakpoint_at,
+  arm_supports_z_point_type,
   arm_insert_point,
   arm_remove_point,
   arm_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/linux-crisv32-low.c b/gdb/gdbserver/linux-crisv32-low.c
index a16a656..203b1ba 100644
--- a/gdb/gdbserver/linux-crisv32-low.c
+++ b/gdb/gdbserver/linux-crisv32-low.c
@@ -143,7 +143,22 @@ cris_write_data_breakpoint (struct regcache *regcache,
 }
 
 static int
-cris_insert_point (char type, CORE_ADDR addr, int len)
+cris_supports_z_point_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_WRITE_WP:
+    case Z_PACKET_READ_WP:
+    case Z_PACKET_ACCESS_WP:
+      return 1;
+    default:
+      return 0;
+    }
+}
+
+static int
+cris_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		   int len, struct raw_breakpoint *bp)
 {
   int bp;
   unsigned long bp_ctrl;
@@ -151,26 +166,12 @@ cris_insert_point (char type, CORE_ADDR addr, int len)
   unsigned long ccs;
   struct regcache *regcache;
 
-  /* Breakpoint/watchpoint types (GDB terminology):
-     0 = memory breakpoint for instructions
-     (not supported; done via memory write instead)
-     1 = hardware breakpoint for instructions (not supported)
-     2 = write watchpoint (supported)
-     3 = read watchpoint (supported)
-     4 = access watchpoint (supported).  */
-
-  if (type < '2' || type > '4')
-    {
-      /* Unsupported.  */
-      return 1;
-    }
-
   regcache = get_thread_regcache (current_inferior, 1);
 
   /* Read watchpoints are set as access watchpoints, because of GDB's
      inability to deal with pure read watchpoints.  */
-  if (type == '3')
-    type = '4';
+  if (type == raw_bkpt_type_read_wp)
+    type = raw_bkpt_type_access_wp;
 
   /* Get the configuration register.  */
   collect_register_by_name (regcache, "s0", &bp_ctrl);
@@ -199,12 +200,12 @@ cris_insert_point (char type, CORE_ADDR addr, int len)
     }
 
   /* Configure the control register first.  */
-  if (type == '3' || type == '4')
+  if (type == raw_bkpt_type_read_wp || type == raw_bkpt_type_access_wp)
     {
       /* Trigger on read.  */
       bp_ctrl |= (1 << (2 + bp * 4));
     }
-  if (type == '2' || type == '4')
+  if (type == raw_bkpt_type_write_wp || type == raw_bkpt_type_access_wp)
     {
       /* Trigger on write.  */
       bp_ctrl |= (2 << (2 + bp * 4));
@@ -229,7 +230,8 @@ cris_insert_point (char type, CORE_ADDR addr, int len)
 }
 
 static int
-cris_remove_point (char type, CORE_ADDR addr, int len)
+cris_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, int len,
+		   struct raw_breakpoint *bp)
 {
   int bp;
   unsigned long bp_ctrl;
@@ -237,22 +239,12 @@ cris_remove_point (char type, CORE_ADDR addr, int len)
   struct regcache *regcache;
   unsigned long bp_d_regs[12];
 
-  /* Breakpoint/watchpoint types:
-     0 = memory breakpoint for instructions
-     (not supported; done via memory write instead)
-     1 = hardware breakpoint for instructions (not supported)
-     2 = write watchpoint (supported)
-     3 = read watchpoint (supported)
-     4 = access watchpoint (supported).  */
-  if (type < '2' || type > '4')
-    return -1;
-
   regcache = get_thread_regcache (current_inferior, 1);
 
   /* Read watchpoints are set as access watchpoints, because of GDB's
      inability to deal with pure read watchpoints.  */
-  if (type == '3')
-    type = '4';
+  if (type == raw_bkpt_type_read_wp)
+    type = raw_bkpt_type_access_wp;
 
   /* Get the configuration register.  */
   collect_register_by_name (regcache, "s0", &bp_ctrl);
@@ -289,9 +281,9 @@ cris_remove_point (char type, CORE_ADDR addr, int len)
 	/* Read/write bits for this BP.  */
 	rw_bits = (bp_ctrl & (0x3 << bitpos)) >> bitpos;
 
-	if ((type == '3' && rw_bits == 0x1)
-	    || (type == '2' && rw_bits == 0x2)
-	    || (type == '4' && rw_bits == 0x3))
+	if ((type == raw_bkpt_type_read_wp && rw_bits == 0x1)
+	    || (type == raw_bkpt_type_write_wp && rw_bits == 0x2)
+	    || (type == raw_bkpt_type_access_wp && rw_bits == 0x3))
 	  {
 	    /* Read/write matched.  */
 	    break;
@@ -433,6 +425,7 @@ struct linux_target_ops the_low_target = {
   cris_reinsert_addr,
   0,
   cris_breakpoint_at,
+  cris_supports_z_point_type,
   cris_insert_point,
   cris_remove_point,
   cris_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index c847c62..5c721ba 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -4852,20 +4852,29 @@ linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
    corresponding function.  */
 
 static int
-linux_insert_point (char type, CORE_ADDR addr, int len)
+linux_supports_z_point_type (char z_type)
+{
+  return (the_low_target.supports_z_point_type != NULL
+	  && the_low_target.supports_z_point_type (z_type));
+}
+
+static int
+linux_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		    int size, struct raw_breakpoint *bp)
 {
   if (the_low_target.insert_point != NULL)
-    return the_low_target.insert_point (type, addr, len);
+    return the_low_target.insert_point (type, addr, size, bp);
   else
     /* Unsupported (see target.h).  */
     return 1;
 }
 
 static int
-linux_remove_point (char type, CORE_ADDR addr, int len)
+linux_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		    int size, struct raw_breakpoint *bp)
 {
   if (the_low_target.remove_point != NULL)
-    return the_low_target.remove_point (type, addr, len);
+    return the_low_target.remove_point (type, addr, size, bp);
   else
     /* Unsupported (see target.h).  */
     return 1;
@@ -5994,6 +6003,7 @@ static struct target_ops linux_target_ops = {
   linux_look_up_symbols,
   linux_request_interrupt,
   linux_read_auxv,
+  linux_supports_z_point_type,
   linux_insert_point,
   linux_remove_point,
   linux_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/linux-low.h b/gdb/gdbserver/linux-low.h
index 7459710..64b6950 100644
--- a/gdb/gdbserver/linux-low.h
+++ b/gdb/gdbserver/linux-low.h
@@ -153,8 +153,12 @@ struct linux_target_ops
 
   /* Breakpoint and watchpoint related functions.  See target.h for
      comments.  */
-  int (*insert_point) (char type, CORE_ADDR addr, int len);
-  int (*remove_point) (char type, CORE_ADDR addr, int len);
+  int (*supports_z_point_type) (char z_type);
+  int (*insert_point) (enum raw_bkpt_type type, CORE_ADDR addr,
+		       int size, struct raw_breakpoint *bp);
+  int (*remove_point) (enum raw_bkpt_type type, CORE_ADDR addr,
+		       int size, struct raw_breakpoint *bp);
+
   int (*stopped_by_watchpoint) (void);
   CORE_ADDR (*stopped_data_address) (void);
 
diff --git a/gdb/gdbserver/linux-mips-low.c b/gdb/gdbserver/linux-mips-low.c
index 112448c..320e2b1 100644
--- a/gdb/gdbserver/linux-mips-low.c
+++ b/gdb/gdbserver/linux-mips-low.c
@@ -372,11 +372,26 @@ mips_linux_prepare_to_resume (struct lwp_info *lwp)
     }
 }
 
+static int
+mips_supports_z_point_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_WRITE_WP:
+    case Z_PACKET_READ_WP:
+    case Z_PACKET_ACCESS_WP:
+      return 1;
+    default:
+      return 0;
+    }
+}
+
 /* This is the implementation of linux_target_ops method
    insert_point.  */
 
 static int
-mips_insert_point (char type, CORE_ADDR addr, int len)
+mips_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		   int len, struct raw_breakpoint *bp)
 {
   struct process_info *proc = current_process ();
   struct arch_process_info *private = proc->private->arch_private;
@@ -388,19 +403,6 @@ mips_insert_point (char type, CORE_ADDR addr, int len)
   enum target_hw_bp_type watch_type;
   uint32_t irw;
 
-  /* Breakpoint/watchpoint types:
-       '0' - software-breakpoint (not supported)
-       '1' - hardware-breakpoint (not supported)
-       '2' - write watchpoint (supported)
-       '3' - read watchpoint (supported)
-       '4' - access watchpoint (supported).  */
-
-  if (type < '2' || type > '4')
-    {
-      /* Unsupported.  */
-      return 1;
-    }
-
   lwpid = lwpid_of (current_inferior);
   if (!mips_linux_read_watch_registers (lwpid,
 					&private->watch_readback,
@@ -416,7 +418,7 @@ mips_insert_point (char type, CORE_ADDR addr, int len)
   mips_linux_watch_populate_regs (private->current_watches, &regs);
 
   /* Now try to add the new watch.  */
-  watch_type = Z_packet_to_target_hw_bp_type (type);
+  watch_type = raw_bkpt_type_to_target_hw_bp_type (type);
   irw = mips_linux_watch_type_to_irw (watch_type);
   if (!mips_linux_watch_try_one_watch (&regs, addr, len, irw))
     return -1;
@@ -446,7 +448,8 @@ mips_insert_point (char type, CORE_ADDR addr, int len)
    remove_point.  */
 
 static int
-mips_remove_point (char type, CORE_ADDR addr, int len)
+mips_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		   int len, struct raw_breakpoint *bp)
 {
   struct process_info *proc = current_process ();
   struct arch_process_info *private = proc->private->arch_private;
@@ -458,21 +461,8 @@ mips_remove_point (char type, CORE_ADDR addr, int len)
   struct mips_watchpoint **pw;
   struct mips_watchpoint *w;
 
-  /* Breakpoint/watchpoint types:
-       '0' - software-breakpoint (not supported)
-       '1' - hardware-breakpoint (not supported)
-       '2' - write watchpoint (supported)
-       '3' - read watchpoint (supported)
-       '4' - access watchpoint (supported).  */
-
-  if (type < '2' || type > '4')
-    {
-      /* Unsupported.  */
-      return 1;
-    }
-
   /* Search for a known watch that matches.  Then unlink and free it.  */
-  watch_type = Z_packet_to_target_hw_bp_type (type);
+  watch_type = raw_bkpt_type_to_target_hw_bp_type (type);
   deleted_one = 0;
   pw = &private->current_watches;
   while ((w = *pw))
@@ -844,6 +834,7 @@ struct linux_target_ops the_low_target = {
   mips_reinsert_addr,
   0,
   mips_breakpoint_at,
+  mips_supports_z_point_type,
   mips_insert_point,
   mips_remove_point,
   mips_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/linux-ppc-low.c b/gdb/gdbserver/linux-ppc-low.c
index 63d4218..d743311 100644
--- a/gdb/gdbserver/linux-ppc-low.c
+++ b/gdb/gdbserver/linux-ppc-low.c
@@ -693,6 +693,7 @@ struct linux_target_ops the_low_target = {
   NULL,
   0,
   ppc_breakpoint_at,
+  NULL, /* supports_z_point_type */
   NULL,
   NULL,
   NULL,
diff --git a/gdb/gdbserver/linux-s390-low.c b/gdb/gdbserver/linux-s390-low.c
index fd1b0c9..c891a6d 100644
--- a/gdb/gdbserver/linux-s390-low.c
+++ b/gdb/gdbserver/linux-s390-low.c
@@ -583,6 +583,7 @@ struct linux_target_ops the_low_target = {
   NULL,
   s390_breakpoint_len,
   s390_breakpoint_at,
+  NULL,  /* supports_z_point_type */
   NULL,
   NULL,
   NULL,
diff --git a/gdb/gdbserver/linux-sparc-low.c b/gdb/gdbserver/linux-sparc-low.c
index 75c97dd..f2fc8cb 100644
--- a/gdb/gdbserver/linux-sparc-low.c
+++ b/gdb/gdbserver/linux-sparc-low.c
@@ -328,6 +328,7 @@ struct linux_target_ops the_low_target = {
   sparc_reinsert_addr,
   0,
   sparc_breakpoint_at,
+  NULL,  /* supports_z_point_type */
   NULL, NULL, NULL, NULL,
   NULL, NULL
 };
diff --git a/gdb/gdbserver/linux-x86-low.c b/gdb/gdbserver/linux-x86-low.c
index ab68572..ac0b4ee 100644
--- a/gdb/gdbserver/linux-x86-low.c
+++ b/gdb/gdbserver/linux-x86-low.c
@@ -621,32 +621,41 @@ i386_dr_low_get_status (void)
 /* Breakpoint/Watchpoint support.  */
 
 static int
-x86_insert_point (char type, CORE_ADDR addr, int len)
+x86_supports_z_point_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_SW_BP:
+    case Z_PACKET_HW_BP:
+    case Z_PACKET_WRITE_WP:
+    case Z_PACKET_ACCESS_WP:
+      return 1;
+    default:
+      return 0;
+    }
+}
+
+static int
+x86_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		  int size, struct raw_breakpoint *bp)
 {
   struct process_info *proc = current_process ();
+
   switch (type)
     {
-    case '0': /* software-breakpoint */
-      {
-	int ret;
-
-	ret = prepare_to_access_memory ();
-	if (ret)
-	  return -1;
-	ret = set_gdb_breakpoint_at (addr);
-	done_accessing_memory ();
-	return ret;
-      }
-    case '1': /* hardware-breakpoint */
-    case '2': /* write watchpoint */
-    case '3': /* read watchpoint */
-    case '4': /* access watchpoint */
+    case raw_bkpt_type_sw:
+      return insert_memory_breakpoint (bp);
+
+    case raw_bkpt_type_hw:
+    case raw_bkpt_type_write_wp:
+    case raw_bkpt_type_access_wp:
       {
-	enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
+	enum target_hw_bp_type hw_type
+	  = raw_bkpt_type_to_target_hw_bp_type (type);
 	struct i386_debug_reg_state *state
 	  = &proc->private->arch_private->debug_reg_state;
 
-	return i386_low_insert_watchpoint (state, hw_type, addr, len);
+	return i386_low_insert_watchpoint (state, hw_type, addr, size);
       }
 
     default:
@@ -656,32 +665,26 @@ x86_insert_point (char type, CORE_ADDR addr, int len)
 }
 
 static int
-x86_remove_point (char type, CORE_ADDR addr, int len)
+x86_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		  int size, struct raw_breakpoint *bp)
 {
   struct process_info *proc = current_process ();
+
   switch (type)
     {
-    case '0': /* software-breakpoint */
-      {
-	int ret;
-
-	ret = prepare_to_access_memory ();
-	if (ret)
-	  return -1;
-	ret = delete_gdb_breakpoint_at (addr);
-	done_accessing_memory ();
-	return ret;
-      }
-    case '1': /* hardware-breakpoint */
-    case '2': /* write watchpoint */
-    case '3': /* read watchpoint */
-    case '4': /* access watchpoint */
+    case raw_bkpt_type_sw:
+      return remove_memory_breakpoint (bp);
+
+    case raw_bkpt_type_hw:
+    case raw_bkpt_type_write_wp:
+    case raw_bkpt_type_access_wp:
       {
-	enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
+	enum target_hw_bp_type hw_type
+	  = raw_bkpt_type_to_target_hw_bp_type (type);
 	struct i386_debug_reg_state *state
 	  = &proc->private->arch_private->debug_reg_state;
 
-	return i386_low_remove_watchpoint (state, hw_type, addr, len);
+	return i386_low_remove_watchpoint (state, hw_type, addr, size);
       }
     default:
       /* Unsupported.  */
@@ -3367,6 +3370,7 @@ struct linux_target_ops the_low_target =
   NULL,
   1,
   x86_breakpoint_at,
+  x86_supports_z_point_type,
   x86_insert_point,
   x86_remove_point,
   x86_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/lynx-low.c b/gdb/gdbserver/lynx-low.c
index ce3ff6e..0b0ff47 100644
--- a/gdb/gdbserver/lynx-low.c
+++ b/gdb/gdbserver/lynx-low.c
@@ -736,6 +736,7 @@ static struct target_ops lynx_target_ops = {
   NULL,  /* look_up_symbols */
   lynx_request_interrupt,
   NULL,  /* read_auxv */
+  NULL,  /* supports_z_point_type */
   NULL,  /* insert_point */
   NULL,  /* remove_point */
   NULL,  /* stopped_by_watchpoint */
diff --git a/gdb/gdbserver/mem-break.c b/gdb/gdbserver/mem-break.c
index b19cbc8..daeb521 100644
--- a/gdb/gdbserver/mem-break.c
+++ b/gdb/gdbserver/mem-break.c
@@ -29,12 +29,31 @@ int breakpoint_len;
 #define MAX_BREAKPOINT_LEN 8
 
 /* GDB will never try to install multiple breakpoints at the same
-   address.  But, we need to keep track of internal breakpoints too,
-   and so we do need to be able to install multiple breakpoints at the
-   same address transparently.  We keep track of two different, and
-   closely related structures.  A raw breakpoint, which manages the
-   low level, close to the metal aspect of a breakpoint.  It holds the
-   breakpoint address, and a buffer holding a copy of the instructions
+   address.  However, we can see GDB requesting to insert a breakpoint
+   at an address is had already inserted one previously in a few
+   situations.
+
+   - The RSP documentation on Z packets says that to avoid potential
+   problems with duplicate packets, the operations should be
+   implemented in an idempotent way.
+
+   - A breakpoint is set at ADDR, an address in a shared library.
+   Then the shared library is unloaded.  And then another, unrelated,
+   breakpoint at ADDR is set.  There is not breakpoint removal request
+   between the first and the second breakpoint.
+
+   - When GDB wants to update the target-side breakpoint conditions or
+   commands, it re-inserts the breakpoint, with updated
+   conditions/commands associated.
+
+   Also, we need to keep track of internal breakpoints too, so we do
+   need to be able to install multiple breakpoints at the same address
+   transparently.
+
+   We keep track of two different, and closely related structures.  A
+   raw breakpoint, which manages the low level, close to the metal
+   aspect of a breakpoint.  It holds the breakpoint address, and for
+   software breakpoints, a buffer holding a copy of the instructions
    that would be in memory had not been a breakpoint there (we call
    that the shadow memory of the breakpoint).  We occasionally need to
    temporarilly uninsert a breakpoint without the client knowing about
@@ -53,6 +72,10 @@ struct raw_breakpoint
 {
   struct raw_breakpoint *next;
 
+  /* The low level type of the breakpoint (software breakpoint,
+     watchpoint, etc.)  */
+  enum raw_bkpt_type raw_type;
+
   /* A reference count.  Each high level breakpoint referencing this
      raw breakpoint accounts for one reference.  */
   int refcount;
@@ -61,23 +84,35 @@ struct raw_breakpoint
      breakpoint for a given PC.  */
   CORE_ADDR pc;
 
+  /* The breakpoint's size.  */
+  int size;
+
   /* The breakpoint's shadow memory.  */
   unsigned char old_data[MAX_BREAKPOINT_LEN];
 
-  /* Non-zero if this breakpoint is currently inserted in the
-     inferior.  */
+  /* Positive if this breakpoint is currently inserted in the
+     inferior.  Negative if it was, but we've detected that it's now
+     gone.  Zero if not inserted.  */
   int inserted;
-
-  /* Non-zero if this breakpoint is currently disabled because we no
-     longer detect it as inserted.  */
-  int shlib_disabled;
 };
 
 /* The type of a breakpoint.  */
 enum bkpt_type
   {
     /* A GDB breakpoint, requested with a Z0 packet.  */
-    gdb_breakpoint,
+    gdb_breakpoint_Z0,
+
+    /* A GDB hardware breakpoint, requested with a Z1 packet.  */
+    gdb_breakpoint_Z1,
+
+    /* A GDB write watchpoint, requested with a Z2 packet.  */
+    gdb_breakpoint_Z2,
+
+    /* A GDB read watchpoint, requested with a Z3 packet.  */
+    gdb_breakpoint_Z3,
+
+    /* A GDB access watchpoint, requested with a Z4 packet.  */
+    gdb_breakpoint_Z4,
 
     /* A basic-software-single-step breakpoint.  */
     reinsert_breakpoint,
@@ -138,21 +173,55 @@ struct breakpoint
   int (*handler) (CORE_ADDR);
 };
 
+/* See mem-break.h.  */
+
 enum target_hw_bp_type
-Z_packet_to_target_hw_bp_type (char type)
+raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
 {
-  switch (type)
+  switch (raw_type)
     {
-    case Z_PACKET_HW_BP:
+    case raw_bkpt_type_hw:
       return hw_execute;
-    case Z_PACKET_WRITE_WP:
+    case raw_bkpt_type_write_wp:
       return hw_write;
-    case Z_PACKET_READ_WP:
+    case raw_bkpt_type_read_wp:
       return hw_read;
-    case Z_PACKET_ACCESS_WP:
+    case raw_bkpt_type_access_wp:
       return hw_access;
     default:
-      fatal ("bad watchpoint type %c", type);
+      fatal ("bad raw breakpoing type %d", (int) raw_type);
+    }
+}
+
+/* See mem-break.h.  */
+
+static enum bkpt_type
+Z_packet_to_bkpt_type (char z_type)
+{
+  gdb_assert ('0' <= z_type && z_type <= '4');
+
+  return gdb_breakpoint_Z0 + (z_type - '0');
+}
+
+/* See mem-break.h.  */
+
+enum raw_bkpt_type
+Z_packet_to_raw_bkpt_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_SW_BP:
+      return raw_bkpt_type_sw;
+    case Z_PACKET_HW_BP:
+      return raw_bkpt_type_hw;
+    case Z_PACKET_WRITE_WP:
+      return raw_bkpt_type_write_wp;
+    case Z_PACKET_READ_WP:
+      return raw_bkpt_type_read_wp;
+    case Z_PACKET_ACCESS_WP:
+      return raw_bkpt_type_access_wp;
+    default:
+      gdb_assert_not_reached ("unhandled Z packet type.");
     }
 }
 
@@ -173,69 +242,169 @@ any_persistent_commands ()
   return 0;
 }
 
+/* Find low-level breakpoint of type TYPE at address ADDR that is not
+   insert-disabled.  Returns NULL if not found.  */
+
 static struct raw_breakpoint *
-find_raw_breakpoint_at (CORE_ADDR where)
+find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
 {
   struct process_info *proc = current_process ();
   struct raw_breakpoint *bp;
 
   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
-    if (bp->pc == where)
+    if (bp->pc == addr
+	&& bp->raw_type == type
+	&& bp->inserted >= 0)
       return bp;
 
   return NULL;
 }
 
+/* Find low-level breakpoint of type TYPE at address ADDR.  Returns
+   NULL if not found.  */
+
 static struct raw_breakpoint *
-set_raw_breakpoint_at (CORE_ADDR where)
+find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int size)
 {
   struct process_info *proc = current_process ();
   struct raw_breakpoint *bp;
-  int err;
+
+  for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
+    if (bp->pc == addr && bp->raw_type == type && bp->size == size)
+      return bp;
+
+  return NULL;
+}
+
+/* See mem-break.h.  */
+
+int
+insert_memory_breakpoint (struct raw_breakpoint *bp)
+{
   unsigned char buf[MAX_BREAKPOINT_LEN];
+  int err;
 
   if (breakpoint_data == NULL)
-    error ("Target does not support breakpoints.");
+    return 1;
 
-  bp = find_raw_breakpoint_at (where);
-  if (bp != NULL)
+  /* If the architecture treats the size field of Z packets as a
+     'kind' field, then we'll need to be able to know which is the
+     breakpoint instruction too.  */
+  if (bp->size != breakpoint_len)
     {
-      bp->refcount++;
-      return bp;
+      if (debug_threads)
+	debug_printf ("Don't know how to insert breakpoints of size %d.\n",
+		      bp->size);
+      return -1;
     }
 
-  bp = xcalloc (1, sizeof (*bp));
-  bp->pc = where;
-  bp->refcount = 1;
-
   /* Note that there can be fast tracepoint jumps installed in the
      same memory range, so to get at the original memory, we need to
      use read_inferior_memory, which masks those out.  */
-  err = read_inferior_memory (where, buf, breakpoint_len);
+  err = read_inferior_memory (bp->pc, buf, breakpoint_len);
   if (err != 0)
     {
       if (debug_threads)
 	debug_printf ("Failed to read shadow memory of"
 		      " breakpoint at 0x%s (%s).\n",
-		      paddress (where), strerror (err));
-      free (bp);
-      return NULL;
+		      paddress (bp->pc), strerror (err));
     }
-  memcpy (bp->old_data, buf, breakpoint_len);
+  else
+    {
+      memcpy (bp->old_data, buf, breakpoint_len);
 
-  err = (*the_target->write_memory) (where, breakpoint_data,
-				     breakpoint_len);
+      err = (*the_target->write_memory) (bp->pc, breakpoint_data,
+					 breakpoint_len);
+      if (err != 0)
+	{
+	  if (debug_threads)
+	    debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
+			  paddress (bp->pc), strerror (err));
+	}
+    }
+  return err != 0 ? -1 : 0;
+}
+
+/* See mem-break.h  */
+
+int
+remove_memory_breakpoint (struct raw_breakpoint *bp)
+{
+  unsigned char buf[MAX_BREAKPOINT_LEN];
+  int err;
+
+  /* Since there can be trap breakpoints inserted in the same address
+     range, we use `write_inferior_memory', which takes care of
+     layering breakpoints on top of fast tracepoints, and on top of
+     the buffer we pass it.  This works because the caller has already
+     either unlinked the breakpoint or marked it uninserted.  Also
+     note that we need to pass the current shadow contents, because
+     write_inferior_memory updates any shadow memory with what we pass
+     here, and we want that to be a nop.  */
+  memcpy (buf, bp->old_data, breakpoint_len);
+  err = write_inferior_memory (bp->pc, buf, breakpoint_len);
   if (err != 0)
     {
       if (debug_threads)
-	debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
-		      paddress (where), strerror (err));
+	debug_printf ("Failed to uninsert raw breakpoint "
+		      "at 0x%s (%s) while deleting it.\n",
+		      paddress (bp->pc), strerror (err));
+    }
+  return err != 0 ? -1 : 0;
+}
+
+/* Set a RAW breakpoint of type TYPE and size SIZE at WHERE.  On
+   success, a pointer to the new breakpoint is returned.  On failure,
+   returns NULL and writes the error code to *ERR.  */
+
+static struct raw_breakpoint *
+set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int size,
+		       int *err)
+{
+  struct process_info *proc = current_process ();
+  struct raw_breakpoint *bp;
+
+  if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
+    {
+      bp = find_enabled_raw_code_breakpoint_at (where, type);
+      if (bp != NULL && bp->size != size)
+	{
+	  /* A different size than previously seen.  The previous
+	     breakpoint must be gone then.  */
+	  if (debug_threads)
+	    debug_printf ("Inconsistent breakpoint size?  Was %d, now %d.\n",
+			  bp->size, size);
+	  bp->inserted = -1;
+	  bp = NULL;
+	}
+    }
+  else
+    bp = find_raw_breakpoint_at (where, type, size);
+
+  if (bp != NULL)
+    {
+      bp->refcount++;
+      return bp;
+    }
+
+  bp = xcalloc (1, sizeof (*bp));
+  bp->pc = where;
+  bp->size = size;
+  bp->refcount = 1;
+  bp->raw_type = type;
+
+  *err = the_target->insert_point (bp->raw_type, bp->pc, bp->size, bp);
+  if (*err != 0)
+    {
+      if (debug_threads)
+	debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
+		      paddress (where), *err);
       free (bp);
       return NULL;
     }
 
-  /* Link the breakpoint in.  */
   bp->inserted = 1;
+  /* Link the breakpoint in.  */
   bp->next = proc->raw_breakpoints;
   proc->raw_breakpoints = bp;
   return bp;
@@ -545,14 +714,23 @@ reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
     }
 }
 
-struct breakpoint *
-set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
+/* Set a high-level breakpoint of type TYPE, with low level type
+   RAW_TYPE and size SIZE, at WHERE.  On success, a pointer to the new
+   breakpoint is returned.  On failure, returns NULL and writes the
+   error code to *ERR.  HANDLER is called when the breakpoint is hit.
+   HANDLER should return 1 if the breakpoint should be deleted, 0
+   otherwise.  */
+
+static struct breakpoint *
+set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
+		CORE_ADDR where, int size,
+		int (*handler) (CORE_ADDR), int *err)
 {
   struct process_info *proc = current_process ();
   struct breakpoint *bp;
   struct raw_breakpoint *raw;
 
-  raw = set_raw_breakpoint_at (where);
+  raw = set_raw_breakpoint_at (raw_type, where, size, err);
 
   if (raw == NULL)
     {
@@ -561,7 +739,7 @@ set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
     }
 
   bp = xcalloc (1, sizeof (struct breakpoint));
-  bp->type = other_breakpoint;
+  bp->type = type;
 
   bp->raw = raw;
   bp->handler = handler;
@@ -572,6 +750,19 @@ set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
   return bp;
 }
 
+/* See mem-break.h  */
+
+struct breakpoint *
+set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
+{
+  int err_ignored;
+
+  return set_breakpoint (other_breakpoint, raw_bkpt_type_sw,
+			 where, breakpoint_len, handler,
+			 &err_ignored);
+}
+
+
 static int
 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
 {
@@ -585,24 +776,14 @@ delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
     {
       if (bp == todel)
 	{
-	  if (bp->inserted)
+	  if (bp->inserted > 0)
 	    {
 	      struct raw_breakpoint *prev_bp_link = *bp_link;
-	      unsigned char buf[MAX_BREAKPOINT_LEN];
 
 	      *bp_link = bp->next;
 
-	      /* Since there can be trap breakpoints inserted in the
-		 same address range, we use `write_inferior_memory',
-		 which takes care of layering breakpoints on top of
-		 fast tracepoints, and on top of the buffer we pass
-		 it.  This works because we've already unlinked the
-		 fast tracepoint jump above.  Also note that we need
-		 to pass the current shadow contents, because
-		 write_inferior_memory updates any shadow memory with
-		 what we pass here, and we want that to be a nop.  */
-	      memcpy (buf, bp->old_data, breakpoint_len);
-	      ret = write_inferior_memory (bp->pc, buf, breakpoint_len);
+	      ret = the_target->remove_point (bp->raw_type, bp->pc, bp->size,
+					      bp);
 	      if (ret != 0)
 		{
 		  /* Something went wrong, relink the breakpoint.  */
@@ -610,11 +791,10 @@ delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
 
 		  if (debug_threads)
 		    debug_printf ("Failed to uninsert raw breakpoint "
-				  "at 0x%s (%s) while deleting it.\n",
-				  paddress (bp->pc), strerror (ret));
+				  "at 0x%s while deleting it.\n",
+				  paddress (bp->pc));
 		  return ret;
 		}
-
 	    }
 	  else
 	    *bp_link = bp->next;
@@ -694,88 +874,225 @@ delete_breakpoint (struct breakpoint *todel)
   return delete_breakpoint_1 (proc, todel);
 }
 
-/* Locate a breakpoint placed at address WHERE and return a pointer
-   to its structure.  */
+/* Locate a GDB breakpoint of type Z_TYPE and size SIZE placed at
+   address ADDR and return a pointer to its structure.  If SIZE is -1,
+   the breakpoints' sizes are ignored.  */
 
 static struct breakpoint *
-find_gdb_breakpoint_at (CORE_ADDR where)
+find_gdb_breakpoint (char z_type, CORE_ADDR addr, int size)
 {
   struct process_info *proc = current_process ();
   struct breakpoint *bp;
+  enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
 
   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
-    if (bp->type == gdb_breakpoint && bp->raw->pc == where)
+    if (bp->type == type && bp->raw->pc == addr
+	&& (size == -1 || bp->raw->size == size))
       return bp;
 
   return NULL;
 }
 
-int
-set_gdb_breakpoint_at (CORE_ADDR where)
+static int
+z_type_supported (char z_type)
+{
+  return (z_type >= '0' && z_type <= '4'
+	  && the_target->supports_z_point_type (z_type));
+}
+
+/* Create a new GDB breakpoint of type Z_TYPE at ADDR with size SIZE.
+   Returns a pointer to the newly created breakpoint on success.  On
+   failure returns NULL and sets *ERR to either -1 for error, or 1 if
+   Z_TYPE breakpoints are not supported on this target.  */
+
+static struct breakpoint *
+set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int size, int *err)
 {
   struct breakpoint *bp;
+  enum bkpt_type type;
+  enum raw_bkpt_type raw_type;
+
+  /* If we see GDB inserting a second code breakpoint at the same
+     address, then either: GDB is updating the breakpoint's conditions
+     or commands; or, the first breakpoint must have disappeared due
+     to a shared library unload.  On targets where the shared
+     libraries are handled by userspace, like SVR4, for example,
+     GDBserver can't tell if a library was loaded or unloaded.  Since
+     we refcount raw breakpoints, we must be careful to make sure GDB
+     breakpoints never contribute more than one reference.  if we
+     didn't do this, in case the previous breakpoint is gone due to a
+     shared library unload, we'd just increase the refcount of the
+     previous breakpoint at this address, but the trap was not planted
+     in the inferior anymore, thus the breakpoint would never be hit.
+     Note this must be careful to not create a window where
+     breakpoints are removed from the target, for non-stop, in case
+     the target can poke at memory while the program is running.  */
+  if (z_type == Z_PACKET_SW_BP
+      || z_type == Z_PACKET_HW_BP)
+    {
+      bp = find_gdb_breakpoint (z_type, addr, -1);
 
-  if (breakpoint_data == NULL)
-    return 1;
+      if (bp != NULL)
+	{
+	  if (bp->raw->size != size)
+	    {
+	      /* A different size than previously seen.  The previous
+		 breakpoint must be gone then.  */
+	      bp->raw->inserted = -1;
+	      delete_breakpoint (bp);
+	      bp = NULL;
+	    }
+	  else if (z_type == Z_PACKET_SW_BP)
+	    {
+	      /* Check if the breakpoint is actually gone from the
+		 target, due to an solib unload, for example.  Might
+		 as well validate _all_ breakpoints.  */
+	      validate_breakpoints ();
+
+	      /* Breakpoints that don't pass validation are
+		 deleted.  */
+	      bp = find_gdb_breakpoint (z_type, addr, -1);
+	    }
+	}
+    }
+  else
+    {
+      /* Data breakpoints for the same address but different size are
+	 expected.  GDB doesn't merge these.  The backend gets to do
+	 that if it wants/can.  */
+      bp = find_gdb_breakpoint (z_type, addr, size);
+    }
 
-  /* If we see GDB inserting a second breakpoint at the same address,
-     then the first breakpoint must have disappeared due to a shared
-     library unload.  On targets where the shared libraries are
-     handled by userspace, like SVR4, for example, GDBserver can't
-     tell if a library was loaded or unloaded.  Since we refcount
-     breakpoints, if we didn't do this, we'd just increase the
-     refcount of the previous breakpoint at this address, but the trap
-     was not planted in the inferior anymore, thus the breakpoint
-     would never be hit.  */
-  bp = find_gdb_breakpoint_at (where);
   if (bp != NULL)
     {
-      delete_gdb_breakpoint_at (where);
+      /* We already know about this breakpoint, there's nothing else
+	 to do - GDB's reference is already accounted for.  Note that
+	 whether the breakpoint inserted is left as is - we may be
+	 stepping over it, for example, in which case we don't want to
+	 force-reinsert it.  */
+      return bp;
+    }
+
+  raw_type = Z_packet_to_raw_bkpt_type (z_type);
+  type = Z_packet_to_bkpt_type (z_type);
+  return set_breakpoint (type, raw_type, addr, size, NULL, err);
+}
+
+static int
+check_gdb_bp_preconditions (char z_type, int *err)
+{
+  /* As software/memory breakpoints work by poking at memory, we need
+     to prepare to access memory.  If that operation fails, we need to
+     return error.  Seeing an error, if this is the first breakpoint
+     of that type that GDB tries to insert, GDB would then assume the
+     breakpoint type is supported, but it may actually not be.  So we
+     need to check whether the type is supported at all before
+     preparing to access memory.  */
+  if (!z_type_supported (z_type))
+    {
+      *err = 1;
+      return 0;
+    }
+  else if (current_inferior == NULL)
+    {
+      *err = -1;
+      return 0;
+    }
+  else
+    return 1;
+}
+
+/* See mem-break.h.  This is a wrapper for set_gdb_breakpoint_1 that
+   knows to prepare to access memory for Z0 breakpoints.  */
 
-      /* Might as well validate all other breakpoints.  */
-      validate_breakpoints ();
+struct breakpoint *
+set_gdb_breakpoint (char z_type, CORE_ADDR addr, int size, int *err)
+{
+  struct breakpoint *bp;
+
+  if (!check_gdb_bp_preconditions (z_type, err))
+    return NULL;
+
+  /* If inserting a software/memory breakpoint, need to prepare to
+     access memory.  */
+  if (z_type == Z_PACKET_SW_BP)
+    {
+      *err = prepare_to_access_memory ();
+      if (*err != 0)
+	return NULL;
     }
 
-  bp = set_breakpoint_at (where, NULL);
-  if (bp == NULL)
-    return -1;
+  bp = set_gdb_breakpoint_1 (z_type, addr, size, err);
 
-  bp->type = gdb_breakpoint;
-  return 0;
+  if (z_type == Z_PACKET_SW_BP)
+    done_accessing_memory ();
+
+  return bp;
 }
 
-int
-delete_gdb_breakpoint_at (CORE_ADDR addr)
+/* Delete a GDB breakpoint of type Z_TYPE and size SIZE previously
+   inserted at ADDR with set_gdb_breakpoint_at.  Returns 0 on success,
+   -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
+   target.  */
+
+static int
+delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int size)
 {
   struct breakpoint *bp;
   int err;
 
-  if (breakpoint_data == NULL)
-    return 1;
-
-  bp = find_gdb_breakpoint_at (addr);
+  bp = find_gdb_breakpoint (z_type, addr, size);
   if (bp == NULL)
     return -1;
 
   /* Before deleting the breakpoint, make sure to free
      its condition list.  */
-  clear_gdb_breakpoint_conditions (addr);
+  clear_breakpoint_conditions (bp);
   err = delete_breakpoint (bp);
-  if (err)
+  if (err != 0)
     return -1;
 
   return 0;
 }
 
-/* Clear all conditions associated with this breakpoint address.  */
+/* See mem-break.h.  This is a wrapper for delete_gdb_breakpoint that
+   knows to prepare to access memory for Z0 breakpoints.  */
+
+int
+delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int size)
+{
+  int ret;
+
+  if (!check_gdb_bp_preconditions (z_type, &ret))
+    return ret;
+
+  /* If inserting a software/memory breakpoint, need to prepare to
+     access memory.  */
+  if (z_type == Z_PACKET_SW_BP)
+    {
+      int err;
+
+      err = prepare_to_access_memory ();
+      if (err != 0)
+	return -1;
+    }
+
+  ret = delete_gdb_breakpoint_1 (z_type, addr, size);
+
+  if (z_type == Z_PACKET_SW_BP)
+    done_accessing_memory ();
+
+  return ret;
+}
+
+/* Clear all conditions associated with a breakpoint.  */
 
 void
-clear_gdb_breakpoint_conditions (CORE_ADDR addr)
+clear_breakpoint_conditions (struct breakpoint *bp)
 {
-  struct breakpoint *bp = find_gdb_breakpoint_at (addr);
   struct point_cond_list *cond;
 
-  if (bp == NULL || bp->cond_list == NULL)
+  if (bp->cond_list == NULL)
     return;
 
   cond = bp->cond_list;
@@ -796,7 +1113,7 @@ clear_gdb_breakpoint_conditions (CORE_ADDR addr)
 
 /* Add condition CONDITION to GDBserver's breakpoint BP.  */
 
-void
+static void
 add_condition_to_breakpoint (struct breakpoint *bp,
 			     struct agent_expr *condition)
 {
@@ -811,12 +1128,11 @@ add_condition_to_breakpoint (struct breakpoint *bp,
   bp->cond_list = new_cond;
 }
 
-/* Add a target-side condition CONDITION to the breakpoint at ADDR.  */
+/* Add a target-side condition CONDITION to a breakpoint.  */
 
 int
-add_breakpoint_condition (CORE_ADDR addr, char **condition)
+add_breakpoint_condition (struct breakpoint *bp, char **condition)
 {
-  struct breakpoint *bp = find_gdb_breakpoint_at (addr);
   char *actparm = *condition;
   struct agent_expr *cond;
 
@@ -845,11 +1161,11 @@ add_breakpoint_condition (CORE_ADDR addr, char **condition)
 /* Evaluate condition (if any) at breakpoint BP.  Return 1 if
    true and 0 otherwise.  */
 
-int
-gdb_condition_true_at_breakpoint (CORE_ADDR where)
+static int
+gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
 {
   /* Fetch registers for the current inferior.  */
-  struct breakpoint *bp = find_gdb_breakpoint_at (where);
+  struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
   ULONGEST value = 0;
   struct point_cond_list *cl;
   int err = 0;
@@ -885,6 +1201,14 @@ gdb_condition_true_at_breakpoint (CORE_ADDR where)
   return (value != 0);
 }
 
+int
+gdb_condition_true_at_breakpoint (CORE_ADDR where)
+{
+  /* Only check code (software or hardware) breakpoints.  */
+  return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
+	  || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
+}
+
 /* Add commands COMMANDS to GDBserver's breakpoint BP.  */
 
 void
@@ -906,9 +1230,9 @@ add_commands_to_breakpoint (struct breakpoint *bp,
 /* Add a target-side command COMMAND to the breakpoint at ADDR.  */
 
 int
-add_breakpoint_commands (CORE_ADDR addr, char **command, int persist)
+add_breakpoint_commands (struct breakpoint *bp, char **command,
+			 int persist)
 {
-  struct breakpoint *bp = find_gdb_breakpoint_at (addr);
   char *actparm = *command;
   struct agent_expr *cmd;
 
@@ -936,33 +1260,48 @@ add_breakpoint_commands (CORE_ADDR addr, char **command, int persist)
 
 /* Return true if there are no commands to run at this location,
    which likely means we want to report back to GDB.  */
-int
-gdb_no_commands_at_breakpoint (CORE_ADDR where)
+
+static int
+gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
 {
-  struct breakpoint *bp = find_gdb_breakpoint_at (where);
+  struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
 
   if (bp == NULL)
-    return 0;
+    return 1;
 
   if (debug_threads)
-    debug_printf ("at 0x%s, bp command_list is 0x%s\n",
-		  paddress (where),
+    debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
+		  paddress (addr), z_type,
 		  phex_nz ((uintptr_t) bp->command_list, 0));
   return (bp->command_list == NULL);
 }
 
-void
-run_breakpoint_commands (CORE_ADDR where)
+/* Return true if there are no commands to run at this location,
+   which likely means we want to report back to GDB.  */
+
+int
+gdb_no_commands_at_breakpoint (CORE_ADDR where)
+{
+  /* Only check code (software or hardware) breakpoints.  */
+  return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
+	  && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
+}
+
+/* Run a breakpoint's commands.  Returns 0 if there was a problem
+   running any command, 1 otherwise.  */
+
+static int
+run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
 {
   /* Fetch registers for the current inferior.  */
-  struct breakpoint *bp = find_gdb_breakpoint_at (where);
+  struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
   ULONGEST value = 0;
   struct point_command_list *cl;
   int err = 0;
   struct eval_agent_expr_context ctx;
 
   if (bp == NULL)
-    return;
+    return 1;
 
   ctx.regcache = get_thread_regcache (current_inferior, 1);
   ctx.tframe = NULL;
@@ -976,17 +1315,29 @@ run_breakpoint_commands (CORE_ADDR where)
 
       /* If one command has a problem, stop digging the hole deeper.  */
       if (err)
-	break;
+	return 0;
     }
+
+  return 1;
 }
 
-/* Return 1 if there is a breakpoint inserted in address WHERE
-   and if its condition, if it exists, is true.  */
+void
+run_breakpoint_commands (CORE_ADDR where)
+{
+  /* Only check code (software or hardware) breakpoints.  If one
+     command has a problem, stop digging the hole deeper.  */
+  if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
+    run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
+}
+
+/* See mem-break.h.  */
 
 int
 gdb_breakpoint_here (CORE_ADDR where)
 {
-  return (find_gdb_breakpoint_at (where) != NULL);
+  /* Only check code (software or hardware) breakpoints.  */
+  return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
+	  || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
 }
 
 void
@@ -1026,29 +1377,26 @@ delete_reinsert_breakpoints (void)
 static void
 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
 {
-  if (bp->inserted)
+  if (bp->inserted < 0)
+    {
+      if (debug_threads)
+	debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
+		      paddress (bp->pc));
+    }
+  else if (bp->inserted > 0)
     {
       int err;
-      unsigned char buf[MAX_BREAKPOINT_LEN];
 
       bp->inserted = 0;
-      /* Since there can be fast tracepoint jumps inserted in the same
-	 address range, we use `write_inferior_memory', which takes
-	 care of layering breakpoints on top of fast tracepoints, and
-	 on top of the buffer we pass it.  This works because we've
-	 already unlinked the fast tracepoint jump above.  Also note
-	 that we need to pass the current shadow contents, because
-	 write_inferior_memory updates any shadow memory with what we
-	 pass here, and we want that to be a nop.  */
-      memcpy (buf, bp->old_data, breakpoint_len);
-      err = write_inferior_memory (bp->pc, buf, breakpoint_len);
+
+      err = the_target->remove_point (bp->raw_type, bp->pc, bp->size, bp);
       if (err != 0)
 	{
 	  bp->inserted = 1;
 
 	  if (debug_threads)
-	    debug_printf ("Failed to uninsert raw breakpoint at 0x%s (%s).\n",
-			  paddress (bp->pc), strerror (err));
+	    debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
+			  paddress (bp->pc));
 	}
     }
 }
@@ -1056,10 +1404,22 @@ uninsert_raw_breakpoint (struct raw_breakpoint *bp)
 void
 uninsert_breakpoints_at (CORE_ADDR pc)
 {
+  struct process_info *proc = current_process ();
   struct raw_breakpoint *bp;
+  int found = 0;
 
-  bp = find_raw_breakpoint_at (pc);
-  if (bp == NULL)
+  for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
+    if ((bp->raw_type == raw_bkpt_type_sw
+	 || bp->raw_type == raw_bkpt_type_hw)
+	&& bp->pc == pc)
+      {
+	found = 1;
+
+	if (bp->inserted)
+	  uninsert_raw_breakpoint (bp);
+      }
+
+  if (!found)
     {
       /* This can happen when we remove all breakpoints while handling
 	 a step-over.  */
@@ -1067,11 +1427,7 @@ uninsert_breakpoints_at (CORE_ADDR pc)
 	debug_printf ("Could not find breakpoint at 0x%s "
 		      "in list (uninserting).\n",
 		      paddress (pc));
-      return;
     }
-
-  if (bp->inserted)
-    uninsert_raw_breakpoint (bp);
 }
 
 void
@@ -1081,7 +1437,9 @@ uninsert_all_breakpoints (void)
   struct raw_breakpoint *bp;
 
   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
-    if (bp->inserted)
+    if ((bp->raw_type == raw_bkpt_type_sw
+	 || bp->raw_type == raw_bkpt_type_hw)
+	&& bp->inserted)
       uninsert_raw_breakpoint (bp);
 }
 
@@ -1093,22 +1451,32 @@ reinsert_raw_breakpoint (struct raw_breakpoint *bp)
   if (bp->inserted)
     error ("Breakpoint already inserted at reinsert time.");
 
-  err = (*the_target->write_memory) (bp->pc, breakpoint_data,
-				     breakpoint_len);
+  err = the_target->insert_point (bp->raw_type, bp->pc, bp->size, bp);
   if (err == 0)
     bp->inserted = 1;
   else if (debug_threads)
-    debug_printf ("Failed to reinsert breakpoint at 0x%s (%s).\n",
-		  paddress (bp->pc), strerror (err));
+    debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
+		  paddress (bp->pc), err);
 }
 
 void
 reinsert_breakpoints_at (CORE_ADDR pc)
 {
+  struct process_info *proc = current_process ();
   struct raw_breakpoint *bp;
+  int found = 0;
 
-  bp = find_raw_breakpoint_at (pc);
-  if (bp == NULL)
+  for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
+    if ((bp->raw_type == raw_bkpt_type_sw
+	 || bp->raw_type == raw_bkpt_type_hw)
+	&& bp->pc == pc)
+      {
+	found = 1;
+
+	reinsert_raw_breakpoint (bp);
+      }
+
+  if (!found)
     {
       /* This can happen when we remove all breakpoints while handling
 	 a step-over.  */
@@ -1116,10 +1484,7 @@ reinsert_breakpoints_at (CORE_ADDR pc)
 	debug_printf ("Could not find raw breakpoint at 0x%s "
 		      "in list (reinserting).\n",
 		      paddress (pc));
-      return;
     }
-
-  reinsert_raw_breakpoint (bp);
 }
 
 void
@@ -1129,7 +1494,9 @@ reinsert_all_breakpoints (void)
   struct raw_breakpoint *bp;
 
   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
-    if (!bp->inserted)
+    if ((bp->raw_type == raw_bkpt_type_sw
+	 || bp->raw_type == raw_bkpt_type_hw)
+	&& !bp->inserted)
       reinsert_raw_breakpoint (bp);
 }
 
@@ -1144,9 +1511,13 @@ check_breakpoints (CORE_ADDR stop_pc)
 
   while (bp)
     {
-      if (bp->raw->pc == stop_pc)
+      struct raw_breakpoint *raw = bp->raw;
+
+      if ((raw->raw_type == raw_bkpt_type_sw
+	   || raw->raw_type == raw_bkpt_type_hw)
+	  && raw->pc == stop_pc)
 	{
-	  if (!bp->raw->inserted)
+	  if (!raw->inserted)
 	    {
 	      warning ("Hit a removed breakpoint?");
 	      return;
@@ -1178,17 +1549,32 @@ set_breakpoint_data (const unsigned char *bp_data, int bp_len)
 int
 breakpoint_here (CORE_ADDR addr)
 {
-  return (find_raw_breakpoint_at (addr) != NULL);
+  struct process_info *proc = current_process ();
+  struct raw_breakpoint *bp;
+
+  for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
+    if ((bp->raw_type == raw_bkpt_type_sw
+	 || bp->raw_type == raw_bkpt_type_hw)
+	&& bp->pc == addr)
+      return 1;
+
+  return 0;
 }
 
 int
 breakpoint_inserted_here (CORE_ADDR addr)
 {
+  struct process_info *proc = current_process ();
   struct raw_breakpoint *bp;
 
-  bp = find_raw_breakpoint_at (addr);
+  for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
+    if ((bp->raw_type == raw_bkpt_type_sw
+	 || bp->raw_type == raw_bkpt_type_hw)
+	&& bp->pc == addr
+	&& bp->inserted)
+      return 1;
 
-  return (bp != NULL && bp->inserted);
+  return 0;
 }
 
 static int
@@ -1198,14 +1584,14 @@ validate_inserted_breakpoint (struct raw_breakpoint *bp)
   int err;
 
   gdb_assert (bp->inserted);
+  gdb_assert (bp->raw_type == raw_bkpt_type_sw);
 
   buf = alloca (breakpoint_len);
   err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
   if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
     {
       /* Tag it as gone.  */
-      bp->inserted = 0;
-      bp->shlib_disabled = 1;
+      bp->inserted = -1;
       return 0;
     }
 
@@ -1221,7 +1607,7 @@ delete_disabled_breakpoints (void)
   for (bp = proc->breakpoints; bp != NULL; bp = next)
     {
       next = bp->next;
-      if (bp->raw->shlib_disabled)
+      if (bp->raw->inserted < 0)
 	delete_breakpoint_1 (proc, bp);
     }
 }
@@ -1241,8 +1627,10 @@ validate_breakpoints (void)
 
   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
     {
-      if (bp->raw->inserted)
-	validate_inserted_breakpoint (bp->raw);
+      struct raw_breakpoint *raw = bp->raw;
+
+      if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
+	validate_inserted_breakpoint (raw);
     }
 
   delete_disabled_breakpoints ();
@@ -1295,6 +1683,9 @@ check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
       CORE_ADDR start, end;
       int copy_offset, copy_len, buf_offset;
 
+      if (bp->raw_type != raw_bkpt_type_sw)
+	continue;
+
       gdb_assert (bp->old_data >= buf + mem_len
 		  || buf >= &bp->old_data[sizeof (bp->old_data)]);
 
@@ -1315,7 +1706,7 @@ check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
       copy_offset = start - bp->pc;
       buf_offset = start - mem_addr;
 
-      if (bp->inserted)
+      if (bp->inserted > 0)
 	{
 	  if (validate_inserted_breakpoint (bp))
 	    memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
@@ -1381,6 +1772,9 @@ check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
       CORE_ADDR start, end;
       int copy_offset, copy_len, buf_offset;
 
+      if (bp->raw_type != raw_bkpt_type_sw)
+	continue;
+
       gdb_assert (bp->old_data >= myaddr + mem_len
 		  || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
 
@@ -1402,7 +1796,7 @@ check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
       buf_offset = start - mem_addr;
 
       memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
-      if (bp->inserted)
+      if (bp->inserted > 0)
 	{
 	  if (validate_inserted_breakpoint (bp))
 	    memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
diff --git a/gdb/gdbserver/mem-break.h b/gdb/gdbserver/mem-break.h
index ffc1b80..2649462 100644
--- a/gdb/gdbserver/mem-break.h
+++ b/gdb/gdbserver/mem-break.h
@@ -26,6 +26,8 @@
 /* Breakpoints are opaque.  */
 struct breakpoint;
 struct fast_tracepoint_jump;
+struct raw_breakpoint;
+struct process_info;
 
 #define Z_PACKET_SW_BP '0'
 #define Z_PACKET_HW_BP '1'
@@ -33,41 +35,78 @@ struct fast_tracepoint_jump;
 #define Z_PACKET_READ_WP '3'
 #define Z_PACKET_ACCESS_WP '4'
 
-/* Map the protocol breakpoint/watchpoint type TYPE to enum
-   target_hw_bp_type.  */
+/* The low level breakpoint types.  */
 
-enum target_hw_bp_type Z_packet_to_target_hw_bp_type (char type);
+enum raw_bkpt_type
+  {
+    /* Software/memory breakpoint.  */
+    raw_bkpt_type_sw,
 
-/* Create a new GDB breakpoint at WHERE.  Returns -1 if breakpoints
-   are not supported on this target, 0 otherwise.  */
+    /* Hardware-assisted breakpoint.  */
+    raw_bkpt_type_hw,
 
-int set_gdb_breakpoint_at (CORE_ADDR where);
+    /* Hardware-assisted write watchpoint.  */
+    raw_bkpt_type_write_wp,
 
-/* Returns TRUE if there's any breakpoint at ADDR in our tables,
-   inserted, or not.  */
+    /* Hardware-assisted read watchpoint.  */
+    raw_bkpt_type_read_wp,
+
+    /* Hardware-assisted access watchpoint.  */
+    raw_bkpt_type_access_wp
+  };
+
+/* Map the protocol breakpoint/watchpoint type Z_TYPE to the internal
+   raw breakpoint type.  */
+
+enum raw_bkpt_type Z_packet_to_raw_bkpt_type (char z_type);
+
+/* Map a raw breakpoint type to an enum target_hw_bp_type.  */
+
+enum target_hw_bp_type raw_bkpt_type_to_target_hw_bp_type
+  (enum raw_bkpt_type raw_type);
+
+/* Create a new GDB breakpoint of type Z_TYPE at ADDR with size SIZE.
+   Returns a pointer to the newly created breakpoint on success.  On
+   failure returns NULL and sets *ERR to either -1 for error, or 1 if
+   Z_TYPE breakpoints are not supported on this target.  */
+
+struct breakpoint *set_gdb_breakpoint (char z_type, CORE_ADDR addr, int size,
+				       int *err);
+
+/* Delete a GDB breakpoint of type Z_TYPE and size SIZE previously
+   inserted at ADDR with set_gdb_breakpoint_at.  Returns 0 on success,
+   -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
+   target.  */
+
+int delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int size);
+
+/* Returns TRUE if there's a software or hardware (code) breakpoint at
+   ADDR in our tables, inserted, or not.  */
 
 int breakpoint_here (CORE_ADDR addr);
 
-/* Returns TRUE if there's any inserted breakpoint set at ADDR.  */
+/* Returns TRUE if there's any inserted software or hardware (code)
+   breakpoint set at ADDR.  */
 
 int breakpoint_inserted_here (CORE_ADDR addr);
 
 /* Clear all breakpoint conditions associated with this address.  */
 
-void clear_gdb_breakpoint_conditions (CORE_ADDR addr);
+void clear_breakpoint_conditions (struct breakpoint *bp);
 
 /* Set target-side condition CONDITION to the breakpoint at ADDR.
    Returns false on failure.  On success, advances CONDITION pointer
    past the condition and returns true.  */
 
-int add_breakpoint_condition (CORE_ADDR addr, char **condition);
+int add_breakpoint_condition (struct breakpoint *bp, char **condition);
 
 /* Set target-side commands COMMANDS to the breakpoint at ADDR.
    Returns false on failure.  On success, advances COMMANDS past the
    commands and returns true.  If PERSIST, the commands should run
    even while GDB is disconnected.  */
 
-int add_breakpoint_commands (CORE_ADDR addr, char **commands, int persist);
+int add_breakpoint_commands (struct breakpoint *bp, char **commands,
+			     int persist);
 
 int any_persistent_commands (void);
 
@@ -80,7 +119,8 @@ int gdb_no_commands_at_breakpoint (CORE_ADDR where);
 
 void run_breakpoint_commands (CORE_ADDR where);
 
-/* Returns TRUE if there's a GDB breakpoint set at ADDR.  */
+/* Returns TRUE if there's a GDB breakpoint (Z0 or Z1) set at
+   WHERE.  */
 
 int gdb_breakpoint_here (CORE_ADDR where);
 
@@ -91,11 +131,6 @@ int gdb_breakpoint_here (CORE_ADDR where);
 struct breakpoint *set_breakpoint_at (CORE_ADDR where,
 				      int (*handler) (CORE_ADDR));
 
-/* Delete a GDB breakpoint previously inserted at ADDR with
-   set_gdb_breakpoint_at.  */
-
-int delete_gdb_breakpoint_at (CORE_ADDR addr);
-
 /* Delete a breakpoint.  */
 
 int delete_breakpoint (struct breakpoint *bkpt);
@@ -199,4 +234,12 @@ void uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc);
 
 void reinsert_fast_tracepoint_jumps_at (CORE_ADDR where);
 
+/* Insert a memory breakpoint.  */
+
+int insert_memory_breakpoint (struct raw_breakpoint *bp);
+
+/* Remove a previously inserted memory breakpoint.  */
+
+int remove_memory_breakpoint (struct raw_breakpoint *bp);
+
 #endif /* MEM_BREAK_H */
diff --git a/gdb/gdbserver/nto-low.c b/gdb/gdbserver/nto-low.c
index 52ca155..3587156 100644
--- a/gdb/gdbserver/nto-low.c
+++ b/gdb/gdbserver/nto-low.c
@@ -771,30 +771,46 @@ nto_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
   return nto_read_auxv_from_initial_stack (initial_stack, myaddr, len);
 }
 
-/* Insert {break/watch}point at address ADDR.
-   TYPE must be in '0'..'4' range.  LEN is not used.  */
+static int
+nto_supports_z_point_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_SW_BP:
+    case Z_PACKET_HW_BP:
+    case Z_PACKET_WRITE_WP:
+    case Z_PACKET_READ_WP:
+    case Z_PACKET_ACCESS_WP:
+      return 1;
+    default:
+      return 0;
+    }
+}
+
+/* Insert {break/watch}point at address ADDR.  SIZE is not used.  */
 
 static int
-nto_insert_point (char type, CORE_ADDR addr, int len)
+nto_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		  int size, struct raw_breakpoint *bp)
 {
   int wtype = _DEBUG_BREAK_HW; /* Always request HW.  */
 
   TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
   switch (type)
     {
-    case '0': /* software-breakpoint */
+    case raw_bkpt_type_sw:
       wtype = _DEBUG_BREAK_EXEC;
       break;
-    case '1': /* hardware-breakpoint */
+    case raw_bkpt_type_hw:
       wtype |= _DEBUG_BREAK_EXEC;
       break;
-    case '2':  /* write watchpoint */
+    case raw_bkpt_type_write_wp:
       wtype |= _DEBUG_BREAK_RW;
       break;
-    case '3':  /* read watchpoint */
+    case raw_bkpt_type_read_wp:
       wtype |= _DEBUG_BREAK_RD;
       break;
-    case '4':  /* access watchpoint */
+    case raw_bkpt_type_access_wp:
       wtype |= _DEBUG_BREAK_RW;
       break;
     default:
@@ -803,30 +819,30 @@ nto_insert_point (char type, CORE_ADDR addr, int len)
   return nto_breakpoint (addr, wtype, 0);
 }
 
-/* Remove {break/watch}point at address ADDR.
-   TYPE must be in '0'..'4' range.  LEN is not used.  */
+/* Remove {break/watch}point at address ADDR.  SIZE is not used.  */
 
 static int
-nto_remove_point (char type, CORE_ADDR addr, int len)
+nto_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		  int size, struct raw_breakpoint *bp)
 {
   int wtype = _DEBUG_BREAK_HW; /* Always request HW.  */
 
   TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
   switch (type)
     {
-    case '0': /* software-breakpoint */
+    case raw_bkpt_type_sw:
       wtype = _DEBUG_BREAK_EXEC;
       break;
-    case '1': /* hardware-breakpoint */
+    case raw_bkpt_type_hw:
       wtype |= _DEBUG_BREAK_EXEC;
       break;
-    case '2':  /* write watchpoint */
+    case raw_bkpt_type_write_wp:
       wtype |= _DEBUG_BREAK_RW;
       break;
-    case '3':  /* read watchpoint */
+    case raw_bkpt_type_read_wp:
       wtype |= _DEBUG_BREAK_RD;
       break;
-    case '4':  /* access watchpoint */
+    case raw_bkpt_type_access_wp:
       wtype |= _DEBUG_BREAK_RW;
       break;
     default:
@@ -927,6 +943,7 @@ static struct target_ops nto_target_ops = {
   NULL, /* nto_look_up_symbols */
   nto_request_interrupt,
   nto_read_auxv,
+  nto_supports_z_point_type,
   nto_insert_point,
   nto_remove_point,
   nto_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 8317ac0..ebc7735 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -3359,12 +3359,12 @@ skip_to_semicolon (char **packet)
     (*packet)++;
 }
 
-/* Process options coming from Z packets for *point at address
-   POINT_ADDR.  PACKET is the packet buffer.  *PACKET is updated
-   to point to the first char after the last processed option.  */
+/* Process options coming from Z packets for a breakpoint.  PACKET is
+   the packet buffer.  *PACKET is updated to point to the first char
+   after the last processed option.  */
 
 static void
-process_point_options (CORE_ADDR point_addr, char **packet)
+process_point_options (struct breakpoint *bp, char **packet)
 {
   char *dataptr = *packet;
   int persist;
@@ -3385,7 +3385,7 @@ process_point_options (CORE_ADDR point_addr, char **packet)
 	  /* Conditional expression.  */
 	  if (debug_threads)
 	    debug_printf ("Found breakpoint condition.\n");
-	  if (!add_breakpoint_condition (point_addr, &dataptr))
+	  if (!add_breakpoint_condition (bp, &dataptr))
 	    skip_to_semicolon (&dataptr);
 	}
       else if (strncmp (dataptr, "cmds:", strlen ("cmds:")) == 0)
@@ -3395,7 +3395,7 @@ process_point_options (CORE_ADDR point_addr, char **packet)
 	    debug_printf ("Found breakpoint commands %s.\n", dataptr);
 	  persist = (*dataptr == '1');
 	  dataptr += 2;
-	  if (add_breakpoint_commands (point_addr, &dataptr, persist))
+	  if (add_breakpoint_commands (bp, &dataptr, persist))
 	    skip_to_semicolon (&dataptr);
 	}
       else
@@ -3732,39 +3732,26 @@ process_serial_event (void)
 	p = unpack_varlen_hex (p, &addr);
 	len = strtol (p + 1, &dataptr, 16);
 
-	/* Default to unrecognized/unsupported.  */
-	res = 1;
-	switch (type)
+	if (insert)
 	  {
-	  case '0': /* software-breakpoint */
-	  case '1': /* hardware-breakpoint */
-	  case '2': /* write watchpoint */
-	  case '3': /* read watchpoint */
-	  case '4': /* access watchpoint */
-	    require_running (own_buf);
-	    if (insert && the_target->insert_point != NULL)
+	    struct breakpoint *bp;
+
+	    bp = set_gdb_breakpoint (type, addr, len, &res);
+	    if (bp != NULL)
 	      {
-		/* Insert the breakpoint.  If it is already inserted, nothing
-		   will take place.  */
-		res = (*the_target->insert_point) (type, addr, len);
-
-		/* GDB may have sent us a list of *point parameters to be
-		   evaluated on the target's side.  Read such list here.  If we
-		   already have a list of parameters, GDB is telling us to drop
-		   that list and use this one instead.  */
-		if (!res && (type == '0' || type == '1'))
-		  {
-		    /* Remove previous conditions.  */
-		    clear_gdb_breakpoint_conditions (addr);
-		    process_point_options (addr, &dataptr);
-		  }
+		res = 0;
+
+		/* GDB may have sent us a list of *point parameters to
+		   be evaluated on the target's side.  Read such list
+		   here.  If we already have a list of parameters, GDB
+		   is telling us to drop that list and use this one
+		   instead.  */
+		clear_breakpoint_conditions (bp);
+		process_point_options (bp, &dataptr);
 	      }
-	    else if (!insert && the_target->remove_point != NULL)
-	      res = (*the_target->remove_point) (type, addr, len);
-	    break;
-	  default:
-	    break;
 	  }
+	else
+	  res = delete_gdb_breakpoint (type, addr, len);
 
 	if (res == 0)
 	  write_ok (own_buf);
diff --git a/gdb/gdbserver/spu-low.c b/gdb/gdbserver/spu-low.c
index 1d07f66..9bb0c40 100644
--- a/gdb/gdbserver/spu-low.c
+++ b/gdb/gdbserver/spu-low.c
@@ -659,6 +659,7 @@ static struct target_ops spu_target_ops = {
   spu_look_up_symbols,
   spu_request_interrupt,
   NULL,
+  NULL,  /* supports_z_point_type */
   NULL,
   NULL,
   NULL,
diff --git a/gdb/gdbserver/target.h b/gdb/gdbserver/target.h
index 7374d58..f5eda8a 100644
--- a/gdb/gdbserver/target.h
+++ b/gdb/gdbserver/target.h
@@ -24,6 +24,7 @@
 #include "target/resume.h"
 #include "target/wait.h"
 #include "target/waitstatus.h"
+#include "mem-break.h"
 
 struct emit_ops;
 struct btrace_target_info;
@@ -136,8 +137,8 @@ struct target_ops
      inferior such that it is possible to access memory.
 
      This should generally only be called from client facing routines,
-     such as gdb_read_memory/gdb_write_memory, or the insert_point
-     callbacks.
+     such as gdb_read_memory/gdb_write_memory, or the GDB breakpoint
+     insertion routine.
 
      Like `read_memory' and `write_memory' below, returns 0 on success
      and errno on failure.  */
@@ -187,17 +188,23 @@ struct target_ops
   int (*read_auxv) (CORE_ADDR offset, unsigned char *myaddr,
 		    unsigned int len);
 
-  /* Insert and remove a break or watchpoint.
-     Returns 0 on success, -1 on failure and 1 on unsupported.
-     The type is coded as follows:
+  /* Returns true if GDB Z breakpoint type TYPE is supported, false
+     otherwise.  The type is coded as follows:
        '0' - software-breakpoint
        '1' - hardware-breakpoint
        '2' - write watchpoint
        '3' - read watchpoint
-       '4' - access watchpoint  */
+       '4' - access watchpoint
+  */
+  int (*supports_z_point_type) (char z_type);
+
+  /* Insert and remove a break or watchpoint.
+     Returns 0 on success, -1 on failure and 1 on unsupported.  */
 
-  int (*insert_point) (char type, CORE_ADDR addr, int len);
-  int (*remove_point) (char type, CORE_ADDR addr, int len);
+  int (*insert_point) (enum raw_bkpt_type type, CORE_ADDR addr,
+		       int size, struct raw_breakpoint *bp);
+  int (*remove_point) (enum raw_bkpt_type type, CORE_ADDR addr,
+		       int size, struct raw_breakpoint *bp);
 
   /* Returns 1 if target was stopped due to a watchpoint hit, 0 otherwise.  */
 
diff --git a/gdb/gdbserver/win32-arm-low.c b/gdb/gdbserver/win32-arm-low.c
index ce6c279..cf64514 100644
--- a/gdb/gdbserver/win32-arm-low.c
+++ b/gdb/gdbserver/win32-arm-low.c
@@ -132,6 +132,7 @@ struct win32_target_ops the_low_target = {
   (const unsigned char *) &arm_wince_breakpoint,
   arm_wince_breakpoint_len,
   /* Watchpoint related functions.  See target.h for comments.  */
+  NULL, /* supports_z_point_type */
   NULL, /* insert_point */
   NULL, /* remove_point */
   NULL, /* stopped_by_watchpoint */
diff --git a/gdb/gdbserver/win32-i386-low.c b/gdb/gdbserver/win32-i386-low.c
index e70ca71..08242aa 100644
--- a/gdb/gdbserver/win32-i386-low.c
+++ b/gdb/gdbserver/win32-i386-low.c
@@ -95,21 +95,35 @@ i386_dr_low_get_status (void)
   return debug_reg_state.dr_status_mirror;
 }
 
-/* Watchpoint support.  */
+/* Breakpoint/watchpoint support.  */
 
 static int
-i386_insert_point (char type, CORE_ADDR addr, int len)
+i386_supports_z_point_type (char z_type)
+{
+  switch (z_type)
+    {
+    case Z_PACKET_WRITE_WP:
+    case Z_PACKET_ACCESS_WP:
+      return 1;
+    default:
+      return 0;
+    }
+}
+
+static int
+i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		   int size, struct raw_breakpoint *bp)
 {
   switch (type)
     {
-    case '2':
-    case '3':
-    case '4':
+    case raw_bkpt_type_write_wp:
+    case raw_bkpt_type_access_wp:
       {
-	enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
+	enum target_hw_bp_type hw_type
+	  = raw_bkpt_type_to_target_hw_bp_type (type);
 
 	return i386_low_insert_watchpoint (&debug_reg_state,
-					   hw_type, addr, len);
+					   hw_type, addr, size);
       }
     default:
       /* Unsupported.  */
@@ -118,18 +132,19 @@ i386_insert_point (char type, CORE_ADDR addr, int len)
 }
 
 static int
-i386_remove_point (char type, CORE_ADDR addr, int len)
+i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		   int size, struct raw_breakpoint *bp)
 {
   switch (type)
     {
-    case '2':
-    case '3':
-    case '4':
+    case raw_bkpt_type_write_wp:
+    case raw_bkpt_type_access_wp:
       {
-	enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
+	enum target_hw_bp_type hw_type
+	  = raw_bkpt_type_to_target_hw_bp_type (type);
 
 	return i386_low_remove_watchpoint (&debug_reg_state,
-					   hw_type, addr, len);
+					   hw_type, addr, size);
       }
     default:
       /* Unsupported.  */
@@ -432,6 +447,7 @@ struct win32_target_ops the_low_target = {
   i386_single_step,
   &i386_win32_breakpoint,
   i386_win32_breakpoint_len,
+  i386_supports_z_point_type,
   i386_insert_point,
   i386_remove_point,
   i386_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 14afbdb..bc2506c 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -247,20 +247,29 @@ child_delete_thread (DWORD pid, DWORD tid)
    if the low target has registered a corresponding function.  */
 
 static int
-win32_insert_point (char type, CORE_ADDR addr, int len)
+win32_supports_z_point_type (char z_type)
+{
+  return (the_low_target.supports_z_point_type != NULL
+	  && the_low_target.supports_z_point_type (z_type));
+}
+
+static int
+win32_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		    int size, struct raw_breakpoint *bp)
 {
   if (the_low_target.insert_point != NULL)
-    return the_low_target.insert_point (type, addr, len);
+    return the_low_target.insert_point (type, addr, size, bp);
   else
     /* Unsupported (see target.h).  */
     return 1;
 }
 
 static int
-win32_remove_point (char type, CORE_ADDR addr, int len)
+win32_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
+		    int size, struct raw_breakpoint *bp)
 {
   if (the_low_target.remove_point != NULL)
-    return the_low_target.remove_point (type, addr, len);
+    return the_low_target.remove_point (type, addr, size, bp);
   else
     /* Unsupported (see target.h).  */
     return 1;
@@ -1773,6 +1782,7 @@ static struct target_ops win32_target_ops = {
   NULL, /* lookup_symbols */
   win32_request_interrupt,
   NULL, /* read_auxv */
+  win32_supports_z_point_type,
   win32_insert_point,
   win32_remove_point,
   win32_stopped_by_watchpoint,
diff --git a/gdb/gdbserver/win32-low.h b/gdb/gdbserver/win32-low.h
index 06983df..4e84957 100644
--- a/gdb/gdbserver/win32-low.h
+++ b/gdb/gdbserver/win32-low.h
@@ -85,8 +85,11 @@ struct win32_target_ops
   int breakpoint_len;
 
   /* Breakpoint/Watchpoint related functions.  See target.h for comments.  */
-  int (*insert_point) (char type, CORE_ADDR addr, int len);
-  int (*remove_point) (char type, CORE_ADDR addr, int len);
+  int (*supports_z_point_type) (char z_type);
+  int (*insert_point) (enum raw_bkpt_type type, CORE_ADDR addr,
+		       int size, struct raw_breakpoint *bp);
+  int (*remove_point) (enum raw_bkpt_type type, CORE_ADDR addr,
+		       int size, struct raw_breakpoint *bp);
   int (*stopped_by_watchpoint) (void);
   CORE_ADDR (*stopped_data_address) (void);
 };
diff --git a/gdb/testsuite/gdb.base/break-idempotent.c b/gdb/testsuite/gdb.base/break-idempotent.c
new file mode 100644
index 0000000..c451116
--- /dev/null
+++ b/gdb/testsuite/gdb.base/break-idempotent.c
@@ -0,0 +1,52 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int global;
+
+int cond_global;
+
+int
+foo (int i)
+{
+  global++;
+  return i;
+}
+
+int
+bar (int i)
+{
+  return i;
+}
+
+void
+test ()
+{
+  foo (1);
+  foo (2);
+  bar (3);
+}
+
+int
+main ()
+{
+  int i;
+
+  for (i = 0; i < 1000; i++)
+    test ();
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/break-idempotent.exp b/gdb/testsuite/gdb.base/break-idempotent.exp
new file mode 100644
index 0000000..4bfac63
--- /dev/null
+++ b/gdb/testsuite/gdb.base/break-idempotent.exp
@@ -0,0 +1,179 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+# Test that the Zx breakpoint/watchpoint packets are idempotent.
+
+# GDBserver used to not treat Zx breakpoints other than Z0 as
+# idempotent, although it must, to avoid problems with
+# retransmissions.  Even without spurious transport problems, if the
+# target supports target conditions or commands, GDB re-inserts Zx
+# breakpoints even if they are already inserted, to update the
+# target-side condition/commands.  E.g., simply when a duplicate
+# breakpoint is created, or when a shared library load causes a
+# re-set, which creates duplicate locations while breakpoints are
+# inserted, or when the condition is really changed while breakpoints
+# are inserted.  To make the test not depend on shared library support
+# or on details of the breakpoint re-set implementation, or on GDB
+# optimizing out re-sends if the condition hasn't actually changed, we
+# force always-inserted on, and really change the breakpoint's
+# condition.  For good measure, test with both always-inserted "on"
+# and "off" modes.
+
+# The test is written in black-box style, and doesn't actually use
+# anything target remote specific, so let it run on all targets.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+if ![runto_main] then {
+    fail "Can't run to main"
+    return 0
+}
+
+if [is_remote host] {
+    set arg [remote_download host $binfile]
+    if { $arg == "" } {
+	perror "download failed"
+	return -1
+    }
+}
+
+# Force a breakpoint re-set in GDB.  Currently this is done by
+# reloading symbols with the "file" command.
+
+proc force_breakpoint_re_set {} {
+    global binfile gdb_prompt
+
+    set test "file \$binfile"
+    gdb_test_multiple "file $binfile" $test {
+	-re "Are you sure you want to change the file. .*y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "Load new symbol table from \".*\".*y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "Reading symbols from.*done.*$gdb_prompt $" {
+	    pass $test
+	}
+    }
+}
+
+# Set a break/hbreak/watch/rwatch/awatch.
+
+proc set_breakpoint { break_command } {
+    global gdb_prompt srcfile
+
+    if { $break_command == "break" } {
+	gdb_test "$break_command foo" "Breakpoint.*at.* file .*$srcfile, line.*"
+    } elseif { $break_command == "hbreak" } {
+	set test "$break_command foo"
+	gdb_test_multiple $test $test {
+	    -re "No hardware breakpoint support in the target.*$gdb_prompt $" {
+		unsupported $test
+	    }
+	    -re "Hardware breakpoints used exceeds limit.*$gdb_prompt $" {
+		unsupported $test
+	    }
+	    -re "Cannot insert hardware breakpoint.*$gdb_prompt $" {
+		unsupported $test
+	    }
+	    -re "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*$gdb_prompt $" {
+		pass $test
+	    }
+	}
+    } elseif { [string first "watch" $break_command] != -1 } {
+	set test "$break_command global"
+	gdb_test_multiple $test $test {
+	    -re "Target does not support this type of hardware watchpoint\\.\r\n$gdb_prompt $" {
+		unsupported $test
+	    }
+	    -re "Could not insert hardware watchpoint.*$gdb_prompt $" {
+		unsupported $test
+	    }
+	    -re "atchpoint \[0-9\]+: global\r\n$gdb_prompt $" {
+		pass $test
+	    }
+	}
+    } else {
+	error "unhandled command: $break_command"
+    }
+}
+
+# Run the test proper.  ALWAYS_INSERT determines whether
+# always-inserted mode is on/off, and BREAK_COMMAND is the
+# break/watch/etc. command being tested.
+#
+proc test_break { always_inserted break_command } {
+    set cmd [lindex [split "$break_command"] 0]
+
+    with_test_prefix "always-inserted $always_inserted: $cmd" {
+	delete_breakpoints
+
+	if ![runto_main] then {
+	    fail "Can't run to main"
+	    return
+	}
+
+	gdb_test_no_output "set breakpoint always-inserted $always_inserted"
+
+	# Set breakpoints/watchpoints twice.  With always-inserted on,
+	# GDB reinserts the exact same Z breakpoint twice...  Do this
+	# to make sure the stub pays attention to idempotency even
+	# when the condition doesn't change.  If GDB end up optimizing
+	# out exact duplicate packets, we should come up with a way to
+	# keep testing this case.
+	foreach iter { "once" "twice" } {
+	    with_test_prefix $iter {
+		set_breakpoint $break_command
+	    }
+	}
+
+	# Force a breakpoint re-set.  In always-inserted mode, this
+	# makes GDB re-send Z packets too...
+	force_breakpoint_re_set
+
+	# Now really change the condition, which forces a reinsert by
+	# design.
+	gdb_test "condition \$bpnum cond_global == 0" ".*"
+
+	delete_breakpoints
+
+	gdb_test "b bar" "Breakpoint .* at .*"
+
+	gdb_test "continue" "Breakpoint .*, bar .*"
+    }
+}
+
+foreach always_inserted { "off" "on" } {
+    test_break $always_inserted "break"
+
+    if {![skip_hw_breakpoint_tests]} {
+	test_break $always_inserted "hbreak"
+    }
+
+    if {![skip_hw_watchpoint_tests]} {
+	test_break $always_inserted "watch"
+    }
+
+    if {![skip_hw_watchpoint_access_tests]} {
+	test_break $always_inserted "rwatch"
+	test_break $always_inserted "awatch"
+    }
+}
-- 
1.7.11.7


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]