The open source OpenXR runtime
at main 394 lines 13 kB view raw
1/* internal.h -- Internal 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_INTERNAL_H 34#define BACKTRACE_INTERNAL_H 35 36/* We assume that <sys/types.h> and "backtrace.h" have already been 37 included. */ 38 39#ifndef GCC_VERSION 40# define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__) 41#endif 42 43#if (GCC_VERSION < 2007) 44# define __attribute__(x) 45#endif 46 47#ifndef ATTRIBUTE_UNUSED 48# define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) 49#endif 50 51#ifndef ATTRIBUTE_MALLOC 52# if (GCC_VERSION >= 2096) 53# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) 54# else 55# define ATTRIBUTE_MALLOC 56# endif 57#endif 58 59#ifndef ATTRIBUTE_FALLTHROUGH 60# if (GCC_VERSION >= 7000) 61# define ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__)) 62# else 63# define ATTRIBUTE_FALLTHROUGH 64# endif 65#endif 66 67#ifndef HAVE_SYNC_FUNCTIONS 68 69/* Define out the sync functions. These should never be called if 70 they are not available. */ 71 72#define __sync_bool_compare_and_swap(A, B, C) (abort(), 1) 73#define __sync_lock_test_and_set(A, B) (abort(), 0) 74#define __sync_lock_release(A) abort() 75 76#endif /* !defined (HAVE_SYNC_FUNCTIONS) */ 77 78#ifdef HAVE_ATOMIC_FUNCTIONS 79 80/* We have the atomic builtin functions. */ 81 82#define backtrace_atomic_load_pointer(p) \ 83 __atomic_load_n ((p), __ATOMIC_ACQUIRE) 84#define backtrace_atomic_load_int(p) \ 85 __atomic_load_n ((p), __ATOMIC_ACQUIRE) 86#define backtrace_atomic_store_pointer(p, v) \ 87 __atomic_store_n ((p), (v), __ATOMIC_RELEASE) 88#define backtrace_atomic_store_size_t(p, v) \ 89 __atomic_store_n ((p), (v), __ATOMIC_RELEASE) 90#define backtrace_atomic_store_int(p, v) \ 91 __atomic_store_n ((p), (v), __ATOMIC_RELEASE) 92 93#else /* !defined (HAVE_ATOMIC_FUNCTIONS) */ 94#ifdef HAVE_SYNC_FUNCTIONS 95 96/* We have the sync functions but not the atomic functions. Define 97 the atomic ones in terms of the sync ones. */ 98 99extern void *backtrace_atomic_load_pointer (void *); 100extern int backtrace_atomic_load_int (int *); 101extern void backtrace_atomic_store_pointer (void *, void *); 102extern void backtrace_atomic_store_size_t (size_t *, size_t); 103extern void backtrace_atomic_store_int (int *, int); 104 105#else /* !defined (HAVE_SYNC_FUNCTIONS) */ 106 107/* We have neither the sync nor the atomic functions. These will 108 never be called. */ 109 110#define backtrace_atomic_load_pointer(p) (abort(), (void *) NULL) 111#define backtrace_atomic_load_int(p) (abort(), 0) 112#define backtrace_atomic_store_pointer(p, v) abort() 113#define backtrace_atomic_store_size_t(p, v) abort() 114#define backtrace_atomic_store_int(p, v) abort() 115 116#endif /* !defined (HAVE_SYNC_FUNCTIONS) */ 117#endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */ 118 119namespace tracy 120{ 121 122/* The type of the function that collects file/line information. This 123 is like backtrace_pcinfo. */ 124 125typedef int (*fileline) (struct backtrace_state *state, uintptr_t pc, 126 backtrace_full_callback callback, 127 backtrace_error_callback error_callback, void *data); 128 129/* The type of the function that collects symbol information. This is 130 like backtrace_syminfo. */ 131 132typedef void (*syminfo) (struct backtrace_state *state, uintptr_t pc, 133 backtrace_syminfo_callback callback, 134 backtrace_error_callback error_callback, void *data); 135 136/* What the backtrace state pointer points to. */ 137 138struct backtrace_state 139{ 140 /* The name of the executable. */ 141 const char *filename; 142 /* Non-zero if threaded. */ 143 int threaded; 144 /* The master lock for fileline_fn, fileline_data, syminfo_fn, 145 syminfo_data, fileline_initialization_failed and everything the 146 data pointers point to. */ 147 void *lock; 148 /* The function that returns file/line information. */ 149 fileline fileline_fn; 150 /* The data to pass to FILELINE_FN. */ 151 void *fileline_data; 152 /* The function that returns symbol information. */ 153 syminfo syminfo_fn; 154 /* The data to pass to SYMINFO_FN. */ 155 void *syminfo_data; 156 /* Whether initializing the file/line information failed. */ 157 int fileline_initialization_failed; 158 /* The lock for the freelist. */ 159 int lock_alloc; 160 /* The freelist when using mmap. */ 161 struct backtrace_freelist_struct *freelist; 162}; 163 164/* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST 165 is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1 166 if the file does not exist. If the file does not exist and 167 DOES_NOT_EXIST is not NULL, the function will return -1 and will 168 not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is 169 NULL, the function will call ERROR_CALLBACK before returning. */ 170extern int backtrace_open (const char *filename, 171 backtrace_error_callback error_callback, 172 void *data, 173 int *does_not_exist); 174 175/* A view of the contents of a file. This supports mmap when 176 available. A view will remain in memory even after backtrace_close 177 is called on the file descriptor from which the view was 178 obtained. */ 179 180struct backtrace_view 181{ 182 /* The data that the caller requested. */ 183 const void *data; 184 /* The base of the view. */ 185 void *base; 186 /* The total length of the view. */ 187 size_t len; 188}; 189 190/* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the 191 result in *VIEW. Returns 1 on success, 0 on error. */ 192extern int backtrace_get_view (struct backtrace_state *state, int descriptor, 193 off_t offset, uint64_t size, 194 backtrace_error_callback error_callback, 195 void *data, struct backtrace_view *view); 196 197/* Release a view created by backtrace_get_view. */ 198extern void backtrace_release_view (struct backtrace_state *state, 199 struct backtrace_view *view, 200 backtrace_error_callback error_callback, 201 void *data); 202 203/* Close a file opened by backtrace_open. Returns 1 on success, 0 on 204 error. */ 205 206extern int backtrace_close (int descriptor, 207 backtrace_error_callback error_callback, 208 void *data); 209 210/* Sort without using memory. */ 211 212extern void backtrace_qsort (void *base, size_t count, size_t size, 213 int (*compar) (const void *, const void *)); 214 215/* Allocate memory. This is like malloc. If ERROR_CALLBACK is NULL, 216 this does not report an error, it just returns NULL. */ 217 218extern void *backtrace_alloc (struct backtrace_state *state, size_t size, 219 backtrace_error_callback error_callback, 220 void *data) ATTRIBUTE_MALLOC; 221 222/* Free memory allocated by backtrace_alloc. If ERROR_CALLBACK is 223 NULL, this does not report an error. */ 224 225extern void backtrace_free (struct backtrace_state *state, void *mem, 226 size_t size, 227 backtrace_error_callback error_callback, 228 void *data); 229 230/* A growable vector of some struct. This is used for more efficient 231 allocation when we don't know the final size of some group of data 232 that we want to represent as an array. */ 233 234struct backtrace_vector 235{ 236 /* The base of the vector. */ 237 void *base; 238 /* The number of bytes in the vector. */ 239 size_t size; 240 /* The number of bytes available at the current allocation. */ 241 size_t alc; 242}; 243 244/* Grow VEC by SIZE bytes. Return a pointer to the newly allocated 245 bytes. Note that this may move the entire vector to a new memory 246 location. Returns NULL on failure. */ 247 248extern void *backtrace_vector_grow (struct backtrace_state *state, size_t size, 249 backtrace_error_callback error_callback, 250 void *data, 251 struct backtrace_vector *vec); 252 253/* Finish the current allocation on VEC. Prepare to start a new 254 allocation. The finished allocation will never be freed. Returns 255 a pointer to the base of the finished entries, or NULL on 256 failure. */ 257 258extern void* backtrace_vector_finish (struct backtrace_state *state, 259 struct backtrace_vector *vec, 260 backtrace_error_callback error_callback, 261 void *data); 262 263/* Release any extra space allocated for VEC. This may change 264 VEC->base. Returns 1 on success, 0 on failure. */ 265 266extern int backtrace_vector_release (struct backtrace_state *state, 267 struct backtrace_vector *vec, 268 backtrace_error_callback error_callback, 269 void *data); 270 271/* Free the space managed by VEC. This will reset VEC. */ 272 273static inline void 274backtrace_vector_free (struct backtrace_state *state, 275 struct backtrace_vector *vec, 276 backtrace_error_callback error_callback, void *data) 277{ 278 vec->alc += vec->size; 279 vec->size = 0; 280 backtrace_vector_release (state, vec, error_callback, data); 281} 282 283/* Read initial debug data from a descriptor, and set the 284 fileline_data, syminfo_fn, and syminfo_data fields of STATE. 285 Return the fileln_fn field in *FILELN_FN--this is done this way so 286 that the synchronization code is only implemented once. This is 287 called after the descriptor has first been opened. It will close 288 the descriptor if it is no longer needed. Returns 1 on success, 0 289 on error. There will be multiple implementations of this function, 290 for different file formats. Each system will compile the 291 appropriate one. */ 292 293extern int backtrace_initialize (struct backtrace_state *state, 294 const char *filename, 295 int descriptor, 296 backtrace_error_callback error_callback, 297 void *data, 298 fileline *fileline_fn); 299 300/* An enum for the DWARF sections we care about. */ 301 302enum dwarf_section 303{ 304 DEBUG_INFO, 305 DEBUG_LINE, 306 DEBUG_ABBREV, 307 DEBUG_RANGES, 308 DEBUG_STR, 309 DEBUG_ADDR, 310 DEBUG_STR_OFFSETS, 311 DEBUG_LINE_STR, 312 DEBUG_RNGLISTS, 313 314 DEBUG_MAX 315}; 316 317/* Data for the DWARF sections we care about. */ 318 319struct dwarf_sections 320{ 321 const unsigned char *data[DEBUG_MAX]; 322 size_t size[DEBUG_MAX]; 323}; 324 325/* DWARF data read from a file, used for .gnu_debugaltlink. */ 326 327struct dwarf_data; 328 329/* Add file/line information for a DWARF module. */ 330 331extern int backtrace_dwarf_add (struct backtrace_state *state, 332 uintptr_t base_address, 333 const struct dwarf_sections *dwarf_sections, 334 int is_bigendian, 335 struct dwarf_data *fileline_altlink, 336 backtrace_error_callback error_callback, 337 void *data, fileline *fileline_fn, 338 struct dwarf_data **fileline_entry); 339 340/* A data structure to pass to backtrace_syminfo_to_full. */ 341 342struct backtrace_call_full 343{ 344 backtrace_full_callback full_callback; 345 backtrace_error_callback full_error_callback; 346 void *full_data; 347 int ret; 348}; 349 350/* A backtrace_syminfo_callback that can call into a 351 backtrace_full_callback, used when we have a symbol table but no 352 debug info. */ 353 354extern void backtrace_syminfo_to_full_callback (void *data, uintptr_t pc, 355 const char *symname, 356 uintptr_t symval, 357 uintptr_t symsize); 358 359/* An error callback that corresponds to 360 backtrace_syminfo_to_full_callback. */ 361 362extern void backtrace_syminfo_to_full_error_callback (void *, const char *, 363 int); 364 365/* A test-only hook for elf_uncompress_zdebug. */ 366 367extern int backtrace_uncompress_zdebug (struct backtrace_state *, 368 const unsigned char *compressed, 369 size_t compressed_size, 370 backtrace_error_callback, void *data, 371 unsigned char **uncompressed, 372 size_t *uncompressed_size); 373 374/* A test-only hook for elf_zstd_decompress. */ 375 376extern int backtrace_uncompress_zstd (struct backtrace_state *, 377 const unsigned char *compressed, 378 size_t compressed_size, 379 backtrace_error_callback, void *data, 380 unsigned char *uncompressed, 381 size_t uncompressed_size); 382 383/* A test-only hook for elf_uncompress_lzma. */ 384 385extern int backtrace_uncompress_lzma (struct backtrace_state *, 386 const unsigned char *compressed, 387 size_t compressed_size, 388 backtrace_error_callback, void *data, 389 unsigned char **uncompressed, 390 size_t *uncompressed_size); 391 392} 393 394#endif