676ce354f0f76fd6d016d2102655620ccd654e84
[platform/upstream/glog.git] / src / stacktrace_x86-inl.h
1 // Copyright 2000 - 2007 Google Inc.
2 // All rights reserved.
3 //
4 // Produce stack trace
5
6 #include <stdint.h>   // for uintptr_t
7
8 #include "utilities.h"   // for OS_* macros
9
10 #if !defined(OS_WINDOWS)
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #endif
14
15 #include <stdio.h>  // for NULL
16 #include "stacktrace.h"
17
18 _START_GOOGLE_NAMESPACE_
19
20 // Given a pointer to a stack frame, locate and return the calling
21 // stackframe, or return NULL if no stackframe can be found. Perform sanity
22 // checks (the strictness of which is controlled by the boolean parameter
23 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
24 template<bool STRICT_UNWINDING>
25 static void **NextStackFrame(void **old_sp) {
26   void **new_sp = (void **) *old_sp;
27
28   // Check that the transition from frame pointer old_sp to frame
29   // pointer new_sp isn't clearly bogus
30   if (STRICT_UNWINDING) {
31     // With the stack growing downwards, older stack frame must be
32     // at a greater address that the current one.
33     if (new_sp <= old_sp) return NULL;
34     // Assume stack frames larger than 100,000 bytes are bogus.
35     if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
36   } else {
37     // In the non-strict mode, allow discontiguous stack frames.
38     // (alternate-signal-stacks for example).
39     if (new_sp == old_sp) return NULL;
40     // And allow frames upto about 1MB.
41     if ((new_sp > old_sp)
42         && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
43   }
44   if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
45 #ifdef __i386__
46   // On 64-bit machines, the stack pointer can be very close to
47   // 0xffffffff, so we explicitly check for a pointer into the
48   // last two pages in the address space
49   if ((uintptr_t)new_sp >= 0xffffe000) return NULL;
50 #endif
51 #if !defined(OS_WINDOWS)
52   if (!STRICT_UNWINDING) {
53     // Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test
54     // on AMD-based machines with VDSO-enabled kernels.
55     // Make an extra sanity check to insure new_sp is readable.
56     // Note: NextStackFrame<false>() is only called while the program
57     //       is already on its last leg, so it's ok to be slow here.
58     static int page_size = getpagesize();
59     void *new_sp_aligned = (void *)((uintptr_t)new_sp & ~(page_size - 1));
60     if (msync(new_sp_aligned, page_size, MS_ASYNC) == -1)
61       return NULL;
62   }
63 #endif
64   return new_sp;
65 }
66
67 // If you change this function, also change GetStackFrames below.
68 int GetStackTrace(void** result, int max_depth, int skip_count) {
69   void **sp;
70 #ifdef __i386__
71   // Stack frame format:
72   //    sp[0]   pointer to previous frame
73   //    sp[1]   caller address
74   //    sp[2]   first argument
75   //    ...
76   sp = (void **)&result - 2;
77 #endif
78
79 #ifdef __x86_64__
80   // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
81   unsigned long rbp;
82   // Move the value of the register %rbp into the local variable rbp.
83   // We need 'volatile' to prevent this instruction from getting moved
84   // around during optimization to before function prologue is done.
85   // An alternative way to achieve this
86   // would be (before this __asm__ instruction) to call Noop() defined as
87   //   static void Noop() __attribute__ ((noinline));  // prevent inlining
88   //   static void Noop() { asm(""); }  // prevent optimizing-away
89   __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
90   // Arguments are passed in registers on x86-64, so we can't just
91   // offset from &result
92   sp = (void **) rbp;
93 #endif
94
95   int n = 0;
96   while (sp && n < max_depth) {
97     if (*(sp+1) == (void *)0) {
98       // In 64-bit code, we often see a frame that
99       // points to itself and has a return address of 0.
100       break;
101     }
102     if (skip_count > 0) {
103       skip_count--;
104     } else {
105       result[n++] = *(sp+1);
106     }
107     // Use strict unwinding rules.
108     sp = NextStackFrame<true>(sp);
109   }
110   return n;
111 }
112
113 // If you change this function, also change GetStackTrace above:
114 //
115 // This GetStackFrames routine shares a lot of code with GetStackTrace
116 // above. This code could have been refactored into a common routine,
117 // and then both GetStackTrace/GetStackFrames could call that routine.
118 // There are two problems with that:
119 //
120 // (1) The performance of the refactored-code suffers substantially - the
121 //     refactored needs to be able to record the stack trace when called
122 //     from GetStackTrace, and both the stack trace and stack frame sizes,
123 //     when called from GetStackFrames - this introduces enough new
124 //     conditionals that GetStackTrace performance can degrade by as much
125 //     as 50%.
126 //
127 // (2) Whether the refactored routine gets inlined into GetStackTrace and
128 //     GetStackFrames depends on the compiler, and we can't guarantee the
129 //     behavior either-way, even with "__attribute__ ((always_inline))"
130 //     or "__attribute__ ((noinline))". But we need this guarantee or the
131 //     frame counts may be off by one.
132 //
133 // Both (1) and (2) can be addressed without this code duplication, by
134 // clever use of template functions, and by defining GetStackTrace and
135 // GetStackFrames as macros that expand to these template functions.
136 // However, this approach comes with its own set of problems - namely,
137 // macros and  preprocessor trouble - for example,  if GetStackTrace
138 // and/or GetStackFrames is ever defined as a member functions in some
139 // class, we are in trouble.
140 int GetStackFrames(void** pcs, int* sizes, int max_depth, int skip_count) {
141   void **sp;
142 #ifdef __i386__
143   // Stack frame format:
144   //    sp[0]   pointer to previous frame
145   //    sp[1]   caller address
146   //    sp[2]   first argument
147   //    ...
148   sp = (void **)&pcs - 2;
149 #endif
150
151 #ifdef __x86_64__
152   // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
153   unsigned long rbp;
154   // Move the value of the register %rbp into the local variable rbp.
155   // We need 'volatile' to prevent this instruction from getting moved
156   // around during optimization to before function prologue is done.
157   // An alternative way to achieve this
158   // would be (before this __asm__ instruction) to call Noop() defined as
159   //   static void Noop() __attribute__ ((noinline));  // prevent inlining
160   //   static void Noop() { asm(""); }  // prevent optimizing-away
161   __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
162   // Arguments are passed in registers on x86-64, so we can't just
163   // offset from &pcs
164   sp = (void **) rbp;
165 #endif
166
167   int n = 0;
168   while (sp && n < max_depth) {
169     if (*(sp+1) == (void *)0) {
170       // In 64-bit code, we often see a frame that
171       // points to itself and has a return address of 0.
172       break;
173     }
174     // The GetStackFrames routine is called when we are in some
175     // informational context (the failure signal handler for example).
176     // Use the non-strict unwinding rules to produce a stack trace
177     // that is as complete as possible (even if it contains a few bogus
178     // entries in some rare cases).
179     void **next_sp = NextStackFrame<false>(sp);
180     if (skip_count > 0) {
181       skip_count--;
182     } else {
183       pcs[n] = *(sp+1);
184       if (next_sp > sp) {
185         sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
186       } else {
187         // A frame-size of 0 is used to indicate unknown frame size.
188         sizes[n] = 0;
189       }
190       n++;
191     }
192     sp = next_sp;
193   }
194   return n;
195 }
196
197 _END_GOOGLE_NAMESPACE_