1 // Copyright 2007 and onwards Google Inc.
2 // All rights reserved.
4 // Author: Craig Silverstein
6 // Produce stack trace. I'm guessing (hoping!) the code is much like
7 // for x86. For apple machines, at least, it seems to be; see
8 // http://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html
9 // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
10 // Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882
13 #include <stdint.h> // for uintptr_t
14 #include "stacktrace.h"
16 _START_GOOGLE_NAMESPACE_
18 // Given a pointer to a stack frame, locate and return the calling
19 // stackframe, or return NULL if no stackframe can be found. Perform sanity
20 // checks (the strictness of which is controlled by the boolean parameter
21 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
22 template<bool STRICT_UNWINDING>
23 static void **NextStackFrame(void **old_sp) {
24 void **new_sp = (void **) *old_sp;
26 // Check that the transition from frame pointer old_sp to frame
27 // pointer new_sp isn't clearly bogus
28 if (STRICT_UNWINDING) {
29 // With the stack growing downwards, older stack frame must be
30 // at a greater address that the current one.
31 if (new_sp <= old_sp) return NULL;
32 // Assume stack frames larger than 100,000 bytes are bogus.
33 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
35 // In the non-strict mode, allow discontiguous stack frames.
36 // (alternate-signal-stacks for example).
37 if (new_sp == old_sp) return NULL;
38 // And allow frames upto about 1MB.
40 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
42 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
46 // This ensures that GetStackTrace stes up the Link Register properly.
47 void StacktracePowerPCDummyFunction() __attribute__((noinline));
48 void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
50 // If you change this function, also change GetStackFrames below.
51 int GetStackTrace(void** result, int max_depth, int skip_count) {
53 // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
54 // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a
55 // different asm syntax. I don't know quite the best way to discriminate
56 // systems using the old as from the new one; I've gone with __APPLE__.
58 __asm__ volatile ("mr %0,r1" : "=r" (sp));
60 __asm__ volatile ("mr %0,1" : "=r" (sp));
63 // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack
64 // entry that holds the return address of the subroutine call (what
65 // instruction we run after our function finishes). This is the
66 // same as the stack-pointer of our parent routine, which is what we
67 // want here. While the compiler will always(?) set up LR for
68 // subroutine calls, it may not for leaf functions (such as this one).
69 // This routine forces the compiler (at least gcc) to push it anyway.
70 StacktracePowerPCDummyFunction();
72 // The LR save area is used by the callee, so the top entry is bogus.
76 while (sp && n < max_depth) {
80 // PowerPC has 3 main ABIs, which say where in the stack the
81 // Link Register is. For DARWIN and AIX (used by apple and
82 // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc),
84 #if defined(_CALL_AIX) || defined(_CALL_DARWIN)
85 result[n++] = *(sp+2);
86 #elif defined(_CALL_SYSV)
87 result[n++] = *(sp+1);
88 #elif defined(__APPLE__) || (defined(__linux) && defined(__PPC64__))
89 // This check is in case the compiler doesn't define _CALL_AIX/etc.
90 result[n++] = *(sp+2);
91 #elif defined(__linux)
92 // This check is in case the compiler doesn't define _CALL_SYSV.
93 result[n++] = *(sp+1);
95 #error Need to specify the PPC ABI for your archiecture.
98 // Use strict unwinding rules.
99 sp = NextStackFrame<true>(sp);
104 // If you change this function, also change GetStackTrace above:
106 // This GetStackFrames routine shares a lot of code with GetStackTrace
107 // above. This code could have been refactored into a common routine,
108 // and then both GetStackTrace/GetStackFrames could call that routine.
109 // There are two problems with that:
111 // (1) The performance of the refactored-code suffers substantially - the
112 // refactored needs to be able to record the stack trace when called
113 // from GetStackTrace, and both the stack trace and stack frame sizes,
114 // when called from GetStackFrames - this introduces enough new
115 // conditionals that GetStackTrace performance can degrade by as much
118 // (2) Whether the refactored routine gets inlined into GetStackTrace and
119 // GetStackFrames depends on the compiler, and we can't guarantee the
120 // behavior either-way, even with "__attribute__ ((always_inline))"
121 // or "__attribute__ ((noinline))". But we need this guarantee or the
122 // frame counts may be off by one.
124 // Both (1) and (2) can be addressed without this code duplication, by
125 // clever use of template functions, and by defining GetStackTrace and
126 // GetStackFrames as macros that expand to these template functions.
127 // However, this approach comes with its own set of problems - namely,
128 // macros and preprocessor trouble - for example, if GetStackTrace
129 // and/or GetStackFrames is ever defined as a member functions in some
130 // class, we are in trouble.
131 int GetStackFrames(void** pcs, int *sizes, int max_depth, int skip_count) {
134 __asm__ volatile ("mr %0,r1" : "=r" (sp));
136 __asm__ volatile ("mr %0,1" : "=r" (sp));
139 StacktracePowerPCDummyFunction();
140 // Note we do *not* increment skip_count here for the SYSV ABI. If
141 // we did, the list of stack frames wouldn't properly match up with
142 // the list of return addresses. Note this means the top pc entry
143 // is probably bogus for linux/ppc (and other SYSV-ABI systems).
146 while (sp && n < max_depth) {
147 // The GetStackFrames routine is called when we are in some
148 // informational context (the failure signal handler for example).
149 // Use the non-strict unwinding rules to produce a stack trace
150 // that is as complete as possible (even if it contains a few bogus
151 // entries in some rare cases).
152 void **next_sp = NextStackFrame<false>(sp);
153 if (skip_count > 0) {
156 #if defined(_CALL_AIX) || defined(_CALL_DARWIN)
158 #elif defined(_CALL_SYSV)
160 #elif defined(__APPLE__) || (defined(__linux) && defined(__PPC64__))
161 // This check is in case the compiler doesn't define _CALL_AIX/etc.
163 #elif defined(__linux)
164 // This check is in case the compiler doesn't define _CALL_SYSV.
167 #error Need to specify the PPC ABI for your archiecture.
170 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
172 // A frame-size of 0 is used to indicate unknown frame size.
182 _END_GOOGLE_NAMESPACE_