1 //! Handling of everything related to debuginfo.
2
3 mod emit;
4 mod line_info;
5 mod object;
6 mod unwind;
7
8 use crate::prelude::*;
9
10 use cranelift_codegen::ir::Endianness;
11 use cranelift_codegen::isa::TargetIsa;
12
13 use gimli::write::{
14 Address, AttributeValue, DwarfUnit, FileId, LineProgram, LineString, Range, RangeList,
15 UnitEntryId,
16 };
17 use gimli::{Encoding, Format, LineEncoding, RunTimeEndian};
18 use indexmap::IndexSet;
19
20 pub(crate) use emit::{DebugReloc, DebugRelocName};
21 pub(crate) use unwind::UnwindContext;
22
producer() -> String23 pub(crate) fn producer() -> String {
24 format!(
25 "cg_clif (rustc {}, cranelift {})",
26 rustc_interface::util::rustc_version_str().unwrap_or("unknown version"),
27 cranelift_codegen::VERSION,
28 )
29 }
30
31 pub(crate) struct DebugContext {
32 endian: RunTimeEndian,
33
34 dwarf: DwarfUnit,
35 unit_range_list: RangeList,
36 }
37
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)>,
42 }
43
44 impl DebugContext {
new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa) -> Self45 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 {
52 3
53 } else {
54 // FIXME change to version 5 once the gdb and lldb shipping with the latest debian
55 // support it.
56 4
57 },
58 address_size: isa.frontend_config().pointer_bytes(),
59 };
60
61 let endian = match isa.endianness() {
62 Endianness::Little => RunTimeEndian::Little,
63 Endianness::Big => RunTimeEndian::Big,
64 };
65
66 let mut dwarf = DwarfUnit::new(encoding);
67
68 let producer = producer();
69 let comp_dir = tcx
70 .sess
71 .opts
72 .working_dir
73 .to_string_lossy(FileNameDisplayPreference::Remapped)
74 .into_owned();
75 let (name, file_info) = match tcx.sess.local_crate_source_file() {
76 Some(path) => {
77 let name = path.to_string_lossy().into_owned();
78 (name, None)
79 }
80 None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
81 };
82
83 let mut line_program = LineProgram::new(
84 encoding,
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),
88 file_info,
89 );
90 line_program.file_has_md5 = file_info.is_some();
91
92 dwarf.unit.line_program = line_program;
93
94 {
95 let name = dwarf.strings.add(name);
96 let comp_dir = dwarf.strings.add(comp_dir);
97
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)));
105 }
106
107 DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
108 }
109
define_function( &mut self, tcx: TyCtxt<'_>, name: &str, function_span: Span, ) -> FunctionDebugContext110 pub(crate) fn define_function(
111 &mut self,
112 tcx: TyCtxt<'_>,
113 name: &str,
114 function_span: Span,
115 ) -> FunctionDebugContext {
116 let (file, line, column) = DebugContext::get_span_loc(tcx, function_span, function_span);
117
118 let file_id = self.add_source_file(&file);
119
120 // FIXME: add to appropriate scope instead of root
121 let scope = self.dwarf.unit.root();
122
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));
129
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));
133
134 FunctionDebugContext {
135 entry_id,
136 function_source_loc: (file_id, line, column),
137 source_loc_set: IndexSet::new(),
138 }
139 }
140 }
141
142 impl FunctionDebugContext {
finalize( mut self, debug_context: &mut DebugContext, func_id: FuncId, context: &Context, )143 pub(crate) fn finalize(
144 mut self,
145 debug_context: &mut DebugContext,
146 func_id: FuncId,
147 context: &Context,
148 ) {
149 let symbol = func_id.as_u32() as usize;
150
151 let end = self.create_debug_lines(debug_context, symbol, context);
152
153 debug_context.unit_range_list.0.push(Range::StartLength {
154 begin: Address::Symbol { symbol, addend: 0 },
155 length: u64::from(end),
156 });
157
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.
160 func_entry.set(
161 gimli::DW_AT_low_pc,
162 AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
163 );
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)));
166 }
167 }
168