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