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_sparc.cc: sparc-specific stackwalker. michael@0: // michael@0: // See stackwalker_sparc.h for documentation. michael@0: // michael@0: // Author: Michael Shang michael@0: michael@0: 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: #include "processor/stackwalker_sparc.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: michael@0: StackwalkerSPARC::StackwalkerSPARC(const SystemInfo* system_info, michael@0: const MDRawContextSPARC* 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: } michael@0: michael@0: michael@0: StackFrame* StackwalkerSPARC::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: StackFrameSPARC* frame = new StackFrameSPARC(); 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 = StackFrameSPARC::CONTEXT_VALID_ALL; michael@0: frame->trust = StackFrame::FRAME_TRUST_CONTEXT; michael@0: frame->instruction = frame->context.pc; michael@0: michael@0: return frame; michael@0: } michael@0: michael@0: michael@0: StackFrame* StackwalkerSPARC::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: StackFrameSPARC* last_frame = static_cast( michael@0: stack->frames()->back()); michael@0: michael@0: // new: caller michael@0: // old: callee michael@0: // %fp, %i6 and g_r[30] is the same, see minidump_format.h michael@0: // %sp, %o6 and g_r[14] is the same, see minidump_format.h michael@0: // %sp_new = %fp_old michael@0: // %fp_new = *(%fp_old + 32 + 32 - 8), where the callee's %i6 michael@0: // %pc_new = *(%fp_old + 32 + 32 - 4) + 8 michael@0: // which is callee's %i7 plus 8 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: uint64_t stack_pointer = last_frame->context.g_r[30]; michael@0: if (stack_pointer <= last_frame->context.g_r[14]) { michael@0: return NULL; michael@0: } michael@0: michael@0: uint32_t instruction; michael@0: if (!memory_->GetMemoryAtAddress(stack_pointer + 60, michael@0: &instruction) || instruction <= 1) { michael@0: return NULL; michael@0: } michael@0: michael@0: uint32_t stack_base; michael@0: if (!memory_->GetMemoryAtAddress(stack_pointer + 56, michael@0: &stack_base) || stack_base <= 1) { michael@0: return NULL; michael@0: } michael@0: michael@0: StackFrameSPARC* frame = new StackFrameSPARC(); michael@0: michael@0: frame->context = last_frame->context; michael@0: frame->context.g_r[14] = stack_pointer; michael@0: frame->context.g_r[30] = stack_base; michael@0: michael@0: // frame->context.pc is the return address, which is 2 instruction michael@0: // past the branch that caused us to arrive at the callee, which are michael@0: // a CALL instruction then a NOP instruction. michael@0: // frame_ppc->instruction to 8 less than that. Since all sparc 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 %i7/g_r[31] field of StackFrameSPARC. michael@0: frame->context.pc = instruction + 8; michael@0: frame->instruction = instruction; michael@0: frame->context_validity = StackFrameSPARC::CONTEXT_VALID_PC | michael@0: StackFrameSPARC::CONTEXT_VALID_SP | michael@0: StackFrameSPARC::CONTEXT_VALID_FP; michael@0: frame->trust = StackFrame::FRAME_TRUST_FP; michael@0: michael@0: return frame; michael@0: } michael@0: michael@0: michael@0: } // namespace google_breakpad