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