-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktraces.html
More file actions
179 lines (153 loc) · 7.85 KB
/
Backtraces.html
File metadata and controls
179 lines (153 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<html lang="en">
<head>
<title>Backtraces - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.9">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Debugging-Support.html#Debugging-Support" title="Debugging Support">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.11, last updated 2007-09-09,
of `The GNU C Library Reference Manual', for version 2.7.
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Backtraces"></a>
Up: <a rel="up" accesskey="u" href="Debugging-Support.html#Debugging-Support">Debugging Support</a>
<hr>
</div>
<h3 class="section">33.1 Backtraces</h3>
<p><a name="index-backtrace-3694"></a><a name="index-backtrace_005fsymbols-3695"></a><a name="index-backtrace_005ffd-3696"></a>A <dfn>backtrace</dfn> is a list of the function calls that are currently
active in a thread. The usual way to inspect a backtrace of a program
is to use an external debugger such as gdb. However, sometimes it is
useful to obtain a backtrace programmatically from within a program,
e.g., for the purposes of logging or diagnostics.
<p>The header file <samp><span class="file">execinfo.h</span></samp> declares three functions that obtain
and manipulate backtraces of the current thread.
<a name="index-execinfo_002eh-3697"></a>
<!-- execinfo.h -->
<!-- GNU -->
<div class="defun">
— Function: int <b>backtrace</b> (<var>void **buffer, int size</var>)<var><a name="index-backtrace-3698"></a></var><br>
<blockquote><p>The <code>backtrace</code> function obtains a backtrace for the current
thread, as a list of pointers, and places the information into
<var>buffer</var>. The argument <var>size</var> should be the number of
<code>void *</code><!-- /@w --> elements that will fit into <var>buffer</var>. The return
value is the actual number of entries of <var>buffer</var> that are obtained,
and is at most <var>size</var>.
<p>The pointers placed in <var>buffer</var> are actually return addresses
obtained by inspecting the stack, one return address per stack frame.
<p>Note that certain compiler optimizations may interfere with obtaining a
valid backtrace. Function inlining causes the inlined function to not
have a stack frame; tail call optimization replaces one stack frame with
another; frame pointer elimination will stop <code>backtrace</code> from
interpreting the stack contents correctly.
</p></blockquote></div>
<!-- execinfo.h -->
<!-- GNU -->
<div class="defun">
— Function: char ** <b>backtrace_symbols</b> (<var>void *const *buffer, int size</var>)<var><a name="index-backtrace_005fsymbols-3699"></a></var><br>
<blockquote><p>The <code>backtrace_symbols</code> function translates the information
obtained from the <code>backtrace</code> function into an array of strings.
The argument <var>buffer</var> should be a pointer to an array of addresses
obtained via the <code>backtrace</code> function, and <var>size</var> is the number
of entries in that array (the return value of <code>backtrace</code>).
<p>The return value is a pointer to an array of strings, which has
<var>size</var> entries just like the array <var>buffer</var>. Each string
contains a printable representation of the corresponding element of
<var>buffer</var>. It includes the function name (if this can be
determined), an offset into the function, and the actual return address
(in hexadecimal).
<p>Currently, the function name and offset only be obtained on systems that
use the ELF binary format for programs and libraries. On other systems,
only the hexadecimal return address will be present. Also, you may need
to pass additional flags to the linker to make the function names
available to the program. (For example, on systems using GNU ld, you
must pass (<code>-rdynamic</code>.)
<p>The return value of <code>backtrace_symbols</code> is a pointer obtained via
the <code>malloc</code> function, and it is the responsibility of the caller
to <code>free</code> that pointer. Note that only the return value need be
freed, not the individual strings.
<p>The return value is <code>NULL</code> if sufficient memory for the strings
cannot be obtained.
</p></blockquote></div>
<!-- execinfo.h -->
<!-- GNU -->
<div class="defun">
— Function: void <b>backtrace_symbols_fd</b> (<var>void *const *buffer, int size, int fd</var>)<var><a name="index-backtrace_005fsymbols_005ffd-3700"></a></var><br>
<blockquote><p>The <code>backtrace_symbols_fd</code> function performs the same translation
as the function <code>backtrace_symbols</code> function. Instead of returning
the strings to the caller, it writes the strings to the file descriptor
<var>fd</var>, one per line. It does not use the <code>malloc</code> function, and
can therefore be used in situations where that function might fail.
</p></blockquote></div>
<p>The following program illustrates the use of these functions. Note that
the array to contain the return addresses returned by <code>backtrace</code>
is allocated on the stack. Therefore code like this can be used in
situations where the memory handling via <code>malloc</code> does not work
anymore (in which case the <code>backtrace_symbols</code> has to be replaced
by a <code>backtrace_symbols_fd</code> call as well). The number of return
addresses is normally not very large. Even complicated programs rather
seldom have a nesting level of more than, say, 50 and with 200 possible
entries probably all programs should be covered.
<pre class="smallexample"> #include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
/* <span class="roman">Obtain a backtrace and print it to </span><code>stdout</code><span class="roman">.</span> */
void
print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
printf ("Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
printf ("%s\n", strings[i]);
free (strings);
}
/* <span class="roman">A dummy function to make the backtrace more interesting.</span> */
void
dummy_function (void)
{
print_trace ();
}
int
main (void)
{
dummy_function ();
return 0;
}
</pre>
<!-- This node must have no pointers. -->
</body></html>