backtrace.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* backtrace.h -- Public header file for stack backtrace library.
  2. Copyright (C) 2012-2019 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, but the function
  56. requires debug info (e.g., backtrace_full, backtrace_pcinfo). The
  57. MSG in this case will be something along the lines of "no debug
  58. info". Similarly, ERRNUM will be passed as -1 if there is no
  59. symbol table, but the function requires a symbol table (e.g.,
  60. backtrace_syminfo). This may be used as a signal that some other
  61. approach should be tried. */
  62. typedef void (*backtrace_error_callback) (void *data, const char *msg,
  63. int errnum);
  64. /* Create state information for the backtrace routines. This must be
  65. called before any of the other routines, and its return value must
  66. be passed to all of the other routines. FILENAME is the path name
  67. of the executable file; if it is NULL the library will try
  68. system-specific path names. If not NULL, FILENAME must point to a
  69. permanent buffer. If THREADED is non-zero the state may be
  70. accessed by multiple threads simultaneously, and the library will
  71. use appropriate atomic operations. If THREADED is zero the state
  72. may only be accessed by one thread at a time. This returns a state
  73. pointer on success, NULL on error. If an error occurs, this will
  74. call the ERROR_CALLBACK routine.
  75. Calling this function allocates resources that cannot be freed.
  76. There is no backtrace_free_state function. The state is used to
  77. cache information that is expensive to recompute. Programs are
  78. expected to call this function at most once and to save the return
  79. value for all later calls to backtrace functions. */
  80. extern struct backtrace_state *backtrace_create_state (
  81. const char *filename, int threaded,
  82. backtrace_error_callback error_callback, void *data);
  83. /* The type of the callback argument to the backtrace_full function.
  84. DATA is the argument passed to backtrace_full. PC is the program
  85. counter. FILENAME is the name of the file containing PC, or NULL
  86. if not available. LINENO is the line number in FILENAME containing
  87. PC, or 0 if not available. FUNCTION is the name of the function
  88. containing PC, or NULL if not available. This should return 0 to
  89. continuing tracing. The FILENAME and FUNCTION buffers may become
  90. invalid after this function returns. */
  91. typedef int (*backtrace_full_callback) (void *data, uintptr_t pc,
  92. const char *filename, int lineno,
  93. const char *function);
  94. /* Get a full stack backtrace. SKIP is the number of frames to skip;
  95. passing 0 will start the trace with the function calling
  96. backtrace_full. DATA is passed to the callback routine. If any
  97. call to CALLBACK returns a non-zero value, the stack backtrace
  98. stops, and backtrace returns that value; this may be used to limit
  99. the number of stack frames desired. If all calls to CALLBACK
  100. return 0, backtrace returns 0. The backtrace_full function will
  101. make at least one call to either CALLBACK or ERROR_CALLBACK. This
  102. function requires debug info for the executable. */
  103. extern int backtrace_full (struct backtrace_state *state, int skip,
  104. backtrace_full_callback callback,
  105. backtrace_error_callback error_callback,
  106. void *data);
  107. /* The type of the callback argument to the backtrace_simple function.
  108. DATA is the argument passed to simple_backtrace. PC is the program
  109. counter. This should return 0 to continue tracing. */
  110. typedef int (*backtrace_simple_callback) (void *data, uintptr_t pc);
  111. /* Get a simple backtrace. SKIP is the number of frames to skip, as
  112. in backtrace. DATA is passed to the callback routine. If any call
  113. to CALLBACK returns a non-zero value, the stack backtrace stops,
  114. and backtrace_simple returns that value. Otherwise
  115. backtrace_simple returns 0. The backtrace_simple function will
  116. make at least one call to either CALLBACK or ERROR_CALLBACK. This
  117. function does not require any debug info for the executable. */
  118. extern int backtrace_simple (struct backtrace_state *state, int skip,
  119. backtrace_simple_callback callback,
  120. backtrace_error_callback error_callback,
  121. void *data);
  122. /* Print the current backtrace in a user readable format to a FILE.
  123. SKIP is the number of frames to skip, as in backtrace_full. Any
  124. error messages are printed to stderr. This function requires debug
  125. info for the executable. */
  126. extern void backtrace_print (struct backtrace_state *state, int skip, FILE *);
  127. /* Given PC, a program counter in the current program, call the
  128. callback function with filename, line number, and function name
  129. information. This will normally call the callback function exactly
  130. once. However, if the PC happens to describe an inlined call, and
  131. the debugging information contains the necessary information, then
  132. this may call the callback function multiple times. This will make
  133. at least one call to either CALLBACK or ERROR_CALLBACK. This
  134. returns the first non-zero value returned by CALLBACK, or 0. */
  135. extern int backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
  136. backtrace_full_callback callback,
  137. backtrace_error_callback error_callback,
  138. void *data);
  139. /* The type of the callback argument to backtrace_syminfo. DATA and
  140. PC are the arguments passed to backtrace_syminfo. SYMNAME is the
  141. name of the symbol for the corresponding code. SYMVAL is the
  142. value and SYMSIZE is the size of the symbol. SYMNAME will be NULL
  143. if no error occurred but the symbol could not be found. */
  144. typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc,
  145. const char *symname,
  146. uintptr_t symval,
  147. uintptr_t symsize);
  148. /* Given ADDR, an address or program counter in the current program,
  149. call the callback information with the symbol name and value
  150. describing the function or variable in which ADDR may be found.
  151. This will call either CALLBACK or ERROR_CALLBACK exactly once.
  152. This returns 1 on success, 0 on failure. This function requires
  153. the symbol table but does not require the debug info. Note that if
  154. the symbol table is present but ADDR could not be found in the
  155. table, CALLBACK will be called with a NULL SYMNAME argument.
  156. Returns 1 on success, 0 on error. */
  157. extern int backtrace_syminfo (struct backtrace_state *state, uintptr_t addr,
  158. backtrace_syminfo_callback callback,
  159. backtrace_error_callback error_callback,
  160. void *data);
  161. #ifdef __cplusplus
  162. } /* End extern "C". */
  163. #endif
  164. #endif