1*9356374aSAndroid Build Coastguard Worker // Copyright 2018 The Abseil Authors. 2*9356374aSAndroid Build Coastguard Worker // 3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at 6*9356374aSAndroid Build Coastguard Worker // 7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 8*9356374aSAndroid Build Coastguard Worker // 9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 13*9356374aSAndroid Build Coastguard Worker // limitations under the License. 14*9356374aSAndroid Build Coastguard Worker // 15*9356374aSAndroid Build Coastguard Worker // ----------------------------------------------------------------------------- 16*9356374aSAndroid Build Coastguard Worker // File: stacktrace.h 17*9356374aSAndroid Build Coastguard Worker // ----------------------------------------------------------------------------- 18*9356374aSAndroid Build Coastguard Worker // 19*9356374aSAndroid Build Coastguard Worker // This file contains routines to extract the current stack trace and associated 20*9356374aSAndroid Build Coastguard Worker // stack frames. These functions are thread-safe and async-signal-safe. 21*9356374aSAndroid Build Coastguard Worker // 22*9356374aSAndroid Build Coastguard Worker // Note that stack trace functionality is platform dependent and requires 23*9356374aSAndroid Build Coastguard Worker // additional support from the compiler/build system in most cases. (That is, 24*9356374aSAndroid Build Coastguard Worker // this functionality generally only works on platforms/builds that have been 25*9356374aSAndroid Build Coastguard Worker // specifically configured to support it.) 26*9356374aSAndroid Build Coastguard Worker // 27*9356374aSAndroid Build Coastguard Worker // Note: stack traces in Abseil that do not utilize a symbolizer will result in 28*9356374aSAndroid Build Coastguard Worker // frames consisting of function addresses rather than human-readable function 29*9356374aSAndroid Build Coastguard Worker // names. (See symbolize.h for information on symbolizing these values.) 30*9356374aSAndroid Build Coastguard Worker 31*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_DEBUGGING_STACKTRACE_H_ 32*9356374aSAndroid Build Coastguard Worker #define ABSL_DEBUGGING_STACKTRACE_H_ 33*9356374aSAndroid Build Coastguard Worker 34*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h" 35*9356374aSAndroid Build Coastguard Worker 36*9356374aSAndroid Build Coastguard Worker namespace absl { 37*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN 38*9356374aSAndroid Build Coastguard Worker 39*9356374aSAndroid Build Coastguard Worker // GetStackFrames() 40*9356374aSAndroid Build Coastguard Worker // 41*9356374aSAndroid Build Coastguard Worker // Records program counter values for up to `max_depth` frames, skipping the 42*9356374aSAndroid Build Coastguard Worker // most recent `skip_count` stack frames, stores their corresponding values 43*9356374aSAndroid Build Coastguard Worker // and sizes in `results` and `sizes` buffers, and returns the number of frames 44*9356374aSAndroid Build Coastguard Worker // stored. (Note that the frame generated for the `absl::GetStackFrames()` 45*9356374aSAndroid Build Coastguard Worker // routine itself is also skipped.) 46*9356374aSAndroid Build Coastguard Worker // 47*9356374aSAndroid Build Coastguard Worker // Example: 48*9356374aSAndroid Build Coastguard Worker // 49*9356374aSAndroid Build Coastguard Worker // main() { foo(); } 50*9356374aSAndroid Build Coastguard Worker // foo() { bar(); } 51*9356374aSAndroid Build Coastguard Worker // bar() { 52*9356374aSAndroid Build Coastguard Worker // void* result[10]; 53*9356374aSAndroid Build Coastguard Worker // int sizes[10]; 54*9356374aSAndroid Build Coastguard Worker // int depth = absl::GetStackFrames(result, sizes, 10, 1); 55*9356374aSAndroid Build Coastguard Worker // } 56*9356374aSAndroid Build Coastguard Worker // 57*9356374aSAndroid Build Coastguard Worker // The current stack frame would consist of three function calls: `bar()`, 58*9356374aSAndroid Build Coastguard Worker // `foo()`, and then `main()`; however, since the `GetStackFrames()` call sets 59*9356374aSAndroid Build Coastguard Worker // `skip_count` to `1`, it will skip the frame for `bar()`, the most recently 60*9356374aSAndroid Build Coastguard Worker // invoked function call. It will therefore return 2 and fill `result` with 61*9356374aSAndroid Build Coastguard Worker // program counters within the following functions: 62*9356374aSAndroid Build Coastguard Worker // 63*9356374aSAndroid Build Coastguard Worker // result[0] foo() 64*9356374aSAndroid Build Coastguard Worker // result[1] main() 65*9356374aSAndroid Build Coastguard Worker // 66*9356374aSAndroid Build Coastguard Worker // (Note: in practice, a few more entries after `main()` may be added to account 67*9356374aSAndroid Build Coastguard Worker // for startup processes.) 68*9356374aSAndroid Build Coastguard Worker // 69*9356374aSAndroid Build Coastguard Worker // Corresponding stack frame sizes will also be recorded: 70*9356374aSAndroid Build Coastguard Worker // 71*9356374aSAndroid Build Coastguard Worker // sizes[0] 16 72*9356374aSAndroid Build Coastguard Worker // sizes[1] 16 73*9356374aSAndroid Build Coastguard Worker // 74*9356374aSAndroid Build Coastguard Worker // (Stack frame sizes of `16` above are just for illustration purposes.) 75*9356374aSAndroid Build Coastguard Worker // 76*9356374aSAndroid Build Coastguard Worker // Stack frame sizes of 0 or less indicate that those frame sizes couldn't 77*9356374aSAndroid Build Coastguard Worker // be identified. 78*9356374aSAndroid Build Coastguard Worker // 79*9356374aSAndroid Build Coastguard Worker // This routine may return fewer stack frame entries than are 80*9356374aSAndroid Build Coastguard Worker // available. Also note that `result` and `sizes` must both be non-null. 81*9356374aSAndroid Build Coastguard Worker extern int GetStackFrames(void** result, int* sizes, int max_depth, 82*9356374aSAndroid Build Coastguard Worker int skip_count); 83*9356374aSAndroid Build Coastguard Worker 84*9356374aSAndroid Build Coastguard Worker // GetStackFramesWithContext() 85*9356374aSAndroid Build Coastguard Worker // 86*9356374aSAndroid Build Coastguard Worker // Records program counter values obtained from a signal handler. Records 87*9356374aSAndroid Build Coastguard Worker // program counter values for up to `max_depth` frames, skipping the most recent 88*9356374aSAndroid Build Coastguard Worker // `skip_count` stack frames, stores their corresponding values and sizes in 89*9356374aSAndroid Build Coastguard Worker // `results` and `sizes` buffers, and returns the number of frames stored. (Note 90*9356374aSAndroid Build Coastguard Worker // that the frame generated for the `absl::GetStackFramesWithContext()` routine 91*9356374aSAndroid Build Coastguard Worker // itself is also skipped.) 92*9356374aSAndroid Build Coastguard Worker // 93*9356374aSAndroid Build Coastguard Worker // The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value 94*9356374aSAndroid Build Coastguard Worker // passed to a signal handler registered via the `sa_sigaction` field of a 95*9356374aSAndroid Build Coastguard Worker // `sigaction` struct. (See 96*9356374aSAndroid Build Coastguard Worker // http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may 97*9356374aSAndroid Build Coastguard Worker // help a stack unwinder to provide a better stack trace under certain 98*9356374aSAndroid Build Coastguard Worker // conditions. `uc` may safely be null. 99*9356374aSAndroid Build Coastguard Worker // 100*9356374aSAndroid Build Coastguard Worker // The `min_dropped_frames` output parameter, if non-null, points to the 101*9356374aSAndroid Build Coastguard Worker // location to note any dropped stack frames, if any, due to buffer limitations 102*9356374aSAndroid Build Coastguard Worker // or other reasons. (This value will be set to `0` if no frames were dropped.) 103*9356374aSAndroid Build Coastguard Worker // The number of total stack frames is guaranteed to be >= skip_count + 104*9356374aSAndroid Build Coastguard Worker // max_depth + *min_dropped_frames. 105*9356374aSAndroid Build Coastguard Worker extern int GetStackFramesWithContext(void** result, int* sizes, int max_depth, 106*9356374aSAndroid Build Coastguard Worker int skip_count, const void* uc, 107*9356374aSAndroid Build Coastguard Worker int* min_dropped_frames); 108*9356374aSAndroid Build Coastguard Worker 109*9356374aSAndroid Build Coastguard Worker // GetStackTrace() 110*9356374aSAndroid Build Coastguard Worker // 111*9356374aSAndroid Build Coastguard Worker // Records program counter values for up to `max_depth` frames, skipping the 112*9356374aSAndroid Build Coastguard Worker // most recent `skip_count` stack frames, stores their corresponding values 113*9356374aSAndroid Build Coastguard Worker // in `results`, and returns the number of frames 114*9356374aSAndroid Build Coastguard Worker // stored. Note that this function is similar to `absl::GetStackFrames()` 115*9356374aSAndroid Build Coastguard Worker // except that it returns the stack trace only, and not stack frame sizes. 116*9356374aSAndroid Build Coastguard Worker // 117*9356374aSAndroid Build Coastguard Worker // Example: 118*9356374aSAndroid Build Coastguard Worker // 119*9356374aSAndroid Build Coastguard Worker // main() { foo(); } 120*9356374aSAndroid Build Coastguard Worker // foo() { bar(); } 121*9356374aSAndroid Build Coastguard Worker // bar() { 122*9356374aSAndroid Build Coastguard Worker // void* result[10]; 123*9356374aSAndroid Build Coastguard Worker // int depth = absl::GetStackTrace(result, 10, 1); 124*9356374aSAndroid Build Coastguard Worker // } 125*9356374aSAndroid Build Coastguard Worker // 126*9356374aSAndroid Build Coastguard Worker // This produces: 127*9356374aSAndroid Build Coastguard Worker // 128*9356374aSAndroid Build Coastguard Worker // result[0] foo 129*9356374aSAndroid Build Coastguard Worker // result[1] main 130*9356374aSAndroid Build Coastguard Worker // .... ... 131*9356374aSAndroid Build Coastguard Worker // 132*9356374aSAndroid Build Coastguard Worker // `result` must not be null. 133*9356374aSAndroid Build Coastguard Worker extern int GetStackTrace(void** result, int max_depth, int skip_count); 134*9356374aSAndroid Build Coastguard Worker 135*9356374aSAndroid Build Coastguard Worker // GetStackTraceWithContext() 136*9356374aSAndroid Build Coastguard Worker // 137*9356374aSAndroid Build Coastguard Worker // Records program counter values obtained from a signal handler. Records 138*9356374aSAndroid Build Coastguard Worker // program counter values for up to `max_depth` frames, skipping the most recent 139*9356374aSAndroid Build Coastguard Worker // `skip_count` stack frames, stores their corresponding values in `results`, 140*9356374aSAndroid Build Coastguard Worker // and returns the number of frames stored. (Note that the frame generated for 141*9356374aSAndroid Build Coastguard Worker // the `absl::GetStackFramesWithContext()` routine itself is also skipped.) 142*9356374aSAndroid Build Coastguard Worker // 143*9356374aSAndroid Build Coastguard Worker // The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value 144*9356374aSAndroid Build Coastguard Worker // passed to a signal handler registered via the `sa_sigaction` field of a 145*9356374aSAndroid Build Coastguard Worker // `sigaction` struct. (See 146*9356374aSAndroid Build Coastguard Worker // http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may 147*9356374aSAndroid Build Coastguard Worker // help a stack unwinder to provide a better stack trace under certain 148*9356374aSAndroid Build Coastguard Worker // conditions. `uc` may safely be null. 149*9356374aSAndroid Build Coastguard Worker // 150*9356374aSAndroid Build Coastguard Worker // The `min_dropped_frames` output parameter, if non-null, points to the 151*9356374aSAndroid Build Coastguard Worker // location to note any dropped stack frames, if any, due to buffer limitations 152*9356374aSAndroid Build Coastguard Worker // or other reasons. (This value will be set to `0` if no frames were dropped.) 153*9356374aSAndroid Build Coastguard Worker // The number of total stack frames is guaranteed to be >= skip_count + 154*9356374aSAndroid Build Coastguard Worker // max_depth + *min_dropped_frames. 155*9356374aSAndroid Build Coastguard Worker extern int GetStackTraceWithContext(void** result, int max_depth, 156*9356374aSAndroid Build Coastguard Worker int skip_count, const void* uc, 157*9356374aSAndroid Build Coastguard Worker int* min_dropped_frames); 158*9356374aSAndroid Build Coastguard Worker 159*9356374aSAndroid Build Coastguard Worker // SetStackUnwinder() 160*9356374aSAndroid Build Coastguard Worker // 161*9356374aSAndroid Build Coastguard Worker // Provides a custom function for unwinding stack frames that will be used in 162*9356374aSAndroid Build Coastguard Worker // place of the default stack unwinder when invoking the static 163*9356374aSAndroid Build Coastguard Worker // GetStack{Frames,Trace}{,WithContext}() functions above. 164*9356374aSAndroid Build Coastguard Worker // 165*9356374aSAndroid Build Coastguard Worker // The arguments passed to the unwinder function will match the 166*9356374aSAndroid Build Coastguard Worker // arguments passed to `absl::GetStackFramesWithContext()` except that sizes 167*9356374aSAndroid Build Coastguard Worker // will be non-null iff the caller is interested in frame sizes. 168*9356374aSAndroid Build Coastguard Worker // 169*9356374aSAndroid Build Coastguard Worker // If unwinder is set to null, we revert to the default stack-tracing behavior. 170*9356374aSAndroid Build Coastguard Worker // 171*9356374aSAndroid Build Coastguard Worker // ***************************************************************************** 172*9356374aSAndroid Build Coastguard Worker // WARNING 173*9356374aSAndroid Build Coastguard Worker // ***************************************************************************** 174*9356374aSAndroid Build Coastguard Worker // 175*9356374aSAndroid Build Coastguard Worker // absl::SetStackUnwinder is not suitable for general purpose use. It is 176*9356374aSAndroid Build Coastguard Worker // provided for custom runtimes. 177*9356374aSAndroid Build Coastguard Worker // Some things to watch out for when calling `absl::SetStackUnwinder()`: 178*9356374aSAndroid Build Coastguard Worker // 179*9356374aSAndroid Build Coastguard Worker // (a) The unwinder may be called from within signal handlers and 180*9356374aSAndroid Build Coastguard Worker // therefore must be async-signal-safe. 181*9356374aSAndroid Build Coastguard Worker // 182*9356374aSAndroid Build Coastguard Worker // (b) Even after a custom stack unwinder has been unregistered, other 183*9356374aSAndroid Build Coastguard Worker // threads may still be in the process of using that unwinder. 184*9356374aSAndroid Build Coastguard Worker // Therefore do not clean up any state that may be needed by an old 185*9356374aSAndroid Build Coastguard Worker // unwinder. 186*9356374aSAndroid Build Coastguard Worker // ***************************************************************************** 187*9356374aSAndroid Build Coastguard Worker extern void SetStackUnwinder(int (*unwinder)(void** pcs, int* sizes, 188*9356374aSAndroid Build Coastguard Worker int max_depth, int skip_count, 189*9356374aSAndroid Build Coastguard Worker const void* uc, 190*9356374aSAndroid Build Coastguard Worker int* min_dropped_frames)); 191*9356374aSAndroid Build Coastguard Worker 192*9356374aSAndroid Build Coastguard Worker // DefaultStackUnwinder() 193*9356374aSAndroid Build Coastguard Worker // 194*9356374aSAndroid Build Coastguard Worker // Records program counter values of up to `max_depth` frames, skipping the most 195*9356374aSAndroid Build Coastguard Worker // recent `skip_count` stack frames, and stores their corresponding values in 196*9356374aSAndroid Build Coastguard Worker // `pcs`. (Note that the frame generated for this call itself is also skipped.) 197*9356374aSAndroid Build Coastguard Worker // This function acts as a generic stack-unwinder; prefer usage of the more 198*9356374aSAndroid Build Coastguard Worker // specific `GetStack{Trace,Frames}{,WithContext}()` functions above. 199*9356374aSAndroid Build Coastguard Worker // 200*9356374aSAndroid Build Coastguard Worker // If you have set your own stack unwinder (with the `SetStackUnwinder()` 201*9356374aSAndroid Build Coastguard Worker // function above, you can still get the default stack unwinder by calling 202*9356374aSAndroid Build Coastguard Worker // `DefaultStackUnwinder()`, which will ignore any previously set stack unwinder 203*9356374aSAndroid Build Coastguard Worker // and use the default one instead. 204*9356374aSAndroid Build Coastguard Worker // 205*9356374aSAndroid Build Coastguard Worker // Because this function is generic, only `pcs` is guaranteed to be non-null 206*9356374aSAndroid Build Coastguard Worker // upon return. It is legal for `sizes`, `uc`, and `min_dropped_frames` to all 207*9356374aSAndroid Build Coastguard Worker // be null when called. 208*9356374aSAndroid Build Coastguard Worker // 209*9356374aSAndroid Build Coastguard Worker // The semantics are the same as the corresponding `GetStack*()` function in the 210*9356374aSAndroid Build Coastguard Worker // case where `absl::SetStackUnwinder()` was never called. Equivalents are: 211*9356374aSAndroid Build Coastguard Worker // 212*9356374aSAndroid Build Coastguard Worker // null sizes | non-nullptr sizes 213*9356374aSAndroid Build Coastguard Worker // |==========================================================| 214*9356374aSAndroid Build Coastguard Worker // null uc | GetStackTrace() | GetStackFrames() | 215*9356374aSAndroid Build Coastguard Worker // non-null uc | GetStackTraceWithContext() | GetStackFramesWithContext() | 216*9356374aSAndroid Build Coastguard Worker // |==========================================================| 217*9356374aSAndroid Build Coastguard Worker extern int DefaultStackUnwinder(void** pcs, int* sizes, int max_depth, 218*9356374aSAndroid Build Coastguard Worker int skip_count, const void* uc, 219*9356374aSAndroid Build Coastguard Worker int* min_dropped_frames); 220*9356374aSAndroid Build Coastguard Worker 221*9356374aSAndroid Build Coastguard Worker namespace debugging_internal { 222*9356374aSAndroid Build Coastguard Worker // Returns true for platforms which are expected to have functioning stack trace 223*9356374aSAndroid Build Coastguard Worker // implementations. Intended to be used for tests which want to exclude 224*9356374aSAndroid Build Coastguard Worker // verification of logic known to be broken because stack traces are not 225*9356374aSAndroid Build Coastguard Worker // working. 226*9356374aSAndroid Build Coastguard Worker extern bool StackTraceWorksForTest(); 227*9356374aSAndroid Build Coastguard Worker } // namespace debugging_internal 228*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END 229*9356374aSAndroid Build Coastguard Worker } // namespace absl 230*9356374aSAndroid Build Coastguard Worker 231*9356374aSAndroid Build Coastguard Worker #endif // ABSL_DEBUGGING_STACKTRACE_H_ 232