1 /* internal.h -- Internal header file for stack backtrace library.
2 Copyright (C) 2012-2024 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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
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.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 #ifndef BACKTRACE_INTERNAL_H
34 #define BACKTRACE_INTERNAL_H
36 /* We assume that <sys/types.h> and "backtrace.h" have already been
40 # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
43 #if (GCC_VERSION < 2007)
44 # define __attribute__(x)
47 #ifndef ATTRIBUTE_UNUSED
48 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
51 #ifndef ATTRIBUTE_MALLOC
52 # if (GCC_VERSION >= 2096)
53 # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
55 # define ATTRIBUTE_MALLOC
59 #ifdef __has_attribute
60 # if __has_attribute(fallthrough)
61 # define ATTRIBUTE_FALLTHROUGH __attribute__ ((fallthrough))
64 #ifndef ATTRIBUTE_FALLTHROUGH
65 # if (GCC_VERSION >= 7000)
66 # define ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__))
68 # define ATTRIBUTE_FALLTHROUGH
72 #ifndef HAVE_SYNC_FUNCTIONS
74 /* Define out the sync functions. These should never be called if
75 they are not available. */
77 #define __sync_bool_compare_and_swap(A, B, C) (abort(), 1)
78 #define __sync_lock_test_and_set(A, B) (abort(), 0)
79 #define __sync_lock_release(A) abort()
81 #endif /* !defined (HAVE_SYNC_FUNCTIONS) */
83 #ifdef HAVE_ATOMIC_FUNCTIONS
85 /* We have the atomic builtin functions. */
87 #define backtrace_atomic_load_pointer(p) \
88 __atomic_load_n ((p), __ATOMIC_ACQUIRE)
89 #define backtrace_atomic_load_int(p) \
90 __atomic_load_n ((p), __ATOMIC_ACQUIRE)
91 #define backtrace_atomic_store_pointer(p, v) \
92 __atomic_store_n ((p), (v), __ATOMIC_RELEASE)
93 #define backtrace_atomic_store_size_t(p, v) \
94 __atomic_store_n ((p), (v), __ATOMIC_RELEASE)
95 #define backtrace_atomic_store_int(p, v) \
96 __atomic_store_n ((p), (v), __ATOMIC_RELEASE)
98 #else /* !defined (HAVE_ATOMIC_FUNCTIONS) */
99 #ifdef HAVE_SYNC_FUNCTIONS
101 /* We have the sync functions but not the atomic functions. Define
102 the atomic ones in terms of the sync ones. */
104 extern void *backtrace_atomic_load_pointer (void *);
105 extern int backtrace_atomic_load_int (int *);
106 extern void backtrace_atomic_store_pointer (void *, void *);
107 extern void backtrace_atomic_store_size_t (size_t *, size_t);
108 extern void backtrace_atomic_store_int (int *, int);
110 #else /* !defined (HAVE_SYNC_FUNCTIONS) */
112 /* We have neither the sync nor the atomic functions. These will
115 #define backtrace_atomic_load_pointer(p) (abort(), (void *) NULL)
116 #define backtrace_atomic_load_int(p) (abort(), 0)
117 #define backtrace_atomic_store_pointer(p, v) abort()
118 #define backtrace_atomic_store_size_t(p, v) abort()
119 #define backtrace_atomic_store_int(p, v) abort()
121 #endif /* !defined (HAVE_SYNC_FUNCTIONS) */
122 #endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */
124 /* The type of the function that collects file/line information. This
125 is like backtrace_pcinfo. */
127 typedef int (*fileline
) (struct backtrace_state
*state
, uintptr_t pc
,
128 backtrace_full_callback callback
,
129 backtrace_error_callback error_callback
, void *data
);
131 /* The type of the function that collects symbol information. This is
132 like backtrace_syminfo. */
134 typedef void (*syminfo
) (struct backtrace_state
*state
, uintptr_t pc
,
135 backtrace_syminfo_callback callback
,
136 backtrace_error_callback error_callback
, void *data
);
138 /* What the backtrace state pointer points to. */
140 struct backtrace_state
142 /* The name of the executable. */
143 const char *filename
;
144 /* Non-zero if threaded. */
146 /* The master lock for fileline_fn, fileline_data, syminfo_fn,
147 syminfo_data, fileline_initialization_failed and everything the
148 data pointers point to. */
150 /* The function that returns file/line information. */
151 fileline fileline_fn
;
152 /* The data to pass to FILELINE_FN. */
154 /* The function that returns symbol information. */
156 /* The data to pass to SYMINFO_FN. */
158 /* Whether initializing the file/line information failed. */
159 int fileline_initialization_failed
;
160 /* The lock for the freelist. */
162 /* The freelist when using mmap. */
163 struct backtrace_freelist_struct
*freelist
;
166 /* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST
167 is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1
168 if the file does not exist. If the file does not exist and
169 DOES_NOT_EXIST is not NULL, the function will return -1 and will
170 not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is
171 NULL, the function will call ERROR_CALLBACK before returning. */
172 extern int backtrace_open (const char *filename
,
173 backtrace_error_callback error_callback
,
175 int *does_not_exist
);
177 /* A view of the contents of a file. This supports mmap when
178 available. A view will remain in memory even after backtrace_close
179 is called on the file descriptor from which the view was
182 struct backtrace_view
184 /* The data that the caller requested. */
186 /* The base of the view. */
188 /* The total length of the view. */
192 /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the
193 result in *VIEW. Returns 1 on success, 0 on error. */
194 extern int backtrace_get_view (struct backtrace_state
*state
, int descriptor
,
195 off_t offset
, uint64_t size
,
196 backtrace_error_callback error_callback
,
197 void *data
, struct backtrace_view
*view
);
199 /* Release a view created by backtrace_get_view. */
200 extern void backtrace_release_view (struct backtrace_state
*state
,
201 struct backtrace_view
*view
,
202 backtrace_error_callback error_callback
,
205 /* Close a file opened by backtrace_open. Returns 1 on success, 0 on
208 extern int backtrace_close (int descriptor
,
209 backtrace_error_callback error_callback
,
212 /* Sort without using memory. */
214 extern void backtrace_qsort (void *base
, size_t count
, size_t size
,
215 int (*compar
) (const void *, const void *));
217 /* Allocate memory. This is like malloc. If ERROR_CALLBACK is NULL,
218 this does not report an error, it just returns NULL. */
220 extern void *backtrace_alloc (struct backtrace_state
*state
, size_t size
,
221 backtrace_error_callback error_callback
,
222 void *data
) ATTRIBUTE_MALLOC
;
224 /* Free memory allocated by backtrace_alloc. If ERROR_CALLBACK is
225 NULL, this does not report an error. */
227 extern void backtrace_free (struct backtrace_state
*state
, void *mem
,
229 backtrace_error_callback error_callback
,
232 /* A growable vector of some struct. This is used for more efficient
233 allocation when we don't know the final size of some group of data
234 that we want to represent as an array. */
236 struct backtrace_vector
238 /* The base of the vector. */
240 /* The number of bytes in the vector. */
242 /* The number of bytes available at the current allocation. */
246 /* Grow VEC by SIZE bytes. Return a pointer to the newly allocated
247 bytes. Note that this may move the entire vector to a new memory
248 location. Returns NULL on failure. */
250 extern void *backtrace_vector_grow (struct backtrace_state
*state
, size_t size
,
251 backtrace_error_callback error_callback
,
253 struct backtrace_vector
*vec
);
255 /* Finish the current allocation on VEC. Prepare to start a new
256 allocation. The finished allocation will never be freed. Returns
257 a pointer to the base of the finished entries, or NULL on
260 extern void* backtrace_vector_finish (struct backtrace_state
*state
,
261 struct backtrace_vector
*vec
,
262 backtrace_error_callback error_callback
,
265 /* Release any extra space allocated for VEC. This may change
266 VEC->base. Returns 1 on success, 0 on failure. */
268 extern int backtrace_vector_release (struct backtrace_state
*state
,
269 struct backtrace_vector
*vec
,
270 backtrace_error_callback error_callback
,
273 /* Free the space managed by VEC. This will reset VEC. */
276 backtrace_vector_free (struct backtrace_state
*state
,
277 struct backtrace_vector
*vec
,
278 backtrace_error_callback error_callback
, void *data
)
280 vec
->alc
+= vec
->size
;
282 backtrace_vector_release (state
, vec
, error_callback
, data
);
285 /* Read initial debug data from a descriptor, and set the
286 fileline_data, syminfo_fn, and syminfo_data fields of STATE.
287 Return the fileln_fn field in *FILELN_FN--this is done this way so
288 that the synchronization code is only implemented once. This is
289 called after the descriptor has first been opened. It will close
290 the descriptor if it is no longer needed. Returns 1 on success, 0
291 on error. There will be multiple implementations of this function,
292 for different file formats. Each system will compile the
295 extern int backtrace_initialize (struct backtrace_state
*state
,
296 const char *filename
,
298 backtrace_error_callback error_callback
,
300 fileline
*fileline_fn
);
302 /* An enum for the DWARF sections we care about. */
319 /* Data for the DWARF sections we care about. */
321 struct dwarf_sections
323 const unsigned char *data
[DEBUG_MAX
];
324 size_t size
[DEBUG_MAX
];
327 /* DWARF data read from a file, used for .gnu_debugaltlink. */
331 /* The load address mapping. */
333 #if defined(__FDPIC__) && defined(HAVE_DL_ITERATE_PHDR) && (defined(HAVE_LINK_H) || defined(HAVE_SYS_LINK_H))
338 #ifdef HAVE_SYS_LINK_H
339 #include <sys/link.h>
342 #define libbacktrace_using_fdpic() (1)
344 struct libbacktrace_base_address
346 struct elf32_fdpic_loadaddr m
;
349 #define libbacktrace_add_base(pc, base) \
350 ((uintptr_t) (__RELOC_POINTER ((pc), (base).m)))
352 #else /* not _FDPIC__ */
354 #define libbacktrace_using_fdpic() (0)
356 struct libbacktrace_base_address
361 #define libbacktrace_add_base(pc, base) ((pc) + (base).m)
363 #endif /* not _FDPIC__ */
365 /* Add file/line information for a DWARF module. */
367 extern int backtrace_dwarf_add (struct backtrace_state
*state
,
368 struct libbacktrace_base_address base_address
,
369 const struct dwarf_sections
*dwarf_sections
,
371 struct dwarf_data
*fileline_altlink
,
372 backtrace_error_callback error_callback
,
373 void *data
, fileline
*fileline_fn
,
374 struct dwarf_data
**fileline_entry
);
376 /* A data structure to pass to backtrace_syminfo_to_full. */
378 struct backtrace_call_full
380 backtrace_full_callback full_callback
;
381 backtrace_error_callback full_error_callback
;
386 /* A backtrace_syminfo_callback that can call into a
387 backtrace_full_callback, used when we have a symbol table but no
390 extern void backtrace_syminfo_to_full_callback (void *data
, uintptr_t pc
,
395 /* An error callback that corresponds to
396 backtrace_syminfo_to_full_callback. */
398 extern void backtrace_syminfo_to_full_error_callback (void *, const char *,
401 /* A test-only hook for elf_uncompress_zdebug. */
403 extern int backtrace_uncompress_zdebug (struct backtrace_state
*,
404 const unsigned char *compressed
,
405 size_t compressed_size
,
406 backtrace_error_callback
, void *data
,
407 unsigned char **uncompressed
,
408 size_t *uncompressed_size
);
410 /* A test-only hook for elf_zstd_decompress. */
412 extern int backtrace_uncompress_zstd (struct backtrace_state
*,
413 const unsigned char *compressed
,
414 size_t compressed_size
,
415 backtrace_error_callback
, void *data
,
416 unsigned char *uncompressed
,
417 size_t uncompressed_size
);
419 /* A test-only hook for elf_uncompress_lzma. */
421 extern int backtrace_uncompress_lzma (struct backtrace_state
*,
422 const unsigned char *compressed
,
423 size_t compressed_size
,
424 backtrace_error_callback
, void *data
,
425 unsigned char **uncompressed
,
426 size_t *uncompressed_size
);