2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / docs / html / debug.html
blobbfdf38b943f3f9e0543b454052107f8d1441f497
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html
3 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7 <head>
8 <meta name="AUTHOR" content="bkoz@gcc.gnu.org (Benjamin Kosnik)" />
9 <meta name="KEYWORDS" content="c++, libstdc++, gdb, g++, debug" />
10 <meta name="DESCRIPTION" content="Debugging C++ binaries" />
11 <meta name="GENERATOR" content="vi and ten fingers" />
12 <title>Debugging schemes and strategies</title>
13 <link rel="StyleSheet" href="lib3styles.css" type="text/css" />
14 <link rel="Copyright" href="17_intro/license.html" type="text/html" />
15 </head>
16 <body>
18 <h1 class="centered"><a name="top">Debugging schemes and strategies</a></h1>
20 <p class="fineprint"><em>
21 The latest version of this document is always available at
22 <a href="http://gcc.gnu.org/onlinedocs/libstdc++/debug.html">
23 http://gcc.gnu.org/onlinedocs/libstdc++/debug.html</a>.
24 </em></p>
26 <p><em>
27 To the <a href="http://gcc.gnu.org/libstdc++/">libstdc++-v3 homepage</a>.
28 </em></p>
30 <!-- ####################################################### -->
31 <hr />
32 <p>There are numerous things that can be done to improve the ease with
33 which C++ binaries are debugged when using the GNU
34 tool chain. Here are some of them.
35 </p>
37 <h3 class="left"><a name="gplusplus">Compiler flags determine debug info</a></h3>
38 <p>The default optimizations and debug flags for a libstdc++ build are
39 <code>-g -O2</code>. However, both debug and optimization flags can
40 be varied to change debugging characteristics. For instance,
41 turning off all optimization via the <code>-g -O0</code> flag will
42 disable inlining, so that stepping through all functions, including
43 inlined constructors and destructors, is possible. In addition,
44 <code>-fno-eliminate-unused-debug-types<code> can be used when
45 additional debug information, such as nested class info, is desired.
46 </p>
48 <p>Or, the debug format that the compiler and debugger use to communicate
49 information about source constructs can be changed via <code>
50 -gdwarf-2 </code> or <code> -gstabs </code> flags: some debugging
51 formats permit more expressive type and scope information to be
52 shown in gdb. The default debug information for a particular
53 platform can be identified via the value set by the
54 PREFERRED_DEBUGGING_TYPE macro in the gcc sources.
55 </p>
57 <p>Many other options are available: please see
58 <a href="http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging%20Options">"Options for Debugging Your Program"</a>
59 in Using the GNU Compiler Collection (GCC) for a complete list.
60 </p>
62 <h3 class="left"><a name="lib">Using special flags to make a debug binary</a></h3>
63 <p>If you would like debug symbols in libstdc++, there are two ways to
64 build libstdc++ with debug flags. The first is to run make from the
65 toplevel in a freshly-configured tree with
66 <pre>
67 --enable-libstdcxx-debug
68 </pre>
69 <p>and perhaps</p>
70 <pre>
71 --enable-libstdcxx-debug-flags='...'
72 </pre>
73 <p>to create a separate debug build. Both the normal build and the
74 debug build will persist, without having to specify
75 <code>CXXFLAGS</code>, and the debug library will be installed in a
76 separate directory tree, in <code>(prefix)/lib/debug</code>. For
77 more information, look at the <a href="configopts.html">configuration
78 options</a> document.
79 </p>
81 <p>A second approach is to use the configuration flags
82 </p>
83 <pre>
84 make CXXFLAGS='-g3 -O0' all
85 </pre>
87 <p>This quick and dirty approach is often sufficient for quick
88 debugging tasks, when you cannot or don't want to recompile your
89 application to use the <a href="#safe">debug mode</a>.</p>
91 <h3 class="left"><a name="safe">The libstdc++ debug mode</a></h3>
92 <p>By default, libstdc++ is built with efficiency in mind, and
93 therefore performs little or no error checking that is not required
94 by the C++ standard. This means that programs that incorrectly use
95 the C++ standard library will exhibit behavior that is not portable
96 and may not even be predictable, because they tread into
97 implementation-specific or undefined behavior. To detect some of
98 these errors before they can become problematic, libstdc++ offers a
99 debug mode that provides additional checking of library facilities,
100 and will report errors in the use of libstdc++ as soon as they can
101 be detected by emitting a description of the problem to standard
102 error and aborting the program. </p>
104 <p>The libstdc++ debug mode performs checking for many areas of the C++
105 standard, but the focus is on checking interactions among standard
106 iterators, containers, and algorithms, including:</p>
108 <ul>
109 <li><em>Safe iterators</em>: Iterators keep track of the
110 container whose elements they reference, so errors such as
111 incrementing a past-the-end iterator or dereferencing an iterator
112 that points to a container that has been destructed are diagnosed
113 immediately.</li>
115 <li><em>Algorithm preconditions</em>: Algorithms attempt to
116 validate their input parameters to detect errors as early as
117 possible. For instance, the <code>set_intersection</code>
118 algorithm requires that its iterator
119 parameters <code>first1</code> and <code>last1</code> form a valid
120 iterator range, and that the sequence
121 [<code>first1</code>, <code>last1</code>) is sorted according to
122 the same predicate that was passed
123 to <code>set_intersection</code>; the libstdc++ debug mode will
124 detect an error if the sequence is not sorted or was sorted by a
125 different predicate.</li>
126 </ul>
128 <h4 class="left">Using the libstdc++ debug mode</h4>
129 <p>To use the libstdc++ debug mode, compile your application with the
130 compiler flag <code>-D_GLIBCXX_DEBUG</code>. Note that this flag
131 changes the sizes and behavior of standard class templates such
132 as <code>std::vector</code>, and therefore you can only link code
133 compiled with debug mode and code compiled without debug mode if no
134 instantiation of a container is passed between the two translation
135 units.</p>
137 <p>For information about the design of the libstdc++ debug mode,
138 please see the <a href="debug_mode.html">libstdc++ debug mode design
139 document</a>.</p>
141 <h4 class="left">Using the debugging containers without debug
142 mode</h4>
143 <p>When it is not feasible to recompile your entire application, or
144 only specific containers need checking, debugging containers are
145 available as GNU extensions. These debugging containers are
146 functionally equivalent to the standard drop-in containers used in
147 debug mode, but they are available in a separate namespace as GNU
148 extensions and may be used in programs compiled with either release
149 mode or with debug mode. The
150 following table provides the names and headers of the debugging
151 containers:
153 <table title="Debugging containers" border="1">
154 <tr>
155 <th>Container</th>
156 <th>Header</th>
157 <th>Debug container</th>
158 <th>Debug header</th>
159 </tr>
160 <tr>
161 <td>std::bitset</td>
162 <td>&lt;bitset&gt;</td>
163 <td>__gnu_debug::bitset</td>
164 <td>&lt;debug/bitset&gt;</td>
165 </tr>
166 <tr>
167 <td>std::deque</td>
168 <td>&lt;deque&gt;</td>
169 <td>__gnu_debug::deque</td>
170 <td>&lt;debug/deque&gt;</td>
171 </tr>
172 <tr>
173 <td>std::list</td>
174 <td>&lt;list&gt;</td>
175 <td>__gnu_debug::list</td>
176 <td>&lt;debug/list&gt;</td>
177 </tr>
178 <tr>
179 <td>std::map</td>
180 <td>&lt;map&gt;</td>
181 <td>__gnu_debug::map</td>
182 <td>&lt;debug/map&gt;</td>
183 </tr>
184 <tr>
185 <td>std::multimap</td>
186 <td>&lt;map&gt;</td>
187 <td>__gnu_debug::multimap</td>
188 <td>&lt;debug/map&gt;</td>
189 </tr>
190 <tr>
191 <td>std::multiset</td>
192 <td>&lt;set&gt;</td>
193 <td>__gnu_debug::multiset</td>
194 <td>&lt;debug/set&gt;</td>
195 </tr>
196 <tr>
197 <td>std::set</td>
198 <td>&lt;set&gt;</td>
199 <td>__gnu_debug::set</td>
200 <td>&lt;debug/set&gt;</td>
201 </tr>
202 <tr>
203 <td>std::string</td>
204 <td>&lt;string&gt;</td>
205 <td>__gnu_debug::string</td>
206 <td>&lt;debug/string&gt;</td>
207 </tr>
208 <tr>
209 <td>std::wstring</td>
210 <td>&lt;string&gt;</td>
211 <td>__gnu_debug::wstring</td>
212 <td>&lt;debug/string&gt;</td>
213 </tr>
214 <tr>
215 <td>std::basic_string</td>
216 <td>&lt;string&gt;</td>
217 <td>__gnu_debug::basic_string</td>
218 <td>&lt;debug/string&gt;</td>
219 </tr>
220 <tr>
221 <td>std::vector</td>
222 <td>&lt;vector&gt;</td>
223 <td>__gnu_debug::vector</td>
224 <td>&lt;debug/vector&gt;</td>
225 </tr>
226 <tr>
227 <td>__gnu_cxx::hash_map</td>
228 <td>&lt;ext/hash_map&gt;</td>
229 <td>__gnu_debug::hash_map</td>
230 <td>&lt;debug/hash_map&gt;</td>
231 </tr>
232 <tr>
233 <td>__gnu_cxx::hash_multimap</td>
234 <td>&lt;ext/hash_map&gt;</td>
235 <td>__gnu_debug::hash_multimap</td>
236 <td>&lt;debug/hash_map&gt;</td>
237 </tr>
238 <tr>
239 <td>__gnu_cxx::hash_set</td>
240 <td>&lt;ext/hash_set&gt;</td>
241 <td>__gnu_debug::hash_set</td>
242 <td>&lt;debug/hash_set&gt;</td>
243 </tr>
244 <tr>
245 <td>__gnu_cxx::hash_multiset</td>
246 <td>&lt;ext/hash_set&gt;</td>
247 <td>__gnu_debug::hash_multiset</td>
248 <td>&lt;debug/hash_set&gt;</td>
249 </tr>
250 </table>
252 <h4 class="left">Debug mode semantics</h4>
253 <p>A program that uses the C++ standard library correctly
254 will maintain the same semantics under debug mode as it had with
255 the normal (release) library. All functional and exception-handling
256 guarantees made by the normal library also hold for the debug mode
257 library, with one exception: performance guarantees made by the
258 normal library may not hold in the debug mode library. For
259 instance, erasing an element in a <code>std::list</code> is a
260 constant-time operation in normal library, but in debug mode it is
261 linear in the number of iterators that reference that particular
262 list. So while your (correct) program won't change its results, it
263 is likely to execute more slowly.</p>
265 <p>libstdc++ includes many extensions to the C++ standard library. In
266 some cases the extensions are obvious, such as the hashed
267 associative containers, whereas other extensions give predictable
268 results to behavior that would otherwise be undefined, such as
269 throwing an exception when a <code>std::basic_string</code> is
270 constructed from a NULL character pointer. This latter category also
271 includes implementation-defined and unspecified semantics, such as
272 the growth rate of a vector. Use of these extensions is not
273 considered incorrect, so code that relies on them will not be
274 rejected by debug mode. However, use of these extensions may affect
275 the portability of code to other implementations of the C++ standard
276 library, and is therefore somewhat hazardous. For this reason, the
277 libstdc++ debug mode offers a "pedantic" mode (similar to
278 GCC's <code>-pedantic</code> compiler flag) that attempts to emulate
279 the semantics guaranteed by the C++ standard. For
280 instance, constructing a <code>std::basic_string</code> with a NULL
281 character pointer would result in an exception under normal mode or
282 non-pedantic debug mode (this is a libstdc++ extension), whereas
283 under pedantic debug mode libstdc++ would signal an error. To enable
284 the pedantic debug mode, compile your program with
285 both <code>-D_GLIBCXX_DEBUG</code>
286 and <code>-D_GLIBCXX_DEBUG_PEDANTIC</code> .</p>
288 <p>The following library components provide extra debugging
289 capabilities in debug mode:</p>
290 <ul>
291 <li><code>std::basic_string</code> (no safe iterators)</li>
292 <li><code>std::bitset</code></li>
293 <li><code>std::deque</code></li>
294 <li><code>__gnu_cxx::hash_map</code></li>
295 <li><code>__gnu_cxx::hash_multimap</code></li>
296 <li><code>__gnu_cxx::hash_multiset</code></li>
297 <li><code>__gnu_cxx::hash_set</code></li>
298 <li><code>std::list</code></li>
299 <li><code>std::map</code></li>
300 <li><code>std::multimap</code></li>
301 <li><code>std::multiset</code></li>
302 <li><code>std::set</code></li>
303 <li><code>std::vector</code></li>
304 </ul>
307 <h3 class="left"><a name="mem">Tips for memory leak hunting</a></h3>
309 <p>There are various third party memory tracing and debug utilities
310 that can be used to provide detailed memory allocation information
311 about C++ code. An exhaustive list of tools is not going to be
312 attempted, but includes <code>mtrace</code>, <code>valgrind</code>,
313 <code>mudflap</code>, and the non-free commercial product
314 <code>purify</code>. In addition, <code>libcwd</code> has a
315 replacement for the global new and delete operators that can track
316 memory allocation and deallocation and provide useful memory
317 statistics.
318 </p>
320 <p>Regardless of the memory debugging tool being used, there is one
321 thing of great importance to keep in mind when debugging C++ code
322 that uses <code>new</code> and <code>delete</code>:
323 there are different kinds of allocation schemes that can be used by
324 <code> std::allocator </code>. For implementation details, see this
325 <a href="ext/howto.html#3"> document</a> and look specifically for
326 <code>GLIBCXX_FORCE_NEW</code>.
327 </p>
329 <p>In a nutshell, the default allocator used by <code>
330 std::allocator</code> is a high-performance pool allocator, and can
331 give the mistaken impression that in a suspect executable, memory
332 is being leaked, when in reality the memory "leak" is a pool being
333 used by the library's allocator and is reclaimed after program
334 termination.
335 </p>
337 <p>For valgrind, there are some specific items to keep in mind. First
338 of all, use a version of valgrind that will work with current GNU
339 C++ tools: the first that can do this is valgrind 1.0.4, but later
340 versions should work at least as well. Second of all, use a
341 completely unoptimized build to avoid confusing valgrind. Third,
342 use GLIBCXX_FORCE_NEW to keep extraneous pool allocation noise from
343 cluttering debug information.
344 </p>
346 <p>Fourth, it may be necessary to force deallocation in other
347 libraries as well, namely the "C" library. On linux, this can be
348 accomplished with the appropriate use of the
349 <code>__cxa_atexit</code> or <code>atexit</code> functions.
350 </p>
352 <pre>
353 #include &lt;cstdlib&gt;
355 extern "C" void __libc_freeres(void);
357 void do_something() { }
359 int main()
361 atexit(__libc_freeres);
362 do_something();
363 return 0;
365 </pre>
368 <p>or, using <code>__cxa_atexit</code>:</p>
370 <pre>
371 extern "C" void __libc_freeres(void);
372 extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
374 void do_something() { }
376 int main()
378 extern void* __dso_handle __attribute__ ((__weak__));
379 __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
380 &amp;__dso_handle ? __dso_handle : NULL);
381 do_test();
382 return 0;
384 </pre>
386 <p>Suggested valgrind flags, given the suggestions above about setting
387 up the runtime environment, library, and test file, might be:
388 </p>
389 <pre>
390 valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
391 </pre>
394 <h3 class="left"><a name="gdb">Some gdb strategies</a></h3>
395 <p>Many options are available for gdb itself: please see <a
396 href="http://sources.redhat.com/gdb/current/onlinedocs/gdb_13.html#SEC109">
397 "GDB features for C++" </a> in the gdb documentation. Also
398 recommended: the other parts of this manual.
399 </p>
401 <p>These settings can either be switched on in at the gdb command
402 line, or put into a .gdbint file to establish default debugging
403 characteristics, like so:
404 </p>
406 <pre>
407 set print pretty on
408 set print object on
409 set print static-members on
410 set print vtbl on
411 set print demangle on
412 set demangle-style gnu-v3
413 </pre>
416 <h3 class="left"><a name="verbterm">Tracking uncaught exceptions</a></h3>
417 <p>The <a href="19_diagnostics/howto.html#4">verbose termination handler</a>
418 gives information about uncaught exceptions which are killing the
419 program. It is described in the linked-to page.
420 </p>
423 <p>Return <a href="#top">to the top of the page</a> or
424 <a href="http://gcc.gnu.org/libstdc++/">to the libstdc++ homepage</a>.
425 </p>
428 <!-- ####################################################### -->
430 <hr />
431 <p class="fineprint"><em>
432 See <a href="17_intro/license.html">license.html</a> for copying conditions.
433 Comments and suggestions are welcome, and may be sent to
434 <a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
435 </em></p>
438 </body>
439 </html>