1 // Copyright (c) 2000 - 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Produce stack trace
32 #include <stdint.h> // for uintptr_t
34 #include "utilities.h" // for OS_* macros
36 #if !defined(OS_WINDOWS)
41 #include <stdio.h> // for NULL
42 #include "stacktrace.h"
44 _START_GOOGLE_NAMESPACE_
46 // Given a pointer to a stack frame, locate and return the calling
47 // stackframe, or return NULL if no stackframe can be found. Perform sanity
48 // checks (the strictness of which is controlled by the boolean parameter
49 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
50 template<bool STRICT_UNWINDING>
51 static void **NextStackFrame(void **old_sp) {
52 void **new_sp = (void **) *old_sp;
54 // Check that the transition from frame pointer old_sp to frame
55 // pointer new_sp isn't clearly bogus
56 if (STRICT_UNWINDING) {
57 // With the stack growing downwards, older stack frame must be
58 // at a greater address that the current one.
59 if (new_sp <= old_sp) return NULL;
60 // Assume stack frames larger than 100,000 bytes are bogus.
61 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
63 // In the non-strict mode, allow discontiguous stack frames.
64 // (alternate-signal-stacks for example).
65 if (new_sp == old_sp) return NULL;
66 // And allow frames upto about 1MB.
68 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
70 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
72 // On 64-bit machines, the stack pointer can be very close to
73 // 0xffffffff, so we explicitly check for a pointer into the
74 // last two pages in the address space
75 if ((uintptr_t)new_sp >= 0xffffe000) return NULL;
77 #if !defined(OS_WINDOWS)
78 if (!STRICT_UNWINDING) {
79 // Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test
80 // on AMD-based machines with VDSO-enabled kernels.
81 // Make an extra sanity check to insure new_sp is readable.
82 // Note: NextStackFrame<false>() is only called while the program
83 // is already on its last leg, so it's ok to be slow here.
84 static int page_size = getpagesize();
85 void *new_sp_aligned = (void *)((uintptr_t)new_sp & ~(page_size - 1));
86 if (msync(new_sp_aligned, page_size, MS_ASYNC) == -1)
93 // If you change this function, also change GetStackFrames below.
94 int GetStackTrace(void** result, int max_depth, int skip_count) {
97 // Stack frame format:
98 // sp[0] pointer to previous frame
99 // sp[1] caller address
100 // sp[2] first argument
102 sp = (void **)&result - 2;
106 // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
108 // Move the value of the register %rbp into the local variable rbp.
109 // We need 'volatile' to prevent this instruction from getting moved
110 // around during optimization to before function prologue is done.
111 // An alternative way to achieve this
112 // would be (before this __asm__ instruction) to call Noop() defined as
113 // static void Noop() __attribute__ ((noinline)); // prevent inlining
114 // static void Noop() { asm(""); } // prevent optimizing-away
115 __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
116 // Arguments are passed in registers on x86-64, so we can't just
117 // offset from &result
122 while (sp && n < max_depth) {
123 if (*(sp+1) == (void *)0) {
124 // In 64-bit code, we often see a frame that
125 // points to itself and has a return address of 0.
128 if (skip_count > 0) {
131 result[n++] = *(sp+1);
133 // Use strict unwinding rules.
134 sp = NextStackFrame<true>(sp);
139 _END_GOOGLE_NAMESPACE_