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// Produce stack trace.
34//
35// There are three different ways we can try to get the stack trace:
36//
37// 1) Our hand-coded stack-unwinder.  This depends on a certain stack
38//    layout, which is used by gcc (and those systems using a
39//    gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
40//    It uses the frame pointer to do its work.
41//
42// 2) The libunwind library.  This is still in development, and as a
43//    separate library adds a new dependency, abut doesn't need a frame
44//    pointer.  It also doesn't call malloc.
45//
46// 3) The gdb unwinder -- also the one used by the c++ exception code.
47//    It's obviously well-tested, but has a fatal flaw: it can call
48//    malloc() from the unwinder.  This is a problem because we're
49//    trying to use the unwinder to instrument malloc().
50//
51// Note: if you add a new implementation here, make sure it works
52// correctly when GetStackTrace() is called with max_depth == 0.
53// Some code may do that.
54
55#include <config.h>
56#include <gperftools/stacktrace.h>
57#include "stacktrace_config.h"
58
59#if defined(STACKTRACE_INL_HEADER)
60
61#define IS_STACK_FRAMES 0
62#define IS_WITH_CONTEXT 0
63#define GET_STACK_TRACE_OR_FRAMES \
64   GetStackTrace(void **result, int max_depth, int skip_count)
65#include STACKTRACE_INL_HEADER
66#undef IS_STACK_FRAMES
67#undef IS_WITH_CONTEXT
68#undef GET_STACK_TRACE_OR_FRAMES
69
70#define IS_STACK_FRAMES 1
71#define IS_WITH_CONTEXT 0
72#define GET_STACK_TRACE_OR_FRAMES \
73  GetStackFrames(void **result, int *sizes, int max_depth, int skip_count)
74#include STACKTRACE_INL_HEADER
75#undef IS_STACK_FRAMES
76#undef IS_WITH_CONTEXT
77#undef GET_STACK_TRACE_OR_FRAMES
78
79#define IS_STACK_FRAMES 0
80#define IS_WITH_CONTEXT 1
81#define GET_STACK_TRACE_OR_FRAMES \
82  GetStackTraceWithContext(void **result, int max_depth, \
83                           int skip_count, const void *ucp)
84#include STACKTRACE_INL_HEADER
85#undef IS_STACK_FRAMES
86#undef IS_WITH_CONTEXT
87#undef GET_STACK_TRACE_OR_FRAMES
88
89#define IS_STACK_FRAMES 1
90#define IS_WITH_CONTEXT 1
91#define GET_STACK_TRACE_OR_FRAMES \
92  GetStackFramesWithContext(void **result, int *sizes, int max_depth, \
93                            int skip_count, const void *ucp)
94#include STACKTRACE_INL_HEADER
95#undef IS_STACK_FRAMES
96#undef IS_WITH_CONTEXT
97#undef GET_STACK_TRACE_OR_FRAMES
98
99#elif 0
100// This is for the benefit of code analysis tools that may have
101// trouble with the computed #include above.
102# include "stacktrace_x86-inl.h"
103# include "stacktrace_libunwind-inl.h"
104# include "stacktrace_generic-inl.h"
105# include "stacktrace_powerpc-inl.h"
106# include "stacktrace_win32-inl.h"
107# include "stacktrace_arm-inl.h"
108#else
109# error Cannot calculate stack trace: will need to write for your environment
110#endif
111