backtrace.h 8.6 KB

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