stackwalker_x86.h revision 6d3a825dbf5b924c2e754309b3008e462af1d8d2
1// -*- mode: c++ -*-
2
3// Copyright (c) 2010 Google Inc.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32// stackwalker_x86.h: x86-specific stackwalker.
33//
34// Provides stack frames given x86 register context and a memory region
35// corresponding to an x86 stack.
36//
37// Author: Mark Mentovai
38
39
40#ifndef PROCESSOR_STACKWALKER_X86_H__
41#define PROCESSOR_STACKWALKER_X86_H__
42
43
44#include "google_breakpad/common/breakpad_types.h"
45#include "google_breakpad/common/minidump_format.h"
46#include "google_breakpad/processor/stackwalker.h"
47#include "google_breakpad/processor/stack_frame_cpu.h"
48#include "src/processor/cfi_frame_info.h"
49
50namespace google_breakpad {
51
52class CodeModules;
53
54
55class StackwalkerX86 : public Stackwalker {
56 public:
57  // context is an x86 context object that gives access to x86-specific
58  // register state corresponding to the innermost called frame to be
59  // included in the stack.  The other arguments are passed directly through
60  // to the base Stackwalker constructor.
61  StackwalkerX86(const SystemInfo *system_info,
62                 const MDRawContextX86 *context,
63                 MemoryRegion *memory,
64                 const CodeModules *modules,
65                 SymbolSupplier *supplier,
66                 SourceLineResolverInterface *resolver);
67
68 private:
69  // A STACK CFI-driven frame walker for the X86.
70  typedef SimpleCFIWalker<u_int32_t, MDRawContextX86> CFIWalker;
71
72  // Implementation of Stackwalker, using x86 context (%ebp, %esp, %eip) and
73  // stack conventions (saved %ebp at [%ebp], saved %eip at 4[%ebp], or
74  // alternate conventions as guided by any WindowsFrameInfo available for the
75  // code in question.).
76  virtual StackFrame *GetContextFrame();
77  virtual StackFrame *GetCallerFrame(const CallStack *stack);
78
79  // Use windows_frame_info (derived from STACK WIN and FUNC records)
80  // to construct the frame that called frames.back(). The caller
81  // takes ownership of the returned frame. Return NULL on failure.
82  StackFrameX86 *GetCallerByWindowsFrameInfo(
83      const vector<StackFrame*> &frames,
84      WindowsFrameInfo *windows_frame_info);
85
86  // Use cfi_frame_info (derived from STACK CFI records) to construct
87  // the frame that called frames.back(). The caller takes ownership
88  // of the returned frame. Return NULL on failure.
89  StackFrameX86 *GetCallerByCFIFrameInfo(const vector<StackFrame*> &frames,
90                                         CFIFrameInfo *cfi_frame_info);
91
92  // Assuming a traditional frame layout --- where the caller's %ebp
93  // has been pushed just after the return address and the callee's
94  // %ebp points to the saved %ebp --- construct the frame that called
95  // frames.back(). The caller takes ownership of the returned frame.
96  // Return NULL on failure.
97  StackFrameX86 *GetCallerByEBPAtBase(const vector<StackFrame*> &frames);
98
99  // Scan the stack starting at location_start, looking for an address
100  // that looks like a valid instruction pointer. Addresses must
101  // 1) be contained in the current stack memory
102  // 2) pass the checks in Stackwalker::InstructionAddressSeemsValid
103  //
104  // Returns true if a valid-looking instruction pointer was found.
105  // When returning true, sets location_found to the address at which
106  // the value was found, and eip_found to the value contained at that
107  // location in memory.
108  bool ScanForReturnAddress(u_int32_t location_start,
109                            u_int32_t *location_found,
110                            u_int32_t *eip_found);
111
112  // Stores the CPU context corresponding to the innermost stack frame to
113  // be returned by GetContextFrame.
114  const MDRawContextX86 *context_;
115
116  // Our register map, for cfi_walker_.
117  static const CFIWalker::RegisterSet cfi_register_map_[];
118
119  // Our CFI frame walker.
120  const CFIWalker cfi_walker_;
121};
122
123
124}  // namespace google_breakpad
125
126
127#endif  // PROCESSOR_STACKWALKER_X86_H__
128