-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffset_span_pretty_printer.py
More file actions
65 lines (47 loc) · 1.76 KB
/
offset_span_pretty_printer.py
File metadata and controls
65 lines (47 loc) · 1.76 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
import gdb
import re
# Encapsulates a list of children. Calls .child() on the pretty printer only
# when requested. This is a workaround for poor performance when inspecting
# large arrays in the debugger.
class LazyChildren:
def __init__(self, obj, length):
self._obj = obj
self._length = length
def __getitem__(self, index):
if not 0 <= index < self._length:
raise IndexError("Index out of range")
return self._obj.child(index)
def __len__(self):
return self._length
class OffsetSpanPrinter:
"""Pretty printer for decodeless::offset_span<T>"""
def __init__(self, val):
self.T = val.type.strip_typedefs().unqualified().template_argument(0)
self.length = val["m_size"]
ptr = val["m_data"]
base = ptr.address
offset = ptr["m_offset"]
if offset == 1:
base = gdb.Value(0)
offset = gdb.Value(0)
self.pointer = (base.cast(gdb.lookup_type("char").pointer()) + offset).cast(self.T.pointer())
def child(self, i):
return (f"[{i}]", (self.pointer + i).dereference())
def children(self):
return LazyChildren(self, self.length)
def __len__(self):
return self.length
def to_string(self):
return f'offset_span<{self.T}> of length {self.length}'
def display_hint(self):
return 'array'
offset_span_lookup_pattern = re.compile("^decodeless::offset_span<.*>$")
def offset_span_lookup(val):
global offset_span_lookup_pattern
lookup_tag = val.type.strip_typedefs().unqualified().tag
if lookup_tag is None:
return None
if offset_span_lookup_pattern.match(lookup_tag):
return OffsetSpanPrinter(val)
return None
gdb.pretty_printers.append(offset_span_lookup)