|
Packit Service |
9def5d |
// Copyright (c) 2000 - 2007, Google Inc.
|
|
Packit Service |
9def5d |
// All rights reserved.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
9def5d |
// modification, are permitted provided that the following conditions are
|
|
Packit Service |
9def5d |
// met:
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// * Redistributions of source code must retain the above copyright
|
|
Packit Service |
9def5d |
// notice, this list of conditions and the following disclaimer.
|
|
Packit Service |
9def5d |
// * Redistributions in binary form must reproduce the above
|
|
Packit Service |
9def5d |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit Service |
9def5d |
// in the documentation and/or other materials provided with the
|
|
Packit Service |
9def5d |
// distribution.
|
|
Packit Service |
9def5d |
// * Neither the name of Google Inc. nor the names of its
|
|
Packit Service |
9def5d |
// contributors may be used to endorse or promote products derived from
|
|
Packit Service |
9def5d |
// this software without specific prior written permission.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
9def5d |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
9def5d |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit Service |
9def5d |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit Service |
9def5d |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit Service |
9def5d |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit Service |
9def5d |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit Service |
9def5d |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit Service |
9def5d |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit Service |
9def5d |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit Service |
9def5d |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Produce stack trace
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <stdint.h> // for uintptr_t
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include "utilities.h" // for OS_* macros
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#if !defined(OS_WINDOWS)
|
|
Packit Service |
9def5d |
#include <unistd.h>
|
|
Packit Service |
9def5d |
#include <sys/mman.h>
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <stdio.h> // for NULL
|
|
Packit Service |
9def5d |
#include "stacktrace.h"
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_START_GOOGLE_NAMESPACE_
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Given a pointer to a stack frame, locate and return the calling
|
|
Packit Service |
9def5d |
// stackframe, or return NULL if no stackframe can be found. Perform sanity
|
|
Packit Service |
9def5d |
// checks (the strictness of which is controlled by the boolean parameter
|
|
Packit Service |
9def5d |
// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
|
|
Packit Service |
9def5d |
template<bool STRICT_UNWINDING>
|
|
Packit Service |
9def5d |
static void **NextStackFrame(void **old_sp) {
|
|
Packit Service |
9def5d |
void **new_sp = (void **) *old_sp;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Check that the transition from frame pointer old_sp to frame
|
|
Packit Service |
9def5d |
// pointer new_sp isn't clearly bogus
|
|
Packit Service |
9def5d |
if (STRICT_UNWINDING) {
|
|
Packit Service |
9def5d |
// With the stack growing downwards, older stack frame must be
|
|
Packit Service |
9def5d |
// at a greater address that the current one.
|
|
Packit Service |
9def5d |
if (new_sp <= old_sp) return NULL;
|
|
Packit Service |
9def5d |
// Assume stack frames larger than 100,000 bytes are bogus.
|
|
Packit Service |
9def5d |
if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
|
|
Packit Service |
9def5d |
} else {
|
|
Packit Service |
9def5d |
// In the non-strict mode, allow discontiguous stack frames.
|
|
Packit Service |
9def5d |
// (alternate-signal-stacks for example).
|
|
Packit Service |
9def5d |
if (new_sp == old_sp) return NULL;
|
|
Packit Service |
9def5d |
// And allow frames upto about 1MB.
|
|
Packit Service |
9def5d |
if ((new_sp > old_sp)
|
|
Packit Service |
9def5d |
&& ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
|
|
Packit Service |
9def5d |
#ifdef __i386__
|
|
Packit Service |
9def5d |
// On 64-bit machines, the stack pointer can be very close to
|
|
Packit Service |
9def5d |
// 0xffffffff, so we explicitly check for a pointer into the
|
|
Packit Service |
9def5d |
// last two pages in the address space
|
|
Packit Service |
9def5d |
if ((uintptr_t)new_sp >= 0xffffe000) return NULL;
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
#if !defined(OS_WINDOWS)
|
|
Packit Service |
9def5d |
if (!STRICT_UNWINDING) {
|
|
Packit Service |
9def5d |
// Lax sanity checks cause a crash in 32-bit tcmalloc/crash_reason_test
|
|
Packit Service |
9def5d |
// on AMD-based machines with VDSO-enabled kernels.
|
|
Packit Service |
9def5d |
// Make an extra sanity check to insure new_sp is readable.
|
|
Packit Service |
9def5d |
// Note: NextStackFrame<false>() is only called while the program
|
|
Packit Service |
9def5d |
// is already on its last leg, so it's ok to be slow here.
|
|
Packit Service |
9def5d |
static int page_size = getpagesize();
|
|
Packit Service |
9def5d |
void *new_sp_aligned = (void *)((uintptr_t)new_sp & ~(page_size - 1));
|
|
Packit Service |
9def5d |
if (msync(new_sp_aligned, page_size, MS_ASYNC) == -1)
|
|
Packit Service |
9def5d |
return NULL;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
return new_sp;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// If you change this function, also change GetStackFrames below.
|
|
Packit Service |
9def5d |
int GetStackTrace(void** result, int max_depth, int skip_count) {
|
|
Packit Service |
9def5d |
void **sp;
|
|
Packit Service |
9def5d |
#ifdef __i386__
|
|
Packit Service |
9def5d |
// Stack frame format:
|
|
Packit Service |
9def5d |
// sp[0] pointer to previous frame
|
|
Packit Service |
9def5d |
// sp[1] caller address
|
|
Packit Service |
9def5d |
// sp[2] first argument
|
|
Packit Service |
9def5d |
// ...
|
|
Packit Service |
9def5d |
sp = (void **)&result - 2;
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#ifdef __x86_64__
|
|
Packit Service |
9def5d |
// __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8
|
|
Packit Service |
9def5d |
unsigned long rbp;
|
|
Packit Service |
9def5d |
// Move the value of the register %rbp into the local variable rbp.
|
|
Packit Service |
9def5d |
// We need 'volatile' to prevent this instruction from getting moved
|
|
Packit Service |
9def5d |
// around during optimization to before function prologue is done.
|
|
Packit Service |
9def5d |
// An alternative way to achieve this
|
|
Packit Service |
9def5d |
// would be (before this __asm__ instruction) to call Noop() defined as
|
|
Packit Service |
9def5d |
// static void Noop() __attribute__ ((noinline)); // prevent inlining
|
|
Packit Service |
9def5d |
// static void Noop() { asm(""); } // prevent optimizing-away
|
|
Packit Service |
9def5d |
__asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
|
|
Packit Service |
9def5d |
// Arguments are passed in registers on x86-64, so we can't just
|
|
Packit Service |
9def5d |
// offset from &result
|
|
Packit Service |
9def5d |
sp = (void **) rbp;
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
int n = 0;
|
|
Packit Service |
9def5d |
while (sp && n < max_depth) {
|
|
Packit Service |
9def5d |
if (*(sp+1) == (void *)0) {
|
|
Packit Service |
9def5d |
// In 64-bit code, we often see a frame that
|
|
Packit Service |
9def5d |
// points to itself and has a return address of 0.
|
|
Packit Service |
9def5d |
break;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
if (skip_count > 0) {
|
|
Packit Service |
9def5d |
skip_count--;
|
|
Packit Service |
9def5d |
} else {
|
|
Packit Service |
9def5d |
result[n++] = *(sp+1);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
// Use strict unwinding rules.
|
|
Packit Service |
9def5d |
sp = NextStackFrame<true>(sp);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
return n;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
_END_GOOGLE_NAMESPACE_
|