OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 // stackwalker.h: Generic stackwalker. | |
31 // | |
32 // The Stackwalker class is an abstract base class providing common generic | |
33 // methods that apply to stacks from all systems. Specific implementations | |
34 // will extend this class by providing GetContextFrame and GetCallerFrame | |
35 // methods to fill in system-specific data in a StackFrame structure. | |
36 // Stackwalker assembles these StackFrame strucutres into a CallStack. | |
37 // | |
38 // Author: Mark Mentovai | |
39 | |
40 | |
41 #ifndef GOOGLE_BREAKPAD_PROCESSOR_STACKWALKER_H__ | |
42 #define GOOGLE_BREAKPAD_PROCESSOR_STACKWALKER_H__ | |
43 | |
44 #include <set> | |
45 #include <string> | |
46 #include "google_breakpad/common/breakpad_types.h" | |
47 #include "google_breakpad/processor/code_modules.h" | |
48 #include "google_breakpad/processor/memory_region.h" | |
49 | |
50 namespace google_breakpad { | |
51 | |
52 class CallStack; | |
53 class MinidumpContext; | |
54 class SourceLineResolverInterface; | |
55 struct StackFrame; | |
56 class SymbolSupplier; | |
57 class SystemInfo; | |
58 | |
59 using std::set; | |
60 | |
61 | |
62 class Stackwalker { | |
63 public: | |
64 virtual ~Stackwalker() {} | |
65 | |
66 // Populates the given CallStack by calling GetContextFrame and | |
67 // GetCallerFrame. The frames are further processed to fill all available | |
68 // data. Returns true if the stackwalk completed, or false if it was | |
69 // interrupted by SymbolSupplier::GetSymbolFile(). | |
70 bool Walk(CallStack *stack); | |
71 | |
72 // Returns a new concrete subclass suitable for the CPU that a stack was | |
73 // generated on, according to the CPU type indicated by the context | |
74 // argument. If no suitable concrete subclass exists, returns NULL. | |
75 static Stackwalker* StackwalkerForCPU(const SystemInfo *system_info, | |
76 MinidumpContext *context, | |
77 MemoryRegion *memory, | |
78 const CodeModules *modules, | |
79 SymbolSupplier *supplier, | |
80 SourceLineResolverInterface *resolver); | |
81 | |
82 static void set_max_frames(u_int32_t max_frames) { max_frames_ = max_frames; } | |
83 static u_int32_t max_frames() { return max_frames_; } | |
84 | |
85 protected: | |
86 // system_info identifies the operating system, NULL or empty if unknown. | |
87 // memory identifies a MemoryRegion that provides the stack memory | |
88 // for the stack to walk. modules, if non-NULL, is a CodeModules | |
89 // object that is used to look up which code module each stack frame is | |
90 // associated with. supplier is an optional caller-supplied SymbolSupplier | |
91 // implementation. If supplier is NULL, source line info will not be | |
92 // resolved. resolver is an instance of SourceLineResolverInterface | |
93 // (see source_line_resolver_interface.h and basic_source_line_resolver.h). | |
94 // If resolver is NULL, source line info will not be resolved. | |
95 Stackwalker(const SystemInfo *system_info, | |
96 MemoryRegion *memory, | |
97 const CodeModules *modules, | |
98 SymbolSupplier *supplier, | |
99 SourceLineResolverInterface *resolver); | |
100 | |
101 // This can be used to filter out potential return addresses when | |
102 // the stack walker resorts to stack scanning. | |
103 // Returns true if any of: | |
104 // * This address is within a loaded module, but we don't have symbols | |
105 // for that module. | |
106 // * This address is within a loaded module for which we have symbols, | |
107 // and falls inside a function in that module. | |
108 // Returns false otherwise. | |
109 bool InstructionAddressSeemsValid(u_int64_t address); | |
110 | |
111 // Scan the stack starting at location_start, looking for an address | |
112 // that looks like a valid instruction pointer. Addresses must | |
113 // 1) be contained in the current stack memory | |
114 // 2) pass the checks in InstructionAddressSeemsValid | |
115 // | |
116 // Returns true if a valid-looking instruction pointer was found. | |
117 // When returning true, sets location_found to the address at which | |
118 // the value was found, and ip_found to the value contained at that | |
119 // location in memory. | |
120 template<typename InstructionType> | |
121 bool ScanForReturnAddress(InstructionType location_start, | |
122 InstructionType *location_found, | |
123 InstructionType *ip_found) { | |
124 const int kRASearchWords = 30; | |
125 for (InstructionType location = location_start; | |
126 location <= location_start + kRASearchWords * sizeof(InstructionType); | |
127 location += sizeof(InstructionType)) { | |
128 InstructionType ip; | |
129 if (!memory_->GetMemoryAtAddress(location, &ip)) | |
130 break; | |
131 | |
132 if (modules_ && modules_->GetModuleForAddress(ip) && | |
133 InstructionAddressSeemsValid(ip)) { | |
134 | |
135 *ip_found = ip; | |
136 *location_found = location; | |
137 return true; | |
138 } | |
139 } | |
140 // nothing found | |
141 return false; | |
142 } | |
143 | |
144 // Information about the system that produced the minidump. Subclasses | |
145 // and the SymbolSupplier may find this information useful. | |
146 const SystemInfo *system_info_; | |
147 | |
148 // The stack memory to walk. Subclasses will require this region to | |
149 // get information from the stack. | |
150 MemoryRegion *memory_; | |
151 | |
152 // A list of modules, for populating each StackFrame's module information. | |
153 // This field is optional and may be NULL. | |
154 const CodeModules *modules_; | |
155 | |
156 protected: | |
157 // The SourceLineResolver implementation. | |
158 SourceLineResolverInterface *resolver_; | |
159 | |
160 private: | |
161 // Obtains the context frame, the innermost called procedure in a stack | |
162 // trace. Returns NULL on failure. GetContextFrame allocates a new | |
163 // StackFrame (or StackFrame subclass), ownership of which is taken by | |
164 // the caller. | |
165 virtual StackFrame* GetContextFrame() = 0; | |
166 | |
167 // Obtains a caller frame. Each call to GetCallerFrame should return the | |
168 // frame that called the last frame returned by GetContextFrame or | |
169 // GetCallerFrame. To aid this purpose, stack contains the CallStack | |
170 // made of frames that have already been walked. GetCallerFrame should | |
171 // return NULL on failure or when there are no more caller frames (when | |
172 // the end of the stack has been reached). GetCallerFrame allocates a new | |
173 // StackFrame (or StackFrame subclass), ownership of which is taken by | |
174 // the caller. | |
175 virtual StackFrame* GetCallerFrame(const CallStack *stack) = 0; | |
176 | |
177 // The optional SymbolSupplier for resolving source line info. | |
178 SymbolSupplier *supplier_; | |
179 | |
180 // A list of modules that we haven't found symbols for. We track | |
181 // this in order to avoid repeatedly looking them up again within | |
182 // one minidump. | |
183 set<std::string> no_symbol_modules_; | |
184 | |
185 // The maximum number of frames Stackwalker will walk through. | |
186 // This defaults to 1024 to prevent infinite loops. | |
187 static u_int32_t max_frames_; | |
188 }; | |
189 | |
190 | |
191 } // namespace google_breakpad | |
192 | |
193 | |
194 #endif // GOOGLE_BREAKPAD_PROCESSOR_STACKWALKER_H__ | |
OLD | NEW |