2020-09-23 15:13:49 +02:00
|
|
|
//! Handling of everything related to debuginfo.
|
|
|
|
|
|
2019-11-12 20:52:32 +01:00
|
|
|
mod emit;
|
2019-11-12 21:08:08 +01:00
|
|
|
mod line_info;
|
2021-09-19 13:56:58 +02:00
|
|
|
mod object;
|
2020-04-25 18:23:31 +02:00
|
|
|
mod unwind;
|
2019-01-17 18:07:27 +01:00
|
|
|
|
2019-11-12 20:52:32 +01:00
|
|
|
use crate::prelude::*;
|
2019-10-16 21:21:20 +02:00
|
|
|
|
2022-08-18 15:14:04 +00:00
|
|
|
use cranelift_codegen::ir::Endianness;
|
2020-04-01 14:12:46 +02:00
|
|
|
use cranelift_codegen::isa::TargetIsa;
|
2019-11-11 21:43:57 +01:00
|
|
|
|
2022-08-18 15:14:04 +00:00
|
|
|
use gimli::write::{Address, AttributeValue, DwarfUnit, LineProgram, LineString, Range, RangeList};
|
|
|
|
|
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian};
|
2019-11-12 20:52:32 +01:00
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use emit::{DebugReloc, DebugRelocName};
|
2020-05-01 19:21:29 +02:00
|
|
|
pub(crate) use unwind::UnwindContext;
|
2019-01-17 18:07:27 +01:00
|
|
|
|
2020-03-27 12:14:45 +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-01-19 12:18:39 +01:00
|
|
|
|
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-06-16 11:13:49 +02:00
|
|
|
impl<'tcx> DebugContext<'tcx> {
|
2020-04-25 18:23:31 +02:00
|
|
|
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
|
2019-01-26 12:37:49 +01:00
|
|
|
let encoding = Encoding {
|
|
|
|
|
format: Format::Dwarf32,
|
2021-08-06 16:26:56 +02:00
|
|
|
// FIXME this should be configurable
|
2019-01-26 12:37:49 +01:00
|
|
|
// macOS doesn't seem to support DWARF > 3
|
2020-04-18 14:56:04 +03:00
|
|
|
// 5 version is required for md5 file hash
|
2020-11-08 14:27:51 +03:00
|
|
|
version: if tcx.sess.target.is_like_osx {
|
2020-04-18 16:05:20 +03:00
|
|
|
3
|
|
|
|
|
} else {
|
2020-04-21 12:13:50 +02:00
|
|
|
// FIXME change to version 5 once the gdb and lldb shipping with the latest debian
|
|
|
|
|
// support it.
|
|
|
|
|
4
|
2020-04-18 16:05:20 +03:00
|
|
|
},
|
2020-04-25 18:23:31 +02:00
|
|
|
address_size: isa.frontend_config().pointer_bytes(),
|
2019-01-26 12:37:49 +01:00
|
|
|
};
|
2019-01-17 18:07:27 +01:00
|
|
|
|
2021-12-30 14:53:41 +01:00
|
|
|
let endian = match isa.endianness() {
|
|
|
|
|
Endianness::Little => RunTimeEndian::Little,
|
|
|
|
|
Endianness::Big => RunTimeEndian::Big,
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-09 17:15:15 +10:00
|
|
|
let mut dwarf = DwarfUnit::new(encoding);
|
|
|
|
|
|
2021-07-07 11:14:20 +02:00
|
|
|
let producer = format!(
|
|
|
|
|
"cg_clif (rustc {}, cranelift {})",
|
|
|
|
|
rustc_interface::util::version_str().unwrap_or("unknown version"),
|
|
|
|
|
cranelift_codegen::VERSION,
|
|
|
|
|
);
|
2021-12-20 18:56:35 +01:00
|
|
|
let comp_dir = tcx
|
|
|
|
|
.sess
|
|
|
|
|
.opts
|
|
|
|
|
.working_dir
|
|
|
|
|
.to_string_lossy(FileNameDisplayPreference::Remapped)
|
|
|
|
|
.into_owned();
|
2020-04-18 17:43:00 +03:00
|
|
|
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
|
2020-04-18 14:56:04 +03:00
|
|
|
Some(path) => {
|
|
|
|
|
let name = path.to_string_lossy().into_owned();
|
2020-06-04 19:57:12 +02:00
|
|
|
(name, None)
|
2020-08-28 12:10:48 +02:00
|
|
|
}
|
2020-04-18 14:56:04 +03:00
|
|
|
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
|
2019-01-17 18:07:27 +01:00
|
|
|
};
|
|
|
|
|
|
2020-04-18 14:56:04 +03:00
|
|
|
let mut 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),
|
2020-04-18 17:43:00 +03:00
|
|
|
file_info,
|
2019-02-09 17:15:15 +10:00
|
|
|
);
|
2020-04-18 17:43:00 +03:00
|
|
|
line_program.file_has_md5 = file_info.is_some();
|
2020-04-18 14:56:04 +03:00
|
|
|
|
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);
|
2021-03-05 19:12:59 +01:00
|
|
|
root.set(gimli::DW_AT_producer, AttributeValue::StringRef(dwarf.strings.add(producer)));
|
|
|
|
|
root.set(gimli::DW_AT_language, AttributeValue::Language(gimli::DW_LANG_Rust));
|
2019-01-17 18:07:27 +01:00
|
|
|
root.set(gimli::DW_AT_name, AttributeValue::StringRef(name));
|
|
|
|
|
root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir));
|
2021-03-05 19:12:59 +01:00
|
|
|
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
|
2019-01-17 18:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-18 15:14:04 +00:00
|
|
|
DebugContext { tcx, endian, dwarf, unit_range_list: RangeList(Vec::new()) }
|
2020-06-13 17:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn define_function(
|
|
|
|
|
&mut self,
|
2019-10-16 21:21:20 +02:00
|
|
|
func_id: FuncId,
|
2019-01-17 18:07:27 +01:00
|
|
|
name: &str,
|
2020-06-13 17:03:34 +02:00
|
|
|
context: &Context,
|
2022-08-18 15:19:29 +00:00
|
|
|
function_span: Span,
|
2020-06-13 17:03:34 +02:00
|
|
|
source_info_set: &indexmap::IndexSet<SourceInfo>,
|
|
|
|
|
) {
|
|
|
|
|
let symbol = func_id.as_u32() as usize;
|
2019-11-11 20:49:20 +01:00
|
|
|
|
2020-06-13 17:03:34 +02:00
|
|
|
// FIXME: add to appropriate scope instead of root
|
|
|
|
|
let scope = self.dwarf.unit.root();
|
2019-01-17 18:07:27 +01:00
|
|
|
|
2020-08-28 12:10:48 +02:00
|
|
|
let entry_id = self.dwarf.unit.add(scope, gimli::DW_TAG_subprogram);
|
2020-06-13 17:03:34 +02:00
|
|
|
let entry = self.dwarf.unit.get_mut(entry_id);
|
|
|
|
|
let name_id = self.dwarf.strings.add(name);
|
2020-04-23 16:44:12 +02:00
|
|
|
// Gdb requires DW_AT_name. Otherwise the DW_TAG_subprogram is skipped.
|
2020-08-28 12:10:48 +02:00
|
|
|
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
|
2021-03-05 19:12:59 +01:00
|
|
|
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id));
|
2019-01-17 18:07:27 +01:00
|
|
|
|
2022-08-18 15:19:29 +00:00
|
|
|
let end =
|
|
|
|
|
self.create_debug_lines(symbol, entry_id, context, function_span, source_info_set);
|
2019-11-12 21:10:51 +01:00
|
|
|
|
2020-08-28 12:10:48 +02:00
|
|
|
self.unit_range_list.0.push(Range::StartLength {
|
|
|
|
|
begin: Address::Symbol { symbol, addend: 0 },
|
|
|
|
|
length: u64::from(end),
|
|
|
|
|
});
|
2019-01-19 16:16:30 +01:00
|
|
|
|
2020-06-13 17:03:34 +02:00
|
|
|
let func_entry = self.dwarf.unit.get_mut(entry_id);
|
2020-04-23 16:44:12 +02:00
|
|
|
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
|
2020-08-28 12:10:48 +02:00
|
|
|
func_entry.set(
|
|
|
|
|
gimli::DW_AT_low_pc,
|
|
|
|
|
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
|
|
|
|
|
);
|
2020-04-23 16:44:12 +02:00
|
|
|
// Using Udata for DW_AT_high_pc requires at least DWARF4
|
|
|
|
|
func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
|
2019-11-11 21:43:57 +01:00
|
|
|
}
|
|
|
|
|
}
|