annotate libbacktrace/backtrace.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* backtrace.h -- Public header file for stack backtrace library.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
7 met:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
14 the documentation and/or other materials provided with the
kono
parents:
diff changeset
15 distribution.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 (3) The name of the author may not be used to
kono
parents:
diff changeset
18 endorse or promote products derived from this software without
kono
parents:
diff changeset
19 specific prior written permission.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
31 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #ifndef BACKTRACE_H
kono
parents:
diff changeset
34 #define BACKTRACE_H
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 #include <stddef.h>
kono
parents:
diff changeset
37 #include <stdio.h>
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 /* We want to get a definition for uintptr_t, but we still care about
kono
parents:
diff changeset
40 systems that don't have <stdint.h>. */
kono
parents:
diff changeset
41 #if defined(__GLIBC__) && __GLIBC__ >= 2
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 #include <stdint.h>
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 #elif defined(HAVE_STDINT_H)
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 #include <stdint.h>
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 #else
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 /* Systems that don't have <stdint.h> must provide gstdint.h, e.g.,
kono
parents:
diff changeset
52 from GCC_HEADER_STDINT in configure.ac. */
kono
parents:
diff changeset
53 #include "gstdint.h"
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 #endif
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 #ifdef __cplusplus
kono
parents:
diff changeset
58 extern "C" {
kono
parents:
diff changeset
59 #endif
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 /* The backtrace state. This struct is intentionally not defined in
kono
parents:
diff changeset
62 the public interface. */
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 struct backtrace_state;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* The type of the error callback argument to backtrace functions.
kono
parents:
diff changeset
67 This function, if not NULL, will be called for certain error cases.
kono
parents:
diff changeset
68 The DATA argument is passed to the function that calls this one.
kono
parents:
diff changeset
69 The MSG argument is an error message. The ERRNUM argument, if
kono
parents:
diff changeset
70 greater than 0, holds an errno value. The MSG buffer may become
kono
parents:
diff changeset
71 invalid after this function returns.
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 As a special case, the ERRNUM argument will be passed as -1 if no
kono
parents:
diff changeset
74 debug info can be found for the executable, but the function
kono
parents:
diff changeset
75 requires debug info (e.g., backtrace_full, backtrace_pcinfo). The
kono
parents:
diff changeset
76 MSG in this case will be something along the lines of "no debug
kono
parents:
diff changeset
77 info". Similarly, ERRNUM will be passed as -1 if there is no
kono
parents:
diff changeset
78 symbol table, but the function requires a symbol table (e.g.,
kono
parents:
diff changeset
79 backtrace_syminfo). This may be used as a signal that some other
kono
parents:
diff changeset
80 approach should be tried. */
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 typedef void (*backtrace_error_callback) (void *data, const char *msg,
kono
parents:
diff changeset
83 int errnum);
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 /* Create state information for the backtrace routines. This must be
kono
parents:
diff changeset
86 called before any of the other routines, and its return value must
kono
parents:
diff changeset
87 be passed to all of the other routines. FILENAME is the path name
kono
parents:
diff changeset
88 of the executable file; if it is NULL the library will try
kono
parents:
diff changeset
89 system-specific path names. If not NULL, FILENAME must point to a
kono
parents:
diff changeset
90 permanent buffer. If THREADED is non-zero the state may be
kono
parents:
diff changeset
91 accessed by multiple threads simultaneously, and the library will
kono
parents:
diff changeset
92 use appropriate atomic operations. If THREADED is zero the state
kono
parents:
diff changeset
93 may only be accessed by one thread at a time. This returns a state
kono
parents:
diff changeset
94 pointer on success, NULL on error. If an error occurs, this will
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
95 call the ERROR_CALLBACK routine.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
96
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
97 Calling this function allocates resources that cannot be freed.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
98 There is no backtrace_free_state function. The state is used to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
99 cache information that is expensive to recompute. Programs are
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
100 expected to call this function at most once and to save the return
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
101 value for all later calls to backtrace functions. */
111
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 extern struct backtrace_state *backtrace_create_state (
kono
parents:
diff changeset
104 const char *filename, int threaded,
kono
parents:
diff changeset
105 backtrace_error_callback error_callback, void *data);
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 /* The type of the callback argument to the backtrace_full function.
kono
parents:
diff changeset
108 DATA is the argument passed to backtrace_full. PC is the program
kono
parents:
diff changeset
109 counter. FILENAME is the name of the file containing PC, or NULL
kono
parents:
diff changeset
110 if not available. LINENO is the line number in FILENAME containing
kono
parents:
diff changeset
111 PC, or 0 if not available. FUNCTION is the name of the function
kono
parents:
diff changeset
112 containing PC, or NULL if not available. This should return 0 to
kono
parents:
diff changeset
113 continuing tracing. The FILENAME and FUNCTION buffers may become
kono
parents:
diff changeset
114 invalid after this function returns. */
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 typedef int (*backtrace_full_callback) (void *data, uintptr_t pc,
kono
parents:
diff changeset
117 const char *filename, int lineno,
kono
parents:
diff changeset
118 const char *function);
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 /* Get a full stack backtrace. SKIP is the number of frames to skip;
kono
parents:
diff changeset
121 passing 0 will start the trace with the function calling
kono
parents:
diff changeset
122 backtrace_full. DATA is passed to the callback routine. If any
kono
parents:
diff changeset
123 call to CALLBACK returns a non-zero value, the stack backtrace
kono
parents:
diff changeset
124 stops, and backtrace returns that value; this may be used to limit
kono
parents:
diff changeset
125 the number of stack frames desired. If all calls to CALLBACK
kono
parents:
diff changeset
126 return 0, backtrace returns 0. The backtrace_full function will
kono
parents:
diff changeset
127 make at least one call to either CALLBACK or ERROR_CALLBACK. This
kono
parents:
diff changeset
128 function requires debug info for the executable. */
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 extern int backtrace_full (struct backtrace_state *state, int skip,
kono
parents:
diff changeset
131 backtrace_full_callback callback,
kono
parents:
diff changeset
132 backtrace_error_callback error_callback,
kono
parents:
diff changeset
133 void *data);
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 /* The type of the callback argument to the backtrace_simple function.
kono
parents:
diff changeset
136 DATA is the argument passed to simple_backtrace. PC is the program
kono
parents:
diff changeset
137 counter. This should return 0 to continue tracing. */
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 typedef int (*backtrace_simple_callback) (void *data, uintptr_t pc);
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 /* Get a simple backtrace. SKIP is the number of frames to skip, as
kono
parents:
diff changeset
142 in backtrace. DATA is passed to the callback routine. If any call
kono
parents:
diff changeset
143 to CALLBACK returns a non-zero value, the stack backtrace stops,
kono
parents:
diff changeset
144 and backtrace_simple returns that value. Otherwise
kono
parents:
diff changeset
145 backtrace_simple returns 0. The backtrace_simple function will
kono
parents:
diff changeset
146 make at least one call to either CALLBACK or ERROR_CALLBACK. This
kono
parents:
diff changeset
147 function does not require any debug info for the executable. */
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 extern int backtrace_simple (struct backtrace_state *state, int skip,
kono
parents:
diff changeset
150 backtrace_simple_callback callback,
kono
parents:
diff changeset
151 backtrace_error_callback error_callback,
kono
parents:
diff changeset
152 void *data);
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 /* Print the current backtrace in a user readable format to a FILE.
kono
parents:
diff changeset
155 SKIP is the number of frames to skip, as in backtrace_full. Any
kono
parents:
diff changeset
156 error messages are printed to stderr. This function requires debug
kono
parents:
diff changeset
157 info for the executable. */
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 extern void backtrace_print (struct backtrace_state *state, int skip, FILE *);
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 /* Given PC, a program counter in the current program, call the
kono
parents:
diff changeset
162 callback function with filename, line number, and function name
kono
parents:
diff changeset
163 information. This will normally call the callback function exactly
kono
parents:
diff changeset
164 once. However, if the PC happens to describe an inlined call, and
kono
parents:
diff changeset
165 the debugging information contains the necessary information, then
kono
parents:
diff changeset
166 this may call the callback function multiple times. This will make
kono
parents:
diff changeset
167 at least one call to either CALLBACK or ERROR_CALLBACK. This
kono
parents:
diff changeset
168 returns the first non-zero value returned by CALLBACK, or 0. */
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 extern int backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
kono
parents:
diff changeset
171 backtrace_full_callback callback,
kono
parents:
diff changeset
172 backtrace_error_callback error_callback,
kono
parents:
diff changeset
173 void *data);
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 /* The type of the callback argument to backtrace_syminfo. DATA and
kono
parents:
diff changeset
176 PC are the arguments passed to backtrace_syminfo. SYMNAME is the
kono
parents:
diff changeset
177 name of the symbol for the corresponding code. SYMVAL is the
kono
parents:
diff changeset
178 value and SYMSIZE is the size of the symbol. SYMNAME will be NULL
kono
parents:
diff changeset
179 if no error occurred but the symbol could not be found. */
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc,
kono
parents:
diff changeset
182 const char *symname,
kono
parents:
diff changeset
183 uintptr_t symval,
kono
parents:
diff changeset
184 uintptr_t symsize);
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 /* Given ADDR, an address or program counter in the current program,
kono
parents:
diff changeset
187 call the callback information with the symbol name and value
kono
parents:
diff changeset
188 describing the function or variable in which ADDR may be found.
kono
parents:
diff changeset
189 This will call either CALLBACK or ERROR_CALLBACK exactly once.
kono
parents:
diff changeset
190 This returns 1 on success, 0 on failure. This function requires
kono
parents:
diff changeset
191 the symbol table but does not require the debug info. Note that if
kono
parents:
diff changeset
192 the symbol table is present but ADDR could not be found in the
kono
parents:
diff changeset
193 table, CALLBACK will be called with a NULL SYMNAME argument.
kono
parents:
diff changeset
194 Returns 1 on success, 0 on error. */
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 extern int backtrace_syminfo (struct backtrace_state *state, uintptr_t addr,
kono
parents:
diff changeset
197 backtrace_syminfo_callback callback,
kono
parents:
diff changeset
198 backtrace_error_callback error_callback,
kono
parents:
diff changeset
199 void *data);
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 #ifdef __cplusplus
kono
parents:
diff changeset
202 } /* End extern "C". */
kono
parents:
diff changeset
203 #endif
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 #endif