-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgdb-printers-shallow.py
More file actions
188 lines (136 loc) · 6.57 KB
/
gdb-printers-shallow.py
File metadata and controls
188 lines (136 loc) · 6.57 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
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
import abc
import logging
import math
from typing import (
Any,
Callable,
Iterable,
)
import re
import gdb
class Buffer(metaclass=abc.ABCMeta):
"""Base class for accessing buffer contents"""
def __init__(self, val: gdb.Value, data_key: str):
self._val: gdb.Value = val
self._data_key: str = data_key
@abc.abstractmethod
def __len__(self) -> int:
...
def __getitem__(self, key) -> Any:
return self.data()[key]
def data(self) -> gdb.Value:
return self._val[self._data_key]
class StackBuffer(Buffer):
def __init__(self, val: gdb.Value):
super().__init__(val, "m_data")
def __len__(self) -> int:
return int(self._val.type.template_argument(1))
class ChaiBuffer(Buffer):
def __init__(self, val: gdb.Value):
super().__init__(val, "m_pointer")
def __len__(self) -> int:
return int(self._val['m_capacity'])
class MallocBuffer(Buffer):
def __init__(self, val: gdb.Value):
super().__init__(val, "m_data")
def __len__(self) -> int:
return int(self._val['m_capacity'])
def extract_buffer(val: gdb.Value) -> Buffer:
# Use self.val.type.fields() to know the fields.
# You can also have a look at the fields of the fields.
t: str = str(val.type)
if re.match('LvArray::ChaiBuffer<.*>', t):
return ChaiBuffer(val)
elif re.match('LvArray::StackBuffer<.*>', t):
return StackBuffer(val)
elif re.match('LvArray::MallocBuffer<.*>', t):
return MallocBuffer(val)
else:
raise ValueError(f"Could not build buffer from `{val.type}`.")
class LvArrayPrinter:
"""Base printer for LvArray classes"""
def __init__(self, val: gdb.Value):
self.val: gdb.Value = val
def real_type(self) -> gdb.Type:
return self.val.type.strip_typedefs()
def display_hint(self) -> str:
return 'array'
class __ArrayPrinter(LvArrayPrinter):
"""Utility class for code factorization"""
def __init__(self,
val: gdb.Value,
data_extractor: Callable[[gdb.Value], gdb.Value],
dimension_extractor: Callable[[gdb.Value], gdb.Value]):
"""
val: The initial `gdb.Value`. Typically a `LvArray::Array`.
data_extractor: How to access the raw data from the initial `val`.
dimension_extractor: How to access the dimensions (since this is a multi-dimensional array) from the initial `val`.
"""
super().__init__(val)
self.data = data_extractor(self.val)
dimensions: gdb.Value = dimension_extractor(self.val)
num_dimensions: int = int(self.val.type.template_argument(1))
dimensions: Iterable[gdb.Value] = map(dimensions.__getitem__, range(num_dimensions))
self.dimensions: tuple[int] = tuple(map(int, dimensions))
def to_string(self) -> str:
# Extracting the permutations from the type name (looks like the integer template parameters are optimized out)
permutation = self.val.type.strip_typedefs().template_argument(2)
if m := re.search(r"\d+(?:[ \t]*,[ \t]*\d+)*", str(permutation)): # Extract a list of integers separated with commas
s = m.group()
permutation: tuple[int, ...] = tuple(map(int, s.split(",")))
if permutation != tuple(range(len(self.dimensions))):
msg = f"Only sorted permutation is supported by pretty printers. " \
f"{permutation} is not sorted so the output of the pretty printers will be jumbled."
logging.warning(msg)
else:
raise ValueError(f"Could not parse permutation for {permutation}.")
dimensions = map(str, self.dimensions)
return f'{self.real_type()} of shape [{", ".join(dimensions)}]'
def children(self) -> Iterable[tuple[str, gdb.Value]]:
d0, ds = self.dimensions[0], self.dimensions[1:]
# The main idea of this loop is to build the multi-dimensional array type to the data (e.g. `int[2][3]`).
# Then we'll cast the raw pointer into this n-d array and gdb will be able to manage it.
array_type: gdb.Type = self.data.type.target()
for d in reversed(ds): # Note that we ditch the first dimension from our loop in order to have it as first level children.
array_type = array_type.array(d - 1)
# We manage the first level children ourselves, so we need to manage the position of the data ourselves too.
stride: int = math.prod(ds)
for i in range(d0):
array = (self.data + i * stride).dereference().cast(array_type)
yield '[%d]' % i, array
class ArrayPrinter(__ArrayPrinter):
"""Pretty-print for Array(View)"""
def __init__(self, val: gdb.Value):
super().__init__(val, lambda v: extract_buffer(v["m_dataBuffer"]).data(), lambda v: v["m_dims"]["data"])
class ArraySlicePrinter(__ArrayPrinter):
"""Pretty-print for ArraySlice"""
def __init__(self, val: gdb.Value):
super().__init__(val, lambda v: v["m_data"], lambda v: v["m_dims"])
class ArrayOfArraysPrinter(LvArrayPrinter):
"""Pretty-print for ArrayOfArrays(View)"""
def __len__(self) -> int:
return int(self.val['m_numArrays'])
def to_string(self) -> str:
return '%s of size %d' % (self.real_type(), len(self))
def children(self) -> Iterable[tuple[str, gdb.Value]]:
# In this function, we are walking along the "sub" arrays ourselves.
# To do this, we manipulate the raw pointer/offsets information ourselves.
data = extract_buffer(self.val["m_values"]).data()
offsets = extract_buffer(self.val["m_offsets"])
sizes = extract_buffer(self.val["m_sizes"])
for i in range(len(self)): # Iterating over all the "sub" arrays.
# Converting a raw pointer `T*` to an equivalent type including the size `T[N]` that gdb will manage.
array_type = data.type.target().array(sizes[i] - 1)
array = (data + offsets[i]).dereference().cast(array_type)
yield '[%d]' % i, array
def build_array_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter("LvArray-arrays-shallow")
pp.add_printer('LvArray::Array', '^LvArray::Array(View)?<.*>$', ArrayPrinter)
pp.add_printer('LvArray::ArraySlice', '^LvArray::ArraySlice<.*>$', ArraySlicePrinter)
pp.add_printer('LvArray::ArrayOfArrays', '^LvArray::ArrayOfArrays(View)?<.*>$', ArrayOfArraysPrinter)
return pp
try:
import gdb.printing
gdb.printing.register_pretty_printer(gdb.current_objfile(), build_array_printer())
except ImportError:
logging.warning("Could not register LvArray pretty printers.")