Skip to content

Commit b74ac6f

Browse files
committed
Add GDB support for ref wrapper
1 parent fe70a7c commit b74ac6f

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/etc/gdb_load_rust_pretty_printers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
gdb_lookup.register_printers(gdb.current_objfile())
1515
except Exception:
1616
gdb_lookup.register_printers(gdb.selected_inferior().progspace)
17+
18+
try:
19+
gdb_lookup.register_type_printers(gdb.current_objfile())
20+
except Exception:
21+
gdb_lookup.register_type_printers(gdb.selected_inferior().progspace)

src/etc/gdb_lookup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def register_printers(objfile):
1616
objfile.pretty_printers.append(printer)
1717

1818

19+
def register_type_printers(objfile):
20+
gdb.types.register_type_printer(objfile, PtrTypePrinter("PtrOrRef"))
21+
22+
1923
# BACKCOMPAT: rust 1.35
2024
def is_hashbrown_hashmap(hash_map):
2125
return len(hash_map.type.fields()) == 1

src/etc/gdb_providers.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,68 @@ def children(self):
480480

481481
def display_hint(self):
482482
return "map" if self._show_values else "array"
483+
484+
485+
class NestedRefProvider(printer_base):
486+
def __init__(self, valobj):
487+
self._valobj = valobj
488+
self._ptr = self._valobj["ptr"]
489+
490+
def to_string(self):
491+
return str(self._valobj.cast(self._ptr.type))
492+
493+
def children(self):
494+
yield ("*ptr", self._ptr.referenced_value())
495+
496+
def num_children(self):
497+
return 1
498+
499+
def display_hint(self):
500+
return "array"
501+
502+
503+
PTR_CODES = (
504+
gdb.TYPE_CODE_PTR,
505+
gdb.TYPE_CODE_REF,
506+
gdb.TYPE_CODE_RVALUE_REF,
507+
gdb.TYPE_CODE_MEMBERPTR,
508+
)
509+
510+
511+
class PtrTypeRecognizer:
512+
def recognize(self, type: gdb.Type) -> str:
513+
if type.code not in PTR_CODES:
514+
return None
515+
516+
name_parts = []
517+
518+
ptr_type: gdb.Type = type
519+
ptee_type: gdb.Type = type.target()
520+
521+
while ptr_type.code in PTR_CODES:
522+
is_ref: bool = ptr_type.code == gdb.TYPE_CODE_REF
523+
524+
if ptee_type.const() == ptee_type:
525+
if is_ref:
526+
name_parts.append("&")
527+
else:
528+
name_parts.append("*const ")
529+
else:
530+
if is_ref:
531+
name_parts.append("&mut ")
532+
else:
533+
name_parts.append("*mut ")
534+
535+
ptr_type = ptee_type
536+
try:
537+
ptee_type = ptee_type.target()
538+
except RuntimeError:
539+
break
540+
541+
name_parts.append(ptr_type.unqualified().name)
542+
return "".join(name_parts)
543+
544+
545+
class PtrTypePrinter(gdb.types.TypePrinter):
546+
def instantiate(self):
547+
return PtrTypeRecognizer()

0 commit comments

Comments
 (0)