5 use std::marker::PhantomData;
7 use syntax::source_map::FileName;
10 Address, AttributeValue, DwarfUnit, EndianVec, LineProgram, Range,
11 RangeList, Result, SectionId, UnitEntryId,
12 Writer, FileId, LineStringTable, LineString, Sections,
14 use gimli::{Encoding, Format, RunTimeEndian};
18 fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
19 use rustc::ty::layout::Endian;
21 match tcx.data_layout.endian {
22 Endian::Big => RunTimeEndian::Big,
23 Endian::Little => RunTimeEndian::Little,
27 fn line_program_add_file(line_program: &mut LineProgram, line_strings: &mut LineStringTable, file: &FileName) -> FileId {
29 FileName::Real(path) => {
30 let dir_name = LineString::new(path.parent().unwrap().to_str().unwrap().as_bytes(), line_program.encoding(), line_strings);
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(
40 // FIXME give more appropriate file names
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(
68 fn name<'a>(&self, ctx: &'a DebugContext) -> &'a str {
70 DebugRelocName::Section(id) => id.name(),
71 DebugRelocName::Symbol(index) => ctx.symbols.get_index(index).unwrap(),
76 pub struct DebugContext<'tcx> {
77 endian: RunTimeEndian,
78 symbols: indexmap::IndexSet<String>,
81 unit_range_list: RangeList,
83 _dummy: PhantomData<&'tcx ()>,
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
96 let mut dwarf = DwarfUnit::new(encoding);
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(),
107 let line_program = LineProgram::new(
113 LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
114 LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
117 dwarf.unit.line_program = line_program;
120 let name = dwarf.strings.add(&*name);
121 let comp_dir = dwarf.strings.add(&*comp_dir);
123 let root = dwarf.unit.root();
124 let root = dwarf.unit.get_mut(root);
126 gimli::DW_AT_producer,
127 AttributeValue::StringRef(dwarf.strings.add(producer)),
130 gimli::DW_AT_language,
131 AttributeValue::Language(gimli::DW_LANG_Rust),
133 root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
134 root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
137 AttributeValue::Address(Address::Absolute(0)),
142 endian: target_endian(tcx),
143 symbols: indexmap::IndexSet::new(),
146 unit_range_list: RangeList(Vec::new()),
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());
155 let file_id = line_program_add_file(
156 &mut self.dwarf.unit.line_program,
157 &mut self.dwarf.line_strings,
161 let entry = self.dwarf.unit.get_mut(entry_id);
163 entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(file_id));
165 gimli::DW_AT_decl_line,
166 AttributeValue::Udata(loc.line as u64),
168 // FIXME: probably omit this
170 gimli::DW_AT_decl_column,
171 AttributeValue::Udata(loc.col.to_usize() as u64),
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);
181 AttributeValue::RangeListRef(unit_range_list_id),
184 let mut sections = Sections::new(WriterRelocate::new(self));
185 self.dwarf.write(&mut sections).unwrap();
187 let _: Result<()> = sections.for_each_mut(|id, section| {
188 if !section.writer.slice().is_empty() {
193 section.writer.take(),
200 let _: Result<()> = sections.for_each(|id, section| {
201 for reloc in §ion.relocs {
206 to: reloc.name(self),
207 at: u64::from(reloc.offset),
209 faerie::Reloc::Debug {
211 addend: reloc.addend as i32,
214 .expect("faerie relocation error");
221 pub struct FunctionDebugContext<'a, 'tcx> {
222 debug_context: &'a mut DebugContext<'tcx>,
223 entry_id: UnitEntryId,
228 impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
230 tcx: TyCtxt<'b, 'tcx, 'tcx>,
231 debug_context: &'a mut DebugContext<'tcx>,
236 let (symbol, _) = debug_context.symbols.insert_full(name.to_string());
238 // FIXME: add to appropriate scope intead of root
239 let scope = debug_context.dwarf.unit.root();
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);
245 gimli::DW_AT_linkage_name,
246 AttributeValue::StringRef(name_id),
251 AttributeValue::Address(Address::Relative { symbol, addend: 0 }),
254 debug_context.emit_location(tcx, entry_id, mir.span);
256 FunctionDebugContext {
267 //module: &mut Module<impl Backend>,
270 isa: &cranelift::codegen::isa::TargetIsa,
271 source_info_set: &indexmap::IndexSet<SourceInfo>,
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));
276 self.debug_context.unit_range_list.0.push(Range::StartLength {
277 begin: Address::Relative {
281 length: code_size as u64,
284 let line_program = &mut self.debug_context.dwarf.unit.line_program;
286 line_program.begin_sequence(Some(Address::Relative {
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
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);
302 "srcloc {:>04X} {}:{}:{}",
303 line_program.row().address_offset,
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();
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);
324 create_row_for_span(line_program, self.mir_span);
330 if code_size != end {
331 line_program.row().address_offset = end as u64;
332 create_row_for_span(line_program, self.mir_span);
335 line_program.end_sequence(code_size as u64);
340 struct WriterRelocate {
341 relocs: Vec<DebugReloc>,
342 writer: EndianVec<RunTimeEndian>,
345 impl WriterRelocate {
346 fn new(ctx: & DebugContext) -> Self {
349 writer: EndianVec::new(ctx.endian),
354 impl Writer for WriterRelocate {
355 type Endian = RunTimeEndian;
357 fn endian(&self) -> Self::Endian {
361 fn len(&self) -> usize {
365 fn write(&mut self, bytes: &[u8]) -> Result<()> {
366 self.writer.write(bytes)
369 fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()> {
370 self.writer.write_at(offset, bytes)
373 fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
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,
381 name: DebugRelocName::Symbol(symbol),
382 addend: addend as i64,
384 self.write_word(0, size)
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 {
394 name: DebugRelocName::Section(section),
397 self.write_word(0, size)
407 self.relocs.push(DebugReloc {
408 offset: offset as u32,
410 name: DebugRelocName::Section(section),
413 self.write_word_at(offset, 0, size)