michael@0: // Copyright (c) 2010 Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // stackwalker_ppc.cc: ppc-specific stackwalker. michael@0: // michael@0: // See stackwalker_ppc.h for documentation. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: michael@0: #include "processor/stackwalker_ppc.h" michael@0: #include "google_breakpad/processor/call_stack.h" michael@0: #include "google_breakpad/processor/memory_region.h" michael@0: #include "google_breakpad/processor/stack_frame_cpu.h" michael@0: #include "common/logging.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: michael@0: StackwalkerPPC::StackwalkerPPC(const SystemInfo* system_info, michael@0: const MDRawContextPPC* context, michael@0: MemoryRegion* memory, michael@0: const CodeModules* modules, michael@0: StackFrameSymbolizer* resolver_helper) michael@0: : Stackwalker(system_info, memory, modules, resolver_helper), michael@0: context_(context) { michael@0: if (memory_ && memory_->GetBase() + memory_->GetSize() - 1 > 0xffffffff) { michael@0: // This implementation only covers 32-bit ppc CPUs. The limits of the michael@0: // supplied stack are invalid. Mark memory_ = NULL, which will cause michael@0: // stackwalking to fail. michael@0: BPLOG(ERROR) << "Memory out of range for stackwalking: " << michael@0: HexString(memory_->GetBase()) << "+" << michael@0: HexString(memory_->GetSize()); michael@0: memory_ = NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: StackFrame* StackwalkerPPC::GetContextFrame() { michael@0: if (!context_) { michael@0: BPLOG(ERROR) << "Can't get context frame without context"; michael@0: return NULL; michael@0: } michael@0: michael@0: StackFramePPC* frame = new StackFramePPC(); michael@0: michael@0: // The instruction pointer is stored directly in a register, so pull it michael@0: // straight out of the CPU context structure. michael@0: frame->context = *context_; michael@0: frame->context_validity = StackFramePPC::CONTEXT_VALID_ALL; michael@0: frame->trust = StackFrame::FRAME_TRUST_CONTEXT; michael@0: frame->instruction = frame->context.srr0; michael@0: michael@0: return frame; michael@0: } michael@0: michael@0: michael@0: StackFrame* StackwalkerPPC::GetCallerFrame(const CallStack* stack, michael@0: bool stack_scan_allowed) { michael@0: if (!memory_ || !stack) { michael@0: BPLOG(ERROR) << "Can't get caller frame without memory or stack"; michael@0: return NULL; michael@0: } michael@0: michael@0: // The instruction pointers for previous frames are saved on the stack. michael@0: // The typical ppc calling convention is for the called procedure to store michael@0: // its return address in the calling procedure's stack frame at 8(%r1), michael@0: // and to allocate its own stack frame by decrementing %r1 (the stack michael@0: // pointer) and saving the old value of %r1 at 0(%r1). Because the ppc has michael@0: // no hardware stack, there is no distinction between the stack pointer and michael@0: // frame pointer, and what is typically thought of as the frame pointer on michael@0: // an x86 is usually referred to as the stack pointer on a ppc. michael@0: michael@0: StackFramePPC* last_frame = static_cast( michael@0: stack->frames()->back()); michael@0: michael@0: // A caller frame must reside higher in memory than its callee frames. michael@0: // Anything else is an error, or an indication that we've reached the michael@0: // end of the stack. michael@0: uint32_t stack_pointer; michael@0: if (!memory_->GetMemoryAtAddress(last_frame->context.gpr[1], michael@0: &stack_pointer) || michael@0: stack_pointer <= last_frame->context.gpr[1]) { michael@0: return NULL; michael@0: } michael@0: michael@0: // Mac OS X/Darwin gives 1 as the return address from the bottom-most michael@0: // frame in a stack (a thread's entry point). I haven't found any michael@0: // documentation on this, but 0 or 1 would be bogus return addresses, michael@0: // so check for them here and return false (end of stack) when they're michael@0: // hit to avoid having a phantom frame. michael@0: uint32_t instruction; michael@0: if (!memory_->GetMemoryAtAddress(stack_pointer + 8, &instruction) || michael@0: instruction <= 1) { michael@0: return NULL; michael@0: } michael@0: michael@0: StackFramePPC* frame = new StackFramePPC(); michael@0: michael@0: frame->context = last_frame->context; michael@0: frame->context.srr0 = instruction; michael@0: frame->context.gpr[1] = stack_pointer; michael@0: frame->context_validity = StackFramePPC::CONTEXT_VALID_SRR0 | michael@0: StackFramePPC::CONTEXT_VALID_GPR1; michael@0: frame->trust = StackFrame::FRAME_TRUST_FP; michael@0: michael@0: // frame->context.srr0 is the return address, which is one instruction michael@0: // past the branch that caused us to arrive at the callee. Set michael@0: // frame_ppc->instruction to four less than that. Since all ppc michael@0: // instructions are 4 bytes wide, this is the address of the branch michael@0: // instruction. This allows source line information to match up with the michael@0: // line that contains a function call. Callers that require the exact michael@0: // return address value may access the context.srr0 field of StackFramePPC. michael@0: frame->instruction = frame->context.srr0 - 4; michael@0: michael@0: return frame; michael@0: } michael@0: michael@0: michael@0: } // namespace google_breakpad