Skip to content

Commit 152f810

Browse files
committed
Auto merge of #116978 - tromey:rust-printers-cleanup, r=Mark-Simulacrum
Rewrite gdb pretty-printer registration Currently, the Rust pretty-printers are registered in gdb using the uninformative name "lookup": (gdb) info pretty-printer global pretty-printers: [...] objfile /home/tromey/[...] lookup It's nicer for users if the top-level registration is given a clear name. Additionally, gdb lets users individually enable and disable specific printers, provided they are registered correctly. This patch implements both these ideas. Now the output looks like: (gdb) info pretty-printer global pretty-printers: [...] objfile /home/tromey/[...] rust StdArc StdBTreeMap StdBTreeSet StdCell StdHashMap StdHashSet StdNonZeroNumber StdOsString StdRc StdRef StdRefCell StdRefMut StdSlice StdStr StdString StdVec StdVecDeque
2 parents 724ba7f + e98beb5 commit 152f810

File tree

1 file changed

+78
-55
lines changed

1 file changed

+78
-55
lines changed

src/etc/gdb_lookup.py

+78-55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import gdb
2+
import gdb.printing
23
import re
34

45
from gdb_providers import *
@@ -9,7 +10,7 @@
910
gdb_version = [int(num) for num in _gdb_version_matched.groups()] if _gdb_version_matched else []
1011

1112
def register_printers(objfile):
12-
objfile.pretty_printers.append(lookup)
13+
objfile.pretty_printers.append(printer)
1314

1415

1516
# BACKCOMPAT: rust 1.35
@@ -38,58 +39,80 @@ def check_enum_discriminant(valobj):
3839
return True
3940

4041

41-
def lookup(valobj):
42-
rust_type = classify_rust_type(valobj.type)
43-
44-
if rust_type == RustType.ENUM:
45-
# use enum provider only for GDB <7.12
46-
if gdb_version[0] < 7 or (gdb_version[0] == 7 and gdb_version[1] < 12):
47-
if check_enum_discriminant(valobj):
48-
return EnumProvider(valobj)
49-
50-
if rust_type == RustType.STD_STRING:
51-
return StdStringProvider(valobj)
52-
if rust_type == RustType.STD_OS_STRING:
53-
return StdOsStringProvider(valobj)
54-
if rust_type == RustType.STD_STR:
55-
return StdStrProvider(valobj)
56-
if rust_type == RustType.STD_SLICE:
57-
return StdSliceProvider(valobj)
58-
if rust_type == RustType.STD_VEC:
59-
return StdVecProvider(valobj)
60-
if rust_type == RustType.STD_VEC_DEQUE:
61-
return StdVecDequeProvider(valobj)
62-
if rust_type == RustType.STD_BTREE_SET:
63-
return StdBTreeSetProvider(valobj)
64-
if rust_type == RustType.STD_BTREE_MAP:
65-
return StdBTreeMapProvider(valobj)
66-
if rust_type == RustType.STD_HASH_MAP:
67-
if is_hashbrown_hashmap(valobj):
68-
return StdHashMapProvider(valobj)
69-
else:
70-
return StdOldHashMapProvider(valobj)
71-
if rust_type == RustType.STD_HASH_SET:
72-
hash_map = valobj[valobj.type.fields()[0]]
73-
if is_hashbrown_hashmap(hash_map):
74-
return StdHashMapProvider(valobj, show_values=False)
75-
else:
76-
return StdOldHashMapProvider(hash_map, show_values=False)
77-
78-
if rust_type == RustType.STD_RC:
79-
return StdRcProvider(valobj)
80-
if rust_type == RustType.STD_ARC:
81-
return StdRcProvider(valobj, is_atomic=True)
82-
83-
if rust_type == RustType.STD_CELL:
84-
return StdCellProvider(valobj)
85-
if rust_type == RustType.STD_REF:
86-
return StdRefProvider(valobj)
87-
if rust_type == RustType.STD_REF_MUT:
88-
return StdRefProvider(valobj)
89-
if rust_type == RustType.STD_REF_CELL:
90-
return StdRefCellProvider(valobj)
91-
92-
if rust_type == RustType.STD_NONZERO_NUMBER:
93-
return StdNonZeroNumberProvider(valobj)
94-
42+
# Helper for enum printing that checks the discriminant. Only used in
43+
# older gdb.
44+
def enum_provider(valobj):
45+
if check_enum_discriminant(valobj):
46+
return EnumProvider(valobj)
9547
return None
48+
49+
50+
# Helper to handle both old and new hash maps.
51+
def hashmap_provider(valobj):
52+
if is_hashbrown_hashmap(valobj):
53+
return StdHashMapProvider(valobj)
54+
else:
55+
return StdOldHashMapProvider(valobj)
56+
57+
58+
# Helper to handle both old and new hash sets.
59+
def hashset_provider(valobj):
60+
hash_map = valobj[valobj.type.fields()[0]]
61+
if is_hashbrown_hashmap(hash_map):
62+
return StdHashMapProvider(valobj, show_values=False)
63+
else:
64+
return StdOldHashMapProvider(hash_map, show_values=False)
65+
66+
67+
class PrintByRustType(gdb.printing.SubPrettyPrinter):
68+
def __init__(self, name, provider):
69+
super(PrintByRustType, self).__init__(name)
70+
self.provider = provider
71+
72+
def __call__(self, val):
73+
if self.enabled:
74+
return self.provider(val)
75+
return None
76+
77+
78+
class RustPrettyPrinter(gdb.printing.PrettyPrinter):
79+
def __init__(self, name):
80+
super(RustPrettyPrinter, self).__init__(name, [])
81+
self.type_map = {}
82+
83+
def add(self, rust_type, provider):
84+
# Just use the rust_type as the name.
85+
printer = PrintByRustType(rust_type, provider)
86+
self.type_map[rust_type] = printer
87+
self.subprinters.append(printer)
88+
89+
def __call__(self, valobj):
90+
rust_type = classify_rust_type(valobj.type)
91+
if rust_type in self.type_map:
92+
return self.type_map[rust_type](valobj)
93+
return None
94+
95+
96+
printer = RustPrettyPrinter("rust")
97+
# use enum provider only for GDB <7.12
98+
if gdb_version[0] < 7 or (gdb_version[0] == 7 and gdb_version[1] < 12):
99+
printer.add(RustType.ENUM, enum_provider)
100+
printer.add(RustType.STD_STRING, StdStringProvider)
101+
printer.add(RustType.STD_OS_STRING, StdOsStringProvider)
102+
printer.add(RustType.STD_STR, StdStrProvider)
103+
printer.add(RustType.STD_SLICE, StdSliceProvider)
104+
printer.add(RustType.STD_VEC, StdVecProvider)
105+
printer.add(RustType.STD_VEC_DEQUE, StdVecDequeProvider)
106+
printer.add(RustType.STD_BTREE_SET, StdBTreeSetProvider)
107+
printer.add(RustType.STD_BTREE_MAP, StdBTreeMapProvider)
108+
printer.add(RustType.STD_HASH_MAP, hashmap_provider)
109+
printer.add(RustType.STD_HASH_SET, hashset_provider)
110+
printer.add(RustType.STD_RC, StdRcProvider)
111+
printer.add(RustType.STD_ARC, lambda valobj: StdRcProvider(valobj, is_atomic=True))
112+
113+
printer.add(RustType.STD_CELL, StdCellProvider)
114+
printer.add(RustType.STD_REF, StdRefProvider)
115+
printer.add(RustType.STD_REF_MUT, StdRefProvider)
116+
printer.add(RustType.STD_REF_CELL, StdRefCellProvider)
117+
118+
printer.add(RustType.STD_NONZERO_NUMBER, StdNonZeroNumberProvider)

0 commit comments

Comments
 (0)