]> git.lizzy.rs Git - rust.git/blob - src/debuginfo.rs
Update gimli
[rust.git] / src / debuginfo.rs
1 extern crate gimli;
2
3 use crate::prelude::*;
4
5 use std::marker::PhantomData;
6
7 use syntax::source_map::FileName;
8
9 use gimli::write::{
10     Address, AttributeValue, DwarfUnit, EndianVec, LineProgram, Range,
11     RangeList, Result, SectionId, UnitEntryId,
12     Writer, FileId, LineStringTable, LineString, Sections,
13 };
14 use gimli::{Encoding, Format, RunTimeEndian};
15
16 use faerie::*;
17
18 fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
19     use rustc::ty::layout::Endian;
20
21     match tcx.data_layout.endian {
22         Endian::Big => RunTimeEndian::Big,
23         Endian::Little => RunTimeEndian::Little,
24     }
25 }
26
27 fn line_program_add_file(line_program: &mut LineProgram, line_strings: &mut LineStringTable, file: &FileName) -> FileId {
28     match file {
29         FileName::Real(path) => {
30             let dir_name = LineString::new(path.parent().unwrap().to_str().unwrap().as_bytes(), line_program.encoding(), line_strings);
31             let dir_id =
32                 line_program.add_directory(dir_name);
33             let file_name = LineString::new(path.file_name().unwrap().to_str().unwrap().as_bytes(), line_program.encoding(), line_strings);
34             line_program.add_file(
35                 file_name,
36                 dir_id,
37                 None,
38             )
39         }
40         // FIXME give more appropriate file names
41         _ => {
42             let dir_id = line_program.default_directory();
43             let dummy_file_name = LineString::new(file.to_string().into_bytes(), line_program.encoding(), line_strings);
44             line_program.add_file(
45                 dummy_file_name,
46                 dir_id,
47                 None,
48             )
49         }
50     }
51 }
52
53 #[derive(Clone)]
54 struct DebugReloc {
55     offset: u32,
56     size: u8,
57     name: DebugRelocName,
58     addend: i64,
59 }
60
61 #[derive(Clone)]
62 enum DebugRelocName {
63     Section(SectionId),
64     Symbol(usize),
65 }
66
67 impl DebugReloc {
68     fn name<'a>(&self, ctx: &'a DebugContext) -> &'a str {
69         match self.name {
70             DebugRelocName::Section(id) => id.name(),
71             DebugRelocName::Symbol(index) => ctx.symbols.get_index(index).unwrap(),
72         }
73     }
74 }
75
76 pub struct DebugContext<'tcx> {
77     endian: RunTimeEndian,
78     symbols: indexmap::IndexSet<String>,
79
80     dwarf: DwarfUnit,
81     unit_range_list: RangeList,
82
83     _dummy: PhantomData<&'tcx ()>,
84 }
85
86 impl<'a, 'tcx: 'a> DebugContext<'tcx> {
87     pub fn new(tcx: TyCtxt, address_size: u8) -> Self {
88         let encoding = Encoding {
89             format: Format::Dwarf32,
90             // TODO: this should be configurable
91             // macOS doesn't seem to support DWARF > 3
92             version: 3,
93             address_size,
94         };
95
96         let mut dwarf = DwarfUnit::new(encoding);
97
98         // FIXME: how to get version when building out of tree?
99         // Normally this would use option_env!("CFG_VERSION").
100         let producer = format!("cranelift fn (rustc version {})", "unknown version");
101         let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned();
102         let name = match tcx.sess.local_crate_source_file {
103             Some(ref path) => path.to_string_lossy().into_owned(),
104             None => tcx.crate_name(LOCAL_CRATE).to_string(),
105         };
106
107         let line_program = LineProgram::new(
108             encoding,
109             1,
110             1,
111             -5,
112             14,
113             LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
114             LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
115             None,
116         );
117         dwarf.unit.line_program = line_program;
118
119         {
120             let name = dwarf.strings.add(&*name);
121             let comp_dir = dwarf.strings.add(&*comp_dir);
122
123             let root = dwarf.unit.root();
124             let root = dwarf.unit.get_mut(root);
125             root.set(
126                 gimli::DW_AT_producer,
127                 AttributeValue::StringRef(dwarf.strings.add(producer)),
128             );
129             root.set(
130                 gimli::DW_AT_language,
131                 AttributeValue::Language(gimli::DW_LANG_Rust),
132             );
133             root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
134             root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
135             root.set(
136                 gimli::DW_AT_low_pc,
137                 AttributeValue::Address(Address::Absolute(0)),
138             );
139         }
140
141          DebugContext {
142             endian: target_endian(tcx),
143             symbols: indexmap::IndexSet::new(),
144
145             dwarf,
146             unit_range_list: RangeList(Vec::new()),
147
148             _dummy: PhantomData,
149         }
150     }
151
152     fn emit_location(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, entry_id: UnitEntryId, span: Span) {
153         let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
154
155         let file_id = line_program_add_file(
156             &mut self.dwarf.unit.line_program,
157             &mut self.dwarf.line_strings,
158             &loc.file.name,
159         );
160
161         let entry = self.dwarf.unit.get_mut(entry_id);
162
163         entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(file_id));
164         entry.set(
165             gimli::DW_AT_decl_line,
166             AttributeValue::Udata(loc.line as u64),
167         );
168         // FIXME: probably omit this
169         entry.set(
170             gimli::DW_AT_decl_column,
171             AttributeValue::Udata(loc.col.to_usize() as u64),
172         );
173     }
174
175     pub fn emit(&mut self, artifact: &mut Artifact) {
176         let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
177         let root = self.dwarf.unit.root();
178         let root = self.dwarf.unit.get_mut(root);
179         root.set(
180             gimli::DW_AT_ranges,
181             AttributeValue::RangeListRef(unit_range_list_id),
182         );
183
184         let mut sections = Sections::new(WriterRelocate::new(self));
185         self.dwarf.write(&mut sections).unwrap();
186
187         let _: Result<()> = sections.for_each_mut(|id, section| {
188             if !section.writer.slice().is_empty() {
189                 artifact
190                     .declare_with(
191                         id.name(),
192                         Decl::DebugSection,
193                         section.writer.take(),
194                     )
195                     .unwrap();
196             }
197             Ok(())
198         });
199
200         let _: Result<()> = sections.for_each(|id, section| {
201             for reloc in &section.relocs {
202                 artifact
203                     .link_with(
204                         faerie::Link {
205                             from: id.name(),
206                             to: reloc.name(self),
207                             at: u64::from(reloc.offset),
208                         },
209                         faerie::Reloc::Debug {
210                             size: reloc.size,
211                             addend: reloc.addend as i32,
212                         },
213                     )
214                     .expect("faerie relocation error");
215             }
216             Ok(())
217         });
218     }
219 }
220
221 pub struct FunctionDebugContext<'a, 'tcx> {
222     debug_context: &'a mut DebugContext<'tcx>,
223     entry_id: UnitEntryId,
224     symbol: usize,
225     mir_span: Span,
226 }
227
228 impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
229     pub fn new(
230         tcx: TyCtxt<'b, 'tcx, 'tcx>,
231         debug_context: &'a mut DebugContext<'tcx>,
232         mir: &Mir,
233         name: &str,
234         _sig: &Signature,
235     ) -> Self {
236         let (symbol, _) = debug_context.symbols.insert_full(name.to_string());
237
238         // FIXME: add to appropriate scope intead of root
239         let scope = debug_context.dwarf.unit.root();
240
241         let entry_id = debug_context.dwarf.unit.add(scope, gimli::DW_TAG_subprogram);
242         let entry = debug_context.dwarf.unit.get_mut(entry_id);
243         let name_id = debug_context.dwarf.strings.add(name);
244         entry.set(
245             gimli::DW_AT_linkage_name,
246             AttributeValue::StringRef(name_id),
247         );
248
249         entry.set(
250             gimli::DW_AT_low_pc,
251             AttributeValue::Address(Address::Relative { symbol, addend: 0 }),
252         );
253
254         debug_context.emit_location(tcx, entry_id, mir.span);
255
256         FunctionDebugContext {
257             debug_context,
258             entry_id,
259             symbol,
260             mir_span: mir.span,
261         }
262     }
263
264     pub fn define(
265         &mut self,
266         tcx: TyCtxt,
267         //module: &mut Module<impl Backend>,
268         code_size: u32,
269         context: &Context,
270         isa: &cranelift::codegen::isa::TargetIsa,
271         source_info_set: &indexmap::IndexSet<SourceInfo>,
272     ) {
273         let entry = self.debug_context.dwarf.unit.get_mut(self.entry_id);
274         entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(code_size as u64));
275
276         self.debug_context.unit_range_list.0.push(Range::StartLength {
277             begin: Address::Relative {
278                 symbol: self.symbol,
279                 addend: 0,
280             },
281             length: code_size as u64,
282         });
283
284         let line_program = &mut self.debug_context.dwarf.unit.line_program;
285
286         line_program.begin_sequence(Some(Address::Relative {
287             symbol: self.symbol,
288             addend: 0,
289         }));
290
291         let encinfo = isa.encoding_info();
292         let func = &context.func;
293         let mut ebbs = func.layout.ebbs().collect::<Vec<_>>();
294         ebbs.sort_by_key(|ebb| func.offsets[*ebb]); // Ensure inst offsets always increase
295
296         let line_strings = &mut self.debug_context.dwarf.line_strings;
297         let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| {
298             let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
299             let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
300
301             /*println!(
302                 "srcloc {:>04X} {}:{}:{}",
303                 line_program.row().address_offset,
304                 file.display(),
305                 loc.line,
306                 loc.col.to_u32()
307             );*/
308
309             line_program.row().file = file_id;
310             line_program.row().line = loc.line as u64;
311             line_program.row().column = loc.col.to_u32() as u64 + 1;
312             line_program.generate_row();
313         };
314
315         let mut end = 0;
316         for ebb in ebbs {
317             for (offset, inst, size) in func.inst_offsets(ebb, &encinfo) {
318                 let srcloc = func.srclocs[inst];
319                 line_program.row().address_offset = offset as u64;
320                 if !srcloc.is_default() {
321                     let source_info = *source_info_set.get_index(srcloc.bits() as usize).unwrap();
322                     create_row_for_span(line_program, source_info.span);
323                 } else {
324                     create_row_for_span(line_program, self.mir_span);
325                 }
326                 end = offset + size;
327             }
328         }
329
330         if code_size != end {
331             line_program.row().address_offset = end as u64;
332             create_row_for_span(line_program, self.mir_span);
333         }
334
335         line_program.end_sequence(code_size as u64);
336     }
337 }
338
339 #[derive(Clone)]
340 struct WriterRelocate {
341     relocs: Vec<DebugReloc>,
342     writer: EndianVec<RunTimeEndian>,
343 }
344
345 impl WriterRelocate {
346     fn new(ctx: & DebugContext) -> Self {
347         WriterRelocate {
348             relocs: Vec::new(),
349             writer: EndianVec::new(ctx.endian),
350         }
351     }
352 }
353
354 impl Writer for WriterRelocate {
355     type Endian = RunTimeEndian;
356
357     fn endian(&self) -> Self::Endian {
358         self.writer.endian()
359     }
360
361     fn len(&self) -> usize {
362         self.writer.len()
363     }
364
365     fn write(&mut self, bytes: &[u8]) -> Result<()> {
366         self.writer.write(bytes)
367     }
368
369     fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()> {
370         self.writer.write_at(offset, bytes)
371     }
372
373     fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
374         match address {
375             Address::Absolute(val) => self.write_word(val, size),
376             Address::Relative { symbol, addend } => {
377                 let offset = self.len() as u64;
378                 self.relocs.push(DebugReloc {
379                     offset: offset as u32,
380                     size,
381                     name: DebugRelocName::Symbol(symbol),
382                     addend: addend as i64,
383                 });
384                 self.write_word(0, size)
385             }
386         }
387     }
388
389     fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
390         let offset = self.len() as u32;
391         self.relocs.push(DebugReloc {
392             offset,
393             size,
394             name: DebugRelocName::Section(section),
395             addend: val as i64,
396         });
397         self.write_word(0, size)
398     }
399
400     fn write_offset_at(
401         &mut self,
402         offset: usize,
403         val: usize,
404         section: SectionId,
405         size: u8,
406     ) -> Result<()> {
407         self.relocs.push(DebugReloc {
408             offset: offset as u32,
409             size,
410             name: DebugRelocName::Section(section),
411             addend: val as i64,
412         });
413         self.write_word_at(offset, 0, size)
414     }
415 }