Rebase.
[official-gcc.git] / libstdc++-v3 / doc / html / manual / debug.html
blobe02507b9e631f8ab5e679c915df5f592388c4f5e
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-NS Stylesheets V1.78.1" /><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>
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 add 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.req"></a>Debug Versions of Library Binary Files</h3></div></div></div><p>
34 If you would like debug symbols in libstdc++, there are two ways to
35 build libstdc++ with debug flags. The first is to create a separate
36 debug build by running make from the top-level of a tree
37 freshly-configured with
38 </p><pre class="programlisting">
39 --enable-libstdcxx-debug
40 </pre><p>and perhaps</p><pre class="programlisting">
41 --enable-libstdcxx-debug-flags='...'
42 </pre><p>
43 Both the normal build and the debug build will persist, without
44 having to specify <code class="code">CXXFLAGS</code>, and the debug library will
45 be installed in a separate directory tree, in <code class="code">(prefix)/lib/debug</code>.
46 For more information, look at the
47 <a class="link" href="configure.html" title="Configure">configuration</a> section.
48 </p><p>
49 A second approach is to use the configuration flags
50 </p><pre class="programlisting">
51 make CXXFLAGS='-g3 -fno-inline -O0' all
52 </pre><p>
53 This quick and dirty approach is often sufficient for quick
54 debugging tasks, when you cannot or don't want to recompile your
55 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>
56 There are various third party memory tracing and debug utilities
57 that can be used to provide detailed memory allocation information
58 about C++ code. An exhaustive list of tools is not going to be
59 attempted, but includes <code class="code">mtrace</code>, <code class="code">valgrind</code>,
60 <code class="code">mudflap</code>, and the non-free commercial product
61 <code class="code">purify</code>. In addition, <code class="code">libcwd</code> has a
62 replacement for the global new and delete operators that can track
63 memory allocation and deallocation and provide useful memory
64 statistics.
65 </p><p>
66 Regardless of the memory debugging tool being used, there is one
67 thing of great importance to keep in mind when debugging C++ code
68 that uses <code class="code">new</code> and <code class="code">delete</code>: there are
69 different kinds of allocation schemes that can be used by <code class="code">
70 std::allocator</code>. For implementation details, see the <a class="link" href="mt_allocator.html" title="Chapter 20. The mt_allocator">mt allocator</a> documentation and
71 look specifically for <code class="code">GLIBCXX_FORCE_NEW</code>.
72 </p><p>
73 In a nutshell, the optional <code class="classname">mt_allocator</code>
74 is a high-performance pool allocator, and can
75 give the mistaken impression that in a suspect executable, memory is
76 being leaked, when in reality the memory "leak" is a pool being used
77 by the library's allocator and is reclaimed after program
78 termination.
79 </p><p>
80 For valgrind, there are some specific items to keep in mind. First
81 of all, use a version of valgrind that will work with current GNU
82 C++ tools: the first that can do this is valgrind 1.0.4, but later
83 versions should work at least as well. Second of all, use a
84 completely unoptimized build to avoid confusing valgrind. Third, use
85 GLIBCXX_FORCE_NEW to keep extraneous pool allocation noise from
86 cluttering debug information.
87 </p><p>
88 Fourth, it may be necessary to force deallocation in other libraries
89 as well, namely the "C" library. On linux, this can be accomplished
90 with the appropriate use of the <code class="code">__cxa_atexit</code> or
91 <code class="code">atexit</code> functions.
92 </p><pre class="programlisting">
93 #include &lt;cstdlib&gt;
95 extern "C" void __libc_freeres(void);
97 void do_something() { }
99 int main()
101 atexit(__libc_freeres);
102 do_something();
103 return 0;
105 </pre><p>or, using <code class="code">__cxa_atexit</code>:</p><pre class="programlisting">
106 extern "C" void __libc_freeres(void);
107 extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
109 void do_something() { }
111 int main()
113 extern void* __dso_handle __attribute__ ((__weak__));
114 __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
115 &amp;__dso_handle ? __dso_handle : NULL);
116 do_test();
117 return 0;
119 </pre><p>
120 Suggested valgrind flags, given the suggestions above about setting
121 up the runtime environment, library, and test file, might be:
122 </p><pre class="programlisting">
123 valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
124 </pre></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>
125 All synchronization primitives used in the library internals need to be
126 understood by race detectors so that they do not produce false reports.
127 </p><p>
128 Two annotation macros are used to explain low-level synchronization
129 to race detectors:
130 <code class="code">_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and
131 <code class="code"> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>.
132 By default, these macros are defined empty -- anyone who wants
133 to use a race detector needs to redefine them to call an
134 appropriate API.
135 Since these macros are empty by default when the library is built,
136 redefining them will only affect inline functions and template
137 instantiations which are compiled in user code. This allows annotation
138 of templates such as <code class="code">shared_ptr</code>, but not code which is
139 only instantiated in the library. Code which is only instantiated in
140 the library needs to be recompiled with the annotation macros defined.
141 That can be done by rebuilding the entire
142 <code class="filename">libstdc++.so</code> file but a simpler
143 alternative exists for ELF platforms such as GNU/Linux, because ELF
144 symbol interposition allows symbols defined in the shared library to be
145 overridden by symbols with the same name that appear earlier in the
146 runtime search path. This means you only need to recompile the functions
147 that are affected by the annotation macros, which can be done by
148 recompiling individual files.
149 Annotating <code class="code">std::string</code> and <code class="code">std::wstring</code>
150 reference counting can be done by disabling extern templates (by defining
151 <code class="code">_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or by rebuilding the
152 <code class="filename">src/string-inst.cc</code> file.
153 Annotating the remaining atomic operations (at the time of writing these
154 are in <code class="code">ios_base::Init::~Init</code>, <code class="code">locale::_Impl</code>,
155 <code class="code">locale::facet</code> and <code class="code">thread::_M_start_thread</code>)
156 requires rebuilding the relevant source files.
157 </p><p>
158 The approach described above is known to work with the following race
159 detection tools:
160 <a class="link" href="http://valgrind.org/docs/manual/drd-manual.html" target="_top">
161 DRD</a>,
162 <a class="link" href="http://valgrind.org/docs/manual/hg-manual.html" target="_top">
163 Helgrind</a>, and
164 <a class="link" href="http://code.google.com/p/data-race-test/" target="_top">
165 ThreadSanitizer</a> (this refers to ThreadSanitizer v1, not the
166 new "tsan" feature built-in to GCC itself).
167 </p><p>
168 With DRD, Helgrind and ThreadSanitizer you will need to define
169 the macros like this:
170 </p><pre class="programlisting">
171 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
172 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A)
173 </pre><p>
174 Refer to the documentation of each particular tool for details.
175 </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>
176 </p><p>
177 Many options are available for GDB itself: please see <a class="link" href="http://sourceware.org/gdb/current/onlinedocs/gdb/" target="_top">
178 "GDB features for C++" </a> in the GDB documentation. Also
179 recommended: the other parts of this manual.
180 </p><p>
181 These settings can either be switched on in at the GDB command line,
182 or put into a <code class="filename">.gdbinit</code> file to establish default
183 debugging characteristics, like so:
184 </p><pre class="programlisting">
185 set print pretty on
186 set print object on
187 set print static-members on
188 set print vtbl on
189 set print demangle on
190 set demangle-style gnu-v3
191 </pre><p>
192 Starting with version 7.0, GDB includes support for writing
193 pretty-printers in Python. Pretty printers for containers and other
194 classes are distributed with GCC from version 4.5.0 and should be installed
195 alongside the libstdc++ shared library files and found automatically by
196 GDB.
197 </p><p>
198 Depending where libstdc++ is installed, GDB might refuse to auto-load
199 the python printers and print a warning instead.
200 If this happens the python printers can be enabled by following the
201 instructions GDB gives for setting your <code class="code">auto-load safe-path</code>
202 in your <code class="filename">.gdbinit</code> configuration file.
203 </p><p>
204 Once loaded, standard library classes that the printers support
205 should print in a more human-readable format. To print the classes
206 in the old style, use the <strong class="userinput"><code>/r</code></strong> (raw) switch in the
207 print command (i.e., <strong class="userinput"><code>print /r foo</code></strong>). This will
208 print the classes as if the Python pretty-printers were not loaded.
209 </p><p>
210 For additional information on STL support and GDB please visit:
211 <a class="link" href="http://sourceware.org/gdb/wiki/STLSupport" target="_top"> "GDB Support
212 for STL" </a> in the GDB wiki. Additionally, in-depth
213 documentation and discussion of the pretty printing feature can be
214 found in "Pretty Printing" node in the GDB manual. You can find
215 on-line versions of the GDB user manual in GDB's homepage, at
216 <a class="link" href="http://sourceware.org/gdb/" target="_top"> "GDB: The GNU Project
217 Debugger" </a>.
218 </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>
219 The <a class="link" href="termination.html#support.termination.verbose" title="Verbose Terminate Handler">verbose
220 termination handler</a> gives information about uncaught
221 exceptions which kill the program.
222 </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>
223 has compile and run-time checks for many containers.
224 </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
225 Checks</a> extension has compile-time checks for many algorithms.
226 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="debug.profile_mode"></a>Profile-based Performance Analysis</h3></div></div></div><p> The <a class="link" href="profile_mode.html" title="Chapter 19. Profile Mode">Profile-based
227 Performance Analysis</a> extension has performance checks for many
228 algorithms.
229 </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. 
230 Standard Contents
231 </td></tr></table></div></body></html>