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 include 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.debug_mode"><info><title>Debug Mode</title></info> 59 60 <para> 61 The <link linkend="manual.ext.debug_mode">Debug Mode</link> 62 has compile and run-time checks for many containers. 63 </para> 64 65 <para> 66 There are also lightweight assertions for checking function preconditions, 67 such as checking for out-of-bounds indices when accessing a 68 <classname>std::vector</classname>. These can be enabled without using 69 the full Debug Mode, by using <option>-D_GLIBCXX_ASSERTIONS</option> 70 (see <xref linkend="manual.intro.using.macros"/>). 71 </para> 72 73 </section> 74 75 <section xml:id="debug.exceptions"><info><title>Tracking uncaught exceptions</title></info> 76 77 <para> 78 The <link linkend="support.termination.verbose">verbose 79 termination handler</link> gives information about uncaught 80 exceptions which kill the program. 81 </para> 82 </section> 83 84 <section xml:id="debug.memory"><info><title>Memory Leak Hunting</title></info> 85 86 <para> 87 On many targets GCC supports AddressSanitizer, a fast memory error detector, 88 which is enabled by the <option>-fsanitize=address</option> option. 89 </para> 90 91 <para> 92 The <classname>std::vector</classname> implementation has additional 93 instrumentation to work with AddressSanitizer, but this has to be enabled 94 explicitly by using <option>-D_GLIBCXX_SANITIZE_VECTOR</option> 95 (see <xref linkend="manual.intro.using.macros"/>). 96 </para> 97 98 <para> 99 There are also 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> (no longer supported since GCC 4.9.0), ElectricFence, 104 and the non-free commercial product <code>purify</code>. 105 In addition, <code>libcwd</code>, jemalloc and TCMalloc have replacements 106 for the global <code>new</code> and <code>delete</code> operators 107 that can track memory allocation and deallocation and provide useful 108 memory statistics. 109 </para> 110 111 <para> 112 For valgrind, there are some specific items to keep in mind. First 113 of all, use a version of valgrind that will work with current GNU 114 C++ tools: the first that can do this is valgrind 1.0.4, but later 115 versions should work better. Second, using an unoptimized build 116 might avoid confusing valgrind. 117 </para> 118 119 <para> 120 Third, it may be necessary to force deallocation in other libraries 121 as well, namely the "C" library. On GNU/Linux, this can be accomplished 122 with the appropriate use of the <code>__cxa_atexit</code> or 123 <code>atexit</code> functions. 124 </para> 125 126 <programlisting> 127 #include <cstdlib> 128 129 extern "C" void __libc_freeres(void); 130 131 void do_something() { } 132 133 int main() 134 { 135 atexit(__libc_freeres); 136 do_something(); 137 return 0; 138 } 139 </programlisting> 140 141 <para>or, using <code>__cxa_atexit</code>:</para> 142 143 <programlisting> 144 extern "C" void __libc_freeres(void); 145 extern "C" int __cxa_atexit(void (*func) (void *), void *arg, void *d); 146 147 void do_something() { } 148 149 int main() 150 { 151 extern void* __dso_handle __attribute__ ((__weak__)); 152 __cxa_atexit((void (*) (void *)) __libc_freeres, NULL, 153 &__dso_handle ? __dso_handle : NULL); 154 do_test(); 155 return 0; 156 } 157 </programlisting> 158 159 <para> 160 Suggested valgrind flags, given the suggestions above about setting 161 up the runtime environment, library, and test file, might be: 162 </para> 163 <programlisting> 164 valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out 165 </programlisting> 166 167 <section xml:id="debug.memory.mtalloc"> 168 <info><title>Non-memory leaks in Pool and MT allocators</title></info> 169 170 <para> 171 There are different kinds of allocation schemes that can be used by 172 <code>std::allocator</code>. Prior to GCC 3.4.0 the default was to use 173 a pooling allocator, <classname>pool_allocator</classname>, 174 which is still available as the optional 175 <classname>__pool_alloc</classname> extension. 176 Another optional extension, <classname>__mt_alloc</classname>, 177 is a high-performance pool allocator. 178 </para> 179 180 <para> 181 In a suspect executable these pooling allocators can give 182 the mistaken impression that memory is being leaked, 183 when in reality the memory "leak" is a pool being used 184 by the library's allocator and is reclaimed after program 185 termination. 186 </para> 187 188 <para> 189 If you're using memory debugging tools on a program that uses 190 one of these pooling allocators, you can set the environment variable 191 <literal>GLIBCXX_FORCE_NEW</literal> to keep extraneous pool allocation 192 noise from cluttering debug information. 193 For more details, see the 194 <link linkend="manual.ext.allocator.mt">mt allocator</link> 195 documentation and look specifically for <code>GLIBCXX_FORCE_NEW</code>. 196 </para> 197 198 </section> 199 200 </section> 201 202 <section xml:id="debug.races"><info><title>Data Race Hunting</title></info> 203 <para> 204 All synchronization primitives used in the library internals need to be 205 understood by race detectors so that they do not produce false reports. 206 </para> 207 208 <para> 209 Two annotation macros are used to explain low-level synchronization 210 to race detectors: 211 <code>_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE()</code> and 212 <code> _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER()</code>. 213 By default, these macros are defined empty -- anyone who wants 214 to use a race detector needs to redefine them to call an 215 appropriate API. 216 Since these macros are empty by default when the library is built, 217 redefining them will only affect inline functions and template 218 instantiations which are compiled in user code. This allows annotation 219 of templates such as <code>shared_ptr</code>, but not code which is 220 only instantiated in the library. Code which is only instantiated in 221 the library needs to be recompiled with the annotation macros defined. 222 That can be done by rebuilding the entire 223 <filename class="libraryfile">libstdc++.so</filename> file but a simpler 224 alternative exists for ELF platforms such as GNU/Linux, because ELF 225 symbol interposition allows symbols defined in the shared library to be 226 overridden by symbols with the same name that appear earlier in the 227 runtime search path. This means you only need to recompile the functions 228 that are affected by the annotation macros, which can be done by 229 recompiling individual files. 230 Annotating <code>std::string</code> and <code>std::wstring</code> 231 reference counting can be done by disabling extern templates (by defining 232 <code>_GLIBCXX_EXTERN_TEMPLATE=-1</code>) or by rebuilding the 233 <filename>src/string-inst.cc</filename> file. 234 Annotating the remaining atomic operations (at the time of writing these 235 are in <code>ios_base::Init::~Init</code>, <code>locale::_Impl</code>, 236 <code>locale::facet</code> and <code>thread::_M_start_thread</code>) 237 requires rebuilding the relevant source files. 238 </para> 239 240 <para> 241 The approach described above is known to work with the following race 242 detection tools: 243 <link xmlns:xlink="http://www.w3.org/1999/xlink" 244 xlink:href="https://valgrind.org/docs/manual/drd-manual.html"> 245 DRD</link>, 246 <link xmlns:xlink="http://www.w3.org/1999/xlink" 247 xlink:href="https://valgrind.org/docs/manual/hg-manual.html"> 248 Helgrind</link>, and 249 <link xmlns:xlink="http://www.w3.org/1999/xlink" 250 xlink:href="https://github.com/google/sanitizers"> 251 ThreadSanitizer</link> (this refers to ThreadSanitizer v1, not the 252 new "tsan" feature built-in to GCC itself). 253 </para> 254 255 <para> 256 With DRD, Helgrind and ThreadSanitizer you will need to define 257 the macros like this: 258 <programlisting> 259 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A) 260 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A) 261 </programlisting> 262 Refer to the documentation of each particular tool for details. 263 </para> 264 265 </section> 266 267 <section xml:id="debug.gdb"><info><title>Using <command>gdb</command></title></info> 268 269 <para> 270 </para> 271 272 <para> 273 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"> 274 "GDB features for C++" </link> in the GDB documentation. Also 275 recommended: the other parts of this manual. 276 </para> 277 278 <para> 279 These settings can either be switched on in at the GDB command line, 280 or put into a <filename>.gdbinit</filename> file to establish default 281 debugging characteristics, like so: 282 </para> 283 284 <programlisting> 285 set print pretty on 286 set print object on 287 set print static-members on 288 set print vtbl on 289 set print demangle on 290 set demangle-style gnu-v3 291 </programlisting> 292 293 <para> 294 Starting with version 7.0, GDB includes support for writing 295 pretty-printers in Python. Pretty printers for containers and other 296 classes are distributed with GCC from version 4.5.0 and should be installed 297 alongside the libstdc++ shared library files and found automatically by 298 GDB. 299 </para> 300 301 <para> 302 Depending where libstdc++ is installed, GDB might refuse to auto-load 303 the python printers and print a warning instead. 304 If this happens the python printers can be enabled by following the 305 instructions GDB gives for setting your <code>auto-load safe-path</code> 306 in your <filename>.gdbinit</filename> configuration file. 307 </para> 308 309 <para> 310 Once loaded, standard library 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 <userinput>/r</userinput> (raw) switch in the 313 print command (i.e., <userinput>print /r foo</userinput>). This will 314 print the classes as if the Python 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.req"><info><title>Debug Versions of Library Binary Files</title></info> 331 332 <para> 333 As described above, libstdc++ is built with debug symbols enabled by default, 334 but because it's also built with optimizations the code can be hard to 335 follow when stepping into the library in a debugger. 336 </para> 337 338 <para> 339 If you would like to debug <filename>libstdc++.so</filename> itself, 340 there are two ways to build an unoptimized libstdc++ with debug flags. 341 The first is to create a separate debug build by running make from the 342 top-level of a tree freshly-configured with 343 </para> 344 <programlisting> 345 --enable-libstdcxx-debug 346 </programlisting> 347 <para>and perhaps</para> 348 <programlisting> 349 --enable-libstdcxx-debug-flags='...' 350 </programlisting> 351 <para> 352 Both the normal build and the debug build will persist, without 353 having to specify <code>CXXFLAGS</code>, and the debug library will 354 be installed in a separate directory tree, in <code>(prefix)/lib/debug</code>. 355 For more information, look at the 356 <link linkend="manual.intro.setup.configure">configuration</link> section. 357 </para> 358 359 <para> 360 A second approach is to use the configuration flags 361 </para> 362 <programlisting> 363 make CXXFLAGS='-g3 -fno-inline -O0' all 364 </programlisting> 365 366 </section> 367 368 369 <section xml:id="debug.compile_time_checks"><info><title>Compile Time Checking</title></info> 370 371 <para> The <link linkend="manual.ext.compile_checks">Compile-Time 372 Checks</link> extension has compile-time checks for many algorithms. 373 These checks were designed for C++98 and have not been updated to work 374 with C++11 and later standards. They might be removed at a future date. 375 </para> 376 </section> 377 378 </section> 379