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_arm.cc: arm-specific stackwalker. michael@0: // michael@0: // See stackwalker_arm.h for documentation. michael@0: // michael@0: // Author: Mark Mentovai, Ted Mielczarek, Jim Blandy michael@0: michael@0: #include michael@0: michael@0: #include "common/scoped_ptr.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/source_line_resolver_interface.h" michael@0: #include "google_breakpad/processor/stack_frame_cpu.h" michael@0: #include "processor/cfi_frame_info.h" michael@0: #include "common/logging.h" michael@0: #include "processor/stackwalker_arm.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: michael@0: StackwalkerARM::StackwalkerARM(const SystemInfo* system_info, michael@0: const MDRawContextARM* context, michael@0: int fp_register, 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), fp_register_(fp_register), michael@0: context_frame_validity_(StackFrameARM::CONTEXT_VALID_ALL) { } michael@0: michael@0: michael@0: StackFrame* StackwalkerARM::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: StackFrameARM* frame = new StackFrameARM(); michael@0: michael@0: // The instruction pointer is stored directly in a register (r15), so pull it michael@0: // straight out of the CPU context structure. michael@0: frame->context = *context_; michael@0: frame->context_validity = context_frame_validity_; michael@0: frame->trust = StackFrame::FRAME_TRUST_CONTEXT; michael@0: frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC]; michael@0: michael@0: return frame; michael@0: } michael@0: michael@0: StackFrameARM* StackwalkerARM::GetCallerByCFIFrameInfo( michael@0: const vector &frames, michael@0: CFIFrameInfo* cfi_frame_info) { michael@0: StackFrameARM* last_frame = static_cast(frames.back()); michael@0: michael@0: static const UniqueString *register_names[] = { michael@0: ToUniqueString("r0"), ToUniqueString("r1"), michael@0: ToUniqueString("r2"), ToUniqueString("r3"), michael@0: ToUniqueString("r4"), ToUniqueString("r5"), michael@0: ToUniqueString("r6"), ToUniqueString("r7"), michael@0: ToUniqueString("r8"), ToUniqueString("r9"), michael@0: ToUniqueString("r10"), ToUniqueString("r11"), michael@0: ToUniqueString("r12"), ToUniqueString("sp"), michael@0: ToUniqueString("lr"), ToUniqueString("pc"), michael@0: ToUniqueString("f0"), ToUniqueString("f1"), michael@0: ToUniqueString("f2"), ToUniqueString("f3"), michael@0: ToUniqueString("f4"), ToUniqueString("f5"), michael@0: ToUniqueString("f6"), ToUniqueString("f7"), michael@0: ToUniqueString("fps"), ToUniqueString("cpsr"), michael@0: NULL michael@0: }; michael@0: michael@0: // Populate a dictionary with the valid register values in last_frame. michael@0: CFIFrameInfo::RegisterValueMap callee_registers; michael@0: for (int i = 0; register_names[i]; i++) michael@0: if (last_frame->context_validity & StackFrameARM::RegisterValidFlag(i)) michael@0: callee_registers.set(register_names[i], last_frame->context.iregs[i]); michael@0: michael@0: // Use the STACK CFI data to recover the caller's register values. michael@0: CFIFrameInfo::RegisterValueMap caller_registers; michael@0: if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_, michael@0: &caller_registers)) michael@0: return NULL; michael@0: michael@0: // Construct a new stack frame given the values the CFI recovered. michael@0: scoped_ptr frame(new StackFrameARM()); michael@0: for (int i = 0; register_names[i]; i++) { michael@0: bool found = false; michael@0: uint32_t v = caller_registers.get(&found, register_names[i]); michael@0: if (found) { michael@0: // We recovered the value of this register; fill the context with the michael@0: // value from caller_registers. michael@0: frame->context_validity |= StackFrameARM::RegisterValidFlag(i); michael@0: frame->context.iregs[i] = v; michael@0: } else if (4 <= i && i <= 11 && (last_frame->context_validity & michael@0: StackFrameARM::RegisterValidFlag(i))) { michael@0: // If the STACK CFI data doesn't mention some callee-saves register, and michael@0: // it is valid in the callee, assume the callee has not yet changed it. michael@0: // Registers r4 through r11 are callee-saves, according to the Procedure michael@0: // Call Standard for the ARM Architecture, which the Linux ABI follows. michael@0: frame->context_validity |= StackFrameARM::RegisterValidFlag(i); michael@0: frame->context.iregs[i] = last_frame->context.iregs[i]; michael@0: } michael@0: } michael@0: // If the CFI doesn't recover the PC explicitly, then use .ra. michael@0: if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_PC)) { michael@0: bool found = false; michael@0: uint32_t v = caller_registers.get(&found, ustr__ZDra()); michael@0: if (found) { michael@0: if (fp_register_ == -1) { michael@0: frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = v; michael@0: } else { michael@0: // The CFI updated the link register and not the program counter. michael@0: // Handle getting the program counter from the link register. michael@0: frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC; michael@0: frame->context_validity |= StackFrameARM::CONTEXT_VALID_LR; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = v; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = michael@0: last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR]; michael@0: } michael@0: } michael@0: } michael@0: // If the CFI doesn't recover the SP explicitly, then use .cfa. michael@0: if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_SP)) { michael@0: bool found = false; michael@0: uint32_t v = caller_registers.get(&found, ustr__ZDcfa()); michael@0: if (found) { michael@0: frame->context_validity |= StackFrameARM::CONTEXT_VALID_SP; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = v; michael@0: } michael@0: } michael@0: michael@0: // If we didn't recover the PC and the SP, then the frame isn't very useful. michael@0: static const int essentials = (StackFrameARM::CONTEXT_VALID_SP michael@0: | StackFrameARM::CONTEXT_VALID_PC); michael@0: if ((frame->context_validity & essentials) != essentials) michael@0: return NULL; michael@0: michael@0: frame->trust = StackFrame::FRAME_TRUST_CFI; michael@0: return frame.release(); michael@0: } michael@0: michael@0: StackFrameARM* StackwalkerARM::GetCallerByStackScan( michael@0: const vector &frames) { michael@0: StackFrameARM* last_frame = static_cast(frames.back()); michael@0: uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP]; michael@0: uint32_t caller_sp, caller_pc; michael@0: michael@0: // When searching for the caller of the context frame, michael@0: // allow the scanner to look farther down the stack. michael@0: const int kRASearchWords = frames.size() == 1 ? michael@0: Stackwalker::kRASearchWords * 4 : michael@0: Stackwalker::kRASearchWords; michael@0: michael@0: if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, michael@0: kRASearchWords)) { michael@0: // No plausible return address was found. michael@0: return NULL; michael@0: } michael@0: michael@0: // ScanForReturnAddress found a reasonable return address. Advance michael@0: // %sp to the location above the one where the return address was michael@0: // found. michael@0: caller_sp += 4; michael@0: michael@0: // Create a new stack frame (ownership will be transferred to the caller) michael@0: // and fill it in. michael@0: StackFrameARM* frame = new StackFrameARM(); michael@0: michael@0: frame->trust = StackFrame::FRAME_TRUST_SCAN; michael@0: frame->context = last_frame->context; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = caller_pc; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp; michael@0: frame->context_validity = StackFrameARM::CONTEXT_VALID_PC | michael@0: StackFrameARM::CONTEXT_VALID_SP; michael@0: michael@0: return frame; michael@0: } michael@0: michael@0: StackFrameARM* StackwalkerARM::GetCallerByFramePointer( michael@0: const vector &frames) { michael@0: StackFrameARM* last_frame = static_cast(frames.back()); michael@0: michael@0: if (!(last_frame->context_validity & michael@0: StackFrameARM::RegisterValidFlag(fp_register_))) { michael@0: return NULL; michael@0: } michael@0: michael@0: uint32_t last_fp = last_frame->context.iregs[fp_register_]; michael@0: michael@0: uint32_t caller_fp = 0; michael@0: if (last_fp && !memory_->GetMemoryAtAddress(last_fp, &caller_fp)) { michael@0: BPLOG(ERROR) << "Unable to read caller_fp from last_fp: 0x" michael@0: << std::hex << last_fp; michael@0: return NULL; michael@0: } michael@0: michael@0: uint32_t caller_lr = 0; michael@0: if (last_fp && !memory_->GetMemoryAtAddress(last_fp + 4, &caller_lr)) { michael@0: BPLOG(ERROR) << "Unable to read caller_lr from last_fp + 4: 0x" michael@0: << std::hex << (last_fp + 4); michael@0: return NULL; michael@0: } michael@0: michael@0: uint32_t caller_sp = last_fp ? last_fp + 8 : michael@0: last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP]; michael@0: michael@0: // Create a new stack frame (ownership will be transferred to the caller) michael@0: // and fill it in. michael@0: StackFrameARM* frame = new StackFrameARM(); michael@0: michael@0: frame->trust = StackFrame::FRAME_TRUST_FP; michael@0: frame->context = last_frame->context; michael@0: frame->context.iregs[fp_register_] = caller_fp; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = michael@0: last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR]; michael@0: frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = caller_lr; michael@0: frame->context_validity = StackFrameARM::CONTEXT_VALID_PC | michael@0: StackFrameARM::CONTEXT_VALID_LR | michael@0: StackFrameARM::RegisterValidFlag(fp_register_) | michael@0: StackFrameARM::CONTEXT_VALID_SP; michael@0: return frame; michael@0: } michael@0: michael@0: StackFrame* StackwalkerARM::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: const vector &frames = *stack->frames(); michael@0: StackFrameARM* last_frame = static_cast(frames.back()); michael@0: scoped_ptr frame; michael@0: michael@0: // See if there is DWARF call frame information covering this address. michael@0: scoped_ptr cfi_frame_info( michael@0: frame_symbolizer_->FindCFIFrameInfo(last_frame)); michael@0: if (cfi_frame_info.get()) michael@0: frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get())); michael@0: michael@0: // If CFI failed, or there wasn't CFI available, fall back michael@0: // to frame pointer, if this is configured. michael@0: if (fp_register_ >= 0 && !frame.get()) michael@0: frame.reset(GetCallerByFramePointer(frames)); michael@0: michael@0: // If everuthing failed, fall back to stack scanning. michael@0: if (stack_scan_allowed && !frame.get()) michael@0: frame.reset(GetCallerByStackScan(frames)); michael@0: michael@0: // If nothing worked, tell the caller. michael@0: if (!frame.get()) michael@0: return NULL; michael@0: michael@0: michael@0: // An instruction address of zero marks the end of the stack. michael@0: if (frame->context.iregs[MD_CONTEXT_ARM_REG_PC] == 0) michael@0: return NULL; michael@0: michael@0: // If the new stack pointer is at a lower address than the old, then michael@0: // that's clearly incorrect. Treat this as end-of-stack to enforce michael@0: // progress and avoid infinite loops. michael@0: if (frame->context.iregs[MD_CONTEXT_ARM_REG_SP] michael@0: < last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP]) michael@0: return NULL; michael@0: michael@0: // The new frame's context's PC is the return address, which is one michael@0: // instruction past the instruction that caused us to arrive at the michael@0: // callee. Set new_frame->instruction to one less than the PC. This won't michael@0: // reference the beginning of the call instruction, but it's at least michael@0: // within it, which is sufficient to get the source line information to michael@0: // match up with the line that contains the function call. Callers that michael@0: // require the exact return address value may access michael@0: // frame->context.iregs[MD_CONTEXT_ARM_REG_PC]. michael@0: frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC] - 2; michael@0: michael@0: return frame.release(); michael@0: } michael@0: michael@0: michael@0: } // namespace google_breakpad