blob: 48e87f7a143d6f160da3ff7069e9e4a72c8d3cde [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// Copyright (c) 2000 - 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29//
30// Produce stack trace
31
32#include <stdint.h> // for uintptr_t
33
34#include "utilities.h" // for OS_* macros
35
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070036#if !defined(GLOG_OS_WINDOWS)
Austin Schuh906616c2019-01-21 20:25:11 -080037#include <unistd.h>
38#include <sys/mman.h>
39#endif
40
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070041#include <cstdio> // for NULL
Austin Schuh906616c2019-01-21 20:25:11 -080042#include "stacktrace.h"
43
44_START_GOOGLE_NAMESPACE_
45
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.
50template<bool STRICT_UNWINDING>
51static void **NextStackFrame(void **old_sp) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070052 void **new_sp = static_cast<void **>(*old_sp);
Austin Schuh906616c2019-01-21 20:25:11 -080053
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.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070061 if (reinterpret_cast<uintptr_t>(new_sp) -
62 reinterpret_cast<uintptr_t>(old_sp) >
63 100000)
64 return NULL;
Austin Schuh906616c2019-01-21 20:25:11 -080065 } else {
66 // In the non-strict mode, allow discontiguous stack frames.
67 // (alternate-signal-stacks for example).
68 if (new_sp == old_sp) return NULL;
69 // And allow frames upto about 1MB.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070070 if ((new_sp > old_sp) && (reinterpret_cast<uintptr_t>(new_sp) -
71 reinterpret_cast<uintptr_t>(old_sp) >
72 1000000))
73 return NULL;
Austin Schuh906616c2019-01-21 20:25:11 -080074 }
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070075 if (reinterpret_cast<uintptr_t>(new_sp) & (sizeof(void *) - 1)) return NULL;
Austin Schuh906616c2019-01-21 20:25:11 -080076#ifdef __i386__
77 // On 64-bit machines, the stack pointer can be very close to
78 // 0xffffffff, so we explicitly check for a pointer into the
79 // last two pages in the address space
80 if ((uintptr_t)new_sp >= 0xffffe000) return NULL;
81#endif
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070082#if !defined(GLOG_OS_WINDOWS)
Austin Schuh906616c2019-01-21 20:25:11 -080083 if (!STRICT_UNWINDING) {
84 // Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test
85 // on AMD-based machines with VDSO-enabled kernels.
86 // Make an extra sanity check to insure new_sp is readable.
87 // Note: NextStackFrame<false>() is only called while the program
88 // is already on its last leg, so it's ok to be slow here.
89 static int page_size = getpagesize();
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070090 void *new_sp_aligned =
91 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(new_sp) &
92 static_cast<uintptr_t>(~(page_size - 1)));
93 if (msync(new_sp_aligned, static_cast<size_t>(page_size), MS_ASYNC) == -1) {
Austin Schuh906616c2019-01-21 20:25:11 -080094 return NULL;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070095 }
Austin Schuh906616c2019-01-21 20:25:11 -080096 }
97#endif
98 return new_sp;
99}
100
101// If you change this function, also change GetStackFrames below.
102int GetStackTrace(void** result, int max_depth, int skip_count) {
103 void **sp;
104
105#ifdef __GNUC__
106#if __GNUC__ * 100 + __GNUC_MINOR__ >= 402
107#define USE_BUILTIN_FRAME_ADDRESS
108#endif
109#endif
110
111#ifdef USE_BUILTIN_FRAME_ADDRESS
112 sp = reinterpret_cast<void**>(__builtin_frame_address(0));
113#elif defined(__i386__)
114 // Stack frame format:
115 // sp[0] pointer to previous frame
116 // sp[1] caller address
117 // sp[2] first argument
118 // ...
119 sp = (void **)&result - 2;
120#elif defined(__x86_64__)
121 // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
122 unsigned long rbp;
123 // Move the value of the register %rbp into the local variable rbp.
124 // We need 'volatile' to prevent this instruction from getting moved
125 // around during optimization to before function prologue is done.
126 // An alternative way to achieve this
127 // would be (before this __asm__ instruction) to call Noop() defined as
128 // static void Noop() __attribute__ ((noinline)); // prevent inlining
129 // static void Noop() { asm(""); } // prevent optimizing-away
130 __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
131 // Arguments are passed in registers on x86-64, so we can't just
132 // offset from &result
133 sp = (void **) rbp;
134#endif
135
136 int n = 0;
137 while (sp && n < max_depth) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700138 if (*(sp + 1) == NULL) {
Austin Schuh906616c2019-01-21 20:25:11 -0800139 // In 64-bit code, we often see a frame that
140 // points to itself and has a return address of 0.
141 break;
142 }
143 if (skip_count > 0) {
144 skip_count--;
145 } else {
146 result[n++] = *(sp+1);
147 }
148 // Use strict unwinding rules.
149 sp = NextStackFrame<true>(sp);
150 }
151 return n;
152}
153
154_END_GOOGLE_NAMESPACE_