Skip to content

[Debuginfo] Use Reference DINode for references, delineate between mut/non-mut ref/ptr #136080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/dwarf_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ macro_rules! declare_constant {
};
}

// DWARF attribute tags
declare_constant!(DW_TAG_const_type: c_uint);
declare_constant!(DW_TAG_reference_type: c_uint);

// DWARF languages.
declare_constant!(DW_LANG_Rust: c_uint);
Expand Down
122 changes: 112 additions & 10 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,118 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
"ptr_type={ptr_type}, pointee_type={pointee_type}",
);

let di_node = unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
data_layout.pointer_size.bits(),
data_layout.pointer_align.abi.bits() as u32,
0, // Ignore DWARF address space.
ptr_type_debuginfo_name.as_c_char_ptr(),
ptr_type_debuginfo_name.len(),
)
let di_node = match (ptr_type.kind(), pointee_type.kind()) {
// if we have a ref-to-ref, treat the outter ref as a pointer and wrap it in a `ref$<>` pseudo-struct.
// This is necessary because LLDB (or more specifically, `TypeSystemClang`) seems to implicitly treat
// references to references as invalid, and treats them as a reference to the first
// non-reference/pointer underlying type (e.g. "&&u8" -> "&u8", "&&&&&u8" -> "&u8"). This is expected
// behavior for C/C++ since ref-to-ref isn't valid in those languages, so it's not possible to fix on
// the LLDB end outside of creating a `TypeSystemRust`.
(ty::Ref(_, _, ptr_mut), ty::Ref(_, _, _)) => unsafe {
let qualified_ref = llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
data_layout.pointer_size.bits(),
data_layout.pointer_align.abi.bits() as u32,
0, // Ignore DWARF address space.
ptr_type_debuginfo_name.as_c_char_ptr(),
ptr_type_debuginfo_name.len(),
);

let qualified_ref = if ptr_mut.is_not() {
llvm::LLVMRustDIBuilderCreateQualifiedType(
DIB(cx),
dwarf_const::DW_TAG_const_type,
qualified_ref,
)
} else {
qualified_ref
};

// now we create a wrapper struct that holds the pointer. This provides the
// info necessary to differentiate a pointer from a ref, but doesn't run into
// the ref-to-ref problem

return type_map::build_type_with_children(
cx,
type_map::stub(
cx,
Stub::Struct,
unique_type_id,
&ptr_type_debuginfo_name,
None,
cx.size_and_align_of(ptr_type),
NO_SCOPE_METADATA,
DIFlags::FlagZero,
),
|cx, owner| {
smallvec![build_field_di_node(
cx,
owner,
"ptr",
cx.size_and_align_of(ptr_type),
Size::ZERO,
DIFlags::FlagZero,
qualified_ref,
None,
)]
},
|_cx| smallvec![pointee_type_di_node],
);
},
// if we have a ref-to-<not a ref>, apply `const` to the inner value as necessary
(ty::Ref(_, _, ptr_mut), _) => unsafe {
let pointee_type_di_node = if ptr_mut.is_not() {
llvm::LLVMRustDIBuilderCreateQualifiedType(
DIB(cx),
dwarf_const::DW_TAG_const_type,
pointee_type_di_node,
)
} else {
pointee_type_di_node
};

llvm::LLVMRustDIBuilderCreateReferenceType(
DIB(cx),
dwarf_const::DW_TAG_reference_type,
pointee_type_di_node,
)
},
// if we have any pointer, apply `const` to the inner value as necessary
(ty::RawPtr(_, ptr_mut), _) => unsafe {
let pointee_type_di_node = if ptr_mut.is_not() {
llvm::LLVMRustDIBuilderCreateQualifiedType(
DIB(cx),
dwarf_const::DW_TAG_const_type,
pointee_type_di_node,
)
} else {
pointee_type_di_node
};

llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
data_layout.pointer_size.bits(),
data_layout.pointer_align.abi.bits() as u32,
0, // Ignore DWARF address space.
ptr_type_debuginfo_name.as_c_char_ptr(),
ptr_type_debuginfo_name.len(),
)
},
// apply no modifications to `Box`
(ty::Adt(_, _), _) => unsafe {
llvm::LLVMRustDIBuilderCreatePointerType(
DIB(cx),
pointee_type_di_node,
data_layout.pointer_size.bits(),
data_layout.pointer_align.abi.bits() as u32,
0, // Ignore DWARF address space.
ptr_type_debuginfo_name.as_c_char_ptr(),
ptr_type_debuginfo_name.len(),
)
},
_ => todo!(),
};

DINodeCreationResult { di_node, already_stored_in_typemap: false }
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,12 @@ unsafe extern "C" {
Type: &'a DIType,
) -> &'a DIDerivedType;

pub fn LLVMRustDIBuilderCreateReferenceType<'a>(
Builder: &DIBuilder<'a>,
Tag: c_uint,
Type: &'a DIType,
) -> &'a DIDerivedType;

pub fn LLVMRustDIBuilderCreateLexicalBlock<'a>(
Builder: &DIBuilder<'a>,
Scope: &'a DIScope,
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ fn push_debuginfo_type_name<'tcx>(
}
}
ty::Ref(_, inner_type, mutbl) => {
if cpp_like_debuginfo {
// Use the MSVC style whenever we have a ref-to-ref, since LLDB does not properly handle
// ref-to-ref.
if cpp_like_debuginfo || matches!(inner_type.kind(), ty::Ref(_, _, _)) {
match mutbl {
Mutability::Not => output.push_str("ref$<"),
Mutability::Mut => output.push_str("ref_mut$<"),
Expand All @@ -172,7 +174,7 @@ fn push_debuginfo_type_name<'tcx>(

push_debuginfo_type_name(tcx, inner_type, qualified, output, visited);

if cpp_like_debuginfo {
if cpp_like_debuginfo || matches!(inner_type.kind(), ty::Ref(_, _, _)) {
push_close_angle_bracket(cpp_like_debuginfo, output);
}
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,13 @@ LLVMRustDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
unwrap(Builder)->createQualifiedType(Tag, unwrapDI<DIType>(Type)));
}

extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
LLVMMetadataRef Type) {
return wrap(
unwrap(Builder)->createReferenceType(Tag, unwrapDI<DIType>(Type)));
}

extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateLexicalBlock(LLVMRustDIBuilderRef Builder,
LLVMMetadataRef Scope, LLVMMetadataRef File,
Expand Down
5 changes: 5 additions & 0 deletions src/etc/gdb_load_rust_pretty_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
gdb_lookup.register_printers(gdb.current_objfile())
except Exception:
gdb_lookup.register_printers(gdb.selected_inferior().progspace)

try:
gdb_lookup.register_type_printers(gdb.current_objfile())
except Exception:
gdb_lookup.register_type_printers(gdb.selected_inferior().progspace)
4 changes: 4 additions & 0 deletions src/etc/gdb_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def register_printers(objfile):
objfile.pretty_printers.append(printer)


def register_type_printers(objfile):
gdb.types.register_type_printer(objfile, PtrTypePrinter("PtrOrRef"))


# BACKCOMPAT: rust 1.35
def is_hashbrown_hashmap(hash_map):
return len(hash_map.type.fields()) == 1
Expand Down
65 changes: 65 additions & 0 deletions src/etc/gdb_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,68 @@ def children(self):

def display_hint(self):
return "map" if self._show_values else "array"


class NestedRefProvider(printer_base):
def __init__(self, valobj):
self._valobj = valobj
self._ptr = self._valobj["ptr"]

def to_string(self):
return str(self._valobj.cast(self._ptr.type))

def children(self):
yield ("*ptr", self._ptr.referenced_value())

def num_children(self):
return 1

def display_hint(self):
return "array"


PTR_CODES = (
gdb.TYPE_CODE_PTR,
gdb.TYPE_CODE_REF,
gdb.TYPE_CODE_RVALUE_REF,
gdb.TYPE_CODE_MEMBERPTR,
)


class PtrTypeRecognizer:
def recognize(self, type: gdb.Type) -> str:
if type.code not in PTR_CODES:
return None

name_parts = []

ptr_type: gdb.Type = type
ptee_type: gdb.Type = type.target()

while ptr_type.code in PTR_CODES:
is_ref: bool = ptr_type.code == gdb.TYPE_CODE_REF

if ptee_type.const() == ptee_type:
if is_ref:
name_parts.append("&")
else:
name_parts.append("*const ")
else:
if is_ref:
name_parts.append("&mut ")
else:
name_parts.append("*mut ")

ptr_type = ptee_type
try:
ptee_type = ptee_type.target()
except RuntimeError:
break

name_parts.append(ptr_type.unqualified().name)
return "".join(name_parts)


class PtrTypePrinter(gdb.types.TypePrinter):
def instantiate(self):
return PtrTypeRecognizer()
90 changes: 69 additions & 21 deletions src/etc/lldb_commands
Original file line number Diff line number Diff line change
@@ -1,43 +1,91 @@
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)String$" --category Rust
# Default
type synthetic add -l lldb_lookup.synthetic_lookup -x ".*" --category Rust
type summary add -F lldb_lookup.summary_lookup -x ".*" --category Rust
# String
type synthetic add -l lldb_lookup.StdStringSyntheticProvider -x "^(alloc::([a-z_]+::)+)String$" --category Rust
type summary add -F lldb_lookup.StdStringSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
# Ref/Ptr
type synthetic add -l lldb_lookup.PtrSyntheticProvider -x "^(const )?.* \*$" --category Rust
type synthetic add -l lldb_lookup.PtrSyntheticProvider -x "^(const )?.* &$" --category Rust
type summary add -F lldb_lookup.PtrSummaryProvider -e -h -x "^(const )?.* &$" --category Rust
type summary add -F lldb_lookup.PtrSummaryProvider -e -h -x "^(const )?.* \*$" --category Rust
## Nested Ref (must occur before &str)
type synthetic add -l lldb_lookup.NestedRefSyntheticProvider -x "^ref(_mut)?\$<.+>$" --category Rust
type summary add -F lldb_lookup.NestedRefSummaryProvider -x "^ref(_mut)?\$<.+>$" --category Rust
# &str/&mut str
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?str$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
## MSVC
type synthetic add -l lldb_lookup.MSVCStrSyntheticProvider -x "^ref(_mut)?\$<str\$>$" --category Rust
type summary add -F lldb_lookup.StdStrSummaryProvider -e -h -x "^ref(_mut)?\$<str\$>$" --category Rust
# Array
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?\\[.+\\]$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
# Slice
type synthetic add -l lldb_lookup.StdSliceSyntheticProvider -x "^ref(_mut)?\$<\[.+\]>" --category Rust
type summary add -F lldb_lookup.StdSliceSummaryProvider -e -x -h "^ref(_mut)?\$<\[.+\]>" --category Rust
## MSVC
type synthetic add -l lldb_lookup.MSVCStdSliceSyntheticProvider -x "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
type summary add -F lldb_lookup.StdSliceSummaryProvider -e -x -h "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
# OsString
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
# Vec
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
# VecDeque
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
# BTreeSet
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
# BTreeMap
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
# HashMap
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
# HashSet
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
# Rc
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
# Arc
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
# Cell
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
# RefCell
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)Ref<.+>$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::([a-z_]+::)+)PathBuf$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(.*)$" --category Rust
type summary add -F _ -e -x -h "^.*$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Ref<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
# NonZero
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
type synthetic add -l lldb_lookup.synthetic_lookup -x "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
# PathBuf
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::([a-z_]+::)+)PathBuf$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::([a-z_]+::)+)PathBuf$" --category Rust
# Path
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
type category enable Rust
# Enum
## MSVC
type synthetic add -l lldb_lookup.MSVCEnumSyntheticProvider -x "^enum2\$<.+>$" --category Rust
type summary add -F lldb_lookup.MSVCEnumSummaryProvider -e -x -h "^enum2\$<.+>$" --category Rust
## MSVC Variants
type synthetic add -l lldb_lookup.synthetic_lookup -x "^enum2\$<.+>::.*$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^enum2\$<.+>::.*$" --category Rust
# Tuple
type synthetic add -l lldb_lookup.synthetic_lookup -x "^\(.*\)$" --category Rust
## MSVC
type synthetic add -l lldb_lookup.MSVCTupleSyntheticProvider -x "^tuple\$<.+>$" --category Rust
type summary add -F lldb_lookup.TupleSummaryProvider -e -x -h "^tuple\$<.+>$" --category Rust

type category enable Rust
Loading
Loading