You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

stacktrace.h 11 kB

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