blob: 811d6cc97eee9e33abaf94a9a4d4b0994a5894f6 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2007, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Craig Silverstein
33//
34// Produce stack trace. I'm guessing (hoping!) the code is much like
35// for x86. For apple machines, at least, it seems to be; see
36// http://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html
37// http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
38// Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882
39
40#ifndef BASE_STACKTRACE_POWERPC_INL_H_
41#define BASE_STACKTRACE_POWERPC_INL_H_
42// Note: this file is included into stacktrace.cc more than once.
43// Anything that should only be defined once should be here:
44
45#include <stdint.h> // for uintptr_t
46#include <stdlib.h> // for NULL
47#include <gperftools/stacktrace.h>
48
49struct layout_ppc {
50 struct layout_ppc *next;
51#if defined(__APPLE__) || (defined(__linux) && defined(__PPC64__))
52 long condition_register;
53#endif
54 void *return_addr;
55};
56
57// Given a pointer to a stack frame, locate and return the calling
58// stackframe, or return NULL if no stackframe can be found. Perform sanity
59// checks (the strictness of which is controlled by the boolean parameter
60// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
61template<bool STRICT_UNWINDING>
62static layout_ppc *NextStackFrame(layout_ppc *current) {
63 uintptr_t old_sp = (uintptr_t)(current);
64 uintptr_t new_sp = (uintptr_t)(current->next);
65
66 // Check that the transition from frame pointer old_sp to frame
67 // pointer new_sp isn't clearly bogus
68 if (STRICT_UNWINDING) {
69 // With the stack growing downwards, older stack frame must be
70 // at a greater address that the current one.
71 if (new_sp <= old_sp)
72 return NULL;
73 // Assume stack frames larger than 100,000 bytes are bogus.
74 if (new_sp - old_sp > 100000)
75 return NULL;
76 } else {
77 // In the non-strict mode, allow discontiguous stack frames.
78 // (alternate-signal-stacks for example).
79 if (new_sp == old_sp)
80 return NULL;
81 // And allow frames upto about 1MB.
82 if ((new_sp > old_sp) && (new_sp - old_sp > 1000000))
83 return NULL;
84 }
85 if (new_sp & (sizeof(void *) - 1))
86 return NULL;
87 return current->next;
88}
89
90// This ensures that GetStackTrace stes up the Link Register properly.
91void StacktracePowerPCDummyFunction() __attribute__((noinline));
92void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
93#endif // BASE_STACKTRACE_POWERPC_INL_H_
94
95// Note: this part of the file is included several times.
96// Do not put globals below.
97
98// Load instruction used on top-of-stack get.
99#if defined(__PPC64__) || defined(__LP64__)
100# define LOAD "ld"
101#else
102# define LOAD "lwz"
103#endif
104
105#if defined(__linux__) && defined(__PPC__)
106# define TOP_STACK "%0,0(1)"
107#elif defined(__MACH__) && defined(__APPLE__)
108// Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
109// and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a
110// different asm syntax. I don't know quite the best way to discriminate
111// systems using the old as from the new one; I've gone with __APPLE__.
112// TODO(csilvers): use autoconf instead, to look for 'as --version' == 1 or 2
113# define TOP_STACK "%0,0(r1)"
114#endif
115
116
117
118// The following 4 functions are generated from the code below:
119// GetStack{Trace,Frames}()
120// GetStack{Trace,Frames}WithContext()
121//
122// These functions take the following args:
123// void** result: the stack-trace, as an array
124// int* sizes: the size of each stack frame, as an array
125// (GetStackFrames* only)
126// int max_depth: the size of the result (and sizes) array(s)
127// int skip_count: how many stack pointers to skip before storing in result
128// void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
129static int GET_STACK_TRACE_OR_FRAMES {
130 layout_ppc *current;
131 int n;
132
133 // Force GCC to spill LR.
134 asm volatile ("" : "=l"(current));
135
136 // Get the address on top-of-stack
137 asm volatile (LOAD " " TOP_STACK : "=r"(current));
138
139 StacktracePowerPCDummyFunction();
140
141 n = 0;
142 skip_count++; // skip parent's frame due to indirection in
143 // stacktrace.cc
144 while (current && n < max_depth) {
145
146 // The GetStackFrames routine is called when we are in some
147 // informational context (the failure signal handler for example).
148 // Use the non-strict unwinding rules to produce a stack trace
149 // that is as complete as possible (even if it contains a few
150 // bogus entries in some rare cases).
151 layout_ppc *next = NextStackFrame<!IS_STACK_FRAMES>(current);
152 if (skip_count > 0) {
153 skip_count--;
154 } else {
155 result[n] = current->return_addr;
156#if IS_STACK_FRAMES
157 if (next > current) {
158 sizes[n] = (uintptr_t)next - (uintptr_t)current;
159 } else {
160 // A frame-size of 0 is used to indicate unknown frame size.
161 sizes[n] = 0;
162 }
163#endif
164 n++;
165 }
166 current = next;
167 }
168
169 // It's possible the second-last stack frame can't return
170 // (that is, it's __libc_start_main), in which case
171 // the CRT startup code will have set its LR to 'NULL'.
172 if (n > 0 && result[n-1] == NULL)
173 n--;
174
175 return n;
176}