1 /* internal.h -- Internal header file for stack backtrace library.
2 Copyright (C) 2012-2013 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 HAVE_SYNC_FUNCTIONS
61 /* Define out the sync functions. These should never be called if
62 they are not available. */
64 #define __sync_bool_compare_and_swap(A, B, C) (abort(), 1)
65 #define __sync_lock_test_and_set(A, B) (abort(), 0)
66 #define __sync_lock_release(A) abort()
68 #endif /* !defined(HAVE_SYNC_FUNCTIONS) */
70 /* The type of the function that collects file/line information. This
71 is like backtrace_pcinfo. */
73 typedef int (*fileline
) (struct backtrace_state
*state
, uintptr_t pc
,
74 backtrace_full_callback callback
,
75 backtrace_error_callback error_callback
, void *data
);
77 /* The type of the function that collects symbol information. This is
78 like backtrace_syminfo. */
80 typedef void (*syminfo
) (struct backtrace_state
*state
, uintptr_t pc
,
81 backtrace_syminfo_callback callback
,
82 backtrace_error_callback error_callback
, void *data
);
84 /* What the backtrace state pointer points to. */
86 struct backtrace_state
88 /* The name of the executable. */
90 /* Non-zero if threaded. */
92 /* The master lock for fileline_fn, fileline_data, syminfo_fn,
93 syminfo_data, fileline_initialization_failed and everything the
94 data pointers point to. */
96 /* The function that returns file/line information. */
98 /* The data to pass to FILELINE_FN. */
100 /* The function that returns symbol information. */
102 /* The data to pass to SYMINFO_FN. */
104 /* Whether initializing the file/line information failed. */
105 int fileline_initialization_failed
;
106 /* The lock for the freelist. */
108 /* The freelist when using mmap. */
109 struct backtrace_freelist_struct
*freelist
;
112 /* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST
113 is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1
114 if the file does not exist. If the file does not exist and
115 DOES_NOT_EXIST is not NULL, the function will return -1 and will
116 not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is
117 NULL, the function will call ERROR_CALLBACK before returning. */
118 extern int backtrace_open (const char *filename
,
119 backtrace_error_callback error_callback
,
121 int *does_not_exist
);
123 /* A view of the contents of a file. This supports mmap when
124 available. A view will remain in memory even after backtrace_close
125 is called on the file descriptor from which the view was
128 struct backtrace_view
130 /* The data that the caller requested. */
132 /* The base of the view. */
134 /* The total length of the view. */
138 /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the
139 result in *VIEW. Returns 1 on success, 0 on error. */
140 extern int backtrace_get_view (struct backtrace_state
*state
, int descriptor
,
141 off_t offset
, size_t size
,
142 backtrace_error_callback error_callback
,
143 void *data
, struct backtrace_view
*view
);
145 /* Release a view created by backtrace_get_view. */
146 extern void backtrace_release_view (struct backtrace_state
*state
,
147 struct backtrace_view
*view
,
148 backtrace_error_callback error_callback
,
151 /* Close a file opened by backtrace_open. Returns 1 on success, 0 on
154 extern int backtrace_close (int descriptor
,
155 backtrace_error_callback error_callback
,
158 /* Allocate memory. This is like malloc. */
160 extern void *backtrace_alloc (struct backtrace_state
*state
, size_t size
,
161 backtrace_error_callback error_callback
,
162 void *data
) ATTRIBUTE_MALLOC
;
164 /* Free memory allocated by backtrace_alloc. */
166 extern void backtrace_free (struct backtrace_state
*state
, void *mem
,
168 backtrace_error_callback error_callback
,
171 /* A growable vector of some struct. This is used for more efficient
172 allocation when we don't know the final size of some group of data
173 that we want to represent as an array. */
175 struct backtrace_vector
177 /* The base of the vector. */
179 /* The number of bytes in the vector. */
181 /* The number of bytes available at the current allocation. */
185 /* Grow VEC by SIZE bytes. Return a pointer to the newly allocated
186 bytes. Note that this may move the entire vector to a new memory
187 location. Returns NULL on failure. */
189 extern void *backtrace_vector_grow (struct backtrace_state
*state
, size_t size
,
190 backtrace_error_callback error_callback
,
192 struct backtrace_vector
*vec
);
194 /* Finish the current allocation on VEC. Prepare to start a new
195 allocation. The finished allocation will never be freed. */
197 extern void backtrace_vector_finish (struct backtrace_state
*state
,
198 struct backtrace_vector
*vec
);
200 /* Release any extra space allocated for VEC. Returns 1 on success, 0
203 extern int backtrace_vector_release (struct backtrace_state
*state
,
204 struct backtrace_vector
*vec
,
205 backtrace_error_callback error_callback
,
208 /* Read initial debug data from a descriptor, and set the
209 fileline_data, syminfo_fn, and syminfo_data fields of STATE.
210 Return the fileln_fn field in *FILELN_FN--this is done this way so
211 that the synchronization code is only implemented once. This is
212 called after the descriptor has first been opened. It will close
213 the descriptor if it is no longer needed. Returns 1 on success, 0
214 on error. There will be multiple implementations of this function,
215 for different file formats. Each system will compile the
218 extern int backtrace_initialize (struct backtrace_state
*state
,
220 backtrace_error_callback error_callback
,
222 fileline
*fileline_fn
);
224 /* Add file/line information for a DWARF module. */
226 extern int backtrace_dwarf_add (struct backtrace_state
*state
,
227 uintptr_t base_address
,
228 const unsigned char* dwarf_info
,
229 size_t dwarf_info_size
,
230 const unsigned char *dwarf_line
,
231 size_t dwarf_line_size
,
232 const unsigned char *dwarf_abbrev
,
233 size_t dwarf_abbrev_size
,
234 const unsigned char *dwarf_ranges
,
235 size_t dwarf_range_size
,
236 const unsigned char *dwarf_str
,
237 size_t dwarf_str_size
,
239 backtrace_error_callback error_callback
,
240 void *data
, fileline
*fileline_fn
);