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       void backtrace_symbols_fd(void *const *buffer, int size, int fd);
16

DESCRIPTION

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

RETURN VALUE

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

VERSIONS

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

ATTRIBUTES

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

CONFORMING TO

73       These functions are GNU extensions.
74

NOTES

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

EXAMPLES

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

SEE ALSO

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

COLOPHON

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