PageRenderTime 48ms CodeModel.GetById 19ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/stackwalker_arm.cc

http://github.com/tomahawk-player/tomahawk
C++ | 293 lines | 175 code | 44 blank | 74 comment | 39 complexity | 644497e8121f84c1a642372549bb5dca MD5 | raw file
  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.
 29
 30// stackwalker_arm.cc: arm-specific stackwalker.
 31//
 32// See stackwalker_arm.h for documentation.
 33//
 34// Author: Mark Mentovai, Ted Mielczarek, Jim Blandy
 35
 36#include "google_breakpad/processor/call_stack.h"
 37#include "google_breakpad/processor/memory_region.h"
 38#include "google_breakpad/processor/source_line_resolver_interface.h"
 39#include "google_breakpad/processor/stack_frame_cpu.h"
 40#include "processor/cfi_frame_info.h"
 41#include "processor/logging.h"
 42#include "processor/scoped_ptr.h"
 43#include "processor/stackwalker_arm.h"
 44
 45namespace google_breakpad {
 46
 47
 48StackwalkerARM::StackwalkerARM(const SystemInfo *system_info,
 49                               const MDRawContextARM *context,
 50                               int fp_register,
 51                               MemoryRegion *memory,
 52                               const CodeModules *modules,
 53                               SymbolSupplier *supplier,
 54                               SourceLineResolverInterface *resolver)
 55    : Stackwalker(system_info, memory, modules, supplier, resolver),
 56      context_(context), fp_register_(fp_register),
 57      context_frame_validity_(StackFrameARM::CONTEXT_VALID_ALL) { }
 58
 59
 60StackFrame* StackwalkerARM::GetContextFrame() {
 61  if (!context_ || !memory_) {
 62    BPLOG(ERROR) << "Can't get context frame without context or memory";
 63    return NULL;
 64  }
 65
 66  StackFrameARM *frame = new StackFrameARM();
 67
 68  // The instruction pointer is stored directly in a register (r15), so pull it
 69  // straight out of the CPU context structure.
 70  frame->context = *context_;
 71  frame->context_validity = context_frame_validity_;
 72  frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
 73  frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC];
 74
 75  return frame;
 76}
 77
 78StackFrameARM *StackwalkerARM::GetCallerByCFIFrameInfo(
 79    const vector<StackFrame *> &frames,
 80    CFIFrameInfo *cfi_frame_info) {
 81  StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
 82
 83  static const char *register_names[] = {
 84    "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
 85    "r8",  "r9",  "r10", "r11", "r12", "sp",  "lr",  "pc",
 86    "f0",  "f1",  "f2",  "f3",  "f4",  "f5",  "f6",  "f7",
 87    "fps", "cpsr",
 88    NULL
 89  };
 90
 91  // Populate a dictionary with the valid register values in last_frame.
 92  CFIFrameInfo::RegisterValueMap<u_int32_t> callee_registers;
 93  for (int i = 0; register_names[i]; i++)
 94    if (last_frame->context_validity & StackFrameARM::RegisterValidFlag(i))
 95      callee_registers[register_names[i]] = last_frame->context.iregs[i];
 96
 97  // Use the STACK CFI data to recover the caller's register values.
 98  CFIFrameInfo::RegisterValueMap<u_int32_t> caller_registers;
 99  if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
100                                      &caller_registers))
101    return NULL;
102
103  // Construct a new stack frame given the values the CFI recovered.
104  scoped_ptr<StackFrameARM> frame(new StackFrameARM());
105  for (int i = 0; register_names[i]; i++) {
106    CFIFrameInfo::RegisterValueMap<u_int32_t>::iterator entry =
107      caller_registers.find(register_names[i]);
108    if (entry != caller_registers.end()) {
109      // We recovered the value of this register; fill the context with the
110      // value from caller_registers.
111      frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
112      frame->context.iregs[i] = entry->second;
113    } else if (4 <= i && i <= 11 && (last_frame->context_validity &
114                                     StackFrameARM::RegisterValidFlag(i))) {
115      // If the STACK CFI data doesn't mention some callee-saves register, and
116      // it is valid in the callee, assume the callee has not yet changed it.
117      // Registers r4 through r11 are callee-saves, according to the Procedure
118      // Call Standard for the ARM Architecture, which the Linux ABI follows.
119      frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
120      frame->context.iregs[i] = last_frame->context.iregs[i];
121    }
122  }
123  // If the CFI doesn't recover the PC explicitly, then use .ra.
124  if (! (frame->context_validity & StackFrameARM::CONTEXT_VALID_PC)) {
125    CFIFrameInfo::RegisterValueMap<u_int32_t>::iterator entry =
126      caller_registers.find(".ra");
127    if (entry != caller_registers.end()) {
128      if (fp_register_ == -1) {
129        frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
130        frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = entry->second;
131      } else {
132        // The CFI updated the link register and not the program counter.
133        // Handle getting the program counter from the link register.
134        frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
135        frame->context_validity |= StackFrameARM::CONTEXT_VALID_LR;
136        frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = entry->second;
137        frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
138            last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
139      }
140    }
141  }
142  // If the CFI doesn't recover the SP explicitly, then use .cfa.
143  if (! (frame->context_validity & StackFrameARM::CONTEXT_VALID_SP)) {
144    CFIFrameInfo::RegisterValueMap<u_int32_t>::iterator entry =
145      caller_registers.find(".cfa");
146    if (entry != caller_registers.end()) {
147      frame->context_validity |= StackFrameARM::CONTEXT_VALID_SP;
148      frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = entry->second;
149    }
150  }
151
152  // If we didn't recover the PC and the SP, then the frame isn't very useful.
153  static const int essentials = (StackFrameARM::CONTEXT_VALID_SP
154                                 | StackFrameARM::CONTEXT_VALID_PC);
155  if ((frame->context_validity & essentials) != essentials)
156    return NULL;
157
158  frame->trust = StackFrame::FRAME_TRUST_CFI;
159  return frame.release();
160}
161
162StackFrameARM *StackwalkerARM::GetCallerByStackScan(
163    const vector<StackFrame *> &frames) {
164  StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
165  u_int32_t last_sp = last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
166  u_int32_t caller_sp, caller_pc;
167
168  if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc)) {
169    // No plausible return address was found.
170    return NULL;
171  }
172
173  // ScanForReturnAddress found a reasonable return address. Advance
174  // %sp to the location above the one where the return address was
175  // found.
176  caller_sp += 4;
177
178  // Create a new stack frame (ownership will be transferred to the caller)
179  // and fill it in.
180  StackFrameARM *frame = new StackFrameARM();
181
182  frame->trust = StackFrame::FRAME_TRUST_SCAN;
183  frame->context = last_frame->context;
184  frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = caller_pc;
185  frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
186  frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
187                            StackFrameARM::CONTEXT_VALID_SP;
188
189  return frame;
190}
191
192StackFrameARM *StackwalkerARM::GetCallerByFramePointer(
193    const vector<StackFrame *> &frames) {
194  StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
195
196  if (!(last_frame->context_validity &
197        StackFrameARM::RegisterValidFlag(fp_register_))) {
198    return NULL;
199  }
200
201  u_int32_t last_fp = last_frame->context.iregs[fp_register_];
202
203  u_int32_t caller_fp = 0;
204  if (last_fp && !memory_->GetMemoryAtAddress(last_fp, &caller_fp)) {
205    BPLOG(ERROR) << "Unable to read caller_fp from last_fp: 0x"
206                 << std::hex << last_fp;
207    return NULL;
208  }
209
210  u_int32_t caller_lr = 0;
211  if (last_fp && !memory_->GetMemoryAtAddress(last_fp + 4, &caller_lr)) {
212    BPLOG(ERROR) << "Unable to read caller_lr from last_fp + 4: 0x"
213                 << std::hex << (last_fp + 4);
214    return NULL;
215  }
216
217  u_int32_t caller_sp = last_fp ? last_fp + 8 :
218      last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
219
220  // Create a new stack frame (ownership will be transferred to the caller)
221  // and fill it in.
222  StackFrameARM *frame = new StackFrameARM();
223
224  frame->trust = StackFrame::FRAME_TRUST_FP;
225  frame->context = last_frame->context;
226  frame->context.iregs[fp_register_] = caller_fp;
227  frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
228  frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
229      last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
230  frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = caller_lr;
231  frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
232                            StackFrameARM::CONTEXT_VALID_LR |
233                            StackFrameARM::RegisterValidFlag(fp_register_) |
234                            StackFrameARM::CONTEXT_VALID_SP;
235  return frame;
236}
237
238StackFrame* StackwalkerARM::GetCallerFrame(const CallStack *stack) {
239  if (!memory_ || !stack) {
240    BPLOG(ERROR) << "Can't get caller frame without memory or stack";
241    return NULL;
242  }
243
244  const vector<StackFrame *> &frames = *stack->frames();
245  StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
246  scoped_ptr<StackFrameARM> frame;
247
248  // See if there is DWARF call frame information covering this address.
249  scoped_ptr<CFIFrameInfo> cfi_frame_info(
250      resolver_ ? resolver_->FindCFIFrameInfo(last_frame) : NULL);
251  if (cfi_frame_info.get())
252    frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
253
254  // If CFI failed, or there wasn't CFI available, fall back
255  // to frame pointer, if this is configured.
256  if (fp_register_ >= 0 && !frame.get())
257    frame.reset(GetCallerByFramePointer(frames));
258
259  // If everuthing failed, fall back to stack scanning.
260  if (!frame.get())
261    frame.reset(GetCallerByStackScan(frames));
262
263  // If nothing worked, tell the caller.
264  if (!frame.get())
265    return NULL;
266
267
268  // An instruction address of zero marks the end of the stack.
269  if (frame->context.iregs[MD_CONTEXT_ARM_REG_PC] == 0)
270    return NULL;
271
272  // If the new stack pointer is at a lower address than the old, then
273  // that's clearly incorrect. Treat this as end-of-stack to enforce
274  // progress and avoid infinite loops.
275  if (frame->context.iregs[MD_CONTEXT_ARM_REG_SP]
276      < last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP])
277    return NULL;
278
279  // The new frame's context's PC is the return address, which is one
280  // instruction past the instruction that caused us to arrive at the
281  // callee. Set new_frame->instruction to one less than the PC. This won't
282  // reference the beginning of the call instruction, but it's at least
283  // within it, which is sufficient to get the source line information to
284  // match up with the line that contains the function call. Callers that
285  // require the exact return address value may access
286  // frame->context.iregs[MD_CONTEXT_ARM_REG_PC].
287  frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC] - 2;
288
289  return frame.release();
290}
291
292
293}  // namespace google_breakpad