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_amd64.h: amd64-specific stackwalker. 31 // 32 // Provides stack frames given amd64 register context and a memory region 33 // corresponding to a amd64 stack. 34 // 35 // Author: Mark Mentovai, Ted Mielczarek 36 37 38 #ifndef PROCESSOR_STACKWALKER_AMD64_H__ 39 #define PROCESSOR_STACKWALKER_AMD64_H__ 40 41 #include <vector> 42 43 #include "google_breakpad/common/breakpad_types.h" 44 #include "google_breakpad/common/minidump_format.h" 45 #include "google_breakpad/processor/stackwalker.h" 46 #include "google_breakpad/processor/stack_frame_cpu.h" 47 #include "processor/cfi_frame_info.h" 48 49 namespace google_breakpad { 50 51 class CodeModules; 52 53 class StackwalkerAMD64 : public Stackwalker { 54 public: 55 // context is a amd64 context object that gives access to amd64-specific 56 // register state corresponding to the innermost called frame to be 57 // included in the stack. The other arguments are passed directly through 58 // to the base Stackwalker constructor. 59 StackwalkerAMD64(const SystemInfo* system_info, 60 const MDRawContextAMD64* context, 61 MemoryRegion* memory, 62 const CodeModules* modules, 63 StackFrameSymbolizer* frame_symbolizer); 64 65 private: 66 // A STACK CFI-driven frame walker for the AMD64 67 typedef SimpleCFIWalker<uint64_t, MDRawContextAMD64> CFIWalker; 68 69 // Implementation of Stackwalker, using amd64 context (stack pointer in %rsp, 70 // stack base in %rbp) and stack conventions (saved stack pointer at 0(%rbp)) 71 virtual StackFrame* GetContextFrame(); 72 virtual StackFrame* GetCallerFrame(const CallStack* stack, 73 bool stack_scan_allowed); 74 75 // Use cfi_frame_info (derived from STACK CFI records) to construct 76 // the frame that called frames.back(). The caller takes ownership 77 // of the returned frame. Return NULL on failure. 78 StackFrameAMD64* GetCallerByCFIFrameInfo(const vector<StackFrame*> &frames, 79 CFIFrameInfo* cfi_frame_info); 80 81 // Checks whether end-of-stack is reached. An instruction address of 0 is an 82 // end-of-stack marker. If the stack pointer of the caller is at a lower 83 // address than the stack pointer of the callee, then that's clearly incorrect 84 // and it is treated as end-of-stack to enforce progress and avoid infinite 85 // loops. 86 bool IsEndOfStack(uint64_t caller_rip, uint64_t caller_rsp, 87 uint64_t callee_rsp); 88 89 // Assumes a traditional frame layout where the frame pointer has not been 90 // omitted. The expectation is that caller's %rbp is pushed to the stack 91 // after the return address of the callee, and that the callee's %rsp can 92 // be used to find the pushed %rbp. 93 // Caller owns the returned frame object. Returns NULL on failure. 94 StackFrameAMD64* GetCallerByFramePointerRecovery( 95 const vector<StackFrame*>& frames); 96 97 // Scan the stack for plausible return addresses. The caller takes ownership 98 // of the returned frame. Return NULL on failure. 99 StackFrameAMD64* GetCallerByStackScan(const vector<StackFrame*> &frames); 100 101 // Stores the CPU context corresponding to the innermost stack frame to 102 // be returned by GetContextFrame. 103 const MDRawContextAMD64* context_; 104 105 // Our register map, for cfi_walker_. 106 static const CFIWalker::RegisterSet cfi_register_map_[]; 107 108 // Our CFI frame walker. 109 const CFIWalker cfi_walker_; 110 }; 111 112 113 } // namespace google_breakpad 114 115 116 #endif // PROCESSOR_STACKWALKER_AMD64_H__ 117