-
Notifications
You must be signed in to change notification settings - Fork 293
/
gdb_pretty_printers.py
executable file
·104 lines (77 loc) · 2.95 KB
/
gdb_pretty_printers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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))
INTX_UINT_TARGET_TYPES = [
"intx::uint<256>",
"const intx::uint<256>",
]
class IntxUintPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
words = self.val['words_']
words_int = [int(words[i]) for i in range(4)]
v = words_int[0] + (words_int[1] << 64) + (words_int[2] << 128) + (words_int[3] << 192)
return str(v)
# 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 '{size = %d, data = {%s}}' % (length, ', '.join(hex(byte) for byte in content))
STRING_VIEW_TARGET_TYPES = [
"std::basic_string_view<unsigned char, evmc::byte_traits<unsigned char> >",
"const std::basic_string_view<unsigned char, evmc::byte_traits<unsigned char> >",
]
class StdBasicStringViewUint8Printer:
def __init__(self, val):
self.val = val
def to_string(self):
start = self.val['_M_str']
length = self.val['_M_len']
content = [int(start[i]) for i in range(length)]
return '{size = %d, data = {%s}}' % (length, ', '.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 INTX_UINT_TARGET_TYPES:
return IntxUintPrinter(val)
if type_str in STRING_TARGET_TYPES:
return StdBasicStringUint8Printer(val)
if type_str in STRING_VIEW_TARGET_TYPES:
return StdBasicStringViewUint8Printer(val)
return None
register_printers(gdb.current_objfile())