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 _END_GOOGLE_NAMESPACE_