OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / xml / manual / debug.xml
1 <section xmlns="http://docbook.org/ns/docbook" version="5.0" 
2          xml:id="manual.intro.using.debug" xreflabel="Debugging Support">
3 <?dbhtml filename="debug.html"?>
4
5 <info><title>Debugging Support</title>
6   <keywordset>
7     <keyword>
8       C++
9     </keyword>
10     <keyword>
11       debug
12     </keyword>
13   </keywordset>
14 </info>
15
16
17
18 <para>
19   There are numerous things that can be done to improve the ease with
20   which C++ binaries are debugged when using the GNU tool chain. Here
21   are some of them.
22 </para>
23
24 <section xml:id="debug.compiler"><info><title>Using <command>g++</command></title></info>
25
26   <para>
27     Compiler flags determine how debug information is transmitted
28     between compilation and debug or analysis tools.
29   </para>
30
31   <para>
32     The default optimizations and debug flags for a libstdc++ build
33     are <code>-g -O2</code>. However, both debug and optimization
34     flags can be varied to change debugging characteristics. For
35     instance, turning off all optimization via the <code>-g -O0
36     -fno-inline</code> flags will disable inlining and optimizations,
37     and add debugging information, so that stepping through all functions,
38     (including inlined constructors and destructors) is possible. In
39     addition, <code>-fno-eliminate-unused-debug-types</code> can be
40     used when additional debug information, such as nested class info,
41     is desired.
42 </para>
43
44 <para>
45   Or, the debug format that the compiler and debugger use to
46   communicate information about source constructs can be changed via
47   <code>-gdwarf-2</code> or <code>-gstabs</code> flags: some debugging
48   formats permit more expressive type and scope information to be
49   shown in gdb. Expressiveness can be enhanced by flags like
50   <code>-g3</code>. The default debug information for a particular
51   platform can be identified via the value set by the
52   PREFERRED_DEBUGGING_TYPE macro in the gcc sources.
53 </para>
54
55 <para>
56   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
57   for Debugging Your Program"</link> in Using the GNU Compiler
58   Collection (GCC) for a complete list.
59 </para>
60 </section>
61
62 <section xml:id="debug.req"><info><title>Debug Versions of Library Binary Files</title></info>
63
64
65 <para>
66   If you would like debug symbols in libstdc++, there are two ways to
67   build libstdc++ with debug flags. The first is to run make from the
68   toplevel in a freshly-configured tree with
69 </para>
70 <programlisting>
71      --enable-libstdcxx-debug
72 </programlisting>
73 <para>and perhaps</para>
74 <programlisting>
75      --enable-libstdcxx-debug-flags='...'
76 </programlisting>
77 <para>
78   to create a separate debug build. Both the normal build and the
79   debug build will persist, without having to specify
80   <code>CXXFLAGS</code>, and the debug library will be installed in a
81   separate directory tree, in <code>(prefix)/lib/debug</code>. For
82   more information, look at the <link linkend="manual.intro.setup.configure">configuration</link> section.
83 </para>
84
85 <para>
86   A second approach is to use the configuration flags
87 </para>
88 <programlisting>
89      make CXXFLAGS='-g3 -fno-inline -O0' all
90 </programlisting>
91
92 <para>
93   This quick and dirty approach is often sufficient for quick
94   debugging tasks, when you cannot or don't want to recompile your
95   application to use the <link linkend="manual.ext.debug_mode">debug mode</link>.</para>
96 </section>
97
98 <section xml:id="debug.memory"><info><title>Memory Leak Hunting</title></info>
99
100
101 <para>
102   There are various third party memory tracing and debug utilities
103   that can be used to provide detailed memory allocation information
104   about C++ code. An exhaustive list of tools is not going to be
105   attempted, but includes <code>mtrace</code>, <code>valgrind</code>,
106   <code>mudflap</code>, and the non-free commercial product
107   <code>purify</code>. In addition, <code>libcwd</code> has a
108   replacement for the global new and delete operators that can track
109   memory allocation and deallocation and provide useful memory
110   statistics.
111 </para>
112
113 <para>
114   Regardless of the memory debugging tool being used, there is one
115   thing of great importance to keep in mind when debugging C++ code
116   that uses <code>new</code> and <code>delete</code>: there are
117   different kinds of allocation schemes that can be used by <code>
118   std::allocator </code>. For implementation details, see the <link linkend="manual.ext.allocator.mt">mt allocator</link> documentation and
119   look specifically for <code>GLIBCXX_FORCE_NEW</code>.
120 </para>
121
122 <para>
123   In a nutshell, the default allocator used by <code>
124   std::allocator</code> is a high-performance pool allocator, and can
125   give the mistaken impression that in a suspect executable, memory is
126   being leaked, when in reality the memory "leak" is a pool being used
127   by the library's allocator and is reclaimed after program
128   termination.
129 </para>
130
131 <para>
132   For valgrind, there are some specific items to keep in mind. First
133   of all, use a version of valgrind that will work with current GNU
134   C++ tools: the first that can do this is valgrind 1.0.4, but later
135   versions should work at least as well. Second of all, use a
136   completely unoptimized build to avoid confusing valgrind. Third, use
137   GLIBCXX_FORCE_NEW to keep extraneous pool allocation noise from
138   cluttering debug information.
139 </para>
140
141 <para>
142   Fourth, it may be necessary to force deallocation in other libraries
143   as well, namely the "C" library. On linux, this can be accomplished
144   with the appropriate use of the <code>__cxa_atexit</code> or
145   <code>atexit</code> functions.
146 </para>
147
148 <programlisting>
149    #include &lt;cstdlib&gt;
150
151    extern "C" void __libc_freeres(void);
152
153    void do_something() { }
154
155    int main()
156    {
157      atexit(__libc_freeres);
158      do_something();
159      return 0;
160    }
161 </programlisting>
162
163
164 <para>or, using <code>__cxa_atexit</code>:</para>
165
166 <programlisting>
167    extern "C" void __libc_freeres(void);
168    extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d);
169
170    void do_something() { }
171
172    int main()
173    {
174       extern void* __dso_handle __attribute__ ((__weak__));
175       __cxa_atexit((void (*) (void *)) __libc_freeres, NULL,
176                    &amp;__dso_handle ? __dso_handle : NULL);
177       do_test();
178       return 0;
179    }
180 </programlisting>
181
182 <para>
183   Suggested valgrind flags, given the suggestions above about setting
184   up the runtime environment, library, and test file, might be:
185 </para>
186 <programlisting>
187    valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
188 </programlisting>
189
190 </section>
191
192 <section xml:id="debug.races"><info><title>Data Race Hunting</title></info>
193 <para>
194   All synchronization primitives used in the library internals should be
195   understood by race detectors so that they do not produce false reports.
196 </para>
197
198 <para>
199   We use two annotations (macros) to explain low-level synchronization 
200   to race detectors:
201   <code>_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and
202   <code> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>.
203   By default, these two macros are defined empty -- anyone who wants
204   to use a race detector will need to redefine these macros to call an
205   appropriate API.
206   Since these macros are empty by default, redefining them in the user code
207   will affect only the inline template code, e.g. <code>shared_ptr</code>.
208   In order to redefine the macros in <code>basic_string</code> one will
209   need to disable extern templates (by defining 
210   <code>_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or rebuild the 
211   <code>.so</code> file.
212   The rest of the cases (currently, <code>ios_base::Init::~Init</code>,
213   <code>locale::_Impl</code> and <code>locale::facet</code>) will require
214   to rebuild the <code>.so</code> file.
215 </para>
216
217 <para>
218   The approach described above works at least with the following race 
219   detection tools:
220   <link xmlns:xlink="http://www.w3.org/1999/xlink" 
221   xlink:href="http://valgrind.org/docs/manual/drd-manual.html">
222   DRD </link>,
223   <link xmlns:xlink="http://www.w3.org/1999/xlink" 
224   xlink:href="http://valgrind.org/docs/manual/hg-manual.html"> 
225   Helgrind </link>,
226   <link xmlns:xlink="http://www.w3.org/1999/xlink" 
227   xlink:href="http://code.google.com/p/data-race-test"> 
228   ThreadSanitizer </link>.
229 </para>
230
231 <para>
232   With DRD, Helgrind and ThreadSanitizer you will need to define
233   the macros like this:
234 <programlisting>
235   #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
236   #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A)  ANNOTATE_HAPPENS_AFTER(A)
237 </programlisting>
238   Refer to the documentation of each particular tool for the details.
239 </para>
240
241 </section>
242
243 <section xml:id="debug.gdb"><info><title>Using <command>gdb</command></title></info>
244
245   <para>
246   </para>
247
248 <para>
249   Many options are available for gdb itself: please see <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sources.redhat.com/gdb/current/onlinedocs/gdb_13.html#SEC125">
250   "GDB features for C++" </link> in the gdb documentation. Also
251   recommended: the other parts of this manual.
252 </para>
253
254 <para>
255   These settings can either be switched on in at the gdb command line,
256   or put into a .gdbint file to establish default debugging
257   characteristics, like so:
258 </para>
259
260 <programlisting>
261    set print pretty on
262    set print object on
263    set print static-members on
264    set print vtbl on
265    set print demangle on
266    set demangle-style gnu-v3
267 </programlisting>
268
269 <para>
270   Starting with version 7.0, GDB includes support for writing
271   pretty-printers in Python.  Pretty printers for STL classes are
272   distributed with GCC from version 4.5.0.  The most recent version of
273   these printers are always found in libstdc++ svn repository.
274   To enable these printers, check-out the latest printers to a local
275   directory:
276 </para>
277
278 <programlisting>
279   svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
280 </programlisting>
281
282 <para>
283   Next, add the following section to your ~/.gdbinit  The path must
284   match the location where the Python module above was checked-out.
285   So if checked out to: /home/maude/gdb_printers/, the path would be as
286   written in the example below.
287 </para>
288
289 <programlisting>
290   python
291   import sys
292   sys.path.insert(0, '/home/maude/gdb_printers/python')
293   from libstdcxx.v6.printers import register_libstdcxx_printers
294   register_libstdcxx_printers (None)
295   end
296 </programlisting>
297
298 <para>
299   The path should be the only element that needs to be adjusted in the
300   example.  Once loaded, STL classes that the printers support
301   should print in a more human-readable format.  To print the classes
302   in the old style, use the /r (raw) switch in the print command
303   (i.e., print /r foo).  This will print the classes as if the Python
304   pretty-printers were not loaded.
305 </para>
306
307 <para>
308   For additional information on STL support and GDB please visit:
309   <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/wiki/STLSupport"> "GDB Support
310   for STL" </link> in the GDB wiki.  Additionally, in-depth
311   documentation and discussion of the pretty printing feature can be
312   found in "Pretty Printing" node in the GDB manual.  You can find
313   on-line versions of the GDB user manual in GDB's homepage, at
314   <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://sourceware.org/gdb/"> "GDB: The GNU Project
315   Debugger" </link>.
316 </para>
317
318 </section>
319
320 <section xml:id="debug.exceptions"><info><title>Tracking uncaught exceptions</title></info>
321
322 <para>
323   The <link linkend="support.termination.verbose">verbose
324   termination handler</link> gives information about uncaught
325   exceptions which are killing the program.  It is described in the
326   linked-to page.
327 </para>
328 </section>
329
330 <section xml:id="debug.debug_mode"><info><title>Debug Mode</title></info>
331
332   <para> The <link linkend="manual.ext.debug_mode">Debug Mode</link>
333   has compile and run-time checks for many containers.
334   </para>
335 </section>
336
337 <section xml:id="debug.compile_time_checks"><info><title>Compile Time Checking</title></info>
338
339   <para> The <link linkend="manual.ext.compile_checks">Compile-Time
340   Checks</link> Extension has compile-time checks for many algorithms.
341   </para>
342 </section>
343
344 <section xml:id="debug.profile_mode" xreflabel="debug.profile_mode"><info><title>Profile-based Performance Analysis</title></info>
345
346   <para> The <link linkend="manual.ext.profile_mode">Profile-based
347   Performance Analysis</link> Extension has performance checks for many
348   algorithms.
349   </para>
350 </section>
351
352 </section>