Add instruction bytes to DAP disassembly response
authorTom Tromey <tromey@adacore.com>
Wed, 28 Jun 2023 12:57:16 +0000 (06:57 -0600)
committerTom Tromey <tromey@adacore.com>
Fri, 21 Jul 2023 15:30:12 +0000 (09:30 -0600)
The DAP disassemble command lets the client return the underlying
bytes of the instruction in an implementation-defined format.  This
patch updates gdb to return this, and simply uses a hex string of the
bytes as the format.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
gdb/doc/gdb.texinfo
gdb/python/lib/gdb/dap/disassemble.py
gdb/testsuite/gdb.dap/basic-dap.exp

index cd86da50f46c0bd46c837f19219e7d8e6f607216..1463f5017689ed1b3a27e5b857198093fa29096f 100644 (file)
@@ -39097,6 +39097,11 @@ The target to which @value{GDBN} should connect.  This is a string and
 is passed to the @code{target remote} command.  @xref{Connecting}.
 @end table
 
+In response to the @code{disassemble} request, DAP allows the client
+to return the bytes of each instruction in an implementation-defined
+format.  @value{GDBN} implements this by sending a string with the
+bytes encoded in hex, like @code{"55a2b900"}.
+
 @node JIT Interface
 @chapter JIT Compilation Interface
 @cindex just-in-time compilation
index bc091eb2c89decc3f7071786aa4df101968dab00..dda2f43b5a3cb86cf9c213413de44452df66aa92 100644 (file)
@@ -21,18 +21,21 @@ from .startup import send_gdb_with_response, in_gdb_thread
 
 @in_gdb_thread
 def _disassemble(pc, skip_insns, count):
+    inf = gdb.selected_inferior()
     try:
         arch = gdb.selected_frame().architecture()
     except gdb.error:
         # Maybe there was no frame.
-        arch = gdb.selected_inferior().architecture()
+        arch = inf.architecture()
     result = []
     total_count = skip_insns + count
     for elt in arch.disassemble(pc, count=total_count)[skip_insns:]:
+        mem = inf.read_memory(elt["addr"], elt["length"])
         result.append(
             {
                 "address": hex(elt["addr"]),
                 "instruction": elt["asm"],
+                "instructionBytes": mem.hex(),
             }
         )
     return {
index 853e1536eabd77a766f8757f428a1b6b07e84e78..ef3c535f6a2d3e6cb6befbabe764b48908132616 100644 (file)
@@ -191,6 +191,15 @@ set obj [dap_check_request_and_response "disassemble one instruction" \
                  $insn_pc]]
 set response [lindex $obj 0]
 gdb_assert { [dict exists $response body instructions] } "instructions in disassemble output"
+foreach insn [dict get $response body instructions] {
+    gdb_assert {[dict exists $insn instructionBytes]} \
+       "instruction bytes in disassemble output"
+    set bytes [dict get $insn instructionBytes]
+    gdb_assert {[string length $bytes] % 2 == 0} \
+       "even number of digits"
+    gdb_assert {[regexp "^\[0-9A-Fa-f\]+\$" $bytes]} \
+       "instructionBytes is hex"
+}
 
 set obj [dap_check_request_and_response "command repl" \
             evaluate {o expression [s {output 23}] context [s repl]}]