diff --git a/src/etc/debugger_tester/gdb/check_gdb.py b/src/etc/debugger_tester/gdb/check_gdb.py index 5c07971ef5046..a5be9b9a198a0 100644 --- a/src/etc/debugger_tester/gdb/check_gdb.py +++ b/src/etc/debugger_tester/gdb/check_gdb.py @@ -154,12 +154,12 @@ def var_matches( value_ok = var.value == expected.value - work_list = [c for _i, c in get_children(valobj)] + work_list = [c for _i, c in get_children(make_visualizer(valobj))] child_types_ok = Result.Ok while len(work_list) != 0: child = work_list.pop() - work_list.extend([c for _i, c in get_children(child)]) + work_list.extend([c for _i, c in get_children(make_visualizer(child))]) # similar to the above, child type mismatches aren't super important if the providers still # work fine. We do still want to output them so they're visible if something else fails diff --git a/src/etc/debugger_tester/gdb/from_gdb.py b/src/etc/debugger_tester/gdb/from_gdb.py index 65a608af7459c..79a2e0c8e929e 100644 --- a/src/etc/debugger_tester/gdb/from_gdb.py +++ b/src/etc/debugger_tester/gdb/from_gdb.py @@ -14,28 +14,39 @@ Variable, ) - -# For whatever reason, gdb doesn't export several of the modules it distributes. It adds its own -# top level module to the search path, but that doesn't let us import the unexported parts inside -# the `gdb` package. To sidestep that, we can manually invoke Python's import pipeline on the -# desired file. -def import_from_path(module_name, file_path) -> ModuleType: - spec = importlib.util.spec_from_file_location(module_name, file_path) - module = importlib.util.module_from_spec(spec) - sys.modules[module_name] = module - spec.loader.exec_module(module) - return module - - -gdb_printing = import_from_path("gdb_printing", gdb.PYTHONDIR + "/gdb/printing.py") - -make_visualizer: Callable[[gdb.Value], "gdb._PrettyPrinter"] = ( - gdb_printing.make_visualizer -) -"""If a pretty printer exists that handles this value's type, returns the pretty printer -instantiated with the given value. If none can be found, returns a "NoOp" pretty printer with the -same interface as a custom pretty printer. -""" +try: + from gdb.printing import make_visualizer +except: # noqa: E722 + pass + # (Walnut356): The below was true for older versions of gdb I think? In newer versions, this + # workaround results in an error during import: + # "pretty-printer already registered: builtin" + # so we just try both + + # For whatever reason, gdb doesn't export several of the modules it distributes. It adds its own + # top level module to the search path, but that doesn't let us import the unexported parts + # inside the `gdb` package. To sidestep that, we can manually invoke Python's import pipeline on + # the desired file. + def import_from_path(module_name, file_path) -> ModuleType: + spec = importlib.util.spec_from_file_location(module_name, file_path) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module) + return module + + # `printing.py` automatically registers `builtin` providers upon import. Reregistering the same + # provider throws an exception, so we yoink it out just before the import. + gdb.pretty_printers = [p for p in gdb.pretty_printers if p.name != "builtin"] + + gdb_printing = import_from_path("gdb_printing", gdb.PYTHONDIR + "/gdb/printing.py") + + make_visualizer: Callable[[gdb.Value], "gdb._PrettyPrinter"] = ( + gdb_printing.make_visualizer + ) + """If a pretty printer exists that handles this value's type, returns the pretty printer + instantiated with the given value. If none can be found, returns a "NoOp" pretty printer with + the same interface as a custom pretty printer. + """ # Same trick as from_lldb.TypeClass/from_lldb.BasicType @@ -106,7 +117,7 @@ def type_from_gdb(ty: gdb.Type) -> Type: def field_from_gdb(field: gdb.Field) -> Field: - return Field(field.name, field.type.name, field.bitpos // 8) + return Field(field.name, get_type_name(field.type), field.bitpos // 8) def variable_from_gdb(var: gdb.Value) -> Variable: @@ -177,7 +188,7 @@ def get_children(obj) -> List[Tuple[str, gdb.Value]]: if children is None: return [] - return children() + return list(children()) def child_from_gdb(ident: str, child: gdb.Value) -> Child: diff --git a/src/etc/debugger_tester/gdb/gdb_commands.py b/src/etc/debugger_tester/gdb/gdb_commands.py index f159117af07c1..ab58b42e12966 100644 --- a/src/etc/debugger_tester/gdb/gdb_commands.py +++ b/src/etc/debugger_tester/gdb/gdb_commands.py @@ -12,14 +12,15 @@ def __init__(self): super().__init__("repr", gdb.COMMAND_OBSCURE) def invoke(self, argument: str, from_tty: bool): - from ..common import Result - from .check_gdb import check + try: + from ..common import Result + from .check_gdb import check - print(f"(gdb) repr {argument}") + print(f"(gdb) repr {argument}") + + global REPR_COMMAND_RUN + REPR_COMMAND_RUN = True - global REPR_COMMAND_RUN - REPR_COMMAND_RUN = True - try: if check(argument) == Result.Mismatch: global REPR_ERROR REPR_ERROR = True diff --git a/tests/debuginfo/pretty-std/gdb_input/non_windows.json b/tests/debuginfo/pretty-std/gdb_input/non_windows.json new file mode 100644 index 0000000000000..2a4e21a9e387a --- /dev/null +++ b/tests/debuginfo/pretty-std/gdb_input/non_windows.json @@ -0,0 +1,387 @@ +{ + "bless_metadata": { + "python_version": "3.14.8 (main, Oct 1 2026, 00:00:00) [GCC 16.2.1 20260819 (Red Hat 16.2.1-2)]", + "debugger_version": "GNU gdb (Fedora Linux) 18.1-1.fc44", + "feature_flags": "" + }, + "breakpoints": [ + { + "slice": { + "type": "&[i32]", + "pretty_print": "&[i32](size=4) = {0, 1, 2, 3}", + "synthetic": "StdSliceProvider", + "summary": "StdSliceProvider.to_string", + "children": [ + { + "name": "[0]", + "type": "i32", + "value": "0" + }, + { + "name": "[1]", + "type": "i32", + "value": "1" + }, + { + "name": "[2]", + "type": "i32", + "value": "2" + }, + { + "name": "[3]", + "type": "i32", + "value": "3" + } + ] + }, + "vec": { + "type": "alloc::vec::Vec", + "pretty_print": "Vec(size=4) = {4, 5, 6, 7}", + "synthetic": "StdVecProvider", + "summary": "StdVecProvider.to_string", + "children": [ + { + "name": "[0]", + "type": "u64", + "value": "4" + }, + { + "name": "[1]", + "type": "u64", + "value": "5" + }, + { + "name": "[2]", + "type": "u64", + "value": "6" + }, + { + "name": "[3]", + "type": "u64", + "value": "7" + } + ] + }, + "str_slice": { + "type": "&str", + "pretty_print": "\"IAMA string slice!\"", + "synthetic": "StdStrProvider", + "summary": "StdStrProvider.to_string" + }, + "string": { + "type": "alloc::string::String", + "pretty_print": "\"IAMA string!\"", + "synthetic": "StdStringProvider", + "summary": "StdStringProvider.to_string" + }, + "some": { + "type": "core::option::Option", + "pretty_print": "core::option::Option::Some(8)", + "children": [ + { + "name": "Some", + "type": "core::option::Option::Some", + "children": [ + { + "name": "__0", + "type": "i16", + "value": "8" + } + ] + } + ] + }, + "none": { + "type": "core::option::Option", + "pretty_print": "core::option::Option::None", + "children": [ + { + "name": "None", + "type": "core::option::Option::None" + } + ] + }, + "os_string": { + "type": "std::ffi::os_str::OsString", + "pretty_print": "\"IAMA OS string \ud83d\ude03\"", + "synthetic": "StdOsStringProvider", + "summary": "StdOsStringProvider.to_string" + }, + "some_string": { + "type": "core::option::Option", + "pretty_print": "core::option::Option::Some(\"IAMA optional string!\")", + "children": [ + { + "name": "Some", + "type": "core::option::Option::Some", + "children": [ + { + "name": "__0", + "type": "alloc::string::String" + } + ] + } + ] + } + } + ], + "types": { + "&[i32]": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "data_ptr", + "type": "*mut i32", + "offset": 0 + }, + { + "name": "length", + "type": "usize", + "offset": 8 + } + ] + }, + "&str": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "data_ptr", + "type": "*mut u8", + "offset": 0 + }, + { + "name": "length", + "type": "usize", + "offset": 8 + } + ] + }, + "*mut i32": { + "size": 8, + "type_class": 0, + "basic_type": 1 + }, + "*mut u8": { + "size": 8, + "type_class": 0, + "basic_type": 1 + }, + "alloc::alloc::Global": { + "size": 0, + "type_class": 0, + "basic_type": 3 + }, + "alloc::raw_vec::RawVec": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "alloc::raw_vec::RawVecInner", + "offset": 0 + }, + { + "name": "_marker", + "type": "core::marker::PhantomData", + "offset": 16 + } + ], + "generic_params": [ + "u64", + "alloc::alloc::Global" + ] + }, + "alloc::raw_vec::RawVec": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "alloc::raw_vec::RawVecInner", + "offset": 0 + }, + { + "name": "_marker", + "type": "core::marker::PhantomData", + "offset": 16 + } + ], + "generic_params": [ + "u8", + "alloc::alloc::Global" + ] + }, + "alloc::raw_vec::RawVecInner": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "ptr", + "type": "core::ptr::non_null::NonNull", + "offset": 8 + }, + { + "name": "cap", + "type": "core::num::niche_types::UsizeNoHighBit", + "offset": 0 + }, + { + "name": "alloc", + "type": "alloc::alloc::Global", + "offset": 16 + } + ], + "generic_params": [ + "alloc::alloc::Global" + ] + }, + "alloc::string::String": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "vec", + "type": "alloc::vec::Vec", + "offset": 0 + } + ] + }, + "alloc::vec::Vec": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "buf", + "type": "alloc::raw_vec::RawVec", + "offset": 0 + }, + { + "name": "len", + "type": "usize", + "offset": 16 + } + ], + "generic_params": [ + "u64", + "alloc::alloc::Global" + ] + }, + "alloc::vec::Vec": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "buf", + "type": "alloc::raw_vec::RawVec", + "offset": 0 + }, + { + "name": "len", + "type": "usize", + "offset": 16 + } + ], + "generic_params": [ + "u8", + "alloc::alloc::Global" + ] + }, + "core::marker::PhantomData": { + "size": 0, + "type_class": 0, + "basic_type": 3, + "generic_params": [ + "u64" + ] + }, + "core::marker::PhantomData": { + "size": 0, + "type_class": 0, + "basic_type": 3, + "generic_params": [ + "u8" + ] + }, + "core::num::niche_types::UsizeNoHighBit": { + "size": 8, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "__0", + "type": "usize", + "offset": 0 + } + ] + }, + "core::ptr::non_null::NonNull": { + "size": 8, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "pointer", + "type": "*mut u8", + "offset": 0 + } + ], + "generic_params": [ + "u8" + ] + }, + "i32": { + "size": 4, + "type_class": 0, + "basic_type": 8 + }, + "std::ffi::os_str::OsString": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "std::sys::os_str::bytes::Buf", + "offset": 0 + } + ] + }, + "std::sys::os_str::bytes::Buf": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "alloc::vec::Vec", + "offset": 0 + } + ] + }, + "u64": { + "size": 8, + "type_class": 0, + "basic_type": 8 + }, + "u8": { + "size": 1, + "type_class": 0, + "basic_type": 8 + }, + "usize": { + "size": 8, + "type_class": 0, + "basic_type": 23 + } + } +} diff --git a/tests/debuginfo/pretty-std/gdb_input/windows_gnu.json b/tests/debuginfo/pretty-std/gdb_input/windows_gnu.json new file mode 100644 index 0000000000000..2fcdd814266d1 --- /dev/null +++ b/tests/debuginfo/pretty-std/gdb_input/windows_gnu.json @@ -0,0 +1,409 @@ +{ + "bless_metadata": { + "python_version": "3.9.7 (heads/mingw-v3.9.7-dirty:aa916ed5c9, Oct 4 2025, 17:26:50) [GCC 15.2.0 64 bit (AMD64)]", + "debugger_version": "GNU gdb (GDB for MinGW-W64 x86_64, built by Brecht Sanders, r2) 16.3", + "feature_flags": "" + }, + "breakpoints": [ + { + "slice": { + "type": "&[i32]", + "pretty_print": "&[i32](size=4) = {0, 1, 2, 3}", + "synthetic": "StdSliceProvider", + "summary": "StdSliceProvider.to_string", + "children": [ + { + "name": "[0]", + "type": "i32", + "value": "0" + }, + { + "name": "[1]", + "type": "i32", + "value": "1" + }, + { + "name": "[2]", + "type": "i32", + "value": "2" + }, + { + "name": "[3]", + "type": "i32", + "value": "3" + } + ] + }, + "vec": { + "type": "alloc::vec::Vec", + "pretty_print": "Vec(size=4) = {4, 5, 6, 7}", + "synthetic": "StdVecProvider", + "summary": "StdVecProvider.to_string", + "children": [ + { + "name": "[0]", + "type": "u64", + "value": "4" + }, + { + "name": "[1]", + "type": "u64", + "value": "5" + }, + { + "name": "[2]", + "type": "u64", + "value": "6" + }, + { + "name": "[3]", + "type": "u64", + "value": "7" + } + ] + }, + "str_slice": { + "type": "&str", + "pretty_print": "\"IAMA string slice!\"", + "synthetic": "StdStrProvider", + "summary": "StdStrProvider.to_string" + }, + "string": { + "type": "alloc::string::String", + "pretty_print": "\"IAMA string!\"", + "synthetic": "StdStringProvider", + "summary": "StdStringProvider.to_string" + }, + "some": { + "type": "core::option::Option", + "pretty_print": "core::option::Option::Some(8)", + "children": [ + { + "name": "Some", + "type": "core::option::Option::Some", + "children": [ + { + "name": "__0", + "type": "i16", + "value": "8" + } + ] + } + ] + }, + "none": { + "type": "core::option::Option", + "pretty_print": "core::option::Option::None", + "children": [ + { + "name": "None", + "type": "core::option::Option::None" + } + ] + }, + "os_string": { + "type": "std::ffi::os_str::OsString", + "pretty_print": "\"IAMA OS string \\360\\237\\230\\203\"", + "synthetic": "StdOsStringProvider", + "summary": "StdOsStringProvider.to_string" + }, + "some_string": { + "type": "core::option::Option", + "pretty_print": "core::option::Option::Some(\"IAMA optional string!\")", + "children": [ + { + "name": "Some", + "type": "core::option::Option::Some", + "children": [ + { + "name": "__0", + "type": "alloc::string::String" + } + ] + } + ] + } + } + ], + "types": { + "&[i32]": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "data_ptr", + "type": "*mut i32", + "offset": 0 + }, + { + "name": "length", + "type": "usize", + "offset": 8 + } + ] + }, + "&str": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "data_ptr", + "type": "*mut u8", + "offset": 0 + }, + { + "name": "length", + "type": "usize", + "offset": 8 + } + ] + }, + "*mut i32": { + "size": 8, + "type_class": 0, + "basic_type": 1 + }, + "*mut u8": { + "size": 8, + "type_class": 0, + "basic_type": 1 + }, + "alloc::alloc::Global": { + "size": 0, + "type_class": 0, + "basic_type": 3 + }, + "alloc::raw_vec::RawVec": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "alloc::raw_vec::RawVecInner", + "offset": 0 + }, + { + "name": "_marker", + "type": "core::marker::PhantomData", + "offset": 16 + } + ], + "generic_params": [ + "u64", + "alloc::alloc::Global" + ] + }, + "alloc::raw_vec::RawVec": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "alloc::raw_vec::RawVecInner", + "offset": 0 + }, + { + "name": "_marker", + "type": "core::marker::PhantomData", + "offset": 16 + } + ], + "generic_params": [ + "u8", + "alloc::alloc::Global" + ] + }, + "alloc::raw_vec::RawVecInner": { + "size": 16, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "ptr", + "type": "core::ptr::non_null::NonNull", + "offset": 8 + }, + { + "name": "cap", + "type": "core::num::niche_types::UsizeNoHighBit", + "offset": 0 + }, + { + "name": "alloc", + "type": "alloc::alloc::Global", + "offset": 16 + } + ], + "generic_params": [ + "alloc::alloc::Global" + ] + }, + "alloc::string::String": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "vec", + "type": "alloc::vec::Vec", + "offset": 0 + } + ] + }, + "alloc::vec::Vec": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "buf", + "type": "alloc::raw_vec::RawVec", + "offset": 0 + }, + { + "name": "len", + "type": "usize", + "offset": 16 + } + ], + "generic_params": [ + "u64", + "alloc::alloc::Global" + ] + }, + "alloc::vec::Vec": { + "size": 24, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "buf", + "type": "alloc::raw_vec::RawVec", + "offset": 0 + }, + { + "name": "len", + "type": "usize", + "offset": 16 + } + ], + "generic_params": [ + "u8", + "alloc::alloc::Global" + ] + }, + "alloc::wtf8::Wtf8Buf": { + "size": 32, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "bytes", + "type": "alloc::vec::Vec", + "offset": 0 + }, + { + "name": "is_known_utf8", + "type": "bool", + "offset": 24 + } + ] + }, + "bool": { + "size": 1, + "type_class": 0, + "basic_type": 21 + }, + "core::marker::PhantomData": { + "size": 0, + "type_class": 0, + "basic_type": 3, + "generic_params": [ + "u64" + ] + }, + "core::marker::PhantomData": { + "size": 0, + "type_class": 0, + "basic_type": 3, + "generic_params": [ + "u8" + ] + }, + "core::num::niche_types::UsizeNoHighBit": { + "size": 8, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "__0", + "type": "usize", + "offset": 0 + } + ] + }, + "core::ptr::non_null::NonNull": { + "size": 8, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "pointer", + "type": "*mut u8", + "offset": 0 + } + ], + "generic_params": [ + "u8" + ] + }, + "i32": { + "size": 4, + "type_class": 0, + "basic_type": 8 + }, + "std::ffi::os_str::OsString": { + "size": 32, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "std::sys::os_str::wtf8::Buf", + "offset": 0 + } + ] + }, + "std::sys::os_str::wtf8::Buf": { + "size": 32, + "type_class": 0, + "basic_type": 3, + "fields": [ + { + "name": "inner", + "type": "alloc::wtf8::Wtf8Buf", + "offset": 0 + } + ] + }, + "u64": { + "size": 8, + "type_class": 0, + "basic_type": 8 + }, + "u8": { + "size": 1, + "type_class": 0, + "basic_type": 8 + }, + "usize": { + "size": 8, + "type_class": 0, + "basic_type": 23 + } + } +} diff --git a/tests/debuginfo/pretty-std/main.rs b/tests/debuginfo/pretty-std/main.rs index 326ea32b2576f..879baf77877d7 100644 --- a/tests/debuginfo/pretty-std/main.rs +++ b/tests/debuginfo/pretty-std/main.rs @@ -16,39 +16,24 @@ //@ min-llvm-lldb-version: 21.1.0 //@ min-apple-lldb-version: 1703.0.236.21 //@ min-cdb-version: 10.0.18317.1001 + +// Due to `debugger_tester` reliance on `make_visualizer` +//@ min-gdb-version: 14.0 + //@ ignore-backends: gcc // === GDB TESTS =================================================================================== //@ gdb-command: run -//@ gdb-command: print slice -//@ gdb-check:$1 = &[i32](size=4) = {0, 1, 2, 3} - -//@ gdb-command: print vec -//@ gdb-check:$2 = Vec(size=4) = {4, 5, 6, 7} - -//@ gdb-command: print str_slice -//@ gdb-check:$3 = "IAMA string slice!" - -//@ gdb-command: print string -//@ gdb-check:$4 = "IAMA string!" - -//@ gdb-command: print some -//@ gdb-check:$5 = core::option::Option::Some(8) - -//@ gdb-command: print none -//@ gdb-check:$6 = core::option::Option::None - -//@ gdb-command: print os_string -//@ gdb-check:$7 = "IAMA OS string [...]" - -//@ gdb-command: print some_string -//@ gdb-check:$8 = core::option::Option::Some("IAMA optional string!") - -//@ gdb-command: set print elements 5 -//@ gdb-command: print some_string -//@ gdb-check:$9 = core::option::Option::Some("IAMA "...) +//@ gdb-repr:slice +//@ gdb-repr:vec +//@ gdb-repr:str_slice +//@ gdb-repr:string +//@ gdb-repr:some +//@ gdb-repr:none +//@ gdb-repr:os_string +//@ gdb-repr:some_string // === LLDB TESTS ==================================================================================