blob: c631765c8a20e28264582d8a2ae3c1ae335cb273 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2013, 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: Jean Lee <xiaoyur347@gmail.com>
33// based on gcc Code-Gen-Options "-finstrument-functions" listed in
34// http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html .
35// Should run configure with CXXFLAGS="-finstrument-functions".
36
37// This file is a backtrace implementation for systems :
38// * The glibc implementation of backtrace() may cause a call to malloc,
39// and cause a deadlock in HeapProfiler.
40// * The libunwind implementation prints no backtrace.
41
42// The backtrace arrays are stored in "thread_back_trace" variable.
43// Maybe to use thread local storage is better and should save memorys.
44
45#ifndef BASE_STACKTRACE_INSTRUMENT_INL_H_
46#define BASE_STACKTRACE_INSTRUMENT_INL_H_
47// Note: this file is included into stacktrace.cc more than once.
48// Anything that should only be defined once should be here:
49
50#include <execinfo.h>
51#include <string.h>
52#include <unistd.h>
53#include <sys/syscall.h>
54#include "gperftools/stacktrace.h"
55
56#define gettid() syscall(__NR_gettid)
57#ifndef __x86_64__
58#define MAX_THREAD (32768)
59#else
60#define MAX_THREAD (65536)
61#endif
62#define MAX_DEPTH (30)
63#define ATTRIBUTE_NOINSTRUMENT __attribute__ ((no_instrument_function))
64
65typedef struct {
66 int stack_depth;
67 void* frame[MAX_DEPTH];
68}BACK_TRACE;
69
70static BACK_TRACE thread_back_trace[MAX_THREAD];
71extern "C" {
72void __cyg_profile_func_enter(void *func_address,
73 void *call_site) ATTRIBUTE_NOINSTRUMENT;
74void __cyg_profile_func_enter(void *func_address, void *call_site) {
75 (void)func_address;
76
77 BACK_TRACE* backtrace = thread_back_trace + gettid();
78 int stack_depth = backtrace->stack_depth;
79 backtrace->stack_depth = stack_depth + 1;
80 if ( stack_depth >= MAX_DEPTH ) {
81 return;
82 }
83 backtrace->frame[stack_depth] = call_site;
84}
85
86void __cyg_profile_func_exit(void *func_address,
87 void *call_site) ATTRIBUTE_NOINSTRUMENT;
88void __cyg_profile_func_exit(void *func_address, void *call_site) {
89 (void)func_address;
90 (void)call_site;
91
92 BACK_TRACE* backtrace = thread_back_trace + gettid();
93 int stack_depth = backtrace->stack_depth;
94 backtrace->stack_depth = stack_depth - 1;
95 if ( stack_depth >= MAX_DEPTH ) {
96 return;
97 }
98 backtrace->frame[stack_depth] = 0;
99}
100} // extern "C"
101
102static int cyg_backtrace(void **buffer, int size) {
103 BACK_TRACE* backtrace = thread_back_trace + gettid();
104 int stack_depth = backtrace->stack_depth;
105 if ( stack_depth >= MAX_DEPTH ) {
106 stack_depth = MAX_DEPTH;
107 }
108 int nSize = (size > stack_depth) ? stack_depth : size;
109 for (int i = 0; i < nSize; i++) {
110 buffer[i] = backtrace->frame[nSize - i - 1];
111 }
112
113 return nSize;
114}
115
116#endif // BASE_STACKTRACE_INSTRUMENT_INL_H_
117
118
119// Note: this part of the file is included several times.
120// Do not put globals below.
121
122// The following 4 functions are generated from the code below:
123// GetStack{Trace,Frames}()
124// GetStack{Trace,Frames}WithContext()
125//
126// These functions take the following args:
127// void** result: the stack-trace, as an array
128// int* sizes: the size of each stack frame, as an array
129// (GetStackFrames* only)
130// int max_depth: the size of the result (and sizes) array(s)
131// int skip_count: how many stack pointers to skip before storing in result
132// void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
133static int GET_STACK_TRACE_OR_FRAMES {
134 static const int kStackLength = 64;
135 void * stack[kStackLength];
136 int size;
137 memset(stack, 0, sizeof(stack));
138
139 size = cyg_backtrace(stack, kStackLength);
140 skip_count += 2; // we want to skip the current and parent frame as well
141 int result_count = size - skip_count;
142 if (result_count < 0)
143 result_count = 0;
144 if (result_count > max_depth)
145 result_count = max_depth;
146 for (int i = 0; i < result_count; i++)
147 result[i] = stack[i + skip_count];
148
149#if IS_STACK_FRAMES
150 // No implementation for finding out the stack frame sizes yet.
151 memset(sizes, 0, sizeof(*sizes) * result_count);
152#endif
153
154 return result_count;
155}