blob: 0ec0ffdabd422e07be93660860202c61c4c60406 [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//
nik727338b70432019-03-08 10:27:53 -05007// https://www.apache.org/licenses/LICENSE-2.0
mistergc2e75482017-09-19 16:54:40 -04008//
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
Abseil Team12bc53e2019-12-12 10:36:03 -080034#include "absl/base/config.h"
35
mistergc2e75482017-09-19 16:54:40 -040036namespace absl {
Abseil Team12bc53e2019-12-12 10:36:03 -080037ABSL_NAMESPACE_BEGIN
mistergc2e75482017-09-19 16:54:40 -040038
Abseil Team04dd99d2018-05-14 13:41:38 -070039// GetStackFrames()
40//
41// Records program counter values for up to `max_depth` frames, skipping the
Abseil Team2796d502019-10-21 10:21:03 -070042// most recent `skip_count` stack frames, stores their corresponding values
43// and sizes in `results` and `sizes` buffers, and returns the number of frames
44// stored. (Note that the frame generated for the `absl::GetStackFrames()`
45// routine itself is also skipped.)
mistergc2e75482017-09-19 16:54:40 -040046//
47// Example:
Abseil Team04dd99d2018-05-14 13:41:38 -070048//
mistergc2e75482017-09-19 16:54:40 -040049// main() { foo(); }
50// foo() { bar(); }
51// bar() {
52// void* result[10];
53// int sizes[10];
54// int depth = absl::GetStackFrames(result, sizes, 10, 1);
55// }
56//
Abseil Team04dd99d2018-05-14 13:41:38 -070057// The current stack frame would consist of three function calls: `bar()`,
58// `foo()`, and then `main()`; however, since the `GetStackFrames()` call sets
59// `skip_count` to `1`, it will skip the frame for `bar()`, the most recently
Abseil Team2796d502019-10-21 10:21:03 -070060// invoked function call. It will therefore return 2 and fill `result` with
61// program counters within the following functions:
Abseil Team04dd99d2018-05-14 13:41:38 -070062//
63// result[0] foo()
64// result[1] main()
65//
66// (Note: in practice, a few more entries after `main()` may be added to account
67// for startup processes.)
68//
69// Corresponding stack frame sizes will also be recorded:
70//
mistergc2e75482017-09-19 16:54:40 -040071// sizes[0] 16
72// sizes[1] 16
Abseil Team04dd99d2018-05-14 13:41:38 -070073//
74// (Stack frame sizes of `16` above are just for illustration purposes.)
75//
mistergc2e75482017-09-19 16:54:40 -040076// Stack frame sizes of 0 or less indicate that those frame sizes couldn't
77// be identified.
78//
79// This routine may return fewer stack frame entries than are
Abseil Team04dd99d2018-05-14 13:41:38 -070080// available. Also note that `result` and `sizes` must both be non-null.
mistergc2e75482017-09-19 16:54:40 -040081extern int GetStackFrames(void** result, int* sizes, int max_depth,
82 int skip_count);
83
Abseil Team04dd99d2018-05-14 13:41:38 -070084// GetStackFramesWithContext()
mistergc2e75482017-09-19 16:54:40 -040085//
Abseil Team04dd99d2018-05-14 13:41:38 -070086// Records program counter values obtained from a signal handler. Records
87// program counter values for up to `max_depth` frames, skipping the most recent
Abseil Team2796d502019-10-21 10:21:03 -070088// `skip_count` stack frames, stores their corresponding values and sizes in
89// `results` and `sizes` buffers, and returns the number of frames stored. (Note
90// that the frame generated for the `absl::GetStackFramesWithContext()` routine
91// itself is also skipped.)
Abseil Team04dd99d2018-05-14 13:41:38 -070092//
93// The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
94// passed to a signal handler registered via the `sa_sigaction` field of a
95// `sigaction` struct. (See
96// http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
97// help a stack unwinder to provide a better stack trace under certain
98// conditions. `uc` may safely be null.
99//
100// The `min_dropped_frames` output parameter, if non-null, points to the
101// location to note any dropped stack frames, if any, due to buffer limitations
102// or other reasons. (This value will be set to `0` if no frames were dropped.)
103// The number of total stack frames is guaranteed to be >= skip_count +
104// max_depth + *min_dropped_frames.
mistergc2e75482017-09-19 16:54:40 -0400105extern int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
106 int skip_count, const void* uc,
107 int* min_dropped_frames);
108
Abseil Team04dd99d2018-05-14 13:41:38 -0700109// GetStackTrace()
110//
111// Records program counter values for up to `max_depth` frames, skipping the
Abseil Team2796d502019-10-21 10:21:03 -0700112// most recent `skip_count` stack frames, stores their corresponding values
113// in `results`, and returns the number of frames
114// stored. Note that this function is similar to `absl::GetStackFrames()`
Abseil Team04dd99d2018-05-14 13:41:38 -0700115// except that it returns the stack trace only, and not stack frame sizes.
116//
mistergc2e75482017-09-19 16:54:40 -0400117// Example:
Abseil Team04dd99d2018-05-14 13:41:38 -0700118//
mistergc2e75482017-09-19 16:54:40 -0400119// main() { foo(); }
120// foo() { bar(); }
121// bar() {
122// void* result[10];
123// int depth = absl::GetStackTrace(result, 10, 1);
124// }
125//
126// This produces:
Abseil Team04dd99d2018-05-14 13:41:38 -0700127//
mistergc2e75482017-09-19 16:54:40 -0400128// result[0] foo
129// result[1] main
130// .... ...
131//
Abseil Team04dd99d2018-05-14 13:41:38 -0700132// `result` must not be null.
mistergc2e75482017-09-19 16:54:40 -0400133extern int GetStackTrace(void** result, int max_depth, int skip_count);
134
Abseil Team04dd99d2018-05-14 13:41:38 -0700135// GetStackTraceWithContext()
mistergc2e75482017-09-19 16:54:40 -0400136//
Abseil Team04dd99d2018-05-14 13:41:38 -0700137// Records program counter values obtained from a signal handler. Records
138// program counter values for up to `max_depth` frames, skipping the most recent
Abseil Team2796d502019-10-21 10:21:03 -0700139// `skip_count` stack frames, stores their corresponding values in `results`,
140// and returns the number of frames stored. (Note that the frame generated for
141// the `absl::GetStackFramesWithContext()` routine itself is also skipped.)
Abseil Team04dd99d2018-05-14 13:41:38 -0700142//
143// The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
144// passed to a signal handler registered via the `sa_sigaction` field of a
145// `sigaction` struct. (See
146// http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
147// help a stack unwinder to provide a better stack trace under certain
148// conditions. `uc` may safely be null.
149//
150// The `min_dropped_frames` output parameter, if non-null, points to the
151// location to note any dropped stack frames, if any, due to buffer limitations
152// or other reasons. (This value will be set to `0` if no frames were dropped.)
153// The number of total stack frames is guaranteed to be >= skip_count +
154// max_depth + *min_dropped_frames.
mistergc2e75482017-09-19 16:54:40 -0400155extern int GetStackTraceWithContext(void** result, int max_depth,
156 int skip_count, const void* uc,
157 int* min_dropped_frames);
158
Abseil Team04dd99d2018-05-14 13:41:38 -0700159// SetStackUnwinder()
160//
161// Provides a custom function for unwinding stack frames that will be used in
162// place of the default stack unwinder when invoking the static
mistergc2e75482017-09-19 16:54:40 -0400163// GetStack{Frames,Trace}{,WithContext}() functions above.
164//
165// The arguments passed to the unwinder function will match the
Abseil Team04dd99d2018-05-14 13:41:38 -0700166// arguments passed to `absl::GetStackFramesWithContext()` except that sizes
mistergc2e75482017-09-19 16:54:40 -0400167// will be non-null iff the caller is interested in frame sizes.
168//
Abseil Team04dd99d2018-05-14 13:41:38 -0700169// If unwinder is set to null, we revert to the default stack-tracing behavior.
mistergc2e75482017-09-19 16:54:40 -0400170//
Abseil Team04dd99d2018-05-14 13:41:38 -0700171// *****************************************************************************
172// WARNING
173// *****************************************************************************
mistergc2e75482017-09-19 16:54:40 -0400174//
175// absl::SetStackUnwinder is not suitable for general purpose use. It is
176// provided for custom runtimes.
Abseil Team04dd99d2018-05-14 13:41:38 -0700177// Some things to watch out for when calling `absl::SetStackUnwinder()`:
mistergc2e75482017-09-19 16:54:40 -0400178//
179// (a) The unwinder may be called from within signal handlers and
180// therefore must be async-signal-safe.
181//
182// (b) Even after a custom stack unwinder has been unregistered, other
183// threads may still be in the process of using that unwinder.
184// Therefore do not clean up any state that may be needed by an old
185// unwinder.
Abseil Team04dd99d2018-05-14 13:41:38 -0700186// *****************************************************************************
mistergc2e75482017-09-19 16:54:40 -0400187extern void SetStackUnwinder(int (*unwinder)(void** pcs, int* sizes,
188 int max_depth, int skip_count,
189 const void* uc,
190 int* min_dropped_frames));
191
Abseil Team04dd99d2018-05-14 13:41:38 -0700192// DefaultStackUnwinder()
mistergc2e75482017-09-19 16:54:40 -0400193//
Abseil Team04dd99d2018-05-14 13:41:38 -0700194// Records program counter values of up to `max_depth` frames, skipping the most
195// recent `skip_count` stack frames, and stores their corresponding values in
196// `pcs`. (Note that the frame generated for this call itself is also skipped.)
197// This function acts as a generic stack-unwinder; prefer usage of the more
198// specific `GetStack{Trace,Frames}{,WithContext}()` functions above.
mistergc2e75482017-09-19 16:54:40 -0400199//
Abseil Team04dd99d2018-05-14 13:41:38 -0700200// If you have set your own stack unwinder (with the `SetStackUnwinder()`
201// function above, you can still get the default stack unwinder by calling
202// `DefaultStackUnwinder()`, which will ignore any previously set stack unwinder
203// and use the default one instead.
mistergc2e75482017-09-19 16:54:40 -0400204//
Abseil Team04dd99d2018-05-14 13:41:38 -0700205// Because this function is generic, only `pcs` is guaranteed to be non-null
206// upon return. It is legal for `sizes`, `uc`, and `min_dropped_frames` to all
207// be null when called.
208//
209// The semantics are the same as the corresponding `GetStack*()` function in the
210// case where `absl::SetStackUnwinder()` was never called. Equivalents are:
mistergc2e75482017-09-19 16:54:40 -0400211//
212// null sizes | non-nullptr sizes
213// |==========================================================|
214// null uc | GetStackTrace() | GetStackFrames() |
215// non-null uc | GetStackTraceWithContext() | GetStackFramesWithContext() |
216// |==========================================================|
217extern int DefaultStackUnwinder(void** pcs, int* sizes, int max_depth,
218 int skip_count, const void* uc,
219 int* min_dropped_frames);
220
Abseil Team5a8de8a2018-01-17 09:53:47 -0800221namespace debugging_internal {
222// Returns true for platforms which are expected to have functioning stack trace
223// implementations. Intended to be used for tests which want to exclude
224// verification of logic known to be broken because stack traces are not
225// working.
226extern bool StackTraceWorksForTest();
227} // namespace debugging_internal
Abseil Team12bc53e2019-12-12 10:36:03 -0800228ABSL_NAMESPACE_END
mistergc2e75482017-09-19 16:54:40 -0400229} // namespace absl
230
231#endif // ABSL_DEBUGGING_STACKTRACE_H_