PageRenderTime 123ms CodeModel.GetById 93ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/thirdparty/breakpad/google_breakpad/processor/stackwalker.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 203 lines | 76 code | 31 blank | 96 comment | 4 complexity | 076bf1548b8e3161bc75a6cbc0d9e8c0 MD5 | raw file
  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
 50namespace google_breakpad {
 51
 52class CallStack;
 53class MinidumpContext;
 54class SourceLineResolverInterface;
 55struct StackFrame;
 56class SymbolSupplier;
 57struct SystemInfo;
 58
 59using std::set;
 60
 61
 62class 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  template<typename InstructionType>
112  bool ScanForReturnAddress(InstructionType location_start,
113                            InstructionType *location_found,
114                            InstructionType *ip_found) {
115    const int kRASearchWords = 30;
116    return ScanForReturnAddress(location_start, location_found, ip_found,
117                                kRASearchWords);
118  }
119
120  // Scan the stack starting at location_start, looking for an address
121  // that looks like a valid instruction pointer. Addresses must
122  // 1) be contained in the current stack memory
123  // 2) pass the checks in InstructionAddressSeemsValid
124  //
125  // Returns true if a valid-looking instruction pointer was found.
126  // When returning true, sets location_found to the address at which
127  // the value was found, and ip_found to the value contained at that
128  // location in memory.
129  template<typename InstructionType>
130  bool ScanForReturnAddress(InstructionType location_start,
131                            InstructionType *location_found,
132                            InstructionType *ip_found,
133                            int searchwords) {
134    for (InstructionType location = location_start;
135         location <= location_start + searchwords * sizeof(InstructionType);
136         location += sizeof(InstructionType)) {
137      InstructionType ip;
138      if (!memory_->GetMemoryAtAddress(location, &ip))
139        break;
140
141      if (modules_ && modules_->GetModuleForAddress(ip) &&
142          InstructionAddressSeemsValid(ip)) {
143
144        *ip_found = ip;
145        *location_found = location;
146        return true;
147      }
148    }
149    // nothing found
150    return false;
151  }
152
153  // Information about the system that produced the minidump.  Subclasses
154  // and the SymbolSupplier may find this information useful.
155  const SystemInfo *system_info_;
156
157  // The stack memory to walk.  Subclasses will require this region to
158  // get information from the stack.
159  MemoryRegion *memory_;
160
161  // A list of modules, for populating each StackFrame's module information.
162  // This field is optional and may be NULL.
163  const CodeModules *modules_;
164
165 protected:
166  // The SourceLineResolver implementation.
167  SourceLineResolverInterface *resolver_;
168
169 private:
170  // Obtains the context frame, the innermost called procedure in a stack
171  // trace.  Returns NULL on failure.  GetContextFrame allocates a new
172  // StackFrame (or StackFrame subclass), ownership of which is taken by
173  // the caller.
174  virtual StackFrame* GetContextFrame() = 0;
175
176  // Obtains a caller frame.  Each call to GetCallerFrame should return the
177  // frame that called the last frame returned by GetContextFrame or
178  // GetCallerFrame.  To aid this purpose, stack contains the CallStack
179  // made of frames that have already been walked.  GetCallerFrame should
180  // return NULL on failure or when there are no more caller frames (when
181  // the end of the stack has been reached).  GetCallerFrame allocates a new
182  // StackFrame (or StackFrame subclass), ownership of which is taken by
183  // the caller.
184  virtual StackFrame* GetCallerFrame(const CallStack *stack) = 0;
185
186  // The optional SymbolSupplier for resolving source line info.
187  SymbolSupplier *supplier_;
188
189  // A list of modules that we haven't found symbols for.  We track
190  // this in order to avoid repeatedly looking them up again within
191  // one minidump.
192  set<std::string> no_symbol_modules_;
193
194  // The maximum number of frames Stackwalker will walk through.
195  // This defaults to 1024 to prevent infinite loops.
196  static u_int32_t max_frames_;
197};
198
199
200}  // namespace google_breakpad
201
202
203#endif  // GOOGLE_BREAKPAD_PROCESSOR_STACKWALKER_H__