Skip to content

Commit 4b80941

Browse files
committed
Add debuginfo for statics
Most types are still unimplemented and get a placeholder byte array instead.
1 parent f086e7a commit 4b80941

File tree

5 files changed

+251
-4
lines changed

5 files changed

+251
-4
lines changed

src/constant.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ impl ConstantCx {
3131
}
3232
}
3333

34-
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
34+
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) -> DataId {
3535
let mut constants_cx = ConstantCx::new();
3636
constants_cx.todo.push(TodoItem::Static(def_id));
3737
constants_cx.finalize(tcx, module);
38+
39+
data_id_for_static(
40+
tcx, module, def_id, false,
41+
// For a declaration the stated mutability doesn't matter.
42+
false,
43+
)
3844
}
3945

4046
pub(crate) fn codegen_tls_ref<'tcx>(

src/debuginfo/emit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(super) fn address_for_func(func_id: FuncId) -> Address {
1515
Address::Symbol { symbol: symbol as usize, addend: 0 }
1616
}
1717

18-
#[allow(dead_code)]
1918
pub(super) fn address_for_data(data_id: DataId) -> Address {
2019
let symbol = data_id.as_u32();
2120
assert!(symbol & 1 << 31 == 0);

src/debuginfo/mod.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
mod emit;
44
mod line_info;
55
mod object;
6+
mod types;
67
mod unwind;
78

89
use cranelift_codegen::ir::Endianness;
910
use cranelift_codegen::isa::TargetIsa;
11+
use cranelift_module::DataId;
1012
use gimli::write::{
1113
Address, AttributeValue, DwarfUnit, Expression, FileId, LineProgram, LineString, Range,
1214
RangeList, UnitEntryId,
1315
};
1416
use gimli::{AArch64, Encoding, Format, LineEncoding, Register, RiscV, RunTimeEndian, X86_64};
1517
use indexmap::IndexSet;
1618
use rustc_codegen_ssa::debuginfo::type_names;
19+
use rustc_hir::def::DefKind;
1720
use rustc_hir::def_id::DefIdMap;
1821
use rustc_session::Session;
1922
use rustc_span::{SourceFileHash, StableSourceFileId};
2023

2124
pub(crate) use self::emit::{DebugReloc, DebugRelocName};
25+
pub(crate) use self::types::TypeDebugContext;
2226
pub(crate) use self::unwind::UnwindContext;
23-
use crate::debuginfo::emit::address_for_func;
27+
use crate::debuginfo::emit::{address_for_data, address_for_func};
2428
use crate::prelude::*;
2529

2630
pub(crate) fn producer(sess: &Session) -> String {
@@ -35,6 +39,7 @@ pub(crate) struct DebugContext {
3539
created_files: FxHashMap<(StableSourceFileId, SourceFileHash), FileId>,
3640
stack_pointer_register: Register,
3741
namespace_map: DefIdMap<UnitEntryId>,
42+
array_size_type: UnitEntryId,
3843

3944
should_remap_filepaths: bool,
4045
}
@@ -129,13 +134,27 @@ impl DebugContext {
129134
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
130135
}
131136

137+
let array_size_type = dwarf.unit.add(dwarf.unit.root(), gimli::DW_TAG_base_type);
138+
let array_size_type_entry = dwarf.unit.get_mut(array_size_type);
139+
array_size_type_entry.set(
140+
gimli::DW_AT_name,
141+
AttributeValue::StringRef(dwarf.strings.add("__ARRAY_SIZE_TYPE__")),
142+
);
143+
array_size_type_entry
144+
.set(gimli::DW_AT_encoding, AttributeValue::Encoding(gimli::DW_ATE_unsigned));
145+
array_size_type_entry.set(
146+
gimli::DW_AT_byte_size,
147+
AttributeValue::Udata(isa.frontend_config().pointer_bytes().into()),
148+
);
149+
132150
DebugContext {
133151
endian,
134152
dwarf,
135153
unit_range_list: RangeList(Vec::new()),
136154
created_files: FxHashMap::default(),
137155
stack_pointer_register,
138156
namespace_map: DefIdMap::default(),
157+
array_size_type,
139158
should_remap_filepaths,
140159
}
141160
}
@@ -231,6 +250,62 @@ impl DebugContext {
231250
source_loc_set: IndexSet::new(),
232251
}
233252
}
253+
254+
// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs#L1288-L1346
255+
pub(crate) fn define_static<'tcx>(
256+
&mut self,
257+
tcx: TyCtxt<'tcx>,
258+
type_dbg: &mut TypeDebugContext<'tcx>,
259+
def_id: DefId,
260+
data_id: DataId,
261+
) {
262+
let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else { bug!() };
263+
if nested {
264+
return;
265+
}
266+
267+
let scope = self.item_namespace(tcx, tcx.parent(def_id));
268+
269+
let span = tcx.def_span(def_id);
270+
let (file_id, line, _column) = self.get_span_loc(tcx, span, span);
271+
272+
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::ParamEnv::reveal_all());
273+
let static_layout = tcx.layout_of(ty::ParamEnv::reveal_all().and(static_type)).unwrap();
274+
// FIXME use the actual type layout
275+
let type_id = self.debug_type(tcx, type_dbg, static_type);
276+
277+
let name = tcx.item_name(def_id);
278+
let linkage_name = tcx.symbol_name(Instance::mono(tcx, def_id)).name;
279+
280+
let entry_id = self.dwarf.unit.add(scope, gimli::DW_TAG_variable);
281+
let entry = self.dwarf.unit.get_mut(entry_id);
282+
let linkage_name_id = if name.as_str() != linkage_name {
283+
Some(self.dwarf.strings.add(linkage_name))
284+
} else {
285+
None
286+
};
287+
let name_id = self.dwarf.strings.add(name.as_str());
288+
289+
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
290+
entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(type_id));
291+
292+
if tcx.is_reachable_non_generic(def_id) {
293+
entry.set(gimli::DW_AT_external, AttributeValue::FlagPresent);
294+
}
295+
296+
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
297+
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
298+
299+
entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(static_layout.align.pref.bytes()));
300+
301+
let mut expr = Expression::new();
302+
expr.op_addr(address_for_data(data_id));
303+
entry.set(gimli::DW_AT_location, AttributeValue::Exprloc(expr));
304+
305+
if let Some(linkage_name_id) = linkage_name_id {
306+
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(linkage_name_id));
307+
}
308+
}
234309
}
235310

236311
impl FunctionDebugContext {

src/debuginfo/types.rs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
2+
3+
use gimli::write::{AttributeValue, UnitEntryId};
4+
use rustc_codegen_ssa::debuginfo::type_names;
5+
use rustc_data_structures::fx::FxHashMap;
6+
use rustc_middle::ty::{self, Ty, TyCtxt};
7+
8+
use crate::{has_ptr_meta, DebugContext};
9+
10+
#[derive(Default)]
11+
pub(crate) struct TypeDebugContext<'tcx> {
12+
type_map: FxHashMap<Ty<'tcx>, UnitEntryId>,
13+
}
14+
15+
/// Returns from the enclosing function if the type debuginfo node with the given
16+
/// unique ID can be found in the type map.
17+
macro_rules! return_if_type_created_in_meantime {
18+
($type_dbg:expr, $ty:expr) => {
19+
if let Some(&type_id) = $type_dbg.type_map.get(&$ty) {
20+
return type_id;
21+
}
22+
};
23+
}
24+
25+
impl DebugContext {
26+
pub(crate) fn debug_type<'tcx>(
27+
&mut self,
28+
tcx: TyCtxt<'tcx>,
29+
type_dbg: &mut TypeDebugContext<'tcx>,
30+
ty: Ty<'tcx>,
31+
) -> UnitEntryId {
32+
if let Some(&type_id) = type_dbg.type_map.get(&ty) {
33+
return type_id;
34+
}
35+
36+
let type_id = match ty.kind() {
37+
ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
38+
self.basic_type(tcx, ty)
39+
}
40+
ty::Tuple(elems) if elems.is_empty() => self.basic_type(tcx, ty),
41+
ty::Array(elem_ty, len) => self.array_type(
42+
tcx,
43+
type_dbg,
44+
*elem_ty,
45+
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()),
46+
),
47+
// ty::Slice(_) | ty::Str
48+
// ty::Dynamic
49+
// ty::Foreign
50+
ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
51+
self.pointer_type(tcx, type_dbg, ty, *pointee_type)
52+
}
53+
// ty::Adt(def, args) if def.is_box() && args.get(1).map_or(true, |arg| cx.layout_of(arg.expect_ty()).is_1zst())
54+
// ty::FnDef(..) | ty::FnPtr(..)
55+
// ty::Closure(..)
56+
// ty::Adt(def, ..)
57+
// ty::Tuple(_)
58+
// ty::Param(_)
59+
// FIXME implement remaining types and add unreachable!() to the fallback branch
60+
_ => self.placeholder_for_type(tcx, type_dbg, ty),
61+
};
62+
63+
type_dbg.type_map.insert(ty, type_id);
64+
65+
type_id
66+
}
67+
68+
fn basic_type<'tcx>(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> UnitEntryId {
69+
let (name, encoding) = match ty.kind() {
70+
ty::Never => ("!", gimli::DW_ATE_unsigned),
71+
ty::Tuple(elems) if elems.is_empty() => ("()", gimli::DW_ATE_unsigned),
72+
ty::Bool => ("bool", gimli::DW_ATE_boolean),
73+
ty::Char => ("char", gimli::DW_ATE_UTF),
74+
ty::Int(int_ty) => (int_ty.name_str(), gimli::DW_ATE_signed),
75+
ty::Uint(uint_ty) => (uint_ty.name_str(), gimli::DW_ATE_unsigned),
76+
ty::Float(float_ty) => (float_ty.name_str(), gimli::DW_ATE_float),
77+
_ => unreachable!(),
78+
};
79+
80+
let type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_base_type);
81+
let type_entry = self.dwarf.unit.get_mut(type_id);
82+
type_entry.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
83+
type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(encoding));
84+
type_entry.set(
85+
gimli::DW_AT_byte_size,
86+
AttributeValue::Udata(
87+
tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).expect("FIXME").size.bytes(),
88+
),
89+
);
90+
91+
type_id
92+
}
93+
94+
fn array_type<'tcx>(
95+
&mut self,
96+
tcx: TyCtxt<'tcx>,
97+
type_dbg: &mut TypeDebugContext<'tcx>,
98+
elem_ty: Ty<'tcx>,
99+
len: u64,
100+
) -> UnitEntryId {
101+
let elem_dw_ty = self.debug_type(tcx, type_dbg, elem_ty);
102+
103+
return_if_type_created_in_meantime!(type_dbg, elem_ty);
104+
105+
let array_type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_array_type);
106+
let array_type_entry = self.dwarf.unit.get_mut(array_type_id);
107+
array_type_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(elem_dw_ty));
108+
109+
let subrange_id = self.dwarf.unit.add(array_type_id, gimli::DW_TAG_subrange_type);
110+
let subrange_entry = self.dwarf.unit.get_mut(subrange_id);
111+
subrange_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(self.array_size_type));
112+
subrange_entry.set(gimli::DW_AT_lower_bound, AttributeValue::Udata(0));
113+
subrange_entry.set(gimli::DW_AT_count, AttributeValue::Udata(len));
114+
115+
array_type_id
116+
}
117+
118+
fn pointer_type<'tcx>(
119+
&mut self,
120+
tcx: TyCtxt<'tcx>,
121+
type_dbg: &mut TypeDebugContext<'tcx>,
122+
ptr_type: Ty<'tcx>,
123+
pointee_type: Ty<'tcx>,
124+
) -> UnitEntryId {
125+
let pointee_dw_ty = self.debug_type(tcx, type_dbg, pointee_type);
126+
127+
return_if_type_created_in_meantime!(type_dbg, ptr_type);
128+
129+
let name = type_names::compute_debuginfo_type_name(tcx, ptr_type, true);
130+
131+
if !has_ptr_meta(tcx, ptr_type) {
132+
let pointer_type_id =
133+
self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_pointer_type);
134+
let pointer_entry = self.dwarf.unit.get_mut(pointer_type_id);
135+
pointer_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(pointee_dw_ty));
136+
pointer_entry
137+
.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
138+
139+
pointer_type_id
140+
} else {
141+
// FIXME implement debuginfo for fat pointers
142+
self.placeholder_for_type(tcx, type_dbg, ptr_type)
143+
}
144+
}
145+
146+
fn placeholder_for_type<'tcx>(
147+
&mut self,
148+
tcx: TyCtxt<'tcx>,
149+
type_dbg: &mut TypeDebugContext<'tcx>,
150+
ty: Ty<'tcx>,
151+
) -> UnitEntryId {
152+
self.debug_type(
153+
tcx,
154+
type_dbg,
155+
Ty::new_array(
156+
tcx,
157+
tcx.types.u8,
158+
tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap().size.bytes(),
159+
),
160+
)
161+
}
162+
}

src/driver/aot.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_session::config::{DebugInfo, OutFileName, OutputFilenames, OutputType}
2323
use rustc_session::Session;
2424

2525
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
26+
use crate::debuginfo::TypeDebugContext;
2627
use crate::global_asm::GlobalAsmConfig;
2728
use crate::{prelude::*, BackendConfig};
2829

@@ -460,6 +461,7 @@ fn module_codegen(
460461
tcx.sess.opts.debuginfo != DebugInfo::None,
461462
cgu_name,
462463
);
464+
let mut type_dbg = TypeDebugContext::default();
463465
super::predefine_mono_items(tcx, &mut module, &mono_items);
464466
let mut codegened_functions = vec![];
465467
for (mono_item, _) in mono_items {
@@ -475,7 +477,10 @@ fn module_codegen(
475477
codegened_functions.push(codegened_function);
476478
}
477479
MonoItem::Static(def_id) => {
478-
crate::constant::codegen_static(tcx, &mut module, def_id)
480+
let data_id = crate::constant::codegen_static(tcx, &mut module, def_id);
481+
if let Some(debug_context) = &mut cx.debug_context {
482+
debug_context.define_static(tcx, &mut type_dbg, def_id, data_id);
483+
}
479484
}
480485
MonoItem::GlobalAsm(item_id) => {
481486
crate::global_asm::codegen_global_asm_item(

0 commit comments

Comments
 (0)