cmake: do not hardcode relative install directories
[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 #ifdef __i386__
97   // Stack frame format:
98   //    sp[0]   pointer to previous frame
99   //    sp[1]   caller address
100   //    sp[2]   first argument
101   //    ...
102   sp = (void **)&result - 2;
103 #endif
104
105 #ifdef __x86_64__
106   // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
107   unsigned long rbp;
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
118   sp = (void **) rbp;
119 #endif
120
121   int n = 0;
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.
126       break;
127     }
128     if (skip_count > 0) {
129       skip_count--;
130     } else {
131       result[n++] = *(sp+1);
132     }
133     // Use strict unwinding rules.
134     sp = NextStackFrame<true>(sp);
135   }
136   return n;
137 }
138
139 _END_GOOGLE_NAMESPACE_