| 1 |
// Copyright 2015 Ted Mielczarek. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
use crate::{FrameSymbolizer, FrameWalker, Module, SymbolError}; |
| 4 |
|
| 5 |
pub use crate::sym_file::types::*; |
| 6 |
pub use parser::SymbolParser; |
| 7 |
use std::fs::File; |
| 8 |
use std::io::Read; |
| 9 |
use std::ops::Deref; |
| 10 |
use std::path::Path; |
| 11 |
use tracing::trace; |
| 12 |
|
| 13 |
mod parser; |
| 14 |
mod types; |
| 15 |
pub mod walker; |
| 16 |
|
| 17 |
// # Sync VS Async |
| 18 |
// |
| 19 |
// There is both a sync and an async entry-point to the parser. |
| 20 |
// The two impls should be essentially identical, except for how they |
| 21 |
// read bytes from the input reader into our circular buffer. |
| 22 |
// |
| 23 |
// |
| 24 |
// # Streaming |
| 25 |
// |
| 26 |
// This parser streams the input to avoid the need to materialize all of |
| 27 |
// it into memory at once (symbol files can be a gigabyte!). As a result, |
| 28 |
// we need to iteratively parse. |
| 29 |
// |
| 30 |
// We do this by repeatedly filling up a buffer with input and asking the |
| 31 |
// parser to parse it. The parser will return how much of the input it |
| 32 |
// consumed, which we can use to clear space in our buffer and to tell |
| 33 |
// if it successfully consumed the whole input when the Reader runs dry. |
| 34 |
// |
| 35 |
// |
| 36 |
// # Handling EOF / Capacity |
| 37 |
// |
| 38 |
// Having a fix-sized buffer has one fatal issue: if one atomic step |
| 39 |
// of the parser needs more than this amount of data, then we won't |
| 40 |
// be able to parse it. |
| 41 |
// |
| 42 |
// This can result in `buf` filling up and `buf.space()` becoming an |
| 43 |
// empty slice. This in turn will make the reader yield 0 bytes, and |
| 44 |
// we'll treat it like EOF and fail the parse. When this happens, we |
| 45 |
// try to double the buffer's size and request more bytes. If we get |
| 46 |
// more, hooray! If we don't, then it's a "real" EOF. |
| 47 |
// |
| 48 |
// The "atom" of our parser is a line, so we need our buffer to be able |
| 49 |
// to fit any line. However we actually only have roughly |
| 50 |
// *half* this value as our limit, as circular::Buffer will only |
| 51 |
// `shift` the buffer's contents if over half of its capacity has been |
| 52 |
// drained by `consume` -- and `space()` only grows when a `shift` happens. |
| 53 |
// |
| 54 |
// I have in fact seen 8kb function names from Rust (thanks generic combinators!) |
| 55 |
// and 82kb function names from C++ (thanks 'auto' returns!), so we |
| 56 |
// need a buffer size that can grow to at least 200KB. This is a *very* large |
| 57 |
// amount to backshift repeatedly, so to keep this under control, we start |
| 58 |
// with only a 10KB buffer, which is generous but tolerable. |
| 59 |
// |
| 60 |
// We should still have *SOME* limit on this to avoid nasty death spirals, |
| 61 |
// so let's go with 2MB (MAX_BUFFER_CAPACITY), letting you have a horrifying 1MB symbol. |
| 62 |
// |
| 63 |
// But just *dying* when we hit this point is terrible, so lets have an |
| 64 |
// extra layer of robustness: if we ever hit the limit, enter "panic recovery" |
| 65 |
// and just start discarding bytes until we hit a newline. Then resume normal |
| 66 |
// parsing. The net effect of this is that we just treat this one line as |
| 67 |
// corrupt (because statistically it won't even be needed!). |
| 68 |
|
| 69 |
// Allows for at least 80KB symbol names, at most 160KB symbol names (fuzzy because of circular). |
| 70 |
static MAX_BUFFER_CAPACITY: usize = 1024 * 160; |
| 71 |
static INITIAL_BUFFER_CAPACITY: usize = 1024 * 10; |
| 72 |
|
| 73 |
impl SymbolFile { |
| 74 |
/// Parse a SymbolFile from the given Reader. |
| 75 |
/// |
| 76 |
/// Every time a chunk of the input is parsed, that chunk will |
| 77 |
/// be passed to `callback` to allow you to do something else |
| 78 |
/// with the data as it's streamed in (e.g. you can save the |
| 79 |
/// input to a cache). |
| 80 |
/// |
| 81 |
/// The reader is wrapped in a buffer reader so you shouldn't |
| 82 |
/// buffer the input yourself. |
| 83 |
pub fn parse<R: Read>( |
| 84 |
mut input_reader: R, |
| 85 |
mut callback: impl FnMut(&[u8]), |
| 86 |
) -> Result<SymbolFile, SymbolError> { |
| 87 |
let mut buf = circular::Buffer::with_capacity(INITIAL_BUFFER_CAPACITY); |
| 88 |
let mut parser = SymbolParser::new(); |
| 89 |
let mut fully_consumed = false; |
| 90 |
let mut tried_to_grow = false; |
| 91 |
let mut in_panic_recovery = false; |
| 92 |
let mut just_finished_recovering = false; |
| 93 |
let mut total_consumed = 0u64; |
| 94 |
loop { |
| 95 |
if in_panic_recovery { |
| 96 |
// PANIC RECOVERY MODE! DISCARD BYTES UNTIL NEWLINE. |
| 97 |
let input = buf.data(); |
| 98 |
if let Some(new_line_idx) = input.iter().position(|&byte| byte == b'\n') { |
| 99 |
// Hooray, we found a new line! Consume up to and including that, and resume. |
| 100 |
let amount = new_line_idx + 1; |
| 101 |
callback(&input[..amount]); |
| 102 |
buf.consume(amount); |
| 103 |
total_consumed += amount as u64; |
| 104 |
|
| 105 |
// Back to normal! |
| 106 |
in_panic_recovery = false; |
| 107 |
fully_consumed = false; |
| 108 |
just_finished_recovering = true; |
| 109 |
parser.lines += 1; |
| 110 |
trace!("RECOVERY: complete!"); |
| 111 |
} else { |
| 112 |
// No newline, discard everything |
| 113 |
let amount = input.len(); |
| 114 |
callback(&input[..amount]); |
| 115 |
buf.consume(amount); |
| 116 |
total_consumed += amount as u64; |
| 117 |
|
| 118 |
// If the next read returns 0 bytes, then that's a proper EOF! |
| 119 |
fully_consumed = true; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Read the data in, and tell the circular buffer about the new data |
| 124 |
let size = input_reader.read(buf.space())?; |
| 125 |
buf.fill(size); |
| 126 |
|
| 127 |
if size == 0 { |
| 128 |
// If the reader returned no more bytes, this can be either mean |
| 129 |
// EOF or the buffer is out of capacity. There are a lot of cases |
| 130 |
// to consider, so let's go through them one at a time... |
| 131 |
if just_finished_recovering && !buf.data().is_empty() { |
| 132 |
// We just finished PANIC RECOVERY, but there's still bytes in |
| 133 |
// the buffer. Assume that is parseable and resume normal parsing |
| 134 |
// (do nothing, fallthrough to normal path). |
| 135 |
} else if fully_consumed { |
| 136 |
// Success! The last iteration cleared the buffer and we still got |
| 137 |
// no more bytes, so that's a proper EOF with a complete parse! |
| 138 |
return Ok(parser.finish()); |
| 139 |
} else if !tried_to_grow { |
| 140 |
// We still have some stuff in the buffer, assume this is because |
| 141 |
// the buffer is full, and try to make it BIGGER and ask for more again. |
| 142 |
let new_cap = buf.capacity().saturating_mul(2); |
| 143 |
if new_cap > MAX_BUFFER_CAPACITY { |
| 144 |
// TIME TO PANIC!!! This line is catastrophically big, just start |
| 145 |
// discarding bytes until we hit a newline. |
| 146 |
trace!("RECOVERY: discarding enormous line {}", parser.lines); |
| 147 |
in_panic_recovery = true; |
| 148 |
continue; |
| 149 |
} |
| 150 |
trace!("parser out of space? trying more ({}KB)", new_cap / 1024); |
| 151 |
buf.grow(new_cap); |
| 152 |
tried_to_grow = true; |
| 153 |
continue; |
| 154 |
} else if total_consumed == 0 { |
| 155 |
// We grew the buffer and still got no more bytes, so it's a proper EOF. |
| 156 |
// But actually, we never consumed any bytes, so this is an empty file? |
| 157 |
// Give a better error message for that. |
| 158 |
return Err(SymbolError::ParseError( |
| 159 |
"empty SymbolFile (probably something wrong with your debuginfo tooling?)", |
| 160 |
0, |
| 161 |
)); |
| 162 |
} else { |
| 163 |
// Ok give up, this input is just impossible. |
| 164 |
return Err(SymbolError::ParseError( |
| 165 |
"unexpected EOF during parsing of SymbolFile (or a line was too long?)", |
| 166 |
parser.lines, |
| 167 |
)); |
| 168 |
} |
| 169 |
} else { |
| 170 |
tried_to_grow = false; |
| 171 |
} |
| 172 |
|
| 173 |
if in_panic_recovery { |
| 174 |
// Don't run the normal parser while we're still recovering! |
| 175 |
continue; |
| 176 |
} |
| 177 |
just_finished_recovering = false; |
| 178 |
|
| 179 |
// Ask the parser to parse more of the input |
| 180 |
let input = buf.data(); |
| 181 |
let consumed = parser.parse_more(input)?; |
| 182 |
total_consumed += consumed as u64; |
| 183 |
|
| 184 |
// Give the other consumer of this Reader a chance to use this data. |
| 185 |
callback(&input[..consumed]); |
| 186 |
|
| 187 |
// Remember for the next iteration if all the input was consumed. |
| 188 |
fully_consumed = input.len() == consumed; |
| 189 |
buf.consume(consumed); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/// `parse` but async |
| 194 |
#[cfg(feature = "http")] |
| 195 |
pub async fn parse_async( |
| 196 |
mut response: reqwest::Response, |
| 197 |
mut callback: impl FnMut(&[u8]), |
| 198 |
) -> Result<SymbolFile, SymbolError> { |
| 199 |
let mut chunk; |
| 200 |
let mut slice = &[][..]; |
| 201 |
let mut input_reader = &mut slice; |
| 202 |
let mut buf = circular::Buffer::with_capacity(INITIAL_BUFFER_CAPACITY); |
| 203 |
let mut parser = SymbolParser::new(); |
| 204 |
|
| 205 |
let mut fully_consumed = false; |
| 206 |
let mut tried_to_grow = false; |
| 207 |
let mut in_panic_recovery = false; |
| 208 |
let mut just_finished_recovering = false; |
| 209 |
let mut total_consumed = 0u64; |
| 210 |
loop { |
| 211 |
if in_panic_recovery { |
| 212 |
// PANIC RECOVERY MODE! DISCARD BYTES UNTIL NEWLINE. |
| 213 |
let input = buf.data(); |
| 214 |
if let Some(new_line_idx) = input.iter().position(|&byte| byte == b'\n') { |
| 215 |
// Hooray, we found a new line! Consume up to and including that, and resume. |
| 216 |
let amount = new_line_idx + 1; |
| 217 |
callback(&input[..amount]); |
| 218 |
buf.consume(amount); |
| 219 |
total_consumed += amount as u64; |
| 220 |
|
| 221 |
// Back to normal! |
| 222 |
in_panic_recovery = false; |
| 223 |
fully_consumed = false; |
| 224 |
just_finished_recovering = true; |
| 225 |
parser.lines += 1; |
| 226 |
trace!("PANIC RECOVERY: complete!"); |
| 227 |
} else { |
| 228 |
// No newline, discard everything |
| 229 |
let amount = input.len(); |
| 230 |
callback(&input[..amount]); |
| 231 |
buf.consume(amount); |
| 232 |
total_consumed += amount as u64; |
| 233 |
|
| 234 |
// If the next read returns 0 bytes, then that's a proper EOF! |
| 235 |
fully_consumed = true; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Little rube-goldberg machine to stream the contents: |
| 240 |
// * get a chunk (Bytes) from the Response |
| 241 |
// * get its underlying slice |
| 242 |
// * then get a mutable reference to that slice |
| 243 |
// * then Read that mutable reference in our circular buffer |
| 244 |
// * when the slice runs out, get the next chunk and repeat |
| 245 |
if input_reader.is_empty() { |
| 246 |
chunk = response |
| 247 |
.chunk() |
| 248 |
.await |
| 249 |
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))? |
| 250 |
.unwrap_or_default(); |
| 251 |
slice = &chunk[..]; |
| 252 |
input_reader = &mut slice; |
| 253 |
} |
| 254 |
|
| 255 |
// Read the data in, and tell the circular buffer about the new data |
| 256 |
let size = input_reader.read(buf.space())?; |
| 257 |
buf.fill(size); |
| 258 |
|
| 259 |
if size == 0 { |
| 260 |
// If the reader returned no more bytes, this can be either mean |
| 261 |
// EOF or the buffer is out of capacity. There are a lot of cases |
| 262 |
// to consider, so let's go through them one at a time... |
| 263 |
if just_finished_recovering && !buf.data().is_empty() { |
| 264 |
// We just finished PANIC RECOVERY, but there's still bytes in |
| 265 |
// the buffer. Assume that is parseable and resume normal parsing |
| 266 |
// (do nothing, fallthrough to normal path). |
| 267 |
} else if fully_consumed { |
| 268 |
// Success! The last iteration cleared the buffer and we still got |
| 269 |
// no more bytes, so that's a proper EOF with a complete parse! |
| 270 |
return Ok(parser.finish()); |
| 271 |
} else if !tried_to_grow { |
| 272 |
// We still have some stuff in the buffer, assume this is because |
| 273 |
// the buffer is full, and try to make it BIGGER and ask for more again. |
| 274 |
let new_cap = buf.capacity().saturating_mul(2); |
| 275 |
if new_cap > MAX_BUFFER_CAPACITY { |
| 276 |
// TIME TO PANIC!!! This line is catastrophically big, just start |
| 277 |
// discarding bytes until we hit a newline. |
| 278 |
trace!("RECOVERY: discarding enormous line {}", parser.lines); |
| 279 |
in_panic_recovery = true; |
| 280 |
continue; |
| 281 |
} |
| 282 |
trace!("parser out of space? trying more ({}KB)", new_cap / 1024); |
| 283 |
buf.grow(new_cap); |
| 284 |
tried_to_grow = true; |
| 285 |
continue; |
| 286 |
} else if total_consumed == 0 { |
| 287 |
// We grew the buffer and still got no more bytes, so it's a proper EOF. |
| 288 |
// But actually, we never consumed any bytes, so this is an empty file? |
| 289 |
// Give a better error message for that. |
| 290 |
return Err(SymbolError::ParseError( |
| 291 |
"empty SymbolFile (probably something wrong with your debuginfo tooling?)", |
| 292 |
0, |
| 293 |
)); |
| 294 |
} else { |
| 295 |
// Ok give up, this input is just impossible. |
| 296 |
return Err(SymbolError::ParseError( |
| 297 |
"unexpected EOF during parsing of SymbolFile (or a line was too long?)", |
| 298 |
parser.lines, |
| 299 |
)); |
| 300 |
} |
| 301 |
} else { |
| 302 |
tried_to_grow = false; |
| 303 |
} |
| 304 |
|
| 305 |
if in_panic_recovery { |
| 306 |
// Don't run the normal parser while we're still recovering! |
| 307 |
continue; |
| 308 |
} |
| 309 |
just_finished_recovering = false; |
| 310 |
|
| 311 |
// Ask the parser to parse more of the input |
| 312 |
let input = buf.data(); |
| 313 |
let consumed = parser.parse_more(input)?; |
| 314 |
total_consumed += consumed as u64; |
| 315 |
|
| 316 |
// Give the other consumer of this Reader a chance to use this data. |
| 317 |
callback(&input[..consumed]); |
| 318 |
|
| 319 |
// Remember for the next iteration if all the input was consumed. |
| 320 |
fully_consumed = input.len() == consumed; |
| 321 |
buf.consume(consumed); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
// Parse a SymbolFile from bytes. |
| 326 |
pub fn from_bytes(bytes: &[u8]) -> Result<SymbolFile, SymbolError> { |
| 327 |
Self::parse(bytes, |_| ()) |
| 328 |
} |
| 329 |
|
| 330 |
// Parse a SymbolFile from a file. |
| 331 |
pub fn from_file(path: &Path) -> Result<SymbolFile, SymbolError> { |
| 332 |
let file = File::open(path)?; |
| 333 |
Self::parse(file, |_| ()) |
| 334 |
} |
| 335 |
|
| 336 |
/// Fill in as much source information for `frame` as possible. |
| 337 |
pub fn fill_symbol(&self, module: &dyn Module, frame: &mut dyn FrameSymbolizer) { |
| 338 |
// Look for a FUNC covering the address first. |
| 339 |
if frame.get_instruction() < module.base_address() { |
| 340 |
return; |
| 341 |
} |
| 342 |
let addr = frame.get_instruction() - module.base_address(); |
| 343 |
if let Some(func) = self.functions.get(addr) { |
| 344 |
// TODO: although FUNC records have a parameter size, it appears that |
| 345 |
// they aren't to be trusted? The STACK WIN records are more reliable |
| 346 |
// when available. This is important precisely because these values |
| 347 |
// are used to unwind subsequent STACK WIN frames (because certain |
| 348 |
// calling conventions have the caller push the callee's arguments, |
| 349 |
// which affects the the stack's size!). |
| 350 |
// |
| 351 |
// Need to spend more time thinking about if this is the right approach |
| 352 |
let parameter_size = if let Some(info) = self.win_stack_framedata_info.get(addr) { |
| 353 |
info.parameter_size |
| 354 |
} else if let Some(info) = self.win_stack_fpo_info.get(addr) { |
| 355 |
info.parameter_size |
| 356 |
} else { |
| 357 |
func.parameter_size |
| 358 |
}; |
| 359 |
|
| 360 |
frame.set_function( |
| 361 |
&func.name, |
| 362 |
func.address + module.base_address(), |
| 363 |
parameter_size, |
| 364 |
); |
| 365 |
|
| 366 |
// See if there's source line and inline info as well. |
| 367 |
// |
| 368 |
// In the following, we transform data between two different representations of inline calls. |
| 369 |
// The input shape has function names associated with the location of the call to that function. |
| 370 |
// The output shape has function names associated with a location *inside* that function. |
| 371 |
// |
| 372 |
// Input: |
| 373 |
// |
| 374 |
// ( |
| 375 |
// outer_name, |
| 376 |
// inline_calls: [ // Each location is the line of the *call* to the function |
| 377 |
// (inline_call_location[0], inline_name[0]), |
| 378 |
// (inline_call_location[1], inline_name[1]), |
| 379 |
// (inline_call_location[2], inline_name[2]), |
| 380 |
// ] |
| 381 |
// innermost_location, |
| 382 |
// ) |
| 383 |
// |
| 384 |
// Output: |
| 385 |
// |
| 386 |
// ( // Each location is the line *inside* the function |
| 387 |
// (outer_name, inline_call_location[0]), |
| 388 |
// inlines: [ |
| 389 |
// (inline_name[0], inline_call_location[1]), |
| 390 |
// (inline_name[1], inline_call_location[2]), |
| 391 |
// (inline_name[2], innermost_location), |
| 392 |
// ] |
| 393 |
// ) |
| 394 |
if let Some((file_id, line, address, next_inline_origin)) = |
| 395 |
func.get_outermost_sourceloc(addr) |
| 396 |
{ |
| 397 |
if let Some(file) = self.files.get(&file_id) { |
| 398 |
frame.set_source_file(file, line, address + module.base_address()); |
| 399 |
} |
| 400 |
|
| 401 |
if let Some(mut inline_origin) = next_inline_origin { |
| 402 |
// There is an inline call at the address. |
| 403 |
// Enumerate all inlines at the address one by one by looking up |
| 404 |
// successively deeper call depths. |
| 405 |
// The call to `get_outermost_source_location` above looked up depth 0, so here |
| 406 |
// we start at depth 1. |
| 407 |
for depth in 1.. { |
| 408 |
match func.get_inlinee_at_depth(depth, addr) { |
| 409 |
Some((call_file_id, call_line, _address, next_inline_origin)) => { |
| 410 |
// We found another inline frame. |
| 411 |
let call_file = self.files.get(&call_file_id).map(Deref::deref); |
| 412 |
if let Some(name) = self.inline_origins.get(&inline_origin) { |
| 413 |
frame.add_inline_frame(name, call_file, Some(call_line)); |
| 414 |
} |
| 415 |
|
| 416 |
inline_origin = next_inline_origin; |
| 417 |
} |
| 418 |
None => break, |
| 419 |
} |
| 420 |
} |
| 421 |
// We've run out of inline calls but we still have to output the final frame. |
| 422 |
let (file, line) = match func.get_innermost_sourceloc(addr) { |
| 423 |
Some((file_id, line, _)) => ( |
| 424 |
self.files.get(&file_id).map(Deref::deref), |
| 425 |
if line != 0 { Some(line) } else { None }, |
| 426 |
), |
| 427 |
None => (None, None), |
| 428 |
}; |
| 429 |
if let Some(name) = self.inline_origins.get(&inline_origin) { |
| 430 |
frame.add_inline_frame(name, file, line); |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} else if let Some(public) = self.find_nearest_public(addr) { |
| 435 |
// We couldn't find a valid FUNC record, but we could find a PUBLIC record. |
| 436 |
// Unfortauntely, PUBLIC records don't have end-points, so this could be |
| 437 |
// a random PUBLIC record from the start of the module that isn't at all |
| 438 |
// applicable. To try limit this problem, we can use the nearest FUNC |
| 439 |
// record that comes *before* the address we're trying to find a symbol for. |
| 440 |
// |
| 441 |
// It is reasonable to assume a PUBLIC record cannot extend *past* a FUNC, |
| 442 |
// so if the PUBLIC has a smaller base address than the nearest previous FUNC |
| 443 |
// to our target address, the PUBLIC must actually end before that FUNC and |
| 444 |
// therefore not actually apply to the target address. |
| 445 |
// |
| 446 |
// We get the nearest previous FUNC by getting the raw slice of ranges |
| 447 |
// and binary searching for our base address. Rust's builtin binary search |
| 448 |
// will fail to find the value since it uses strict equality *but* the Err |
| 449 |
// will helpfully contain the index in the slice where our value "should" |
| 450 |
// be inserted to preserve the sort. The element before this index is |
| 451 |
// therefore the nearest previous value! |
| 452 |
// |
| 453 |
// Case analysis for this -1 because binary search is an off-by-one minefield: |
| 454 |
// |
| 455 |
// * if the address we were looking for came *before* every FUNC, binary_search |
| 456 |
// would yield "0" because that's where it should go to preserve the sort. |
| 457 |
// The checked_sub will then fail and make us just assume the PUBLIC is reasonable, |
| 458 |
// which is correct. |
| 459 |
// |
| 460 |
// * if we get 1, this saying we actually want element 0, so again -1 is |
| 461 |
// correct. (This generalizes to all other "reasonable" values, but 1 is easiest |
| 462 |
// to think about given the previous case's analysis.) |
| 463 |
// |
| 464 |
// * if the address we were looking for came *after* every FUNC, binary search |
| 465 |
// would yield "slice.len()", and the nearest FUNC is indeed at `len-1`, so |
| 466 |
// again correct. |
| 467 |
let funcs_slice = self.functions.ranges_values().as_slice(); |
| 468 |
let prev_func = funcs_slice |
| 469 |
.binary_search_by_key(&addr, |(range, _)| range.start) |
| 470 |
.err() |
| 471 |
.and_then(|idx| idx.checked_sub(1)) |
| 472 |
.and_then(|idx| funcs_slice.get(idx)); |
| 473 |
|
| 474 |
if let Some(prev_func) = prev_func { |
| 475 |
if public.address <= prev_func.1.address { |
| 476 |
// This PUBLIC is truncated by a FUNC before it gets to `addr`, |
| 477 |
// so we shouldn't use it. |
| 478 |
return; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
// Settle for a PUBLIC. |
| 483 |
frame.set_function( |
| 484 |
&public.name, |
| 485 |
public.address + module.base_address(), |
| 486 |
public.parameter_size, |
| 487 |
); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
pub fn walk_frame(&self, module: &dyn Module, walker: &mut dyn FrameWalker) -> Option<()> { |
| 492 |
if walker.get_instruction() < module.base_address() { |
| 493 |
return None; |
| 494 |
} |
| 495 |
let addr = walker.get_instruction() - module.base_address(); |
| 496 |
|
| 497 |
// Preferentially use framedata over fpo, because if both are present, |
| 498 |
// the former tends to be more precise (breakpad heuristic). |
| 499 |
let win_stack_result = if let Some(info) = self.win_stack_framedata_info.get(addr) { |
| 500 |
walker::walk_with_stack_win_framedata(info, walker) |
| 501 |
} else if let Some(info) = self.win_stack_fpo_info.get(addr) { |
| 502 |
walker::walk_with_stack_win_fpo(info, walker) |
| 503 |
} else { |
| 504 |
None |
| 505 |
}; |
| 506 |
|
| 507 |
// If STACK WIN failed, try STACK CFI |
| 508 |
win_stack_result.or_else(|| { |
| 509 |
if let Some(info) = self.cfi_stack_info.get(addr) { |
| 510 |
// Don't use add_rules that come after this address |
| 511 |
let mut count = 0; |
| 512 |
let len = info.add_rules.len(); |
| 513 |
while count < len && info.add_rules[count].address <= addr { |
| 514 |
count += 1; |
| 515 |
} |
| 516 |
|
| 517 |
walker::walk_with_stack_cfi(&info.init, &info.add_rules[0..count], walker) |
| 518 |
} else { |
| 519 |
None |
| 520 |
} |
| 521 |
}) |
| 522 |
} |
| 523 |
|
| 524 |
/// Find the nearest `PublicSymbol` whose address is less than or equal to `addr`. |
| 525 |
pub fn find_nearest_public(&self, addr: u64) -> Option<&PublicSymbol> { |
| 526 |
self.publics.iter().rev().find(|&p| p.address <= addr) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
#[cfg(test)] |
| 531 |
mod test { |
| 532 |
use super::*; |
| 533 |
use std::ffi::OsStr; |
| 534 |
fn test_symbolfile_from_file(rel_path: &str) { |
| 535 |
let mut path = std::env::current_dir().unwrap(); |
| 536 |
if path.file_name() == Some(OsStr::new("rust-minidump")) { |
| 537 |
path.push("breakpad-symbols"); |
| 538 |
} |
| 539 |
path.push(rel_path); |
| 540 |
let sym = SymbolFile::from_file(&path).unwrap(); |
| 541 |
assert_eq!(sym.files.len(), 6661); |
| 542 |
assert_eq!(sym.publics.len(), 5); |
| 543 |
assert_eq!(sym.find_nearest_public(0x9b07).unwrap().name, "_NLG_Return"); |
| 544 |
assert_eq!( |
| 545 |
sym.find_nearest_public(0x142e7).unwrap().name, |
| 546 |
"_NLG_Return" |
| 547 |
); |
| 548 |
assert_eq!( |
| 549 |
sym.find_nearest_public(0x23b06).unwrap().name, |
| 550 |
"__from_strstr_to_strchr" |
| 551 |
); |
| 552 |
assert_eq!( |
| 553 |
sym.find_nearest_public(0xFFFFFFFF).unwrap().name, |
| 554 |
"__from_strstr_to_strchr" |
| 555 |
); |
| 556 |
assert_eq!(sym.functions.ranges_values().count(), 1065); |
| 557 |
assert_eq!(sym.functions.get(0x1000).unwrap().name, "vswprintf"); |
| 558 |
assert_eq!(sym.functions.get(0x1012).unwrap().name, "vswprintf"); |
| 559 |
assert!(sym.functions.get(0x1013).is_none()); |
| 560 |
// There are 1556 `STACK WIN 4` lines in the symbol file, but only 856 |
| 561 |
// that don't overlap. However they all overlap in ways that we have |
| 562 |
// to handle in the wild. |
| 563 |
assert_eq!(sym.win_stack_framedata_info.ranges_values().count(), 1556); |
| 564 |
assert_eq!(sym.win_stack_fpo_info.ranges_values().count(), 259); |
| 565 |
assert_eq!( |
| 566 |
sym.win_stack_framedata_info.get(0x41b0).unwrap().address, |
| 567 |
0x41b0 |
| 568 |
); |
| 569 |
} |
| 570 |
|
| 571 |
#[test] |
| 572 |
fn test_symbolfile_from_lf_file() { |
| 573 |
test_symbolfile_from_file( |
| 574 |
"testdata/symbols/test_app.pdb/5A9832E5287241C1838ED98914E9B7FF1/test_app.sym", |
| 575 |
); |
| 576 |
} |
| 577 |
|
| 578 |
#[test] |
| 579 |
fn test_symbolfile_from_crlf_file() { |
| 580 |
test_symbolfile_from_file( |
| 581 |
"testdata/symbols/test_app.pdb/6A9832E5287241C1838ED98914E9B7FF1/test_app.sym", |
| 582 |
); |
| 583 |
} |
| 584 |
|
| 585 |
fn test_symbolfile_from_bytes(symbolfile_bytes: &[u8]) { |
| 586 |
let sym = SymbolFile::from_bytes(symbolfile_bytes).unwrap(); |
| 587 |
|
| 588 |
assert_eq!(sym.files.len(), 1); |
| 589 |
assert_eq!(sym.publics.len(), 1); |
| 590 |
assert_eq!(sym.functions.ranges_values().count(), 1); |
| 591 |
assert_eq!(sym.functions.get(0x1000).unwrap().name, "another func"); |
| 592 |
assert_eq!( |
| 593 |
sym.functions |
| 594 |
.get(0x1000) |
| 595 |
.unwrap() |
| 596 |
.lines |
| 597 |
.ranges_values() |
| 598 |
.count(), |
| 599 |
1 |
| 600 |
); |
| 601 |
// test fallback |
| 602 |
assert_eq!(sym.functions.get(0x1001).unwrap().name, "another func"); |
| 603 |
} |
| 604 |
|
| 605 |
#[test] |
| 606 |
fn test_symbolfile_from_bytes_with_lf() { |
| 607 |
test_symbolfile_from_bytes( |
| 608 |
b"MODULE Linux x86 ffff0000 bar |
| 609 |
FILE 53 bar.c |
| 610 |
PUBLIC 1234 10 some public |
| 611 |
FUNC 1000 30 10 another func |
| 612 |
1000 30 7 53 |
| 613 |
", |
| 614 |
); |
| 615 |
} |
| 616 |
|
| 617 |
#[test] |
| 618 |
fn test_symbolfile_from_bytes_with_crlf() { |
| 619 |
test_symbolfile_from_bytes( |
| 620 |
b"MODULE Linux x86 ffff0000 bar |
| 621 |
FILE 53 bar.c |
| 622 |
PUBLIC 1234 10 some public |
| 623 |
FUNC 1000 30 10 another func |
| 624 |
1000 30 7 53 |
| 625 |
", |
| 626 |
); |
| 627 |
} |
| 628 |
} |