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