136ac495dSmrg<?xml version="1.0" encoding="UTF-8" standalone="no"?> 236ac495dSmrg<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Debugging Support</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="C++, debug" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="using.html" title="Chapter 3. Using" /><link rel="prev" href="using_exceptions.html" title="Exceptions" /><link rel="next" href="std_contents.html" title="Part II. Standard Contents" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Debugging Support</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="using_exceptions.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Using</th><td width="20%" align="right"> <a accesskey="n" href="std_contents.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.intro.using.debug"></a>Debugging Support</h2></div></div></div><p> 336ac495dSmrg There are numerous things that can be done to improve the ease with 436ac495dSmrg which C++ binaries are debugged when using the GNU tool chain. Here 536ac495dSmrg are some of them. 636ac495dSmrg</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.compiler"></a>Using <span class="command"><strong>g++</strong></span></h3></div></div></div><p> 736ac495dSmrg Compiler flags determine how debug information is transmitted 836ac495dSmrg between compilation and debug or analysis tools. 936ac495dSmrg </p><p> 1036ac495dSmrg The default optimizations and debug flags for a libstdc++ build 1136ac495dSmrg are <code class="code">-g -O2</code>. However, both debug and optimization 1236ac495dSmrg flags can be varied to change debugging characteristics. For 1336ac495dSmrg instance, turning off all optimization via the <code class="code">-g -O0 1436ac495dSmrg -fno-inline</code> flags will disable inlining and optimizations, 1536ac495dSmrg and add debugging information, so that stepping through all functions, 1636ac495dSmrg (including inlined constructors and destructors) is possible. In 1736ac495dSmrg addition, <code class="code">-fno-eliminate-unused-debug-types</code> can be 1836ac495dSmrg used when additional debug information, such as nested class info, 1936ac495dSmrg is desired. 2036ac495dSmrg</p><p> 2136ac495dSmrg Or, the debug format that the compiler and debugger use to 2236ac495dSmrg communicate information about source constructs can be changed via 2336ac495dSmrg <code class="code">-gdwarf-2</code> or <code class="code">-gstabs</code> flags: some debugging 2436ac495dSmrg formats permit more expressive type and scope information to be 2536ac495dSmrg shown in GDB. Expressiveness can be enhanced by flags like 2636ac495dSmrg <code class="code">-g3</code>. The default debug information for a particular 2736ac495dSmrg platform can be identified via the value set by the 2836ac495dSmrg PREFERRED_DEBUGGING_TYPE macro in the GCC sources. 2936ac495dSmrg</p><p> 3036ac495dSmrg Many other options are available: please see <a class="link" href="http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging%20Options" target="_top">"Options 3136ac495dSmrg for Debugging Your Program"</a> in Using the GNU Compiler 3236ac495dSmrg Collection (GCC) for a complete list. 3336ac495dSmrg</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.req"></a>Debug Versions of Library Binary Files</h3></div></div></div><p> 3436ac495dSmrg If you would like debug symbols in libstdc++, there are two ways to 3536ac495dSmrg build libstdc++ with debug flags. The first is to create a separate 3636ac495dSmrg debug build by running make from the top-level of a tree 3736ac495dSmrg freshly-configured with 3836ac495dSmrg</p><pre class="programlisting"> 3936ac495dSmrg --enable-libstdcxx-debug 4036ac495dSmrg</pre><p>and perhaps</p><pre class="programlisting"> 4136ac495dSmrg --enable-libstdcxx-debug-flags='...' 4236ac495dSmrg</pre><p> 4336ac495dSmrg Both the normal build and the debug build will persist, without 4436ac495dSmrg having to specify <code class="code">CXXFLAGS</code>, and the debug library will 4536ac495dSmrg be installed in a separate directory tree, in <code class="code">(prefix)/lib/debug</code>. 4636ac495dSmrg For more information, look at the 4736ac495dSmrg <a class="link" href="configure.html" title="Configure">configuration</a> section. 4836ac495dSmrg</p><p> 4936ac495dSmrg A second approach is to use the configuration flags 5036ac495dSmrg</p><pre class="programlisting"> 5136ac495dSmrg make CXXFLAGS='-g3 -fno-inline -O0' all 5236ac495dSmrg</pre><p> 5336ac495dSmrg This quick and dirty approach is often sufficient for quick 5436ac495dSmrg debugging tasks, when you cannot or don't want to recompile your 5536ac495dSmrg application to use the <a class="link" href="debug_mode.html" title="Chapter 17. Debug Mode">debug mode</a>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.memory"></a>Memory Leak Hunting</h3></div></div></div><p> 56a2dc1f3fSmrg On many targets GCC supports AddressSanitizer, a fast memory error detector, 57a2dc1f3fSmrg which is enabled by the <code class="option">-fsanitize=address</code> option. 58a2dc1f3fSmrg</p><p> 59a2dc1f3fSmrg There are also various third party memory tracing and debug utilities 6036ac495dSmrg that can be used to provide detailed memory allocation information 6136ac495dSmrg about C++ code. An exhaustive list of tools is not going to be 6236ac495dSmrg attempted, but includes <code class="code">mtrace</code>, <code class="code">valgrind</code>, 63a2dc1f3fSmrg <code class="code">mudflap</code> (no longer supported since GCC 4.9.0), ElectricFence, 64a2dc1f3fSmrg and the non-free commercial product <code class="code">purify</code>. 65a2dc1f3fSmrg In addition, <code class="code">libcwd</code>, jemalloc and TCMalloc have replacements 66a2dc1f3fSmrg for the global <code class="code">new</code> and <code class="code">delete</code> operators 67a2dc1f3fSmrg that can track memory allocation and deallocation and provide useful 68a2dc1f3fSmrg memory statistics. 6936ac495dSmrg</p><p> 7036ac495dSmrg For valgrind, there are some specific items to keep in mind. First 7136ac495dSmrg of all, use a version of valgrind that will work with current GNU 7236ac495dSmrg C++ tools: the first that can do this is valgrind 1.0.4, but later 73a2dc1f3fSmrg versions should work better. Second, using an unoptimized build 74a2dc1f3fSmrg might avoid confusing valgrind. 7536ac495dSmrg</p><p> 76a2dc1f3fSmrg Third, it may be necessary to force deallocation in other libraries 77a2dc1f3fSmrg as well, namely the "C" library. On GNU/Linux, this can be accomplished 7836ac495dSmrg with the appropriate use of the <code class="code">__cxa_atexit</code> or 7936ac495dSmrg <code class="code">atexit</code> functions. 8036ac495dSmrg</p><pre class="programlisting"> 8136ac495dSmrg #include <cstdlib> 8236ac495dSmrg 8336ac495dSmrg extern "C" void __libc_freeres(void); 8436ac495dSmrg 8536ac495dSmrg void do_something() { } 8636ac495dSmrg 8736ac495dSmrg int main() 8836ac495dSmrg { 8936ac495dSmrg atexit(__libc_freeres); 9036ac495dSmrg do_something(); 9136ac495dSmrg return 0; 9236ac495dSmrg } 9336ac495dSmrg</pre><p>or, using <code class="code">__cxa_atexit</code>:</p><pre class="programlisting"> 9436ac495dSmrg extern "C" void __libc_freeres(void); 9536ac495dSmrg extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d); 9636ac495dSmrg 9736ac495dSmrg void do_something() { } 9836ac495dSmrg 9936ac495dSmrg int main() 10036ac495dSmrg { 10136ac495dSmrg extern void* __dso_handle __attribute__ ((__weak__)); 10236ac495dSmrg __cxa_atexit((void (*) (void *)) __libc_freeres, NULL, 10336ac495dSmrg &__dso_handle ? __dso_handle : NULL); 10436ac495dSmrg do_test(); 10536ac495dSmrg return 0; 10636ac495dSmrg } 10736ac495dSmrg</pre><p> 10836ac495dSmrg Suggested valgrind flags, given the suggestions above about setting 10936ac495dSmrg up the runtime environment, library, and test file, might be: 11036ac495dSmrg</p><pre class="programlisting"> 11136ac495dSmrg valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out 112a2dc1f3fSmrg</pre><div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="debug.memory.mtalloc"></a>Non-memory leaks in Pool and MT allocators</h4></div></div></div><p> 113a2dc1f3fSmrg There are different kinds of allocation schemes that can be used by 114a2dc1f3fSmrg <code class="code">std::allocator</code>. Prior to GCC 3.4.0 the default was to use 115a2dc1f3fSmrg a pooling allocator, <code class="classname">pool_allocator</code>, 116a2dc1f3fSmrg which is still available as the optional 117a2dc1f3fSmrg <code class="classname">__pool_alloc</code> extension. 118a2dc1f3fSmrg Another optional extension, <code class="classname">__mt_alloc</code>, 119a2dc1f3fSmrg is a high-performance pool allocator. 120a2dc1f3fSmrg</p><p> 121a2dc1f3fSmrg In a suspect executable these pooling allocators can give 122a2dc1f3fSmrg the mistaken impression that memory is being leaked, 123a2dc1f3fSmrg when in reality the memory "leak" is a pool being used 124a2dc1f3fSmrg by the library's allocator and is reclaimed after program 125a2dc1f3fSmrg termination. 126a2dc1f3fSmrg</p><p> 127a2dc1f3fSmrg If you're using memory debugging tools on a program that uses 128a2dc1f3fSmrg one of these pooling allocators, you can set the environment variable 129a2dc1f3fSmrg <code class="literal">GLIBCXX_FORCE_NEW</code> to keep extraneous pool allocation 130a2dc1f3fSmrg noise from cluttering debug information. 131a2dc1f3fSmrg For more details, see the 132*8feb0f0bSmrg <a class="link" href="mt_allocator.html" title="Chapter 19. The mt_allocator">mt allocator</a> 133a2dc1f3fSmrg documentation and look specifically for <code class="code">GLIBCXX_FORCE_NEW</code>. 134a2dc1f3fSmrg</p></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.races"></a>Data Race Hunting</h3></div></div></div><p> 13536ac495dSmrg All synchronization primitives used in the library internals need to be 13636ac495dSmrg understood by race detectors so that they do not produce false reports. 13736ac495dSmrg</p><p> 13836ac495dSmrg Two annotation macros are used to explain low-level synchronization 13936ac495dSmrg to race detectors: 14036ac495dSmrg <code class="code">_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and 14136ac495dSmrg <code class="code"> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>. 14236ac495dSmrg By default, these macros are defined empty -- anyone who wants 14336ac495dSmrg to use a race detector needs to redefine them to call an 14436ac495dSmrg appropriate API. 14536ac495dSmrg Since these macros are empty by default when the library is built, 14636ac495dSmrg redefining them will only affect inline functions and template 14736ac495dSmrg instantiations which are compiled in user code. This allows annotation 14836ac495dSmrg of templates such as <code class="code">shared_ptr</code>, but not code which is 14936ac495dSmrg only instantiated in the library. Code which is only instantiated in 15036ac495dSmrg the library needs to be recompiled with the annotation macros defined. 15136ac495dSmrg That can be done by rebuilding the entire 15236ac495dSmrg <code class="filename">libstdc++.so</code> file but a simpler 15336ac495dSmrg alternative exists for ELF platforms such as GNU/Linux, because ELF 15436ac495dSmrg symbol interposition allows symbols defined in the shared library to be 15536ac495dSmrg overridden by symbols with the same name that appear earlier in the 15636ac495dSmrg runtime search path. This means you only need to recompile the functions 15736ac495dSmrg that are affected by the annotation macros, which can be done by 15836ac495dSmrg recompiling individual files. 15936ac495dSmrg Annotating <code class="code">std::string</code> and <code class="code">std::wstring</code> 16036ac495dSmrg reference counting can be done by disabling extern templates (by defining 16136ac495dSmrg <code class="code">_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or by rebuilding the 16236ac495dSmrg <code class="filename">src/string-inst.cc</code> file. 16336ac495dSmrg Annotating the remaining atomic operations (at the time of writing these 16436ac495dSmrg are in <code class="code">ios_base::Init::~Init</code>, <code class="code">locale::_Impl</code>, 16536ac495dSmrg <code class="code">locale::facet</code> and <code class="code">thread::_M_start_thread</code>) 16636ac495dSmrg requires rebuilding the relevant source files. 16736ac495dSmrg</p><p> 16836ac495dSmrg The approach described above is known to work with the following race 16936ac495dSmrg detection tools: 17036ac495dSmrg <a class="link" href="http://valgrind.org/docs/manual/drd-manual.html" target="_top"> 17136ac495dSmrg DRD</a>, 17236ac495dSmrg <a class="link" href="http://valgrind.org/docs/manual/hg-manual.html" target="_top"> 17336ac495dSmrg Helgrind</a>, and 17436ac495dSmrg <a class="link" href="https://github.com/google/sanitizers" target="_top"> 17536ac495dSmrg ThreadSanitizer</a> (this refers to ThreadSanitizer v1, not the 17636ac495dSmrg new "tsan" feature built-in to GCC itself). 17736ac495dSmrg</p><p> 17836ac495dSmrg With DRD, Helgrind and ThreadSanitizer you will need to define 17936ac495dSmrg the macros like this: 18036ac495dSmrg</p><pre class="programlisting"> 18136ac495dSmrg #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A) 18236ac495dSmrg #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A) 18336ac495dSmrg</pre><p> 18436ac495dSmrg Refer to the documentation of each particular tool for details. 18536ac495dSmrg</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.gdb"></a>Using <span class="command"><strong>gdb</strong></span></h3></div></div></div><p> 18636ac495dSmrg </p><p> 18736ac495dSmrg Many options are available for GDB itself: please see <a class="link" href="http://sourceware.org/gdb/current/onlinedocs/gdb/" target="_top"> 18836ac495dSmrg "GDB features for C++" </a> in the GDB documentation. Also 18936ac495dSmrg recommended: the other parts of this manual. 19036ac495dSmrg</p><p> 19136ac495dSmrg These settings can either be switched on in at the GDB command line, 19236ac495dSmrg or put into a <code class="filename">.gdbinit</code> file to establish default 19336ac495dSmrg debugging characteristics, like so: 19436ac495dSmrg</p><pre class="programlisting"> 19536ac495dSmrg set print pretty on 19636ac495dSmrg set print object on 19736ac495dSmrg set print static-members on 19836ac495dSmrg set print vtbl on 19936ac495dSmrg set print demangle on 20036ac495dSmrg set demangle-style gnu-v3 20136ac495dSmrg</pre><p> 20236ac495dSmrg Starting with version 7.0, GDB includes support for writing 20336ac495dSmrg pretty-printers in Python. Pretty printers for containers and other 20436ac495dSmrg classes are distributed with GCC from version 4.5.0 and should be installed 20536ac495dSmrg alongside the libstdc++ shared library files and found automatically by 20636ac495dSmrg GDB. 20736ac495dSmrg</p><p> 20836ac495dSmrg Depending where libstdc++ is installed, GDB might refuse to auto-load 20936ac495dSmrg the python printers and print a warning instead. 21036ac495dSmrg If this happens the python printers can be enabled by following the 21136ac495dSmrg instructions GDB gives for setting your <code class="code">auto-load safe-path</code> 21236ac495dSmrg in your <code class="filename">.gdbinit</code> configuration file. 21336ac495dSmrg</p><p> 21436ac495dSmrg Once loaded, standard library classes that the printers support 21536ac495dSmrg should print in a more human-readable format. To print the classes 21636ac495dSmrg in the old style, use the <strong class="userinput"><code>/r</code></strong> (raw) switch in the 21736ac495dSmrg print command (i.e., <strong class="userinput"><code>print /r foo</code></strong>). This will 21836ac495dSmrg print the classes as if the Python pretty-printers were not loaded. 21936ac495dSmrg</p><p> 22036ac495dSmrg For additional information on STL support and GDB please visit: 22136ac495dSmrg <a class="link" href="http://sourceware.org/gdb/wiki/STLSupport" target="_top"> "GDB Support 22236ac495dSmrg for STL" </a> in the GDB wiki. Additionally, in-depth 22336ac495dSmrg documentation and discussion of the pretty printing feature can be 22436ac495dSmrg found in "Pretty Printing" node in the GDB manual. You can find 22536ac495dSmrg on-line versions of the GDB user manual in GDB's homepage, at 22636ac495dSmrg <a class="link" href="http://sourceware.org/gdb/" target="_top"> "GDB: The GNU Project 22736ac495dSmrg Debugger" </a>. 22836ac495dSmrg</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.exceptions"></a>Tracking uncaught exceptions</h3></div></div></div><p> 22936ac495dSmrg The <a class="link" href="termination.html#support.termination.verbose" title="Verbose Terminate Handler">verbose 23036ac495dSmrg termination handler</a> gives information about uncaught 23136ac495dSmrg exceptions which kill the program. 23236ac495dSmrg</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.debug_mode"></a>Debug Mode</h3></div></div></div><p> The <a class="link" href="debug_mode.html" title="Chapter 17. Debug Mode">Debug Mode</a> 23336ac495dSmrg has compile and run-time checks for many containers. 23436ac495dSmrg </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.compile_time_checks"></a>Compile Time Checking</h3></div></div></div><p> The <a class="link" href="ext_compile_checks.html" title="Chapter 16. Compile Time Checks">Compile-Time 23536ac495dSmrg Checks</a> extension has compile-time checks for many algorithms. 23636ac495dSmrg </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="using_exceptions.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="using.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="std_contents.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Exceptions </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Part II. 23736ac495dSmrg Standard Contents 23836ac495dSmrg </td></tr></table></div></body></html>