blob: a6213cf4388771386ca373847f017fe55cb45b28 [file] [log] [blame]
Abseil Team04dd99d2018-05-14 13:41:38 -07001// Copyright 2018 The Abseil Authors.
mistergc2e75482017-09-19 16:54:40 -04002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
Abseil Team04dd99d2018-05-14 13:41:38 -070015// -----------------------------------------------------------------------------
16// File: stacktrace.h
17// -----------------------------------------------------------------------------
18//
19// This file contains routines to extract the current stack trace and associated
20// stack frames. These functions are thread-safe and async-signal-safe.
21//
mistergc2e75482017-09-19 16:54:40 -040022// Note that stack trace functionality is platform dependent and requires
Abseil Team04dd99d2018-05-14 13:41:38 -070023// additional support from the compiler/build system in most cases. (That is,
24// this functionality generally only works on platforms/builds that have been
25// specifically configured to support it.)
26//
27// Note: stack traces in Abseil that do not utilize a symbolizer will result in
28// frames consisting of function addresses rather than human-readable function
29// names. (See symbolize.h for information on symbolizing these values.)
mistergc2e75482017-09-19 16:54:40 -040030
31#ifndef ABSL_DEBUGGING_STACKTRACE_H_
32#define ABSL_DEBUGGING_STACKTRACE_H_
33
34namespace absl {
Abseil Team6c7de162018-06-20 06:25:23 -070035inline namespace lts_2018_06_20 {
mistergc2e75482017-09-19 16:54:40 -040036
Abseil Team04dd99d2018-05-14 13:41:38 -070037// GetStackFrames()
38//
39// Records program counter values for up to `max_depth` frames, skipping the
40// most recent `skip_count` stack frames, and stores their corresponding values
41// and sizes in `results` and `sizes` buffers. (Note that the frame generated
42// for the `absl::GetStackFrames()` routine itself is also skipped.)
43// routine itself.
mistergc2e75482017-09-19 16:54:40 -040044//
45// Example:
Abseil Team04dd99d2018-05-14 13:41:38 -070046//
mistergc2e75482017-09-19 16:54:40 -040047// main() { foo(); }
48// foo() { bar(); }
49// bar() {
50// void* result[10];
51// int sizes[10];
52// int depth = absl::GetStackFrames(result, sizes, 10, 1);
53// }
54//
Abseil Team04dd99d2018-05-14 13:41:38 -070055// The current stack frame would consist of three function calls: `bar()`,
56// `foo()`, and then `main()`; however, since the `GetStackFrames()` call sets
57// `skip_count` to `1`, it will skip the frame for `bar()`, the most recently
58// invoked function call. It will therefore return two program counters and will
59// produce values that map to the following function calls:
60//
61// result[0] foo()
62// result[1] main()
63//
64// (Note: in practice, a few more entries after `main()` may be added to account
65// for startup processes.)
66//
67// Corresponding stack frame sizes will also be recorded:
68//
mistergc2e75482017-09-19 16:54:40 -040069// sizes[0] 16
70// sizes[1] 16
Abseil Team04dd99d2018-05-14 13:41:38 -070071//
72// (Stack frame sizes of `16` above are just for illustration purposes.)
73//
mistergc2e75482017-09-19 16:54:40 -040074// Stack frame sizes of 0 or less indicate that those frame sizes couldn't
75// be identified.
76//
77// This routine may return fewer stack frame entries than are
Abseil Team04dd99d2018-05-14 13:41:38 -070078// available. Also note that `result` and `sizes` must both be non-null.
mistergc2e75482017-09-19 16:54:40 -040079extern int GetStackFrames(void** result, int* sizes, int max_depth,
80 int skip_count);
81
Abseil Team04dd99d2018-05-14 13:41:38 -070082// GetStackFramesWithContext()
mistergc2e75482017-09-19 16:54:40 -040083//
Abseil Team04dd99d2018-05-14 13:41:38 -070084// Records program counter values obtained from a signal handler. Records
85// program counter values for up to `max_depth` frames, skipping the most recent
86// `skip_count` stack frames, and stores their corresponding values and sizes in
87// `results` and `sizes` buffers. (Note that the frame generated for the
88// `absl::GetStackFramesWithContext()` routine itself is also skipped.)
89//
90// The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
91// passed to a signal handler registered via the `sa_sigaction` field of a
92// `sigaction` struct. (See
93// http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
94// help a stack unwinder to provide a better stack trace under certain
95// conditions. `uc` may safely be null.
96//
97// The `min_dropped_frames` output parameter, if non-null, points to the
98// location to note any dropped stack frames, if any, due to buffer limitations
99// or other reasons. (This value will be set to `0` if no frames were dropped.)
100// The number of total stack frames is guaranteed to be >= skip_count +
101// max_depth + *min_dropped_frames.
mistergc2e75482017-09-19 16:54:40 -0400102extern int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
103 int skip_count, const void* uc,
104 int* min_dropped_frames);
105
Abseil Team04dd99d2018-05-14 13:41:38 -0700106// GetStackTrace()
107//
108// Records program counter values for up to `max_depth` frames, skipping the
109// most recent `skip_count` stack frames, and stores their corresponding values
110// in `results`. Note that this function is similar to `absl::GetStackFrames()`
111// except that it returns the stack trace only, and not stack frame sizes.
112//
mistergc2e75482017-09-19 16:54:40 -0400113// Example:
Abseil Team04dd99d2018-05-14 13:41:38 -0700114//
mistergc2e75482017-09-19 16:54:40 -0400115// main() { foo(); }
116// foo() { bar(); }
117// bar() {
118// void* result[10];
119// int depth = absl::GetStackTrace(result, 10, 1);
120// }
121//
122// This produces:
Abseil Team04dd99d2018-05-14 13:41:38 -0700123//
mistergc2e75482017-09-19 16:54:40 -0400124// result[0] foo
125// result[1] main
126// .... ...
127//
Abseil Team04dd99d2018-05-14 13:41:38 -0700128// `result` must not be null.
mistergc2e75482017-09-19 16:54:40 -0400129extern int GetStackTrace(void** result, int max_depth, int skip_count);
130
Abseil Team04dd99d2018-05-14 13:41:38 -0700131// GetStackTraceWithContext()
mistergc2e75482017-09-19 16:54:40 -0400132//
Abseil Team04dd99d2018-05-14 13:41:38 -0700133// Records program counter values obtained from a signal handler. Records
134// program counter values for up to `max_depth` frames, skipping the most recent
135// `skip_count` stack frames, and stores their corresponding values in
136// `results`. (Note that the frame generated for the
137// `absl::GetStackFramesWithContext()` routine itself is also skipped.)
138//
139// The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
140// passed to a signal handler registered via the `sa_sigaction` field of a
141// `sigaction` struct. (See
142// http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
143// help a stack unwinder to provide a better stack trace under certain
144// conditions. `uc` may safely be null.
145//
146// The `min_dropped_frames` output parameter, if non-null, points to the
147// location to note any dropped stack frames, if any, due to buffer limitations
148// or other reasons. (This value will be set to `0` if no frames were dropped.)
149// The number of total stack frames is guaranteed to be >= skip_count +
150// max_depth + *min_dropped_frames.
mistergc2e75482017-09-19 16:54:40 -0400151extern int GetStackTraceWithContext(void** result, int max_depth,
152 int skip_count, const void* uc,
153 int* min_dropped_frames);
154
Abseil Team04dd99d2018-05-14 13:41:38 -0700155// SetStackUnwinder()
156//
157// Provides a custom function for unwinding stack frames that will be used in
158// place of the default stack unwinder when invoking the static
mistergc2e75482017-09-19 16:54:40 -0400159// GetStack{Frames,Trace}{,WithContext}() functions above.
160//
161// The arguments passed to the unwinder function will match the
Abseil Team04dd99d2018-05-14 13:41:38 -0700162// arguments passed to `absl::GetStackFramesWithContext()` except that sizes
mistergc2e75482017-09-19 16:54:40 -0400163// will be non-null iff the caller is interested in frame sizes.
164//
Abseil Team04dd99d2018-05-14 13:41:38 -0700165// If unwinder is set to null, we revert to the default stack-tracing behavior.
mistergc2e75482017-09-19 16:54:40 -0400166//
Abseil Team04dd99d2018-05-14 13:41:38 -0700167// *****************************************************************************
168// WARNING
169// *****************************************************************************
mistergc2e75482017-09-19 16:54:40 -0400170//
171// absl::SetStackUnwinder is not suitable for general purpose use. It is
172// provided for custom runtimes.
Abseil Team04dd99d2018-05-14 13:41:38 -0700173// Some things to watch out for when calling `absl::SetStackUnwinder()`:
mistergc2e75482017-09-19 16:54:40 -0400174//
175// (a) The unwinder may be called from within signal handlers and
176// therefore must be async-signal-safe.
177//
178// (b) Even after a custom stack unwinder has been unregistered, other
179// threads may still be in the process of using that unwinder.
180// Therefore do not clean up any state that may be needed by an old
181// unwinder.
Abseil Team04dd99d2018-05-14 13:41:38 -0700182// *****************************************************************************
mistergc2e75482017-09-19 16:54:40 -0400183extern void SetStackUnwinder(int (*unwinder)(void** pcs, int* sizes,
184 int max_depth, int skip_count,
185 const void* uc,
186 int* min_dropped_frames));
187
Abseil Team04dd99d2018-05-14 13:41:38 -0700188// DefaultStackUnwinder()
mistergc2e75482017-09-19 16:54:40 -0400189//
Abseil Team04dd99d2018-05-14 13:41:38 -0700190// Records program counter values of up to `max_depth` frames, skipping the most
191// recent `skip_count` stack frames, and stores their corresponding values in
192// `pcs`. (Note that the frame generated for this call itself is also skipped.)
193// This function acts as a generic stack-unwinder; prefer usage of the more
194// specific `GetStack{Trace,Frames}{,WithContext}()` functions above.
mistergc2e75482017-09-19 16:54:40 -0400195//
Abseil Team04dd99d2018-05-14 13:41:38 -0700196// If you have set your own stack unwinder (with the `SetStackUnwinder()`
197// function above, you can still get the default stack unwinder by calling
198// `DefaultStackUnwinder()`, which will ignore any previously set stack unwinder
199// and use the default one instead.
mistergc2e75482017-09-19 16:54:40 -0400200//
Abseil Team04dd99d2018-05-14 13:41:38 -0700201// Because this function is generic, only `pcs` is guaranteed to be non-null
202// upon return. It is legal for `sizes`, `uc`, and `min_dropped_frames` to all
203// be null when called.
204//
205// The semantics are the same as the corresponding `GetStack*()` function in the
206// case where `absl::SetStackUnwinder()` was never called. Equivalents are:
mistergc2e75482017-09-19 16:54:40 -0400207//
208// null sizes | non-nullptr sizes
209// |==========================================================|
210// null uc | GetStackTrace() | GetStackFrames() |
211// non-null uc | GetStackTraceWithContext() | GetStackFramesWithContext() |
212// |==========================================================|
213extern int DefaultStackUnwinder(void** pcs, int* sizes, int max_depth,
214 int skip_count, const void* uc,
215 int* min_dropped_frames);
216
Abseil Team5a8de8a2018-01-17 09:53:47 -0800217namespace debugging_internal {
218// Returns true for platforms which are expected to have functioning stack trace
219// implementations. Intended to be used for tests which want to exclude
220// verification of logic known to be broken because stack traces are not
221// working.
222extern bool StackTraceWorksForTest();
223} // namespace debugging_internal
Abseil Team6c7de162018-06-20 06:25:23 -0700224} // inline namespace lts_2018_06_20
mistergc2e75482017-09-19 16:54:40 -0400225} // namespace absl
226
227#endif // ABSL_DEBUGGING_STACKTRACE_H_