This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit daac426f79e4eb166f2098626abe2a5f7d92a4e3
Author: anjiahao <anjia...@xiaomi.com>
AuthorDate: Thu Jul 18 20:53:17 2024 +0800

    gdb python plugin:add hexdump command support
    
    hexdump useful to debug
    
    format such as:
    
    (gdb) hexdump 0x0062fd30 500
    0062fd30  73 68 6f 77 5f 76 61 72 69 61 62 6c 65 3a 20 45  show_variable: E
    0062fd40  52 52 4f 52 20 56 61 72 69 61 62 6c 65 3d 25 73  RROR Variable=%s
    0062fd50  20 68 61 73 20 74 68 65 20 77 72 6f 6e 67 20 76   has the wrong v
    0062fd60  61 6c 75 65 0a 00 6f 73 74 65 73 74 5f 6d 61 69  alue..ostest_mai
    0062fd70  6e 2e 63 00 73 68 6f 77 5f 76 61 72 69 61 62 6c  n.c.show_variabl
    0062fd80  65 3a 20 45 52 52 4f 52 20 56 61 72 69 61 62 6c  e: ERROR Variabl
    0062fd90  65 3d 25 73 20 68 61 73 20 61 20 76 61 6c 75 65  e=%s has a value
    0062fda0  20 77 68 65 6e 20 69 74 20 73 68 6f 75 6c 64 20   when it should
    0062fdb0  6e 6f 74 0a 00 73 68 6f 77 5f 76 61 72 69 61 62  not..show_variab
    
    Signed-off-by: anjiahao <anjia...@xiaomi.com>
---
 tools/gdb/utils.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/tools/gdb/utils.py b/tools/gdb/utils.py
index d030a53eb3..17474a2609 100644
--- a/tools/gdb/utils.py
+++ b/tools/gdb/utils.py
@@ -21,7 +21,7 @@
 ############################################################################
 
 import gdb
-
+import re
 
 class CachedType:
     """Cache a type object, so that we can reconnect to the new_objfile 
event"""
@@ -188,3 +188,50 @@ def get_symbol_value(name):
 
     gdb.execute("set $_%s = %s" % (name, name))
     return gdb.parse_and_eval("$_%s" % name)
+
+def hexdump(address, size):
+    inf = gdb.inferiors()[0]
+    mem = inf.read_memory(address, size)
+    bytes = mem.tobytes()
+    for i in range(0, len(bytes), 16):
+        chunk = bytes[i:i+16]
+        gdb.write(f"{i + address:08x}  ")
+        hex_values = " ".join(f"{byte:02x}" for byte in chunk)
+        hex_display = f"{hex_values:<47}"
+        gdb.write(hex_display)
+        ascii_values = "".join(chr(byte) if 32 <= byte <= 126 else "." for 
byte in chunk)
+        gdb.write(f"  {ascii_values} \n")
+
+
+def is_decimal(s):
+    return re.fullmatch(r"\d+", s) is not None
+
+def is_hexadecimal(s):
+    return re.fullmatch(r"0[xX][0-9a-fA-F]+|[0-9a-fA-F]+", s) is not None
+
+class Hexdump(gdb.Command):
+    """hexdump address/symbol <size>"""
+
+    def __init__(self):
+        super(Hexdump, self).__init__("hexdump", gdb.COMMAND_USER)
+    def invoke(self, args, from_tty):
+        argv = args.split(" ")
+        argc = len(argv)
+        address = 0
+        size = 0
+        if (argv[0] == ""):
+            gdb.write("Usage: hexdump address/symbol <size>\n")
+            return
+
+        if is_decimal(argv[0]) or is_hexadecimal(argv[0]):
+            address = int(argv[0], 0)
+            size = int(argv[1], 0)
+        else:
+            var = gdb.parse_and_eval(f'{argv[0]}')
+            address = int(var.address)
+            size = int(var.type.sizeof)
+            gdb.write(f"{argv[0]} {hex(address)} {int(size)}\n")
+
+        hexdump(address, size)
+
+Hexdump()

Reply via email to