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 _END_GOOGLE_NAMESPACE_