rust/src/debuginfo/mod.rs

438 lines
15 KiB
Rust
Raw Normal View History

mod emit;
mod line_info;
2019-01-17 18:07:27 +01:00
use crate::prelude::*;
use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
2020-03-17 15:30:32 +01:00
use cranelift_codegen::isa::{RegUnit, TargetIsa};
use cranelift_codegen::ValueLocRange;
2019-11-11 21:43:57 +01:00
2019-01-17 18:07:27 +01:00
use gimli::write::{
2019-11-12 21:13:15 +01:00
self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
LocationList, Range, RangeList, UnitEntryId, Writer,
2019-01-17 18:07:27 +01:00
};
use gimli::{Encoding, Format, LineEncoding, Register, RunTimeEndian, X86_64};
pub(crate) use emit::{DebugReloc, DebugRelocName};
2019-01-17 18:07:27 +01:00
fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
use rustc_middle::ty::layout::Endian;
2019-01-17 18:07:27 +01:00
match tcx.data_layout.endian {
Endian::Big => RunTimeEndian::Big,
Endian::Little => RunTimeEndian::Little,
}
}
2019-01-19 12:07:42 +01:00
pub(crate) struct DebugContext<'tcx> {
2019-11-09 18:40:11 +01:00
tcx: TyCtxt<'tcx>,
2019-01-17 18:07:27 +01:00
endian: RunTimeEndian,
2019-10-19 10:52:56 +02:00
symbols: indexmap::IndexMap<FuncId, String>,
2019-02-09 17:15:15 +10:00
dwarf: DwarfUnit,
2019-01-19 12:00:51 +01:00
unit_range_list: RangeList,
2019-01-17 18:07:27 +01:00
2019-11-09 18:40:11 +01:00
types: HashMap<Ty<'tcx>, UnitEntryId>,
2019-01-17 18:07:27 +01:00
}
impl<'tcx> DebugContext<'tcx> {
pub(crate) fn new(tcx: TyCtxt<'tcx>, address_size: u8) -> Self {
2019-01-26 12:37:49 +01:00
let encoding = Encoding {
format: Format::Dwarf32,
// TODO: this should be configurable
// macOS doesn't seem to support DWARF > 3
version: 3,
address_size,
};
2019-01-17 18:07:27 +01:00
2019-02-09 17:15:15 +10:00
let mut dwarf = DwarfUnit::new(encoding);
2019-01-17 18:07:27 +01:00
// FIXME: how to get version when building out of tree?
// Normally this would use option_env!("CFG_VERSION").
let producer = format!("cg_clif (rustc {})", "unknown version");
2019-01-17 18:07:27 +01:00
let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned();
let name = match tcx.sess.local_crate_source_file {
Some(ref path) => path.to_string_lossy().into_owned(),
None => tcx.crate_name(LOCAL_CRATE).to_string(),
};
2019-02-09 17:15:15 +10:00
let line_program = LineProgram::new(
2019-01-26 12:37:49 +01:00
encoding,
2019-02-18 18:32:40 +01:00
LineEncoding::default(),
2019-02-09 17:15:15 +10:00
LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
2019-01-17 18:07:27 +01:00
None,
2019-02-09 17:15:15 +10:00
);
dwarf.unit.line_program = line_program;
2019-01-17 18:07:27 +01:00
{
2020-03-07 11:27:49 +01:00
let name = dwarf.strings.add(name);
let comp_dir = dwarf.strings.add(comp_dir);
2019-01-17 18:07:27 +01:00
2019-02-09 17:15:15 +10:00
let root = dwarf.unit.root();
let root = dwarf.unit.get_mut(root);
2019-01-17 18:07:27 +01:00
root.set(
gimli::DW_AT_producer,
2019-02-09 17:15:15 +10:00
AttributeValue::StringRef(dwarf.strings.add(producer)),
2019-01-17 18:07:27 +01:00
);
root.set(
gimli::DW_AT_language,
AttributeValue::Language(gimli::DW_LANG_Rust),
);
root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
root.set(
gimli::DW_AT_low_pc,
2019-05-04 16:20:43 +02:00
AttributeValue::Address(Address::Constant(0)),
2019-01-17 18:07:27 +01:00
);
}
2019-02-21 15:06:09 +01:00
DebugContext {
2019-11-09 18:40:11 +01:00
tcx,
2019-01-17 18:07:27 +01:00
endian: target_endian(tcx),
2019-10-19 10:52:56 +02:00
symbols: indexmap::IndexMap::new(),
2019-02-09 17:15:15 +10:00
dwarf,
2019-01-19 12:00:51 +01:00
unit_range_list: RangeList(Vec::new()),
2019-11-09 18:40:11 +01:00
types: HashMap::new(),
2019-01-17 18:07:27 +01:00
}
}
2019-11-09 18:40:11 +01:00
fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
if let Some(type_id) = self.types.get(ty) {
return *type_id;
}
2019-11-12 21:13:15 +01:00
let new_entry = |dwarf: &mut DwarfUnit, tag| dwarf.unit.add(dwarf.unit.root(), tag);
2019-11-09 18:40:11 +01:00
let primitive = |dwarf: &mut DwarfUnit, ate| {
2019-11-09 18:40:11 +01:00
let type_id = new_entry(dwarf, gimli::DW_TAG_base_type);
let type_entry = dwarf.unit.get_mut(type_id);
type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(ate));
type_id
};
let name = format!("{}", ty);
let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
2019-11-09 18:40:11 +01:00
let type_id = match ty.kind {
ty::Bool => primitive(&mut self.dwarf, gimli::DW_ATE_boolean),
ty::Char => primitive(&mut self.dwarf, gimli::DW_ATE_UTF),
ty::Uint(_) => primitive(&mut self.dwarf, gimli::DW_ATE_unsigned),
ty::Int(_) => primitive(&mut self.dwarf, gimli::DW_ATE_signed),
ty::Float(_) => primitive(&mut self.dwarf, gimli::DW_ATE_float),
2020-02-22 15:17:30 +01:00
ty::Ref(_, pointee_ty, _mutbl)
2019-11-12 21:13:15 +01:00
| ty::RawPtr(ty::TypeAndMut {
ty: pointee_ty,
2020-02-22 15:17:30 +01:00
mutbl: _mutbl,
2019-11-12 21:13:15 +01:00
}) => {
2019-11-09 18:40:11 +01:00
let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_pointer_type);
// Ensure that type is inserted before recursing to avoid duplicates
self.types.insert(ty, type_id);
let pointee = self.dwarf_ty(pointee_ty);
let type_entry = self.dwarf.unit.get_mut(type_id);
//type_entry.set(gimli::DW_AT_mutable, AttributeValue::Flag(mutbl == rustc_hir::Mutability::Mut));
2019-11-09 18:40:11 +01:00
type_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(pointee));
type_id
}
ty::Adt(adt_def, _substs) if adt_def.is_struct() && !layout.is_unsized() => {
let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type);
// Ensure that type is inserted before recursing to avoid duplicates
self.types.insert(ty, type_id);
let variant = adt_def.non_enum_variant();
for (field_idx, field_def) in variant.fields.iter().enumerate() {
let field_offset = layout.fields.offset(field_idx);
let field_layout = layout.field(&layout::LayoutCx {
tcx: self.tcx,
param_env: ParamEnv::reveal_all(),
}, field_idx).unwrap();
let field_type = self.dwarf_ty(field_layout.ty);
let field_id = self.dwarf.unit.add(type_id, gimli::DW_TAG_member);
let field_entry = self.dwarf.unit.get_mut(field_id);
field_entry.set(gimli::DW_AT_name, AttributeValue::String(field_def.ident.as_str().to_string().into_bytes()));
field_entry.set(gimli::DW_AT_data_member_location, AttributeValue::Udata(field_offset.bytes()));
field_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(field_type));
}
type_id
}
2019-11-09 18:40:11 +01:00
_ => new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type),
};
let type_entry = self.dwarf.unit.get_mut(type_id);
type_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
2019-11-12 21:13:15 +01:00
type_entry.set(
gimli::DW_AT_byte_size,
AttributeValue::Udata(layout.size.bytes()),
);
2019-11-09 18:40:11 +01:00
self.types.insert(ty, type_id);
type_id
}
2019-01-17 18:07:27 +01:00
}
pub(crate) struct FunctionDebugContext<'a, 'tcx> {
2019-01-17 18:07:27 +01:00
debug_context: &'a mut DebugContext<'tcx>,
entry_id: UnitEntryId,
symbol: usize,
2019-11-11 20:49:20 +01:00
instance: Instance<'tcx>,
mir: &'tcx mir::Body<'tcx>,
2019-01-17 18:07:27 +01:00
}
impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
pub(crate) fn new(
2019-01-17 18:07:27 +01:00
debug_context: &'a mut DebugContext<'tcx>,
2019-11-11 20:49:20 +01:00
instance: Instance<'tcx>,
func_id: FuncId,
2019-01-17 18:07:27 +01:00
name: &str,
) -> Self {
2019-12-16 12:11:14 +01:00
let mir = *debug_context.tcx.instance_mir(instance.def);
2019-11-11 20:49:20 +01:00
2019-10-19 10:52:56 +02:00
let (symbol, _) = debug_context.symbols.insert_full(func_id, name.to_string());
2019-01-17 18:07:27 +01:00
// FIXME: add to appropriate scope intead of root
2019-02-09 17:15:15 +10:00
let scope = debug_context.dwarf.unit.root();
2019-01-17 18:07:27 +01:00
2019-02-21 15:06:09 +01:00
let entry_id = debug_context
.dwarf
.unit
.add(scope, gimli::DW_TAG_subprogram);
2019-02-09 17:15:15 +10:00
let entry = debug_context.dwarf.unit.get_mut(entry_id);
let name_id = debug_context.dwarf.strings.add(name);
2019-01-17 18:07:27 +01:00
entry.set(
gimli::DW_AT_linkage_name,
AttributeValue::StringRef(name_id),
);
FunctionDebugContext {
debug_context,
entry_id,
symbol,
2019-11-11 20:49:20 +01:00
instance,
mir,
2019-01-17 18:07:27 +01:00
}
}
fn define_local(&mut self, name: String, ty: Ty<'tcx>) -> UnitEntryId {
let ty = self.debug_context.tcx.subst_and_normalize_erasing_regions(
self.instance.substs,
ty::ParamEnv::reveal_all(),
&ty,
);
let dw_ty = self.debug_context.dwarf_ty(ty);
let var_id = self
.debug_context
.dwarf
.unit
.add(self.entry_id, gimli::DW_TAG_variable);
let var_entry = self.debug_context.dwarf.unit.get_mut(var_id);
2019-11-12 21:13:15 +01:00
var_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
var_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(dw_ty));
var_id
}
pub(crate) fn define(
2019-01-17 18:07:27 +01:00
&mut self,
context: &Context,
2020-03-17 15:30:32 +01:00
isa: &dyn TargetIsa,
source_info_set: &indexmap::IndexSet<SourceInfo>,
local_map: HashMap<mir::Local, CPlace<'tcx>>,
2019-01-17 18:07:27 +01:00
) {
2019-11-12 21:10:51 +01:00
let end = self.create_debug_lines(context, isa, source_info_set);
self.debug_context
.unit_range_list
.0
.push(Range::StartLength {
begin: Address::Symbol {
symbol: self.symbol,
addend: 0,
},
length: end as u64,
});
2019-01-19 16:16:30 +01:00
2019-12-16 12:12:51 +01:00
// FIXME make it more reliable and implement scopes before re-enabling this.
if false {
let value_labels_ranges = context.build_value_labels_ranges(isa).unwrap();
for (local, _local_decl) in self.mir.local_decls.iter_enumerated() {
let var_id = self.define_local(format!("{:?}", local), &self.mir.local_decls[local].ty);
let location = place_location(
self,
2020-03-17 15:30:32 +01:00
isa,
2019-12-16 12:12:51 +01:00
context,
&local_map,
&value_labels_ranges,
Place {
local,
2019-12-16 12:12:51 +01:00
projection: ty::List::empty(),
},
);
let var_entry = self.debug_context.dwarf.unit.get_mut(var_id);
var_entry.set(gimli::DW_AT_location, location);
}
2019-11-09 16:42:21 +01:00
}
// FIXME create locals for all entries in mir.var_debug_info
2019-01-17 18:07:27 +01:00
}
}
fn place_location<'a, 'tcx>(
func_debug_ctx: &mut FunctionDebugContext<'a, 'tcx>,
2020-03-17 15:30:32 +01:00
isa: &dyn TargetIsa,
context: &Context,
local_map: &HashMap<mir::Local, CPlace<'tcx>>,
value_labels_ranges: &HashMap<ValueLabel, Vec<ValueLocRange>>,
place: Place<'tcx>,
) -> AttributeValue {
assert!(place.projection.is_empty()); // FIXME implement them
match local_map[&place.local].inner() {
CPlaceInner::Var(local) => {
let value_label = cranelift_codegen::ir::ValueLabel::from_u32(local.as_u32());
if let Some(value_loc_ranges) = value_labels_ranges.get(&value_label) {
let loc_list = LocationList(
value_loc_ranges
.iter()
.map(|value_loc_range| Location::StartEnd {
begin: Address::Symbol {
symbol: func_debug_ctx.symbol,
addend: i64::from(value_loc_range.start),
},
end: Address::Symbol {
symbol: func_debug_ctx.symbol,
addend: i64::from(value_loc_range.end),
},
data: Expression(
2020-03-17 15:30:32 +01:00
translate_loc(isa, value_loc_range.loc, &context.func.stack_slots).unwrap(),
),
})
.collect(),
);
let loc_list_id = func_debug_ctx.debug_context.dwarf.unit.locations.add(loc_list);
AttributeValue::LocationListRef(loc_list_id)
} else {
// FIXME set value labels for unused locals
AttributeValue::Exprloc(Expression(vec![]))
}
}
CPlaceInner::Addr(_, _) => {
// FIXME implement this (used by arguments and returns)
2019-11-11 21:43:57 +01:00
AttributeValue::Exprloc(Expression(vec![]))
2019-12-20 22:00:12 +01:00
// For PointerBase::Stack:
//AttributeValue::Exprloc(Expression(translate_loc(ValueLoc::Stack(*stack_slot), &context.func.stack_slots).unwrap()))
}
CPlaceInner::NoPlace => AttributeValue::Exprloc(Expression(vec![])),
}
}
2019-11-11 21:43:57 +01:00
2020-03-17 15:30:32 +01:00
// Adapted from https://github.com/bytecodealliance/wasmtime/blob/50496efb6bac32aaf469c6d9186b322de83549bf/crates/debug/src/transform/map_reg.rs
pub(crate) fn map_reg(isa: &dyn TargetIsa, reg: RegUnit) -> Register {
// TODO avoid duplication with fde.rs
assert!(isa.name() == "x86" && isa.pointer_bits() == 64);
// Mapping from https://github.com/bytecodealliance/cranelift/pull/902 by @iximeow
const X86_GP_REG_MAP: [Register; 16] = [
X86_64::RAX,
X86_64::RCX,
X86_64::RDX,
X86_64::RBX,
X86_64::RSP,
X86_64::RBP,
X86_64::RSI,
X86_64::RDI,
X86_64::R8,
X86_64::R9,
X86_64::R10,
X86_64::R11,
X86_64::R12,
X86_64::R13,
X86_64::R14,
X86_64::R15,
];
const X86_XMM_REG_MAP: [Register; 16] = [
X86_64::XMM0,
X86_64::XMM1,
X86_64::XMM2,
X86_64::XMM3,
X86_64::XMM4,
X86_64::XMM5,
X86_64::XMM6,
X86_64::XMM7,
X86_64::XMM8,
X86_64::XMM9,
X86_64::XMM10,
X86_64::XMM11,
X86_64::XMM12,
X86_64::XMM13,
X86_64::XMM14,
X86_64::XMM15,
];
let reg_info = isa.register_info();
let bank = reg_info.bank_containing_regunit(reg).unwrap();
match bank.name {
"IntRegs" => {
// x86 GP registers have a weird mapping to DWARF registers, so we use a
// lookup table.
X86_GP_REG_MAP[(reg - bank.first_unit) as usize]
2019-11-11 21:43:57 +01:00
}
2020-03-17 15:30:32 +01:00
"FloatRegs" => X86_XMM_REG_MAP[(reg - bank.first_unit) as usize],
bank_name => {
panic!("unsupported register bank: {}", bank_name);
2019-11-11 21:43:57 +01:00
}
}
}
2020-03-17 15:30:32 +01:00
// Adapted from https://github.com/CraneStation/wasmtime/blob/5a1845b4caf7a5dba8eda1fef05213a532ed4259/crates/debug/src/transform/expression.rs#L59-L137
fn translate_loc(isa: &dyn TargetIsa, loc: ValueLoc, stack_slots: &StackSlots) -> Option<Vec<u8>> {
2019-11-11 21:43:57 +01:00
match loc {
ValueLoc::Reg(reg) => {
2020-03-17 15:30:32 +01:00
let machine_reg = map_reg(isa, reg).0 as u8;
2019-11-11 21:43:57 +01:00
assert!(machine_reg <= 32); // FIXME
Some(vec![gimli::constants::DW_OP_reg0.0 + machine_reg])
}
ValueLoc::Stack(ss) => {
if let Some(ss_offset) = stack_slots[ss].offset {
let endian = gimli::RunTimeEndian::Little;
let mut writer = write::EndianVec::new(endian);
writer
.write_u8(gimli::constants::DW_OP_breg0.0 + X86_64::RBP.0 as u8)
.expect("bp wr");
writer.write_sleb128(ss_offset as i64 + 16).expect("ss wr");
let buf = writer.into_vec();
return Some(buf);
}
None
}
_ => None,
}
}