1BACKTRACE(3)               Linux Programmer's Manual              BACKTRACE(3)
2
3
4

NAME

6       backtrace, backtrace_symbols, backtrace_symbols_fd - support for appli‐
7       cation self-debugging
8

SYNOPSIS

10       #include <execinfo.h>
11
12       int backtrace(void **buffer, int size);
13
14       char **backtrace_symbols(void *const *buffer, int size);
15
16       void backtrace_symbols_fd(void *const *buffer, int size, int fd);
17

DESCRIPTION

19       backtrace() returns a backtrace for the calling program, in  the  array
20       pointed  to  by  buffer.  A backtrace is the series of currently active
21       function calls for the program.  Each item in the array pointed  to  by
22       buffer  is  of  type  void *, and is the return address from the corre‐
23       sponding stack frame.  The size argument specifies the  maximum  number
24       of  addresses that can be stored in buffer.  If the backtrace is larger
25       than size, then the addresses corresponding to  the  size  most  recent
26       function  calls  are  returned;  to obtain the complete backtrace, make
27       sure that buffer and size are large enough.
28
29       Given the set of addresses returned by  backtrace()  in  buffer,  back‐
30       trace_symbols()  translates the addresses into an array of strings that
31       describe the addresses symbolically.  The size argument  specifies  the
32       number  of  addresses  in  buffer.  The symbolic representation of each
33       address consists of the function name (if this can  be  determined),  a
34       hexadecimal offset into the function, and the actual return address (in
35       hexadecimal).  The address of the array of string pointers is  returned
36       as  the  function  result  of  backtrace_symbols().  This array is mal‐
37       loc(3)ed by backtrace_symbols(), and must be freed by the caller.  (The
38       strings  pointed to by the array of pointers need not and should not be
39       freed.)
40
41       backtrace_symbols_fd() takes the same  buffer  and  size  arguments  as
42       backtrace_symbols(),  but  instead  of returning an array of strings to
43       the caller, it writes the strings, one per line, to the file descriptor
44       fd.   backtrace_symbols_fd()  does  not  call  malloc(3), and so can be
45       employed in situations where the latter function might  fail,  but  see
46       NOTES.
47

RETURN VALUE

49       backtrace()  returns  the number of addresses returned in buffer, which
50       is not greater than size.  If the return value is less than size,  then
51       the full backtrace was stored; if it is equal to size, then it may have
52       been truncated, in which case the addresses of the oldest stack  frames
53       are not returned.
54
55       On  success,  backtrace_symbols()  returns  a pointer to the array mal‐
56       loc(3)ed by the call; on error, NULL is returned.
57

VERSIONS

59       backtrace(), backtrace_symbols(), and backtrace_symbols_fd()  are  pro‐
60       vided in glibc since version 2.1.
61

ATTRIBUTES

63       For   an   explanation   of   the  terms  used  in  this  section,  see
64       attributes(7).
65
66       ┌───────────────────────┬───────────────┬─────────┐
67Interface              Attribute     Value   
68       ├───────────────────────┼───────────────┼─────────┤
69backtrace(),           │ Thread safety │ MT-Safe │
70backtrace_symbols(),   │               │         │
71backtrace_symbols_fd() │               │         │
72       └───────────────────────┴───────────────┴─────────┘

CONFORMING TO

74       These functions are GNU extensions.
75

NOTES

77       These functions make some assumptions about  how  a  function's  return
78       address is stored on the stack.  Note the following:
79
80       *  Omission  of  the  frame  pointers  (as  implied  by any of gcc(1)'s
81          nonzero optimization levels) may cause these assumptions to be  vio‐
82          lated.
83
84       *  Inlined functions do not have stack frames.
85
86       *  Tail-call optimization causes one stack frame to replace another.
87
88       *  backtrace()  and  backtrace_symbols_fd() don't call malloc() explic‐
89          itly, but they are part of libgcc,  which  gets  loaded  dynamically
90          when  first  used.   Dynamic loading usually triggers a call to mal‐
91          loc(3).  If you need certain calls to these  two  functions  to  not
92          allocate  memory (in signal handlers, for example), you need to make
93          sure libgcc is loaded beforehand.
94
95       The symbol names may be unavailable without the use of  special  linker
96       options.   For systems using the GNU linker, it is necessary to use the
97       -rdynamic linker option.  Note that names of "static" functions are not
98       exposed, and won't be available in the backtrace.
99

EXAMPLE

101       The  program  below  demonstrates  the  use  of  backtrace()  and back‐
102       trace_symbols().  The following shell session shows what we  might  see
103       when running the program:
104
105           $ cc -rdynamic prog.c -o prog
106           $ ./prog 3
107           backtrace() returned 8 addresses
108           ./prog(myfunc3+0x5c) [0x80487f0]
109           ./prog [0x8048871]
110           ./prog(myfunc+0x21) [0x8048894]
111           ./prog(myfunc+0x1a) [0x804888d]
112           ./prog(myfunc+0x1a) [0x804888d]
113           ./prog(main+0x65) [0x80488fb]
114           /lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
115           ./prog [0x8048711]
116
117   Program source
118
119       #include <execinfo.h>
120       #include <stdio.h>
121       #include <stdlib.h>
122       #include <unistd.h>
123
124       #define BT_BUF_SIZE 100
125
126       void
127       myfunc3(void)
128       {
129           int j, nptrs;
130           void *buffer[BT_BUF_SIZE];
131           char **strings;
132
133           nptrs = backtrace(buffer, BT_BUF_SIZE);
134           printf("backtrace() returned %d addresses\n", nptrs);
135
136           /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
137              would produce similar output to the following: */
138
139           strings = backtrace_symbols(buffer, nptrs);
140           if (strings == NULL) {
141               perror("backtrace_symbols");
142               exit(EXIT_FAILURE);
143           }
144
145           for (j = 0; j < nptrs; j++)
146               printf("%s\n", strings[j]);
147
148           free(strings);
149       }
150
151       static void   /* "static" means don't export the symbol... */
152       myfunc2(void)
153       {
154           myfunc3();
155       }
156
157       void
158       myfunc(int ncalls)
159       {
160           if (ncalls > 1)
161               myfunc(ncalls - 1);
162           else
163               myfunc2();
164       }
165
166       int
167       main(int argc, char *argv[])
168       {
169           if (argc != 2) {
170               fprintf(stderr, "%s num-calls\n", argv[0]);
171               exit(EXIT_FAILURE);
172           }
173
174           myfunc(atoi(argv[1]));
175           exit(EXIT_SUCCESS);
176       }
177

SEE ALSO

179       addr2line(1), gcc(1), gdb(1), ld(1), dlopen(3), malloc(3)
180

COLOPHON

182       This  page  is  part of release 5.02 of the Linux man-pages project.  A
183       description of the project, information about reporting bugs,  and  the
184       latest     version     of     this    page,    can    be    found    at
185       https://www.kernel.org/doc/man-pages/.
186
187
188
189GNU                               2019-03-06                      BACKTRACE(3)
Impressum