PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/stackwalker_sparc.cc

http://github.com/tomahawk-player/tomahawk
C++ | 139 lines | 61 code | 24 blank | 54 comment | 9 complexity | 027c9ae2f34fe2f1689b6748cd4e6804 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_sparc.cc: sparc-specific stackwalker.
 31//
 32// See stackwalker_sparc.h for documentation.
 33//
 34// Author: Michael Shang
 35
 36
 37#include "google_breakpad/processor/call_stack.h"
 38#include "google_breakpad/processor/memory_region.h"
 39#include "google_breakpad/processor/stack_frame_cpu.h"
 40#include "processor/logging.h"
 41#include "processor/stackwalker_sparc.h"
 42
 43namespace google_breakpad {
 44
 45
 46StackwalkerSPARC::StackwalkerSPARC(const SystemInfo *system_info,
 47                                   const MDRawContextSPARC *context,
 48                                   MemoryRegion *memory,
 49                                   const CodeModules *modules,
 50                                   SymbolSupplier *supplier,
 51                                   SourceLineResolverInterface *resolver)
 52    : Stackwalker(system_info, memory, modules, supplier, resolver),
 53      context_(context) {
 54}
 55
 56
 57StackFrame* StackwalkerSPARC::GetContextFrame() {
 58  if (!context_ || !memory_) {
 59    BPLOG(ERROR) << "Can't get context frame without context or memory";
 60    return NULL;
 61  }
 62
 63  StackFrameSPARC *frame = new StackFrameSPARC();
 64
 65  // The instruction pointer is stored directly in a register, so pull it
 66  // straight out of the CPU context structure.
 67  frame->context = *context_;
 68  frame->context_validity = StackFrameSPARC::CONTEXT_VALID_ALL;
 69  frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
 70  frame->instruction = frame->context.pc;
 71
 72  return frame;
 73}
 74
 75
 76StackFrame* StackwalkerSPARC::GetCallerFrame(const CallStack *stack) {
 77  if (!memory_ || !stack) {
 78    BPLOG(ERROR) << "Can't get caller frame without memory or stack";
 79    return NULL;
 80  }
 81
 82  StackFrameSPARC *last_frame = static_cast<StackFrameSPARC*>(
 83      stack->frames()->back());
 84
 85  // new: caller
 86  // old: callee
 87  // %fp, %i6 and g_r[30] is the same, see minidump_format.h
 88  // %sp, %o6 and g_r[14] is the same, see minidump_format.h
 89  // %sp_new = %fp_old
 90  // %fp_new = *(%fp_old + 32 + 32 - 8), where the callee's %i6 
 91  // %pc_new = *(%fp_old + 32 + 32 - 4) + 8
 92  // which is callee's %i7 plus 8
 93
 94  // A caller frame must reside higher in memory than its callee frames.
 95  // Anything else is an error, or an indication that we've reached the
 96  // end of the stack.
 97  u_int64_t stack_pointer = last_frame->context.g_r[30];
 98  if (stack_pointer <= last_frame->context.g_r[14]) {
 99    return NULL;
100  }
101
102  u_int32_t instruction;
103  if (!memory_->GetMemoryAtAddress(stack_pointer + 60,
104                     &instruction) || instruction <= 1) {
105    return NULL;
106  }
107
108  u_int32_t stack_base;
109  if (!memory_->GetMemoryAtAddress(stack_pointer + 56,
110                     &stack_base) || stack_base <= 1) {
111    return NULL;
112  }
113
114  StackFrameSPARC *frame = new StackFrameSPARC();
115
116  frame->context = last_frame->context;
117  frame->context.g_r[14] = stack_pointer;
118  frame->context.g_r[30] = stack_base;
119  
120  // frame->context.pc is the return address, which is 2 instruction
121  // past the branch that caused us to arrive at the callee, which are 
122  // a CALL instruction then a NOP instruction.
123  // frame_ppc->instruction to 8 less than that.  Since all sparc
124  // instructions are 4 bytes wide, this is the address of the branch
125  // instruction.  This allows source line information to match up with the
126  // line that contains a function call.  Callers that require the exact
127  // return address value may access the %i7/g_r[31] field of StackFrameSPARC.
128  frame->context.pc = instruction + 8;
129  frame->instruction = instruction;
130  frame->context_validity = StackFrameSPARC::CONTEXT_VALID_PC |
131                            StackFrameSPARC::CONTEXT_VALID_SP |
132                            StackFrameSPARC::CONTEXT_VALID_FP;
133  frame->trust = StackFrame::FRAME_TRUST_FP;
134                            
135  return frame;
136}
137
138
139}  // namespace google_breakpad