4 use std::time::SystemTime;
8 use rustc_span::{FileName, SourceFileHash, SourceFileHashAlgorithm};
10 use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
11 use cranelift_codegen::isa::TargetIsa;
12 use cranelift_codegen::ValueLocRange;
15 self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
16 LocationList, Range, RangeList, UnitEntryId, Writer, FileInfo,
18 use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, X86_64};
20 pub(crate) use emit::{DebugReloc, DebugRelocName};
22 fn target_endian(tcx: TyCtxt<'_>) -> RunTimeEndian {
23 use rustc_target::abi::Endian;
25 match tcx.data_layout.endian {
26 Endian::Big => RunTimeEndian::Big,
27 Endian::Little => RunTimeEndian::Little,
31 const MD5_LEN: usize = 16;
33 pub(crate) struct DebugContext<'tcx> {
36 endian: RunTimeEndian,
37 symbols: indexmap::IndexMap<FuncId, String>,
40 unit_range_list: RangeList,
42 types: FxHashMap<Ty<'tcx>, UnitEntryId>,
45 impl<'tcx> DebugContext<'tcx> {
46 pub(crate) fn new(tcx: TyCtxt<'tcx>, address_size: u8) -> Self {
47 let encoding = Encoding {
48 format: Format::Dwarf32,
49 // TODO: this should be configurable
50 // macOS doesn't seem to support DWARF > 3
51 // 5 version is required for md5 file hash
56 let mut dwarf = DwarfUnit::new(encoding);
58 // FIXME: how to get version when building out of tree?
59 // Normally this would use option_env!("CFG_VERSION").
60 let producer = format!("cg_clif (rustc {})", "unknown version");
61 let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned();
62 let (name, md5) = match tcx.sess.local_crate_source_file.clone() {
64 let name = path.to_string_lossy().into_owned();
67 .get_source_file(&FileName::Real(path))
69 .filter(|h| matches!(h, SourceFileHash { kind: SourceFileHashAlgorithm::Md5, .. }))
71 let mut buf = [0u8; MD5_LEN];
72 buf.copy_from_slice(h.hash_bytes());
77 None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
80 let mut line_program = LineProgram::new(
82 LineEncoding::default(),
83 LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
84 LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
86 timestamp: SystemTime::now()
87 .duration_since(SystemTime::UNIX_EPOCH)
91 md5: md5.unwrap_or_default(),
94 line_program.file_has_timestamp = true;
95 line_program.file_has_md5 = md5.is_some();
97 dwarf.unit.line_program = line_program;
100 let name = dwarf.strings.add(name);
101 let comp_dir = dwarf.strings.add(comp_dir);
103 let root = dwarf.unit.root();
104 let root = dwarf.unit.get_mut(root);
106 gimli::DW_AT_producer,
107 AttributeValue::StringRef(dwarf.strings.add(producer)),
110 gimli::DW_AT_language,
111 AttributeValue::Language(gimli::DW_LANG_Rust),
113 root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
114 root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
117 AttributeValue::Address(Address::Constant(0)),
124 endian: target_endian(tcx),
125 symbols: indexmap::IndexMap::new(),
128 unit_range_list: RangeList(Vec::new()),
130 types: FxHashMap::default(),
134 fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
135 if let Some(type_id) = self.types.get(ty) {
139 let new_entry = |dwarf: &mut DwarfUnit, tag| dwarf.unit.add(dwarf.unit.root(), tag);
141 let primitive = |dwarf: &mut DwarfUnit, ate| {
142 let type_id = new_entry(dwarf, gimli::DW_TAG_base_type);
143 let type_entry = dwarf.unit.get_mut(type_id);
144 type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(ate));
148 let name = format!("{}", ty);
149 let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
151 let type_id = match ty.kind {
152 ty::Bool => primitive(&mut self.dwarf, gimli::DW_ATE_boolean),
153 ty::Char => primitive(&mut self.dwarf, gimli::DW_ATE_UTF),
154 ty::Uint(_) => primitive(&mut self.dwarf, gimli::DW_ATE_unsigned),
155 ty::Int(_) => primitive(&mut self.dwarf, gimli::DW_ATE_signed),
156 ty::Float(_) => primitive(&mut self.dwarf, gimli::DW_ATE_float),
157 ty::Ref(_, pointee_ty, _mutbl)
158 | ty::RawPtr(ty::TypeAndMut {
162 let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_pointer_type);
164 // Ensure that type is inserted before recursing to avoid duplicates
165 self.types.insert(ty, type_id);
167 let pointee = self.dwarf_ty(pointee_ty);
169 let type_entry = self.dwarf.unit.get_mut(type_id);
171 //type_entry.set(gimli::DW_AT_mutable, AttributeValue::Flag(mutbl == rustc_hir::Mutability::Mut));
172 type_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(pointee));
176 ty::Adt(adt_def, _substs) if adt_def.is_struct() && !layout.is_unsized() => {
177 let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type);
179 // Ensure that type is inserted before recursing to avoid duplicates
180 self.types.insert(ty, type_id);
182 let variant = adt_def.non_enum_variant();
184 for (field_idx, field_def) in variant.fields.iter().enumerate() {
185 let field_offset = layout.fields.offset(field_idx);
186 let field_layout = layout.field(&layout::LayoutCx {
188 param_env: ParamEnv::reveal_all(),
189 }, field_idx).unwrap();
191 let field_type = self.dwarf_ty(field_layout.ty);
193 let field_id = self.dwarf.unit.add(type_id, gimli::DW_TAG_member);
194 let field_entry = self.dwarf.unit.get_mut(field_id);
196 field_entry.set(gimli::DW_AT_name, AttributeValue::String(field_def.ident.as_str().to_string().into_bytes()));
197 field_entry.set(gimli::DW_AT_data_member_location, AttributeValue::Udata(field_offset.bytes()));
198 field_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(field_type));
203 _ => new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type),
206 let type_entry = self.dwarf.unit.get_mut(type_id);
208 type_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
210 gimli::DW_AT_byte_size,
211 AttributeValue::Udata(layout.size.bytes()),
214 self.types.insert(ty, type_id);
220 pub(crate) struct FunctionDebugContext<'a, 'tcx> {
221 debug_context: &'a mut DebugContext<'tcx>,
222 entry_id: UnitEntryId,
224 instance: Instance<'tcx>,
225 mir: &'tcx mir::Body<'tcx>,
228 impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
230 debug_context: &'a mut DebugContext<'tcx>,
231 instance: Instance<'tcx>,
235 let mir = *debug_context.tcx.instance_mir(instance.def);
237 let (symbol, _) = debug_context.symbols.insert_full(func_id, name.to_string());
239 // FIXME: add to appropriate scope intead of root
240 let scope = debug_context.dwarf.unit.root();
242 let entry_id = debug_context
245 .add(scope, gimli::DW_TAG_subprogram);
246 let entry = debug_context.dwarf.unit.get_mut(entry_id);
247 let name_id = debug_context.dwarf.strings.add(name);
249 gimli::DW_AT_linkage_name,
250 AttributeValue::StringRef(name_id),
253 FunctionDebugContext {
262 fn define_local(&mut self, name: String, ty: Ty<'tcx>) -> UnitEntryId {
263 let ty = self.debug_context.tcx.subst_and_normalize_erasing_regions(
264 self.instance.substs,
265 ty::ParamEnv::reveal_all(),
268 let dw_ty = self.debug_context.dwarf_ty(ty);
274 .add(self.entry_id, gimli::DW_TAG_variable);
275 let var_entry = self.debug_context.dwarf.unit.get_mut(var_id);
277 var_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
278 var_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(dw_ty));
283 pub(crate) fn define(
287 source_info_set: &indexmap::IndexSet<SourceInfo>,
288 local_map: FxHashMap<mir::Local, CPlace<'tcx>>,
290 let end = self.create_debug_lines(context, isa, source_info_set);
295 .push(Range::StartLength {
296 begin: Address::Symbol {
303 // FIXME make it more reliable and implement scopes before re-enabling this.
305 let value_labels_ranges = context.build_value_labels_ranges(isa).unwrap();
307 for (local, _local_decl) in self.mir.local_decls.iter_enumerated() {
308 let var_id = self.define_local(format!("{:?}", local), &self.mir.local_decls[local].ty);
310 let location = place_location(
315 &value_labels_ranges,
318 projection: ty::List::empty(),
322 let var_entry = self.debug_context.dwarf.unit.get_mut(var_id);
323 var_entry.set(gimli::DW_AT_location, location);
327 // FIXME create locals for all entries in mir.var_debug_info
331 fn place_location<'a, 'tcx>(
332 func_debug_ctx: &mut FunctionDebugContext<'a, 'tcx>,
335 local_map: &FxHashMap<mir::Local, CPlace<'tcx>>,
336 #[allow(rustc::default_hash_types)]
337 value_labels_ranges: &std::collections::HashMap<ValueLabel, Vec<ValueLocRange>>,
339 ) -> AttributeValue {
340 assert!(place.projection.is_empty()); // FIXME implement them
342 match local_map[&place.local].inner() {
343 CPlaceInner::Var(local) => {
344 let value_label = cranelift_codegen::ir::ValueLabel::from_u32(local.as_u32());
345 if let Some(value_loc_ranges) = value_labels_ranges.get(&value_label) {
346 let loc_list = LocationList(
349 .map(|value_loc_range| Location::StartEnd {
350 begin: Address::Symbol {
351 symbol: func_debug_ctx.symbol,
352 addend: i64::from(value_loc_range.start),
354 end: Address::Symbol {
355 symbol: func_debug_ctx.symbol,
356 addend: i64::from(value_loc_range.end),
359 translate_loc(isa, value_loc_range.loc, &context.func.stack_slots).unwrap(),
364 let loc_list_id = func_debug_ctx.debug_context.dwarf.unit.locations.add(loc_list);
366 AttributeValue::LocationListRef(loc_list_id)
368 // FIXME set value labels for unused locals
370 AttributeValue::Exprloc(Expression(vec![]))
373 CPlaceInner::Addr(_, _) => {
374 // FIXME implement this (used by arguments and returns)
376 AttributeValue::Exprloc(Expression(vec![]))
378 // For PointerBase::Stack:
379 //AttributeValue::Exprloc(Expression(translate_loc(ValueLoc::Stack(*stack_slot), &context.func.stack_slots).unwrap()))
384 // Adapted from https://github.com/CraneStation/wasmtime/blob/5a1845b4caf7a5dba8eda1fef05213a532ed4259/crates/debug/src/transform/expression.rs#L59-L137
385 fn translate_loc(isa: &dyn TargetIsa, loc: ValueLoc, stack_slots: &StackSlots) -> Option<Vec<u8>> {
387 ValueLoc::Reg(reg) => {
388 let machine_reg = isa.map_dwarf_register(reg).unwrap();
389 assert!(machine_reg <= 32); // FIXME
390 Some(vec![gimli::constants::DW_OP_reg0.0 + machine_reg as u8])
392 ValueLoc::Stack(ss) => {
393 if let Some(ss_offset) = stack_slots[ss].offset {
394 let endian = gimli::RunTimeEndian::Little;
395 let mut writer = write::EndianVec::new(endian);
397 .write_u8(gimli::constants::DW_OP_breg0.0 + X86_64::RBP.0 as u8)
399 writer.write_sleb128(ss_offset as i64 + 16).expect("ss wr");
400 let buf = writer.into_vec();