]> git.lizzy.rs Git - rust.git/blob - src/debuginfo/mod.rs
Support file hashes in .debug_line
[rust.git] / src / debuginfo / mod.rs
1 mod emit;
2 mod line_info;
3
4 use std::time::SystemTime;
5
6 use crate::prelude::*;
7
8 use rustc_span::{FileName, SourceFileHash, SourceFileHashAlgorithm};
9
10 use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
11 use cranelift_codegen::isa::TargetIsa;
12 use cranelift_codegen::ValueLocRange;
13
14 use gimli::write::{
15     self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
16     LocationList, Range, RangeList, UnitEntryId, Writer, FileInfo,
17 };
18 use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, X86_64};
19
20 pub(crate) use emit::{DebugReloc, DebugRelocName};
21
22 fn target_endian(tcx: TyCtxt<'_>) -> RunTimeEndian {
23     use rustc_target::abi::Endian;
24
25     match tcx.data_layout.endian {
26         Endian::Big => RunTimeEndian::Big,
27         Endian::Little => RunTimeEndian::Little,
28     }
29 }
30
31 const MD5_LEN: usize = 16;
32
33 pub(crate) struct DebugContext<'tcx> {
34     tcx: TyCtxt<'tcx>,
35
36     endian: RunTimeEndian,
37     symbols: indexmap::IndexMap<FuncId, String>,
38
39     dwarf: DwarfUnit,
40     unit_range_list: RangeList,
41
42     types: FxHashMap<Ty<'tcx>, UnitEntryId>,
43 }
44
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
52             version: 5,
53             address_size,
54         };
55
56         let mut dwarf = DwarfUnit::new(encoding);
57
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() {
63             Some(path) => {
64                 let name = path.to_string_lossy().into_owned();
65                 let hash = tcx.sess
66                     .source_map()
67                     .get_source_file(&FileName::Real(path))
68                     .map(|f| f.src_hash)
69                     .filter(|h| matches!(h, SourceFileHash { kind: SourceFileHashAlgorithm::Md5, .. }))
70                     .map(|h| {
71                         let mut buf = [0u8; MD5_LEN];
72                         buf.copy_from_slice(h.hash_bytes());
73                         buf
74                     });
75                 (name, hash)
76             },
77             None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
78         };
79
80         let mut line_program = LineProgram::new(
81             encoding,
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),
85             Some(FileInfo {
86                 timestamp: SystemTime::now()
87                     .duration_since(SystemTime::UNIX_EPOCH)
88                     .map(|t| t.as_secs())
89                     .unwrap_or(0),
90                 size: 0,
91                 md5: md5.unwrap_or_default(),
92             }),
93         );
94         line_program.file_has_timestamp = true;
95         line_program.file_has_md5 = md5.is_some();
96
97         dwarf.unit.line_program = line_program;
98
99         {
100             let name = dwarf.strings.add(name);
101             let comp_dir = dwarf.strings.add(comp_dir);
102
103             let root = dwarf.unit.root();
104             let root = dwarf.unit.get_mut(root);
105             root.set(
106                 gimli::DW_AT_producer,
107                 AttributeValue::StringRef(dwarf.strings.add(producer)),
108             );
109             root.set(
110                 gimli::DW_AT_language,
111                 AttributeValue::Language(gimli::DW_LANG_Rust),
112             );
113             root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
114             root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
115             root.set(
116                 gimli::DW_AT_low_pc,
117                 AttributeValue::Address(Address::Constant(0)),
118             );
119         }
120
121         DebugContext {
122             tcx,
123
124             endian: target_endian(tcx),
125             symbols: indexmap::IndexMap::new(),
126
127             dwarf,
128             unit_range_list: RangeList(Vec::new()),
129
130             types: FxHashMap::default(),
131         }
132     }
133
134     fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
135         if let Some(type_id) = self.types.get(ty) {
136             return *type_id;
137         }
138
139         let new_entry = |dwarf: &mut DwarfUnit, tag| dwarf.unit.add(dwarf.unit.root(), tag);
140
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));
145             type_id
146         };
147
148         let name = format!("{}", ty);
149         let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
150
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 {
159                 ty: pointee_ty,
160                 mutbl: _mutbl,
161             }) => {
162                 let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_pointer_type);
163
164                 // Ensure that type is inserted before recursing to avoid duplicates
165                 self.types.insert(ty, type_id);
166
167                 let pointee = self.dwarf_ty(pointee_ty);
168
169                 let type_entry = self.dwarf.unit.get_mut(type_id);
170
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));
173
174                 type_id
175             }
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);
178
179                 // Ensure that type is inserted before recursing to avoid duplicates
180                 self.types.insert(ty, type_id);
181
182                 let variant = adt_def.non_enum_variant();
183
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 {
187                         tcx: self.tcx,
188                         param_env: ParamEnv::reveal_all(),
189                     }, field_idx).unwrap();
190
191                     let field_type = self.dwarf_ty(field_layout.ty);
192
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);
195
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));
199                 }
200
201                 type_id
202             }
203             _ => new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type),
204         };
205
206         let type_entry = self.dwarf.unit.get_mut(type_id);
207
208         type_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
209         type_entry.set(
210             gimli::DW_AT_byte_size,
211             AttributeValue::Udata(layout.size.bytes()),
212         );
213
214         self.types.insert(ty, type_id);
215
216         type_id
217     }
218 }
219
220 pub(crate) struct FunctionDebugContext<'a, 'tcx> {
221     debug_context: &'a mut DebugContext<'tcx>,
222     entry_id: UnitEntryId,
223     symbol: usize,
224     instance: Instance<'tcx>,
225     mir: &'tcx mir::Body<'tcx>,
226 }
227
228 impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
229     pub(crate) fn new(
230         debug_context: &'a mut DebugContext<'tcx>,
231         instance: Instance<'tcx>,
232         func_id: FuncId,
233         name: &str,
234     ) -> Self {
235         let mir = *debug_context.tcx.instance_mir(instance.def);
236
237         let (symbol, _) = debug_context.symbols.insert_full(func_id, name.to_string());
238
239         // FIXME: add to appropriate scope intead of root
240         let scope = debug_context.dwarf.unit.root();
241
242         let entry_id = debug_context
243             .dwarf
244             .unit
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);
248         entry.set(
249             gimli::DW_AT_linkage_name,
250             AttributeValue::StringRef(name_id),
251         );
252
253         FunctionDebugContext {
254             debug_context,
255             entry_id,
256             symbol,
257             instance,
258             mir,
259         }
260     }
261
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(),
266             &ty,
267         );
268         let dw_ty = self.debug_context.dwarf_ty(ty);
269
270         let var_id = self
271             .debug_context
272             .dwarf
273             .unit
274             .add(self.entry_id, gimli::DW_TAG_variable);
275         let var_entry = self.debug_context.dwarf.unit.get_mut(var_id);
276
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));
279
280         var_id
281     }
282
283     pub(crate) fn define(
284         &mut self,
285         context: &Context,
286         isa: &dyn TargetIsa,
287         source_info_set: &indexmap::IndexSet<SourceInfo>,
288         local_map: FxHashMap<mir::Local, CPlace<'tcx>>,
289     ) {
290         let end = self.create_debug_lines(context, isa, source_info_set);
291
292         self.debug_context
293             .unit_range_list
294             .0
295             .push(Range::StartLength {
296                 begin: Address::Symbol {
297                     symbol: self.symbol,
298                     addend: 0,
299                 },
300                 length: end as u64,
301             });
302
303         // FIXME make it more reliable and implement scopes before re-enabling this.
304         if false {
305             let value_labels_ranges = context.build_value_labels_ranges(isa).unwrap();
306
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);
309
310                 let location = place_location(
311                     self,
312                     isa,
313                     context,
314                     &local_map,
315                     &value_labels_ranges,
316                     Place {
317                         local,
318                         projection: ty::List::empty(),
319                     },
320                 );
321
322                 let var_entry = self.debug_context.dwarf.unit.get_mut(var_id);
323                 var_entry.set(gimli::DW_AT_location, location);
324             }
325         }
326
327         // FIXME create locals for all entries in mir.var_debug_info
328     }
329 }
330
331 fn place_location<'a, 'tcx>(
332     func_debug_ctx: &mut FunctionDebugContext<'a, 'tcx>,
333     isa: &dyn TargetIsa,
334     context: &Context,
335     local_map: &FxHashMap<mir::Local, CPlace<'tcx>>,
336     #[allow(rustc::default_hash_types)]
337     value_labels_ranges: &std::collections::HashMap<ValueLabel, Vec<ValueLocRange>>,
338     place: Place<'tcx>,
339 ) -> AttributeValue {
340     assert!(place.projection.is_empty()); // FIXME implement them
341
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(
347                     value_loc_ranges
348                         .iter()
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),
353                             },
354                             end: Address::Symbol {
355                                 symbol: func_debug_ctx.symbol,
356                                 addend: i64::from(value_loc_range.end),
357                             },
358                             data: Expression(
359                                 translate_loc(isa, value_loc_range.loc, &context.func.stack_slots).unwrap(),
360                             ),
361                         })
362                         .collect(),
363                 );
364                 let loc_list_id = func_debug_ctx.debug_context.dwarf.unit.locations.add(loc_list);
365
366                 AttributeValue::LocationListRef(loc_list_id)
367             } else {
368                 // FIXME set value labels for unused locals
369
370                 AttributeValue::Exprloc(Expression(vec![]))
371             }
372         }
373         CPlaceInner::Addr(_, _) => {
374             // FIXME implement this (used by arguments and returns)
375
376             AttributeValue::Exprloc(Expression(vec![]))
377
378             // For PointerBase::Stack:
379             //AttributeValue::Exprloc(Expression(translate_loc(ValueLoc::Stack(*stack_slot), &context.func.stack_slots).unwrap()))
380         }
381     }
382 }
383
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>> {
386     match loc {
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])
391         }
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);
396                 writer
397                     .write_u8(gimli::constants::DW_OP_breg0.0 + X86_64::RBP.0 as u8)
398                     .expect("bp wr");
399                 writer.write_sleb128(ss_offset as i64 + 16).expect("ss wr");
400                 let buf = writer.into_vec();
401                 return Some(buf);
402             }
403             None
404         }
405         _ => None,
406     }
407 }