Skip to content

Commit

Permalink
Simplify Gsym inline stack fixup work
Browse files Browse the repository at this point in the history
Simplify the Gsym inline stack fixup work we have to do by merging the
two loops.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o committed Sep 22, 2023
1 parent aefca36 commit d16ca3a
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions src/gsym/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,52 +239,52 @@ impl SymResolver for GsymResolver<'_> {
let mut inlined = Vec::new();

if let Some(inline_info) = inline_info {
let inline_stack = inline_info.inline_stack(addr as u64);
let () = inlined.reserve(inline_stack.len());

for frame in inline_stack {
let name = self
.ctx
.get_str(frame.name as usize)
.and_then(|s| s.to_str())
.ok_or_invalid_data(|| {
format!("failed to read string table entry at offset {}", frame.name)
})?;

let code_info = if let Some(file) = frame.call_file {
let code_info = self.query_frame_code_info(file, frame.call_line)?;
Some(code_info)
} else {
None
};
let () = inlined.push((name, code_info));
}

if !inlined.is_empty() {
let (name, _code_info) = inlined.remove(0);
let mut prev = Option::<usize>::None;

for i in 0..inlined.len() {
if let Some(prev_i) = prev {
let inlined = inlined.as_mut_ptr();
// TODO: Use `slice::get_many_mut` once it is stable.
// SAFETY: `i` and `prev_i` are different so
// we are creating exclusive
// references to disjunct region of
// memory. It is an invariant that
// both are within bounds of the
// `symbols` slice.
let first = unsafe { &mut *inlined.add(i) };
let second = unsafe { &mut *inlined.add(prev_i) };
let () = swap(&mut first.1, &mut second.1);
let mut inline_stack = inline_info.inline_stack(addr as u64).into_iter();
// As per Gsym file format, the first "frame" only contains the
// name and it effectively is meant to overwrite what is already
// contained in the line table.
if let Some(inline_info) = inline_stack.next() {
direct_name = Some(
self.ctx
.get_str(inline_info.name as usize)
.and_then(|s| s.to_str())
.ok_or_invalid_data(|| {
format!(
"failed to read string table entry at offset {}",
inline_info.name
)
})?,
);

let () = inlined.reserve(inline_stack.len());

for frame in inline_stack {
let name = self
.ctx
.get_str(frame.name as usize)
.and_then(|s| s.to_str())
.ok_or_invalid_data(|| {
format!(
"failed to read string table entry at offset {}",
frame.name
)
})?;

let mut code_info = if let Some(file) = frame.call_file {
let code_info = self.query_frame_code_info(file, frame.call_line)?;
Some(code_info)
} else {
direct_name = Some(name);
if let Some(code_info) = &mut inlined[i].1 {
let () = swap(code_info, &mut line_tab_info);
}
None
};

// For each frame we need to move the code information
// up by one layer.
if let Some((_last_name, ref mut last_code_info)) = inlined.last_mut() {
let () = swap(&mut code_info, last_code_info);
} else if let Some(code_info) = &mut code_info {
let () = swap(code_info, &mut line_tab_info);
}

prev = Some(i);
let () = inlined.push((name, code_info));
}
}
}
Expand Down

0 comments on commit d16ca3a

Please sign in to comment.