Debian Codegraph @

third_party/rust/minidump-unwind/src/symbols/mod.rs

raw
1 //! This module defines the interface used by minidump-unwind to symbolize stack traces.
2 //!
3 //! minidump-unwind uses a series of traits to represent symbolizing functionality and interfaces:
4 //!
5 //! * [SymbolProvider][] - provides symbolication, cfi evaluation, and debug statistics
6 //! * Implemented by [Symbolizer][] and [debuginfo::DebugInfoSymbolProvider][] (requires the
7 //! `debuginfo` feature to be enabled).
8 //!
9 //! * [SymbolSupplier][] - maps a [Module][] to a [SymbolFile][]
10 //! * minidump-unwind does not directly use this, it's just there so the Symbolizer can
11 //! generically handle different symbol fetching strategies.
12 //!
13 //! * [FrameSymbolizer][] - callbacks that symbolication uses to return its results.
14 //! * Implemented by [StackFrame][crate::StackFrame]
15 //! * Implemented by DummyFrame (private, for a stack scanning heuristic)
16 //! * [FrameWalker][] - callbacks that cfi eval uses to read callee state and write caller state.
17 //! * Implemented by CfiStackWalker (private)
18 //!
19 //!
20 //! The following concrete functions are provided to allow configuration of the symbol fetching
21 //! strategy:
22 //!
23 //! * [http_symbol_supplier][] - a [SymbolSupplier][] that can find symbols over HTTP (and cache).
24 //! Requires the `http` feature to be enabled.
25 //! * [simple_symbol_supplier][] - a [SymbolSupplier][] that can find symbols on disk.
26 //! * [string_symbol_supplier][] - a mock [SymbolSupplier][] for tests.
27 //!
28 //!
29 //! The following concrete types are provided:
30 //!
31 //! * [Symbolizer][] - the main interface of the symbolizer, implementing [SymbolProvider][].
32 //! * Wraps the [SymbolSupplier][] implementation that is selected.
33 //! * Queries the [SymbolSupplier] and manages the SymbolFiles however it pleases.
34 //! * [SymbolStats][] - debug statistic output.
35 //! * [SymbolFile][] - part of [LocateSymbolsResult][] that a [SymbolProvider][] returns to the
36 //! Symbolizer.
37 //! * Never handled by minidump-unwind, public for the trait.
38 //! * [SymbolError][] - possible errors a [SymbolProvider][] can yield.
39 //! * Never handled by minidump-unwind, public for the trait.
40 //! * [FillSymbolError][] - possible errors for `fill_symbol`.
41 //! * While this *is* handled by minidump-unwind, it doesn't actually look at the value. It's
42 //! just there to be an Error type for the sake of API design.
43 //! * [LocateSymbolsResult][] - a payload that a [SymbolProvider][] returns to the Symbolizer.
44 //! * Never handled by minidump-unwind, public for the trait.
45 //! * [DebugInfoResult][] - part of [LocateSymbolsResult][] that a [SymbolProvider][] returns to
46 //! the Symbolizer.
47 //! * Never handled by minidump-unwind, public for the trait.
48
49 use std::collections::HashMap;
50 use std::path::PathBuf;
51
52 use async_trait::async_trait;
53 use minidump::Module;
54
55 pub use breakpad_symbols::{
56 DebugInfoResult, FileError, FileKind, FillSymbolError, FrameSymbolizer, FrameWalker,
57 LocateSymbolsResult, PendingSymbolStats, SymbolError, SymbolFile, SymbolStats, SymbolSupplier,
58 Symbolizer,
59 };
60
61 #[cfg(feature = "debuginfo-unwind")]
62 pub mod debuginfo;
63
64 /// The [`SymbolProvider`] is the main extension point for minidump processing.
65 ///
66 /// It is primarily used by the `process_minidump` function to do stack
67 /// unwinding via CFI (call frame information) of a [`Module`] using the
68 /// `walk_frame` function.
69 ///
70 /// The `fill_symbol` function is responsible for filling in the source location
71 /// (function, file, line triple) corresponding to an instruction address, as
72 /// well as a dual purpose of informing the stack scanning heuristic whether a
73 /// given instruction address might be valid inside of a [`Module`].
74 ///
75 /// All the asynchronous trait methods can be called concurrently and need to
76 /// handle synchronization and request coalescing (based on the [`Module`]).
77 #[async_trait]
78 pub trait SymbolProvider {
79 /// Fill symbol information in [`FrameSymbolizer`] using the instruction
80 /// address from `frame`, and the module information from [`Module`].
81 ///
82 /// An Error indicates that no symbols could be found for the relevant
83 /// module.
84 ///
85 /// This is used for filling in the resulting source location of the
86 /// frame as a (function, file, line) triple, as well as providing the
87 /// `parameter_size` which is used during CFI evaluation and stack walking.
88 ///
89 /// This function also serves a dual purpose in informing the stack scanning
90 /// heuristic whether a potential instruction address points to a valid
91 /// function or not.
92 async fn fill_symbol(
93 &self,
94 module: &(dyn Module + Sync),
95 frame: &mut (dyn FrameSymbolizer + Send),
96 ) -> Result<(), FillSymbolError>;
97
98 /// Tries to use CFI to walk the stack frame of the [`FrameWalker`]
99 /// using the symbols of the given [`Module`].
100 ///
101 /// Output should be written using the [`FrameWalker`]'s `set_caller_*` APIs.
102 async fn walk_frame(
103 &self,
104 module: &(dyn Module + Sync),
105 walker: &mut (dyn FrameWalker + Send),
106 ) -> Option<()>;
107
108 /// Gets the path to the binary code file for a given module (or an Error).
109 ///
110 /// This might be used later on to inspect the assembly instructions of
111 /// a module.
112 async fn get_file_path(
113 &self,
114 module: &(dyn Module + Sync),
115 file_kind: FileKind,
116 ) -> Result<PathBuf, FileError>;
117
118 /// Collect various statistics on the symbols.
119 ///
120 /// Keys are implementation dependent.
121 /// For example the file name of the module (code_file's file name).
122 ///
123 /// This is only really intended to be queried after processing an
124 /// entire minidump, and may have non-trivial overhead to compute.
125 /// It's als possible we'd want it to also be able to contain stats
126 /// that don't really make sense in intermediate states.
127 ///
128 /// In a world where you might want to have one SymbolSupplier shared
129 /// by multiple instances of `process` running in parallel, it's unclear
130 /// if this is the right abstraction. Perhaps we should have some kind
131 /// of "session" abstraction so you can get stats about each individual
132 /// processing task? Of course all pooling/caching between the tasks
133 /// muddies things too.
134 fn stats(&self) -> HashMap<String, SymbolStats> {
135 HashMap::new()
136 }
137
138 /// Collect various pending statistics on the symbols.
139 ///
140 /// This is intended to be queried during processing to give some
141 /// interactive feedback to the user, and so is fine to poll as
142 /// much as you want, whenever you want.
143 fn pending_stats(&self) -> PendingSymbolStats {
144 PendingSymbolStats::default()
145 }
146 }
147
148 #[async_trait]
149 impl SymbolProvider for &(dyn SymbolProvider + Sync) {
150 async fn fill_symbol(
151 &self,
152 module: &(dyn Module + Sync),
153 frame: &mut (dyn FrameSymbolizer + Send),
154 ) -> Result<(), FillSymbolError> {
155 (*self).fill_symbol(module, frame).await
156 }
157
158 async fn walk_frame(
159 &self,
160 module: &(dyn Module + Sync),
161 walker: &mut (dyn FrameWalker + Send),
162 ) -> Option<()> {
163 (*self).walk_frame(module, walker).await
164 }
165
166 async fn get_file_path(
167 &self,
168 module: &(dyn Module + Sync),
169 file_kind: FileKind,
170 ) -> Result<PathBuf, FileError> {
171 (*self).get_file_path(module, file_kind).await
172 }
173
174 fn stats(&self) -> HashMap<String, SymbolStats> {
175 (*self).stats()
176 }
177
178 fn pending_stats(&self) -> PendingSymbolStats {
179 (*self).pending_stats()
180 }
181 }
182
183 #[derive(Default)]
184 pub struct MultiSymbolProvider {
185 providers: Vec<Box<dyn SymbolProvider + Send + Sync>>,
186 }
187
188 impl MultiSymbolProvider {
189 pub fn new() -> MultiSymbolProvider {
190 Default::default()
191 }
192
193 pub fn add(&mut self, provider: Box<dyn SymbolProvider + Send + Sync>) {
194 self.providers.push(provider);
195 }
196 }
197
198 #[async_trait]
199 impl SymbolProvider for MultiSymbolProvider {
200 async fn fill_symbol(
201 &self,
202 module: &(dyn Module + Sync),
203 frame: &mut (dyn FrameSymbolizer + Send),
204 ) -> Result<(), FillSymbolError> {
205 // Return Ok if *any* symbol provider came back with Ok, so that the user can
206 // distinguish between having no symbols at all and just not being able to
207 // symbolize this particular frame.
208 let mut best_result = Err(FillSymbolError {});
209 for p in self.providers.iter() {
210 let new_result = p.fill_symbol(module, frame).await;
211 best_result = best_result.or(new_result);
212 }
213 best_result
214 }
215
216 async fn walk_frame(
217 &self,
218 module: &(dyn Module + Sync),
219 walker: &mut (dyn FrameWalker + Send),
220 ) -> Option<()> {
221 for p in self.providers.iter() {
222 let result = p.walk_frame(module, walker).await;
223 if result.is_some() {
224 return result;
225 }
226 }
227 None
228 }
229
230 async fn get_file_path(
231 &self,
232 module: &(dyn Module + Sync),
233 file_kind: FileKind,
234 ) -> Result<PathBuf, FileError> {
235 // Return Ok if *any* symbol provider came back with Ok
236 let mut best_result = Err(FileError::NotFound);
237 for p in self.providers.iter() {
238 let new_result = p.get_file_path(module, file_kind).await;
239 best_result = best_result.or(new_result);
240 }
241 best_result
242 }
243
244 fn stats(&self) -> HashMap<String, SymbolStats> {
245 let mut result = HashMap::new();
246 for p in self.providers.iter() {
247 // FIXME: do more intelligent merging of the stats
248 // (currently doesn't matter as only one provider reports non-empty stats).
249 result.extend(p.stats());
250 }
251 result
252 }
253
254 fn pending_stats(&self) -> PendingSymbolStats {
255 let mut result = PendingSymbolStats::default();
256 for p in self.providers.iter() {
257 // FIXME: do more intelligent merging of the stats
258 // (currently doesn't matter as only one provider reports non-empty stats).
259 result = p.pending_stats();
260 }
261 result
262 }
263 }
264
265 #[async_trait]
266 impl SymbolProvider for Symbolizer {
267 async fn fill_symbol(
268 &self,
269 module: &(dyn Module + Sync),
270 frame: &mut (dyn FrameSymbolizer + Send),
271 ) -> Result<(), FillSymbolError> {
272 self.fill_symbol(module, frame).await
273 }
274 async fn walk_frame(
275 &self,
276 module: &(dyn Module + Sync),
277 walker: &mut (dyn FrameWalker + Send),
278 ) -> Option<()> {
279 self.walk_frame(module, walker).await
280 }
281 async fn get_file_path(
282 &self,
283 module: &(dyn Module + Sync),
284 file_kind: FileKind,
285 ) -> Result<PathBuf, FileError> {
286 self.get_file_path(module, file_kind).await
287 }
288 fn stats(&self) -> HashMap<String, SymbolStats> {
289 self.stats()
290 }
291 fn pending_stats(&self) -> PendingSymbolStats {
292 self.pending_stats()
293 }
294 }
295
296 /// Gets a SymbolSupplier that looks up symbols by path or with urls.
297 ///
298 /// * `symbols_paths` is a list of paths to check for symbol files. Paths
299 /// are searched in order until one returns a payload. If none do, then
300 /// urls are used.
301 ///
302 /// * `symbols_urls` is a list of "base urls" that should all point to Tecken
303 /// servers. urls are queried in order until one returns a payload. If none
304 /// do, then it's an error.
305 ///
306 /// * `symbols_cache` is a directory where an on-disk cache should be located.
307 /// This should be assumed to be a "temp" directory that another process
308 /// you don't control is garbage-collecting old files from (to provide an LRU cache).
309 /// The cache is queried before paths and urls (otherwise it wouldn't be much of a cache).
310 ///
311 /// * `symbols_tmp` is a directory where symbol files should be downloaded to
312 /// before atomically swapping them into the cache. Has the same "temp"
313 /// assumptions as symbols_cache.
314 ///
315 /// * `timeout` a maximum time limit for a symbol file download. This
316 /// is primarily defined to avoid getting stuck on buggy infinite downloads.
317 /// As of this writing, minidump-stackwalk defaults this to 1000 seconds. In
318 /// the event of a timeout, the supplier may still try to parse the truncated
319 /// download.
320 #[cfg(feature = "http")]
321 pub fn http_symbol_supplier(
322 symbol_paths: Vec<PathBuf>,
323 symbol_urls: Vec<String>,
324 symbols_cache: PathBuf,
325 symbols_tmp: PathBuf,
326 timeout: std::time::Duration,
327 ) -> impl SymbolSupplier {
328 breakpad_symbols::HttpSymbolSupplier::new(
329 symbol_urls,
330 symbols_cache,
331 symbols_tmp,
332 symbol_paths,
333 timeout,
334 )
335 }
336
337 /// Gets a SymbolSupplier that looks up symbols by path.
338 ///
339 /// Paths are queried in order until one returns a payload.
340 pub fn simple_symbol_supplier(symbol_paths: Vec<PathBuf>) -> impl SymbolSupplier {
341 breakpad_symbols::SimpleSymbolSupplier::new(symbol_paths)
342 }
343
344 /// Gets a mock SymbolSupplier that just maps module names
345 /// to a string containing an entire breakpad .sym file, for tests.
346 pub fn string_symbol_supplier(modules: HashMap<String, String>) -> impl SymbolSupplier {
347 breakpad_symbols::StringSymbolSupplier::new(modules)
348 }