backtrace.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* backtrace.h -- Public header file for stack backtrace library.
  2. Copyright (C) 2012-2021 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #ifndef BACKTRACE_H
  28. #define BACKTRACE_H
  29. #include <stddef.h>
  30. #include <stdio.h>
  31. /* We want to get a definition for uintptr_t, but we still care about
  32. systems that don't have <stdint.h>. */
  33. #if defined(__GLIBC__) && __GLIBC__ >= 2
  34. #include <stdint.h>
  35. #elif defined(HAVE_STDINT_H)
  36. #include <stdint.h>
  37. #else
  38. /* Systems that don't have <stdint.h> must provide gstdint.h, e.g.,
  39. from GCC_HEADER_STDINT in configure.ac. */
  40. #include "gstdint.h"
  41. #endif
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* The backtrace state. This struct is intentionally not defined in
  46. the public interface. */
  47. struct backtrace_state;
  48. /* The type of the error callback argument to backtrace functions.
  49. This function, if not NULL, will be called for certain error cases.
  50. The DATA argument is passed to the function that calls this one.
  51. The MSG argument is an error message. The ERRNUM argument, if
  52. greater than 0, holds an errno value. The MSG buffer may become
  53. invalid after this function returns.
  54. As a special case, the ERRNUM argument will be passed as -1 if no
  55. debug info can be found for the executable, or if the debug info
  56. exists but has an unsupported version, but the function requires
  57. debug info (e.g., backtrace_full, backtrace_pcinfo). The MSG in
  58. this case will be something along the lines of "no debug info".
  59. Similarly, ERRNUM will be passed as -1 if there is no symbol table,
  60. but the function requires a symbol table (e.g., backtrace_syminfo).
  61. This may be used as a signal that some other approach should be
  62. tried. */
  63. typedef void (*backtrace_error_callback) (void *data, const char *msg,
  64. int errnum);
  65. /* Create state information for the backtrace routines. This must be
  66. called before any of the other routines, and its return value must
  67. be passed to all of the other routines. FILENAME is the path name
  68. of the executable file; if it is NULL the library will try
  69. system-specific path names. If not NULL, FILENAME must point to a
  70. permanent buffer. If THREADED is non-zero the state may be
  71. accessed by multiple threads simultaneously, and the library will
  72. use appropriate atomic operations. If THREADED is zero the state
  73. may only be accessed by one thread at a time. This returns a state
  74. pointer on success, NULL on error. If an error occurs, this will
  75. call the ERROR_CALLBACK routine.
  76. Calling this function allocates resources that cannot be freed.
  77. There is no backtrace_free_state function. The state is used to
  78. cache information that is expensive to recompute. Programs are
  79. expected to call this function at most once and to save the return
  80. value for all later calls to backtrace functions. */
  81. extern struct backtrace_state *backtrace_create_state (
  82. const char *filename, int threaded,
  83. backtrace_error_callback error_callback, void *data);
  84. /* The type of the callback argument to the backtrace_full function.
  85. DATA is the argument passed to backtrace_full. PC is the program
  86. counter. FILENAME is the name of the file containing PC, or NULL
  87. if not available. LINENO is the line number in FILENAME containing
  88. PC, or 0 if not available. FUNCTION is the name of the function
  89. containing PC, or NULL if not available. This should return 0 to
  90. continuing tracing. The FILENAME and FUNCTION buffers may become
  91. invalid after this function returns. */
  92. typedef int (*backtrace_full_callback) (void *data, uintptr_t pc,
  93. const char *filename, int lineno,
  94. const char *function);
  95. /* Get a full stack backtrace. SKIP is the number of frames to skip;
  96. passing 0 will start the trace with the function calling
  97. backtrace_full. DATA is passed to the callback routine. If any
  98. call to CALLBACK returns a non-zero value, the stack backtrace
  99. stops, and backtrace returns that value; this may be used to limit
  100. the number of stack frames desired. If all calls to CALLBACK
  101. return 0, backtrace returns 0. The backtrace_full function will
  102. make at least one call to either CALLBACK or ERROR_CALLBACK. This
  103. function requires debug info for the executable. */
  104. extern int backtrace_full (struct backtrace_state *state, int skip,
  105. backtrace_full_callback callback,
  106. backtrace_error_callback error_callback,
  107. void *data);
  108. /* The type of the callback argument to the backtrace_simple function.
  109. DATA is the argument passed to simple_backtrace. PC is the program
  110. counter. This should return 0 to continue tracing. */
  111. typedef int (*backtrace_simple_callback) (void *data, uintptr_t pc);
  112. /* Get a simple backtrace. SKIP is the number of frames to skip, as
  113. in backtrace. DATA is passed to the callback routine. If any call
  114. to CALLBACK returns a non-zero value, the stack backtrace stops,
  115. and backtrace_simple returns that value. Otherwise
  116. backtrace_simple returns 0. The backtrace_simple function will
  117. make at least one call to either CALLBACK or ERROR_CALLBACK. This
  118. function does not require any debug info for the executable. */
  119. extern int backtrace_simple (struct backtrace_state *state, int skip,
  120. backtrace_simple_callback callback,
  121. backtrace_error_callback error_callback,
  122. void *data);
  123. /* Print the current backtrace in a user readable format to a FILE.
  124. SKIP is the number of frames to skip, as in backtrace_full. Any
  125. error messages are printed to stderr. This function requires debug
  126. info for the executable. */
  127. extern void backtrace_print (struct backtrace_state *state, int skip, FILE *);
  128. /* Given PC, a program counter in the current program, call the
  129. callback function with filename, line number, and function name
  130. information. This will normally call the callback function exactly
  131. once. However, if the PC happens to describe an inlined call, and
  132. the debugging information contains the necessary information, then
  133. this may call the callback function multiple times. This will make
  134. at least one call to either CALLBACK or ERROR_CALLBACK. This
  135. returns the first non-zero value returned by CALLBACK, or 0. */
  136. extern int backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
  137. backtrace_full_callback callback,
  138. backtrace_error_callback error_callback,
  139. void *data);
  140. /* The type of the callback argument to backtrace_syminfo. DATA and
  141. PC are the arguments passed to backtrace_syminfo. SYMNAME is the
  142. name of the symbol for the corresponding code. SYMVAL is the
  143. value and SYMSIZE is the size of the symbol. SYMNAME will be NULL
  144. if no error occurred but the symbol could not be found. */
  145. typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc,
  146. const char *symname,
  147. uintptr_t symval,
  148. uintptr_t symsize);
  149. /* Given ADDR, an address or program counter in the current program,
  150. call the callback information with the symbol name and value
  151. describing the function or variable in which ADDR may be found.
  152. This will call either CALLBACK or ERROR_CALLBACK exactly once.
  153. This returns 1 on success, 0 on failure. This function requires
  154. the symbol table but does not require the debug info. Note that if
  155. the symbol table is present but ADDR could not be found in the
  156. table, CALLBACK will be called with a NULL SYMNAME argument.
  157. Returns 1 on success, 0 on error. */
  158. extern int backtrace_syminfo (struct backtrace_state *state, uintptr_t addr,
  159. backtrace_syminfo_callback callback,
  160. backtrace_error_callback error_callback,
  161. void *data);
  162. #ifdef __cplusplus
  163. } /* End extern "C". */
  164. #endif
  165. #endif