Path: blob/master/thirdparty/libbacktrace/backtrace.h
9902 views
/* backtrace.h -- Public header file for stack backtrace library.1Copyright (C) 2012-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#ifndef BACKTRACE_H33#define BACKTRACE_H3435#include <stddef.h>36#include <stdint.h>37#include <stdio.h>3839#ifdef __cplusplus40extern "C" {41#endif4243/* The backtrace state. This struct is intentionally not defined in44the public interface. */4546struct backtrace_state;4748/* The type of the error callback argument to backtrace functions.49This function, if not NULL, will be called for certain error cases.50The DATA argument is passed to the function that calls this one.51The MSG argument is an error message. The ERRNUM argument, if52greater than 0, holds an errno value. The MSG buffer may become53invalid after this function returns.5455As a special case, the ERRNUM argument will be passed as -1 if no56debug info can be found for the executable, or if the debug info57exists but has an unsupported version, but the function requires58debug info (e.g., backtrace_full, backtrace_pcinfo). The MSG in59this case will be something along the lines of "no debug info".60Similarly, ERRNUM will be passed as -1 if there is no symbol table,61but the function requires a symbol table (e.g., backtrace_syminfo).62This may be used as a signal that some other approach should be63tried. */6465typedef void (*backtrace_error_callback) (void *data, const char *msg,66int errnum);6768/* Create state information for the backtrace routines. This must be69called before any of the other routines, and its return value must70be passed to all of the other routines. FILENAME is the path name71of the executable file; if it is NULL the library will try72system-specific path names. If not NULL, FILENAME must point to a73permanent buffer. If THREADED is non-zero the state may be74accessed by multiple threads simultaneously, and the library will75use appropriate atomic operations. If THREADED is zero the state76may only be accessed by one thread at a time. This returns a state77pointer on success, NULL on error. If an error occurs, this will78call the ERROR_CALLBACK routine.7980Calling this function allocates resources that cannot be freed.81There is no backtrace_free_state function. The state is used to82cache information that is expensive to recompute. Programs are83expected to call this function at most once and to save the return84value for all later calls to backtrace functions. */8586extern struct backtrace_state *backtrace_create_state (87const char *filename, int threaded,88backtrace_error_callback error_callback, void *data);8990/* The type of the callback argument to the backtrace_full function.91DATA is the argument passed to backtrace_full. PC is the program92counter. FILENAME is the name of the file containing PC, or NULL93if not available. LINENO is the line number in FILENAME containing94PC, or 0 if not available. FUNCTION is the name of the function95containing PC, or NULL if not available. This should return 0 to96continuing tracing. The FILENAME and FUNCTION buffers may become97invalid after this function returns. */9899typedef int (*backtrace_full_callback) (void *data, uintptr_t pc,100const char *filename, int lineno,101const char *function);102103/* Get a full stack backtrace. SKIP is the number of frames to skip;104passing 0 will start the trace with the function calling105backtrace_full. DATA is passed to the callback routine. If any106call to CALLBACK returns a non-zero value, the stack backtrace107stops, and backtrace returns that value; this may be used to limit108the number of stack frames desired. If all calls to CALLBACK109return 0, backtrace returns 0. The backtrace_full function will110make at least one call to either CALLBACK or ERROR_CALLBACK. This111function requires debug info for the executable. */112113extern int backtrace_full (struct backtrace_state *state, int skip,114backtrace_full_callback callback,115backtrace_error_callback error_callback,116void *data);117118/* The type of the callback argument to the backtrace_simple function.119DATA is the argument passed to simple_backtrace. PC is the program120counter. This should return 0 to continue tracing. */121122typedef int (*backtrace_simple_callback) (void *data, uintptr_t pc);123124/* Get a simple backtrace. SKIP is the number of frames to skip, as125in backtrace. DATA is passed to the callback routine. If any call126to CALLBACK returns a non-zero value, the stack backtrace stops,127and backtrace_simple returns that value. Otherwise128backtrace_simple returns 0. The backtrace_simple function will129make at least one call to either CALLBACK or ERROR_CALLBACK. This130function does not require any debug info for the executable. */131132extern int backtrace_simple (struct backtrace_state *state, int skip,133backtrace_simple_callback callback,134backtrace_error_callback error_callback,135void *data);136137/* Print the current backtrace in a user readable format to a FILE.138SKIP is the number of frames to skip, as in backtrace_full. Any139error messages are printed to stderr. This function requires debug140info for the executable. */141142extern void backtrace_print (struct backtrace_state *state, int skip, FILE *);143144/* Given PC, a program counter in the current program, call the145callback function with filename, line number, and function name146information. This will normally call the callback function exactly147once. However, if the PC happens to describe an inlined call, and148the debugging information contains the necessary information, then149this may call the callback function multiple times. This will make150at least one call to either CALLBACK or ERROR_CALLBACK. This151returns the first non-zero value returned by CALLBACK, or 0. */152153extern int backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,154backtrace_full_callback callback,155backtrace_error_callback error_callback,156void *data);157158/* The type of the callback argument to backtrace_syminfo. DATA and159PC are the arguments passed to backtrace_syminfo. SYMNAME is the160name of the symbol for the corresponding code. SYMVAL is the161value and SYMSIZE is the size of the symbol. SYMNAME will be NULL162if no error occurred but the symbol could not be found. */163164typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc,165const char *symname,166uintptr_t symval,167uintptr_t symsize);168169/* Given ADDR, an address or program counter in the current program,170call the callback information with the symbol name and value171describing the function or variable in which ADDR may be found.172This will call either CALLBACK or ERROR_CALLBACK exactly once.173This returns 1 on success, 0 on failure. This function requires174the symbol table but does not require the debug info. Note that if175the symbol table is present but ADDR could not be found in the176table, CALLBACK will be called with a NULL SYMNAME argument.177Returns 1 on success, 0 on error. */178179extern int backtrace_syminfo (struct backtrace_state *state, uintptr_t addr,180backtrace_syminfo_callback callback,181backtrace_error_callback error_callback,182void *data);183184#ifdef __cplusplus185} /* End extern "C". */186#endif187188#endif189190191