1 //! Handling of everything related to debuginfo.
10 use cranelift_codegen::ir::Endianness;
11 use cranelift_codegen::isa::TargetIsa;
14 Address, AttributeValue, DwarfUnit, FileId, LineProgram, LineString, Range, RangeList,
17 use gimli::{Encoding, Format, LineEncoding, RunTimeEndian};
18 use indexmap::IndexSet;
20 pub(crate) use emit::{DebugReloc, DebugRelocName};
21 pub(crate) use unwind::UnwindContext;
23 pub(crate) fn producer() -> String {
25 "cg_clif (rustc {}, cranelift {})",
26 rustc_interface::util::rustc_version_str().unwrap_or("unknown version"),
27 cranelift_codegen::VERSION,
31 pub(crate) struct DebugContext {
32 endian: RunTimeEndian,
35 unit_range_list: RangeList,
38 pub(crate) struct FunctionDebugContext {
39 entry_id: UnitEntryId,
40 function_source_loc: (FileId, u64, u64),
41 source_loc_set: indexmap::IndexSet<(FileId, u64, u64)>,
45 pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa) -> Self {
46 let encoding = Encoding {
47 format: Format::Dwarf32,
48 // FIXME this should be configurable
49 // macOS doesn't seem to support DWARF > 3
50 // 5 version is required for md5 file hash
51 version: if tcx.sess.target.is_like_osx {
54 // FIXME change to version 5 once the gdb and lldb shipping with the latest debian
58 address_size: isa.frontend_config().pointer_bytes(),
61 let endian = match isa.endianness() {
62 Endianness::Little => RunTimeEndian::Little,
63 Endianness::Big => RunTimeEndian::Big,
66 let mut dwarf = DwarfUnit::new(encoding);
68 let producer = producer();
73 .to_string_lossy(FileNameDisplayPreference::Remapped)
75 let (name, file_info) = match tcx.sess.local_crate_source_file() {
77 let name = path.to_string_lossy().into_owned();
80 None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
83 let mut line_program = LineProgram::new(
85 LineEncoding::default(),
86 LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
87 LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
90 line_program.file_has_md5 = file_info.is_some();
92 dwarf.unit.line_program = line_program;
95 let name = dwarf.strings.add(name);
96 let comp_dir = dwarf.strings.add(comp_dir);
98 let root = dwarf.unit.root();
99 let root = dwarf.unit.get_mut(root);
100 root.set(gimli::DW_AT_producer, AttributeValue::StringRef(dwarf.strings.add(producer)));
101 root.set(gimli::DW_AT_language, AttributeValue::Language(gimli::DW_LANG_Rust));
102 root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
103 root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
104 root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
107 DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
110 pub(crate) fn define_function(
115 ) -> FunctionDebugContext {
116 let (file, line, column) = DebugContext::get_span_loc(tcx, function_span, function_span);
118 let file_id = self.add_source_file(&file);
120 // FIXME: add to appropriate scope instead of root
121 let scope = self.dwarf.unit.root();
123 let entry_id = self.dwarf.unit.add(scope, gimli::DW_TAG_subprogram);
124 let entry = self.dwarf.unit.get_mut(entry_id);
125 let name_id = self.dwarf.strings.add(name);
126 // Gdb requires DW_AT_name. Otherwise the DW_TAG_subprogram is skipped.
127 entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
128 entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id));
130 entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
131 entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
132 entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(column));
134 FunctionDebugContext {
136 function_source_loc: (file_id, line, column),
137 source_loc_set: IndexSet::new(),
142 impl FunctionDebugContext {
143 pub(crate) fn finalize(
145 debug_context: &mut DebugContext,
149 let symbol = func_id.as_u32() as usize;
151 let end = self.create_debug_lines(debug_context, symbol, context);
153 debug_context.unit_range_list.0.push(Range::StartLength {
154 begin: Address::Symbol { symbol, addend: 0 },
155 length: u64::from(end),
158 let func_entry = debug_context.dwarf.unit.get_mut(self.entry_id);
159 // Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
162 AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
164 // Using Udata for DW_AT_high_pc requires at least DWARF4
165 func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));