stacktrace_x86-inl.h
Go to the documentation of this file.
1 // 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 
36 #if !defined(GLOG_OS_WINDOWS)
37 #include <unistd.h>
38 #include <sys/mman.h>
39 #endif
40 
41 #include <cstdio> // for NULL
42 #include "stacktrace.h"
43 
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.
50 template<bool STRICT_UNWINDING>
51 static void **NextStackFrame(void **old_sp) {
52  void **new_sp = static_cast<void **>(*old_sp);
53 
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;
62  } else {
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.
67  if ((new_sp > old_sp)
68  && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
69  }
70  if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
71 #ifdef __i386__
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;
76 #endif
77 #if !defined(GLOG_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) {
87  return NULL;
88  }
89  }
90 #endif
91  return new_sp;
92 }
93 
94 // If you change this function, also change GetStackFrames below.
95 int GetStackTrace(void** result, int max_depth, int skip_count) {
96  void **sp;
97 
98 #ifdef __GNUC__
99 #if __GNUC__ * 100 + __GNUC_MINOR__ >= 402
100 #define USE_BUILTIN_FRAME_ADDRESS
101 #endif
102 #endif
103 
104 #ifdef USE_BUILTIN_FRAME_ADDRESS
105  sp = reinterpret_cast<void**>(__builtin_frame_address(0));
106 #elif defined(__i386__)
107  // Stack frame format:
108  // sp[0] pointer to previous frame
109  // sp[1] caller address
110  // sp[2] first argument
111  // ...
112  sp = (void **)&result - 2;
113 #elif defined(__x86_64__)
114  // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
115  unsigned long rbp;
116  // Move the value of the register %rbp into the local variable rbp.
117  // We need 'volatile' to prevent this instruction from getting moved
118  // around during optimization to before function prologue is done.
119  // An alternative way to achieve this
120  // would be (before this __asm__ instruction) to call Noop() defined as
121  // static void Noop() __attribute__ ((noinline)); // prevent inlining
122  // static void Noop() { asm(""); } // prevent optimizing-away
123  __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
124  // Arguments are passed in registers on x86-64, so we can't just
125  // offset from &result
126  sp = (void **) rbp;
127 #endif
128 
129  int n = 0;
130  while (sp && n < max_depth) {
131  if (*(sp+1) == (void *)0) {
132  // In 64-bit code, we often see a frame that
133  // points to itself and has a return address of 0.
134  break;
135  }
136  if (skip_count > 0) {
137  skip_count--;
138  } else {
139  result[n++] = *(sp+1);
140  }
141  // Use strict unwinding rules.
142  sp = NextStackFrame<true>(sp);
143  }
144  return n;
145 }
146 
147 _END_GOOGLE_NAMESPACE_
NULL
NULL
Definition: test_security_zap.cpp:405
NextStackFrame
static _START_GOOGLE_NAMESPACE_ void ** NextStackFrame(void **old_sp)
Definition: stacktrace_x86-inl.h:51
utilities.h
GetStackTrace
int GetStackTrace(void **result, int max_depth, int skip_count)
Definition: stacktrace_x86-inl.h:95
n
GLdouble n
Definition: glcorearb.h:4153
_START_GOOGLE_NAMESPACE_
Definition: signalhandler.cc:51
stacktrace.h


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58