Add debuginfo for statics
Most types are still unimplemented and get a placeholder byte array instead.
This commit is contained in:
parent
f086e7abaf
commit
4b80941ae3
@ -31,10 +31,16 @@ pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut dyn Module) {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
|
||||
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) -> DataId {
|
||||
let mut constants_cx = ConstantCx::new();
|
||||
constants_cx.todo.push(TodoItem::Static(def_id));
|
||||
constants_cx.finalize(tcx, module);
|
||||
|
||||
data_id_for_static(
|
||||
tcx, module, def_id, false,
|
||||
// For a declaration the stated mutability doesn't matter.
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn codegen_tls_ref<'tcx>(
|
||||
|
@ -15,7 +15,6 @@ pub(super) fn address_for_func(func_id: FuncId) -> Address {
|
||||
Address::Symbol { symbol: symbol as usize, addend: 0 }
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(super) fn address_for_data(data_id: DataId) -> Address {
|
||||
let symbol = data_id.as_u32();
|
||||
assert!(symbol & 1 << 31 == 0);
|
||||
|
@ -3,10 +3,12 @@
|
||||
mod emit;
|
||||
mod line_info;
|
||||
mod object;
|
||||
mod types;
|
||||
mod unwind;
|
||||
|
||||
use cranelift_codegen::ir::Endianness;
|
||||
use cranelift_codegen::isa::TargetIsa;
|
||||
use cranelift_module::DataId;
|
||||
use gimli::write::{
|
||||
Address, AttributeValue, DwarfUnit, Expression, FileId, LineProgram, LineString, Range,
|
||||
RangeList, UnitEntryId,
|
||||
@ -14,13 +16,15 @@
|
||||
use gimli::{AArch64, Encoding, Format, LineEncoding, Register, RiscV, RunTimeEndian, X86_64};
|
||||
use indexmap::IndexSet;
|
||||
use rustc_codegen_ssa::debuginfo::type_names;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::DefIdMap;
|
||||
use rustc_session::Session;
|
||||
use rustc_span::{SourceFileHash, StableSourceFileId};
|
||||
|
||||
pub(crate) use self::emit::{DebugReloc, DebugRelocName};
|
||||
pub(crate) use self::types::TypeDebugContext;
|
||||
pub(crate) use self::unwind::UnwindContext;
|
||||
use crate::debuginfo::emit::address_for_func;
|
||||
use crate::debuginfo::emit::{address_for_data, address_for_func};
|
||||
use crate::prelude::*;
|
||||
|
||||
pub(crate) fn producer(sess: &Session) -> String {
|
||||
@ -35,6 +39,7 @@ pub(crate) struct DebugContext {
|
||||
created_files: FxHashMap<(StableSourceFileId, SourceFileHash), FileId>,
|
||||
stack_pointer_register: Register,
|
||||
namespace_map: DefIdMap<UnitEntryId>,
|
||||
array_size_type: UnitEntryId,
|
||||
|
||||
should_remap_filepaths: bool,
|
||||
}
|
||||
@ -129,6 +134,19 @@ pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, cgu_name: &str) -> Self
|
||||
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
|
||||
}
|
||||
|
||||
let array_size_type = dwarf.unit.add(dwarf.unit.root(), gimli::DW_TAG_base_type);
|
||||
let array_size_type_entry = dwarf.unit.get_mut(array_size_type);
|
||||
array_size_type_entry.set(
|
||||
gimli::DW_AT_name,
|
||||
AttributeValue::StringRef(dwarf.strings.add("__ARRAY_SIZE_TYPE__")),
|
||||
);
|
||||
array_size_type_entry
|
||||
.set(gimli::DW_AT_encoding, AttributeValue::Encoding(gimli::DW_ATE_unsigned));
|
||||
array_size_type_entry.set(
|
||||
gimli::DW_AT_byte_size,
|
||||
AttributeValue::Udata(isa.frontend_config().pointer_bytes().into()),
|
||||
);
|
||||
|
||||
DebugContext {
|
||||
endian,
|
||||
dwarf,
|
||||
@ -136,6 +154,7 @@ pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, cgu_name: &str) -> Self
|
||||
created_files: FxHashMap::default(),
|
||||
stack_pointer_register,
|
||||
namespace_map: DefIdMap::default(),
|
||||
array_size_type,
|
||||
should_remap_filepaths,
|
||||
}
|
||||
}
|
||||
@ -231,6 +250,62 @@ pub(crate) fn define_function<'tcx>(
|
||||
source_loc_set: IndexSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs#L1288-L1346
|
||||
pub(crate) fn define_static<'tcx>(
|
||||
&mut self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
type_dbg: &mut TypeDebugContext<'tcx>,
|
||||
def_id: DefId,
|
||||
data_id: DataId,
|
||||
) {
|
||||
let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else { bug!() };
|
||||
if nested {
|
||||
return;
|
||||
}
|
||||
|
||||
let scope = self.item_namespace(tcx, tcx.parent(def_id));
|
||||
|
||||
let span = tcx.def_span(def_id);
|
||||
let (file_id, line, _column) = self.get_span_loc(tcx, span, span);
|
||||
|
||||
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::ParamEnv::reveal_all());
|
||||
let static_layout = tcx.layout_of(ty::ParamEnv::reveal_all().and(static_type)).unwrap();
|
||||
// FIXME use the actual type layout
|
||||
let type_id = self.debug_type(tcx, type_dbg, static_type);
|
||||
|
||||
let name = tcx.item_name(def_id);
|
||||
let linkage_name = tcx.symbol_name(Instance::mono(tcx, def_id)).name;
|
||||
|
||||
let entry_id = self.dwarf.unit.add(scope, gimli::DW_TAG_variable);
|
||||
let entry = self.dwarf.unit.get_mut(entry_id);
|
||||
let linkage_name_id = if name.as_str() != linkage_name {
|
||||
Some(self.dwarf.strings.add(linkage_name))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let name_id = self.dwarf.strings.add(name.as_str());
|
||||
|
||||
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
|
||||
entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(type_id));
|
||||
|
||||
if tcx.is_reachable_non_generic(def_id) {
|
||||
entry.set(gimli::DW_AT_external, AttributeValue::FlagPresent);
|
||||
}
|
||||
|
||||
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
|
||||
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
|
||||
|
||||
entry.set(gimli::DW_AT_alignment, AttributeValue::Udata(static_layout.align.pref.bytes()));
|
||||
|
||||
let mut expr = Expression::new();
|
||||
expr.op_addr(address_for_data(data_id));
|
||||
entry.set(gimli::DW_AT_location, AttributeValue::Exprloc(expr));
|
||||
|
||||
if let Some(linkage_name_id) = linkage_name_id {
|
||||
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(linkage_name_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FunctionDebugContext {
|
||||
|
162
src/debuginfo/types.rs
Normal file
162
src/debuginfo/types.rs
Normal file
@ -0,0 +1,162 @@
|
||||
// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
|
||||
|
||||
use gimli::write::{AttributeValue, UnitEntryId};
|
||||
use rustc_codegen_ssa::debuginfo::type_names;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
|
||||
use crate::{has_ptr_meta, DebugContext};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct TypeDebugContext<'tcx> {
|
||||
type_map: FxHashMap<Ty<'tcx>, UnitEntryId>,
|
||||
}
|
||||
|
||||
/// Returns from the enclosing function if the type debuginfo node with the given
|
||||
/// unique ID can be found in the type map.
|
||||
macro_rules! return_if_type_created_in_meantime {
|
||||
($type_dbg:expr, $ty:expr) => {
|
||||
if let Some(&type_id) = $type_dbg.type_map.get(&$ty) {
|
||||
return type_id;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl DebugContext {
|
||||
pub(crate) fn debug_type<'tcx>(
|
||||
&mut self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
type_dbg: &mut TypeDebugContext<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> UnitEntryId {
|
||||
if let Some(&type_id) = type_dbg.type_map.get(&ty) {
|
||||
return type_id;
|
||||
}
|
||||
|
||||
let type_id = match ty.kind() {
|
||||
ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
|
||||
self.basic_type(tcx, ty)
|
||||
}
|
||||
ty::Tuple(elems) if elems.is_empty() => self.basic_type(tcx, ty),
|
||||
ty::Array(elem_ty, len) => self.array_type(
|
||||
tcx,
|
||||
type_dbg,
|
||||
*elem_ty,
|
||||
len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()),
|
||||
),
|
||||
// ty::Slice(_) | ty::Str
|
||||
// ty::Dynamic
|
||||
// ty::Foreign
|
||||
ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
|
||||
self.pointer_type(tcx, type_dbg, ty, *pointee_type)
|
||||
}
|
||||
// ty::Adt(def, args) if def.is_box() && args.get(1).map_or(true, |arg| cx.layout_of(arg.expect_ty()).is_1zst())
|
||||
// ty::FnDef(..) | ty::FnPtr(..)
|
||||
// ty::Closure(..)
|
||||
// ty::Adt(def, ..)
|
||||
// ty::Tuple(_)
|
||||
// ty::Param(_)
|
||||
// FIXME implement remaining types and add unreachable!() to the fallback branch
|
||||
_ => self.placeholder_for_type(tcx, type_dbg, ty),
|
||||
};
|
||||
|
||||
type_dbg.type_map.insert(ty, type_id);
|
||||
|
||||
type_id
|
||||
}
|
||||
|
||||
fn basic_type<'tcx>(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> UnitEntryId {
|
||||
let (name, encoding) = match ty.kind() {
|
||||
ty::Never => ("!", gimli::DW_ATE_unsigned),
|
||||
ty::Tuple(elems) if elems.is_empty() => ("()", gimli::DW_ATE_unsigned),
|
||||
ty::Bool => ("bool", gimli::DW_ATE_boolean),
|
||||
ty::Char => ("char", gimli::DW_ATE_UTF),
|
||||
ty::Int(int_ty) => (int_ty.name_str(), gimli::DW_ATE_signed),
|
||||
ty::Uint(uint_ty) => (uint_ty.name_str(), gimli::DW_ATE_unsigned),
|
||||
ty::Float(float_ty) => (float_ty.name_str(), gimli::DW_ATE_float),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_base_type);
|
||||
let type_entry = self.dwarf.unit.get_mut(type_id);
|
||||
type_entry.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
|
||||
type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(encoding));
|
||||
type_entry.set(
|
||||
gimli::DW_AT_byte_size,
|
||||
AttributeValue::Udata(
|
||||
tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).expect("FIXME").size.bytes(),
|
||||
),
|
||||
);
|
||||
|
||||
type_id
|
||||
}
|
||||
|
||||
fn array_type<'tcx>(
|
||||
&mut self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
type_dbg: &mut TypeDebugContext<'tcx>,
|
||||
elem_ty: Ty<'tcx>,
|
||||
len: u64,
|
||||
) -> UnitEntryId {
|
||||
let elem_dw_ty = self.debug_type(tcx, type_dbg, elem_ty);
|
||||
|
||||
return_if_type_created_in_meantime!(type_dbg, elem_ty);
|
||||
|
||||
let array_type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_array_type);
|
||||
let array_type_entry = self.dwarf.unit.get_mut(array_type_id);
|
||||
array_type_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(elem_dw_ty));
|
||||
|
||||
let subrange_id = self.dwarf.unit.add(array_type_id, gimli::DW_TAG_subrange_type);
|
||||
let subrange_entry = self.dwarf.unit.get_mut(subrange_id);
|
||||
subrange_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(self.array_size_type));
|
||||
subrange_entry.set(gimli::DW_AT_lower_bound, AttributeValue::Udata(0));
|
||||
subrange_entry.set(gimli::DW_AT_count, AttributeValue::Udata(len));
|
||||
|
||||
array_type_id
|
||||
}
|
||||
|
||||
fn pointer_type<'tcx>(
|
||||
&mut self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
type_dbg: &mut TypeDebugContext<'tcx>,
|
||||
ptr_type: Ty<'tcx>,
|
||||
pointee_type: Ty<'tcx>,
|
||||
) -> UnitEntryId {
|
||||
let pointee_dw_ty = self.debug_type(tcx, type_dbg, pointee_type);
|
||||
|
||||
return_if_type_created_in_meantime!(type_dbg, ptr_type);
|
||||
|
||||
let name = type_names::compute_debuginfo_type_name(tcx, ptr_type, true);
|
||||
|
||||
if !has_ptr_meta(tcx, ptr_type) {
|
||||
let pointer_type_id =
|
||||
self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_pointer_type);
|
||||
let pointer_entry = self.dwarf.unit.get_mut(pointer_type_id);
|
||||
pointer_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(pointee_dw_ty));
|
||||
pointer_entry
|
||||
.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name)));
|
||||
|
||||
pointer_type_id
|
||||
} else {
|
||||
// FIXME implement debuginfo for fat pointers
|
||||
self.placeholder_for_type(tcx, type_dbg, ptr_type)
|
||||
}
|
||||
}
|
||||
|
||||
fn placeholder_for_type<'tcx>(
|
||||
&mut self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
type_dbg: &mut TypeDebugContext<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> UnitEntryId {
|
||||
self.debug_type(
|
||||
tcx,
|
||||
type_dbg,
|
||||
Ty::new_array(
|
||||
tcx,
|
||||
tcx.types.u8,
|
||||
tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap().size.bytes(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@
|
||||
use rustc_session::Session;
|
||||
|
||||
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
|
||||
use crate::debuginfo::TypeDebugContext;
|
||||
use crate::global_asm::GlobalAsmConfig;
|
||||
use crate::{prelude::*, BackendConfig};
|
||||
|
||||
@ -460,6 +461,7 @@ fn module_codegen(
|
||||
tcx.sess.opts.debuginfo != DebugInfo::None,
|
||||
cgu_name,
|
||||
);
|
||||
let mut type_dbg = TypeDebugContext::default();
|
||||
super::predefine_mono_items(tcx, &mut module, &mono_items);
|
||||
let mut codegened_functions = vec![];
|
||||
for (mono_item, _) in mono_items {
|
||||
@ -475,7 +477,10 @@ fn module_codegen(
|
||||
codegened_functions.push(codegened_function);
|
||||
}
|
||||
MonoItem::Static(def_id) => {
|
||||
crate::constant::codegen_static(tcx, &mut module, def_id)
|
||||
let data_id = crate::constant::codegen_static(tcx, &mut module, def_id);
|
||||
if let Some(debug_context) = &mut cx.debug_context {
|
||||
debug_context.define_static(tcx, &mut type_dbg, def_id, data_id);
|
||||
}
|
||||
}
|
||||
MonoItem::GlobalAsm(item_id) => {
|
||||
crate::global_asm::codegen_global_asm_item(
|
||||
|
Loading…
Reference in New Issue
Block a user