1 <?xml version=
"1.0" encoding=
"ISO-8859-1"?>
3 PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
8 <meta name=
"AUTHOR" content=
"bkoz@gcc.gnu.org (Benjamin Kosnik)" />
9 <meta name=
"KEYWORDS" content=
"c++, libstdc++, gdb, g++, debug" />
10 <meta name=
"DESCRIPTION" content=
"Debugging C++ binaries" />
11 <meta name=
"GENERATOR" content=
"vi and ten fingers" />
12 <title>Debugging schemes and strategies
</title>
13 <link rel=
"StyleSheet" href=
"lib3styles.css" />
17 <h1 class=
"centered"><a name=
"top">Debugging schemes and strategies
</a></h1>
19 <p class=
"fineprint"><em>
20 The latest version of this document is always available at
21 <a href=
"http://gcc.gnu.org/onlinedocs/libstdc++/debug.html">
22 http://gcc.gnu.org/onlinedocs/libstdc++/debug.html
</a>.
26 To the
<a href=
"http://gcc.gnu.org/libstdc++/">libstdc++-v3 homepage
</a>.
29 <!-- ####################################################### -->
31 <p>There are numerous things that can be done to improve the ease with
32 which C++ binaries are debugged when using the GNU C++
33 tool chain. Here are some things to keep in mind when debugging C++
37 <h3 class=
"left"><a name=
"gplusplus">Compiler flags determine debug info
</a></h3>
38 <p>The default optimizations and debug flags for a libstdc++ build are
39 <code>-g -O2
</code>. However, both debug and optimization flags can
40 be varied to change debugging characteristics. For instance,
41 turning off all optimization via the
<code>-g -O0
</code> flag will
42 disable inlining, so that stepping through all functions, including
43 inlined constructors and destructors, is possible. Or, the debug
44 format that the compiler and debugger use to communicate
45 information about source constructs can be changed via
<code>
46 -gdwarf-
2 </code> or
<code> -gstabs
</code> flags: some debugging
47 formats permit more expressive type and scope information to be
49 The default debug information for a particular platform can be
50 identified via the value set by the PREFERRED_DEBUGGING_TYPE macro
54 <p>Many other options are available: please see
55 <a href=
"http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging%20Options">"Options for Debugging Your Program"</a>
56 in Using the GNU Compiler Collection (GCC) for a complete list.
60 <h3 class=
"left"><a name=
"lib">Using special flags to make a debug binary
</a></h3>
61 <p>There are two ways to build libstdc++ with debug flags. The first
62 is to run make from the toplevel in a freshly-configured tree with
63 specialized debug
<code>CXXFLAGS
</code>, as in
66 make CXXFLAGS='-g3 -O0' all
69 <p>This quick and dirty approach is often sufficient for quick
70 debugging tasks, but the lack of state can be confusing in the long
73 <p>A second approach is to use the configuration flags
80 --enable-debug-flags='...'
82 <p>to create a separate debug build. Both the normal build and the
83 debug build will persist, without having to specify
84 <code>CXXFLAGS
</code>, and the debug library will be installed in a
85 separate directory tree, in
<code>(prefix)/lib/debug
</code>. For
86 more information, look at the
<a href=
"configopts.html">configuration
91 <h3 class=
"left"><a name=
"mem">Tips for memory leak hunting
</a></h3>
93 <p>There are various third party memory tracing and debug utilities
94 that can be used to provide detailed memory allocation information
95 about C++ code. An exhaustive list of tools is not going to be
96 attempted, but includes
<code>mtrace
</code>,
<code>valgrind
</code>,
97 <code>mudflap
</code>, and
<code>purify
</code>. Also highly
98 recommended are
<code>libcwd
</code> and some other one that I
102 <p>Regardless of the memory debugging tool being used, there is one
103 thing of great importance to keep in mind when debugging C++ code
104 that uses
<code>new
</code> and
<code>delete
</code>:
105 there are different kinds of allocation schemes that can be used by
106 <code> std::allocator
</code>. For implementation details, see this
107 <a href=
"ext/howto.html#3"> document
</a> and look specifically for
108 <code>GLIBCPP_FORCE_NEW
</code>.
111 <p>In a nutshell, the default allocator used by
<code>
112 std::allocator
</code> is a high-performance pool allocator, and can
113 give the mistaken impression that memory is being leaked, when in
114 reality the memory is still being used by the library and is reclaimed
115 after program termination.
118 <p>For valgrind, there are some specific items to keep in mind. First
119 of all, use a version of valgrind that will work with current GNU
120 C++ tools: the first that can do this is valgrind
1.0.4, but later
121 versions should work at least as well. Second of all, use a
122 completely unoptimized build to avoid confusing valgrind. Third,
123 use GLIBCPP_FORCE_NEW to keep extraneous pool allocation noise from
124 cluttering debug information.
127 <p>Fourth, it may be necessary to force deallocation in other
128 libraries as well, namely the
"C" library. On linux, this can be
129 accomplished with the appropriate use of the
130 <code>__cxa_atexit
</code> or
<code>atexit
</code> functions.
134 #include
<cstdlib
>
136 extern
"C" void __libc_freeres(void);
138 void do_something() { }
142 atexit(__libc_freeres);
149 <p>or, using
<code>__cxa_atexit
</code>:
</p>
152 extern
"C" void __libc_freeres(void);
153 extern
"C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
155 void do_something() { }
159 extern void* __dso_handle __attribute__ ((__weak__));
160 __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
161 &__dso_handle ? __dso_handle : NULL);
167 <p>Suggested valgrind flags, given the suggestions above about setting
168 up the runtime environment, library, and test file, might be:
171 valgrind -v --num-callers=
20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
175 <h3 class=
"left"><a name=
"gdb">Some gdb strategies
</a></h3>
176 <p>Many options are available for gdb itself: please see
<a
177 href=
"http://sources.redhat.com/gdb/current/onlinedocs/gdb_13.html#SEC109">
178 "GDB features for C++" </a> in the gdb documentation. Also
179 recommended: the other parts of this manual.
182 <p>These settings can either be switched on in at the gdb command
183 line, or put into a .gdbint file to establish default debugging
184 characteristics, like so:
190 set print static-members on
192 set print demangle on
193 set demangle-style gnu-v3
197 <h3 class=
"left"><a name=
"verbterm">Tracking uncaught exceptions
</a></h3>
198 <p>The
<a href=
"19_diagnostics/howto.html#4">verbose termination handler
</a>
199 gives information about uncaught exceptions which are killing the
200 program. It is described in the linked-to page.
204 <p>Return
<a href=
"#top">to the top of the page
</a> or
205 <a href=
"http://gcc.gnu.org/libstdc++/">to the libstdc++ homepage
</a>.
209 <!-- ####################################################### -->
212 <p class=
"fineprint"><em>
213 See
<a href=
"17_intro/license.html">license.html
</a> for copying conditions.
214 Comments and suggestions are welcome, and may be sent to
215 <a href=
"mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list
</a>.