-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GDB pretty printers for evmc_address, evmc_bytes32, evmc_bytes
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
source -v 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import gdb | ||
|
||
EVMC_BYTES_TARGET_TYPES = [ | ||
"evmc::address", | ||
"const evmc::address", | ||
"evmc_address", | ||
"const evmc_address", | ||
"evmc::bytes32", | ||
"const evmc::bytes32", | ||
"evmc_bytes32", | ||
"const evmc_bytes32", | ||
"evmc_uint256be", | ||
"const evmc_uint256be", | ||
"evmc::uint256be", | ||
"const evmc::uint256be", | ||
] | ||
|
||
|
||
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)) | ||
|
||
|
||
# In CLion these are automatically overwritten by std::* pretty printers. | ||
# Reload this script manually (`source -v ../../gdb_pretty_printers.py`) to activate them again. | ||
STRING_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> >", | ||
] | ||
|
||
|
||
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 | ||
|
||
if type_str in EVMC_BYTES_TARGET_TYPES: | ||
return EvmcBytesPrinter(val) | ||
|
||
if type_str in STRING_TARGET_TYPES: | ||
return StdBasicStringUint8Printer(val) | ||
|
||
return None | ||
|
||
|
||
register_printers(gdb.current_objfile()) |