This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[RFC/WIP PATCH 02/14] Mask software breakpoints from memory writes too
When running the testsuite with both always-inserted and displaced
stepping on, I see nostdlib.exp failing. The symptom is a breakpoint
at marker being missed.
The problem is that the test sets a breakpoint at *_start, and another
at *marker, and when displace stepping *_start, when we copy a chunk
of copy to the scratch pad (to step it there), we also copy *marker,
because that address is just a few bytes away from *_start. When the
displace step finishes, we restore the scratch pad's original
contents, which again writes over *marker. But, while doing so, we
_don't_ restore the original breakpoint, because target_read_memory
masks out breakpoints, but conversely target_read_memory does not make
sure to staple breakpoint insns in place before actually writting to
memory! This patch fixes that, and adds a target_write_raw_memory
wrapper that callers that want to write to real memory without
stapling breakpoint insns on top can use -- mem-break.c is one such a
user. An alternative to changing mem-break.c would be to clear the
breakpoints inserted flag _before_ telling the target to remove the
breakpoint (this is how gdbserver does it). I haven't tried that yet.
Now that I've written this, I wonder if it's safe against a remote
target that implements software breakpoints itself...
---
gdb/breakpoint.c | 21 ++++++++++++--
gdb/breakpoint.h | 13 ++++++---
gdb/mem-break.c | 8 +++--
gdb/target.c | 80 ++++++++++++++++++++++++++++++++++++++++++++----------
gdb/target.h | 3 ++
5 files changed, 99 insertions(+), 26 deletions(-)
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 37e177b..7b8ab77 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -1049,7 +1049,8 @@ bp_location_has_shadow (struct bp_location *bl)
bl->address + bp_location_shadow_len_after_address_max <= memaddr */
void
-breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
+breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
+ ULONGEST memaddr, LONGEST len)
{
/* Left boundary, right boundary and median element of our binary
search. */
@@ -1161,8 +1162,22 @@ breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
bp_size -= (bp_addr + bp_size) - (memaddr + len);
}
- memcpy (buf + bp_addr - memaddr,
- bl->target_info.shadow_contents + bptoffset, bp_size);
+ if (readbuf != NULL)
+ {
+ memcpy (readbuf + bp_addr - memaddr,
+ bl->target_info.shadow_contents + bptoffset, bp_size);
+ }
+ else
+ {
+ struct gdbarch *gdbarch = bl->gdbarch;
+ const unsigned char *bp;
+ CORE_ADDR placed_address = bl->target_info.placed_address;
+ unsigned placed_size = bl->target_info.placed_size;
+
+ /* Determine appropriate breakpoint contents and size for this address. */
+ bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
+ memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
+ }
}
}
diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h
index 438f347..6b51137 100644
--- a/gdb/breakpoint.h
+++ b/gdb/breakpoint.h
@@ -1297,10 +1297,15 @@ extern int deprecated_remove_raw_breakpoint (struct gdbarch *, void *);
target. */
int watchpoints_triggered (struct target_waitstatus *);
-/* Update BUF, which is LEN bytes read from the target address MEMADDR,
- by replacing any memory breakpoints with their shadowed contents. */
-void breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr,
- LONGEST len);
+/* Helper for transparent breakpoint hiding for memory read and write
+ routines.
+
+ Update one of READBUF or WRITEBUF with either the shadows
+ (READBUF), or the breakpoint instructions (WRITEBUF) of inserted
+ breakpoints at the memory range defined by MEMADDR and extending
+ for LEN bytes. */
+extern void breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
+ ULONGEST memaddr, LONGEST len);
extern int breakpoints_always_inserted_mode (void);
diff --git a/gdb/mem-break.c b/gdb/mem-break.c
index ba7dc24..31ca45c 100644
--- a/gdb/mem-break.c
+++ b/gdb/mem-break.c
@@ -60,8 +60,8 @@ default_memory_insert_breakpoint (struct gdbarch *gdbarch,
/* Write the breakpoint. */
if (val == 0)
- val = target_write_memory (bp_tgt->placed_address, bp,
- bp_tgt->placed_size);
+ val = target_write_raw_memory (bp_tgt->placed_address, bp,
+ bp_tgt->placed_size);
return val;
}
@@ -71,8 +71,8 @@ int
default_memory_remove_breakpoint (struct gdbarch *gdbarch,
struct bp_target_info *bp_tgt)
{
- return target_write_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
- bp_tgt->placed_size);
+ return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
+ bp_tgt->placed_size);
}
diff --git a/gdb/target.c b/gdb/target.c
index 6358b00..3bc4be2 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -1388,19 +1388,15 @@ memory_xfer_live_readonly_partial (struct target_ops *ops,
For docs see target.h, to_xfer_partial. */
static LONGEST
-memory_xfer_partial (struct target_ops *ops, enum target_object object,
- void *readbuf, const void *writebuf, ULONGEST memaddr,
- LONGEST len)
+memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
+ void *readbuf, const void *writebuf, ULONGEST memaddr,
+ LONGEST len)
{
LONGEST res;
int reg_len;
struct mem_region *region;
struct inferior *inf;
- /* Zero length requests are ok and require no work. */
- if (len == 0)
- return 0;
-
/* For accesses to unmapped overlay sections, read directly from
files. Must do this first, as MEMADDR may need adjustment. */
if (readbuf != NULL && overlay_debugging)
@@ -1551,11 +1547,7 @@ memory_xfer_partial (struct target_ops *ops, enum target_object object,
if (res <= 0)
return -1;
else
- {
- if (readbuf && !show_memory_breakpoints)
- breakpoint_restore_shadows (readbuf, memaddr, reg_len);
- return res;
- }
+ return res;
}
/* If none of those methods found the memory we wanted, fall back
@@ -1584,9 +1576,6 @@ memory_xfer_partial (struct target_ops *ops, enum target_object object,
}
while (ops != NULL);
- if (res > 0 && readbuf != NULL && !show_memory_breakpoints)
- breakpoint_restore_shadows (readbuf, memaddr, reg_len);
-
/* Make sure the cache gets updated no matter what - if we are writing
to the stack. Even if this write is not tagged as such, we still need
to update the cache. */
@@ -1606,6 +1595,48 @@ memory_xfer_partial (struct target_ops *ops, enum target_object object,
return res;
}
+/* Perform a partial memory transfer. For docs see target.h,
+ to_xfer_partial. */
+
+static LONGEST
+memory_xfer_partial (struct target_ops *ops, enum target_object object,
+ void *readbuf, const void *writebuf, ULONGEST memaddr,
+ LONGEST len)
+{
+ int res;
+
+ /* Zero length requests are ok and require no work. */
+ if (len == 0)
+ return 0;
+
+ /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
+ breakpoint insns, thus hiding out from higher layers whether
+ there are software breakpoints inserted in the code stream. */
+ if (readbuf != NULL)
+ {
+ res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len);
+
+ if (res > 0 && !show_memory_breakpoints)
+ breakpoint_xfer_memory (readbuf, NULL, memaddr, res);
+ }
+ else
+ {
+ void *buf;
+ struct cleanup *old_chain;
+
+ buf = xmalloc (len);
+ old_chain = make_cleanup (xfree, buf);
+ memcpy (buf, writebuf, len);
+
+ breakpoint_xfer_memory (NULL, buf, memaddr, len);
+ res = memory_xfer_partial_1 (ops, object, NULL, buf, memaddr, len);
+
+ do_cleanups (old_chain);
+ }
+
+ return res;
+}
+
static void
restore_show_memory_breakpoints (void *arg)
{
@@ -1761,6 +1792,25 @@ target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
return EIO;
}
+/* Write LEN bytes from MYADDR to target raw memory at address
+ MEMADDR. Returns either 0 for success or an errno value if any
+ error occurs. If an error occurs, no guarantee is made about how
+ much data got written. Callers that can deal with partial writes
+ should call target_write. */
+
+int
+target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
+{
+ /* Dispatch to the topmost target, not the flattened current_target.
+ Memory accesses check target->to_has_(all_)memory, and the
+ flattened target doesn't inherit those. */
+ if (target_write (current_target.beneath, TARGET_OBJECT_RAW_MEMORY, NULL,
+ myaddr, memaddr, len) == len)
+ return 0;
+ else
+ return EIO;
+}
+
/* Fetch the target's memory map. */
VEC(mem_region_s) *
diff --git a/gdb/target.h b/gdb/target.h
index 73c8f7c..25e833f 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -939,6 +939,9 @@ extern int target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, int len);
extern int target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr,
int len);
+extern int target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr,
+ int len);
+
/* Fetches the target's memory map. If one is found it is sorted
and returned, after some consistency checking. Otherwise, NULL
is returned. */