-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdb_formatter.py
257 lines (178 loc) · 6.27 KB
/
gdb_formatter.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Defines pretty printers for core railguard classes
import gdb.printing as printing
# ====== Vector<T> ======
class VectorPrinter:
"""Print a rg::Vector<T>"""
class _iterator:
def __init__(self, array, count):
self.array = array
self.i = 0
self.count = count
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
if self.i == self.count:
raise StopIteration
value = self.array[self.i]
self.i += 1
return '[%d]' % (self.i - 1), value
def __init__(self, val):
self.val = val
value_type = val.type.template_argument(0)
# Get impl field
impl = self.val['m_impl']
# Get count field
self.count = impl['m_count']
# Get data field and cast it as a value_type*
self.data = impl['m_data'].cast(value_type.pointer())
def to_string(self):
# Load all values of self.data
values = [self.data[i] for i in range(self.count)]
return "[" + ", ".join(map(str, values)) + "]"
def children(self):
return self._iterator(self.data, self.count)
def display_hint(self):
return 'array'
# ====== Array<T> ======
class ArrayPrinter:
"""Prints a rg::Array<T>"""
class _iterator:
def __init__(self, array, count):
self.array = array
self.i = 0
self.count = count
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
if self.i == self.count:
raise StopIteration
value = self.array[self.i]
self.i += 1
return '[%d]' % (self.i - 1), value
def __init__(self, val):
self.value_type = val.type.template_argument(0)
# Get impl field
self.data = val['m_data'].cast(self.value_type.pointer())
# Get count field
self.count = val['m_count']
def to_string(self):
# Load all values of self.data
return "Array<%s>(%d)" % (self.value_type, self.count)
def children(self):
return self._iterator(self.data, self.count)
def display_hint(self):
return 'array'
# ====== HashMap ======
class HashMapPrinter:
"""Prints a rg::HashMap"""
class _iterator:
def __init__(self, entries, capacity):
self.entries = entries
self.i = 0
self.capacity = capacity
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
if self.i == self.capacity:
raise StopIteration
# Find the next entry that has no a null key
while self.entries[self.i]['key'] == 0:
self.i += 1
if self.i == self.capacity:
raise StopIteration
# Get the values of the entry
self.key = self.entries[self.i]['key']
self.value = self.entries[self.i]['value']
self.i += 1
return str(self.key), self.value
def __init__(self, val):
self.val = val
# Get data
data = self.val['m_data']
# Set to null if empty
if data == 0:
self.capacity = 0
self.count = 0
self.entries = None
else:
print(data)
# Get capacity and count
self.capacity = data['capacity']
self.count = data['count']
self.entries = data['entries']
def to_string(self):
return 'HashMap(capacity=%d, count=%d)' % (self.capacity, self.count)
def children(self):
return self._iterator(self.entries, self.capacity)
def display_hint(self):
return 'array'
# ====== rg::Map<T> ======
class MapPrinter:
"""Prints a rg::Map<T>"""
class _iterator:
def __init__(self, storage):
self.storage_it = storage.children()
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
_, entry = next(self.storage_it)
return str(entry["m_key"]), entry["m_value"]
def __init__(self, val):
# Get template parameters
self.value_type = val.type.template_argument(0)
# Get vector
self.storage = VectorPrinter(val['m_storage'])
# Get hash map
hash_map = val['m_hash_map']
# Check if m_data is null
if hash_map['m_data'] == 0:
self.capacity = 0
self.count = 0
else:
# Get capacity and count
self.capacity = hash_map['m_data']['capacity']
self.count = hash_map['m_data']['count']
def to_string(self):
return 'Map<%s>(capacity=%d, count=%d)' % (self.value_type, self.capacity, self.count)
def children(self):
return self._iterator(self.storage)
def display_hint(self):
return 'array'
# ====== Storage<T> ======
class StoragePrinter:
"""Prints a rg::Storage<T>"""
def __init__(self, val):
# Get template parameters
self.value_type = val.type.template_argument(0)
# Get counter
self.counter = val['m_id_counter']
# Get map
self.map = val['m_map']
self.map_printer = MapPrinter(self.map)
# Get count
self.count = self.map['m_hash_map']['m_data']['count']
def to_string(self):
return 'Storage<%s>(count=%d, id_counter=%d)' % (self.value_type, self.count, self.counter)
def children(self):
return self.map_printer.children()
def display_hint(self):
return 'array'
# ====== Register printers ======
def build_pretty_printer():
pp = printing.RegexpCollectionPrettyPrinter("rg")
pp.add_printer('Vector', '^rg::Vector<.*>$', VectorPrinter)
pp.add_printer('Array', '^rg::Array<.*>$', ArrayPrinter)
pp.add_printer('HashMap', '^rg::HashMap$', HashMapPrinter)
pp.add_printer('Map', '^rg::Map<.*>$', MapPrinter)
pp.add_printer('Storage', '^rg::Storage<.*>$', StoragePrinter)
return pp
print("Registering pretty printers for railguard")
printing.register_pretty_printer(None, build_pretty_printer())