Coverage Report

Created: 2025-10-27 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/debugging/stacktrace.h
Line
Count
Source
1
// Copyright 2018 The Abseil Authors.
2
//
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
//      https://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
//
15
// -----------------------------------------------------------------------------
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
//
22
// Note that stack trace functionality is platform dependent and requires
23
// 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.)
30
31
#ifndef ABSL_DEBUGGING_STACKTRACE_H_
32
#define ABSL_DEBUGGING_STACKTRACE_H_
33
34
#include <stddef.h>
35
#include <stdint.h>
36
37
#include "absl/base/attributes.h"
38
#include "absl/base/config.h"
39
40
namespace absl {
41
ABSL_NAMESPACE_BEGIN
42
43
namespace internal_stacktrace {
44
45
// Same as `absl::GetStackFrames`, but with an optional `frames` parameter to
46
// allow callers to receive the raw stack frame addresses.
47
// This is internal for now; use `absl::GetStackFrames()` instead.
48
extern int GetStackFrames(void** result, uintptr_t* frames, int* sizes,
49
                          int max_depth, int skip_count);
50
51
// Same as `absl::GetStackFramesWithContext`, but with an optional `frames`
52
// parameter to allow callers to receive a start address for each stack frame.
53
// The address may be zero in cases where it cannot be computed.
54
//
55
// DO NOT use this function without consulting the owners of absl/debuggging.
56
// There is NO GUARANTEE on the precise frame addresses returned on any given
57
// platform. It is only intended to provide sufficient non-overlapping bounds on
58
// the local variables of a stack frame when used in conjunction with the
59
// returned frame sizes. The actual pointers may be ABI-dependent, may vary at
60
// run time, and are subject to breakage without notice.
61
//
62
// Implementation note:
63
// Currently, we *attempt* to return the Canonical Frame Address (CFA) in DWARF
64
// on most platforms. This is the value of the stack pointer just before the
65
// 'call' instruction is executed in the caller.
66
// Not all platforms and toolchains support this exact address, so this should
67
// not be relied on for correctness.
68
extern int GetStackFramesWithContext(void** result, uintptr_t* frames,
69
                                     int* sizes, int max_depth, int skip_count,
70
                                     const void* uc, int* min_dropped_frames);
71
72
// As above, but skips fix-ups for efficiency.
73
extern int GetStackTraceNoFixup(void** result, int max_depth, int skip_count);
74
75
// Same as `absl::DefaultStackUnwinder`, but with an optional `frames` parameter
76
// to allow callers to receive the raw stack frame addresses.
77
// This is internal for now; do not depend on this externally.
78
extern int DefaultStackUnwinder(void** pcs, uintptr_t* frames, int* sizes,
79
                                int max_depth, int skip_count, const void* uc,
80
                                int* min_dropped_frames);
81
82
}  // namespace internal_stacktrace
83
84
// GetStackFrames()
85
//
86
// Records program counter values for up to `max_depth` frames, skipping the
87
// most recent `skip_count` stack frames, stores their corresponding values
88
// and sizes in `results` and `sizes` buffers, and returns the number of frames
89
// stored. (Note that the frame generated for the `absl::GetStackFrames()`
90
// routine itself is also skipped.)
91
//
92
// Example:
93
//
94
//      main() { foo(); }
95
//      foo() { bar(); }
96
//      bar() {
97
//        void* result[10];
98
//        int sizes[10];
99
//        int depth = absl::GetStackFrames(result, sizes, 10, 1);
100
//      }
101
//
102
// The current stack frame would consist of three function calls: `bar()`,
103
// `foo()`, and then `main()`; however, since the `GetStackFrames()` call sets
104
// `skip_count` to `1`, it will skip the frame for `bar()`, the most recently
105
// invoked function call. It will therefore return 2 and fill `result` with
106
// program counters within the following functions:
107
//
108
//      result[0]       foo()
109
//      result[1]       main()
110
//
111
// (Note: in practice, a few more entries after `main()` may be added to account
112
// for startup processes.)
113
//
114
// Corresponding stack frame sizes will also be recorded:
115
//
116
//    sizes[0]       16
117
//    sizes[1]       16
118
//
119
// (Stack frame sizes of `16` above are just for illustration purposes.)
120
//
121
// Stack frame sizes of 0 or less indicate that those frame sizes couldn't
122
// be identified.
123
//
124
// This routine may return fewer stack frame entries than are
125
// available. Also note that `result` and `sizes` must both be non-null.
126
ABSL_ATTRIBUTE_ALWAYS_INLINE inline int GetStackFrames(void** result,
127
                                                       int* sizes,
128
                                                       int max_depth,
129
0
                                                       int skip_count) {
130
0
  return internal_stacktrace::GetStackFrames(result, nullptr, sizes, max_depth,
131
0
                                             skip_count);
132
0
}
133
134
// GetStackFramesWithContext()
135
//
136
// Records program counter values obtained from a signal handler. Records
137
// program counter values for up to `max_depth` frames, skipping the most recent
138
// `skip_count` stack frames, stores their corresponding values and sizes in
139
// `results` and `sizes` buffers, and returns the number of frames stored. (Note
140
// that the frame generated for the `absl::GetStackFramesWithContext()` routine
141
// itself is also skipped.)
142
//
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.
155
ABSL_ATTRIBUTE_ALWAYS_INLINE inline int GetStackFramesWithContext(
156
    void** result, int* sizes, int max_depth, int skip_count, const void* uc,
157
0
    int* min_dropped_frames) {
158
0
  return internal_stacktrace::GetStackFramesWithContext(
159
0
      result, nullptr, sizes, max_depth, skip_count, uc, min_dropped_frames);
160
0
}
161
162
// GetStackTrace()
163
//
164
// Records program counter values for up to `max_depth` frames, skipping the
165
// most recent `skip_count` stack frames, stores their corresponding values
166
// in `results`, and returns the number of frames
167
// stored. Note that this function is similar to `absl::GetStackFrames()`
168
// except that it returns the stack trace only, and not stack frame sizes.
169
//
170
// Example:
171
//
172
//      main() { foo(); }
173
//      foo() { bar(); }
174
//      bar() {
175
//        void* result[10];
176
//        int depth = absl::GetStackTrace(result, 10, 1);
177
//      }
178
//
179
// This produces:
180
//
181
//      result[0]       foo
182
//      result[1]       main
183
//           ....       ...
184
//
185
// `result` must not be null.
186
extern int GetStackTrace(void** result, int max_depth, int skip_count);
187
188
// GetStackTraceWithContext()
189
//
190
// Records program counter values obtained from a signal handler. Records
191
// program counter values for up to `max_depth` frames, skipping the most recent
192
// `skip_count` stack frames, stores their corresponding values in `results`,
193
// and returns the number of frames stored. (Note that the frame generated for
194
// the `absl::GetStackFramesWithContext()` routine itself is also skipped.)
195
//
196
// The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
197
// passed to a signal handler registered via the `sa_sigaction` field of a
198
// `sigaction` struct. (See
199
// http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
200
// help a stack unwinder to provide a better stack trace under certain
201
// conditions. `uc` may safely be null.
202
//
203
// The `min_dropped_frames` output parameter, if non-null, points to the
204
// location to note any dropped stack frames, if any, due to buffer limitations
205
// or other reasons. (This value will be set to `0` if no frames were dropped.)
206
// The number of total stack frames is guaranteed to be >= skip_count +
207
// max_depth + *min_dropped_frames.
208
extern int GetStackTraceWithContext(void** result, int max_depth,
209
                                    int skip_count, const void* uc,
210
                                    int* min_dropped_frames);
211
212
// SetStackUnwinder()
213
//
214
// Provides a custom function for unwinding stack frames that will be used in
215
// place of the default stack unwinder when invoking the static
216
// GetStack{Frames,Trace}{,WithContext}() functions above.
217
//
218
// The arguments passed to the unwinder function will match the
219
// arguments passed to `absl::GetStackFramesWithContext()` except that sizes
220
// will be non-null iff the caller is interested in frame sizes.
221
//
222
// If unwinder is set to null, we revert to the default stack-tracing behavior.
223
//
224
// *****************************************************************************
225
// WARNING
226
// *****************************************************************************
227
//
228
// absl::SetStackUnwinder is not suitable for general purpose use.  It is
229
// provided for custom runtimes.
230
// Some things to watch out for when calling `absl::SetStackUnwinder()`:
231
//
232
// (a) The unwinder may be called from within signal handlers and
233
// therefore must be async-signal-safe.
234
//
235
// (b) Even after a custom stack unwinder has been unregistered, other
236
// threads may still be in the process of using that unwinder.
237
// Therefore do not clean up any state that may be needed by an old
238
// unwinder.
239
// *****************************************************************************
240
extern void SetStackUnwinder(int (*unwinder)(void** pcs, int* sizes,
241
                                             int max_depth, int skip_count,
242
                                             const void* uc,
243
                                             int* min_dropped_frames));
244
245
// DefaultStackUnwinder()
246
//
247
// Records program counter values of up to `max_depth` frames, skipping the most
248
// recent `skip_count` stack frames, and stores their corresponding values in
249
// `pcs`. (Note that the frame generated for this call itself is also skipped.)
250
// This function acts as a generic stack-unwinder; prefer usage of the more
251
// specific `GetStack{Trace,Frames}{,WithContext}()` functions above.
252
//
253
// If you have set your own stack unwinder (with the `SetStackUnwinder()`
254
// function above, you can still get the default stack unwinder by calling
255
// `DefaultStackUnwinder()`, which will ignore any previously set stack unwinder
256
// and use the default one instead.
257
//
258
// Because this function is generic, only `pcs` is guaranteed to be non-null
259
// upon return. It is legal for `sizes`, `uc`, and `min_dropped_frames` to all
260
// be null when called.
261
//
262
// The semantics are the same as the corresponding `GetStack*()` function in the
263
// case where `absl::SetStackUnwinder()` was never called. Equivalents are:
264
//
265
//                       null sizes         |        non-nullptr sizes
266
//             |==========================================================|
267
//     null uc | GetStackTrace()            | GetStackFrames()            |
268
// non-null uc | GetStackTraceWithContext() | GetStackFramesWithContext() |
269
//             |==========================================================|
270
extern int DefaultStackUnwinder(void** pcs, int* sizes, int max_depth,
271
                                int skip_count, const void* uc,
272
                                int* min_dropped_frames);
273
274
namespace debugging_internal {
275
// Returns true for platforms which are expected to have functioning stack trace
276
// implementations. Intended to be used for tests which want to exclude
277
// verification of logic known to be broken because stack traces are not
278
// working.
279
extern bool StackTraceWorksForTest();
280
}  // namespace debugging_internal
281
282
namespace internal_stacktrace {
283
extern bool ShouldFixUpStack();
284
285
// Fixes up the stack trace of the current thread, in the first `depth` frames
286
// of each buffer. The buffers need to be larger than `depth`, to accommodate
287
// any newly inserted elements. `depth` is updated to reflect the new number of
288
// elements valid across all the buffers. (It is therefore recommended that all
289
// buffer sizes be equal.)
290
//
291
// The `frames` and `sizes` parameters denote the bounds of the stack frame
292
// corresponding to each instruction pointer in the `pcs`.
293
// Any elements inside these buffers may be zero or null, in which case that
294
// information is assumed to be absent/unavailable.
295
extern void FixUpStack(void** pcs, uintptr_t* frames, int* sizes,
296
                       size_t capacity, size_t& depth);
297
}  // namespace internal_stacktrace
298
299
ABSL_NAMESPACE_END
300
}  // namespace absl
301
302
#endif  // ABSL_DEBUGGING_STACKTRACE_H_