Last active
September 20, 2024 10:02
-
-
Save gumb0/444d6be6a2b1d03fb2e30331d266906d to your computer and use it in GitHub Desktop.
evmc::bytes, evmc::bytes32, evmc::address pretty printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source -v /home/andrei/dev/evmone/gdb_pretty_printers/gdb_pretty_printers.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
# print ("register_printers") | |
if obj == None: | |
obj = gdb | |
obj.pretty_printers.insert(0, lookup_function) | |
# print ("lookup_function inserted") | |
def lookup_function(val): | |
# print("lookup") | |
type_str = str(val.type.strip_typedefs()) | |
# print("lookup " + type_str) | |
bytes_target_types = [ | |
"std::__cxx11::basic_string<unsigned char, evmc::byte_traits<unsigned char>, std::allocator<unsigned char> >", | |
"const std::__cxx11::basic_string<unsigned char, evmc::byte_traits<unsigned char>, std::allocator<unsigned char> >", | |
] | |
if type_str in bytes_target_types: | |
return StdBasicStringUint8Printer(val) | |
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) | |
return None | |
# print ("~/dev/evmone/gdb_pretty_printers") | |
register_printers(gdb.current_objfile()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment