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 need to be
195 understood by race detectors so that they do not produce false reports.
199 Two annotation macros are used to explain low-level synchronization
201 <code>_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and
202 <code> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>.
203 By default, these macros are defined empty -- anyone who wants
204 to use a race detector needs to redefine them to call an
206 Since these macros are empty by default when the library is built,
207 redefining them will only affect inline functions and template
208 instantiations which are compiled in user code. This allows annotation
209 of templates such as <code>shared_ptr</code>, but not code which is
210 only instantiated in the library. Code which is only instantiated in
211 the library needs to be recompiled with the annotation macros defined.
212 That can be done by rebuilding the entire
213 <filename class="libraryfile">libstdc++.so</filename> file but a simpler
214 alternative exists for ELF platforms such as GNU/Linux, because ELF
215 symbol interposition allows symbols defined in the shared library to be
216 overridden by symbols with the same name that appear earlier in the
217 runtime search path. This means you only need to recompile the functions
218 that are affected by the annotation macros, which can be done by
219 recompiling individual files.
220 Annotating <code>std::string</code> and <code>std::wstring</code>
221 reference counting can be done by disabling extern templates (by defining
222 <code>_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or by rebuilding the
223 <filename>src/string-inst.cc</filename> file.
224 Annotating the remaining atomic operations (at the time of writing these
225 are in <code>ios_base::Init::~Init</code>, <code>locale::_Impl</code>,
226 <code>locale::facet</code> and <code>thread::_M_start_thread</code>)
227 requires rebuilding the relevant source files.
231 The approach described above is known to work with the following race
233 <link xmlns:xlink="http://www.w3.org/1999/xlink"
234 xlink:href="http://valgrind.org/docs/manual/drd-manual.html">
236 <link xmlns:xlink="http://www.w3.org/1999/xlink"
237 xlink:href="http://valgrind.org/docs/manual/hg-manual.html">
239 <link xmlns:xlink="http://www.w3.org/1999/xlink"
240 xlink:href="http://code.google.com/p/data-race-test">
241 ThreadSanitizer</link>.
245 With DRD, Helgrind and ThreadSanitizer you will need to define
246 the macros like this:
248 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
249 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A)
251 Refer to the documentation of each particular tool for details.
256 <section xml:id="debug.gdb"><info><title>Using <command>gdb</command></title></info>
262 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/">
263 "GDB features for C++" </link> in the GDB documentation. Also
264 recommended: the other parts of this manual.
268 These settings can either be switched on in at the GDB command line,
269 or put into a .gdbint file to establish default debugging
270 characteristics, like so:
276 set print static-members on
278 set print demangle on
279 set demangle-style gnu-v3
283 Starting with version 7.0, GDB includes support for writing
284 pretty-printers in Python. Pretty printers for STL classes are
285 distributed with GCC from version 4.5.0. The most recent version of
286 these printers are always found in libstdc++ svn repository.
287 To enable these printers, check-out the latest printers to a local
292 svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
296 Next, add the following section to your ~/.gdbinit The path must
297 match the location where the Python module above was checked-out.
298 So if checked out to: /home/maude/gdb_printers/, the path would be as
299 written in the example below.
305 sys.path.insert(0, '/home/maude/gdb_printers/python')
306 from libstdcxx.v6.printers import register_libstdcxx_printers
307 register_libstdcxx_printers (None)
312 The path should be the only element that needs to be adjusted in the
313 example. Once loaded, STL classes that the printers support
314 should print in a more human-readable format. To print the classes
315 in the old style, use the /r (raw) switch in the print command
316 (i.e., print /r foo). This will print the classes as if the Python
317 pretty-printers were not loaded.
321 For additional information on STL support and GDB please visit:
322 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/wiki/STLSupport"> "GDB Support
323 for STL" </link> in the GDB wiki. Additionally, in-depth
324 documentation and discussion of the pretty printing feature can be
325 found in "Pretty Printing" node in the GDB manual. You can find
326 on-line versions of the GDB user manual in GDB's homepage, at
327 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/"> "GDB: The GNU Project
333 <section xml:id="debug.exceptions"><info><title>Tracking uncaught exceptions</title></info>
336 The <link linkend="support.termination.verbose">verbose
337 termination handler</link> gives information about uncaught
338 exceptions which are killing the program. It is described in the
343 <section xml:id="debug.debug_mode"><info><title>Debug Mode</title></info>
345 <para> The <link linkend="manual.ext.debug_mode">Debug Mode</link>
346 has compile and run-time checks for many containers.
350 <section xml:id="debug.compile_time_checks"><info><title>Compile Time Checking</title></info>
352 <para> The <link linkend="manual.ext.compile_checks">Compile-Time
353 Checks</link> Extension has compile-time checks for many algorithms.
357 <section xml:id="debug.profile_mode" xreflabel="debug.profile_mode"><info><title>Profile-based Performance Analysis</title></info>
359 <para> The <link linkend="manual.ext.profile_mode">Profile-based
360 Performance Analysis</link> Extension has performance checks for many