1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/stackwalker_ppc.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +// Copyright (c) 2010 Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// stackwalker_ppc.cc: ppc-specific stackwalker. 1.34 +// 1.35 +// See stackwalker_ppc.h for documentation. 1.36 +// 1.37 +// Author: Mark Mentovai 1.38 + 1.39 + 1.40 +#include "processor/stackwalker_ppc.h" 1.41 +#include "google_breakpad/processor/call_stack.h" 1.42 +#include "google_breakpad/processor/memory_region.h" 1.43 +#include "google_breakpad/processor/stack_frame_cpu.h" 1.44 +#include "common/logging.h" 1.45 + 1.46 +namespace google_breakpad { 1.47 + 1.48 + 1.49 +StackwalkerPPC::StackwalkerPPC(const SystemInfo* system_info, 1.50 + const MDRawContextPPC* context, 1.51 + MemoryRegion* memory, 1.52 + const CodeModules* modules, 1.53 + StackFrameSymbolizer* resolver_helper) 1.54 + : Stackwalker(system_info, memory, modules, resolver_helper), 1.55 + context_(context) { 1.56 + if (memory_ && memory_->GetBase() + memory_->GetSize() - 1 > 0xffffffff) { 1.57 + // This implementation only covers 32-bit ppc CPUs. The limits of the 1.58 + // supplied stack are invalid. Mark memory_ = NULL, which will cause 1.59 + // stackwalking to fail. 1.60 + BPLOG(ERROR) << "Memory out of range for stackwalking: " << 1.61 + HexString(memory_->GetBase()) << "+" << 1.62 + HexString(memory_->GetSize()); 1.63 + memory_ = NULL; 1.64 + } 1.65 +} 1.66 + 1.67 + 1.68 +StackFrame* StackwalkerPPC::GetContextFrame() { 1.69 + if (!context_) { 1.70 + BPLOG(ERROR) << "Can't get context frame without context"; 1.71 + return NULL; 1.72 + } 1.73 + 1.74 + StackFramePPC* frame = new StackFramePPC(); 1.75 + 1.76 + // The instruction pointer is stored directly in a register, so pull it 1.77 + // straight out of the CPU context structure. 1.78 + frame->context = *context_; 1.79 + frame->context_validity = StackFramePPC::CONTEXT_VALID_ALL; 1.80 + frame->trust = StackFrame::FRAME_TRUST_CONTEXT; 1.81 + frame->instruction = frame->context.srr0; 1.82 + 1.83 + return frame; 1.84 +} 1.85 + 1.86 + 1.87 +StackFrame* StackwalkerPPC::GetCallerFrame(const CallStack* stack, 1.88 + bool stack_scan_allowed) { 1.89 + if (!memory_ || !stack) { 1.90 + BPLOG(ERROR) << "Can't get caller frame without memory or stack"; 1.91 + return NULL; 1.92 + } 1.93 + 1.94 + // The instruction pointers for previous frames are saved on the stack. 1.95 + // The typical ppc calling convention is for the called procedure to store 1.96 + // its return address in the calling procedure's stack frame at 8(%r1), 1.97 + // and to allocate its own stack frame by decrementing %r1 (the stack 1.98 + // pointer) and saving the old value of %r1 at 0(%r1). Because the ppc has 1.99 + // no hardware stack, there is no distinction between the stack pointer and 1.100 + // frame pointer, and what is typically thought of as the frame pointer on 1.101 + // an x86 is usually referred to as the stack pointer on a ppc. 1.102 + 1.103 + StackFramePPC* last_frame = static_cast<StackFramePPC*>( 1.104 + stack->frames()->back()); 1.105 + 1.106 + // A caller frame must reside higher in memory than its callee frames. 1.107 + // Anything else is an error, or an indication that we've reached the 1.108 + // end of the stack. 1.109 + uint32_t stack_pointer; 1.110 + if (!memory_->GetMemoryAtAddress(last_frame->context.gpr[1], 1.111 + &stack_pointer) || 1.112 + stack_pointer <= last_frame->context.gpr[1]) { 1.113 + return NULL; 1.114 + } 1.115 + 1.116 + // Mac OS X/Darwin gives 1 as the return address from the bottom-most 1.117 + // frame in a stack (a thread's entry point). I haven't found any 1.118 + // documentation on this, but 0 or 1 would be bogus return addresses, 1.119 + // so check for them here and return false (end of stack) when they're 1.120 + // hit to avoid having a phantom frame. 1.121 + uint32_t instruction; 1.122 + if (!memory_->GetMemoryAtAddress(stack_pointer + 8, &instruction) || 1.123 + instruction <= 1) { 1.124 + return NULL; 1.125 + } 1.126 + 1.127 + StackFramePPC* frame = new StackFramePPC(); 1.128 + 1.129 + frame->context = last_frame->context; 1.130 + frame->context.srr0 = instruction; 1.131 + frame->context.gpr[1] = stack_pointer; 1.132 + frame->context_validity = StackFramePPC::CONTEXT_VALID_SRR0 | 1.133 + StackFramePPC::CONTEXT_VALID_GPR1; 1.134 + frame->trust = StackFrame::FRAME_TRUST_FP; 1.135 + 1.136 + // frame->context.srr0 is the return address, which is one instruction 1.137 + // past the branch that caused us to arrive at the callee. Set 1.138 + // frame_ppc->instruction to four less than that. Since all ppc 1.139 + // instructions are 4 bytes wide, this is the address of the branch 1.140 + // instruction. This allows source line information to match up with the 1.141 + // line that contains a function call. Callers that require the exact 1.142 + // return address value may access the context.srr0 field of StackFramePPC. 1.143 + frame->instruction = frame->context.srr0 - 4; 1.144 + 1.145 + return frame; 1.146 +} 1.147 + 1.148 + 1.149 +} // namespace google_breakpad