internal.h 12 KB

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