diff --git a/lib/smol_str/src/gdb_smolstr_printer.py b/lib/smol_str/src/gdb_smolstr_printer.py index 9b2984fc6d..5f28ddd5e6 100644 --- a/lib/smol_str/src/gdb_smolstr_printer.py +++ b/lib/smol_str/src/gdb_smolstr_printer.py @@ -25,15 +25,17 @@ import re SMOL_INLINE_SIZE_RE = re.compile(r".*::_V(\d+)$") + def _read_utf8(mem): try: return mem.tobytes().decode("utf-8", errors="replace") except Exception: return repr(mem.tobytes()) + def _active_variant(enum_val): """Return (variant_name, variant_value) for a Rust enum value using discriminant logic. - Assume layout: fields[0] is unnamed u8 discriminant; fields[1] is the active variant. + Assume layout: fields[0] is unnamed u8 discriminant; fields[1] is the active variant. """ fields = enum_val.type.fields() if len(fields) < 2: @@ -41,6 +43,7 @@ def _active_variant(enum_val): variant_field = fields[1] return variant_field.name, enum_val[variant_field] + class SmolStrProvider: def __init__(self, val): self.val = val @@ -86,8 +89,10 @@ class SmolStrProvider: length = int(inner["length"]) # ArcInner layout: # strong: Atomic, weak: Atomic | unsized tail 'data' bytes. - sizeof_AtomicUsize = gdb.lookup_type("core::sync::atomic::AtomicUsize").sizeof - header_size = sizeof_AtomicUsize * 2 # strong + weak counters + sizeof_AtomicUsize = gdb.lookup_type( + "core::sync::atomic::AtomicUsize" + ).sizeof + header_size = sizeof_AtomicUsize * 2 # strong + weak counters data_arr = int(data_ptr) + header_size mem = gdb.selected_inferior().read_memory(data_arr, length) return _read_utf8(mem) @@ -99,6 +104,7 @@ class SmolStrProvider: def display_hint(self): return "string" + class SmolStrSubPrinter(gdb.printing.SubPrettyPrinter): def __init__(self): super(SmolStrSubPrinter, self).__init__("SmolStr") @@ -114,6 +120,7 @@ class SmolStrSubPrinter(gdb.printing.SubPrettyPrinter): pass return None + class SmolStrPrettyPrinter(gdb.printing.PrettyPrinter): def __init__(self): super(SmolStrPrettyPrinter, self).__init__("smol_str", []) @@ -129,9 +136,12 @@ class SmolStrPrettyPrinter(gdb.printing.PrettyPrinter): return pp return None + printer = SmolStrPrettyPrinter() + def register_printers(objfile=None): gdb.printing.register_pretty_printer(objfile, printer, replace=True) + register_printers()