Skip to content

Commit

Permalink
Ignore debug_info/debug_line for dead code.
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Feb 28, 2019
1 parent eb956b1 commit ee89540
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
29 changes: 17 additions & 12 deletions lib/debug/src/address_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,44 +72,49 @@ impl AddressTransform {
}
}

pub fn translate(&self, addr: u64) -> write::Address {
pub fn translate(&self, addr: u64) -> Option<write::Address> {
if addr == 0 {
return write::Address::Absolute(0);
// It's normally 0 for debug info without the linked code.
return None;
}
let search = self.lookup.range((Unbounded, Included(addr)));
if let Some((_, value)) = search.last() {
return write::Address::Relative {
return Some(write::Address::Relative {
symbol: value.0,
addend: value.1 as i64,
};
});
}
panic!("address was not found {}", addr);
// Address was not found: function was not compiled?
None
}

pub fn diff(&self, addr1: u64, addr2: u64) -> u64 {
pub fn diff(&self, addr1: u64, addr2: u64) -> Option<u64> {
let t1 = self.translate(addr1);
let t2 = self.translate(addr2);
if t1.is_none() || t2.is_none() {
return None;
}
if let (
write::Address::Relative {
Some(write::Address::Relative {
symbol: s1,
addend: a,
},
write::Address::Relative {
}),
Some(write::Address::Relative {
symbol: s2,
addend: b,
},
}),
) = (t1, t2)
{
if s1 != s2 {
panic!("different symbol");
}
(b - a) as u64
Some((b - a) as u64)
} else {
unreachable!();
}
}

pub fn delta(&self, addr1: u64, u: u64) -> u64 {
pub fn delta(&self, addr1: u64, u: u64) -> Option<u64> {
self.diff(addr1, addr1 + u)
}

Expand Down
60 changes: 51 additions & 9 deletions lib/debug/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
write::AttributeValue::Udata(subprogram_range.unwrap().1)
}
AttributeValue::Addr(u) => {
let addr = addr_tr.translate(u);
let addr = addr_tr.translate(u).unwrap_or(write::Address::Absolute(0));
if attr.name() == gimli::DW_AT_low_pc {
low_pc = Some((u, addr));
}
Expand All @@ -94,7 +94,7 @@ where
if attr.name() != gimli::DW_AT_high_pc || low_pc.is_none() {
write::AttributeValue::Udata(u)
} else {
let u = addr_tr.delta(low_pc.unwrap().0, u);
let u = addr_tr.delta(low_pc.unwrap().0, u).unwrap_or(0);
write::AttributeValue::Udata(u)
}
}
Expand Down Expand Up @@ -235,6 +235,13 @@ enum SavedLineProgramRow {
EndOfSequence(u64),
}

#[derive(Debug, Eq, PartialEq)]
enum ReadLineProgramState {
SequenceEnded,
ReadSequence,
IgnoreSequence,
}

fn clone_line_program<R>(
unit: &CompilationUnitHeader<R, R::Offset>,
root: &DebuggingInformationEntry<R>,
Expand Down Expand Up @@ -320,10 +327,26 @@ where

let mut rows = program.rows();
let mut saved_rows = BTreeMap::new();
let mut state = ReadLineProgramState::SequenceEnded;
while let Some((_header, row)) = rows.next_row()? {
if state == ReadLineProgramState::IgnoreSequence {
if row.end_sequence() {
state = ReadLineProgramState::SequenceEnded;
}
continue;
}
let saved_row = if row.end_sequence() {
state = ReadLineProgramState::SequenceEnded;
SavedLineProgramRow::EndOfSequence(row.address())
} else {
if state == ReadLineProgramState::SequenceEnded {
// Discard sequences for non-existent code.
if row.address() == 0 {
state = ReadLineProgramState::IgnoreSequence;
continue;
}
state = ReadLineProgramState::ReadSequence;
}
SavedLineProgramRow::Normal {
address: row.address(),
op_index: row.op_index(),
Expand Down Expand Up @@ -412,7 +435,7 @@ where
let low_pc = entry.attr_value(gimli::DW_AT_low_pc)?;
if let Some(AttributeValue::Addr(addr)) = low_pc {
let transformed = addr_tr.translate(addr);
if let write::Address::Relative { symbol, .. } = transformed {
if let Some(write::Address::Relative { symbol, .. }) = transformed {
let range = addr_tr.func_range(symbol);
let addr = write::Address::Relative {
symbol,
Expand Down Expand Up @@ -485,7 +508,31 @@ where
} else {
return Ok(()); // empty
};
let mut skip_at_depth = None;
while let Some((depth_delta, entry)) = entries.next_dfs()? {
let depth_delta = if let Some(depth) = skip_at_depth {
let new_depth = depth + depth_delta;
if new_depth >= 0 {
skip_at_depth = Some(new_depth);
continue;
}
skip_at_depth = None;
new_depth
} else {
depth_delta
};
let range = if entry.tag() == gimli::DW_TAG_subprogram {
let range = get_subprogram_range(entry, addr_tr)?;
if range.is_none() {
// Subprogram was not compiled: discarding all its info.
skip_at_depth = Some(0);
continue;
}
range
} else {
None
};

if depth_delta <= 0 {
for _ in depth_delta..1 {
stack.pop();
Expand All @@ -495,14 +542,9 @@ where
}
let parent = stack.last().unwrap();
let die_id = comp_unit.add(*parent, entry.tag());
stack.push(die_id);
let current_scope = comp_unit.get_mut(die_id);
let range = if entry.tag() == gimli::DW_TAG_subprogram {
get_subprogram_range(entry, addr_tr)?
} else {
None
};

stack.push(die_id);
die_ref_map.insert(entry.offset(), die_id);

clone_die_attributes(
Expand Down

0 comments on commit ee89540

Please sign in to comment.