1 // Copyright (c) 2013 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_ppc64.cc: ppc64-specific stackwalker.
31 //
32 // See stackwalker_ppc64.h for documentation.
33 
34 
35 #include "common/scoped_ptr.h"
36 #include "processor/stackwalker_ppc64.h"
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 
42 #include <stdio.h>
43 
44 namespace google_breakpad {
45 
46 
StackwalkerPPC64(const SystemInfo * system_info,const MDRawContextPPC64 * context,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)47 StackwalkerPPC64::StackwalkerPPC64(const SystemInfo* system_info,
48                                    const MDRawContextPPC64* context,
49                                    MemoryRegion* memory,
50                                    const CodeModules* modules,
51                                    StackFrameSymbolizer* resolver_helper)
52     : Stackwalker(system_info, memory, modules, resolver_helper),
53       context_(context) {
54 }
55 
56 
GetContextFrame()57 StackFrame* StackwalkerPPC64::GetContextFrame() {
58   if (!context_) {
59     BPLOG(ERROR) << "Can't get context frame without context";
60     return NULL;
61   }
62 
63   StackFramePPC64* frame = new StackFramePPC64();
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 = StackFramePPC64::CONTEXT_VALID_ALL;
69   frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
70   frame->instruction = frame->context.srr0;
71 
72   return frame;
73 }
74 
75 
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)76 StackFrame* StackwalkerPPC64::GetCallerFrame(const CallStack* stack,
77                                              bool stack_scan_allowed) {
78   if (!memory_ || !stack) {
79     BPLOG(ERROR) << "Can't get caller frame without memory or stack";
80     return NULL;
81   }
82 
83   // The instruction pointers for previous frames are saved on the stack.
84   // The typical ppc64 calling convention is for the called procedure to store
85   // its return address in the calling procedure's stack frame at 8(%r1),
86   // and to allocate its own stack frame by decrementing %r1 (the stack
87   // pointer) and saving the old value of %r1 at 0(%r1).  Because the ppc64 has
88   // no hardware stack, there is no distinction between the stack pointer and
89   // frame pointer, and what is typically thought of as the frame pointer on
90   // an x86 is usually referred to as the stack pointer on a ppc64.
91 
92   StackFramePPC64* last_frame = static_cast<StackFramePPC64*>(
93       stack->frames()->back());
94 
95   // A caller frame must reside higher in memory than its callee frames.
96   // Anything else is an error, or an indication that we've reached the
97   // end of the stack.
98   uint64_t stack_pointer;
99   if (!memory_->GetMemoryAtAddress(last_frame->context.gpr[1],
100                                    &stack_pointer) ||
101       stack_pointer <= last_frame->context.gpr[1]) {
102     return NULL;
103   }
104 
105   // Mac OS X/Darwin gives 1 as the return address from the bottom-most
106   // frame in a stack (a thread's entry point).  I haven't found any
107   // documentation on this, but 0 or 1 would be bogus return addresses,
108   // so check for them here and return false (end of stack) when they're
109   // hit to avoid having a phantom frame.
110   uint64_t instruction;
111   if (!memory_->GetMemoryAtAddress(stack_pointer + 16, &instruction) ||
112       instruction <= 1) {
113     return NULL;
114   }
115 
116   scoped_ptr<StackFramePPC64> frame(new StackFramePPC64());
117 
118   frame->context = last_frame->context;
119   frame->context.srr0 = instruction;
120   frame->context.gpr[1] = stack_pointer;
121   frame->context_validity = StackFramePPC64::CONTEXT_VALID_SRR0 |
122                             StackFramePPC64::CONTEXT_VALID_GPR1;
123   frame->trust = StackFrame::FRAME_TRUST_FP;
124 
125   // Should we terminate the stack walk? (end-of-stack or broken invariant)
126   if (TerminateWalk(instruction,
127                     stack_pointer,
128                     last_frame->context.gpr[1],
129                     stack->frames()->size() == 1)) {
130     return NULL;
131   }
132 
133   // frame->context.srr0 is the return address, which is one instruction
134   // past the branch that caused us to arrive at the callee.  Set
135   // frame_ppc64->instruction to eight less than that.  Since all ppc64
136   // instructions are 8 bytes wide, this is the address of the branch
137   // instruction.  This allows source line information to match up with the
138   // line that contains a function call.  Callers that require the exact
139   // return address value may access the context.srr0 field of StackFramePPC64.
140   frame->instruction = frame->context.srr0 - 8;
141 
142   return frame.release();
143 }
144 
145 
146 }  // namespace google_breakpad
147