-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgdb_mass.py
More file actions
145 lines (120 loc) · 4.74 KB
/
gdb_mass.py
File metadata and controls
145 lines (120 loc) · 4.74 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import generated_gdb
import gdb
# See https://stackoverflow.com/questions/68561176/converting-python-string-to-gdb-value-for-gdb-pretty-printing
def make_char_pointer(string):
original_bytes = string.encode("UTF-8")
original = gdb.Value(original_bytes + b"\0", gdb.lookup_type("char").array(len(original_bytes)))
adjusted = original.cast(gdb.lookup_type("char").pointer())
return adjusted
class ValueViewPrinter:
# The constructor takes the value and stores it for later.
def __init__(self, val):
self.val = val
def children(self):
data = self.val['values'].dereference()
length = int(self.val['length'])
for i in range(length):
yield '[%d]' % i, data[i]
@staticmethod
def display_hint():
return 'array'
class SourceRangePrinter:
# The constructor takes the value and stores it for later.
def __init__(self, val):
self.val = val
def children(self):
data = self.val['file'].dereference()['text']['bytes'] + self.val['offsets']['from']
length = int(self.val['offsets']['to'] - self.val['offsets']['from'])
yield '[text]', make_char_pointer(data.string(encoding='utf-8', length=length))
for key in self.val.type.keys():
yield key, self.val[key]
class SymbolPrinter:
# The constructor takes the value and stores it for later.
def __init__(self, val):
self.val = val
def to_string(self):
return f"Symbol({self.val['name'].format_string()})"
class HashMapPrinter:
# The constructor takes the value and stores it for later.
def __init__(self, val):
self.val = val
def is_string_like_key(self):
key_type = str(self.val['entries'].dereference()['key'].dereference().type.unqualified())
return key_type == 'Slice' or key_type == 'Symbol'
def children(self):
keys = {}
entries = self.val['entries']
length = int(self.val['capacity'])
hint = self.display_hint()
for i in range(length):
entry = entries[i]
if bool(entry['occupied']) and not bool(entry['tombstone']):
key = entry['key'].dereference()
value = entry['value'].dereference()
if hint == 'array':
key_string = key.format_string()
# for Symbols we can have duplicates which is a bug and nice to see in the debugger
if key_string in keys:
key_string += " (duplicate)"
else:
keys[key_string] = True
yield key_string, value
else:
yield f"key{i}", key
yield f"value{i}", value
def display_hint(self):
return 'array' if self.is_string_like_key() else 'map'
class TaggedUnionPrinter:
# The constructor takes the value and stores it for later.
def __init__(self, val):
self.val = val
def children(self):
tag = self.val['tag']
type_name = str(self.val.type.unqualified())
tag_name = str(tag).replace(type_name + "_Tag_", "")
for key in self.val.type.keys():
value = self.val[key]
if key.startswith('_') and key.endswith('_padding'):
continue
# anonymous union
if key == '':
if tag_name in value.type.keys():
yield (tag_name, value[tag_name])
else:
yield (tag_name, "{}")
else:
yield (key, value)
class RegisterBitsetPrinter:
REGISTER_NAMES = ['A', 'C', 'D', 'B', 'SP', 'BP', 'SI', 'DI']
# The constructor takes the value and stores it for later.
def __init__(self, val):
self.val = val
def to_string(self):
registers = []
bits = int(self.val['bits'])
for i in range(32):
if i < 8:
name = self.REGISTER_NAMES[i]
elif i < 16:
name = f"R{i}"
else:
name = f"XMM{i}"
if bits & (1 << i):
registers.append(name)
return "{" + ", ".join(registers) + "}"
def printer(val):
type_string = str(val.type.unqualified())
if type_string == 'Value_View':
return ValueViewPrinter(val)
if type_string == 'Source_Range':
return SourceRangePrinter(val)
if type_string == 'Symbol':
return SymbolPrinter(val)
if type_string == 'Register_Bitset':
return RegisterBitsetPrinter(val)
if type_string in generated_gdb.MASS_TYPES:
kind = generated_gdb.MASS_TYPES[type_string]
if kind == 'tagged_union':
return TaggedUnionPrinter(val)
if kind == 'hash_map':
return HashMapPrinter(val)