blob: 7d7c22d9e450eb6222d0662e3935608a8a889d1a [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2005, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Sanjay Ghemawat
33//
34// Portable implementation - just use glibc
35//
36// Note: The glibc implementation may cause a call to malloc.
37// This can cause a deadlock in HeapProfiler.
38
39#ifndef BASE_STACKTRACE_GENERIC_INL_H_
40#define BASE_STACKTRACE_GENERIC_INL_H_
41// Note: this file is included into stacktrace.cc more than once.
42// Anything that should only be defined once should be here:
43
44#include <execinfo.h>
45#include <string.h>
46#include "gperftools/stacktrace.h"
47#endif // BASE_STACKTRACE_GENERIC_INL_H_
48
49// Note: this part of the file is included several times.
50// Do not put globals below.
51
52// The following 4 functions are generated from the code below:
53// GetStack{Trace,Frames}()
54// GetStack{Trace,Frames}WithContext()
55//
56// These functions take the following args:
57// void** result: the stack-trace, as an array
58// int* sizes: the size of each stack frame, as an array
59// (GetStackFrames* only)
60// int max_depth: the size of the result (and sizes) array(s)
61// int skip_count: how many stack pointers to skip before storing in result
62// void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
63static int GET_STACK_TRACE_OR_FRAMES {
64 static const int kStackLength = 64;
65 void * stack[kStackLength];
66 int size;
67
68 size = backtrace(stack, kStackLength);
69 skip_count += 2; // we want to skip the current and it's parent frame as well
70 int result_count = size - skip_count;
71 if (result_count < 0)
72 result_count = 0;
73 if (result_count > max_depth)
74 result_count = max_depth;
75 for (int i = 0; i < result_count; i++)
76 result[i] = stack[i + skip_count];
77
78#if IS_STACK_FRAMES
79 // No implementation for finding out the stack frame sizes yet.
80 memset(sizes, 0, sizeof(*sizes) * result_count);
81#endif
82
83 return result_count;
84}