Skip to content

Commit 079a4af

Browse files
debugging: Add GDB pretty printers for common types (#1957)
By default, GDB will not autoload this for security reasons. See https://sourceware.org/gdb/current/onlinedocs/gdb.html/Auto_002dloading-safe-path.html to see how to enable it.
1 parent fe70fdc commit 079a4af

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

.gdbinit

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
python
2+
3+
import gdb
4+
import re
5+
6+
7+
class BetypePrinter:
8+
PATTERN = re.compile("^betype<.*>$")
9+
10+
def __init__(self, obj):
11+
self._obj = obj
12+
13+
def to_string(self):
14+
underlying = self._obj["m_value"]
15+
reversed_bytes = bytes(reversed(underlying.bytes))
16+
return gdb.Value(reversed_bytes, underlying.type)
17+
18+
class MemptrPrinter:
19+
PATTERN = re.compile("^MEMPTR<.*>$")
20+
21+
def __init__(self, obj):
22+
self._ptr_type = obj.type.strip_typedefs().unqualified().template_argument(0).pointer()
23+
self._guest_address = obj["m_value"].cast(gdb.lookup_type("uint32"))
24+
25+
def ptr(self):
26+
if self._guest_address == 0:
27+
return gdb.Value(0, self._ptr_type)
28+
base_addr = gdb.parse_and_eval('memory_base', global_context=True)
29+
return (self._guest_address + base_addr).reinterpret_cast(self._ptr_type)
30+
31+
def children(self):
32+
return [("raw", self.ptr())]
33+
34+
def to_string(self):
35+
return self._guest_address.format_string(format="x")
36+
37+
def lookup(val):
38+
tag = val.type.strip_typedefs().unqualified().tag
39+
if tag is None:
40+
return None
41+
printers = [BetypePrinter, MemptrPrinter]
42+
for printer in printers:
43+
if printer.PATTERN.match(tag):
44+
return printer(val)
45+
return None
46+
47+
gdb.pretty_printers.append(lookup)

0 commit comments

Comments
 (0)