1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" drawing on material by Justin Pryzby <pryzbyj@justinpryzby.com>
4 .\" %%%LICENSE_START(PERMISSIVE_MISC)
5 .\" Permission is hereby granted, free of charge, to any person obtaining
6 .\" a copy of this software and associated documentation files (the
7 .\" "Software"), to deal in the Software without restriction, including
8 .\" without limitation the rights to use, copy, modify, merge, publish,
9 .\" distribute, sublicense, and/or sell copies of the Software, and to
10 .\" permit persons to whom the Software is furnished to do so, subject to
11 .\" the following conditions:
13 .\" The above copyright notice and this permission notice shall be
14 .\" included in all copies or substantial portions of the Software.
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 .\" glibc manual and source
27 .TH BACKTRACE 3 2021-03-22 GNU "Linux Programmer's Manual"
29 backtrace, backtrace_symbols, backtrace_symbols_fd \- support
30 for application self-debugging
33 .B #include <execinfo.h>
35 .BI "int backtrace(void **" buffer ", int " size );
37 .BI "char **backtrace_symbols(void *const *" buffer ", int " size );
38 .BI "void backtrace_symbols_fd(void *const *" buffer ", int " size ", int " fd );
42 returns a backtrace for the calling program,
43 in the array pointed to by
45 A backtrace is the series of currently active function calls for
47 Each item in the array pointed to by
51 and is the return address from
52 the corresponding stack frame.
55 argument specifies the maximum number of addresses
58 If the backtrace is larger than
60 then the addresses corresponding to the
62 most recent function calls are returned;
63 to obtain the complete backtrace, make sure that
69 Given the set of addresses returned by
73 .BR backtrace_symbols ()
74 translates the addresses into an array of strings that describe
75 the addresses symbolically.
78 argument specifies the number of addresses in
80 The symbolic representation of each address consists of the function name
81 (if this can be determined), a hexadecimal offset into the function,
82 and the actual return address (in hexadecimal).
83 The address of the array of string pointers is returned
84 as the function result of
85 .BR backtrace_symbols ().
89 .BR backtrace_symbols (),
90 and must be freed by the caller.
91 (The strings pointed to by the array of pointers
92 need not and should not be freed.)
94 .BR backtrace_symbols_fd ()
100 .BR backtrace_symbols (),
101 but instead of returning an array of strings to the caller,
102 it writes the strings, one per line, to the file descriptor
104 .BR backtrace_symbols_fd ()
107 and so can be employed in situations where the latter function might
111 returns the number of addresses returned in
113 which is not greater than
115 If the return value is less than
117 then the full backtrace was stored; if it is equal to
119 then it may have been truncated, in which case the addresses of the
120 oldest stack frames are not returned.
123 .BR backtrace_symbols ()
124 returns a pointer to the array
127 on error, NULL is returned.
130 .BR backtrace_symbols (),
132 .BR backtrace_symbols_fd ()
133 are provided in glibc since version 2.1.
135 For an explanation of the terms used in this section, see
143 Interface Attribute Value
146 .BR backtrace_symbols (),
147 .BR backtrace_symbols_fd ()
148 T} Thread safety MT-Safe
154 These functions are GNU extensions.
156 These functions make some assumptions about how a function's return
157 address is stored on the stack.
160 Omission of the frame pointers (as
163 nonzero optimization levels) may cause these assumptions to be
166 Inlined functions do not have stack frames.
168 Tail-call optimization causes one stack frame to replace another.
172 .BR backtrace_symbols_fd ()
175 explicitly, but they are part of
177 which gets loaded dynamically when first used.
178 Dynamic loading usually triggers a call to
180 If you need certain calls to these two functions to not allocate memory
181 (in signal handlers, for example), you need to make sure
183 is loaded beforehand.
185 The symbol names may be unavailable without the use of special linker
187 For systems using the GNU linker, it is necessary to use the
190 Note that names of "static" functions are not exposed,
191 and won't be available in the backtrace.
193 The program below demonstrates the use of
196 .BR backtrace_symbols ().
197 The following shell session shows what we might see when running the
202 .RB "$" " cc \-rdynamic prog.c \-o prog"
204 backtrace() returned 8 addresses
205 \&./prog(myfunc3+0x5c) [0x80487f0]
207 \&./prog(myfunc+0x21) [0x8048894]
208 \&./prog(myfunc+0x1a) [0x804888d]
209 \&./prog(myfunc+0x1a) [0x804888d]
210 \&./prog(main+0x65) [0x80488fb]
211 \&/lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
218 #include <execinfo.h>
223 #define BT_BUF_SIZE 100
229 void *buffer[BT_BUF_SIZE];
232 nptrs = backtrace(buffer, BT_BUF_SIZE);
233 printf("backtrace() returned %d addresses\en", nptrs);
235 /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
236 would produce similar output to the following: */
238 strings = backtrace_symbols(buffer, nptrs);
239 if (strings == NULL) {
240 perror("backtrace_symbols");
244 for (int j = 0; j < nptrs; j++)
245 printf("%s\en", strings[j]);
250 static void /* "static" means don\(aqt export the symbol... */
266 main(int argc, char *argv[])
269 fprintf(stderr, "%s num\-calls\en", argv[0]);
273 myfunc(atoi(argv[1]));