415 lines
9.8 KiB
HTML
415 lines
9.8 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of BACKTRACE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>BACKTRACE</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
backtrace, backtrace_symbols, backtrace_symbols_fd - support
|
|
for application self-debugging
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/execinfo.h">execinfo.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int backtrace(void</B>
|
|
|
|
<B>**</B><I>buffer</I><B>,</B>
|
|
|
|
<B>int</B>
|
|
|
|
<I>size</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
<B>char **backtrace_symbols(void *const</B>
|
|
|
|
<B>*</B><I>buffer</I><B>,</B>
|
|
|
|
<B>int</B>
|
|
|
|
<I>size</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
<B>void backtrace_symbols_fd(void *const</B>
|
|
|
|
<B>*</B><I>buffer</I><B>,</B>
|
|
|
|
<B>int</B>
|
|
|
|
<I>size</I><B>,</B>
|
|
|
|
<B>int</B>
|
|
|
|
<I>fd</I><B>);</B>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>backtrace</B>()
|
|
|
|
returns a backtrace for the calling program,
|
|
in the array pointed to by
|
|
<I>buffer</I>.
|
|
|
|
A backtrace is the series of currently active function calls for
|
|
the program.
|
|
Each item in the array pointed to by
|
|
<I>buffer</I>
|
|
|
|
is of type
|
|
<I>void *</I>,
|
|
|
|
and is the return address from
|
|
the corresponding stack frame.
|
|
The
|
|
<I>size</I>
|
|
|
|
argument specifies the maximum number of addresses
|
|
that can be stored in
|
|
<I>buffer</I>.
|
|
|
|
If the backtrace is larger than
|
|
<I>size</I>,
|
|
|
|
then the addresses corresponding to the
|
|
<I>size</I>
|
|
|
|
most recent function calls are returned;
|
|
to obtain the complete backtrace, make sure that
|
|
<I>buffer</I>
|
|
|
|
and
|
|
<I>size</I>
|
|
|
|
are large enough.
|
|
<P>
|
|
|
|
Given the set of addresses returned by
|
|
<B>backtrace</B>()
|
|
|
|
in
|
|
<I>buffer</I>,
|
|
|
|
<B>backtrace_symbols</B>()
|
|
|
|
translates the addresses into an array of strings that describe
|
|
the addresses symbolically.
|
|
The
|
|
<I>size</I>
|
|
|
|
argument specifies the number of addresses in
|
|
<I>buffer</I>.
|
|
|
|
The symbolic representation of each address consists of the function name
|
|
(if this can be determined), a hexadecimal offset into the function,
|
|
and the actual return address (in hexadecimal).
|
|
The address of the array of string pointers is returned
|
|
as the function result of
|
|
<B>backtrace_symbols</B>().
|
|
|
|
This array is
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)ed
|
|
|
|
by
|
|
<B>backtrace_symbols</B>(),
|
|
|
|
and must be freed by the caller.
|
|
(The strings pointed to by the array of pointers
|
|
need not and should not be freed.)
|
|
<P>
|
|
|
|
<B>backtrace_symbols_fd</B>()
|
|
|
|
takes the same
|
|
<I>buffer</I>
|
|
|
|
and
|
|
<I>size</I>
|
|
|
|
arguments as
|
|
<B>backtrace_symbols</B>(),
|
|
|
|
but instead of returning an array of strings to the caller,
|
|
it writes the strings, one per line, to the file descriptor
|
|
<I>fd</I>.
|
|
|
|
<B>backtrace_symbols_fd</B>()
|
|
|
|
does not call
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
|
|
|
|
and so can be employed in situations where the latter function might
|
|
fail, but see NOTES.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
<B>backtrace</B>()
|
|
|
|
returns the number of addresses returned in
|
|
<I>buffer</I>,
|
|
|
|
which is not greater than
|
|
<I>size</I>.
|
|
|
|
If the return value is less than
|
|
<I>size</I>,
|
|
|
|
then the full backtrace was stored; if it is equal to
|
|
<I>size</I>,
|
|
|
|
then it may have been truncated, in which case the addresses of the
|
|
oldest stack frames are not returned.
|
|
<P>
|
|
|
|
On success,
|
|
<B>backtrace_symbols</B>()
|
|
|
|
returns a pointer to the array
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)ed
|
|
|
|
by the call;
|
|
on error, NULL is returned.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>backtrace</B>(),
|
|
|
|
<B>backtrace_symbols</B>(),
|
|
|
|
and
|
|
<B>backtrace_symbols_fd</B>()
|
|
|
|
are provided in glibc since version 2.1.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>backtrace</B>(),
|
|
|
|
<BR>
|
|
|
|
<B>backtrace_symbols</B>(),
|
|
|
|
<BR>
|
|
|
|
<B>backtrace_symbols_fd</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These functions are GNU extensions.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
These functions make some assumptions about how a function's return
|
|
address is stored on the stack.
|
|
Note the following:
|
|
<DL COMPACT>
|
|
<DT id="1">*<DD>
|
|
Omission of the frame pointers (as
|
|
implied by any of
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)'s
|
|
|
|
nonzero optimization levels) may cause these assumptions to be
|
|
violated.
|
|
<DT id="2">*<DD>
|
|
Inlined functions do not have stack frames.
|
|
<DT id="3">*<DD>
|
|
Tail-call optimization causes one stack frame to replace another.
|
|
<DT id="4">*<DD>
|
|
<B>backtrace</B>()
|
|
|
|
and
|
|
<B>backtrace_symbols_fd</B>()
|
|
|
|
don't call
|
|
<B>malloc</B>()
|
|
|
|
explicitly, but they are part of
|
|
<I>libgcc</I>,
|
|
|
|
which gets loaded dynamically when first used.
|
|
Dynamic loading usually triggers a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3).
|
|
|
|
If you need certain calls to these two functions to not allocate memory
|
|
(in signal handlers, for example), you need to make sure
|
|
<I>libgcc</I>
|
|
|
|
is loaded beforehand.
|
|
</DL>
|
|
<P>
|
|
|
|
The symbol names may be unavailable without the use of special linker
|
|
options.
|
|
For systems using the GNU linker, it is necessary to use the
|
|
<I>-rdynamic</I>
|
|
|
|
linker option.
|
|
Note that names of "static" functions are not exposed,
|
|
and won't be available in the backtrace.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>backtrace</B>()
|
|
|
|
and
|
|
<B>backtrace_symbols</B>().
|
|
|
|
The following shell session shows what we might see when running the
|
|
program:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> cc -rdynamic prog.c -o prog</B>
|
|
|
|
$<B> ./prog 3</B>
|
|
|
|
backtrace() returned 8 addresses
|
|
./prog(myfunc3+0x5c) [0x80487f0]
|
|
./prog [0x8048871]
|
|
./prog(myfunc+0x21) [0x8048894]
|
|
./prog(myfunc+0x1a) [0x804888d]
|
|
./prog(myfunc+0x1a) [0x804888d]
|
|
./prog(main+0x65) [0x80488fb]
|
|
/lib/libc.so.6(__libc_start_main+0xdc) [0xb7e38f9c]
|
|
./prog [0x8048711]
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/execinfo.h">execinfo.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
<P>
|
|
#define BT_BUF_SIZE 100
|
|
<P>
|
|
void
|
|
myfunc3(void)
|
|
{
|
|
<BR> int j, nptrs;
|
|
<BR> void *buffer[BT_BUF_SIZE];
|
|
<BR> char **strings;
|
|
<P>
|
|
<BR> nptrs = backtrace(buffer, BT_BUF_SIZE);
|
|
<BR> printf("backtrace() returned %d addresses\n", nptrs);
|
|
<P>
|
|
<BR> /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
|
|
<BR> would produce similar output to the following: */
|
|
<P>
|
|
<BR> strings = backtrace_symbols(buffer, nptrs);
|
|
<BR> if (strings == NULL) {
|
|
<BR> perror("backtrace_symbols");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> for (j = 0; j < nptrs; j++)
|
|
<BR> printf("%s\n", strings[j]);
|
|
<P>
|
|
<BR> free(strings);
|
|
}
|
|
<P>
|
|
static void /* "static" means don't export the symbol... */
|
|
myfunc2(void)
|
|
{
|
|
<BR> myfunc3();
|
|
}
|
|
<P>
|
|
void
|
|
myfunc(int ncalls)
|
|
{
|
|
<BR> if (ncalls > 1)
|
|
<BR> myfunc(ncalls - 1);
|
|
<BR> else
|
|
<BR> myfunc2();
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "%s num-calls\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> myfunc(atoi(argv[1]));
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+addr2line">addr2line</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gdb">gdb</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+ld">ld</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="9"><A HREF="#lbAF">VERSIONS</A><DD>
|
|
<DT id="10"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="11"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="12"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="13"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="14"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="15"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="16"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:36 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|