From 7ac92f6edf2e1b71816e3c39b22e0619725d66b9 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Fri, 20 Sep 2024 15:57:36 +0200 Subject: [PATCH] Add GDB pretty printers for evmc_address, evmc_bytes32, evmc_bytes --- .gdbinit | 1 + gdb_pretty_printers.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 .gdbinit create mode 100755 gdb_pretty_printers.py diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000000..e26629cbff --- /dev/null +++ b/.gdbinit @@ -0,0 +1 @@ +source -v gdb_pretty_printers.py diff --git a/gdb_pretty_printers.py b/gdb_pretty_printers.py new file mode 100755 index 0000000000..a33f8cd059 --- /dev/null +++ b/gdb_pretty_printers.py @@ -0,0 +1,56 @@ +import gdb + +class EvmcBytesPrinter: + def __init__(self, val): + self.val = val + + def to_string(self): + start = self.val['bytes'] + bytes_int = [int(start[i]) for i in range(start.type.range()[1] + 1)] + return "0x" + (''.join(f'{byte:02x}' for byte in bytes_int)) + +class StdBasicStringUint8Printer: + def __init__(self, val): + self.val = val + + def to_string(self): + start = self.val['_M_dataplus']['_M_p'] + length = self.val['_M_string_length'] + content = [int(start[i]) for i in range(length)] + return '{%s}' % (', '.join(hex(byte) for byte in content)) + +def register_printers(obj): + if obj == None: + obj = gdb + obj.pretty_printers.insert(0, lookup_function) + +def lookup_function(val): + type_str = str(val.type.strip_typedefs()) + # print("lookup " + type_str) # uncomment to see exact type requested + + address_target_types = [ + "evmc::address", + "const evmc::address", + "evmc_address", + "const evmc_address", + "evmc::bytes32", + "const evmc::bytes32", + "evmc_bytes32", + "const evmc_bytes32", + ] + if type_str in address_target_types: + return EvmcBytesPrinter(val) + + # In CLion these are automatically overwritten by std::* pretty printers. + # Reload this script manually (`source -v ../../gdb_pretty_printers.py`) to activate them again. + bytes_target_types = [ + "std::__cxx11::basic_string, std::allocator >", + "const std::__cxx11::basic_string, std::allocator >", + ] + if type_str in bytes_target_types: + return StdBasicStringUint8Printer(val) + + return None + +register_printers(gdb.current_objfile()) +