From ca3a5b09062f0bf937a964ecb8bfd0a03736a6a2 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/6] generate typedef nodes for ptr/ref and array --- .../src/debuginfo/metadata.rs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 04c0b6953290c..8baa7d8822923 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -128,6 +128,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) } @@ -177,8 +192,19 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>( pointer_align.abi, &ptr_type_debuginfo_name, ); + 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 8627e998f271fc01de6d18282cb8094f91863b36 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/6] 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 2088f9060af98f123c51b86532772d77ae46e136 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/6] 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 d3a3d03ef7424..5bccecd7bc785 100644 --- a/tests/debuginfo/basic-types-metadata.rs +++ b/tests/debuginfo/basic-types-metadata.rs @@ -43,12 +43,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: } //@ gdb-command:continue diff --git a/tests/debuginfo/function-call.rs b/tests/debuginfo/function-call.rs index 37eda165d99ca..fe89c4b71d2cf 100644 --- a/tests/debuginfo/function-call.rs +++ b/tests/debuginfo/function-call.rs @@ -13,7 +13,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 5973f63bbae9e..01d135be6183e 100644 --- a/tests/debuginfo/function-names.rs +++ b/tests/debuginfo/function-names.rs @@ -29,9 +29,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 3b15e0541867e..72f4ce718a000 100644 --- a/tests/debuginfo/type-names.rs +++ b/tests/debuginfo/type-names.rs @@ -166,10 +166,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 5c6b81e8b5e7f..adba9cf192568 100644 --- a/tests/debuginfo/unit-type.rs +++ b/tests/debuginfo/unit-type.rs @@ -9,10 +9,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 502055cf9acbffbcf9e087cd5cc1373ea33ff565 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/6] 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 bd27998b37706..e155ad46ee636 100644 --- a/src/etc/gdb_providers.py +++ b/src/etc/gdb_providers.py @@ -147,8 +147,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) @@ -177,8 +178,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 ac220e8f1f47920962142943f7e567e34cf0ea95 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/6] 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 e155ad46ee636..6202fd6327798 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]] From afca5a9e75d9500223339135e49d4bb020315af7 Mon Sep 17 00:00:00 2001 From: Walnut <39544927+Walnut356@users.noreply.github.com> Date: Thu, 2 Oct 2025 04:51:48 -0500 Subject: [PATCH 6/6] use updated dibuilder --- .../rustc_codegen_llvm/src/debuginfo/metadata.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 8baa7d8822923..81bb676fa9e1e 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -117,7 +117,7 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>( let subrange = unsafe { llvm::LLVMDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) }; let subscripts = &[subrange]; - let di_node = unsafe { + let mut di_node = unsafe { llvm::LLVMDIBuilderCreateArrayType( DIB(cx), size.bits(), @@ -131,14 +131,15 @@ 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( + llvm::LLVMDIBuilderCreateTypedef( DIB(cx), di_node, - array_type_name.as_c_char_ptr(), + array_type_name.as_ptr(), array_type_name.len(), unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER, None, + 0, ) }; } @@ -193,14 +194,15 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>( &ptr_type_debuginfo_name, ); let typedefed_ptr = unsafe { - llvm::LLVMRustDIBuilderCreateTypedef( + llvm::LLVMDIBuilderCreateTypedef( DIB(cx), di_node, - ptr_type_debuginfo_name.as_c_char_ptr(), + ptr_type_debuginfo_name.as_ptr(), ptr_type_debuginfo_name.len(), unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER, None, + 0, ) };