xref: /aosp_15_r20/external/google-breakpad/src/google_breakpad/processor/stack_frame.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef GOOGLE_BREAKPAD_PROCESSOR_STACK_FRAME_H__
30 #define GOOGLE_BREAKPAD_PROCESSOR_STACK_FRAME_H__
31 
32 #include <string>
33 
34 #include "common/using_std_string.h"
35 #include "google_breakpad/common/breakpad_types.h"
36 
37 namespace google_breakpad {
38 
39 class CodeModule;
40 
41 struct StackFrame {
42   // Indicates how well the instruction pointer derived during
43   // stack walking is trusted. Since the stack walker can resort to
44   // stack scanning, it can wind up with dubious frames.
45   // In rough order of "trust metric".
46   enum FrameTrust {
47     FRAME_TRUST_NONE,      // Unknown
48     FRAME_TRUST_SCAN,      // Scanned the stack, found this
49     FRAME_TRUST_CFI_SCAN,  // Found while scanning stack using call frame info
50     FRAME_TRUST_FP,        // Derived from frame pointer
51     FRAME_TRUST_CFI,       // Derived from call frame info
52     // Explicitly provided by some external stack walker.
53     FRAME_TRUST_PREWALKED,
54     FRAME_TRUST_CONTEXT,   // Given as instruction pointer in a context
55     FRAME_TRUST_INLINE,    // Found by inline records in symbol files.
56     // Derived from leaf function by simulating a return.
57     FRAME_TRUST_LEAF,
58   };
59 
StackFrameStackFrame60   StackFrame()
61       : instruction(),
62         module(NULL),
63         function_name(),
64         function_base(),
65         source_file_name(),
66         source_line(0),
67         source_line_base(),
68         trust(FRAME_TRUST_NONE),
69         is_multiple(false) {}
~StackFrameStackFrame70   virtual ~StackFrame() {}
71 
72   // Return a string describing how this stack frame was found
73   // by the stackwalker.
trust_descriptionStackFrame74   string trust_description() const {
75     switch (trust) {
76       case StackFrame::FRAME_TRUST_CONTEXT:
77         return "given as instruction pointer in context";
78       case StackFrame::FRAME_TRUST_PREWALKED:
79         return "recovered by external stack walker";
80       case StackFrame::FRAME_TRUST_CFI:
81         return "call frame info";
82       case StackFrame::FRAME_TRUST_CFI_SCAN:
83         return "call frame info with scanning";
84       case StackFrame::FRAME_TRUST_FP:
85         return "previous frame's frame pointer";
86       case StackFrame::FRAME_TRUST_SCAN:
87         return "stack scanning";
88       case StackFrame::FRAME_TRUST_INLINE:
89         return "inline record";
90       case StackFrame::FRAME_TRUST_LEAF:
91         return "simulating a return from leaf function";
92     default:
93         return "unknown";
94     }
95   }
96 
97   // Return the actual return address, as saved on the stack or in a
98   // register. See the comments for 'instruction', below, for details.
ReturnAddressStackFrame99   virtual uint64_t ReturnAddress() const { return instruction; }
100 
101   // The program counter location as an absolute virtual address.
102   //
103   // - For the innermost called frame in a stack, this will be an exact
104   //   program counter or instruction pointer value.
105   //
106   // - For all other frames, this address is within the instruction that
107   //   caused execution to branch to this frame's callee (although it may
108   //   not point to the exact beginning of that instruction). This ensures
109   //   that, when we look up the source code location for this frame, we
110   //   get the source location of the call, not of the point at which
111   //   control will resume when the call returns, which may be on the next
112   //   line. (If the compiler knows the callee never returns, it may even
113   //   place the call instruction at the very end of the caller's machine
114   //   code, such that the "return address" (which will never be used)
115   //   immediately after the call instruction is in an entirely different
116   //   function, perhaps even from a different source file.)
117   //
118   // On some architectures, the return address as saved on the stack or in
119   // a register is fine for looking up the point of the call. On others, it
120   // requires adjustment. ReturnAddress returns the address as saved by the
121   // machine.
122   uint64_t instruction;
123 
124   // The module in which the instruction resides.
125   const CodeModule *module;
126 
127   // The function name, may be omitted if debug symbols are not available.
128   string function_name;
129 
130   // The start address of the function, may be omitted if debug symbols
131   // are not available.
132   uint64_t function_base;
133 
134   // The source file name, may be omitted if debug symbols are not available.
135   string source_file_name;
136 
137   // The (1-based) source line number, may be omitted if debug symbols are
138   // not available.
139   int source_line;
140 
141   // The start address of the source line, may be omitted if debug symbols
142   // are not available.
143   uint64_t source_line_base;
144 
145   // Amount of trust the stack walker has in the instruction pointer
146   // of this frame.
147   FrameTrust trust;
148 
149   // True if the frame corresponds to multiple functions, for example as the
150   // result of identical code folding by the linker. In that case the function
151   // name, filename, etc. information above represents the state of an arbitrary
152   // one of these functions.
153   bool is_multiple;
154 };
155 
156 }  // namespace google_breakpad
157 
158 #endif  // GOOGLE_BREAKPAD_PROCESSOR_STACK_FRAME_H__
159