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.
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

CONFORMING TO

62       These functions are GNU extensions.
63

NOTES

65       These functions make some assumptions about  how  a  function's  return
66       address is stored on the stack.  Note the following:
67
68       *  Omission  of  the  frame  pointers  (as  implied  by any of gcc(1)'s
69          nonzero optimization levels) may cause these assumptions to be  vio‐
70          lated.
71
72       *  Inlined functions do not have stack frames.
73
74       *  Tail-call optimization causes one stack frame to replace another.
75
76       The  symbol  names may be unavailable without the use of special linker
77       options.  For systems using the GNU linker, it is necessary to use  the
78       -rdynamic linker option.  Note that names of "static" functions are not
79       exposed, and won't be available in the backtrace.
80

EXAMPLE

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

SEE ALSO

159       gcc(1), ld(1), dlopen(3), malloc(3)
160

COLOPHON

162       This page is part of release 3.53 of the Linux  man-pages  project.   A
163       description  of  the project, and information about reporting bugs, can
164       be found at http://www.kernel.org/doc/man-pages/.
165
166
167
168GNU                               2008-06-14                      BACKTRACE(3)
Impressum