1 use crate::debug::Reader;
2 
3 use super::address_transform::AddressTransform;
4 use super::expression::{CompiledExpression, FunctionFrameInfo};
5 use anyhow::Error;
6 use cranelift_codegen::isa::TargetIsa;
7 use gimli::{write, AttributeValue, DebuggingInformationEntry, Unit};
8 
9 pub(crate) fn append_vmctx_info(
10     comp_unit: &mut write::Unit,
11     parent_id: write::UnitEntryId,
12     vmctx_ptr_die_ref: write::Reference,
13     addr_tr: &AddressTransform,
14     frame_info: Option<&FunctionFrameInfo>,
15     scope_ranges: &[(u64, u64)],
16     out_strings: &mut write::StringTable,
17     isa: &dyn TargetIsa,
18 ) -> Result<(), Error> {
19     let loc = {
20         let expr = CompiledExpression::vmctx();
21         let locs = expr
22             .build_with_locals(scope_ranges, addr_tr, frame_info, isa)
23             .map(|i| {
24                 i.map(|(begin, length, data)| write::Location::StartLength {
25                     begin,
26                     length,
27                     data,
28                 })
29             })
30             .collect::<Result<Vec<_>, _>>()?;
31         let list_id = comp_unit.locations.add(write::LocationList(locs));
32         write::AttributeValue::LocationListRef(list_id)
33     };
34 
35     let var_die_id = comp_unit.add(parent_id, gimli::DW_TAG_variable);
36     let var_die = comp_unit.get_mut(var_die_id);
37     var_die.set(
38         gimli::DW_AT_name,
39         write::AttributeValue::StringRef(out_strings.add("__vmctx")),
40     );
41     var_die.set(
42         gimli::DW_AT_type,
43         write::AttributeValue::DebugInfoRef(vmctx_ptr_die_ref),
44     );
45     var_die.set(gimli::DW_AT_location, loc);
46 
47     Ok(())
48 }
49 
50 pub fn resolve_die_ref<'a>(
51     unit: &'a Unit<Reader<'a>>,
52     die_ref: &'a AttributeValue<Reader<'a>>,
53 ) -> Result<Option<DebuggingInformationEntry<'a, 'a, Reader<'a>>>, Error> {
54     let die = match die_ref {
55         AttributeValue::UnitRef(unit_offs) => Some(unit.entry(*unit_offs)?),
56         // TODO-DebugInfo: support AttributeValue::DebugInfoRef. The trouble is that we don't have
57         // a fast way to go from a DI offset to a unit offset (which is needed to parse the DIE).
58         // We would likely need to maintain a cache.
59         _ => None,
60     };
61     Ok(die)
62 }
63