From a4f2f1133e30b9585ab81cc2b887cbce7e51a47a Mon Sep 17 00:00:00 2001 From: Walnut <39544927+Walnut356@users.noreply.github.com> Date: Thu, 24 Jul 2025 04:06:33 -0500 Subject: [PATCH 1/5] generate typedef nodes for ptr/ref and array --- .../src/debuginfo/metadata.rs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 0e9dbfba658d2..04d040581a084 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -120,7 +120,7 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>( unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) }; let subscripts = create_DIArray(DIB(cx), &[subrange]); - let di_node = unsafe { + let mut di_node = unsafe { llvm::LLVMRustDIBuilderCreateArrayType( DIB(cx), size.bits(), @@ -130,6 +130,21 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>( ) }; + if cpp_like_debuginfo(cx.tcx) { + let array_type_name = compute_debuginfo_type_name(cx.tcx, array_type, false); + di_node = unsafe { + llvm::LLVMRustDIBuilderCreateTypedef( + DIB(cx), + di_node, + array_type_name.as_c_char_ptr(), + array_type_name.len(), + unknown_file_metadata(cx), + UNKNOWN_LINE_NUMBER, + None, + ) + }; + } + DINodeCreationResult::new(di_node, false) } @@ -183,8 +198,19 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>( ptr_type_debuginfo_name.len(), ) }; + let typedefed_ptr = unsafe { + llvm::LLVMRustDIBuilderCreateTypedef( + DIB(cx), + di_node, + ptr_type_debuginfo_name.as_c_char_ptr(), + ptr_type_debuginfo_name.len(), + unknown_file_metadata(cx), + UNKNOWN_LINE_NUMBER, + None, + ) + }; - DINodeCreationResult { di_node, already_stored_in_typemap: false } + DINodeCreationResult { di_node: typedefed_ptr, already_stored_in_typemap: false } } Some(wide_pointer_kind) => { type_map::build_type_with_children( From 8b4f5db662a565c5bfb6032b18748cf6bdb1026c Mon Sep 17 00:00:00 2001 From: Walnut <39544927+Walnut356@users.noreply.github.com> Date: Sat, 26 Jul 2025 02:48:57 -0500 Subject: [PATCH 2/5] check typedef underlying matches ptr type --- tests/codegen-llvm/debug-vtable.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/codegen-llvm/debug-vtable.rs b/tests/codegen-llvm/debug-vtable.rs index 8a7b1cc3c4bcc..d08a52d1634d1 100644 --- a/tests/codegen-llvm/debug-vtable.rs +++ b/tests/codegen-llvm/debug-vtable.rs @@ -21,19 +21,21 @@ // NONMSVC: ![[USIZE:[0-9]+]] = !DIBasicType(name: "usize" // MSVC: ![[USIZE:[0-9]+]] = !DIDerivedType(tag: DW_TAG_typedef, name: "usize" -// NONMSVC: ![[PTR:[0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "*const ()" -// MSVC: ![[PTR:[0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "ptr_const$ >" +// NONMSVC: ![[PTR_TYPEDEF:[0-9]+]] = !DIDerivedType(tag: DW_TAG_typedef, name: "*const ()", file: {{.*}}, baseType: ![[PTR:[0-9]+]]) +// MSVC: ![[PTR_TYPEDEF:[0-9]+]] = !DIDerivedType(tag: DW_TAG_typedef, name: "ptr_const$ >", file: {{.*}}, baseType: ![[PTR:[0-9]+]]) +// NONMSVC: ![[PTR]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "*const ()" +// MSVC: ![[PTR]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "ptr_const$ >" // NONMSVC: !DIGlobalVariable(name: "::{vtable}" // MSVC: !DIGlobalVariable(name: "impl$::vtable$" // NONMSVC: ![[VTABLE_TY0:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "::{vtable_type}", {{.*}} size: {{320|160}}, align: {{64|32}}, flags: DIFlagArtificial, {{.*}} vtableHolder: ![[FOO_TYPE:[0-9]+]], // MSVC: ![[VTABLE_TY0:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "impl$::vtable_type$", {{.*}} size: {{320|160}}, align: {{64|32}}, flags: DIFlagArtificial, {{.*}} vtableHolder: ![[FOO_TYPE:[0-9]+]], -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "drop_in_place", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[PTR]], size: {{64|32}}, align: {{64|32}}) +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "drop_in_place", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[PTR_TYPEDEF]], size: {{64|32}}, align: {{64|32}}) // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "size", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[USIZE]], size: {{64|32}}, align: {{64|32}}, offset: {{64|32}}) // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "align", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[USIZE]], size: {{64|32}}, align: {{64|32}}, offset: {{128|64}}) -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__method3", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[PTR]], size: {{64|32}}, align: {{64|32}}, offset: {{192|96}}) -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__method4", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[PTR]], size: {{64|32}}, align: {{64|32}}, offset: {{256|128}}) +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__method3", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[PTR_TYPEDEF]], size: {{64|32}}, align: {{64|32}}, offset: {{192|96}}) +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__method4", scope: ![[VTABLE_TY0]], {{.*}} baseType: ![[PTR_TYPEDEF]], size: {{64|32}}, align: {{64|32}}, offset: {{256|128}}) // CHECK: ![[FOO_TYPE]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", // NONMSVC: !DIGlobalVariable(name: ">::{vtable}" @@ -41,17 +43,17 @@ // NONMSVC: ![[VTABLE_TY1:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: ">::{vtable_type}", {{.*}}, size: {{256|128}}, align: {{64|32}}, flags: DIFlagArtificial, {{.*}}, vtableHolder: ![[FOO_TYPE]], // MSVC: ![[VTABLE_TY1:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "impl$ >::vtable_type$", {{.*}}, size: {{256|128}}, align: {{64|32}}, flags: DIFlagArtificial, {{.*}}, vtableHolder: ![[FOO_TYPE]], -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "drop_in_place", scope: ![[VTABLE_TY1]], {{.*}} baseType: ![[PTR]], size: {{64|32}}, align: {{64|32}}) +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "drop_in_place", scope: ![[VTABLE_TY1]], {{.*}} baseType: ![[PTR_TYPEDEF]], size: {{64|32}}, align: {{64|32}}) // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "size", scope: ![[VTABLE_TY1]], {{.*}} baseType: ![[USIZE]], size: {{64|32}}, align: {{64|32}}, offset: {{64|32}}) // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "align", scope: ![[VTABLE_TY1]], {{.*}} baseType: ![[USIZE]], size: {{64|32}}, align: {{64|32}}, offset: {{128|64}}) -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__method3", scope: ![[VTABLE_TY1]], {{.*}} baseType: ![[PTR]], size: {{64|32}}, align: {{64|32}}, offset: {{192|96}}) +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "__method3", scope: ![[VTABLE_TY1]], {{.*}} baseType: ![[PTR_TYPEDEF]], size: {{64|32}}, align: {{64|32}}, offset: {{192|96}}) // NONMSVC: !DIGlobalVariable(name: "::{vtable}" // MSVC: !DIGlobalVariable(name: "impl$::vtable$" // NONMSVC: ![[VTABLE_TY2:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "::{vtable_type}", {{.*}}, size: {{192|96}}, align: {{64|32}}, flags: DIFlagArtificial, {{.*}}, vtableHolder: ![[FOO_TYPE]], // MSVC: ![[VTABLE_TY2:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "impl$::vtable_type$", {{.*}}, size: {{192|96}}, align: {{64|32}}, flags: DIFlagArtificial, {{.*}}, vtableHolder: ![[FOO_TYPE]], -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "drop_in_place", scope: ![[VTABLE_TY2]], {{.*}}, baseType: ![[PTR]], size: {{64|32}}, align: {{64|32}}) +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "drop_in_place", scope: ![[VTABLE_TY2]], {{.*}}, baseType: ![[PTR_TYPEDEF]], size: {{64|32}}, align: {{64|32}}) // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "size", scope: ![[VTABLE_TY2]], {{.*}}, baseType: ![[USIZE]], size: {{64|32}}, align: {{64|32}}, offset: {{64|32}}) // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "align", scope: ![[VTABLE_TY2]], {{.*}}, baseType: ![[USIZE]], size: {{64|32}}, align: {{64|32}}, offset: {{128|64}}) From 788e1dd76136ed78aa560ec21ae57fccb04469e1 Mon Sep 17 00:00:00 2001 From: Walnut <39544927+Walnut356@users.noreply.github.com> Date: Sun, 27 Jul 2025 01:57:47 -0500 Subject: [PATCH 3/5] update gdb tests --- tests/debuginfo/basic-types-metadata.rs | 6 +++--- tests/debuginfo/function-call.rs | 2 +- tests/debuginfo/function-names.rs | 6 +++--- tests/debuginfo/type-names.rs | 4 ++-- tests/debuginfo/unit-type.rs | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/debuginfo/basic-types-metadata.rs b/tests/debuginfo/basic-types-metadata.rs index 6b7cfbdebca92..ba39b7eca49f6 100644 --- a/tests/debuginfo/basic-types-metadata.rs +++ b/tests/debuginfo/basic-types-metadata.rs @@ -41,12 +41,12 @@ // gdb-check: type = struct basic_types_metadata::main::{closure_env#0} // gdb-command:ptype closure_1 // gdb-check: type = struct basic_types_metadata::main::{closure_env#1} { -// gdb-check: *mut bool, +// gdb-check: _ref__b: &bool, // gdb-check: } // gdb-command:ptype closure_2 // gdb-check: type = struct basic_types_metadata::main::{closure_env#2} { -// gdb-check: *mut bool, -// gdb-check: *mut isize, +// gdb-check: _ref__b: &bool, +// gdb-check: _ref__i: &isize, // gdb-check: } // diff --git a/tests/debuginfo/function-call.rs b/tests/debuginfo/function-call.rs index 3d8d798928d7f..f588e158f36f8 100644 --- a/tests/debuginfo/function-call.rs +++ b/tests/debuginfo/function-call.rs @@ -12,7 +12,7 @@ // gdb-command:print fun(444, false) // gdb-check:$2 = false -// gdb-command:print r.get_x() +// gdb-command: print function_call::RegularStruct::get_x(&r) // gdb-check:$3 = 4 #![allow(dead_code, unused_variables)] diff --git a/tests/debuginfo/function-names.rs b/tests/debuginfo/function-names.rs index c51884451e56f..5f0ec7ec36ea2 100644 --- a/tests/debuginfo/function-names.rs +++ b/tests/debuginfo/function-names.rs @@ -27,9 +27,9 @@ // Closure // gdb-command:info functions -q function_names::.*::{closure.* -// gdb-check:[...]static fn function_names::generic_func::{closure#0}(*mut function_names::generic_func::{closure_env#0}); -// gdb-check:[...]static fn function_names::main::{closure#0}(*mut function_names::main::{closure_env#0}); -// gdb-check:[...]static fn function_names::{impl#2}::impl_function::{closure#0}(*mut function_names::{impl#2}::impl_function::{closure_env#0}); +// gdb-check:[...]static fn function_names::generic_func::{closure#0}(&function_names::generic_func::{closure_env#0}); +// gdb-check:[...]static fn function_names::main::{closure#0}(&function_names::main::{closure_env#0}); +// gdb-check:[...]static fn function_names::{impl#2}::impl_function::{closure#0}(&function_names::{impl#2}::impl_function::{closure_env#0}); // Coroutine // Coroutines don't seem to appear in GDB's symbol table. diff --git a/tests/debuginfo/type-names.rs b/tests/debuginfo/type-names.rs index ac61fef48fe16..64236aa3ba07a 100644 --- a/tests/debuginfo/type-names.rs +++ b/tests/debuginfo/type-names.rs @@ -164,10 +164,10 @@ // FOREIGN TYPES // gdb-command:whatis foreign1 -// gdb-check:type = *mut type_names::{extern#0}::ForeignType1 +// gdb-check:type = *const type_names::{extern#0}::ForeignType1 // gdb-command:whatis foreign2 -// gdb-check:type = *mut type_names::mod1::{extern#0}::ForeignType2 +// gdb-check:type = *const type_names::mod1::{extern#0}::ForeignType2 // === CDB TESTS ================================================================================== diff --git a/tests/debuginfo/unit-type.rs b/tests/debuginfo/unit-type.rs index 0ffa2fe490a5e..ac3dd2f21aafa 100644 --- a/tests/debuginfo/unit-type.rs +++ b/tests/debuginfo/unit-type.rs @@ -8,10 +8,10 @@ // gdb-command: run // gdb-command: print _ref -// gdb-check: $1 = (*mut ()) 0x[...] +// gdb-check: $1 = (&()) 0x[...] // gdb-command: print _ptr -// gdb-check: $2 = (*mut ()) 0x[...] +// gdb-check: $2 = (*const ()) 0x[...] // gdb-command: print _local // gdb-check: $3 = () From 2369b83d9389ad433fae80b8c3a1cba445df4bd6 Mon Sep 17 00:00:00 2001 From: Walnut <39544927+Walnut356@users.noreply.github.com> Date: Sun, 27 Jul 2025 01:58:01 -0500 Subject: [PATCH 4/5] look through pointer typedefs --- src/etc/gdb_providers.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/etc/gdb_providers.py b/src/etc/gdb_providers.py index c8f4a32cb17e2..9afabaed4b863 100644 --- a/src/etc/gdb_providers.py +++ b/src/etc/gdb_providers.py @@ -138,8 +138,9 @@ def __init__(self, valobj): self._valobj = valobj self._length = int(valobj["len"]) self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["inner"]["ptr"]) - ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0)) - self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty) + self._data_ptr = self._data_ptr.cast(self._data_ptr.type.strip_typedefs()) + ptr_ty = valobj.type.template_argument(0).pointer() + self._data_ptr = self._data_ptr.cast(ptr_ty) def to_string(self): return "Vec(size={})".format(self._length) @@ -165,8 +166,9 @@ def __init__(self, valobj): cap = cap[ZERO_FIELD] self._cap = int(cap) self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["inner"]["ptr"]) - ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0)) - self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty) + self._data_ptr = self._data_ptr.cast(self._data_ptr.type.strip_typedefs()) + ptr_ty = valobj.type.template_argument(0).pointer() + self._data_ptr = self._data_ptr.cast(ptr_ty) def to_string(self): return "VecDeque(size={})".format(self._size) From 4b1cdeb43aa80c8d4c52a4b70dee356ca8f798ac Mon Sep 17 00:00:00 2001 From: Walnut <39544927+Walnut356@users.noreply.github.com> Date: Sun, 27 Jul 2025 02:25:45 -0500 Subject: [PATCH 5/5] look through nonnull pointer typedef --- src/etc/gdb_providers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/etc/gdb_providers.py b/src/etc/gdb_providers.py index 9afabaed4b863..70fc726a5f9d8 100644 --- a/src/etc/gdb_providers.py +++ b/src/etc/gdb_providers.py @@ -15,6 +15,9 @@ def unwrap_unique_or_non_null(unique_or_nonnull): # BACKCOMPAT: rust 1.60 # https://github.com/rust-lang/rust/commit/2a91eeac1a2d27dd3de1bf55515d765da20fd86f ptr = unique_or_nonnull["pointer"] + if ptr.type.code == gdb.TYPE_CODE_TYPEDEF: + ptr = ptr.cast(ptr.type.strip_typedefs()) + return ptr if ptr.type.code == gdb.TYPE_CODE_PTR else ptr[ptr.type.fields()[0]]