1 |
742 |
jeremybenn |
|
2 |
|
|
xml:id="manual.intro.using.debug" xreflabel="Debugging Support">
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
Debugging Support
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
C++
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
debug
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
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 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
Compiler flags determine how debug information is transmitted
|
28 |
|
|
between compilation and debug or analysis tools.
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
The default optimizations and debug flags for a libstdc++ build
|
33 |
|
|
are -g -O2 . However, both debug and optimization
|
34 |
|
|
flags can be varied to change debugging characteristics. For
|
35 |
|
|
instance, turning off all optimization via the -g -O0
|
36 |
|
|
-fno-inline 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, -fno-eliminate-unused-debug-types can be
|
40 |
|
|
used when additional debug information, such as nested class info,
|
41 |
|
|
is desired.
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
Or, the debug format that the compiler and debugger use to
|
46 |
|
|
communicate information about source constructs can be changed via
|
47 |
|
|
-gdwarf-2 or -gstabs 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 |
|
|
-g3 . 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 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
Many other options are available: please see "Options
|
57 |
|
|
for Debugging Your Program" in Using the GNU Compiler
|
58 |
|
|
Collection (GCC) for a complete list.
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
Debug Versions of Library Binary Files
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
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 |
|
|
|
70 |
|
|
|
71 |
|
|
--enable-libstdcxx-debug
|
72 |
|
|
|
73 |
|
|
and perhaps
|
74 |
|
|
|
75 |
|
|
--enable-libstdcxx-debug-flags='...'
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
to create a separate debug build. Both the normal build and the
|
79 |
|
|
debug build will persist, without having to specify
|
80 |
|
|
CXXFLAGS , and the debug library will be installed in a
|
81 |
|
|
separate directory tree, in (prefix)/lib/debug . For
|
82 |
|
|
more information, look at the configuration section.
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
A second approach is to use the configuration flags
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
make CXXFLAGS='-g3 -fno-inline -O0' all
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
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 debug mode.
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
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 mtrace , valgrind ,
|
106 |
|
|
mudflap , and the non-free commercial product
|
107 |
|
|
purify . In addition, libcwd 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 |
|
|
|
112 |
|
|
|
113 |
|
|
|
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 new and delete : there are
|
117 |
|
|
different kinds of allocation schemes that can be used by
|
118 |
|
|
std::allocator . For implementation details, see the mt allocator documentation and
|
119 |
|
|
look specifically for GLIBCXX_FORCE_NEW .
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
In a nutshell, the default allocator used by
|
124 |
|
|
std::allocator 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 |
|
|
|
130 |
|
|
|
131 |
|
|
|
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 |
|
|
|
140 |
|
|
|
141 |
|
|
|
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 __cxa_atexit or
|
145 |
|
|
atexit functions.
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
#include <cstdlib>
|
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 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
or, using __cxa_atexit :
|
165 |
|
|
|
166 |
|
|
|
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 |
|
|
&__dso_handle ? __dso_handle : NULL);
|
177 |
|
|
do_test();
|
178 |
|
|
return 0;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
Suggested valgrind flags, given the suggestions above about setting
|
184 |
|
|
up the runtime environment, library, and test file, might be:
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
valgrind -v --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes a.out
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
All synchronization primitives used in the library internals need to be
|
195 |
|
|
understood by race detectors so that they do not produce false reports.
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
Two annotation macros are used to explain low-level synchronization
|
200 |
|
|
to race detectors:
|
201 |
|
|
_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE() and
|
202 |
|
|
_GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER() .
|
203 |
|
|
By default, these macros are defined empty -- anyone who wants
|
204 |
|
|
to use a race detector needs to redefine them to call an
|
205 |
|
|
appropriate API.
|
206 |
|
|
Since these macros are empty by default when the library is built,
|
207 |
|
|
redefining them will only affect inline functions and template
|
208 |
|
|
instantiations which are compiled in user code. This allows annotation
|
209 |
|
|
of templates such as shared_ptr , but not code which is
|
210 |
|
|
only instantiated in the library. Code which is only instantiated in
|
211 |
|
|
the library needs to be recompiled with the annotation macros defined.
|
212 |
|
|
That can be done by rebuilding the entire
|
213 |
|
|
libstdc++.so file but a simpler
|
214 |
|
|
alternative exists for ELF platforms such as GNU/Linux, because ELF
|
215 |
|
|
symbol interposition allows symbols defined in the shared library to be
|
216 |
|
|
overridden by symbols with the same name that appear earlier in the
|
217 |
|
|
runtime search path. This means you only need to recompile the functions
|
218 |
|
|
that are affected by the annotation macros, which can be done by
|
219 |
|
|
recompiling individual files.
|
220 |
|
|
Annotating std::string and std::wstring
|
221 |
|
|
reference counting can be done by disabling extern templates (by defining
|
222 |
|
|
_GLIBCXX_EXTERN_TEMPLATE=-1 ) or by rebuilding the
|
223 |
|
|
src/string-inst.cc file.
|
224 |
|
|
Annotating the remaining atomic operations (at the time of writing these
|
225 |
|
|
are in ios_base::Init::~Init , locale::_Impl ,
|
226 |
|
|
locale::facet and thread::_M_start_thread )
|
227 |
|
|
requires rebuilding the relevant source files.
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
The approach described above is known to work with the following race
|
232 |
|
|
detection tools:
|
233 |
|
|
|
234 |
|
|
xlink:href="http://valgrind.org/docs/manual/drd-manual.html">
|
235 |
|
|
DRD,
|
236 |
|
|
|
237 |
|
|
xlink:href="http://valgrind.org/docs/manual/hg-manual.html">
|
238 |
|
|
Helgrind, and
|
239 |
|
|
|
240 |
|
|
xlink:href="http://code.google.com/p/data-race-test">
|
241 |
|
|
ThreadSanitizer.
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
With DRD, Helgrind and ThreadSanitizer you will need to define
|
246 |
|
|
the macros like this:
|
247 |
|
|
|
248 |
|
|
#define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(A) ANNOTATE_HAPPENS_BEFORE(A)
|
249 |
|
|
#define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(A) ANNOTATE_HAPPENS_AFTER(A)
|
250 |
|
|
|
251 |
|
|
Refer to the documentation of each particular tool for details.
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
Many options are available for GDB itself: please see
|
263 |
|
|
"GDB features for C++" in the GDB documentation. Also
|
264 |
|
|
recommended: the other parts of this manual.
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
These settings can either be switched on in at the GDB command line,
|
269 |
|
|
or put into a .gdbint file to establish default debugging
|
270 |
|
|
characteristics, like so:
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
set print pretty on
|
275 |
|
|
set print object on
|
276 |
|
|
set print static-members on
|
277 |
|
|
set print vtbl on
|
278 |
|
|
set print demangle on
|
279 |
|
|
set demangle-style gnu-v3
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
Starting with version 7.0, GDB includes support for writing
|
284 |
|
|
pretty-printers in Python. Pretty printers for STL classes are
|
285 |
|
|
distributed with GCC from version 4.5.0. The most recent version of
|
286 |
|
|
these printers are always found in libstdc++ svn repository.
|
287 |
|
|
To enable these printers, check-out the latest printers to a local
|
288 |
|
|
directory:
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
Next, add the following section to your ~/.gdbinit The path must
|
297 |
|
|
match the location where the Python module above was checked-out.
|
298 |
|
|
So if checked out to: /home/maude/gdb_printers/, the path would be as
|
299 |
|
|
written in the example below.
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
python
|
304 |
|
|
import sys
|
305 |
|
|
sys.path.insert(0, '/home/maude/gdb_printers/python')
|
306 |
|
|
from libstdcxx.v6.printers import register_libstdcxx_printers
|
307 |
|
|
register_libstdcxx_printers (None)
|
308 |
|
|
end
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
The path should be the only element that needs to be adjusted in the
|
313 |
|
|
example. Once loaded, STL classes that the printers support
|
314 |
|
|
should print in a more human-readable format. To print the classes
|
315 |
|
|
in the old style, use the /r (raw) switch in the print command
|
316 |
|
|
(i.e., print /r foo). This will print the classes as if the Python
|
317 |
|
|
pretty-printers were not loaded.
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
For additional information on STL support and GDB please visit:
|
322 |
|
|
"GDB Support
|
323 |
|
|
for STL" in the GDB wiki. Additionally, in-depth
|
324 |
|
|
documentation and discussion of the pretty printing feature can be
|
325 |
|
|
found in "Pretty Printing" node in the GDB manual. You can find
|
326 |
|
|
on-line versions of the GDB user manual in GDB's homepage, at
|
327 |
|
|
"GDB: The GNU Project
|
328 |
|
|
Debugger" .
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
Tracking uncaught exceptions
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
The verbose
|
337 |
|
|
termination handler gives information about uncaught
|
338 |
|
|
exceptions which are killing the program. It is described in the
|
339 |
|
|
linked-to page.
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
The Debug Mode
|
346 |
|
|
has compile and run-time checks for many containers.
|
347 |
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
The Compile-Time
|
353 |
|
|
Checks Extension has compile-time checks for many algorithms.
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
Profile-based Performance Analysis
|
358 |
|
|
|
359 |
|
|
The Profile-based
|
360 |
|
|
Performance Analysis Extension has performance checks for many
|
361 |
|
|
algorithms.
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|