/* internal.h -- Internal 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_INTERNAL_H33#define BACKTRACE_INTERNAL_H3435/* We assume that <sys/types.h> and "backtrace.h" have already been36included. */3738#ifndef GCC_VERSION39# define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)40#endif4142#if (GCC_VERSION < 2007)43# define __attribute__(x)44#endif4546#ifndef ATTRIBUTE_UNUSED47# define ATTRIBUTE_UNUSED __attribute__ ((__unused__))48#endif4950#ifndef ATTRIBUTE_MALLOC51# if (GCC_VERSION >= 2096)52# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))53# else54# define ATTRIBUTE_MALLOC55# endif56#endif5758#ifndef ATTRIBUTE_FALLTHROUGH59# if (GCC_VERSION >= 7000)60# define ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__))61# else62# define ATTRIBUTE_FALLTHROUGH63# endif64#endif6566#ifndef HAVE_SYNC_FUNCTIONS6768/* Define out the sync functions. These should never be called if69they are not available. */7071#define __sync_bool_compare_and_swap(A, B, C) (abort(), 1)72#define __sync_lock_test_and_set(A, B) (abort(), 0)73#define __sync_lock_release(A) abort()7475#endif /* !defined (HAVE_SYNC_FUNCTIONS) */7677#ifdef HAVE_ATOMIC_FUNCTIONS7879/* We have the atomic builtin functions. */8081#define backtrace_atomic_load_pointer(p) \82__atomic_load_n ((p), __ATOMIC_ACQUIRE)83#define backtrace_atomic_load_int(p) \84__atomic_load_n ((p), __ATOMIC_ACQUIRE)85#define backtrace_atomic_store_pointer(p, v) \86__atomic_store_n ((p), (v), __ATOMIC_RELEASE)87#define backtrace_atomic_store_size_t(p, v) \88__atomic_store_n ((p), (v), __ATOMIC_RELEASE)89#define backtrace_atomic_store_int(p, v) \90__atomic_store_n ((p), (v), __ATOMIC_RELEASE)9192#else /* !defined (HAVE_ATOMIC_FUNCTIONS) */93#ifdef HAVE_SYNC_FUNCTIONS9495/* We have the sync functions but not the atomic functions. Define96the atomic ones in terms of the sync ones. */9798extern void *backtrace_atomic_load_pointer (void *);99extern int backtrace_atomic_load_int (int *);100extern void backtrace_atomic_store_pointer (void *, void *);101extern void backtrace_atomic_store_size_t (size_t *, size_t);102extern void backtrace_atomic_store_int (int *, int);103104#else /* !defined (HAVE_SYNC_FUNCTIONS) */105106/* We have neither the sync nor the atomic functions. These will107never be called. */108109#define backtrace_atomic_load_pointer(p) (abort(), (void *) NULL)110#define backtrace_atomic_load_int(p) (abort(), 0)111#define backtrace_atomic_store_pointer(p, v) abort()112#define backtrace_atomic_store_size_t(p, v) abort()113#define backtrace_atomic_store_int(p, v) abort()114115#endif /* !defined (HAVE_SYNC_FUNCTIONS) */116#endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */117118/* The type of the function that collects file/line information. This119is like backtrace_pcinfo. */120121typedef int (*fileline) (struct backtrace_state *state, uintptr_t pc,122backtrace_full_callback callback,123backtrace_error_callback error_callback, void *data);124125/* The type of the function that collects symbol information. This is126like backtrace_syminfo. */127128typedef void (*syminfo) (struct backtrace_state *state, uintptr_t pc,129backtrace_syminfo_callback callback,130backtrace_error_callback error_callback, void *data);131132/* What the backtrace state pointer points to. */133134struct backtrace_state135{136/* The name of the executable. */137const char *filename;138/* Non-zero if threaded. */139int threaded;140/* The master lock for fileline_fn, fileline_data, syminfo_fn,141syminfo_data, fileline_initialization_failed and everything the142data pointers point to. */143void *lock;144/* The function that returns file/line information. */145fileline fileline_fn;146/* The data to pass to FILELINE_FN. */147void *fileline_data;148/* The function that returns symbol information. */149syminfo syminfo_fn;150/* The data to pass to SYMINFO_FN. */151void *syminfo_data;152/* Whether initializing the file/line information failed. */153int fileline_initialization_failed;154/* The lock for the freelist. */155int lock_alloc;156/* The freelist when using mmap. */157struct backtrace_freelist_struct *freelist;158};159160/* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST161is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1162if the file does not exist. If the file does not exist and163DOES_NOT_EXIST is not NULL, the function will return -1 and will164not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is165NULL, the function will call ERROR_CALLBACK before returning. */166extern int backtrace_open (const char *filename,167backtrace_error_callback error_callback,168void *data,169int *does_not_exist);170171/* A view of the contents of a file. This supports mmap when172available. A view will remain in memory even after backtrace_close173is called on the file descriptor from which the view was174obtained. */175176struct backtrace_view177{178/* The data that the caller requested. */179const void *data;180/* The base of the view. */181void *base;182/* The total length of the view. */183size_t len;184};185186/* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the187result in *VIEW. Returns 1 on success, 0 on error. */188extern int backtrace_get_view (struct backtrace_state *state, int descriptor,189off_t offset, uint64_t size,190backtrace_error_callback error_callback,191void *data, struct backtrace_view *view);192193/* Release a view created by backtrace_get_view. */194extern void backtrace_release_view (struct backtrace_state *state,195struct backtrace_view *view,196backtrace_error_callback error_callback,197void *data);198199/* Close a file opened by backtrace_open. Returns 1 on success, 0 on200error. */201202extern int backtrace_close (int descriptor,203backtrace_error_callback error_callback,204void *data);205206/* Sort without using memory. */207208extern void backtrace_qsort (void *base, size_t count, size_t size,209int (*compar) (const void *, const void *));210211/* Allocate memory. This is like malloc. If ERROR_CALLBACK is NULL,212this does not report an error, it just returns NULL. */213214extern void *backtrace_alloc (struct backtrace_state *state, size_t size,215backtrace_error_callback error_callback,216void *data) ATTRIBUTE_MALLOC;217218/* Free memory allocated by backtrace_alloc. If ERROR_CALLBACK is219NULL, this does not report an error. */220221extern void backtrace_free (struct backtrace_state *state, void *mem,222size_t size,223backtrace_error_callback error_callback,224void *data);225226/* A growable vector of some struct. This is used for more efficient227allocation when we don't know the final size of some group of data228that we want to represent as an array. */229230struct backtrace_vector231{232/* The base of the vector. */233void *base;234/* The number of bytes in the vector. */235size_t size;236/* The number of bytes available at the current allocation. */237size_t alc;238};239240/* Grow VEC by SIZE bytes. Return a pointer to the newly allocated241bytes. Note that this may move the entire vector to a new memory242location. Returns NULL on failure. */243244extern void *backtrace_vector_grow (struct backtrace_state *state, size_t size,245backtrace_error_callback error_callback,246void *data,247struct backtrace_vector *vec);248249/* Finish the current allocation on VEC. Prepare to start a new250allocation. The finished allocation will never be freed. Returns251a pointer to the base of the finished entries, or NULL on252failure. */253254extern void* backtrace_vector_finish (struct backtrace_state *state,255struct backtrace_vector *vec,256backtrace_error_callback error_callback,257void *data);258259/* Release any extra space allocated for VEC. This may change260VEC->base. Returns 1 on success, 0 on failure. */261262extern int backtrace_vector_release (struct backtrace_state *state,263struct backtrace_vector *vec,264backtrace_error_callback error_callback,265void *data);266267/* Free the space managed by VEC. This will reset VEC. */268269static inline void270backtrace_vector_free (struct backtrace_state *state,271struct backtrace_vector *vec,272backtrace_error_callback error_callback, void *data)273{274vec->alc += vec->size;275vec->size = 0;276backtrace_vector_release (state, vec, error_callback, data);277}278279/* Read initial debug data from a descriptor, and set the280fileline_data, syminfo_fn, and syminfo_data fields of STATE.281Return the fileln_fn field in *FILELN_FN--this is done this way so282that the synchronization code is only implemented once. This is283called after the descriptor has first been opened. It will close284the descriptor if it is no longer needed. Returns 1 on success, 0285on error. There will be multiple implementations of this function,286for different file formats. Each system will compile the287appropriate one. */288289extern int backtrace_initialize (struct backtrace_state *state,290const char *filename,291int descriptor,292backtrace_error_callback error_callback,293void *data,294fileline *fileline_fn);295296/* An enum for the DWARF sections we care about. */297298enum dwarf_section299{300DEBUG_INFO,301DEBUG_LINE,302DEBUG_ABBREV,303DEBUG_RANGES,304DEBUG_STR,305DEBUG_ADDR,306DEBUG_STR_OFFSETS,307DEBUG_LINE_STR,308DEBUG_RNGLISTS,309310DEBUG_MAX311};312313/* Data for the DWARF sections we care about. */314315struct dwarf_sections316{317const unsigned char *data[DEBUG_MAX];318size_t size[DEBUG_MAX];319};320321/* DWARF data read from a file, used for .gnu_debugaltlink. */322323struct dwarf_data;324325/* Add file/line information for a DWARF module. */326327extern int backtrace_dwarf_add (struct backtrace_state *state,328uintptr_t base_address,329const struct dwarf_sections *dwarf_sections,330int is_bigendian,331struct dwarf_data *fileline_altlink,332backtrace_error_callback error_callback,333void *data, fileline *fileline_fn,334struct dwarf_data **fileline_entry);335336/* A data structure to pass to backtrace_syminfo_to_full. */337338struct backtrace_call_full339{340backtrace_full_callback full_callback;341backtrace_error_callback full_error_callback;342void *full_data;343int ret;344};345346/* A backtrace_syminfo_callback that can call into a347backtrace_full_callback, used when we have a symbol table but no348debug info. */349350extern void backtrace_syminfo_to_full_callback (void *data, uintptr_t pc,351const char *symname,352uintptr_t symval,353uintptr_t symsize);354355/* An error callback that corresponds to356backtrace_syminfo_to_full_callback. */357358extern void backtrace_syminfo_to_full_error_callback (void *, const char *,359int);360361/* A test-only hook for elf_uncompress_zdebug. */362363extern int backtrace_uncompress_zdebug (struct backtrace_state *,364const unsigned char *compressed,365size_t compressed_size,366backtrace_error_callback, void *data,367unsigned char **uncompressed,368size_t *uncompressed_size);369370/* A test-only hook for elf_uncompress_lzma. */371372extern int backtrace_uncompress_lzma (struct backtrace_state *,373const unsigned char *compressed,374size_t compressed_size,375backtrace_error_callback, void *data,376unsigned char **uncompressed,377size_t *uncompressed_size);378379#endif380381382