xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/doc/xml/manual/debug.xml (revision b1e838363e3c6fc78a55519254d99869742dd33c)
148fb7bfaSmrg<section xmlns="http://docbook.org/ns/docbook" version="5.0"
248fb7bfaSmrg	 xml:id="manual.intro.using.debug" xreflabel="Debugging Support">
34fee23f9Smrg<?dbhtml filename="debug.html"?>
44fee23f9Smrg
548fb7bfaSmrg<info><title>Debugging Support</title>
64fee23f9Smrg  <keywordset>
748fb7bfaSmrg    <keyword>C++</keyword>
848fb7bfaSmrg    <keyword>debug</keyword>
94fee23f9Smrg  </keywordset>
1048fb7bfaSmrg</info>
114fee23f9Smrg
1248fb7bfaSmrg
134fee23f9Smrg
144fee23f9Smrg<para>
154fee23f9Smrg  There are numerous things that can be done to improve the ease with
164fee23f9Smrg  which C++ binaries are debugged when using the GNU tool chain. Here
174fee23f9Smrg  are some of them.
184fee23f9Smrg</para>
194fee23f9Smrg
2048fb7bfaSmrg<section xml:id="debug.compiler"><info><title>Using <command>g++</command></title></info>
2148fb7bfaSmrg
224fee23f9Smrg  <para>
234fee23f9Smrg    Compiler flags determine how debug information is transmitted
244fee23f9Smrg    between compilation and debug or analysis tools.
254fee23f9Smrg  </para>
264fee23f9Smrg
274fee23f9Smrg  <para>
284fee23f9Smrg    The default optimizations and debug flags for a libstdc++ build
294fee23f9Smrg    are <code>-g -O2</code>. However, both debug and optimization
304fee23f9Smrg    flags can be varied to change debugging characteristics. For
314fee23f9Smrg    instance, turning off all optimization via the <code>-g -O0
324fee23f9Smrg    -fno-inline</code> flags will disable inlining and optimizations,
334fee23f9Smrg    and add debugging information, so that stepping through all functions,
344fee23f9Smrg    (including inlined constructors and destructors) is possible. In
354fee23f9Smrg    addition, <code>-fno-eliminate-unused-debug-types</code> can be
364fee23f9Smrg    used when additional debug information, such as nested class info,
374fee23f9Smrg    is desired.
384fee23f9Smrg</para>
394fee23f9Smrg
404fee23f9Smrg<para>
414fee23f9Smrg  Or, the debug format that the compiler and debugger use to
424fee23f9Smrg  communicate information about source constructs can be changed via
434fee23f9Smrg  <code>-gdwarf-2</code> or <code>-gstabs</code> flags: some debugging
444fee23f9Smrg  formats permit more expressive type and scope information to be
454fee23f9Smrg  shown in GDB. Expressiveness can be enhanced by flags like
464fee23f9Smrg  <code>-g3</code>. The default debug information for a particular
474fee23f9Smrg  platform can be identified via the value set by the
4848fb7bfaSmrg  PREFERRED_DEBUGGING_TYPE macro in the GCC sources.
494fee23f9Smrg</para>
504fee23f9Smrg
514fee23f9Smrg<para>
5248fb7bfaSmrg  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
5348fb7bfaSmrg  for Debugging Your Program"</link> in Using the GNU Compiler
544fee23f9Smrg  Collection (GCC) for a complete list.
554fee23f9Smrg</para>
5648fb7bfaSmrg</section>
574fee23f9Smrg
5848fb7bfaSmrg<section xml:id="debug.req"><info><title>Debug Versions of Library Binary Files</title></info>
5948fb7bfaSmrg
604fee23f9Smrg
614fee23f9Smrg<para>
624fee23f9Smrg  If you would like debug symbols in libstdc++, there are two ways to
6348fb7bfaSmrg  build libstdc++ with debug flags. The first is to create a separate
6448fb7bfaSmrg  debug build by running make from the top-level of a tree
6548fb7bfaSmrg  freshly-configured with
664fee23f9Smrg</para>
674fee23f9Smrg<programlisting>
684fee23f9Smrg     --enable-libstdcxx-debug
694fee23f9Smrg</programlisting>
704fee23f9Smrg<para>and perhaps</para>
714fee23f9Smrg<programlisting>
724fee23f9Smrg     --enable-libstdcxx-debug-flags='...'
734fee23f9Smrg</programlisting>
744fee23f9Smrg<para>
7548fb7bfaSmrg  Both the normal build and the debug build will persist, without
7648fb7bfaSmrg  having to specify <code>CXXFLAGS</code>, and the debug library will
7748fb7bfaSmrg  be installed in a separate directory tree, in <code>(prefix)/lib/debug</code>.
7848fb7bfaSmrg  For more information, look at the
7948fb7bfaSmrg  <link linkend="manual.intro.setup.configure">configuration</link> section.
804fee23f9Smrg</para>
814fee23f9Smrg
824fee23f9Smrg<para>
834fee23f9Smrg  A second approach is to use the configuration flags
844fee23f9Smrg</para>
854fee23f9Smrg<programlisting>
864fee23f9Smrg     make CXXFLAGS='-g3 -fno-inline -O0' all
874fee23f9Smrg</programlisting>
884fee23f9Smrg
894fee23f9Smrg<para>
904fee23f9Smrg  This quick and dirty approach is often sufficient for quick
914fee23f9Smrg  debugging tasks, when you cannot or don't want to recompile your
924fee23f9Smrg  application to use the <link linkend="manual.ext.debug_mode">debug mode</link>.</para>
9348fb7bfaSmrg</section>
944fee23f9Smrg
9548fb7bfaSmrg<section xml:id="debug.memory"><info><title>Memory Leak Hunting</title></info>
9648fb7bfaSmrg
97a3e9eb18Smrg<para>
98a3e9eb18Smrg  On many targets GCC supports AddressSanitizer, a fast memory error detector,
99a3e9eb18Smrg  which is enabled by the <option>-fsanitize=address</option> option.
100a3e9eb18Smrg</para>
1014fee23f9Smrg
1024fee23f9Smrg<para>
103a3e9eb18Smrg  There are also various third party memory tracing and debug utilities
1044fee23f9Smrg  that can be used to provide detailed memory allocation information
1054fee23f9Smrg  about C++ code. An exhaustive list of tools is not going to be
1064fee23f9Smrg  attempted, but includes <code>mtrace</code>, <code>valgrind</code>,
107a3e9eb18Smrg  <code>mudflap</code> (no longer supported since GCC 4.9.0), ElectricFence,
108a3e9eb18Smrg  and the non-free commercial product <code>purify</code>.
109a3e9eb18Smrg  In addition, <code>libcwd</code>, jemalloc and TCMalloc have replacements
110a3e9eb18Smrg  for the global <code>new</code> and <code>delete</code> operators
111a3e9eb18Smrg  that can track memory allocation and deallocation and provide useful
112a3e9eb18Smrg  memory statistics.
1134fee23f9Smrg</para>
1144fee23f9Smrg
1154fee23f9Smrg<para>
1164fee23f9Smrg  For valgrind, there are some specific items to keep in mind. First
1174fee23f9Smrg  of all, use a version of valgrind that will work with current GNU
1184fee23f9Smrg  C++ tools: the first that can do this is valgrind 1.0.4, but later
119a3e9eb18Smrg  versions should work better. Second, using an unoptimized build
120a3e9eb18Smrg  might avoid confusing valgrind.
1214fee23f9Smrg</para>
1224fee23f9Smrg
1234fee23f9Smrg<para>
124a3e9eb18Smrg  Third, it may be necessary to force deallocation in other libraries
125a3e9eb18Smrg  as well, namely the "C" library. On GNU/Linux, this can be accomplished
1264fee23f9Smrg  with the appropriate use of the <code>__cxa_atexit</code> or
1274fee23f9Smrg  <code>atexit</code> functions.
1284fee23f9Smrg</para>
1294fee23f9Smrg
1304fee23f9Smrg<programlisting>
1314fee23f9Smrg   #include &lt;cstdlib&gt;
1324fee23f9Smrg
1334fee23f9Smrg   extern "C" void __libc_freeres(void);
1344fee23f9Smrg
1354fee23f9Smrg   void do_something() { }
1364fee23f9Smrg
1374fee23f9Smrg   int main()
1384fee23f9Smrg   {
1394fee23f9Smrg     atexit(__libc_freeres);
1404fee23f9Smrg     do_something();
1414fee23f9Smrg     return 0;
1424fee23f9Smrg   }
1434fee23f9Smrg</programlisting>
1444fee23f9Smrg
1454fee23f9Smrg<para>or, using <code>__cxa_atexit</code>:</para>
1464fee23f9Smrg
1474fee23f9Smrg<programlisting>
1484fee23f9Smrg   extern "C" void __libc_freeres(void);
1494fee23f9Smrg   extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
1504fee23f9Smrg
1514fee23f9Smrg   void do_something() { }
1524fee23f9Smrg
1534fee23f9Smrg   int main()
1544fee23f9Smrg   {
1554fee23f9Smrg      extern void* __dso_handle __attribute__ ((__weak__));
1564fee23f9Smrg      __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
1574fee23f9Smrg		   &amp;__dso_handle ? __dso_handle : NULL);
1584fee23f9Smrg      do_test();
1594fee23f9Smrg      return 0;
1604fee23f9Smrg   }
1614fee23f9Smrg</programlisting>
1624fee23f9Smrg
1634fee23f9Smrg<para>
1644fee23f9Smrg  Suggested valgrind flags, given the suggestions above about setting
1654fee23f9Smrg  up the runtime environment, library, and test file, might be:
1664fee23f9Smrg</para>
1674fee23f9Smrg<programlisting>
1684fee23f9Smrg   valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
1694fee23f9Smrg</programlisting>
1704fee23f9Smrg
171a3e9eb18Smrg<section xml:id="debug.memory.mtalloc">
172a3e9eb18Smrg<info><title>Non-memory leaks in Pool and MT allocators</title></info>
173a3e9eb18Smrg
174a3e9eb18Smrg<para>
175a3e9eb18Smrg  There are different kinds of allocation schemes that can be used by
176a3e9eb18Smrg  <code>std::allocator</code>. Prior to GCC 3.4.0 the default was to use
177a3e9eb18Smrg  a pooling allocator, <classname>pool_allocator</classname>,
178a3e9eb18Smrg  which is still available as the optional
179a3e9eb18Smrg  <classname>__pool_alloc</classname> extension.
180a3e9eb18Smrg  Another optional extension, <classname>__mt_alloc</classname>,
181a3e9eb18Smrg  is a high-performance pool allocator.
182a3e9eb18Smrg</para>
183a3e9eb18Smrg
184a3e9eb18Smrg<para>
185a3e9eb18Smrg  In a suspect executable these pooling allocators can give
186a3e9eb18Smrg  the mistaken impression that memory is being leaked,
187a3e9eb18Smrg  when in reality the memory "leak" is a pool being used
188a3e9eb18Smrg  by the library's allocator and is reclaimed after program
189a3e9eb18Smrg  termination.
190a3e9eb18Smrg</para>
191a3e9eb18Smrg
192a3e9eb18Smrg<para>
193a3e9eb18Smrg  If you're using memory debugging tools on a program that uses
194a3e9eb18Smrg  one of these pooling allocators, you can set the environment variable
195a3e9eb18Smrg  <literal>GLIBCXX_FORCE_NEW</literal> to keep extraneous pool allocation
196a3e9eb18Smrg  noise from cluttering debug information.
197a3e9eb18Smrg  For more details, see the
198a3e9eb18Smrg  <link linkend="manual.ext.allocator.mt">mt allocator</link>
199a3e9eb18Smrg  documentation and look specifically for <code>GLIBCXX_FORCE_NEW</code>.
200a3e9eb18Smrg</para>
201a3e9eb18Smrg
202a3e9eb18Smrg</section>
203a3e9eb18Smrg
20448fb7bfaSmrg</section>
2054fee23f9Smrg
20648fb7bfaSmrg<section xml:id="debug.races"><info><title>Data Race Hunting</title></info>
20748fb7bfaSmrg<para>
20848fb7bfaSmrg  All synchronization primitives used in the library internals need to be
20948fb7bfaSmrg  understood by race detectors so that they do not produce false reports.
21048fb7bfaSmrg</para>
21148fb7bfaSmrg
21248fb7bfaSmrg<para>
21348fb7bfaSmrg  Two annotation macros are used to explain low-level synchronization
21448fb7bfaSmrg  to race detectors:
21548fb7bfaSmrg  <code>_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and
21648fb7bfaSmrg  <code> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>.
21748fb7bfaSmrg  By default, these macros are defined empty -- anyone who wants
21848fb7bfaSmrg  to use a race detector needs to redefine them to call an
21948fb7bfaSmrg  appropriate API.
22048fb7bfaSmrg  Since these macros are empty by default when the library is built,
22148fb7bfaSmrg  redefining them will only affect inline functions and template
22248fb7bfaSmrg  instantiations which are compiled in user code. This allows annotation
22348fb7bfaSmrg  of templates such as <code>shared_ptr</code>, but not code which is
22448fb7bfaSmrg  only instantiated in the library.  Code which is only instantiated in
22548fb7bfaSmrg  the library needs to be recompiled with the annotation macros defined.
22648fb7bfaSmrg  That can be done by rebuilding the entire
22748fb7bfaSmrg  <filename class="libraryfile">libstdc++.so</filename> file but a simpler
22848fb7bfaSmrg  alternative exists for ELF platforms such as GNU/Linux, because ELF
22948fb7bfaSmrg  symbol interposition allows symbols defined in the shared library to be
23048fb7bfaSmrg  overridden by symbols with the same name that appear earlier in the
23148fb7bfaSmrg  runtime search path. This means you only need to recompile the functions
23248fb7bfaSmrg  that are affected by the annotation macros, which can be done by
23348fb7bfaSmrg  recompiling individual files.
23448fb7bfaSmrg  Annotating <code>std::string</code> and <code>std::wstring</code>
23548fb7bfaSmrg  reference counting can be done by disabling extern templates (by defining
23648fb7bfaSmrg  <code>_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or by rebuilding the
23748fb7bfaSmrg  <filename>src/string-inst.cc</filename> file.
23848fb7bfaSmrg  Annotating the remaining atomic operations (at the time of writing these
23948fb7bfaSmrg  are in <code>ios_base::Init::~Init</code>, <code>locale::_Impl</code>,
24048fb7bfaSmrg  <code>locale::facet</code> and <code>thread::_M_start_thread</code>)
24148fb7bfaSmrg  requires rebuilding the relevant source files.
24248fb7bfaSmrg</para>
24348fb7bfaSmrg
24448fb7bfaSmrg<para>
24548fb7bfaSmrg  The approach described above is known to work with the following race
24648fb7bfaSmrg  detection tools:
24748fb7bfaSmrg  <link xmlns:xlink="http://www.w3.org/1999/xlink"
248*b1e83836Smrg  xlink:href="https://valgrind.org/docs/manual/drd-manual.html">
24948fb7bfaSmrg  DRD</link>,
25048fb7bfaSmrg  <link xmlns:xlink="http://www.w3.org/1999/xlink"
251*b1e83836Smrg  xlink:href="https://valgrind.org/docs/manual/hg-manual.html">
25248fb7bfaSmrg  Helgrind</link>, and
25348fb7bfaSmrg  <link xmlns:xlink="http://www.w3.org/1999/xlink"
254b17d1066Smrg  xlink:href="https://github.com/google/sanitizers">
2554d5abbe8Smrg  ThreadSanitizer</link> (this refers to ThreadSanitizer v1, not the
2564d5abbe8Smrg  new "tsan" feature built-in to GCC itself).
25748fb7bfaSmrg</para>
25848fb7bfaSmrg
25948fb7bfaSmrg<para>
26048fb7bfaSmrg  With DRD, Helgrind and ThreadSanitizer you will need to define
26148fb7bfaSmrg  the macros like this:
26248fb7bfaSmrg<programlisting>
26348fb7bfaSmrg  #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
26448fb7bfaSmrg  #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A)  ANNOTATE_HAPPENS_AFTER(A)
26548fb7bfaSmrg</programlisting>
26648fb7bfaSmrg  Refer to the documentation of each particular tool for details.
26748fb7bfaSmrg</para>
26848fb7bfaSmrg
26948fb7bfaSmrg</section>
27048fb7bfaSmrg
27148fb7bfaSmrg<section xml:id="debug.gdb"><info><title>Using <command>gdb</command></title></info>
27248fb7bfaSmrg
2734fee23f9Smrg  <para>
2744fee23f9Smrg  </para>
2754fee23f9Smrg
2764fee23f9Smrg<para>
27748fb7bfaSmrg  Many options are available for GDB itself: please see <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/current/onlinedocs/gdb/">
27848fb7bfaSmrg  "GDB features for C++" </link> in the GDB documentation. Also
2794fee23f9Smrg  recommended: the other parts of this manual.
2804fee23f9Smrg</para>
2814fee23f9Smrg
2824fee23f9Smrg<para>
2834fee23f9Smrg  These settings can either be switched on in at the GDB command line,
2844d5abbe8Smrg  or put into a <filename>.gdbinit</filename> file to establish default
2854d5abbe8Smrg  debugging characteristics, like so:
2864fee23f9Smrg</para>
2874fee23f9Smrg
2884fee23f9Smrg<programlisting>
2894fee23f9Smrg   set print pretty on
2904fee23f9Smrg   set print object on
2914fee23f9Smrg   set print static-members on
2924fee23f9Smrg   set print vtbl on
2934fee23f9Smrg   set print demangle on
2944fee23f9Smrg   set demangle-style gnu-v3
2954fee23f9Smrg</programlisting>
2964fee23f9Smrg
2974fee23f9Smrg<para>
2984fee23f9Smrg  Starting with version 7.0, GDB includes support for writing
2994d5abbe8Smrg  pretty-printers in Python.  Pretty printers for containers and other
3004d5abbe8Smrg  classes are distributed with GCC from version 4.5.0 and should be installed
3014d5abbe8Smrg  alongside the libstdc++ shared library files and found automatically by
3024d5abbe8Smrg  GDB.
3034fee23f9Smrg</para>
3044fee23f9Smrg
3054fee23f9Smrg<para>
3064d5abbe8Smrg  Depending where libstdc++ is installed, GDB might refuse to auto-load
3074d5abbe8Smrg  the python printers and print a warning instead.
3084d5abbe8Smrg  If this happens the python printers can be enabled by following the
3094d5abbe8Smrg  instructions GDB gives for setting your <code>auto-load safe-path</code>
3104d5abbe8Smrg  in your <filename>.gdbinit</filename> configuration file.
3114fee23f9Smrg</para>
3124fee23f9Smrg
3134fee23f9Smrg<para>
3144d5abbe8Smrg  Once loaded, standard library classes that the printers support
3154fee23f9Smrg  should print in a more human-readable format.  To print the classes
3164d5abbe8Smrg  in the old style, use the <userinput>/r</userinput> (raw) switch in the
3174d5abbe8Smrg  print command (i.e., <userinput>print /r foo</userinput>).  This will
3184d5abbe8Smrg  print the classes as if the Python pretty-printers were not loaded.
3194fee23f9Smrg</para>
3204fee23f9Smrg
3214fee23f9Smrg<para>
3224fee23f9Smrg  For additional information on STL support and GDB please visit:
32348fb7bfaSmrg  <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/wiki/STLSupport"> "GDB Support
32448fb7bfaSmrg  for STL" </link> in the GDB wiki.  Additionally, in-depth
3254fee23f9Smrg  documentation and discussion of the pretty printing feature can be
3264fee23f9Smrg  found in "Pretty Printing" node in the GDB manual.  You can find
3274fee23f9Smrg  on-line versions of the GDB user manual in GDB's homepage, at
32848fb7bfaSmrg  <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/"> "GDB: The GNU Project
32948fb7bfaSmrg  Debugger" </link>.
3304fee23f9Smrg</para>
3314fee23f9Smrg
33248fb7bfaSmrg</section>
3334fee23f9Smrg
33448fb7bfaSmrg<section xml:id="debug.exceptions"><info><title>Tracking uncaught exceptions</title></info>
33548fb7bfaSmrg
3364fee23f9Smrg<para>
3374fee23f9Smrg  The <link linkend="support.termination.verbose">verbose
3384fee23f9Smrg  termination handler</link> gives information about uncaught
3394d5abbe8Smrg  exceptions which kill the program.
3404fee23f9Smrg</para>
34148fb7bfaSmrg</section>
3424fee23f9Smrg
34348fb7bfaSmrg<section xml:id="debug.debug_mode"><info><title>Debug Mode</title></info>
34448fb7bfaSmrg
3454fee23f9Smrg  <para> The <link linkend="manual.ext.debug_mode">Debug Mode</link>
3464fee23f9Smrg  has compile and run-time checks for many containers.
3474fee23f9Smrg  </para>
34848fb7bfaSmrg</section>
3494fee23f9Smrg
35048fb7bfaSmrg<section xml:id="debug.compile_time_checks"><info><title>Compile Time Checking</title></info>
35148fb7bfaSmrg
3524fee23f9Smrg  <para> The <link linkend="manual.ext.compile_checks">Compile-Time
3534d5abbe8Smrg  Checks</link> extension has compile-time checks for many algorithms.
3544fee23f9Smrg  </para>
35548fb7bfaSmrg</section>
3564fee23f9Smrg
35748fb7bfaSmrg</section>
358