1 <section xmlns="http://docbook.org/ns/docbook" version="5.0"
2 xml:id="manual.intro.using.debug" xreflabel="Debugging Support">
3 <?dbhtml filename="debug.html"?>
5 <info><title>Debugging Support</title>
19 There are numerous things that can be done to improve the ease with
20 which C++ binaries are debugged when using the GNU tool chain. Here
24 <section xml:id="debug.compiler"><info><title>Using <command>g++</command></title></info>
27 Compiler flags determine how debug information is transmitted
28 between compilation and debug or analysis tools.
32 The default optimizations and debug flags for a libstdc++ build
33 are <code>-g -O2</code>. However, both debug and optimization
34 flags can be varied to change debugging characteristics. For
35 instance, turning off all optimization via the <code>-g -O0
36 -fno-inline</code> flags will disable inlining and optimizations,
37 and add debugging information, so that stepping through all functions,
38 (including inlined constructors and destructors) is possible. In
39 addition, <code>-fno-eliminate-unused-debug-types</code> can be
40 used when additional debug information, such as nested class info,
45 Or, the debug format that the compiler and debugger use to
46 communicate information about source constructs can be changed via
47 <code>-gdwarf-2</code> or <code>-gstabs</code> flags: some debugging
48 formats permit more expressive type and scope information to be
49 shown in GDB. Expressiveness can be enhanced by flags like
50 <code>-g3</code>. The default debug information for a particular
51 platform can be identified via the value set by the
52 PREFERRED_DEBUGGING_TYPE macro in the gcc sources.
56 Many other options are available: please see <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging%20Options">"Options
57 for Debugging Your Program"</link> in Using the GNU Compiler
58 Collection (GCC) for a complete list.
62 <section xml:id="debug.req"><info><title>Debug Versions of Library Binary Files</title></info>
66 If you would like debug symbols in libstdc++, there are two ways to
67 build libstdc++ with debug flags. The first is to run make from the
68 toplevel in a freshly-configured tree with
71 --enable-libstdcxx-debug
73 <para>and perhaps</para>
75 --enable-libstdcxx-debug-flags='...'
78 to create a separate debug build. Both the normal build and the
79 debug build will persist, without having to specify
80 <code>CXXFLAGS</code>, and the debug library will be installed in a
81 separate directory tree, in <code>(prefix)/lib/debug</code>. For
82 more information, look at the <link linkend="manual.intro.setup.configure">configuration</link> section.
86 A second approach is to use the configuration flags
89 make CXXFLAGS='-g3 -fno-inline -O0' all
93 This quick and dirty approach is often sufficient for quick
94 debugging tasks, when you cannot or don't want to recompile your
95 application to use the <link linkend="manual.ext.debug_mode">debug mode</link>.</para>
98 <section xml:id="debug.memory"><info><title>Memory Leak Hunting</title></info>
102 There are various third party memory tracing and debug utilities
103 that can be used to provide detailed memory allocation information
104 about C++ code. An exhaustive list of tools is not going to be
105 attempted, but includes <code>mtrace</code>, <code>valgrind</code>,
106 <code>mudflap</code>, and the non-free commercial product
107 <code>purify</code>. In addition, <code>libcwd</code> has a
108 replacement for the global new and delete operators that can track
109 memory allocation and deallocation and provide useful memory
114 Regardless of the memory debugging tool being used, there is one
115 thing of great importance to keep in mind when debugging C++ code
116 that uses <code>new</code> and <code>delete</code>: there are
117 different kinds of allocation schemes that can be used by <code>
118 std::allocator </code>. For implementation details, see the <link linkend="manual.ext.allocator.mt">mt allocator</link> documentation and
119 look specifically for <code>GLIBCXX_FORCE_NEW</code>.
123 In a nutshell, the default allocator used by <code>
124 std::allocator</code> is a high-performance pool allocator, and can
125 give the mistaken impression that in a suspect executable, memory is
126 being leaked, when in reality the memory "leak" is a pool being used
127 by the library's allocator and is reclaimed after program
132 For valgrind, there are some specific items to keep in mind. First
133 of all, use a version of valgrind that will work with current GNU
134 C++ tools: the first that can do this is valgrind 1.0.4, but later
135 versions should work at least as well. Second of all, use a
136 completely unoptimized build to avoid confusing valgrind. Third, use
137 GLIBCXX_FORCE_NEW to keep extraneous pool allocation noise from
138 cluttering debug information.
142 Fourth, it may be necessary to force deallocation in other libraries
143 as well, namely the "C" library. On linux, this can be accomplished
144 with the appropriate use of the <code>__cxa_atexit</code> or
145 <code>atexit</code> functions.
149 #include <cstdlib>
151 extern "C" void __libc_freeres(void);
153 void do_something() { }
157 atexit(__libc_freeres);
164 <para>or, using <code>__cxa_atexit</code>:</para>
167 extern "C" void __libc_freeres(void);
168 extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
170 void do_something() { }
174 extern void* __dso_handle __attribute__ ((__weak__));
175 __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
176 &__dso_handle ? __dso_handle : NULL);
183 Suggested valgrind flags, given the suggestions above about setting
184 up the runtime environment, library, and test file, might be:
187 valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
192 <section xml:id="debug.races"><info><title>Data Race Hunting</title></info>
194 All synchronization primitives used in the library internals should be
195 understood by race detectors so that they do not produce false reports.
199 We use two annotations (macros) to explain low-level synchronization
201 <code>_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and
202 <code> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>.
203 By default, these two macros are defined empty -- anyone who wants
204 to use a race detector will need to redefine these macros to call an
206 Since these macros are empty by default, redefining them in the user code
207 will affect only the inline template code, e.g. <code>shared_ptr</code>.
208 In order to redefine the macros in <code>basic_string</code> one will
209 need to disable extern templates (by defining
210 <code>_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or rebuild the
211 <code>.so</code> file.
212 The rest of the cases (currently, <code>ios_base::Init::~Init</code>,
213 <code>locale::_Impl</code> and <code>locale::facet</code>) will require
214 to rebuild the <code>.so</code> file.
218 The approach described above works at least with the following race
220 <link xmlns:xlink="http://www.w3.org/1999/xlink"
221 xlink:href="http://valgrind.org/docs/manual/drd-manual.html">
223 <link xmlns:xlink="http://www.w3.org/1999/xlink"
224 xlink:href="http://valgrind.org/docs/manual/hg-manual.html">
226 <link xmlns:xlink="http://www.w3.org/1999/xlink"
227 xlink:href="http://code.google.com/p/data-race-test">
228 ThreadSanitizer </link>.
232 With DRD, Helgrind and ThreadSanitizer you will need to define
233 the macros like this:
235 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
236 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A)
238 Refer to the documentation of each particular tool for the details.
243 <section xml:id="debug.gdb"><info><title>Using <command>gdb</command></title></info>
249 Many options are available for GDB itself: please see <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sources.redhat.com/gdb/current/onlinedocs/gdb/">
250 "GDB features for C++" </link> in the GDB documentation. Also
251 recommended: the other parts of this manual.
255 These settings can either be switched on in at the GDB command line,
256 or put into a .gdbint file to establish default debugging
257 characteristics, like so:
263 set print static-members on
265 set print demangle on
266 set demangle-style gnu-v3
270 Starting with version 7.0, GDB includes support for writing
271 pretty-printers in Python. Pretty printers for STL classes are
272 distributed with GCC from version 4.5.0. The most recent version of
273 these printers are always found in libstdc++ svn repository.
274 To enable these printers, check-out the latest printers to a local
279 svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
283 Next, add the following section to your ~/.gdbinit The path must
284 match the location where the Python module above was checked-out.
285 So if checked out to: /home/maude/gdb_printers/, the path would be as
286 written in the example below.
292 sys.path.insert(0, '/home/maude/gdb_printers/python')
293 from libstdcxx.v6.printers import register_libstdcxx_printers
294 register_libstdcxx_printers (None)
299 The path should be the only element that needs to be adjusted in the
300 example. Once loaded, STL classes that the printers support
301 should print in a more human-readable format. To print the classes
302 in the old style, use the /r (raw) switch in the print command
303 (i.e., print /r foo). This will print the classes as if the Python
304 pretty-printers were not loaded.
308 For additional information on STL support and GDB please visit:
309 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/wiki/STLSupport"> "GDB Support
310 for STL" </link> in the GDB wiki. Additionally, in-depth
311 documentation and discussion of the pretty printing feature can be
312 found in "Pretty Printing" node in the GDB manual. You can find
313 on-line versions of the GDB user manual in GDB's homepage, at
314 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/"> "GDB: The GNU Project
320 <section xml:id="debug.exceptions"><info><title>Tracking uncaught exceptions</title></info>
323 The <link linkend="support.termination.verbose">verbose
324 termination handler</link> gives information about uncaught
325 exceptions which are killing the program. It is described in the
330 <section xml:id="debug.debug_mode"><info><title>Debug Mode</title></info>
332 <para> The <link linkend="manual.ext.debug_mode">Debug Mode</link>
333 has compile and run-time checks for many containers.
337 <section xml:id="debug.compile_time_checks"><info><title>Compile Time Checking</title></info>
339 <para> The <link linkend="manual.ext.compile_checks">Compile-Time
340 Checks</link> Extension has compile-time checks for many algorithms.
344 <section xml:id="debug.profile_mode" xreflabel="debug.profile_mode"><info><title>Profile-based Performance Analysis</title></info>
346 <para> The <link linkend="manual.ext.profile_mode">Profile-based
347 Performance Analysis</link> Extension has performance checks for many