1// Copyright (c) 2005, 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// --- 31// Author: Sanjay Ghemawat 32// 33// Portable implementation - just use glibc 34// 35// Note: The glibc implementation may cause a call to malloc. 36// This can cause a deadlock in HeapProfiler. 37 38#ifndef BASE_STACKTRACE_GENERIC_INL_H_ 39#define BASE_STACKTRACE_GENERIC_INL_H_ 40// Note: this file is included into stacktrace.cc more than once. 41// Anything that should only be defined once should be here: 42 43#include <execinfo.h> 44#include <string.h> 45#include "gperftools/stacktrace.h" 46#endif // BASE_STACKTRACE_GENERIC_INL_H_ 47 48// Note: this part of the file is included several times. 49// Do not put globals below. 50 51// The following 4 functions are generated from the code below: 52// GetStack{Trace,Frames}() 53// GetStack{Trace,Frames}WithContext() 54// 55// These functions take the following args: 56// void** result: the stack-trace, as an array 57// int* sizes: the size of each stack frame, as an array 58// (GetStackFrames* only) 59// int max_depth: the size of the result (and sizes) array(s) 60// int skip_count: how many stack pointers to skip before storing in result 61// void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) 62int GET_STACK_TRACE_OR_FRAMES { 63 static const int kStackLength = 64; 64 void * stack[kStackLength]; 65 int size; 66 67 size = backtrace(stack, kStackLength); 68 skip_count++; // we want to skip the current frame as well 69 int result_count = size - skip_count; 70 if (result_count < 0) 71 result_count = 0; 72 if (result_count > max_depth) 73 result_count = max_depth; 74 for (int i = 0; i < result_count; i++) 75 result[i] = stack[i + skip_count]; 76 77#if IS_STACK_FRAMES 78 // No implementation for finding out the stack frame sizes yet. 79 memset(sizes, 0, sizeof(*sizes) * result_count); 80#endif 81 82 return result_count; 83} 84