Fix wrong assertion for LIPO
[official-gcc.git] / gcc-4_6_3-mobile / libstdc++-v3 / doc / html / manual / debug.html
blob55b5abf767418e8cea0d281d46c058418bb2b560
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Debugging Support</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"/><meta name="keywords" content="&#10; C++&#10; , &#10; debug&#10; "/><meta name="keywords" content="&#10; ISO C++&#10; , &#10; library&#10; "/><link rel="home" href="../spine.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="bk01pt02.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 align="left"><a accesskey="p" href="using_exceptions.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Using</th><td align="right"> <a accesskey="n" href="bk01pt02.html">Next</a></td></tr></table><hr/></div><div class="section" title="Debugging Support"><div class="titlepage"><div><div><h2 class="title"><a id="manual.intro.using.debug"/>Debugging Support</h2></div></div></div><p>
4 There are numerous things that can be done to improve the ease with
5 which C++ binaries are debugged when using the GNU tool chain. Here
6 are some of them.
7 </p><div class="section" title="Using g++"><div class="titlepage"><div><div><h3 class="title"><a id="debug.compiler"/>Using <span class="command"><strong>g++</strong></span></h3></div></div></div><p>
8 Compiler flags determine how debug information is transmitted
9 between compilation and debug or analysis tools.
10 </p><p>
11 The default optimizations and debug flags for a libstdc++ build
12 are <code class="code">-g -O2</code>. However, both debug and optimization
13 flags can be varied to change debugging characteristics. For
14 instance, turning off all optimization via the <code class="code">-g -O0
15 -fno-inline</code> flags will disable inlining and optimizations,
16 and add debugging information, so that stepping through all functions,
17 (including inlined constructors and destructors) is possible. In
18 addition, <code class="code">-fno-eliminate-unused-debug-types</code> can be
19 used when additional debug information, such as nested class info,
20 is desired.
21 </p><p>
22 Or, the debug format that the compiler and debugger use to
23 communicate information about source constructs can be changed via
24 <code class="code">-gdwarf-2</code> or <code class="code">-gstabs</code> flags: some debugging
25 formats permit more expressive type and scope information to be
26 shown in GDB. Expressiveness can be enhanced by flags like
27 <code class="code">-g3</code>. The default debug information for a particular
28 platform can be identified via the value set by the
29 PREFERRED_DEBUGGING_TYPE macro in the gcc sources.
30 </p><p>
31 Many other options are available: please see <a class="link" href="http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging%20Options">"Options
32 for Debugging Your Program"</a> in Using the GNU Compiler
33 Collection (GCC) for a complete list.
34 </p></div><div class="section" title="Debug Versions of Library Binary Files"><div class="titlepage"><div><div><h3 class="title"><a id="debug.req"/>Debug Versions of Library Binary Files</h3></div></div></div><p>
35 If you would like debug symbols in libstdc++, there are two ways to
36 build libstdc++ with debug flags. The first is to run make from the
37 toplevel in a freshly-configured tree 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 to create a separate debug build. Both the normal build and the
44 debug build will persist, without having to specify
45 <code class="code">CXXFLAGS</code>, and the debug library will be installed in a
46 separate directory tree, in <code class="code">(prefix)/lib/debug</code>. For
47 more information, look at the <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" title="Memory Leak Hunting"><div class="titlepage"><div><div><h3 class="title"><a id="debug.memory"/>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="ext_allocators.html#manual.ext.allocator.mt" title="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 default allocator used by <code class="code">
74 std::allocator</code> 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" title="Data Race Hunting"><div class="titlepage"><div><div><h3 class="title"><a id="debug.races"/>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.
140 In order to annotate <code class="code">basic_string</code> reference counting it
141 is necessary to disable extern templates (by defining
142 <code class="code">_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or to rebuild the
143 <code class="code">.so</code> file.
144 Annotating the remaining atomic operations (at the time of writing these
145 are in <code class="code">ios_base::Init::~Init</code>, <code class="code">locale::_Impl</code> and
146 <code class="code">locale::facet</code>) requires rebuilding the <code class="code">.so</code> file.
147 </p><p>
148 The approach described above is known to work with the following race
149 detection tools:
150 <a class="link" href="http://valgrind.org/docs/manual/drd-manual.html">
151 DRD</a>,
152 <a class="link" href="http://valgrind.org/docs/manual/hg-manual.html">
153 Helgrind</a>, and
154 <a class="link" href="http://code.google.com/p/data-race-test">
155 ThreadSanitizer</a>.
156 </p><p>
157 With DRD, Helgrind and ThreadSanitizer you will need to define
158 the macros like this:
159 </p><pre class="programlisting">
160 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
161 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A)
162 </pre><p>
163 Refer to the documentation of each particular tool for details.
164 </p></div><div class="section" title="Using gdb"><div class="titlepage"><div><div><h3 class="title"><a id="debug.gdb"/>Using <span class="command"><strong>gdb</strong></span></h3></div></div></div><p>
165 </p><p>
166 Many options are available for GDB itself: please see <a class="link" href="http://sources.redhat.com/gdb/current/onlinedocs/gdb/">
167 "GDB features for C++" </a> in the GDB documentation. Also
168 recommended: the other parts of this manual.
169 </p><p>
170 These settings can either be switched on in at the GDB command line,
171 or put into a .gdbint file to establish default debugging
172 characteristics, like so:
173 </p><pre class="programlisting">
174 set print pretty on
175 set print object on
176 set print static-members on
177 set print vtbl on
178 set print demangle on
179 set demangle-style gnu-v3
180 </pre><p>
181 Starting with version 7.0, GDB includes support for writing
182 pretty-printers in Python. Pretty printers for STL classes are
183 distributed with GCC from version 4.5.0. The most recent version of
184 these printers are always found in libstdc++ svn repository.
185 To enable these printers, check-out the latest printers to a local
186 directory:
187 </p><pre class="programlisting">
188 svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
189 </pre><p>
190 Next, add the following section to your ~/.gdbinit The path must
191 match the location where the Python module above was checked-out.
192 So if checked out to: /home/maude/gdb_printers/, the path would be as
193 written in the example below.
194 </p><pre class="programlisting">
195 python
196 import sys
197 sys.path.insert(0, '/home/maude/gdb_printers/python')
198 from libstdcxx.v6.printers import register_libstdcxx_printers
199 register_libstdcxx_printers (None)
201 </pre><p>
202 The path should be the only element that needs to be adjusted in the
203 example. Once loaded, STL classes that the printers support
204 should print in a more human-readable format. To print the classes
205 in the old style, use the /r (raw) switch in the print command
206 (i.e., print /r foo). This will print the classes as if the Python
207 pretty-printers were not loaded.
208 </p><p>
209 For additional information on STL support and GDB please visit:
210 <a class="link" href="http://sourceware.org/gdb/wiki/STLSupport"> "GDB Support
211 for STL" </a> in the GDB wiki. Additionally, in-depth
212 documentation and discussion of the pretty printing feature can be
213 found in "Pretty Printing" node in the GDB manual. You can find
214 on-line versions of the GDB user manual in GDB's homepage, at
215 <a class="link" href="http://sourceware.org/gdb/"> "GDB: The GNU Project
216 Debugger" </a>.
217 </p></div><div class="section" title="Tracking uncaught exceptions"><div class="titlepage"><div><div><h3 class="title"><a id="debug.exceptions"/>Tracking uncaught exceptions</h3></div></div></div><p>
218 The <a class="link" href="termination.html#support.termination.verbose" title="Verbose Terminate Handler">verbose
219 termination handler</a> gives information about uncaught
220 exceptions which are killing the program. It is described in the
221 linked-to page.
222 </p></div><div class="section" title="Debug Mode"><div class="titlepage"><div><div><h3 class="title"><a id="debug.debug_mode"/>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" title="Compile Time Checking"><div class="titlepage"><div><div><h3 class="title"><a id="debug.compile_time_checks"/>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" title="Profile-based Performance Analysis"><div class="titlepage"><div><div><h3 class="title"><a id="debug.profile_mode"/>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 align="left"><a accesskey="p" href="using_exceptions.html">Prev</a> </td><td align="center"><a accesskey="u" href="using.html">Up</a></td><td align="right"> <a accesskey="n" href="bk01pt02.html">Next</a></td></tr><tr><td align="left" valign="top">Exceptions </td><td align="center"><a accesskey="h" href="../spine.html">Home</a></td><td align="right" valign="top"> Part II. 
230 Standard Contents
231 </td></tr></table></div></body></html>