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.cc: Generic stackwalker. michael@0: // michael@0: // See stackwalker.h for documentation. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #include "google_breakpad/processor/stackwalker.h" 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/code_module.h" michael@0: #include "google_breakpad/processor/code_modules.h" michael@0: #include "google_breakpad/processor/minidump.h" michael@0: #include "google_breakpad/processor/stack_frame.h" michael@0: #include "google_breakpad/processor/stack_frame_symbolizer.h" michael@0: #include "google_breakpad/processor/system_info.h" michael@0: #include "processor/linked_ptr.h" michael@0: #include "common/logging.h" michael@0: #include "processor/stackwalker_ppc.h" michael@0: #include "processor/stackwalker_sparc.h" michael@0: #include "processor/stackwalker_x86.h" michael@0: #include "processor/stackwalker_amd64.h" michael@0: #include "processor/stackwalker_arm.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: const int Stackwalker::kRASearchWords = 30; michael@0: michael@0: uint32_t Stackwalker::max_frames_ = 1024; michael@0: bool Stackwalker::max_frames_set_ = false; michael@0: michael@0: uint32_t Stackwalker::max_frames_scanned_ = 1024; michael@0: michael@0: Stackwalker::Stackwalker(const SystemInfo* system_info, michael@0: MemoryRegion* memory, michael@0: const CodeModules* modules, michael@0: StackFrameSymbolizer* frame_symbolizer) michael@0: : system_info_(system_info), michael@0: memory_(memory), michael@0: modules_(modules), michael@0: frame_symbolizer_(frame_symbolizer) { michael@0: assert(frame_symbolizer_); michael@0: } michael@0: michael@0: michael@0: bool Stackwalker::Walk(CallStack* stack, michael@0: vector* modules_without_symbols) { michael@0: BPLOG_IF(ERROR, !stack) << "Stackwalker::Walk requires |stack|"; michael@0: assert(stack); michael@0: stack->Clear(); michael@0: michael@0: BPLOG_IF(ERROR, !modules_without_symbols) << "Stackwalker::Walk requires " michael@0: << "|modules_without_symbols|"; michael@0: assert(modules_without_symbols); michael@0: michael@0: // Begin with the context frame, and keep getting callers until there are michael@0: // no more. michael@0: michael@0: // Keep track of the number of scanned or otherwise dubious frames seen michael@0: // so far, as the caller may have set a limit. michael@0: uint32_t n_scanned_frames = 0; michael@0: michael@0: // Take ownership of the pointer returned by GetContextFrame. michael@0: scoped_ptr frame(GetContextFrame()); michael@0: michael@0: while (frame.get()) { michael@0: // frame already contains a good frame with properly set instruction and michael@0: // frame_pointer fields. The frame structure comes from either the michael@0: // context frame (above) or a caller frame (below). michael@0: michael@0: // Resolve the module information, if a module map was provided. michael@0: StackFrameSymbolizer::SymbolizerResult symbolizer_result = michael@0: frame_symbolizer_->FillSourceLineInfo(modules_, system_info_, michael@0: frame.get()); michael@0: if (symbolizer_result == StackFrameSymbolizer::kInterrupt) { michael@0: BPLOG(INFO) << "Stack walk is interrupted."; michael@0: return false; michael@0: } michael@0: michael@0: // Keep track of modules that have no symbols. michael@0: if (symbolizer_result == StackFrameSymbolizer::kError && michael@0: frame->module != NULL) { michael@0: bool found = false; michael@0: vector::iterator iter; michael@0: for (iter = modules_without_symbols->begin(); michael@0: iter != modules_without_symbols->end(); michael@0: ++iter) { michael@0: if (*iter == frame->module) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: if (!found) { michael@0: BPLOG(INFO) << "Couldn't load symbols for: " michael@0: << frame->module->debug_file() << "|" michael@0: << frame->module->debug_identifier(); michael@0: modules_without_symbols->push_back(frame->module); michael@0: } michael@0: } michael@0: michael@0: // Keep track of the number of dubious frames so far. michael@0: switch (frame.get()->trust) { michael@0: case StackFrame::FRAME_TRUST_NONE: michael@0: case StackFrame::FRAME_TRUST_SCAN: michael@0: case StackFrame::FRAME_TRUST_CFI_SCAN: michael@0: n_scanned_frames++; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: // Add the frame to the call stack. Relinquish the ownership claim michael@0: // over the frame, because the stack now owns it. michael@0: stack->frames_.push_back(frame.release()); michael@0: if (stack->frames_.size() > max_frames_) { michael@0: // Only emit an error message in the case where the limit that we michael@0: // reached is the default limit, not set by the user. michael@0: if (!max_frames_set_) michael@0: BPLOG(ERROR) << "The stack is over " << max_frames_ << " frames."; michael@0: break; michael@0: } michael@0: michael@0: // Get the next frame and take ownership. michael@0: bool stack_scan_allowed = n_scanned_frames < max_frames_scanned_; michael@0: frame.reset(GetCallerFrame(stack, stack_scan_allowed)); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // static michael@0: Stackwalker* Stackwalker::StackwalkerForCPU( michael@0: const SystemInfo* system_info, michael@0: MinidumpContext* context, michael@0: MemoryRegion* memory, michael@0: const CodeModules* modules, michael@0: StackFrameSymbolizer* frame_symbolizer) { michael@0: if (!context) { michael@0: BPLOG(ERROR) << "Can't choose a stackwalker implementation without context"; michael@0: return NULL; michael@0: } michael@0: michael@0: Stackwalker* cpu_stackwalker = NULL; michael@0: michael@0: uint32_t cpu = context->GetContextCPU(); michael@0: switch (cpu) { michael@0: case MD_CONTEXT_X86: michael@0: cpu_stackwalker = new StackwalkerX86(system_info, michael@0: context->GetContextX86(), michael@0: memory, modules, frame_symbolizer); michael@0: break; michael@0: michael@0: case MD_CONTEXT_PPC: michael@0: cpu_stackwalker = new StackwalkerPPC(system_info, michael@0: context->GetContextPPC(), michael@0: memory, modules, frame_symbolizer); michael@0: break; michael@0: michael@0: case MD_CONTEXT_AMD64: michael@0: cpu_stackwalker = new StackwalkerAMD64(system_info, michael@0: context->GetContextAMD64(), michael@0: memory, modules, frame_symbolizer); michael@0: break; michael@0: michael@0: case MD_CONTEXT_SPARC: michael@0: cpu_stackwalker = new StackwalkerSPARC(system_info, michael@0: context->GetContextSPARC(), michael@0: memory, modules, frame_symbolizer); michael@0: break; michael@0: michael@0: case MD_CONTEXT_ARM: michael@0: int fp_register = -1; michael@0: if (system_info->os_short == "ios") michael@0: fp_register = MD_CONTEXT_ARM_REG_IOS_FP; michael@0: cpu_stackwalker = new StackwalkerARM(system_info, michael@0: context->GetContextARM(), michael@0: fp_register, memory, modules, michael@0: frame_symbolizer); michael@0: break; michael@0: } michael@0: michael@0: BPLOG_IF(ERROR, !cpu_stackwalker) << "Unknown CPU type " << HexString(cpu) << michael@0: ", can't choose a stackwalker " michael@0: "implementation"; michael@0: return cpu_stackwalker; michael@0: } michael@0: michael@0: bool Stackwalker::InstructionAddressSeemsValid(uint64_t address) { michael@0: StackFrame frame; michael@0: frame.instruction = address; michael@0: StackFrameSymbolizer::SymbolizerResult symbolizer_result = michael@0: frame_symbolizer_->FillSourceLineInfo(modules_, system_info_, &frame); michael@0: michael@0: if (!frame.module) { michael@0: // not inside any loaded module michael@0: return false; michael@0: } michael@0: michael@0: if (!frame_symbolizer_->HasImplementation()) { michael@0: // No valid implementation to symbolize stack frame, but the address is michael@0: // within a known module. michael@0: return true; michael@0: } michael@0: michael@0: if (symbolizer_result != StackFrameSymbolizer::kNoError) { michael@0: // Some error occurred during symbolization, but the address is within a michael@0: // known module michael@0: return true; michael@0: } michael@0: michael@0: return !frame.function_name.empty(); michael@0: } michael@0: michael@0: } // namespace google_breakpad