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