glog 0.1
[platform/upstream/glog.git] / src / stacktrace_powerpc-inl.h
1 // Copyright 2007 and onwards Google Inc.
2 // All rights reserved.
3 //
4 // Author: Craig Silverstein
5 //
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
11
12 #include <stdio.h>
13 #include <stdint.h>   // for uintptr_t
14 #include "stacktrace.h"
15
16 _START_GOOGLE_NAMESPACE_
17
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;
25
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;
34   } else {
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.
39     if ((new_sp > old_sp)
40         && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
41   }
42   if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
43   return new_sp;
44 }
45
46 // This ensures that GetStackTrace stes up the Link Register properly.
47 void StacktracePowerPCDummyFunction() __attribute__((noinline));
48 void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
49
50 // If you change this function, also change GetStackFrames below.
51 int GetStackTrace(void** result, int max_depth, int skip_count) {
52   void **sp;
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__.
57 #ifdef __APPLE__
58   __asm__ volatile ("mr %0,r1" : "=r" (sp));
59 #else
60   __asm__ volatile ("mr %0,1" : "=r" (sp));
61 #endif
62
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();
71
72   // The LR save area is used by the callee, so the top entry is bogus.
73   skip_count++;
74
75   int n = 0;
76   while (sp && n < max_depth) {
77     if (skip_count > 0) {
78       skip_count--;
79     } else {
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),
83       // it's in sp[1].
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);
94 #else
95 #error Need to specify the PPC ABI for your archiecture.
96 #endif
97     }
98     // Use strict unwinding rules.
99     sp = NextStackFrame<true>(sp);
100   }
101   return n;
102 }
103
104 // If you change this function, also change GetStackTrace above:
105 //
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:
110 //
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
116 //     as 50%.
117 //
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.
123 //
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) {
132   void **sp;
133 #ifdef __APPLE__
134   __asm__ volatile ("mr %0,r1" : "=r" (sp));
135 #else
136   __asm__ volatile ("mr %0,1" : "=r" (sp));
137 #endif
138
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).
144
145   int n = 0;
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) {
154       skip_count--;
155     } else {
156 #if defined(_CALL_AIX) || defined(_CALL_DARWIN)
157       pcs[n++] = *(sp+2);
158 #elif defined(_CALL_SYSV)
159       pcs[n++] = *(sp+1);
160 #elif defined(__APPLE__) || (defined(__linux) && defined(__PPC64__))
161       // This check is in case the compiler doesn't define _CALL_AIX/etc.
162       pcs[n++] = *(sp+2);
163 #elif defined(__linux)
164       // This check is in case the compiler doesn't define _CALL_SYSV.
165       pcs[n++] = *(sp+1);
166 #else
167 #error Need to specify the PPC ABI for your archiecture.
168 #endif
169       if (next_sp > sp) {
170         sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
171       } else {
172         // A frame-size of 0 is used to indicate unknown frame size.
173         sizes[n] = 0;
174       }
175       n++;
176     }
177     sp = next_sp;
178   }
179   return n;
180 }
181
182 _END_GOOGLE_NAMESPACE_