|
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_x86.cc: x86-specific stackwalker. |
|
31 // |
|
32 // See stackwalker_x86.h for documentation. |
|
33 // |
|
34 // Author: Mark Mentovai |
|
35 |
|
36 #include <assert.h> |
|
37 #include <string> |
|
38 |
|
39 #include "common/scoped_ptr.h" |
|
40 #include "google_breakpad/processor/call_stack.h" |
|
41 #include "google_breakpad/processor/code_modules.h" |
|
42 #include "google_breakpad/processor/memory_region.h" |
|
43 #include "google_breakpad/processor/source_line_resolver_interface.h" |
|
44 #include "google_breakpad/processor/stack_frame_cpu.h" |
|
45 #include "common/logging.h" |
|
46 #include "processor/postfix_evaluator-inl.h" |
|
47 #include "processor/stackwalker_x86.h" |
|
48 #include "processor/windows_frame_info.h" |
|
49 #include "processor/cfi_frame_info.h" |
|
50 |
|
51 namespace google_breakpad { |
|
52 |
|
53 |
|
54 const StackwalkerX86::CFIWalker::RegisterSet |
|
55 StackwalkerX86::cfi_register_map_[] = { |
|
56 // It may seem like $eip and $esp are callee-saves, because (with Unix or |
|
57 // cdecl calling conventions) the callee is responsible for having them |
|
58 // restored upon return. But the callee_saves flags here really means |
|
59 // that the walker should assume they're unchanged if the CFI doesn't |
|
60 // mention them, which is clearly wrong for $eip and $esp. |
|
61 { ToUniqueString("$eip"), ToUniqueString(".ra"), false, |
|
62 StackFrameX86::CONTEXT_VALID_EIP, &MDRawContextX86::eip }, |
|
63 { ToUniqueString("$esp"), ToUniqueString(".cfa"), false, |
|
64 StackFrameX86::CONTEXT_VALID_ESP, &MDRawContextX86::esp }, |
|
65 { ToUniqueString("$ebp"), NULL, true, |
|
66 StackFrameX86::CONTEXT_VALID_EBP, &MDRawContextX86::ebp }, |
|
67 { ToUniqueString("$eax"), NULL, false, |
|
68 StackFrameX86::CONTEXT_VALID_EAX, &MDRawContextX86::eax }, |
|
69 { ToUniqueString("$ebx"), NULL, true, |
|
70 StackFrameX86::CONTEXT_VALID_EBX, &MDRawContextX86::ebx }, |
|
71 { ToUniqueString("$ecx"), NULL, false, |
|
72 StackFrameX86::CONTEXT_VALID_ECX, &MDRawContextX86::ecx }, |
|
73 { ToUniqueString("$edx"), NULL, false, |
|
74 StackFrameX86::CONTEXT_VALID_EDX, &MDRawContextX86::edx }, |
|
75 { ToUniqueString("$esi"), NULL, true, |
|
76 StackFrameX86::CONTEXT_VALID_ESI, &MDRawContextX86::esi }, |
|
77 { ToUniqueString("$edi"), NULL, true, |
|
78 StackFrameX86::CONTEXT_VALID_EDI, &MDRawContextX86::edi }, |
|
79 }; |
|
80 |
|
81 StackwalkerX86::StackwalkerX86(const SystemInfo* system_info, |
|
82 const MDRawContextX86* context, |
|
83 MemoryRegion* memory, |
|
84 const CodeModules* modules, |
|
85 StackFrameSymbolizer* resolver_helper) |
|
86 : Stackwalker(system_info, memory, modules, resolver_helper), |
|
87 context_(context), |
|
88 cfi_walker_(cfi_register_map_, |
|
89 (sizeof(cfi_register_map_) / sizeof(cfi_register_map_[0]))) { |
|
90 if (memory_ && memory_->GetBase() + memory_->GetSize() - 1 > 0xffffffff) { |
|
91 // The x86 is a 32-bit CPU, the limits of the supplied stack are invalid. |
|
92 // Mark memory_ = NULL, which will cause stackwalking to fail. |
|
93 BPLOG(ERROR) << "Memory out of range for stackwalking: " << |
|
94 HexString(memory_->GetBase()) << "+" << |
|
95 HexString(memory_->GetSize()); |
|
96 memory_ = NULL; |
|
97 } |
|
98 } |
|
99 |
|
100 StackFrameX86::~StackFrameX86() { |
|
101 if (windows_frame_info) |
|
102 delete windows_frame_info; |
|
103 windows_frame_info = NULL; |
|
104 if (cfi_frame_info) |
|
105 delete cfi_frame_info; |
|
106 cfi_frame_info = NULL; |
|
107 } |
|
108 |
|
109 uint64_t StackFrameX86::ReturnAddress() const |
|
110 { |
|
111 assert(context_validity & StackFrameX86::CONTEXT_VALID_EIP); |
|
112 return context.eip; |
|
113 } |
|
114 |
|
115 StackFrame* StackwalkerX86::GetContextFrame() { |
|
116 if (!context_) { |
|
117 BPLOG(ERROR) << "Can't get context frame without context"; |
|
118 return NULL; |
|
119 } |
|
120 |
|
121 StackFrameX86* frame = new StackFrameX86(); |
|
122 |
|
123 // The instruction pointer is stored directly in a register, so pull it |
|
124 // straight out of the CPU context structure. |
|
125 frame->context = *context_; |
|
126 frame->context_validity = StackFrameX86::CONTEXT_VALID_ALL; |
|
127 frame->trust = StackFrame::FRAME_TRUST_CONTEXT; |
|
128 frame->instruction = frame->context.eip; |
|
129 |
|
130 return frame; |
|
131 } |
|
132 |
|
133 StackFrameX86* StackwalkerX86::GetCallerByWindowsFrameInfo( |
|
134 const vector<StackFrame*> &frames, |
|
135 WindowsFrameInfo* last_frame_info, |
|
136 bool stack_scan_allowed) { |
|
137 StackFrame::FrameTrust trust = StackFrame::FRAME_TRUST_NONE; |
|
138 |
|
139 StackFrameX86* last_frame = static_cast<StackFrameX86*>(frames.back()); |
|
140 |
|
141 // Save the stack walking info we found, in case we need it later to |
|
142 // find the callee of the frame we're constructing now. |
|
143 last_frame->windows_frame_info = last_frame_info; |
|
144 |
|
145 // This function only covers the full STACK WIN case. If |
|
146 // last_frame_info is VALID_PARAMETER_SIZE-only, then we should |
|
147 // assume the traditional frame format or use some other strategy. |
|
148 if (last_frame_info->valid != WindowsFrameInfo::VALID_ALL) |
|
149 return NULL; |
|
150 |
|
151 // This stackwalker sets each frame's %esp to its value immediately prior |
|
152 // to the CALL into the callee. This means that %esp points to the last |
|
153 // callee argument pushed onto the stack, which may not be where %esp points |
|
154 // after the callee returns. Specifically, the value is correct for the |
|
155 // cdecl calling convention, but not other conventions. The cdecl |
|
156 // convention requires a caller to pop its callee's arguments from the |
|
157 // stack after the callee returns. This is usually accomplished by adding |
|
158 // the known size of the arguments to %esp. Other calling conventions, |
|
159 // including stdcall, thiscall, and fastcall, require the callee to pop any |
|
160 // parameters stored on the stack before returning. This is usually |
|
161 // accomplished by using the RET n instruction, which pops n bytes off |
|
162 // the stack after popping the return address. |
|
163 // |
|
164 // Because each frame's %esp will point to a location on the stack after |
|
165 // callee arguments have been PUSHed, when locating things in a stack frame |
|
166 // relative to %esp, the size of the arguments to the callee need to be |
|
167 // taken into account. This seems a little bit unclean, but it's better |
|
168 // than the alternative, which would need to take these same things into |
|
169 // account, but only for cdecl functions. With this implementation, we get |
|
170 // to be agnostic about each function's calling convention. Furthermore, |
|
171 // this is how Windows debugging tools work, so it means that the %esp |
|
172 // values produced by this stackwalker directly correspond to the %esp |
|
173 // values you'll see there. |
|
174 // |
|
175 // If the last frame has no callee (because it's the context frame), just |
|
176 // set the callee parameter size to 0: the stack pointer can't point to |
|
177 // callee arguments because there's no callee. This is correct as long |
|
178 // as the context wasn't captured while arguments were being pushed for |
|
179 // a function call. Note that there may be functions whose parameter sizes |
|
180 // are unknown, 0 is also used in that case. When that happens, it should |
|
181 // be possible to walk to the next frame without reference to %esp. |
|
182 |
|
183 uint32_t last_frame_callee_parameter_size = 0; |
|
184 int frames_already_walked = frames.size(); |
|
185 if (frames_already_walked >= 2) { |
|
186 const StackFrameX86* last_frame_callee |
|
187 = static_cast<StackFrameX86*>(frames[frames_already_walked - 2]); |
|
188 WindowsFrameInfo* last_frame_callee_info |
|
189 = last_frame_callee->windows_frame_info; |
|
190 if (last_frame_callee_info && |
|
191 (last_frame_callee_info->valid |
|
192 & WindowsFrameInfo::VALID_PARAMETER_SIZE)) { |
|
193 last_frame_callee_parameter_size = |
|
194 last_frame_callee_info->parameter_size; |
|
195 } |
|
196 } |
|
197 |
|
198 // Set up the dictionary for the PostfixEvaluator. %ebp and %esp are used |
|
199 // in each program string, and their previous values are known, so set them |
|
200 // here. |
|
201 PostfixEvaluator<uint32_t>::DictionaryType dictionary; |
|
202 // Provide the current register values. |
|
203 dictionary.set(ustr__ZSebp(), last_frame->context.ebp); |
|
204 dictionary.set(ustr__ZSesp(), last_frame->context.esp); |
|
205 // Provide constants from the debug info for last_frame and its callee. |
|
206 // .cbCalleeParams is a Breakpad extension that allows us to use the |
|
207 // PostfixEvaluator engine when certain types of debugging information |
|
208 // are present without having to write the constants into the program |
|
209 // string as literals. |
|
210 dictionary.set(ustr__ZDcbCalleeParams(), last_frame_callee_parameter_size); |
|
211 dictionary.set(ustr__ZDcbSavedRegs(), last_frame_info->saved_register_size); |
|
212 dictionary.set(ustr__ZDcbLocals(), last_frame_info->local_size); |
|
213 |
|
214 uint32_t raSearchStart = last_frame->context.esp + |
|
215 last_frame_callee_parameter_size + |
|
216 last_frame_info->local_size + |
|
217 last_frame_info->saved_register_size; |
|
218 |
|
219 uint32_t raSearchStartOld = raSearchStart; |
|
220 uint32_t found = 0; // dummy value |
|
221 // Scan up to three words above the calculated search value, in case |
|
222 // the stack was aligned to a quadword boundary. |
|
223 if (ScanForReturnAddress(raSearchStart, &raSearchStart, &found, 3) && |
|
224 last_frame->trust == StackFrame::FRAME_TRUST_CONTEXT && |
|
225 last_frame->windows_frame_info != NULL && |
|
226 last_frame_info->type_ == WindowsFrameInfo::STACK_INFO_FPO && |
|
227 raSearchStartOld == raSearchStart && |
|
228 found == last_frame->context.eip) { |
|
229 // The context frame represents an FPO-optimized Windows system call. |
|
230 // On the top of the stack we have a pointer to the current instruction. |
|
231 // This means that the callee has returned but the return address is still |
|
232 // on the top of the stack which is very atypical situaltion. |
|
233 // Skip one slot from the stack and do another scan in order to get the |
|
234 // actual return address. |
|
235 raSearchStart += 4; |
|
236 ScanForReturnAddress(raSearchStart, &raSearchStart, &found, 3); |
|
237 } |
|
238 |
|
239 // The difference between raSearch and raSearchStart is unknown, |
|
240 // but making them the same seems to work well in practice. |
|
241 dictionary.set(ustr__ZDraSearchStart(), raSearchStart); |
|
242 dictionary.set(ustr__ZDraSearch(), raSearchStart); |
|
243 |
|
244 dictionary.set(ustr__ZDcbParams(), last_frame_info->parameter_size); |
|
245 |
|
246 // Decide what type of program string to use. The program string is in |
|
247 // postfix notation and will be passed to PostfixEvaluator::Evaluate. |
|
248 // Given the dictionary and the program string, it is possible to compute |
|
249 // the return address and the values of other registers in the calling |
|
250 // function. Because of bugs described below, the stack may need to be |
|
251 // scanned for these values. The results of program string evaluation |
|
252 // will be used to determine whether to scan for better values. |
|
253 string program_string; |
|
254 bool recover_ebp = true; |
|
255 |
|
256 trust = StackFrame::FRAME_TRUST_CFI; |
|
257 if (!last_frame_info->program_string.empty()) { |
|
258 // The FPO data has its own program string, which will tell us how to |
|
259 // get to the caller frame, and may even fill in the values of |
|
260 // nonvolatile registers and provide pointers to local variables and |
|
261 // parameters. In some cases, particularly with program strings that use |
|
262 // .raSearchStart, the stack may need to be scanned afterward. |
|
263 program_string = last_frame_info->program_string; |
|
264 } else if (last_frame_info->allocates_base_pointer) { |
|
265 // The function corresponding to the last frame doesn't use the frame |
|
266 // pointer for conventional purposes, but it does allocate a new |
|
267 // frame pointer and use it for its own purposes. Its callee's |
|
268 // information is still accessed relative to %esp, and the previous |
|
269 // value of %ebp can be recovered from a location in its stack frame, |
|
270 // within the saved-register area. |
|
271 // |
|
272 // Functions that fall into this category use the %ebp register for |
|
273 // a purpose other than the frame pointer. They restore the caller's |
|
274 // %ebp before returning. These functions create their stack frame |
|
275 // after a CALL by decrementing the stack pointer in an amount |
|
276 // sufficient to store local variables, and then PUSHing saved |
|
277 // registers onto the stack. Arguments to a callee function, if any, |
|
278 // are PUSHed after that. Walking up to the caller, therefore, |
|
279 // can be done solely with calculations relative to the stack pointer |
|
280 // (%esp). The return address is recovered from the memory location |
|
281 // above the known sizes of the callee's parameters, saved registers, |
|
282 // and locals. The caller's stack pointer (the value of %esp when |
|
283 // the caller executed CALL) is the location immediately above the |
|
284 // saved return address. The saved value of %ebp to be restored for |
|
285 // the caller is at a known location in the saved-register area of |
|
286 // the stack frame. |
|
287 // |
|
288 // For this type of frame, MSVC 14 (from Visual Studio 8/2005) in |
|
289 // link-time code generation mode (/LTCG and /GL) can generate erroneous |
|
290 // debugging data. The reported size of saved registers can be 0, |
|
291 // which is clearly an error because these frames must, at the very |
|
292 // least, save %ebp. For this reason, in addition to those given above |
|
293 // about the use of .raSearchStart, the stack may need to be scanned |
|
294 // for a better return address and a better frame pointer after the |
|
295 // program string is evaluated. |
|
296 // |
|
297 // %eip_new = *(%esp_old + callee_params + saved_regs + locals) |
|
298 // %ebp_new = *(%esp_old + callee_params + saved_regs - 8) |
|
299 // %esp_new = %esp_old + callee_params + saved_regs + locals + 4 |
|
300 program_string = "$eip .raSearchStart ^ = " |
|
301 "$ebp $esp .cbCalleeParams + .cbSavedRegs + 8 - ^ = " |
|
302 "$esp .raSearchStart 4 + ="; |
|
303 } else { |
|
304 // The function corresponding to the last frame doesn't use %ebp at |
|
305 // all. The callee frame is located relative to %esp. |
|
306 // |
|
307 // The called procedure's instruction pointer and stack pointer are |
|
308 // recovered in the same way as the case above, except that no |
|
309 // frame pointer (%ebp) is used at all, so it is not saved anywhere |
|
310 // in the callee's stack frame and does not need to be recovered. |
|
311 // Because %ebp wasn't used in the callee, whatever value it has |
|
312 // is the value that it had in the caller, so it can be carried |
|
313 // straight through without bringing its validity into question. |
|
314 // |
|
315 // Because of the use of .raSearchStart, the stack will possibly be |
|
316 // examined to locate a better return address after program string |
|
317 // evaluation. The stack will not be examined to locate a saved |
|
318 // %ebp value, because these frames do not save (or use) %ebp. |
|
319 // |
|
320 // %eip_new = *(%esp_old + callee_params + saved_regs + locals) |
|
321 // %esp_new = %esp_old + callee_params + saved_regs + locals + 4 |
|
322 // %ebp_new = %ebp_old |
|
323 program_string = "$eip .raSearchStart ^ = " |
|
324 "$esp .raSearchStart 4 + ="; |
|
325 recover_ebp = false; |
|
326 } |
|
327 |
|
328 // Now crank it out, making sure that the program string set at least the |
|
329 // two required variables. |
|
330 PostfixEvaluator<uint32_t> evaluator = |
|
331 PostfixEvaluator<uint32_t>(&dictionary, memory_); |
|
332 PostfixEvaluator<uint32_t>::DictionaryValidityType dictionary_validity; |
|
333 if (!evaluator.Evaluate(program_string, &dictionary_validity) || |
|
334 !dictionary_validity.have(ustr__ZSeip()) || |
|
335 !dictionary_validity.have(ustr__ZSesp())) { |
|
336 // Program string evaluation failed. It may be that %eip is not somewhere |
|
337 // with stack frame info, and %ebp is pointing to non-stack memory, so |
|
338 // our evaluation couldn't succeed. We'll scan the stack for a return |
|
339 // address. This can happen if the stack is in a module for which |
|
340 // we don't have symbols, and that module is compiled without a |
|
341 // frame pointer. |
|
342 uint32_t location_start = last_frame->context.esp; |
|
343 uint32_t location, eip; |
|
344 if (!stack_scan_allowed |
|
345 || !ScanForReturnAddress(location_start, &location, &eip)) { |
|
346 // if we can't find an instruction pointer even with stack scanning, |
|
347 // give up. |
|
348 return NULL; |
|
349 } |
|
350 |
|
351 // This seems like a reasonable return address. Since program string |
|
352 // evaluation failed, use it and set %esp to the location above the |
|
353 // one where the return address was found. |
|
354 dictionary.set(ustr__ZSeip(), eip); |
|
355 dictionary.set(ustr__ZSesp(), location + 4); |
|
356 trust = StackFrame::FRAME_TRUST_SCAN; |
|
357 } |
|
358 |
|
359 // Since this stack frame did not use %ebp in a traditional way, |
|
360 // locating the return address isn't entirely deterministic. In that |
|
361 // case, the stack can be scanned to locate the return address. |
|
362 // |
|
363 // However, if program string evaluation resulted in both %eip and |
|
364 // %ebp values of 0, trust that the end of the stack has been |
|
365 // reached and don't scan for anything else. |
|
366 if (dictionary.get(ustr__ZSeip()) != 0 || |
|
367 dictionary.get(ustr__ZSebp()) != 0) { |
|
368 int offset = 0; |
|
369 |
|
370 // This scan can only be done if a CodeModules object is available, to |
|
371 // check that candidate return addresses are in fact inside a module. |
|
372 // |
|
373 // TODO(mmentovai): This ignores dynamically-generated code. One possible |
|
374 // solution is to check the minidump's memory map to see if the candidate |
|
375 // %eip value comes from a mapped executable page, although this would |
|
376 // require dumps that contain MINIDUMP_MEMORY_INFO, which the Breakpad |
|
377 // client doesn't currently write (it would need to call MiniDumpWriteDump |
|
378 // with the MiniDumpWithFullMemoryInfo type bit set). Even given this |
|
379 // ability, older OSes (pre-XP SP2) and CPUs (pre-P4) don't enforce |
|
380 // an independent execute privilege on memory pages. |
|
381 |
|
382 uint32_t eip = dictionary.get(ustr__ZSeip()); |
|
383 if (modules_ && !modules_->GetModuleForAddress(eip)) { |
|
384 // The instruction pointer at .raSearchStart was invalid, so start |
|
385 // looking one 32-bit word above that location. |
|
386 uint32_t location_start = dictionary.get(ustr__ZDraSearchStart()) + 4; |
|
387 uint32_t location; |
|
388 if (stack_scan_allowed |
|
389 && ScanForReturnAddress(location_start, &location, &eip)) { |
|
390 // This is a better return address that what program string |
|
391 // evaluation found. Use it, and set %esp to the location above the |
|
392 // one where the return address was found. |
|
393 dictionary.set(ustr__ZSeip(), eip); |
|
394 dictionary.set(ustr__ZSesp(), location + 4); |
|
395 offset = location - location_start; |
|
396 trust = StackFrame::FRAME_TRUST_CFI_SCAN; |
|
397 } |
|
398 } |
|
399 |
|
400 if (recover_ebp) { |
|
401 // When trying to recover the previous value of the frame pointer (%ebp), |
|
402 // start looking at the lowest possible address in the saved-register |
|
403 // area, and look at the entire saved register area, increased by the |
|
404 // size of |offset| to account for additional data that may be on the |
|
405 // stack. The scan is performed from the highest possible address to |
|
406 // the lowest, because the expectation is that the function's prolog |
|
407 // would have saved %ebp early. |
|
408 uint32_t ebp = dictionary.get(ustr__ZSebp()); |
|
409 |
|
410 // When a scan for return address is used, it is possible to skip one or |
|
411 // more frames (when return address is not in a known module). One |
|
412 // indication for skipped frames is when the value of %ebp is lower than |
|
413 // the location of the return address on the stack |
|
414 bool has_skipped_frames = |
|
415 (trust != StackFrame::FRAME_TRUST_CFI && ebp <= raSearchStart + offset); |
|
416 |
|
417 uint32_t value; // throwaway variable to check pointer validity |
|
418 if (has_skipped_frames || !memory_->GetMemoryAtAddress(ebp, &value)) { |
|
419 int fp_search_bytes = last_frame_info->saved_register_size + offset; |
|
420 uint32_t location_end = last_frame->context.esp + |
|
421 last_frame_callee_parameter_size; |
|
422 |
|
423 for (uint32_t location = location_end + fp_search_bytes; |
|
424 location >= location_end; |
|
425 location -= 4) { |
|
426 if (!memory_->GetMemoryAtAddress(location, &ebp)) |
|
427 break; |
|
428 |
|
429 if (memory_->GetMemoryAtAddress(ebp, &value)) { |
|
430 // The candidate value is a pointer to the same memory region |
|
431 // (the stack). Prefer it as a recovered %ebp result. |
|
432 dictionary.set(ustr__ZSebp(), ebp); |
|
433 break; |
|
434 } |
|
435 } |
|
436 } |
|
437 } |
|
438 } |
|
439 |
|
440 // Create a new stack frame (ownership will be transferred to the caller) |
|
441 // and fill it in. |
|
442 StackFrameX86* frame = new StackFrameX86(); |
|
443 |
|
444 frame->trust = trust; |
|
445 frame->context = last_frame->context; |
|
446 frame->context.eip = dictionary.get(ustr__ZSeip()); |
|
447 frame->context.esp = dictionary.get(ustr__ZSesp()); |
|
448 frame->context.ebp = dictionary.get(ustr__ZSebp()); |
|
449 frame->context_validity = StackFrameX86::CONTEXT_VALID_EIP | |
|
450 StackFrameX86::CONTEXT_VALID_ESP | |
|
451 StackFrameX86::CONTEXT_VALID_EBP; |
|
452 |
|
453 // These are nonvolatile (callee-save) registers, and the program string |
|
454 // may have filled them in. |
|
455 if (dictionary_validity.have(ustr__ZSebx())) { |
|
456 frame->context.ebx = dictionary.get(ustr__ZSebx()); |
|
457 frame->context_validity |= StackFrameX86::CONTEXT_VALID_EBX; |
|
458 } |
|
459 if (dictionary_validity.have(ustr__ZSesi())) { |
|
460 frame->context.esi = dictionary.get(ustr__ZSesi()); |
|
461 frame->context_validity |= StackFrameX86::CONTEXT_VALID_ESI; |
|
462 } |
|
463 if (dictionary_validity.have(ustr__ZSedi())) { |
|
464 frame->context.edi = dictionary.get(ustr__ZSedi()); |
|
465 frame->context_validity |= StackFrameX86::CONTEXT_VALID_EDI; |
|
466 } |
|
467 |
|
468 return frame; |
|
469 } |
|
470 |
|
471 StackFrameX86* StackwalkerX86::GetCallerByCFIFrameInfo( |
|
472 const vector<StackFrame*> &frames, |
|
473 CFIFrameInfo* cfi_frame_info) { |
|
474 StackFrameX86* last_frame = static_cast<StackFrameX86*>(frames.back()); |
|
475 last_frame->cfi_frame_info = cfi_frame_info; |
|
476 |
|
477 scoped_ptr<StackFrameX86> frame(new StackFrameX86()); |
|
478 if (!cfi_walker_ |
|
479 .FindCallerRegisters(*memory_, *cfi_frame_info, |
|
480 last_frame->context, last_frame->context_validity, |
|
481 &frame->context, &frame->context_validity)) |
|
482 return NULL; |
|
483 |
|
484 // Make sure we recovered all the essentials. |
|
485 static const int essentials = (StackFrameX86::CONTEXT_VALID_EIP |
|
486 | StackFrameX86::CONTEXT_VALID_ESP |
|
487 | StackFrameX86::CONTEXT_VALID_EBP); |
|
488 if ((frame->context_validity & essentials) != essentials) |
|
489 return NULL; |
|
490 |
|
491 frame->trust = StackFrame::FRAME_TRUST_CFI; |
|
492 |
|
493 return frame.release(); |
|
494 } |
|
495 |
|
496 StackFrameX86* StackwalkerX86::GetCallerByEBPAtBase( |
|
497 const vector<StackFrame*> &frames, |
|
498 bool stack_scan_allowed) { |
|
499 StackFrame::FrameTrust trust; |
|
500 StackFrameX86* last_frame = static_cast<StackFrameX86*>(frames.back()); |
|
501 uint32_t last_esp = last_frame->context.esp; |
|
502 uint32_t last_ebp = last_frame->context.ebp; |
|
503 |
|
504 // Assume that the standard %ebp-using x86 calling convention is in |
|
505 // use. |
|
506 // |
|
507 // The typical x86 calling convention, when frame pointers are present, |
|
508 // is for the calling procedure to use CALL, which pushes the return |
|
509 // address onto the stack and sets the instruction pointer (%eip) to |
|
510 // the entry point of the called routine. The called routine then |
|
511 // PUSHes the calling routine's frame pointer (%ebp) onto the stack |
|
512 // before copying the stack pointer (%esp) to the frame pointer (%ebp). |
|
513 // Therefore, the calling procedure's frame pointer is always available |
|
514 // by dereferencing the called procedure's frame pointer, and the return |
|
515 // address is always available at the memory location immediately above |
|
516 // the address pointed to by the called procedure's frame pointer. The |
|
517 // calling procedure's stack pointer (%esp) is 8 higher than the value |
|
518 // of the called procedure's frame pointer at the time the calling |
|
519 // procedure made the CALL: 4 bytes for the return address pushed by the |
|
520 // CALL itself, and 4 bytes for the callee's PUSH of the caller's frame |
|
521 // pointer. |
|
522 // |
|
523 // %eip_new = *(%ebp_old + 4) |
|
524 // %esp_new = %ebp_old + 8 |
|
525 // %ebp_new = *(%ebp_old) |
|
526 |
|
527 uint32_t caller_eip, caller_esp, caller_ebp; |
|
528 |
|
529 if (memory_->GetMemoryAtAddress(last_ebp + 4, &caller_eip) && |
|
530 memory_->GetMemoryAtAddress(last_ebp, &caller_ebp)) { |
|
531 caller_esp = last_ebp + 8; |
|
532 trust = StackFrame::FRAME_TRUST_FP; |
|
533 } else { |
|
534 // We couldn't read the memory %ebp refers to. It may be that %ebp |
|
535 // is pointing to non-stack memory. We'll scan the stack for a |
|
536 // return address. This can happen if last_frame is executing code |
|
537 // for a module for which we don't have symbols, and that module |
|
538 // is compiled without a frame pointer. |
|
539 if (!stack_scan_allowed |
|
540 || !ScanForReturnAddress(last_esp, &caller_esp, &caller_eip)) { |
|
541 // if we can't find an instruction pointer even with stack scanning, |
|
542 // give up. |
|
543 return NULL; |
|
544 } |
|
545 |
|
546 // ScanForReturnAddress found a reasonable return address. Advance |
|
547 // %esp to the location above the one where the return address was |
|
548 // found. Assume that %ebp is unchanged. |
|
549 caller_esp += 4; |
|
550 caller_ebp = last_ebp; |
|
551 |
|
552 trust = StackFrame::FRAME_TRUST_SCAN; |
|
553 } |
|
554 |
|
555 // Create a new stack frame (ownership will be transferred to the caller) |
|
556 // and fill it in. |
|
557 StackFrameX86* frame = new StackFrameX86(); |
|
558 |
|
559 frame->trust = trust; |
|
560 frame->context = last_frame->context; |
|
561 frame->context.eip = caller_eip; |
|
562 frame->context.esp = caller_esp; |
|
563 frame->context.ebp = caller_ebp; |
|
564 frame->context_validity = StackFrameX86::CONTEXT_VALID_EIP | |
|
565 StackFrameX86::CONTEXT_VALID_ESP | |
|
566 StackFrameX86::CONTEXT_VALID_EBP; |
|
567 |
|
568 return frame; |
|
569 } |
|
570 |
|
571 StackFrame* StackwalkerX86::GetCallerFrame(const CallStack* stack, |
|
572 bool stack_scan_allowed) { |
|
573 if (!memory_ || !stack) { |
|
574 BPLOG(ERROR) << "Can't get caller frame without memory or stack"; |
|
575 return NULL; |
|
576 } |
|
577 |
|
578 const vector<StackFrame*> &frames = *stack->frames(); |
|
579 StackFrameX86* last_frame = static_cast<StackFrameX86*>(frames.back()); |
|
580 scoped_ptr<StackFrameX86> new_frame; |
|
581 |
|
582 // If the resolver has Windows stack walking information, use that. |
|
583 WindowsFrameInfo* windows_frame_info |
|
584 = frame_symbolizer_->FindWindowsFrameInfo(last_frame); |
|
585 if (windows_frame_info) |
|
586 new_frame.reset(GetCallerByWindowsFrameInfo(frames, windows_frame_info, |
|
587 stack_scan_allowed)); |
|
588 |
|
589 // If the resolver has DWARF CFI information, use that. |
|
590 if (!new_frame.get()) { |
|
591 CFIFrameInfo* cfi_frame_info = |
|
592 frame_symbolizer_->FindCFIFrameInfo(last_frame); |
|
593 if (cfi_frame_info) |
|
594 new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info)); |
|
595 } |
|
596 |
|
597 // Otherwise, hope that the program was using a traditional frame structure. |
|
598 if (!new_frame.get()) |
|
599 new_frame.reset(GetCallerByEBPAtBase(frames, stack_scan_allowed)); |
|
600 |
|
601 // If nothing worked, tell the caller. |
|
602 if (!new_frame.get()) |
|
603 return NULL; |
|
604 |
|
605 // Treat an instruction address of 0 as end-of-stack. |
|
606 if (new_frame->context.eip == 0) |
|
607 return NULL; |
|
608 |
|
609 // If the new stack pointer is at a lower address than the old, then |
|
610 // that's clearly incorrect. Treat this as end-of-stack to enforce |
|
611 // progress and avoid infinite loops. |
|
612 if (new_frame->context.esp <= last_frame->context.esp) |
|
613 return NULL; |
|
614 |
|
615 // new_frame->context.eip is the return address, which is the instruction |
|
616 // after the CALL that caused us to arrive at the callee. Set |
|
617 // new_frame->instruction to one less than that, so it points within the |
|
618 // CALL instruction. See StackFrame::instruction for details, and |
|
619 // StackFrameAMD64::ReturnAddress. |
|
620 new_frame->instruction = new_frame->context.eip - 1; |
|
621 |
|
622 return new_frame.release(); |
|
623 } |
|
624 |
|
625 } // namespace google_breakpad |