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 "processor/stackwalker_ppc64.h"
36 #include "google_breakpad/processor/call_stack.h"
37 #include "google_breakpad/processor/memory_region.h"
38 #include "google_breakpad/processor/stack_frame_cpu.h"
39 #include "processor/logging.h"
40 
41 #include <stdio.h>
42 
43 namespace google_breakpad {
44 
45 
StackwalkerPPC64(const SystemInfo * system_info,const MDRawContextPPC64 * context,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)46 StackwalkerPPC64::StackwalkerPPC64(const SystemInfo* system_info,
47                                    const MDRawContextPPC64* 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 
GetContextFrame()56 StackFrame* StackwalkerPPC64::GetContextFrame() {
57   if (!context_) {
58     BPLOG(ERROR) << "Can't get context frame without context";
59     return NULL;
60   }
61 
62   StackFramePPC64* frame = new StackFramePPC64();
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 = StackFramePPC64::CONTEXT_VALID_ALL;
68   frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
69   frame->instruction = frame->context.srr0;
70 
71   return frame;
72 }
73 
74 
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)75 StackFrame* StackwalkerPPC64::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   // The instruction pointers for previous frames are saved on the stack.
83   // The typical ppc64 calling convention is for the called procedure to store
84   // its return address in the calling procedure's stack frame at 8(%r1),
85   // and to allocate its own stack frame by decrementing %r1 (the stack
86   // pointer) and saving the old value of %r1 at 0(%r1).  Because the ppc64 has
87   // no hardware stack, there is no distinction between the stack pointer and
88   // frame pointer, and what is typically thought of as the frame pointer on
89   // an x86 is usually referred to as the stack pointer on a ppc64.
90 
91   StackFramePPC64* last_frame = static_cast<StackFramePPC64*>(
92       stack->frames()->back());
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;
98   if (!memory_->GetMemoryAtAddress(last_frame->context.gpr[1],
99                                    &stack_pointer) ||
100       stack_pointer <= last_frame->context.gpr[1]) {
101     return NULL;
102   }
103 
104   // Mac OS X/Darwin gives 1 as the return address from the bottom-most
105   // frame in a stack (a thread's entry point).  I haven't found any
106   // documentation on this, but 0 or 1 would be bogus return addresses,
107   // so check for them here and return false (end of stack) when they're
108   // hit to avoid having a phantom frame.
109   uint64_t instruction;
110   if (!memory_->GetMemoryAtAddress(stack_pointer + 16, &instruction) ||
111       instruction <= 1) {
112     return NULL;
113   }
114 
115   StackFramePPC64* frame = new StackFramePPC64();
116 
117   frame->context = last_frame->context;
118   frame->context.srr0 = instruction;
119   frame->context.gpr[1] = stack_pointer;
120   frame->context_validity = StackFramePPC64::CONTEXT_VALID_SRR0 |
121                             StackFramePPC64::CONTEXT_VALID_GPR1;
122   frame->trust = StackFrame::FRAME_TRUST_FP;
123 
124   // frame->context.srr0 is the return address, which is one instruction
125   // past the branch that caused us to arrive at the callee.  Set
126   // frame_ppc64->instruction to eight less than that.  Since all ppc64
127   // instructions are 8 bytes wide, this is the address of the branch
128   // instruction.  This allows source line information to match up with the
129   // line that contains a function call.  Callers that require the exact
130   // return address value may access the context.srr0 field of StackFramePPC64.
131   frame->instruction = frame->context.srr0 - 8;
132 
133   return frame;
134 }
135 
136 
137 }  // namespace google_breakpad
138