]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/backtrace.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / backtrace.3
CommitLineData
a1eaacb1 1'\" t
c11b1abf 2.\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
46b75119
MK
3.\" drawing on material by Justin Pryzby <pryzbyj@justinpryzby.com>
4.\"
7c576f45 5.\" %%%LICENSE_START(PERMISSIVE_MISC)
46b75119
MK
6.\" Permission is hereby granted, free of charge, to any person obtaining
7.\" a copy of this software and associated documentation files (the
8.\" "Software"), to deal in the Software without restriction, including
9.\" without limitation the rights to use, copy, modify, merge, publish,
10.\" distribute, sublicense, and/or sell copies of the Software, and to
11.\" permit persons to whom the Software is furnished to do so, subject to
12.\" the following conditions:
13.\"
14.\" The above copyright notice and this permission notice shall be
15.\" included in all copies or substantial portions of the Software.
16.\"
17.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18.\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20.\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21.\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22.\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23.\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8ff7380d 24.\" %%%LICENSE_END
46b75119
MK
25.\"
26.\" References:
27.\" glibc manual and source
a5ebdc8d 28.TH backtrace 3 (date) "Linux man-pages (unreleased)"
46b75119
MK
29.SH NAME
30backtrace, backtrace_symbols, backtrace_symbols_fd \- support
31for application self-debugging
b813014f
AC
32.SH LIBRARY
33Standard C library
34.RI ( libc ", " \-lc )
46b75119 35.SH SYNOPSIS
7b5d39f4 36.nf
46b75119 37.B #include <execinfo.h>
c6d039a3 38.P
c64cd13e 39.BI "int backtrace(void *" buffer [. size "], int " size );
c6d039a3 40.P
c64cd13e
AC
41.BI "char **backtrace_symbols(void *const " buffer [. size "], int " size );
42.BI "void backtrace_symbols_fd(void *const " buffer [. size "], int " size ", \
43int " fd );
7b5d39f4 44.fi
46b75119
MK
45.SH DESCRIPTION
46.BR backtrace ()
47returns a backtrace for the calling program,
48in the array pointed to by
49.IR buffer .
50A backtrace is the series of currently active function calls for
51the program.
52Each item in the array pointed to by
53.I buffer
c9942389
MK
54is of type
55.IR "void\ *" ,
56and is the return address from
46b75119
MK
57the corresponding stack frame.
58The
59.I size
60argument specifies the maximum number of addresses
61that can be stored in
62.IR buffer .
63If the backtrace is larger than
64.IR size ,
65then the addresses corresponding to the
66.I size
67most recent function calls are returned;
68to obtain the complete backtrace, make sure that
69.I buffer
70and
71.I size
72are large enough.
c6d039a3 73.P
46b75119
MK
74Given the set of addresses returned by
75.BR backtrace ()
76in
77.IR buffer ,
78.BR backtrace_symbols ()
79translates the addresses into an array of strings that describe
80the addresses symbolically.
81The
82.I size
83argument specifies the number of addresses in
84.IR buffer .
85The symbolic representation of each address consists of the function name
86(if this can be determined), a hexadecimal offset into the function,
87and the actual return address (in hexadecimal).
88The address of the array of string pointers is returned
89as the function result of
90.BR backtrace_symbols ().
91This array is
92.BR malloc (3)ed
988db661 93by
46b75119
MK
94.BR backtrace_symbols (),
95and must be freed by the caller.
02770bac 96(The strings pointed to by the array of pointers
46b75119 97need not and should not be freed.)
c6d039a3 98.P
46b75119
MK
99.BR backtrace_symbols_fd ()
100takes the same
101.I buffer
102and
103.I size
104arguments as
105.BR backtrace_symbols (),
106but instead of returning an array of strings to the caller,
107it writes the strings, one per line, to the file descriptor
108.IR fd .
ada00e83 109.BR backtrace_symbols_fd ()
46b75119
MK
110does not call
111.BR malloc (3),
ca5d5169
SP
112and so can be employed in situations where the latter function might
113fail, but see NOTES.
47297adb 114.SH RETURN VALUE
46b75119
MK
115.BR backtrace ()
116returns the number of addresses returned in
117.IR buffer ,
118which is not greater than
119.IR size .
120If the return value is less than
121.IR size ,
122then the full backtrace was stored; if it is equal to
123.IR size ,
124then it may have been truncated, in which case the addresses of the
125oldest stack frames are not returned.
c6d039a3 126.P
46b75119
MK
127On success,
128.BR backtrace_symbols ()
129returns a pointer to the array
130.BR malloc (3)ed
131by the call;
132on error, NULL is returned.
952509e7
MS
133.SH ATTRIBUTES
134For an explanation of the terms used in this section, see
135.BR attributes (7).
136.TS
137allbox;
c466875e 138lbx lb lb
952509e7
MS
139l l l.
140Interface Attribute Value
141T{
9e54434e
BR
142.na
143.nh
952509e7 144.BR backtrace (),
952509e7 145.BR backtrace_symbols (),
952509e7
MS
146.BR backtrace_symbols_fd ()
147T} Thread safety MT-Safe
148.TE
3113c7f3 149.SH STANDARDS
4131356c
AC
150GNU.
151.SH HISTORY
152glibc 2.1.
46b75119
MK
153.SH NOTES
154These functions make some assumptions about how a function's return
155address is stored on the stack.
156Note the following:
cdede5cd 157.IP \[bu] 3
46b75119
MK
158Omission of the frame pointers (as
159implied by any of
160.BR gcc (1)'s
c7094399 161nonzero optimization levels) may cause these assumptions to be
46b75119 162violated.
cdede5cd 163.IP \[bu]
46b75119 164Inlined functions do not have stack frames.
cdede5cd 165.IP \[bu]
46b75119 166Tail-call optimization causes one stack frame to replace another.
cdede5cd 167.IP \[bu]
ca5d5169
SP
168.BR backtrace ()
169and
170.BR backtrace_symbols_fd ()
171don't call
172.BR malloc ()
454f90d7
MK
173explicitly, but they are part of
174.IR libgcc ,
175which gets loaded dynamically when first used.
176Dynamic loading usually triggers a call to
177.BR malloc (3).
178If you need certain calls to these two functions to not allocate memory
179(in signal handlers, for example), you need to make sure
180.I libgcc
181is loaded beforehand.
c6d039a3 182.P
46b75119
MK
183The symbol names may be unavailable without the use of special linker
184options.
185For systems using the GNU linker, it is necessary to use the
186.I \-rdynamic
187linker option.
188Note that names of "static" functions are not exposed,
189and won't be available in the backtrace.
a14af333 190.SH EXAMPLES
46b75119
MK
191The program below demonstrates the use of
192.BR backtrace ()
193and
194.BR backtrace_symbols ().
195The following shell session shows what we might see when running the
196program:
c6d039a3 197.P
088a639b 198.in +4n
e646a1ba 199.EX
b43a3b30
MK
200.RB "$" " cc \-rdynamic prog.c \-o prog"
201.RB "$" " ./prog 3"
202backtrace() returned 8 addresses
203\&./prog(myfunc3+0x5c) [0x80487f0]
204\&./prog [0x8048871]
205\&./prog(myfunc+0x21) [0x8048894]
206\&./prog(myfunc+0x1a) [0x804888d]
207\&./prog(myfunc+0x1a) [0x804888d]
208\&./prog(main+0x65) [0x80488fb]
209\&/lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
210\&./prog [0x8048711]
b8302363 211.EE
e646a1ba 212.in
9c330504 213.SS Program source
d84d0300 214\&
b0b6ab4e 215.\" SRC BEGIN (backtrace.c)
e7d0bb47 216.EX
46b75119
MK
217#include <execinfo.h>
218#include <stdio.h>
219#include <stdlib.h>
220#include <unistd.h>
fe5dba13 221\&
cefcfd92 222#define BT_BUF_SIZE 100
fe5dba13 223\&
46b75119
MK
224void
225myfunc3(void)
226{
88893a77 227 int nptrs;
cefcfd92 228 void *buffer[BT_BUF_SIZE];
46b75119 229 char **strings;
fe5dba13 230\&
cefcfd92 231 nptrs = backtrace(buffer, BT_BUF_SIZE);
d1a71985 232 printf("backtrace() returned %d addresses\en", nptrs);
fe5dba13 233\&
46b75119
MK
234 /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
235 would produce similar output to the following: */
fe5dba13 236\&
46b75119
MK
237 strings = backtrace_symbols(buffer, nptrs);
238 if (strings == NULL) {
239 perror("backtrace_symbols");
240 exit(EXIT_FAILURE);
241 }
fe5dba13 242\&
b42296e4 243 for (size_t j = 0; j < nptrs; j++)
d1a71985 244 printf("%s\en", strings[j]);
fe5dba13 245\&
46b75119
MK
246 free(strings);
247}
fe5dba13 248\&
b957f81f 249static void /* "static" means don\[aq]t export the symbol... */
46b75119
MK
250myfunc2(void)
251{
252 myfunc3();
253}
fe5dba13 254\&
46b75119
MK
255void
256myfunc(int ncalls)
257{
258 if (ncalls > 1)
29059a65 259 myfunc(ncalls \- 1);
46b75119
MK
260 else
261 myfunc2();
262}
fe5dba13 263\&
46b75119
MK
264int
265main(int argc, char *argv[])
266{
267 if (argc != 2) {
d1a71985 268 fprintf(stderr, "%s num\-calls\en", argv[0]);
46b75119
MK
269 exit(EXIT_FAILURE);
270 }
fe5dba13 271\&
46b75119
MK
272 myfunc(atoi(argv[1]));
273 exit(EXIT_SUCCESS);
274}
e7d0bb47 275.EE
b0b6ab4e 276.\" SRC END
46b75119 277.SH SEE ALSO
4f9d01a9 278.BR addr2line (1),
46b75119 279.BR gcc (1),
d64b3725 280.BR gdb (1),
46b75119
MK
281.BR ld (1),
282.BR dlopen (3),
283.BR malloc (3)