forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gdbinit
executable file
·313 lines (294 loc) · 9.46 KB
/
.gdbinit
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# vi: ft=python
set print static-members off
set print object
set unwindonsignal on
set unwind-on-terminating-exception on
set python print-stack full
if $_isvoid($bpnum)
break P4::ErrorReporter::emit_message
disable
break P4::Util::P4CExceptionBase::P4CExceptionBase<>
break P4::Util::P4CExceptionBase::traceCreation
break P4::BaseCompileContext::getDefaultErrorDiagnosticAction
end
define pn
if $argc == 2
call P4::dbprint($arg0 $arg1)
else
call P4::dbprint($arg0)
end
end
document pn
print a P4::IR::Node pointer
end
define d
if $argc == 2
call P4::dump($arg0 $arg1)
else
call P4::dump($arg0)
end
end
document d
dump P4::IR::Node tree or Visitor::Context
end
define dnt
if $argc == 2
call P4::dump_notype($arg0 $arg1)
else
call P4::dump_notype($arg0)
end
end
document dnt
dump P4::IR::Node tree, skipping 'type' fields
end
define dsrc
if $argc == 2
call P4::dump_src($arg0 $arg1)
else
call P4::dump_src($arg0)
end
end
document dsrc
dump P4::IR::Node tree, skipping 'type' fields, and printing source info
end
python
import sys
import re
def template_split(s):
parts = []
bracket_level = 0
current = []
for c in (s):
if c == "," and bracket_level == 1:
parts.append("".join(current))
current = []
else:
if c == '>':
bracket_level -= 1
if bracket_level > 0:
current.append(c)
if c == '<':
bracket_level += 1
parts.append("".join(current))
return parts
TYPE_CACHE = {}
def lookup_type(typename):
"""Return a gdb.Type object represents given `typename`.
For example, x.cast(ty('Buffer'))"""
if typename in TYPE_CACHE:
return TYPE_CACHE[typename]
m0 = re.match(r"^(.*\S)\s*const$", typename)
m1 = re.match(r"^(.*\S)\s*[*|&]$", typename)
if m0 is not None:
tp = lookup_type(m0.group(1))
elif m1 is None:
tp = gdb.lookup_type(typename)
else:
if m1.group(0).endswith('*'):
tp = lookup_type(m1.group(1)).pointer()
else:
tp = lookup_type(m1.group(1)).reference()
TYPE_CACHE[typename] = tp
return tp
class ordered_map_Printer:
"Print an P4::ordered_map<>"
def __init__(self, val):
self.val = val
self.args = template_split(val.type.tag)
self.eltype = gdb.lookup_type('std::pair<' + self.args[0] + ' const,' + self.args[1] + '>')
def to_string(self):
return "ordered_map<..>"
class _iter:
def __init__(self, eltype, it, e):
self.eltype = eltype
self.it = it
self.e = e
def __iter__(self):
return self
def __next__(self):
if self.it == self.e:
raise StopIteration
el = (self.it + 1).cast(self.eltype.pointer()).dereference()
self.it = self.it.dereference()['_M_next']
return ("[" + str(el['first']) + "]", el['second']);
def next(self): return self.__next__()
def children(self):
return self._iter(self.eltype, self.val['data']['_M_impl']['_M_node']['_M_next'],
self.val['data']['_M_impl']['_M_node'].address)
class ordered_set_Printer:
"Print an P4::ordered_set<>"
def __init__(self, val):
self.val = val
self.args = template_split(val.type.tag)
self.eltypestr = self.args[0]
self.eltype = lookup_type(self.args[0])
self.isptr = True if re.match(r"^.*[*]$", self.args[0]) else False
def to_string(self):
return "ordered_set<..>"
class _iter:
def __init__(self, eltype, eltypestr, isptr, it, e):
self.eltype = eltype
self.eltypestr = eltypestr
self.isptr = isptr
self.it = it
self.e = e
self.idx = 0
def __iter__(self):
return self
def __next__(self):
if self.it == self.e:
raise StopIteration
el = (self.it + 1).cast(self.eltype.pointer()).dereference()
self.it = self.it.dereference()['_M_next']
idx = self.idx
self.idx = idx + 1
if self.isptr:
return ("[%d]" % idx, "(" + self.eltypestr + ")" + str(el))
return ("[%d]" % idx, el);
def next(self): return self.__next__()
def children(self):
return self._iter(self.eltype, self.eltypestr, self.isptr,
self.val['data']['_M_impl']['_M_node']['_M_next'],
self.val['data']['_M_impl']['_M_node'].address)
class cstringPrinter(object):
"Print a P4::cstring"
def __init__(self, val):
self.val = val
def to_string(self):
if self.val['str']:
return str(self.val['str'])
else:
return "nullptr"
class SourceInfoPrinter(object):
"Print a P4::Util::SourceInfo"
def __init__(self, val):
self.val = val
def to_string(self):
return (str(self.val['start']['lineNumber']) + ':' +
str(self.val['start']['columnNumber']) + '-' +
str(self.val['end']['lineNumber']) + ':' +
str(self.val['end']['columnNumber']))
class bitvecPrinter(object):
"Print a P4::bitvec"
def __init__(self, val):
self.val = val
def to_string(self):
data = self.val['data']
rv = ""
size = self.val['size']
ptr = self.val['ptr']
unitsize = ptr.type.target().sizeof * 8
while size > 1:
data = ptr.dereference()
i = 0
while i < unitsize:
if (rv.__len__() % 120 == 119): rv += ':'
elif (rv.__len__() % 30 == 29): rv += ' '
elif (rv.__len__() % 6 == 5): rv += '_'
if (data & 1) == 0:
rv += "0"
else:
rv += "1"
data >>= 1
i += 1
ptr += 1
size -= 1
data = ptr.dereference()
while rv == "" or data > 0:
if (rv.__len__() % 120 == 119): rv += ':'
elif (rv.__len__() % 30 == 29): rv += ' '
elif (rv.__len__() % 6 == 5): rv += '_'
if (data & 1) == 0:
rv += "0"
else:
rv += "1"
data >>= 1
return rv
def vec_begin(vec):
return vec['_M_impl']['_M_start']
def vec_end(vec):
return vec['_M_impl']['_M_finish']
def vec_size(vec):
return int(vec_end(vec) - vec_begin(vec))
def vec_at(vec, i):
return (vec_begin(vec) + i).dereference()
class safe_vector_Printer:
"Print a P4::safe_vector<>"
def __init__(self, val):
self.val = val
def to_string(self):
return "" if vec_size(self.val) > 0 else "[]"
class _iter:
def __init__(self, val):
self.val = val
self.size = vec_size(val)
self.idx = 0
def __iter__(self):
return self
def __next__(self):
if self.idx >= self.size:
raise StopIteration
idx = self.idx
self.idx = idx + 1
return ("[%d]" % idx, vec_at(self.val, idx));
def next(self): return self.__next__()
def children(self):
return self._iter(self.val)
def bvec_size(vec):
sz = int(vec_end(vec)['_M_p'] - vec_begin(vec)['_M_p'])
sz = sz * vec_begin(vec)['_M_p'].type.target().sizeof * 8
sz = sz + int(vec_end(vec)['_M_offset'] - vec_begin(vec)['_M_offset'])
return sz
class safe_vector_bool_Printer:
"Print a P4::safe_vector<bool>"
def __init__(self, val):
self.val = val
def to_string(self):
return "" if bvec_size(self.val) > 0 else "[]"
class _iter:
def __init__(self, val):
self.val = val
self.end = vec_end(val)
self.ptr = vec_begin(val)['_M_p']
self.offset = vec_begin(val)['_M_offset']
self.idx = 0
def __iter__(self):
return self
def __next__(self):
if self.ptr == self.end['_M_p'] and self.offset >= self.end['_M_offset']:
raise StopIteration
ptr = self.ptr
offset = self.offset
idx = self.idx
self.offset = offset + 1
self.idx = idx + 1
if self.offset == self.ptr.type.target().sizeof * 8:
self.offset = 0
self.ptr = self.ptr + 1
return ("[%d]" % idx, (self.ptr.dereference() >> self.offset) & 1);
def next(self): return self.__next__()
def children(self):
return self._iter(self.val)
def find_pp(val):
if str(val.type.tag).startswith('P4::ordered_map<'):
return ordered_map_Printer(val)
if str(val.type.tag).startswith('P4::ordered_set<'):
return ordered_set_Printer(val)
if str(val.type.tag).startswith('P4::safe_vector<bool'):
return safe_vector_bool_Printer(val)
if str(val.type.tag).startswith('P4::safe_vector<'):
return safe_vector_Printer(val)
if val.type.tag == 'P4::bitvec':
return bitvecPrinter(val)
if val.type.tag == 'P4::cstring':
return cstringPrinter(val)
if val.type.tag == 'P4::Util::SourceInfo':
return SourceInfoPrinter(val)
return None
try:
while gdb.pretty_printers[-1].__name__ == "find_pp":
gdb.pretty_printers = gdb.pretty_printers[:-1]
except:
pass
gdb.pretty_printers.append(find_pp)
end