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