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                                   StackFrameSymbolizer* resolver_helper)
51    : Stackwalker(system_info, memory, modules, resolver_helper),
52      context_(context) {
53}
54
55
56StackFrame* StackwalkerSPARC::GetContextFrame() {
57  if (!context_) {
58    BPLOG(ERROR) << "Can't get context frame without context";
59    return NULL;
60  }
61
62  StackFrameSPARC* frame = new StackFrameSPARC();
63
64  // The instruction pointer is stored directly in a register, so pull it
65  // straight out of the CPU context structure.
66  frame->context = *context_;
67  frame->context_validity = StackFrameSPARC::CONTEXT_VALID_ALL;
68  frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
69  frame->instruction = frame->context.pc;
70
71  return frame;
72}
73
74
75StackFrame* StackwalkerSPARC::GetCallerFrame(const CallStack* stack,
76                                             bool stack_scan_allowed) {
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  uint64_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  uint32_t instruction;
103  if (!memory_->GetMemoryAtAddress(stack_pointer + 60,
104                     &instruction) || instruction <= 1) {
105    return NULL;
106  }
107
108  uint32_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
140