Home | History | Annotate | Line # | Download | only in manual
      1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      2 <!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="Chapter3.Using" /><link rel="prev" href="using_exceptions.html" title="Exceptions" /><link rel="next" href="std_contents.html" title="PartII. 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">Chapter3.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>
      3   There are numerous things that can be done to improve the ease with
      4   which C++ binaries are debugged when using the GNU tool chain. Here
      5   are some of them.
      6 </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>
      7     Compiler flags determine how debug information is transmitted
      8     between compilation and debug or analysis tools.
      9   </p><p>
     10     The default optimizations and debug flags for a libstdc++ build
     11     are <code class="code">-g -O2</code>. However, both debug and optimization
     12     flags can be varied to change debugging characteristics. For
     13     instance, turning off all optimization via the <code class="code">-g -O0
     14     -fno-inline</code> flags will disable inlining and optimizations,
     15     and include debugging information, so that stepping through all functions,
     16     (including inlined constructors and destructors) is possible. In
     17     addition, <code class="code">-fno-eliminate-unused-debug-types</code> can be
     18     used when additional debug information, such as nested class info,
     19     is desired.
     20 </p><p>
     21   Or, the debug format that the compiler and debugger use to
     22   communicate information about source constructs can be changed via
     23   <code class="code">-gdwarf-2</code> or <code class="code">-gstabs</code> flags: some debugging
     24   formats permit more expressive type and scope information to be
     25   shown in GDB. Expressiveness can be enhanced by flags like
     26   <code class="code">-g3</code>. The default debug information for a particular
     27   platform can be identified via the value set by the
     28   PREFERRED_DEBUGGING_TYPE macro in the GCC sources.
     29 </p><p>
     30   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
     31   for Debugging Your Program"</a> in Using the GNU Compiler
     32   Collection (GCC) for a complete list.
     33 </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>
     34   The <a class="link" href="debug_mode.html" title="Chapter17.Debug Mode">Debug Mode</a>
     35   has compile and run-time checks for many containers.
     36 </p><p>
     37   There are also lightweight assertions for checking function preconditions,
     38   such as checking for out-of-bounds indices when accessing a
     39   <code class="classname">std::vector</code>. These can be enabled without using
     40   the full Debug Mode, by using <code class="option">-D_GLIBCXX_ASSERTIONS</code>
     41   (see <a class="xref" href="using_macros.html" title="Macros">Macros</a>).
     42 </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>
     43   The <a class="link" href="termination.html#support.termination.verbose" title="Verbose Terminate Handler">verbose
     44   termination handler</a> gives information about uncaught
     45   exceptions which kill the program.
     46 </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>
     47   On many targets GCC supports AddressSanitizer, a fast memory error detector,
     48   which is enabled by the <code class="option">-fsanitize=address</code> option.
     49 </p><p>
     50   The <code class="classname">std::vector</code> implementation has additional
     51   instrumentation to work with AddressSanitizer, but this has to be enabled
     52   explicitly by using <code class="option">-D_GLIBCXX_SANITIZE_VECTOR</code>
     53   (see <a class="xref" href="using_macros.html" title="Macros">Macros</a>).
     54 </p><p>
     55   There are also various third party memory tracing and debug utilities
     56   that can be used to provide detailed memory allocation information
     57   about C++ code. An exhaustive list of tools is not going to be
     58   attempted, but includes <code class="code">mtrace</code>, <code class="code">valgrind</code>,
     59   <code class="code">mudflap</code> (no longer supported since GCC 4.9.0), ElectricFence,
     60   and the non-free commercial product <code class="code">purify</code>.
     61   In addition, <code class="code">libcwd</code>, jemalloc and TCMalloc have replacements
     62   for the global <code class="code">new</code> and <code class="code">delete</code> operators
     63   that can track memory allocation and deallocation and provide useful
     64   memory statistics.
     65 </p><p>
     66   For valgrind, there are some specific items to keep in mind. First
     67   of all, use a version of valgrind that will work with current GNU
     68   C++ tools: the first that can do this is valgrind 1.0.4, but later
     69   versions should work better. Second, using an unoptimized build
     70   might avoid confusing valgrind.
     71 </p><p>
     72   Third, it may be necessary to force deallocation in other libraries
     73   as well, namely the "C" library. On GNU/Linux, this can be accomplished
     74   with the appropriate use of the <code class="code">__cxa_atexit</code> or
     75   <code class="code">atexit</code> functions.
     76 </p><pre class="programlisting">
     77    #include &lt;cstdlib&gt;
     78 
     79    extern "C" void __libc_freeres(void);
     80 
     81    void do_something() { }
     82 
     83    int main()
     84    {
     85      atexit(__libc_freeres);
     86      do_something();
     87      return 0;
     88    }
     89 </pre><p>or, using <code class="code">__cxa_atexit</code>:</p><pre class="programlisting">
     90    extern "C" void __libc_freeres(void);
     91    extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
     92 
     93    void do_something() { }
     94 
     95    int main()
     96    {
     97       extern void* __dso_handle __attribute__ ((__weak__));
     98       __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
     99 		   &amp;__dso_handle ? __dso_handle : NULL);
    100       do_test();
    101       return 0;
    102    }
    103 </pre><p>
    104   Suggested valgrind flags, given the suggestions above about setting
    105   up the runtime environment, library, and test file, might be:
    106 </p><pre class="programlisting">
    107    valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
    108 </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>
    109   There are different kinds of allocation schemes that can be used by
    110   <code class="code">std::allocator</code>. Prior to GCC 3.4.0 the default was to use
    111   a pooling allocator, <code class="classname">pool_allocator</code>,
    112   which is still available as the optional
    113   <code class="classname">__pool_alloc</code> extension.
    114   Another optional extension, <code class="classname">__mt_alloc</code>,
    115   is a high-performance pool allocator.
    116 </p><p>
    117   In a suspect executable these pooling allocators can give
    118   the mistaken impression that memory is being leaked,
    119   when in reality the memory "leak" is a pool being used
    120   by the library's allocator and is reclaimed after program
    121   termination.
    122 </p><p>
    123   If you're using memory debugging tools on a program that uses
    124   one of these pooling allocators, you can set the environment variable
    125   <code class="literal">GLIBCXX_FORCE_NEW</code> to keep extraneous pool allocation
    126   noise from cluttering debug information.
    127   For more details, see the
    128   <a class="link" href="mt_allocator.html" title="Chapter19.The mt_allocator">mt allocator</a>
    129   documentation and look specifically for <code class="code">GLIBCXX_FORCE_NEW</code>.
    130 </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>
    131   All synchronization primitives used in the library internals need to be
    132   understood by race detectors so that they do not produce false reports.
    133 </p><p>
    134   Two annotation macros are used to explain low-level synchronization
    135   to race detectors:
    136   <code class="code">_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and
    137   <code class="code"> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>.
    138   By default, these macros are defined empty -- anyone who wants
    139   to use a race detector needs to redefine them to call an
    140   appropriate API.
    141   Since these macros are empty by default when the library is built,
    142   redefining them will only affect inline functions and template
    143   instantiations which are compiled in user code. This allows annotation
    144   of templates such as <code class="code">shared_ptr</code>, but not code which is
    145   only instantiated in the library.  Code which is only instantiated in
    146   the library needs to be recompiled with the annotation macros defined.
    147   That can be done by rebuilding the entire
    148   <code class="filename">libstdc++.so</code> file but a simpler
    149   alternative exists for ELF platforms such as GNU/Linux, because ELF
    150   symbol interposition allows symbols defined in the shared library to be
    151   overridden by symbols with the same name that appear earlier in the
    152   runtime search path. This means you only need to recompile the functions
    153   that are affected by the annotation macros, which can be done by
    154   recompiling individual files.
    155   Annotating <code class="code">std::string</code> and <code class="code">std::wstring</code>
    156   reference counting can be done by disabling extern templates (by defining
    157   <code class="code">_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or by rebuilding the
    158   <code class="filename">src/string-inst.cc</code> file.
    159   Annotating the remaining atomic operations (at the time of writing these
    160   are in <code class="code">ios_base::Init::~Init</code>, <code class="code">locale::_Impl</code>,
    161   <code class="code">locale::facet</code> and <code class="code">thread::_M_start_thread</code>)
    162   requires rebuilding the relevant source files.
    163 </p><p>
    164   The approach described above is known to work with the following race
    165   detection tools:
    166   <a class="link" href="https://valgrind.org/docs/manual/drd-manual.html" target="_top">
    167   DRD</a>,
    168   <a class="link" href="https://valgrind.org/docs/manual/hg-manual.html" target="_top">
    169   Helgrind</a>, and
    170   <a class="link" href="https://github.com/google/sanitizers" target="_top">
    171   ThreadSanitizer</a> (this refers to ThreadSanitizer v1, not the
    172   new "tsan" feature built-in to GCC itself).
    173 </p><p>
    174   With DRD, Helgrind and ThreadSanitizer you will need to define
    175   the macros like this:
    176 </p><pre class="programlisting">
    177   #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
    178   #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A)  ANNOTATE_HAPPENS_AFTER(A)
    179 </pre><p>
    180   Refer to the documentation of each particular tool for details.
    181 </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>
    182   </p><p>
    183   Many options are available for GDB itself: please see <a class="link" href="http://sourceware.org/gdb/current/onlinedocs/gdb" target="_top">
    184   "GDB features for C++" </a> in the GDB documentation. Also
    185   recommended: the other parts of this manual.
    186 </p><p>
    187   These settings can either be switched on in at the GDB command line,
    188   or put into a <code class="filename">.gdbinit</code> file to establish default
    189   debugging characteristics, like so:
    190 </p><pre class="programlisting">
    191    set print pretty on
    192    set print object on
    193    set print static-members on
    194    set print vtbl on
    195    set print demangle on
    196    set demangle-style gnu-v3
    197 </pre><p>
    198   Starting with version 7.0, GDB includes support for writing
    199   pretty-printers in Python.  Pretty printers for containers and other
    200   classes are distributed with GCC from version 4.5.0 and should be installed
    201   alongside the libstdc++ shared library files and found automatically by
    202   GDB.
    203 </p><p>
    204   Depending where libstdc++ is installed, GDB might refuse to auto-load
    205   the python printers and print a warning instead.
    206   If this happens the python printers can be enabled by following the
    207   instructions GDB gives for setting your <code class="code">auto-load safe-path</code>
    208   in your <code class="filename">.gdbinit</code> configuration file.
    209 </p><p>
    210   Once loaded, standard library classes that the printers support
    211   should print in a more human-readable format.  To print the classes
    212   in the old style, use the <strong class="userinput"><code>/r</code></strong> (raw) switch in the
    213   print command (i.e., <strong class="userinput"><code>print /r foo</code></strong>).  This will
    214   print the classes as if the Python pretty-printers were not loaded.
    215 </p><p>
    216   For additional information on STL support and GDB please visit:
    217   <a class="link" href="http://sourceware.org/gdb/wiki/STLSupport" target="_top"> "GDB Support
    218   for STL" </a> in the GDB wiki.  Additionally, in-depth
    219   documentation and discussion of the pretty printing feature can be
    220   found in "Pretty Printing" node in the GDB manual.  You can find
    221   on-line versions of the GDB user manual in GDB's homepage, at
    222   <a class="link" href="http://sourceware.org/gdb/" target="_top"> "GDB: The GNU Project
    223   Debugger" </a>.
    224 </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>
    225   As described above, libstdc++ is built with debug symbols enabled by default,
    226   but because it's also built with optimizations the code can be hard to
    227   follow when stepping into the library in a debugger.
    228 </p><p>
    229   If you would like to debug <code class="filename">libstdc++.so</code> itself,
    230   there are two ways to build an unoptimized libstdc++ with debug flags.
    231   The first is to create a separate debug build by running make from the
    232   top-level of a tree freshly-configured with
    233 </p><pre class="programlisting">
    234      --enable-libstdcxx-debug
    235 </pre><p>and perhaps</p><pre class="programlisting">
    236      --enable-libstdcxx-debug-flags='...'
    237 </pre><p>
    238   Both the normal build and the debug build will persist, without
    239   having to specify <code class="code">CXXFLAGS</code>, and the debug library will
    240   be installed in a separate directory tree, in <code class="code">(prefix)/lib/debug</code>.
    241   For more information, look at the
    242   <a class="link" href="configure.html" title="Configure">configuration</a> section.
    243 </p><p>
    244   A second approach is to use the configuration flags
    245 </p><pre class="programlisting">
    246      make CXXFLAGS='-g3 -fno-inline -O0' all
    247 </pre></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="Chapter16.Compile Time Checks">Compile-Time
    248   Checks</a> extension has compile-time checks for many algorithms.
    249   These checks were designed for C++98 and have not been updated to work
    250   with C++11 and later standards. They might be removed at a future date.
    251   </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">PartII.
    252     Standard Contents
    253   </td></tr></table></div></body></html>