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 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
29 backtrace, backtrace_symbols, backtrace_symbols_fd \- support
30 for application self-debugging
33 .RI ( libc ", " \-lc )
36 .B #include <execinfo.h>
38 .BI "int backtrace(void **" buffer ", int " size );
40 .BI "char **backtrace_symbols(void *const *" buffer ", int " size );
41 .BI "void backtrace_symbols_fd(void *const *" buffer ", int " size ", int " fd );
45 returns a backtrace for the calling program,
46 in the array pointed to by
48 A backtrace is the series of currently active function calls for
50 Each item in the array pointed to by
54 and is the return address from
55 the corresponding stack frame.
58 argument specifies the maximum number of addresses
61 If the backtrace is larger than
63 then the addresses corresponding to the
65 most recent function calls are returned;
66 to obtain the complete backtrace, make sure that
72 Given the set of addresses returned by
76 .BR backtrace_symbols ()
77 translates the addresses into an array of strings that describe
78 the addresses symbolically.
81 argument specifies the number of addresses in
83 The symbolic representation of each address consists of the function name
84 (if this can be determined), a hexadecimal offset into the function,
85 and the actual return address (in hexadecimal).
86 The address of the array of string pointers is returned
87 as the function result of
88 .BR backtrace_symbols ().
92 .BR backtrace_symbols (),
93 and must be freed by the caller.
94 (The strings pointed to by the array of pointers
95 need not and should not be freed.)
97 .BR backtrace_symbols_fd ()
103 .BR backtrace_symbols (),
104 but instead of returning an array of strings to the caller,
105 it writes the strings, one per line, to the file descriptor
107 .BR backtrace_symbols_fd ()
110 and so can be employed in situations where the latter function might
114 returns the number of addresses returned in
116 which is not greater than
118 If the return value is less than
120 then the full backtrace was stored; if it is equal to
122 then it may have been truncated, in which case the addresses of the
123 oldest stack frames are not returned.
126 .BR backtrace_symbols ()
127 returns a pointer to the array
130 on error, NULL is returned.
133 .BR backtrace_symbols (),
135 .BR backtrace_symbols_fd ()
136 are provided in glibc since version 2.1.
138 For an explanation of the terms used in this section, see
146 Interface Attribute Value
149 .BR backtrace_symbols (),
150 .BR backtrace_symbols_fd ()
151 T} Thread safety MT-Safe
157 These functions are GNU extensions.
159 These functions make some assumptions about how a function's return
160 address is stored on the stack.
163 Omission of the frame pointers (as
166 nonzero optimization levels) may cause these assumptions to be
169 Inlined functions do not have stack frames.
171 Tail-call optimization causes one stack frame to replace another.
175 .BR backtrace_symbols_fd ()
178 explicitly, but they are part of
180 which gets loaded dynamically when first used.
181 Dynamic loading usually triggers a call to
183 If you need certain calls to these two functions to not allocate memory
184 (in signal handlers, for example), you need to make sure
186 is loaded beforehand.
188 The symbol names may be unavailable without the use of special linker
190 For systems using the GNU linker, it is necessary to use the
193 Note that names of "static" functions are not exposed,
194 and won't be available in the backtrace.
196 The program below demonstrates the use of
199 .BR backtrace_symbols ().
200 The following shell session shows what we might see when running the
205 .RB "$" " cc \-rdynamic prog.c \-o prog"
207 backtrace() returned 8 addresses
208 \&./prog(myfunc3+0x5c) [0x80487f0]
210 \&./prog(myfunc+0x21) [0x8048894]
211 \&./prog(myfunc+0x1a) [0x804888d]
212 \&./prog(myfunc+0x1a) [0x804888d]
213 \&./prog(main+0x65) [0x80488fb]
214 \&/lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
221 #include <execinfo.h>
226 #define BT_BUF_SIZE 100
232 void *buffer[BT_BUF_SIZE];
235 nptrs = backtrace(buffer, BT_BUF_SIZE);
236 printf("backtrace() returned %d addresses\en", nptrs);
238 /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
239 would produce similar output to the following: */
241 strings = backtrace_symbols(buffer, nptrs);
242 if (strings == NULL) {
243 perror("backtrace_symbols");
247 for (int j = 0; j < nptrs; j++)
248 printf("%s\en", strings[j]);
253 static void /* "static" means don\(aqt export the symbol... */
269 main(int argc, char *argv[])
272 fprintf(stderr, "%s num\-calls\en", argv[0]);
276 myfunc(atoi(argv[1]));