wasmer_types/stack/
frame.rs

1use crate::SourceLoc;
2
3/// Description of a frame in a backtrace.
4///
5/// Each runtime error includes a backtrace of the WebAssembly frames that led
6/// to the trap, and each frame is described by this structure.
7#[derive(Debug, Clone)]
8pub struct FrameInfo {
9    /// The name of the module
10    module_name: String,
11    /// The index of the function in the module
12    func_index: u32,
13    /// The function name, if one is available.
14    function_name: Option<String>,
15    /// The source location of the function
16    func_start: SourceLoc,
17    /// The source location of the instruction
18    instr: SourceLoc,
19}
20
21impl FrameInfo {
22    /// Creates a new [FrameInfo], useful for testing.
23    pub fn new(
24        module_name: String,
25        func_index: u32,
26        function_name: Option<String>,
27        func_start: SourceLoc,
28        instr: SourceLoc,
29    ) -> Self {
30        Self {
31            module_name,
32            func_index,
33            function_name,
34            func_start,
35            instr,
36        }
37    }
38
39    /// Returns the WebAssembly function index for this frame.
40    ///
41    /// This function index is the index in the function index space of the
42    /// WebAssembly module that this frame comes from.
43    pub fn func_index(&self) -> u32 {
44        self.func_index
45    }
46
47    /// Returns the identifer of the module that this frame is for.
48    ///
49    /// ModuleInfo identifiers are present in the `name` section of a WebAssembly
50    /// binary, but this may not return the exact item in the `name` section.
51    /// ModuleInfo names can be overwritten at construction time or perhaps inferred
52    /// from file names. The primary purpose of this function is to assist in
53    /// debugging and therefore may be tweaked over time.
54    ///
55    /// This function returns `None` when no name can be found or inferred.
56    pub fn module_name(&self) -> &str {
57        &self.module_name
58    }
59
60    /// Returns a descriptive name of the function for this frame, if one is
61    /// available.
62    ///
63    /// The name of this function may come from the `name` section of the
64    /// WebAssembly binary, or wasmer may try to infer a better name for it if
65    /// not available, for example the name of the export if it's exported.
66    ///
67    /// This return value is primarily used for debugging and human-readable
68    /// purposes for things like traps. Note that the exact return value may be
69    /// tweaked over time here and isn't guaranteed to be something in
70    /// particular about a wasm module due to its primary purpose of assisting
71    /// in debugging.
72    ///
73    /// This function returns `None` when no name could be inferred.
74    pub fn function_name(&self) -> Option<&str> {
75        self.function_name.as_deref()
76    }
77
78    /// Returns the offset within the original wasm module this frame's program
79    /// counter was at.
80    ///
81    /// The offset here is the offset from the beginning of the original wasm
82    /// module to the instruction that this frame points to.
83    pub fn module_offset(&self) -> usize {
84        self.instr.bits() as usize
85    }
86
87    /// Returns the offset from the original wasm module's function to this
88    /// frame's program counter.
89    ///
90    /// The offset here is the offset from the beginning of the defining
91    /// function of this frame (within the wasm module) to the instruction this
92    /// frame points to.
93    pub fn func_offset(&self) -> usize {
94        (self.instr.bits() - self.func_start.bits()) as usize
95    }
96}