OSDN Git Service

2004-07-23 Jonathan Wakely <redi@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / docs / html / debug.html
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">
5
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>
17
18 <h1 class="centered"><a name="top">Debugging schemes and strategies</a></h1>
19
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>
25
26 <p><em>
27    To the <a href="http://gcc.gnu.org/libstdc++/">libstdc++-v3 homepage</a>.
28 </em></p>
29
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>
36
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>
47
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>
56
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>
61
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>
80
81 <p>A second approach is to use the configuration flags 
82 </p>
83 <pre>
84      make CXXFLAGS='-g3 -O0' all
85 </pre>
86
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>
90
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.  This debug mode is available with
103   GCC 3.4.0 and later versions. </p>
104
105 <p>The libstdc++ debug mode performs checking for many areas of the C++
106   standard, but the focus is on checking interactions among standard
107   iterators, containers, and algorithms, including:</p>
108
109   <ul>
110     <li><em>Safe iterators</em>: Iterators keep track of the
111     container whose elements they reference, so errors such as
112     incrementing a past-the-end iterator or dereferencing an iterator
113     that points to a container that has been destructed are diagnosed
114     immediately.</li>
115     
116     <li><em>Algorithm preconditions</em>: Algorithms attempt to
117     validate their input parameters to detect errors as early as
118     possible. For instance, the <code>set_intersection</code>
119     algorithm requires that its iterator
120     parameters <code>first1</code> and <code>last1</code> form a valid
121     iterator range, and that the sequence
122     [<code>first1</code>, <code>last1</code>) is sorted according to
123     the same predicate that was passed
124     to <code>set_intersection</code>; the libstdc++ debug mode will
125     detect an error if the sequence is not sorted or was sorted by a
126     different predicate.</li>
127   </ul>
128
129 <h4 class="left">Using the libstdc++ debug mode</h4>
130 <p>To use the libstdc++ debug mode, compile your application with the
131   compiler flag <code>-D_GLIBCXX_DEBUG</code>. Note that this flag
132   changes the sizes and behavior of standard class templates such
133   as <code>std::vector</code>, and therefore you can only link code
134   compiled with debug mode and code compiled without debug mode if no
135   instantiation of a container is passed between the two translation
136   units.</p>
137
138 <p>For information about the design of the libstdc++ debug mode,
139   please see the <a href="debug_mode.html">libstdc++ debug mode design
140   document</a>.</p>
141
142 <h4 class="left">Using the debugging containers without debug
143   mode</h4>
144 <p>When it is not feasible to recompile your entire application, or
145   only specific containers need checking, debugging containers are
146   available as GNU extensions. These debugging containers are
147   functionally equivalent to the standard drop-in containers used in
148   debug mode, but they are available in a separate namespace as GNU
149   extensions and may be used in programs compiled with either release
150   mode or with debug mode. The
151   following table provides the names and headers of the debugging
152   containers:
153
154 <table title="Debugging containers" border="1">
155   <tr>
156     <th>Container</th>
157     <th>Header</th>
158     <th>Debug container</th>
159     <th>Debug header</th>
160   </tr>
161   <tr>
162     <td>std::bitset</td>
163     <td>&lt;bitset&gt;</td>
164     <td>__gnu_debug::bitset</td>
165     <td>&lt;debug/bitset&gt;</td>
166   </tr>
167   <tr>
168     <td>std::deque</td>
169     <td>&lt;deque&gt;</td>
170     <td>__gnu_debug::deque</td>
171     <td>&lt;debug/deque&gt;</td>
172   </tr>
173   <tr>
174     <td>std::list</td>
175     <td>&lt;list&gt;</td>
176     <td>__gnu_debug::list</td>
177     <td>&lt;debug/list&gt;</td>
178   </tr>
179   <tr>
180     <td>std::map</td>
181     <td>&lt;map&gt;</td>
182     <td>__gnu_debug::map</td>
183     <td>&lt;debug/map&gt;</td>
184   </tr>
185   <tr>
186     <td>std::multimap</td>
187     <td>&lt;map&gt;</td>
188     <td>__gnu_debug::multimap</td>
189     <td>&lt;debug/map&gt;</td>
190   </tr>
191   <tr>
192     <td>std::multiset</td>
193     <td>&lt;set&gt;</td>
194     <td>__gnu_debug::multiset</td>
195     <td>&lt;debug/set&gt;</td>
196   </tr>
197   <tr>
198     <td>std::set</td>
199     <td>&lt;set&gt;</td>
200     <td>__gnu_debug::set</td>
201     <td>&lt;debug/set&gt;</td>
202   </tr>
203   <tr>
204     <td>std::string</td>
205     <td>&lt;string&gt;</td>
206     <td>__gnu_debug::string</td>
207     <td>&lt;debug/string&gt;</td>
208   </tr>
209   <tr>
210     <td>std::wstring</td>
211     <td>&lt;string&gt;</td>
212     <td>__gnu_debug::wstring</td>
213     <td>&lt;debug/string&gt;</td>
214   </tr>
215   <tr>
216     <td>std::basic_string</td>
217     <td>&lt;string&gt;</td>
218     <td>__gnu_debug::basic_string</td>
219     <td>&lt;debug/string&gt;</td>
220   </tr>
221   <tr>
222     <td>std::vector</td>
223     <td>&lt;vector&gt;</td>
224     <td>__gnu_debug::vector</td>
225     <td>&lt;debug/vector&gt;</td>
226   </tr>
227   <tr>
228     <td>__gnu_cxx::hash_map</td>
229     <td>&lt;ext/hash_map&gt;</td>
230     <td>__gnu_debug::hash_map</td>
231     <td>&lt;debug/hash_map&gt;</td>
232   </tr>
233   <tr>
234     <td>__gnu_cxx::hash_multimap</td>
235     <td>&lt;ext/hash_map&gt;</td>
236     <td>__gnu_debug::hash_multimap</td>
237     <td>&lt;debug/hash_map&gt;</td>
238   </tr>
239   <tr>
240     <td>__gnu_cxx::hash_set</td>
241     <td>&lt;ext/hash_set&gt;</td>
242     <td>__gnu_debug::hash_set</td>
243     <td>&lt;debug/hash_set&gt;</td>
244   </tr>
245   <tr>
246     <td>__gnu_cxx::hash_multiset</td>
247     <td>&lt;ext/hash_set&gt;</td>
248     <td>__gnu_debug::hash_multiset</td>
249     <td>&lt;debug/hash_set&gt;</td>
250   </tr>
251 </table>
252
253 <h4 class="left">Debug mode semantics</h4>
254 <p>A program that uses the C++ standard library correctly
255   will maintain the same semantics under debug mode as it had with
256   the normal (release) library. All functional and exception-handling
257   guarantees made by the normal library also hold for the debug mode
258   library, with one exception: performance guarantees made by the
259   normal library may not hold in the debug mode library. For
260   instance, erasing an element in a <code>std::list</code> is a
261   constant-time operation in normal library, but in debug mode it is
262   linear in the number of iterators that reference that particular
263   list. So while your (correct) program won't change its results, it 
264   is likely to execute more slowly.</p>
265
266 <p>libstdc++ includes many extensions to the C++ standard library. In
267   some cases the extensions are obvious, such as the hashed
268   associative containers, whereas other extensions give predictable
269   results to behavior that would otherwise be undefined, such as
270   throwing an exception when a <code>std::basic_string</code> is
271   constructed from a NULL character pointer. This latter category also
272   includes implementation-defined and unspecified semantics, such as
273   the growth rate of a vector. Use of these extensions is not
274   considered incorrect, so code that relies on them will not be
275   rejected by debug mode. However, use of these extensions may affect
276   the portability of code to other implementations of the C++ standard
277   library, and is therefore somewhat hazardous. For this reason, the
278   libstdc++ debug mode offers a "pedantic" mode (similar to
279   GCC's <code>-pedantic</code> compiler flag) that attempts to emulate
280   the semantics guaranteed by the C++ standard. For
281   instance, constructing a <code>std::basic_string</code> with a NULL
282   character pointer would result in an exception under normal mode or
283   non-pedantic debug mode (this is a libstdc++ extension), whereas
284   under pedantic debug mode libstdc++ would signal an error. To enable
285   the pedantic debug mode, compile your program with
286   both <code>-D_GLIBCXX_DEBUG</code>
287   and <code>-D_GLIBCXX_DEBUG_PEDANTIC</code> .</p>
288
289 <p>The following library components provide extra debugging
290   capabilities in debug mode:</p>
291 <ul>
292   <li><code>std::basic_string</code> (no safe iterators)</li>
293   <li><code>std::bitset</code></li>
294   <li><code>std::deque</code></li>
295   <li><code>__gnu_cxx::hash_map</code></li>
296   <li><code>__gnu_cxx::hash_multimap</code></li>
297   <li><code>__gnu_cxx::hash_multiset</code></li>
298   <li><code>__gnu_cxx::hash_set</code></li>
299   <li><code>std::list</code></li>
300   <li><code>std::map</code></li>
301   <li><code>std::multimap</code></li>
302   <li><code>std::multiset</code></li>
303   <li><code>std::set</code></li>
304   <li><code>std::vector</code></li>
305 </ul>
306
307
308 <h3 class="left"><a name="mem">Tips for memory leak hunting</a></h3>
309
310 <p>There are various third party memory tracing and debug utilities
311    that can be used to provide detailed memory allocation information
312    about C++ code. An exhaustive list of tools is not going to be
313    attempted, but includes <code>mtrace</code>, <code>valgrind</code>,
314    <code>mudflap</code>, and the non-free commercial product
315    <code>purify</code>. In addition, <code>libcwd</code> has a
316    replacement for the global new and delete operators that can track
317    memory allocation and deallocation and provide useful memory
318    statistics.
319 </p>
320
321 <p>Regardless of the memory debugging tool being used, there is one
322    thing of great importance to keep in mind when debugging C++ code
323    that uses <code>new</code> and <code>delete</code>:
324    there are different kinds of allocation schemes that can be used by
325    <code> std::allocator </code>. For implementation details, see this
326    <a href="ext/howto.html#3"> document</a> and look specifically for
327    <code>GLIBCXX_FORCE_NEW</code>. 
328 </p>
329
330 <p>In a nutshell, the default allocator used by <code>
331    std::allocator</code> is a high-performance pool allocator, and can
332    give the mistaken impression that in a suspect executable, memory
333    is being leaked, when in reality the memory "leak" is a pool being
334    used by the library's allocator and is reclaimed after program
335    termination.
336 </p>
337
338 <p>For valgrind, there are some specific items to keep in mind. First
339    of all, use a version of valgrind that will work with current GNU
340    C++ tools: the first that can do this is valgrind 1.0.4, but later
341    versions should work at least as well. Second of all, use a
342    completely unoptimized build to avoid confusing valgrind. Third,
343    use GLIBCXX_FORCE_NEW to keep extraneous pool allocation noise from
344    cluttering debug information.
345 </p>
346
347 <p>Fourth, it may be necessary to force deallocation in other
348    libraries as well, namely the "C" library. On linux, this can be
349    accomplished with the appropriate use of the
350    <code>__cxa_atexit</code> or <code>atexit</code> functions.
351 </p>
352
353 <pre>
354    #include &lt;cstdlib&gt;
355
356    extern "C" void __libc_freeres(void);
357
358    void do_something() { }
359
360    int main()
361    {
362      atexit(__libc_freeres);
363      do_something();
364      return 0;
365    }
366 </pre>
367
368
369 <p>or, using <code>__cxa_atexit</code>:</p>
370
371 <pre>
372    extern "C" void __libc_freeres(void);
373    extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
374
375    void do_something() { }
376
377    int main()
378    {
379       extern void* __dso_handle __attribute__ ((__weak__));
380       __cxa_atexit((void (*) (void *)) __libc_freeres, NULL, 
381                    &amp;__dso_handle ? __dso_handle : NULL);
382       do_test();
383       return 0;
384    }
385 </pre>
386
387 <p>Suggested valgrind flags, given the suggestions above about setting
388    up the runtime environment, library, and test file, might be:
389 </p>
390 <pre> 
391    valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
392 </pre>
393
394
395 <h3 class="left"><a name="gdb">Some gdb strategies</a></h3>
396 <p>Many options are available for gdb itself: please see <a
397    href="http://sources.redhat.com/gdb/current/onlinedocs/gdb_13.html#SEC109">
398    "GDB features for C++" </a> in the gdb documentation. Also
399    recommended: the other parts of this manual.
400 </p>
401
402 <p>These settings can either be switched on in at the gdb command
403    line, or put into a .gdbint file to establish default debugging
404    characteristics, like so:
405 </p>
406
407 <pre>
408    set print pretty on
409    set print object on
410    set print static-members on
411    set print vtbl on
412    set print demangle on
413    set demangle-style gnu-v3
414 </pre>
415
416
417 <h3 class="left"><a name="verbterm">Tracking uncaught exceptions</a></h3>
418 <p>The <a href="19_diagnostics/howto.html#4">verbose termination handler</a>
419    gives information about uncaught exceptions which are killing the
420    program.  It is described in the linked-to page.
421 </p>
422
423
424 <p>Return <a href="#top">to the top of the page</a> or
425    <a href="http://gcc.gnu.org/libstdc++/">to the libstdc++ homepage</a>.
426 </p>
427
428
429 <!-- ####################################################### -->
430
431 <hr />
432 <p class="fineprint"><em>
433 See <a href="17_intro/license.html">license.html</a> for copying conditions.
434 Comments and suggestions are welcome, and may be sent to
435 <a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
436 </em></p>
437
438
439 </body>
440 </html>