Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2010 Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // stackwalker.cc: Generic stackwalker. |
michael@0 | 31 | // |
michael@0 | 32 | // See stackwalker.h for documentation. |
michael@0 | 33 | // |
michael@0 | 34 | // Author: Mark Mentovai |
michael@0 | 35 | |
michael@0 | 36 | #include "google_breakpad/processor/stackwalker.h" |
michael@0 | 37 | |
michael@0 | 38 | #include <assert.h> |
michael@0 | 39 | |
michael@0 | 40 | #include "common/scoped_ptr.h" |
michael@0 | 41 | #include "google_breakpad/processor/call_stack.h" |
michael@0 | 42 | #include "google_breakpad/processor/code_module.h" |
michael@0 | 43 | #include "google_breakpad/processor/code_modules.h" |
michael@0 | 44 | #include "google_breakpad/processor/minidump.h" |
michael@0 | 45 | #include "google_breakpad/processor/stack_frame.h" |
michael@0 | 46 | #include "google_breakpad/processor/stack_frame_symbolizer.h" |
michael@0 | 47 | #include "google_breakpad/processor/system_info.h" |
michael@0 | 48 | #include "processor/linked_ptr.h" |
michael@0 | 49 | #include "common/logging.h" |
michael@0 | 50 | #include "processor/stackwalker_ppc.h" |
michael@0 | 51 | #include "processor/stackwalker_sparc.h" |
michael@0 | 52 | #include "processor/stackwalker_x86.h" |
michael@0 | 53 | #include "processor/stackwalker_amd64.h" |
michael@0 | 54 | #include "processor/stackwalker_arm.h" |
michael@0 | 55 | |
michael@0 | 56 | namespace google_breakpad { |
michael@0 | 57 | |
michael@0 | 58 | const int Stackwalker::kRASearchWords = 30; |
michael@0 | 59 | |
michael@0 | 60 | uint32_t Stackwalker::max_frames_ = 1024; |
michael@0 | 61 | bool Stackwalker::max_frames_set_ = false; |
michael@0 | 62 | |
michael@0 | 63 | uint32_t Stackwalker::max_frames_scanned_ = 1024; |
michael@0 | 64 | |
michael@0 | 65 | Stackwalker::Stackwalker(const SystemInfo* system_info, |
michael@0 | 66 | MemoryRegion* memory, |
michael@0 | 67 | const CodeModules* modules, |
michael@0 | 68 | StackFrameSymbolizer* frame_symbolizer) |
michael@0 | 69 | : system_info_(system_info), |
michael@0 | 70 | memory_(memory), |
michael@0 | 71 | modules_(modules), |
michael@0 | 72 | frame_symbolizer_(frame_symbolizer) { |
michael@0 | 73 | assert(frame_symbolizer_); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | |
michael@0 | 77 | bool Stackwalker::Walk(CallStack* stack, |
michael@0 | 78 | vector<const CodeModule*>* modules_without_symbols) { |
michael@0 | 79 | BPLOG_IF(ERROR, !stack) << "Stackwalker::Walk requires |stack|"; |
michael@0 | 80 | assert(stack); |
michael@0 | 81 | stack->Clear(); |
michael@0 | 82 | |
michael@0 | 83 | BPLOG_IF(ERROR, !modules_without_symbols) << "Stackwalker::Walk requires " |
michael@0 | 84 | << "|modules_without_symbols|"; |
michael@0 | 85 | assert(modules_without_symbols); |
michael@0 | 86 | |
michael@0 | 87 | // Begin with the context frame, and keep getting callers until there are |
michael@0 | 88 | // no more. |
michael@0 | 89 | |
michael@0 | 90 | // Keep track of the number of scanned or otherwise dubious frames seen |
michael@0 | 91 | // so far, as the caller may have set a limit. |
michael@0 | 92 | uint32_t n_scanned_frames = 0; |
michael@0 | 93 | |
michael@0 | 94 | // Take ownership of the pointer returned by GetContextFrame. |
michael@0 | 95 | scoped_ptr<StackFrame> frame(GetContextFrame()); |
michael@0 | 96 | |
michael@0 | 97 | while (frame.get()) { |
michael@0 | 98 | // frame already contains a good frame with properly set instruction and |
michael@0 | 99 | // frame_pointer fields. The frame structure comes from either the |
michael@0 | 100 | // context frame (above) or a caller frame (below). |
michael@0 | 101 | |
michael@0 | 102 | // Resolve the module information, if a module map was provided. |
michael@0 | 103 | StackFrameSymbolizer::SymbolizerResult symbolizer_result = |
michael@0 | 104 | frame_symbolizer_->FillSourceLineInfo(modules_, system_info_, |
michael@0 | 105 | frame.get()); |
michael@0 | 106 | if (symbolizer_result == StackFrameSymbolizer::kInterrupt) { |
michael@0 | 107 | BPLOG(INFO) << "Stack walk is interrupted."; |
michael@0 | 108 | return false; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // Keep track of modules that have no symbols. |
michael@0 | 112 | if (symbolizer_result == StackFrameSymbolizer::kError && |
michael@0 | 113 | frame->module != NULL) { |
michael@0 | 114 | bool found = false; |
michael@0 | 115 | vector<const CodeModule*>::iterator iter; |
michael@0 | 116 | for (iter = modules_without_symbols->begin(); |
michael@0 | 117 | iter != modules_without_symbols->end(); |
michael@0 | 118 | ++iter) { |
michael@0 | 119 | if (*iter == frame->module) { |
michael@0 | 120 | found = true; |
michael@0 | 121 | break; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | if (!found) { |
michael@0 | 125 | BPLOG(INFO) << "Couldn't load symbols for: " |
michael@0 | 126 | << frame->module->debug_file() << "|" |
michael@0 | 127 | << frame->module->debug_identifier(); |
michael@0 | 128 | modules_without_symbols->push_back(frame->module); |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Keep track of the number of dubious frames so far. |
michael@0 | 133 | switch (frame.get()->trust) { |
michael@0 | 134 | case StackFrame::FRAME_TRUST_NONE: |
michael@0 | 135 | case StackFrame::FRAME_TRUST_SCAN: |
michael@0 | 136 | case StackFrame::FRAME_TRUST_CFI_SCAN: |
michael@0 | 137 | n_scanned_frames++; |
michael@0 | 138 | break; |
michael@0 | 139 | default: |
michael@0 | 140 | break; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | // Add the frame to the call stack. Relinquish the ownership claim |
michael@0 | 144 | // over the frame, because the stack now owns it. |
michael@0 | 145 | stack->frames_.push_back(frame.release()); |
michael@0 | 146 | if (stack->frames_.size() > max_frames_) { |
michael@0 | 147 | // Only emit an error message in the case where the limit that we |
michael@0 | 148 | // reached is the default limit, not set by the user. |
michael@0 | 149 | if (!max_frames_set_) |
michael@0 | 150 | BPLOG(ERROR) << "The stack is over " << max_frames_ << " frames."; |
michael@0 | 151 | break; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | // Get the next frame and take ownership. |
michael@0 | 155 | bool stack_scan_allowed = n_scanned_frames < max_frames_scanned_; |
michael@0 | 156 | frame.reset(GetCallerFrame(stack, stack_scan_allowed)); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | return true; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | |
michael@0 | 163 | // static |
michael@0 | 164 | Stackwalker* Stackwalker::StackwalkerForCPU( |
michael@0 | 165 | const SystemInfo* system_info, |
michael@0 | 166 | MinidumpContext* context, |
michael@0 | 167 | MemoryRegion* memory, |
michael@0 | 168 | const CodeModules* modules, |
michael@0 | 169 | StackFrameSymbolizer* frame_symbolizer) { |
michael@0 | 170 | if (!context) { |
michael@0 | 171 | BPLOG(ERROR) << "Can't choose a stackwalker implementation without context"; |
michael@0 | 172 | return NULL; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | Stackwalker* cpu_stackwalker = NULL; |
michael@0 | 176 | |
michael@0 | 177 | uint32_t cpu = context->GetContextCPU(); |
michael@0 | 178 | switch (cpu) { |
michael@0 | 179 | case MD_CONTEXT_X86: |
michael@0 | 180 | cpu_stackwalker = new StackwalkerX86(system_info, |
michael@0 | 181 | context->GetContextX86(), |
michael@0 | 182 | memory, modules, frame_symbolizer); |
michael@0 | 183 | break; |
michael@0 | 184 | |
michael@0 | 185 | case MD_CONTEXT_PPC: |
michael@0 | 186 | cpu_stackwalker = new StackwalkerPPC(system_info, |
michael@0 | 187 | context->GetContextPPC(), |
michael@0 | 188 | memory, modules, frame_symbolizer); |
michael@0 | 189 | break; |
michael@0 | 190 | |
michael@0 | 191 | case MD_CONTEXT_AMD64: |
michael@0 | 192 | cpu_stackwalker = new StackwalkerAMD64(system_info, |
michael@0 | 193 | context->GetContextAMD64(), |
michael@0 | 194 | memory, modules, frame_symbolizer); |
michael@0 | 195 | break; |
michael@0 | 196 | |
michael@0 | 197 | case MD_CONTEXT_SPARC: |
michael@0 | 198 | cpu_stackwalker = new StackwalkerSPARC(system_info, |
michael@0 | 199 | context->GetContextSPARC(), |
michael@0 | 200 | memory, modules, frame_symbolizer); |
michael@0 | 201 | break; |
michael@0 | 202 | |
michael@0 | 203 | case MD_CONTEXT_ARM: |
michael@0 | 204 | int fp_register = -1; |
michael@0 | 205 | if (system_info->os_short == "ios") |
michael@0 | 206 | fp_register = MD_CONTEXT_ARM_REG_IOS_FP; |
michael@0 | 207 | cpu_stackwalker = new StackwalkerARM(system_info, |
michael@0 | 208 | context->GetContextARM(), |
michael@0 | 209 | fp_register, memory, modules, |
michael@0 | 210 | frame_symbolizer); |
michael@0 | 211 | break; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | BPLOG_IF(ERROR, !cpu_stackwalker) << "Unknown CPU type " << HexString(cpu) << |
michael@0 | 215 | ", can't choose a stackwalker " |
michael@0 | 216 | "implementation"; |
michael@0 | 217 | return cpu_stackwalker; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | bool Stackwalker::InstructionAddressSeemsValid(uint64_t address) { |
michael@0 | 221 | StackFrame frame; |
michael@0 | 222 | frame.instruction = address; |
michael@0 | 223 | StackFrameSymbolizer::SymbolizerResult symbolizer_result = |
michael@0 | 224 | frame_symbolizer_->FillSourceLineInfo(modules_, system_info_, &frame); |
michael@0 | 225 | |
michael@0 | 226 | if (!frame.module) { |
michael@0 | 227 | // not inside any loaded module |
michael@0 | 228 | return false; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | if (!frame_symbolizer_->HasImplementation()) { |
michael@0 | 232 | // No valid implementation to symbolize stack frame, but the address is |
michael@0 | 233 | // within a known module. |
michael@0 | 234 | return true; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | if (symbolizer_result != StackFrameSymbolizer::kNoError) { |
michael@0 | 238 | // Some error occurred during symbolization, but the address is within a |
michael@0 | 239 | // known module |
michael@0 | 240 | return true; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | return !frame.function_name.empty(); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | } // namespace google_breakpad |