internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* internal.h -- Internal header file for stack backtrace library.
  2. Copyright (C) 2012-2015 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 HAVE_SYNC_FUNCTIONS
  48. /* Define out the sync functions. These should never be called if
  49. they are not available. */
  50. #define __sync_bool_compare_and_swap(A, B, C) (abort(), 1)
  51. #define __sync_lock_test_and_set(A, B) (abort(), 0)
  52. #define __sync_lock_release(A) abort()
  53. #endif /* !defined (HAVE_SYNC_FUNCTIONS) */
  54. #ifdef HAVE_ATOMIC_FUNCTIONS
  55. /* We have the atomic builtin functions. */
  56. #define backtrace_atomic_load_pointer(p) \
  57. __atomic_load_n ((p), __ATOMIC_ACQUIRE)
  58. #define backtrace_atomic_load_int(p) \
  59. __atomic_load_n ((p), __ATOMIC_ACQUIRE)
  60. #define backtrace_atomic_store_pointer(p, v) \
  61. __atomic_store_n ((p), (v), __ATOMIC_RELEASE)
  62. #define backtrace_atomic_store_size_t(p, v) \
  63. __atomic_store_n ((p), (v), __ATOMIC_RELEASE)
  64. #define backtrace_atomic_store_int(p, v) \
  65. __atomic_store_n ((p), (v), __ATOMIC_RELEASE)
  66. #else /* !defined (HAVE_ATOMIC_FUNCTIONS) */
  67. #ifdef HAVE_SYNC_FUNCTIONS
  68. /* We have the sync functions but not the atomic functions. Define
  69. the atomic ones in terms of the sync ones. */
  70. extern void *backtrace_atomic_load_pointer (void *);
  71. extern int backtrace_atomic_load_int (int *);
  72. extern void backtrace_atomic_store_pointer (void *, void *);
  73. extern void backtrace_atomic_store_size_t (size_t *, size_t);
  74. extern void backtrace_atomic_store_int (int *, int);
  75. #else /* !defined (HAVE_SYNC_FUNCTIONS) */
  76. /* We have neither the sync nor the atomic functions. These will
  77. never be called. */
  78. #define backtrace_atomic_load_pointer(p) (abort(), (void *) NULL)
  79. #define backtrace_atomic_load_int(p) (abort(), 0)
  80. #define backtrace_atomic_store_pointer(p, v) abort()
  81. #define backtrace_atomic_store_size_t(p, v) abort()
  82. #define backtrace_atomic_store_int(p, v) abort()
  83. #endif /* !defined (HAVE_SYNC_FUNCTIONS) */
  84. #endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */
  85. /* The type of the function that collects file/line information. This
  86. is like backtrace_pcinfo. */
  87. typedef int (*fileline) (struct backtrace_state *state, uintptr_t pc,
  88. backtrace_full_callback callback,
  89. backtrace_error_callback error_callback, void *data);
  90. /* The type of the function that collects symbol information. This is
  91. like backtrace_syminfo. */
  92. typedef void (*syminfo) (struct backtrace_state *state, uintptr_t pc,
  93. backtrace_syminfo_callback callback,
  94. backtrace_error_callback error_callback, void *data);
  95. /* What the backtrace state pointer points to. */
  96. struct backtrace_state
  97. {
  98. /* The name of the executable. */
  99. const char *filename;
  100. /* Non-zero if threaded. */
  101. int threaded;
  102. /* The master lock for fileline_fn, fileline_data, syminfo_fn,
  103. syminfo_data, fileline_initialization_failed and everything the
  104. data pointers point to. */
  105. void *lock;
  106. /* The function that returns file/line information. */
  107. fileline fileline_fn;
  108. /* The data to pass to FILELINE_FN. */
  109. void *fileline_data;
  110. /* The function that returns symbol information. */
  111. syminfo syminfo_fn;
  112. /* The data to pass to SYMINFO_FN. */
  113. void *syminfo_data;
  114. /* Whether initializing the file/line information failed. */
  115. int fileline_initialization_failed;
  116. /* The lock for the freelist. */
  117. int lock_alloc;
  118. /* The freelist when using mmap. */
  119. struct backtrace_freelist_struct *freelist;
  120. };
  121. /* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST
  122. is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1
  123. if the file does not exist. If the file does not exist and
  124. DOES_NOT_EXIST is not NULL, the function will return -1 and will
  125. not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is
  126. NULL, the function will call ERROR_CALLBACK before returning. */
  127. extern int backtrace_open (const char *filename,
  128. backtrace_error_callback error_callback,
  129. void *data,
  130. int *does_not_exist);
  131. /* A view of the contents of a file. This supports mmap when
  132. available. A view will remain in memory even after backtrace_close
  133. is called on the file descriptor from which the view was
  134. obtained. */
  135. struct backtrace_view
  136. {
  137. /* The data that the caller requested. */
  138. const void *data;
  139. /* The base of the view. */
  140. void *base;
  141. /* The total length of the view. */
  142. size_t len;
  143. };
  144. /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the
  145. result in *VIEW. Returns 1 on success, 0 on error. */
  146. extern int backtrace_get_view (struct backtrace_state *state, int descriptor,
  147. off_t offset, size_t size,
  148. backtrace_error_callback error_callback,
  149. void *data, struct backtrace_view *view);
  150. /* Release a view created by backtrace_get_view. */
  151. extern void backtrace_release_view (struct backtrace_state *state,
  152. struct backtrace_view *view,
  153. backtrace_error_callback error_callback,
  154. void *data);
  155. /* Close a file opened by backtrace_open. Returns 1 on success, 0 on
  156. error. */
  157. extern int backtrace_close (int descriptor,
  158. backtrace_error_callback error_callback,
  159. void *data);
  160. /* Sort without using memory. */
  161. extern void backtrace_qsort (void *base, size_t count, size_t size,
  162. int (*compar) (const void *, const void *));
  163. /* Allocate memory. This is like malloc. */
  164. extern void *backtrace_alloc (struct backtrace_state *state, size_t size,
  165. backtrace_error_callback error_callback,
  166. void *data) ATTRIBUTE_MALLOC;
  167. /* Free memory allocated by backtrace_alloc. */
  168. extern void backtrace_free (struct backtrace_state *state, void *mem,
  169. size_t size,
  170. backtrace_error_callback error_callback,
  171. void *data);
  172. /* A growable vector of some struct. This is used for more efficient
  173. allocation when we don't know the final size of some group of data
  174. that we want to represent as an array. */
  175. struct backtrace_vector
  176. {
  177. /* The base of the vector. */
  178. void *base;
  179. /* The number of bytes in the vector. */
  180. size_t size;
  181. /* The number of bytes available at the current allocation. */
  182. size_t alc;
  183. };
  184. /* Grow VEC by SIZE bytes. Return a pointer to the newly allocated
  185. bytes. Note that this may move the entire vector to a new memory
  186. location. Returns NULL on failure. */
  187. extern void *backtrace_vector_grow (struct backtrace_state *state, size_t size,
  188. backtrace_error_callback error_callback,
  189. void *data,
  190. struct backtrace_vector *vec);
  191. /* Finish the current allocation on VEC. Prepare to start a new
  192. allocation. The finished allocation will never be freed. Returns
  193. a pointer to the base of the finished entries, or NULL on
  194. failure. */
  195. extern void* backtrace_vector_finish (struct backtrace_state *state,
  196. struct backtrace_vector *vec,
  197. backtrace_error_callback error_callback,
  198. void *data);
  199. /* Release any extra space allocated for VEC. This may change
  200. VEC->base. Returns 1 on success, 0 on failure. */
  201. extern int backtrace_vector_release (struct backtrace_state *state,
  202. struct backtrace_vector *vec,
  203. backtrace_error_callback error_callback,
  204. void *data);
  205. /* Read initial debug data from a descriptor, and set the
  206. fileline_data, syminfo_fn, and syminfo_data fields of STATE.
  207. Return the fileln_fn field in *FILELN_FN--this is done this way so
  208. that the synchronization code is only implemented once. This is
  209. called after the descriptor has first been opened. It will close
  210. the descriptor if it is no longer needed. Returns 1 on success, 0
  211. on error. There will be multiple implementations of this function,
  212. for different file formats. Each system will compile the
  213. appropriate one. */
  214. extern int backtrace_initialize (struct backtrace_state *state,
  215. int descriptor,
  216. backtrace_error_callback error_callback,
  217. void *data,
  218. fileline *fileline_fn);
  219. /* Add file/line information for a DWARF module. */
  220. extern int backtrace_dwarf_add (struct backtrace_state *state,
  221. uintptr_t base_address,
  222. const unsigned char* dwarf_info,
  223. size_t dwarf_info_size,
  224. const unsigned char *dwarf_line,
  225. size_t dwarf_line_size,
  226. const unsigned char *dwarf_abbrev,
  227. size_t dwarf_abbrev_size,
  228. const unsigned char *dwarf_ranges,
  229. size_t dwarf_range_size,
  230. const unsigned char *dwarf_str,
  231. size_t dwarf_str_size,
  232. int is_bigendian,
  233. backtrace_error_callback error_callback,
  234. void *data, fileline *fileline_fn);
  235. #endif