stacktrace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #ifndef ABSL_DEBUGGING_STACKTRACE_H_
  31. #define ABSL_DEBUGGING_STACKTRACE_H_
  32. #include "absl/base/config.h"
  33. namespace absl {
  34. ABSL_NAMESPACE_BEGIN
  35. // GetStackFrames()
  36. //
  37. // Records program counter values for up to `max_depth` frames, skipping the
  38. // most recent `skip_count` stack frames, stores their corresponding values
  39. // and sizes in `results` and `sizes` buffers, and returns the number of frames
  40. // stored. (Note that the frame generated for the `absl::GetStackFrames()`
  41. // routine itself is also skipped.)
  42. //
  43. // Example:
  44. //
  45. // main() { foo(); }
  46. // foo() { bar(); }
  47. // bar() {
  48. // void* result[10];
  49. // int sizes[10];
  50. // int depth = absl::GetStackFrames(result, sizes, 10, 1);
  51. // }
  52. //
  53. // The current stack frame would consist of three function calls: `bar()`,
  54. // `foo()`, and then `main()`; however, since the `GetStackFrames()` call sets
  55. // `skip_count` to `1`, it will skip the frame for `bar()`, the most recently
  56. // invoked function call. It will therefore return 2 and fill `result` with
  57. // program counters within the following functions:
  58. //
  59. // result[0] foo()
  60. // result[1] main()
  61. //
  62. // (Note: in practice, a few more entries after `main()` may be added to account
  63. // for startup processes.)
  64. //
  65. // Corresponding stack frame sizes will also be recorded:
  66. //
  67. // sizes[0] 16
  68. // sizes[1] 16
  69. //
  70. // (Stack frame sizes of `16` above are just for illustration purposes.)
  71. //
  72. // Stack frame sizes of 0 or less indicate that those frame sizes couldn't
  73. // be identified.
  74. //
  75. // This routine may return fewer stack frame entries than are
  76. // available. Also note that `result` and `sizes` must both be non-null.
  77. extern int GetStackFrames(void** result, int* sizes, int max_depth,
  78. int skip_count);
  79. // GetStackFramesWithContext()
  80. //
  81. // Records program counter values obtained from a signal handler. Records
  82. // program counter values for up to `max_depth` frames, skipping the most recent
  83. // `skip_count` stack frames, stores their corresponding values and sizes in
  84. // `results` and `sizes` buffers, and returns the number of frames stored. (Note
  85. // that the frame generated for the `absl::GetStackFramesWithContext()` routine
  86. // itself is also skipped.)
  87. //
  88. // The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
  89. // passed to a signal handler registered via the `sa_sigaction` field of a
  90. // `sigaction` struct. (See
  91. // http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
  92. // help a stack unwinder to provide a better stack trace under certain
  93. // conditions. `uc` may safely be null.
  94. //
  95. // The `min_dropped_frames` output parameter, if non-null, points to the
  96. // location to note any dropped stack frames, if any, due to buffer limitations
  97. // or other reasons. (This value will be set to `0` if no frames were dropped.)
  98. // The number of total stack frames is guaranteed to be >= skip_count +
  99. // max_depth + *min_dropped_frames.
  100. extern int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
  101. int skip_count, const void* uc,
  102. int* min_dropped_frames);
  103. // GetStackTrace()
  104. //
  105. // Records program counter values for up to `max_depth` frames, skipping the
  106. // most recent `skip_count` stack frames, stores their corresponding values
  107. // in `results`, and returns the number of frames
  108. // stored. Note that this function is similar to `absl::GetStackFrames()`
  109. // except that it returns the stack trace only, and not stack frame sizes.
  110. //
  111. // Example:
  112. //
  113. // main() { foo(); }
  114. // foo() { bar(); }
  115. // bar() {
  116. // void* result[10];
  117. // int depth = absl::GetStackTrace(result, 10, 1);
  118. // }
  119. //
  120. // This produces:
  121. //
  122. // result[0] foo
  123. // result[1] main
  124. // .... ...
  125. //
  126. // `result` must not be null.
  127. extern int GetStackTrace(void** result, int max_depth, int skip_count);
  128. // GetStackTraceWithContext()
  129. //
  130. // Records program counter values obtained from a signal handler. Records
  131. // program counter values for up to `max_depth` frames, skipping the most recent
  132. // `skip_count` stack frames, stores their corresponding values in `results`,
  133. // and returns the number of frames stored. (Note that the frame generated for
  134. // the `absl::GetStackFramesWithContext()` routine itself is also skipped.)
  135. //
  136. // The `uc` parameter, if non-null, should be a pointer to a `ucontext_t` value
  137. // passed to a signal handler registered via the `sa_sigaction` field of a
  138. // `sigaction` struct. (See
  139. // http://man7.org/linux/man-pages/man2/sigaction.2.html.) The `uc` value may
  140. // help a stack unwinder to provide a better stack trace under certain
  141. // conditions. `uc` may safely be null.
  142. //
  143. // The `min_dropped_frames` output parameter, if non-null, points to the
  144. // location to note any dropped stack frames, if any, due to buffer limitations
  145. // or other reasons. (This value will be set to `0` if no frames were dropped.)
  146. // The number of total stack frames is guaranteed to be >= skip_count +
  147. // max_depth + *min_dropped_frames.
  148. extern int GetStackTraceWithContext(void** result, int max_depth,
  149. int skip_count, const void* uc,
  150. int* min_dropped_frames);
  151. // SetStackUnwinder()
  152. //
  153. // Provides a custom function for unwinding stack frames that will be used in
  154. // place of the default stack unwinder when invoking the static
  155. // GetStack{Frames,Trace}{,WithContext}() functions above.
  156. //
  157. // The arguments passed to the unwinder function will match the
  158. // arguments passed to `absl::GetStackFramesWithContext()` except that sizes
  159. // will be non-null iff the caller is interested in frame sizes.
  160. //
  161. // If unwinder is set to null, we revert to the default stack-tracing behavior.
  162. //
  163. // *****************************************************************************
  164. // WARNING
  165. // *****************************************************************************
  166. //
  167. // absl::SetStackUnwinder is not suitable for general purpose use. It is
  168. // provided for custom runtimes.
  169. // Some things to watch out for when calling `absl::SetStackUnwinder()`:
  170. //
  171. // (a) The unwinder may be called from within signal handlers and
  172. // therefore must be async-signal-safe.
  173. //
  174. // (b) Even after a custom stack unwinder has been unregistered, other
  175. // threads may still be in the process of using that unwinder.
  176. // Therefore do not clean up any state that may be needed by an old
  177. // unwinder.
  178. // *****************************************************************************
  179. extern void SetStackUnwinder(int (*unwinder)(void** pcs, int* sizes,
  180. int max_depth, int skip_count,
  181. const void* uc,
  182. int* min_dropped_frames));
  183. // DefaultStackUnwinder()
  184. //
  185. // Records program counter values of up to `max_depth` frames, skipping the most
  186. // recent `skip_count` stack frames, and stores their corresponding values in
  187. // `pcs`. (Note that the frame generated for this call itself is also skipped.)
  188. // This function acts as a generic stack-unwinder; prefer usage of the more
  189. // specific `GetStack{Trace,Frames}{,WithContext}()` functions above.
  190. //
  191. // If you have set your own stack unwinder (with the `SetStackUnwinder()`
  192. // function above, you can still get the default stack unwinder by calling
  193. // `DefaultStackUnwinder()`, which will ignore any previously set stack unwinder
  194. // and use the default one instead.
  195. //
  196. // Because this function is generic, only `pcs` is guaranteed to be non-null
  197. // upon return. It is legal for `sizes`, `uc`, and `min_dropped_frames` to all
  198. // be null when called.
  199. //
  200. // The semantics are the same as the corresponding `GetStack*()` function in the
  201. // case where `absl::SetStackUnwinder()` was never called. Equivalents are:
  202. //
  203. // null sizes | non-nullptr sizes
  204. // |==========================================================|
  205. // null uc | GetStackTrace() | GetStackFrames() |
  206. // non-null uc | GetStackTraceWithContext() | GetStackFramesWithContext() |
  207. // |==========================================================|
  208. extern int DefaultStackUnwinder(void** pcs, int* sizes, int max_depth,
  209. int skip_count, const void* uc,
  210. int* min_dropped_frames);
  211. namespace debugging_internal {
  212. // Returns true for platforms which are expected to have functioning stack trace
  213. // implementations. Intended to be used for tests which want to exclude
  214. // verification of logic known to be broken because stack traces are not
  215. // working.
  216. extern bool StackTraceWorksForTest();
  217. } // namespace debugging_internal
  218. ABSL_NAMESPACE_END
  219. } // namespace absl
  220. #endif // ABSL_DEBUGGING_STACKTRACE_H_