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.
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 #ifndef ATTRIBUTE_FALLTHROUGH
60 # if (GCC_VERSION >= 7000)
61 # define ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__))
63 # define ATTRIBUTE_FALLTHROUGH
67 #ifndef HAVE_SYNC_FUNCTIONS
69 /* Define out the sync functions. These should never be called if
70 they are not available. */
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()
76 #endif /* !defined (HAVE_SYNC_FUNCTIONS) */
78 #ifdef HAVE_ATOMIC_FUNCTIONS
80 /* We have the atomic builtin functions. */
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)
93 #else /* !defined (HAVE_ATOMIC_FUNCTIONS) */
94 #ifdef HAVE_SYNC_FUNCTIONS
96 /* We have the sync functions but not the atomic functions. Define
97 the atomic ones in terms of the sync ones. */
99 extern void *backtrace_atomic_load_pointer (void *);
100 extern int backtrace_atomic_load_int (int *);
101 extern void backtrace_atomic_store_pointer (void *, void *);
102 extern void backtrace_atomic_store_size_t (size_t *, size_t);
103 extern void backtrace_atomic_store_int (int *, int);
105 #else /* !defined (HAVE_SYNC_FUNCTIONS) */
107 /* We have neither the sync nor the atomic functions. These will
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()
116 #endif /* !defined (HAVE_SYNC_FUNCTIONS) */
117 #endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */
119 /* The type of the function that collects file/line information. This
120 is like backtrace_pcinfo. */
122 typedef int (*fileline
) (struct backtrace_state
*state
, uintptr_t pc
,
123 backtrace_full_callback callback
,
124 backtrace_error_callback error_callback
, void *data
);
126 /* The type of the function that collects symbol information. This is
127 like backtrace_syminfo. */
129 typedef void (*syminfo
) (struct backtrace_state
*state
, uintptr_t pc
,
130 backtrace_syminfo_callback callback
,
131 backtrace_error_callback error_callback
, void *data
);
133 /* What the backtrace state pointer points to. */
135 struct backtrace_state
137 /* The name of the executable. */
138 const char *filename
;
139 /* Non-zero if threaded. */
141 /* The master lock for fileline_fn, fileline_data, syminfo_fn,
142 syminfo_data, fileline_initialization_failed and everything the
143 data pointers point to. */
145 /* The function that returns file/line information. */
146 fileline fileline_fn
;
147 /* The data to pass to FILELINE_FN. */
149 /* The function that returns symbol information. */
151 /* The data to pass to SYMINFO_FN. */
153 /* Whether initializing the file/line information failed. */
154 int fileline_initialization_failed
;
155 /* The lock for the freelist. */
157 /* The freelist when using mmap. */
158 struct backtrace_freelist_struct
*freelist
;
161 /* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST
162 is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1
163 if the file does not exist. If the file does not exist and
164 DOES_NOT_EXIST is not NULL, the function will return -1 and will
165 not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is
166 NULL, the function will call ERROR_CALLBACK before returning. */
167 extern int backtrace_open (const char *filename
,
168 backtrace_error_callback error_callback
,
170 int *does_not_exist
);
172 /* A view of the contents of a file. This supports mmap when
173 available. A view will remain in memory even after backtrace_close
174 is called on the file descriptor from which the view was
177 struct backtrace_view
179 /* The data that the caller requested. */
181 /* The base of the view. */
183 /* The total length of the view. */
187 /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the
188 result in *VIEW. Returns 1 on success, 0 on error. */
189 extern int backtrace_get_view (struct backtrace_state
*state
, int descriptor
,
190 off_t offset
, uint64_t size
,
191 backtrace_error_callback error_callback
,
192 void *data
, struct backtrace_view
*view
);
194 /* Release a view created by backtrace_get_view. */
195 extern void backtrace_release_view (struct backtrace_state
*state
,
196 struct backtrace_view
*view
,
197 backtrace_error_callback error_callback
,
200 /* Close a file opened by backtrace_open. Returns 1 on success, 0 on
203 extern int backtrace_close (int descriptor
,
204 backtrace_error_callback error_callback
,
207 /* Sort without using memory. */
209 extern void backtrace_qsort (void *base
, size_t count
, size_t size
,
210 int (*compar
) (const void *, const void *));
212 /* Allocate memory. This is like malloc. If ERROR_CALLBACK is NULL,
213 this does not report an error, it just returns NULL. */
215 extern void *backtrace_alloc (struct backtrace_state
*state
, size_t size
,
216 backtrace_error_callback error_callback
,
217 void *data
) ATTRIBUTE_MALLOC
;
219 /* Free memory allocated by backtrace_alloc. If ERROR_CALLBACK is
220 NULL, this does not report an error. */
222 extern void backtrace_free (struct backtrace_state
*state
, void *mem
,
224 backtrace_error_callback error_callback
,
227 /* A growable vector of some struct. This is used for more efficient
228 allocation when we don't know the final size of some group of data
229 that we want to represent as an array. */
231 struct backtrace_vector
233 /* The base of the vector. */
235 /* The number of bytes in the vector. */
237 /* The number of bytes available at the current allocation. */
241 /* Grow VEC by SIZE bytes. Return a pointer to the newly allocated
242 bytes. Note that this may move the entire vector to a new memory
243 location. Returns NULL on failure. */
245 extern void *backtrace_vector_grow (struct backtrace_state
*state
, size_t size
,
246 backtrace_error_callback error_callback
,
248 struct backtrace_vector
*vec
);
250 /* Finish the current allocation on VEC. Prepare to start a new
251 allocation. The finished allocation will never be freed. Returns
252 a pointer to the base of the finished entries, or NULL on
255 extern void* backtrace_vector_finish (struct backtrace_state
*state
,
256 struct backtrace_vector
*vec
,
257 backtrace_error_callback error_callback
,
260 /* Release any extra space allocated for VEC. This may change
261 VEC->base. Returns 1 on success, 0 on failure. */
263 extern int backtrace_vector_release (struct backtrace_state
*state
,
264 struct backtrace_vector
*vec
,
265 backtrace_error_callback error_callback
,
268 /* Free the space managed by VEC. This will reset VEC. */
271 backtrace_vector_free (struct backtrace_state
*state
,
272 struct backtrace_vector
*vec
,
273 backtrace_error_callback error_callback
, void *data
)
275 vec
->alc
+= vec
->size
;
277 backtrace_vector_release (state
, vec
, error_callback
, data
);
280 /* Read initial debug data from a descriptor, and set the
281 fileline_data, syminfo_fn, and syminfo_data fields of STATE.
282 Return the fileln_fn field in *FILELN_FN--this is done this way so
283 that the synchronization code is only implemented once. This is
284 called after the descriptor has first been opened. It will close
285 the descriptor if it is no longer needed. Returns 1 on success, 0
286 on error. There will be multiple implementations of this function,
287 for different file formats. Each system will compile the
290 extern int backtrace_initialize (struct backtrace_state
*state
,
291 const char *filename
,
293 backtrace_error_callback error_callback
,
295 fileline
*fileline_fn
);
297 /* An enum for the DWARF sections we care about. */
314 /* Data for the DWARF sections we care about. */
316 struct dwarf_sections
318 const unsigned char *data
[DEBUG_MAX
];
319 size_t size
[DEBUG_MAX
];
322 /* DWARF data read from a file, used for .gnu_debugaltlink. */
326 /* Add file/line information for a DWARF module. */
328 extern int backtrace_dwarf_add (struct backtrace_state
*state
,
329 uintptr_t base_address
,
330 const struct dwarf_sections
*dwarf_sections
,
332 struct dwarf_data
*fileline_altlink
,
333 backtrace_error_callback error_callback
,
334 void *data
, fileline
*fileline_fn
,
335 struct dwarf_data
**fileline_entry
);
337 /* A data structure to pass to backtrace_syminfo_to_full. */
339 struct backtrace_call_full
341 backtrace_full_callback full_callback
;
342 backtrace_error_callback full_error_callback
;
347 /* A backtrace_syminfo_callback that can call into a
348 backtrace_full_callback, used when we have a symbol table but no
351 extern void backtrace_syminfo_to_full_callback (void *data
, uintptr_t pc
,
356 /* An error callback that corresponds to
357 backtrace_syminfo_to_full_callback. */
359 extern void backtrace_syminfo_to_full_error_callback (void *, const char *,
362 /* A test-only hook for elf_uncompress_zdebug. */
364 extern int backtrace_uncompress_zdebug (struct backtrace_state
*,
365 const unsigned char *compressed
,
366 size_t compressed_size
,
367 backtrace_error_callback
, void *data
,
368 unsigned char **uncompressed
,
369 size_t *uncompressed_size
);
371 /* A test-only hook for elf_uncompress_lzma. */
373 extern int backtrace_uncompress_lzma (struct backtrace_state
*,
374 const unsigned char *compressed
,
375 size_t compressed_size
,
376 backtrace_error_callback
, void *data
,
377 unsigned char **uncompressed
,
378 size_t *uncompressed_size
);