1 // Copyright 2000 - 2007 Google Inc.
2 // All rights reserved.
6 #include <stdint.h> // for uintptr_t
8 #include "utilities.h" // for OS_* macros
10 #if !defined(OS_WINDOWS)
15 #include <stdio.h> // for NULL
16 #include "stacktrace.h"
18 _START_GOOGLE_NAMESPACE_
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;
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;
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.
42 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
44 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
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;
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)
67 // If you change this function, also change GetStackFrames below.
68 int GetStackTrace(void** result, int max_depth, int skip_count) {
71 // Stack frame format:
72 // sp[0] pointer to previous frame
73 // sp[1] caller address
74 // sp[2] first argument
76 sp = (void **)&result - 2;
80 // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
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
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.
102 if (skip_count > 0) {
105 result[n++] = *(sp+1);
107 // Use strict unwinding rules.
108 sp = NextStackFrame<true>(sp);
113 // If you change this function, also change GetStackTrace above:
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:
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
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.
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) {
143 // Stack frame format:
144 // sp[0] pointer to previous frame
145 // sp[1] caller address
146 // sp[2] first argument
148 sp = (void **)&pcs - 2;
152 // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
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
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.
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) {
185 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
187 // A frame-size of 0 is used to indicate unknown frame size.
197 _END_GOOGLE_NAMESPACE_