man-pages/man3/backtrace.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
backtrace, backtrace_symbols, backtrace_symbols_fd - support
for application self-debugging
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/execinfo.h">execinfo.h</A>&gt;</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">&nbsp;</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&nbsp;*</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>CONFORMING TO</H2>
These functions are GNU extensions.
<A NAME="lbAI">&nbsp;</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 &quot;static&quot; functions are not exposed,
and won't be available in the backtrace.
<A NAME="lbAJ">&nbsp;</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">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/execinfo.h">execinfo.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
#define BT_BUF_SIZE 100
<P>
void
myfunc3(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j,&nbsp;nptrs;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;*buffer[BT_BUF_SIZE];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;**strings;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;nptrs&nbsp;=&nbsp;backtrace(buffer,&nbsp;BT_BUF_SIZE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;backtrace()&nbsp;returned&nbsp;%d&nbsp;addresses\n&quot;,&nbsp;nptrs);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;call&nbsp;backtrace_symbols_fd(buffer,&nbsp;nptrs,&nbsp;STDOUT_FILENO)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;would&nbsp;produce&nbsp;similar&nbsp;output&nbsp;to&nbsp;the&nbsp;following:&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;strings&nbsp;=&nbsp;backtrace_symbols(buffer,&nbsp;nptrs);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(strings&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;backtrace_symbols&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;nptrs;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s\n&quot;,&nbsp;strings[j]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(strings);
}
<P>
static void /* &quot;static&quot; means don't export the symbol... */
myfunc2(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;myfunc3();
}
<P>
void
myfunc(int ncalls)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(ncalls&nbsp;&gt;&nbsp;1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfunc(ncalls&nbsp;-&nbsp;1);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfunc2();
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;%s&nbsp;num-calls\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;myfunc(atoi(argv[1]));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</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">&nbsp;</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">&nbsp;</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>