1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::{error::TraceError, type_value_tree::TypeValueTree, Frame, FrameType, Location};
use addr2line::object::{Object, ObjectSection, SectionKind};
use funty::Fundamental;
use gimli::{DebugInfoOffset, EndianRcSlice, RunTimeEndian};
use stackdump_core::{device_memory::DeviceMemory, memory_region::VecMemoryRegion};
use std::collections::HashMap;
pub mod cortex_m;
pub enum UnwindResult<ADDR: funty::Integral> {
Finished,
Corrupted {
error_frame: Option<Frame<ADDR>>,
},
Proceeded,
}
pub trait Platform<'data> {
type Word: funty::Integral;
fn create_context(
elf: &addr2line::object::File<'data, &'data [u8]>,
) -> Result<Self, TraceError>
where
Self: Sized;
fn unwind(
&mut self,
device_memory: &mut DeviceMemory<Self::Word>,
previous_frame: Option<&mut Frame<Self::Word>>,
) -> Result<UnwindResult<Self::Word>, TraceError>;
}
pub fn trace<'data, P: Platform<'data>>(
mut device_memory: DeviceMemory<P::Word>,
elf_data: &'data [u8],
) -> Result<Vec<Frame<P::Word>>, TraceError>
where
<P::Word as funty::Numeric>::Bytes: bitvec::view::BitView<Store = u8>,
{
let elf = addr2line::object::File::parse(elf_data)?;
for section in elf.sections().filter(|section| {
matches!(
section.kind(),
SectionKind::Text | SectionKind::ReadOnlyData | SectionKind::ReadOnlyString
)
}) {
device_memory.add_memory_region(VecMemoryRegion::new(
section.address(),
section.uncompressed_data()?.to_vec(),
));
}
let mut frames = Vec::new();
let addr2line_context = addr2line::Context::new(&elf)?;
let mut platform_context = P::create_context(&elf)?;
let mut type_cache = Default::default();
loop {
match add_current_frames::<P>(
&mut device_memory,
&addr2line_context,
&mut frames,
&mut type_cache,
) {
Ok(_) => {}
Err(e @ TraceError::DwarfUnitNotFound { pc: _ }) => {
frames.push(Frame {
function: "Unknown".into(),
location: Location::default(),
frame_type: FrameType::Corrupted(e.to_string()),
variables: Vec::default(),
});
break;
}
Err(e) => return Err(e),
}
match platform_context.unwind(&mut device_memory, frames.last_mut())? {
UnwindResult::Finished => {
frames.push(Frame {
function: "RESET".into(),
location: crate::Location {
file: None,
line: None,
column: None,
},
frame_type: FrameType::Function,
variables: Vec::new(),
});
break;
}
UnwindResult::Corrupted {
error_frame: Some(error_frame),
} => {
frames.push(error_frame);
break;
}
UnwindResult::Corrupted { error_frame: None } => {
break;
}
UnwindResult::Proceeded => {
continue;
}
}
}
let static_variables = crate::variables::find_static_variables(
addr2line_context.dwarf(),
&device_memory,
&mut type_cache,
)?;
let static_frame = Frame {
function: "Static".into(),
location: Location {
file: None,
line: None,
column: None,
},
frame_type: FrameType::Static,
variables: static_variables,
};
frames.push(static_frame);
Ok(frames)
}
fn add_current_frames<'a, P: Platform<'a>>(
device_memory: &DeviceMemory<P::Word>,
addr2line_context: &addr2line::Context<EndianRcSlice<RunTimeEndian>>,
frames: &mut Vec<Frame<P::Word>>,
type_cache: &mut HashMap<DebugInfoOffset, Result<TypeValueTree<P::Word>, TraceError>>,
) -> Result<(), TraceError>
where
<P::Word as funty::Numeric>::Bytes: bitvec::view::BitView<Store = u8>,
{
let mut context_frames =
addr2line_context.find_frames(device_memory.register(gimli::Arm::PC)?.as_u64())?;
let unit = addr2line_context
.find_dwarf_unit(device_memory.register(gimli::Arm::PC)?.as_u64())
.ok_or(TraceError::DwarfUnitNotFound {
pc: device_memory.register(gimli::Arm::PC)?.as_u64(),
})?;
let abbreviations = addr2line_context.dwarf().abbreviations(&unit.header)?;
let mut added_frames = 0;
while let Some(context_frame) = context_frames.next()? {
let (file, line, column) = context_frame
.location
.map(|l| {
(
l.file.map(|f| f.to_string()),
l.line.map(|line| line as _),
l.column.map(|column| column as _),
)
})
.unwrap_or_default();
let mut variables = Vec::new();
if let Some(die_offset) = context_frame.dw_die_offset {
let mut entries = match unit.header.entries_tree(&abbreviations, Some(die_offset)) {
Ok(entries) => entries,
Err(_) => {
continue;
}
};
if let Ok(entry_root) = entries.root() {
variables = crate::variables::find_variables_in_function(
addr2line_context.dwarf(),
unit,
&abbreviations,
device_memory,
entry_root,
type_cache,
)?;
}
}
frames.push(Frame {
function: context_frame
.function
.and_then(|f| f.demangle().ok().map(|f| f.into_owned()))
.unwrap_or_else(|| "UNKNOWN".into()),
location: crate::Location { file, line, column },
frame_type: FrameType::InlineFunction,
variables,
});
added_frames += 1;
}
if added_frames > 0 {
frames.last_mut().unwrap().frame_type = FrameType::Function;
}
Ok(())
}