|
Packit Service |
9def5d |
// Copyright (c) 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 |
// Author: Craig Silverstein
|
|
Packit Service |
9def5d |
//
|
|
Packit Service |
9def5d |
// Produce stack trace. I'm guessing (hoping!) the code is much like
|
|
Packit Service |
9def5d |
// for x86. For apple machines, at least, it seems to be; see
|
|
Packit Service |
9def5d |
// http://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html
|
|
Packit Service |
9def5d |
// http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
|
|
Packit Service |
9def5d |
// Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <stdio.h>
|
|
Packit Service |
9def5d |
#include <stdint.h> // for uintptr_t
|
|
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 |
return new_sp;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// This ensures that GetStackTrace stes up the Link Register properly.
|
|
Packit Service |
9def5d |
void StacktracePowerPCDummyFunction() __attribute__((noinline));
|
|
Packit Service |
9def5d |
void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
|
|
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 |
// Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
|
|
Packit Service |
9def5d |
// and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a
|
|
Packit Service |
9def5d |
// different asm syntax. I don't know quite the best way to discriminate
|
|
Packit Service |
9def5d |
// systems using the old as from the new one; I've gone with __APPLE__.
|
|
Packit Service |
9def5d |
#ifdef __APPLE__
|
|
Packit Service |
9def5d |
__asm__ volatile ("mr %0,r1" : "=r" (sp));
|
|
Packit Service |
9def5d |
#else
|
|
Packit Service |
9def5d |
__asm__ volatile ("mr %0,1" : "=r" (sp));
|
|
Packit Service |
9def5d |
#endif
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// On PowerPC, the "Link Register" or "Link Record" (LR), is a stack
|
|
Packit Service |
9def5d |
// entry that holds the return address of the subroutine call (what
|
|
Packit Service |
9def5d |
// instruction we run after our function finishes). This is the
|
|
Packit Service |
9def5d |
// same as the stack-pointer of our parent routine, which is what we
|
|
Packit Service |
9def5d |
// want here. While the compiler will always(?) set up LR for
|
|
Packit Service |
9def5d |
// subroutine calls, it may not for leaf functions (such as this one).
|
|
Packit Service |
9def5d |
// This routine forces the compiler (at least gcc) to push it anyway.
|
|
Packit Service |
9def5d |
StacktracePowerPCDummyFunction();
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// The LR save area is used by the callee, so the top entry is bogus.
|
|
Packit Service |
9def5d |
skip_count++;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
int n = 0;
|
|
Packit Service |
9def5d |
while (sp && n < max_depth) {
|
|
Packit Service |
9def5d |
if (skip_count > 0) {
|
|
Packit Service |
9def5d |
skip_count--;
|
|
Packit Service |
9def5d |
} else {
|
|
Packit Service |
9def5d |
// PowerPC has 3 main ABIs, which say where in the stack the
|
|
Packit Service |
9def5d |
// Link Register is. For DARWIN and AIX (used by apple and
|
|
Packit Service |
9def5d |
// linux ppc64), it's in sp[2]. For SYSV (used by linux ppc),
|
|
Packit Service |
9def5d |
// it's in sp[1].
|
|
Packit Service |
9def5d |
#if defined(_CALL_AIX) || defined(_CALL_DARWIN)
|
|
Packit Service |
9def5d |
result[n++] = *(sp+2);
|
|
Packit Service |
9def5d |
#elif defined(_CALL_SYSV)
|
|
Packit Service |
9def5d |
result[n++] = *(sp+1);
|
|
Packit Service |
9def5d |
#elif defined(__APPLE__) || ((defined(__linux) || defined(__linux__)) && defined(__PPC64__))
|
|
Packit Service |
9def5d |
// This check is in case the compiler doesn't define _CALL_AIX/etc.
|
|
Packit Service |
9def5d |
result[n++] = *(sp+2);
|
|
Packit Service |
9def5d |
#elif defined(__linux)
|
|
Packit Service |
9def5d |
// This check is in case the compiler doesn't define _CALL_SYSV.
|
|
Packit Service |
9def5d |
result[n++] = *(sp+1);
|
|
Packit Service |
9def5d |
#else
|
|
Packit Service |
9def5d |
#error Need to specify the PPC ABI for your archiecture.
|
|
Packit Service |
9def5d |
#endif
|
|
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_
|