toolkit/crashreporter/google-breakpad/src/processor/stackwalker_arm.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/stackwalker_arm.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,311 @@
     1.4 +// Copyright (c) 2010 Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// stackwalker_arm.cc: arm-specific stackwalker.
    1.34 +//
    1.35 +// See stackwalker_arm.h for documentation.
    1.36 +//
    1.37 +// Author: Mark Mentovai, Ted Mielczarek, Jim Blandy
    1.38 +
    1.39 +#include <vector>
    1.40 +
    1.41 +#include "common/scoped_ptr.h"
    1.42 +#include "google_breakpad/processor/call_stack.h"
    1.43 +#include "google_breakpad/processor/memory_region.h"
    1.44 +#include "google_breakpad/processor/source_line_resolver_interface.h"
    1.45 +#include "google_breakpad/processor/stack_frame_cpu.h"
    1.46 +#include "processor/cfi_frame_info.h"
    1.47 +#include "common/logging.h"
    1.48 +#include "processor/stackwalker_arm.h"
    1.49 +
    1.50 +namespace google_breakpad {
    1.51 +
    1.52 +
    1.53 +StackwalkerARM::StackwalkerARM(const SystemInfo* system_info,
    1.54 +                               const MDRawContextARM* context,
    1.55 +                               int fp_register,
    1.56 +                               MemoryRegion* memory,
    1.57 +                               const CodeModules* modules,
    1.58 +                               StackFrameSymbolizer* resolver_helper)
    1.59 +    : Stackwalker(system_info, memory, modules, resolver_helper),
    1.60 +      context_(context), fp_register_(fp_register),
    1.61 +      context_frame_validity_(StackFrameARM::CONTEXT_VALID_ALL) { }
    1.62 +
    1.63 +
    1.64 +StackFrame* StackwalkerARM::GetContextFrame() {
    1.65 +  if (!context_) {
    1.66 +    BPLOG(ERROR) << "Can't get context frame without context";
    1.67 +    return NULL;
    1.68 +  }
    1.69 +
    1.70 +  StackFrameARM* frame = new StackFrameARM();
    1.71 +
    1.72 +  // The instruction pointer is stored directly in a register (r15), so pull it
    1.73 +  // straight out of the CPU context structure.
    1.74 +  frame->context = *context_;
    1.75 +  frame->context_validity = context_frame_validity_;
    1.76 +  frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
    1.77 +  frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC];
    1.78 +
    1.79 +  return frame;
    1.80 +}
    1.81 +
    1.82 +StackFrameARM* StackwalkerARM::GetCallerByCFIFrameInfo(
    1.83 +    const vector<StackFrame*> &frames,
    1.84 +    CFIFrameInfo* cfi_frame_info) {
    1.85 +  StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
    1.86 +
    1.87 +  static const UniqueString *register_names[] = {
    1.88 +    ToUniqueString("r0"),  ToUniqueString("r1"),
    1.89 +    ToUniqueString("r2"),  ToUniqueString("r3"),
    1.90 +    ToUniqueString("r4"),  ToUniqueString("r5"),
    1.91 +    ToUniqueString("r6"),  ToUniqueString("r7"),
    1.92 +    ToUniqueString("r8"),  ToUniqueString("r9"),
    1.93 +    ToUniqueString("r10"), ToUniqueString("r11"),
    1.94 +    ToUniqueString("r12"), ToUniqueString("sp"),
    1.95 +    ToUniqueString("lr"),  ToUniqueString("pc"),
    1.96 +    ToUniqueString("f0"),  ToUniqueString("f1"),
    1.97 +    ToUniqueString("f2"),  ToUniqueString("f3"),
    1.98 +    ToUniqueString("f4"),  ToUniqueString("f5"),
    1.99 +    ToUniqueString("f6"),  ToUniqueString("f7"),
   1.100 +    ToUniqueString("fps"), ToUniqueString("cpsr"),
   1.101 +    NULL
   1.102 +  };
   1.103 +
   1.104 +  // Populate a dictionary with the valid register values in last_frame.
   1.105 +  CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
   1.106 +  for (int i = 0; register_names[i]; i++)
   1.107 +    if (last_frame->context_validity & StackFrameARM::RegisterValidFlag(i))
   1.108 +      callee_registers.set(register_names[i], last_frame->context.iregs[i]);
   1.109 +
   1.110 +  // Use the STACK CFI data to recover the caller's register values.
   1.111 +  CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
   1.112 +  if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
   1.113 +                                      &caller_registers))
   1.114 +    return NULL;
   1.115 +
   1.116 +  // Construct a new stack frame given the values the CFI recovered.
   1.117 +  scoped_ptr<StackFrameARM> frame(new StackFrameARM());
   1.118 +  for (int i = 0; register_names[i]; i++) {
   1.119 +    bool found = false;
   1.120 +    uint32_t v = caller_registers.get(&found, register_names[i]);
   1.121 +    if (found) {
   1.122 +      // We recovered the value of this register; fill the context with the
   1.123 +      // value from caller_registers.
   1.124 +      frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
   1.125 +      frame->context.iregs[i] = v;
   1.126 +    } else if (4 <= i && i <= 11 && (last_frame->context_validity &
   1.127 +                                     StackFrameARM::RegisterValidFlag(i))) {
   1.128 +      // If the STACK CFI data doesn't mention some callee-saves register, and
   1.129 +      // it is valid in the callee, assume the callee has not yet changed it.
   1.130 +      // Registers r4 through r11 are callee-saves, according to the Procedure
   1.131 +      // Call Standard for the ARM Architecture, which the Linux ABI follows.
   1.132 +      frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
   1.133 +      frame->context.iregs[i] = last_frame->context.iregs[i];
   1.134 +    }
   1.135 +  }
   1.136 +  // If the CFI doesn't recover the PC explicitly, then use .ra.
   1.137 +  if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_PC)) {
   1.138 +    bool found = false;
   1.139 +    uint32_t v = caller_registers.get(&found, ustr__ZDra());
   1.140 +    if (found) {
   1.141 +      if (fp_register_ == -1) {
   1.142 +        frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
   1.143 +        frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = v;
   1.144 +      } else {
   1.145 +        // The CFI updated the link register and not the program counter.
   1.146 +        // Handle getting the program counter from the link register.
   1.147 +        frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
   1.148 +        frame->context_validity |= StackFrameARM::CONTEXT_VALID_LR;
   1.149 +        frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = v;
   1.150 +        frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
   1.151 +            last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
   1.152 +      }
   1.153 +    }
   1.154 +  }
   1.155 +  // If the CFI doesn't recover the SP explicitly, then use .cfa.
   1.156 +  if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_SP)) {
   1.157 +    bool found = false;
   1.158 +    uint32_t v = caller_registers.get(&found, ustr__ZDcfa());
   1.159 +    if (found) {
   1.160 +      frame->context_validity |= StackFrameARM::CONTEXT_VALID_SP;
   1.161 +      frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = v;
   1.162 +    }
   1.163 +  }
   1.164 +
   1.165 +  // If we didn't recover the PC and the SP, then the frame isn't very useful.
   1.166 +  static const int essentials = (StackFrameARM::CONTEXT_VALID_SP
   1.167 +                                 | StackFrameARM::CONTEXT_VALID_PC);
   1.168 +  if ((frame->context_validity & essentials) != essentials)
   1.169 +    return NULL;
   1.170 +
   1.171 +  frame->trust = StackFrame::FRAME_TRUST_CFI;
   1.172 +  return frame.release();
   1.173 +}
   1.174 +
   1.175 +StackFrameARM* StackwalkerARM::GetCallerByStackScan(
   1.176 +    const vector<StackFrame*> &frames) {
   1.177 +  StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
   1.178 +  uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
   1.179 +  uint32_t caller_sp, caller_pc;
   1.180 +
   1.181 +  // When searching for the caller of the context frame,
   1.182 +  // allow the scanner to look farther down the stack.
   1.183 +  const int kRASearchWords = frames.size() == 1 ?
   1.184 +    Stackwalker::kRASearchWords * 4 :
   1.185 +    Stackwalker::kRASearchWords;
   1.186 +
   1.187 +  if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc,
   1.188 +                            kRASearchWords)) {
   1.189 +    // No plausible return address was found.
   1.190 +    return NULL;
   1.191 +  }
   1.192 +
   1.193 +  // ScanForReturnAddress found a reasonable return address. Advance
   1.194 +  // %sp to the location above the one where the return address was
   1.195 +  // found.
   1.196 +  caller_sp += 4;
   1.197 +
   1.198 +  // Create a new stack frame (ownership will be transferred to the caller)
   1.199 +  // and fill it in.
   1.200 +  StackFrameARM* frame = new StackFrameARM();
   1.201 +
   1.202 +  frame->trust = StackFrame::FRAME_TRUST_SCAN;
   1.203 +  frame->context = last_frame->context;
   1.204 +  frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = caller_pc;
   1.205 +  frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
   1.206 +  frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
   1.207 +                            StackFrameARM::CONTEXT_VALID_SP;
   1.208 +
   1.209 +  return frame;
   1.210 +}
   1.211 +
   1.212 +StackFrameARM* StackwalkerARM::GetCallerByFramePointer(
   1.213 +    const vector<StackFrame*> &frames) {
   1.214 +  StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
   1.215 +
   1.216 +  if (!(last_frame->context_validity &
   1.217 +        StackFrameARM::RegisterValidFlag(fp_register_))) {
   1.218 +    return NULL;
   1.219 +  }
   1.220 +
   1.221 +  uint32_t last_fp = last_frame->context.iregs[fp_register_];
   1.222 +
   1.223 +  uint32_t caller_fp = 0;
   1.224 +  if (last_fp && !memory_->GetMemoryAtAddress(last_fp, &caller_fp)) {
   1.225 +    BPLOG(ERROR) << "Unable to read caller_fp from last_fp: 0x"
   1.226 +                 << std::hex << last_fp;
   1.227 +    return NULL;
   1.228 +  }
   1.229 +
   1.230 +  uint32_t caller_lr = 0;
   1.231 +  if (last_fp && !memory_->GetMemoryAtAddress(last_fp + 4, &caller_lr)) {
   1.232 +    BPLOG(ERROR) << "Unable to read caller_lr from last_fp + 4: 0x"
   1.233 +                 << std::hex << (last_fp + 4);
   1.234 +    return NULL;
   1.235 +  }
   1.236 +
   1.237 +  uint32_t caller_sp = last_fp ? last_fp + 8 :
   1.238 +      last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
   1.239 +
   1.240 +  // Create a new stack frame (ownership will be transferred to the caller)
   1.241 +  // and fill it in.
   1.242 +  StackFrameARM* frame = new StackFrameARM();
   1.243 +
   1.244 +  frame->trust = StackFrame::FRAME_TRUST_FP;
   1.245 +  frame->context = last_frame->context;
   1.246 +  frame->context.iregs[fp_register_] = caller_fp;
   1.247 +  frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
   1.248 +  frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
   1.249 +      last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
   1.250 +  frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = caller_lr;
   1.251 +  frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
   1.252 +                            StackFrameARM::CONTEXT_VALID_LR |
   1.253 +                            StackFrameARM::RegisterValidFlag(fp_register_) |
   1.254 +                            StackFrameARM::CONTEXT_VALID_SP;
   1.255 +  return frame;
   1.256 +}
   1.257 +
   1.258 +StackFrame* StackwalkerARM::GetCallerFrame(const CallStack* stack,
   1.259 +                                           bool stack_scan_allowed) {
   1.260 +  if (!memory_ || !stack) {
   1.261 +    BPLOG(ERROR) << "Can't get caller frame without memory or stack";
   1.262 +    return NULL;
   1.263 +  }
   1.264 +
   1.265 +  const vector<StackFrame*> &frames = *stack->frames();
   1.266 +  StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
   1.267 +  scoped_ptr<StackFrameARM> frame;
   1.268 +
   1.269 +  // See if there is DWARF call frame information covering this address.
   1.270 +  scoped_ptr<CFIFrameInfo> cfi_frame_info(
   1.271 +      frame_symbolizer_->FindCFIFrameInfo(last_frame));
   1.272 +  if (cfi_frame_info.get())
   1.273 +    frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
   1.274 +
   1.275 +  // If CFI failed, or there wasn't CFI available, fall back
   1.276 +  // to frame pointer, if this is configured.
   1.277 +  if (fp_register_ >= 0 && !frame.get())
   1.278 +    frame.reset(GetCallerByFramePointer(frames));
   1.279 +
   1.280 +  // If everuthing failed, fall back to stack scanning.
   1.281 +  if (stack_scan_allowed && !frame.get())
   1.282 +    frame.reset(GetCallerByStackScan(frames));
   1.283 +
   1.284 +  // If nothing worked, tell the caller.
   1.285 +  if (!frame.get())
   1.286 +    return NULL;
   1.287 +
   1.288 +
   1.289 +  // An instruction address of zero marks the end of the stack.
   1.290 +  if (frame->context.iregs[MD_CONTEXT_ARM_REG_PC] == 0)
   1.291 +    return NULL;
   1.292 +
   1.293 +  // If the new stack pointer is at a lower address than the old, then
   1.294 +  // that's clearly incorrect. Treat this as end-of-stack to enforce
   1.295 +  // progress and avoid infinite loops.
   1.296 +  if (frame->context.iregs[MD_CONTEXT_ARM_REG_SP]
   1.297 +      < last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP])
   1.298 +    return NULL;
   1.299 +
   1.300 +  // The new frame's context's PC is the return address, which is one
   1.301 +  // instruction past the instruction that caused us to arrive at the
   1.302 +  // callee. Set new_frame->instruction to one less than the PC. This won't
   1.303 +  // reference the beginning of the call instruction, but it's at least
   1.304 +  // within it, which is sufficient to get the source line information to
   1.305 +  // match up with the line that contains the function call. Callers that
   1.306 +  // require the exact return address value may access
   1.307 +  // frame->context.iregs[MD_CONTEXT_ARM_REG_PC].
   1.308 +  frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC] - 2;
   1.309 +
   1.310 +  return frame.release();
   1.311 +}
   1.312 +
   1.313 +
   1.314 +}  // namespace google_breakpad

mercurial