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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2010 Google Inc.
     2 // All rights reserved.
     3 //
     4 // Redistribution and use in source and binary forms, with or without
     5 // modification, are permitted provided that the following conditions are
     6 // met:
     7 //
     8 //     * Redistributions of source code must retain the above copyright
     9 // notice, this list of conditions and the following disclaimer.
    10 //     * Redistributions in binary form must reproduce the above
    11 // copyright notice, this list of conditions and the following disclaimer
    12 // in the documentation and/or other materials provided with the
    13 // distribution.
    14 //     * Neither the name of Google Inc. nor the names of its
    15 // contributors may be used to endorse or promote products derived from
    16 // this software without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30 // stackwalker_arm.cc: arm-specific stackwalker.
    31 //
    32 // See stackwalker_arm.h for documentation.
    33 //
    34 // Author: Mark Mentovai, Ted Mielczarek, Jim Blandy
    36 #include <vector>
    38 #include "common/scoped_ptr.h"
    39 #include "google_breakpad/processor/call_stack.h"
    40 #include "google_breakpad/processor/memory_region.h"
    41 #include "google_breakpad/processor/source_line_resolver_interface.h"
    42 #include "google_breakpad/processor/stack_frame_cpu.h"
    43 #include "processor/cfi_frame_info.h"
    44 #include "common/logging.h"
    45 #include "processor/stackwalker_arm.h"
    47 namespace google_breakpad {
    50 StackwalkerARM::StackwalkerARM(const SystemInfo* system_info,
    51                                const MDRawContextARM* context,
    52                                int fp_register,
    53                                MemoryRegion* memory,
    54                                const CodeModules* modules,
    55                                StackFrameSymbolizer* resolver_helper)
    56     : Stackwalker(system_info, memory, modules, resolver_helper),
    57       context_(context), fp_register_(fp_register),
    58       context_frame_validity_(StackFrameARM::CONTEXT_VALID_ALL) { }
    61 StackFrame* StackwalkerARM::GetContextFrame() {
    62   if (!context_) {
    63     BPLOG(ERROR) << "Can't get context frame without context";
    64     return NULL;
    65   }
    67   StackFrameARM* frame = new StackFrameARM();
    69   // The instruction pointer is stored directly in a register (r15), so pull it
    70   // straight out of the CPU context structure.
    71   frame->context = *context_;
    72   frame->context_validity = context_frame_validity_;
    73   frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
    74   frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC];
    76   return frame;
    77 }
    79 StackFrameARM* StackwalkerARM::GetCallerByCFIFrameInfo(
    80     const vector<StackFrame*> &frames,
    81     CFIFrameInfo* cfi_frame_info) {
    82   StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
    84   static const UniqueString *register_names[] = {
    85     ToUniqueString("r0"),  ToUniqueString("r1"),
    86     ToUniqueString("r2"),  ToUniqueString("r3"),
    87     ToUniqueString("r4"),  ToUniqueString("r5"),
    88     ToUniqueString("r6"),  ToUniqueString("r7"),
    89     ToUniqueString("r8"),  ToUniqueString("r9"),
    90     ToUniqueString("r10"), ToUniqueString("r11"),
    91     ToUniqueString("r12"), ToUniqueString("sp"),
    92     ToUniqueString("lr"),  ToUniqueString("pc"),
    93     ToUniqueString("f0"),  ToUniqueString("f1"),
    94     ToUniqueString("f2"),  ToUniqueString("f3"),
    95     ToUniqueString("f4"),  ToUniqueString("f5"),
    96     ToUniqueString("f6"),  ToUniqueString("f7"),
    97     ToUniqueString("fps"), ToUniqueString("cpsr"),
    98     NULL
    99   };
   101   // Populate a dictionary with the valid register values in last_frame.
   102   CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
   103   for (int i = 0; register_names[i]; i++)
   104     if (last_frame->context_validity & StackFrameARM::RegisterValidFlag(i))
   105       callee_registers.set(register_names[i], last_frame->context.iregs[i]);
   107   // Use the STACK CFI data to recover the caller's register values.
   108   CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
   109   if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
   110                                       &caller_registers))
   111     return NULL;
   113   // Construct a new stack frame given the values the CFI recovered.
   114   scoped_ptr<StackFrameARM> frame(new StackFrameARM());
   115   for (int i = 0; register_names[i]; i++) {
   116     bool found = false;
   117     uint32_t v = caller_registers.get(&found, register_names[i]);
   118     if (found) {
   119       // We recovered the value of this register; fill the context with the
   120       // value from caller_registers.
   121       frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
   122       frame->context.iregs[i] = v;
   123     } else if (4 <= i && i <= 11 && (last_frame->context_validity &
   124                                      StackFrameARM::RegisterValidFlag(i))) {
   125       // If the STACK CFI data doesn't mention some callee-saves register, and
   126       // it is valid in the callee, assume the callee has not yet changed it.
   127       // Registers r4 through r11 are callee-saves, according to the Procedure
   128       // Call Standard for the ARM Architecture, which the Linux ABI follows.
   129       frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
   130       frame->context.iregs[i] = last_frame->context.iregs[i];
   131     }
   132   }
   133   // If the CFI doesn't recover the PC explicitly, then use .ra.
   134   if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_PC)) {
   135     bool found = false;
   136     uint32_t v = caller_registers.get(&found, ustr__ZDra());
   137     if (found) {
   138       if (fp_register_ == -1) {
   139         frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
   140         frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = v;
   141       } else {
   142         // The CFI updated the link register and not the program counter.
   143         // Handle getting the program counter from the link register.
   144         frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
   145         frame->context_validity |= StackFrameARM::CONTEXT_VALID_LR;
   146         frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = v;
   147         frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
   148             last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
   149       }
   150     }
   151   }
   152   // If the CFI doesn't recover the SP explicitly, then use .cfa.
   153   if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_SP)) {
   154     bool found = false;
   155     uint32_t v = caller_registers.get(&found, ustr__ZDcfa());
   156     if (found) {
   157       frame->context_validity |= StackFrameARM::CONTEXT_VALID_SP;
   158       frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = v;
   159     }
   160   }
   162   // If we didn't recover the PC and the SP, then the frame isn't very useful.
   163   static const int essentials = (StackFrameARM::CONTEXT_VALID_SP
   164                                  | StackFrameARM::CONTEXT_VALID_PC);
   165   if ((frame->context_validity & essentials) != essentials)
   166     return NULL;
   168   frame->trust = StackFrame::FRAME_TRUST_CFI;
   169   return frame.release();
   170 }
   172 StackFrameARM* StackwalkerARM::GetCallerByStackScan(
   173     const vector<StackFrame*> &frames) {
   174   StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
   175   uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
   176   uint32_t caller_sp, caller_pc;
   178   // When searching for the caller of the context frame,
   179   // allow the scanner to look farther down the stack.
   180   const int kRASearchWords = frames.size() == 1 ?
   181     Stackwalker::kRASearchWords * 4 :
   182     Stackwalker::kRASearchWords;
   184   if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc,
   185                             kRASearchWords)) {
   186     // No plausible return address was found.
   187     return NULL;
   188   }
   190   // ScanForReturnAddress found a reasonable return address. Advance
   191   // %sp to the location above the one where the return address was
   192   // found.
   193   caller_sp += 4;
   195   // Create a new stack frame (ownership will be transferred to the caller)
   196   // and fill it in.
   197   StackFrameARM* frame = new StackFrameARM();
   199   frame->trust = StackFrame::FRAME_TRUST_SCAN;
   200   frame->context = last_frame->context;
   201   frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = caller_pc;
   202   frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
   203   frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
   204                             StackFrameARM::CONTEXT_VALID_SP;
   206   return frame;
   207 }
   209 StackFrameARM* StackwalkerARM::GetCallerByFramePointer(
   210     const vector<StackFrame*> &frames) {
   211   StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
   213   if (!(last_frame->context_validity &
   214         StackFrameARM::RegisterValidFlag(fp_register_))) {
   215     return NULL;
   216   }
   218   uint32_t last_fp = last_frame->context.iregs[fp_register_];
   220   uint32_t caller_fp = 0;
   221   if (last_fp && !memory_->GetMemoryAtAddress(last_fp, &caller_fp)) {
   222     BPLOG(ERROR) << "Unable to read caller_fp from last_fp: 0x"
   223                  << std::hex << last_fp;
   224     return NULL;
   225   }
   227   uint32_t caller_lr = 0;
   228   if (last_fp && !memory_->GetMemoryAtAddress(last_fp + 4, &caller_lr)) {
   229     BPLOG(ERROR) << "Unable to read caller_lr from last_fp + 4: 0x"
   230                  << std::hex << (last_fp + 4);
   231     return NULL;
   232   }
   234   uint32_t caller_sp = last_fp ? last_fp + 8 :
   235       last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
   237   // Create a new stack frame (ownership will be transferred to the caller)
   238   // and fill it in.
   239   StackFrameARM* frame = new StackFrameARM();
   241   frame->trust = StackFrame::FRAME_TRUST_FP;
   242   frame->context = last_frame->context;
   243   frame->context.iregs[fp_register_] = caller_fp;
   244   frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
   245   frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
   246       last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
   247   frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = caller_lr;
   248   frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
   249                             StackFrameARM::CONTEXT_VALID_LR |
   250                             StackFrameARM::RegisterValidFlag(fp_register_) |
   251                             StackFrameARM::CONTEXT_VALID_SP;
   252   return frame;
   253 }
   255 StackFrame* StackwalkerARM::GetCallerFrame(const CallStack* stack,
   256                                            bool stack_scan_allowed) {
   257   if (!memory_ || !stack) {
   258     BPLOG(ERROR) << "Can't get caller frame without memory or stack";
   259     return NULL;
   260   }
   262   const vector<StackFrame*> &frames = *stack->frames();
   263   StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
   264   scoped_ptr<StackFrameARM> frame;
   266   // See if there is DWARF call frame information covering this address.
   267   scoped_ptr<CFIFrameInfo> cfi_frame_info(
   268       frame_symbolizer_->FindCFIFrameInfo(last_frame));
   269   if (cfi_frame_info.get())
   270     frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
   272   // If CFI failed, or there wasn't CFI available, fall back
   273   // to frame pointer, if this is configured.
   274   if (fp_register_ >= 0 && !frame.get())
   275     frame.reset(GetCallerByFramePointer(frames));
   277   // If everuthing failed, fall back to stack scanning.
   278   if (stack_scan_allowed && !frame.get())
   279     frame.reset(GetCallerByStackScan(frames));
   281   // If nothing worked, tell the caller.
   282   if (!frame.get())
   283     return NULL;
   286   // An instruction address of zero marks the end of the stack.
   287   if (frame->context.iregs[MD_CONTEXT_ARM_REG_PC] == 0)
   288     return NULL;
   290   // If the new stack pointer is at a lower address than the old, then
   291   // that's clearly incorrect. Treat this as end-of-stack to enforce
   292   // progress and avoid infinite loops.
   293   if (frame->context.iregs[MD_CONTEXT_ARM_REG_SP]
   294       < last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP])
   295     return NULL;
   297   // The new frame's context's PC is the return address, which is one
   298   // instruction past the instruction that caused us to arrive at the
   299   // callee. Set new_frame->instruction to one less than the PC. This won't
   300   // reference the beginning of the call instruction, but it's at least
   301   // within it, which is sufficient to get the source line information to
   302   // match up with the line that contains the function call. Callers that
   303   // require the exact return address value may access
   304   // frame->context.iregs[MD_CONTEXT_ARM_REG_PC].
   305   frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC] - 2;
   307   return frame.release();
   308 }
   311 }  // namespace google_breakpad

mercurial