1 |
424 |
jeremybenn |
|
2 |
|
|
|
3 |
|
|
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
|
4 |
|
|
[ ]>
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
C++
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
library
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
debug
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
Debug Mode
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
Intro
|
27 |
|
|
|
28 |
|
|
By default, libstdc++ is built with efficiency in mind, and
|
29 |
|
|
therefore performs little or no error checking that is not
|
30 |
|
|
required by the C++ standard. This means that programs that
|
31 |
|
|
incorrectly use the C++ standard library will exhibit behavior
|
32 |
|
|
that is not portable and may not even be predictable, because they
|
33 |
|
|
tread into implementation-specific or undefined behavior. To
|
34 |
|
|
detect some of these errors before they can become problematic,
|
35 |
|
|
libstdc++ offers a debug mode that provides additional checking of
|
36 |
|
|
library facilities, and will report errors in the use of libstdc++
|
37 |
|
|
as soon as they can be detected by emitting a description of the
|
38 |
|
|
problem to standard error and aborting the program. This debug
|
39 |
|
|
mode is available with GCC 3.4.0 and later versions.
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
The libstdc++ debug mode performs checking for many areas of the
|
44 |
|
|
C++ standard, but the focus is on checking interactions among
|
45 |
|
|
standard iterators, containers, and algorithms, including:
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
Safe iterators: Iterators keep track of the
|
50 |
|
|
container whose elements they reference, so errors such as
|
51 |
|
|
incrementing a past-the-end iterator or dereferencing an iterator
|
52 |
|
|
that points to a container that has been destructed are diagnosed
|
53 |
|
|
immediately.
|
54 |
|
|
|
55 |
|
|
Algorithm preconditions: Algorithms attempt to
|
56 |
|
|
validate their input parameters to detect errors as early as
|
57 |
|
|
possible. For instance, the set_intersection
|
58 |
|
|
algorithm requires that its iterator
|
59 |
|
|
parameters first1 and last1 form a valid
|
60 |
|
|
iterator range, and that the sequence
|
61 |
|
|
[first1 , last1 ) is sorted according to
|
62 |
|
|
the same predicate that was passed
|
63 |
|
|
to set_intersection ; the libstdc++ debug mode will
|
64 |
|
|
detect an error if the sequence is not sorted or was sorted by a
|
65 |
|
|
different predicate.
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
Semantics
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
A program that uses the C++ standard library correctly
|
76 |
|
|
will maintain the same semantics under debug mode as it had with
|
77 |
|
|
the normal (release) library. All functional and exception-handling
|
78 |
|
|
guarantees made by the normal library also hold for the debug mode
|
79 |
|
|
library, with one exception: performance guarantees made by the
|
80 |
|
|
normal library may not hold in the debug mode library. For
|
81 |
|
|
instance, erasing an element in a std::list is a
|
82 |
|
|
constant-time operation in normal library, but in debug mode it is
|
83 |
|
|
linear in the number of iterators that reference that particular
|
84 |
|
|
list. So while your (correct) program won't change its results, it
|
85 |
|
|
is likely to execute more slowly.
|
86 |
|
|
|
87 |
|
|
libstdc++ includes many extensions to the C++ standard library. In
|
88 |
|
|
some cases the extensions are obvious, such as the hashed
|
89 |
|
|
associative containers, whereas other extensions give predictable
|
90 |
|
|
results to behavior that would otherwise be undefined, such as
|
91 |
|
|
throwing an exception when a std::basic_string is
|
92 |
|
|
constructed from a NULL character pointer. This latter category also
|
93 |
|
|
includes implementation-defined and unspecified semantics, such as
|
94 |
|
|
the growth rate of a vector. Use of these extensions is not
|
95 |
|
|
considered incorrect, so code that relies on them will not be
|
96 |
|
|
rejected by debug mode. However, use of these extensions may affect
|
97 |
|
|
the portability of code to other implementations of the C++ standard
|
98 |
|
|
library, and is therefore somewhat hazardous. For this reason, the
|
99 |
|
|
libstdc++ debug mode offers a "pedantic" mode (similar to
|
100 |
|
|
GCC's -pedantic compiler flag) that attempts to emulate
|
101 |
|
|
the semantics guaranteed by the C++ standard. For
|
102 |
|
|
instance, constructing a std::basic_string with a NULL
|
103 |
|
|
character pointer would result in an exception under normal mode or
|
104 |
|
|
non-pedantic debug mode (this is a libstdc++ extension), whereas
|
105 |
|
|
under pedantic debug mode libstdc++ would signal an error. To enable
|
106 |
|
|
the pedantic debug mode, compile your program with
|
107 |
|
|
both -D_GLIBCXX_DEBUG
|
108 |
|
|
and -D_GLIBCXX_DEBUG_PEDANTIC .
|
109 |
|
|
(N.B. In GCC 3.4.x and 4.0.0, due to a bug,
|
110 |
|
|
-D_GLIBXX_DEBUG_PEDANTIC was also needed. The problem has
|
111 |
|
|
been fixed in GCC 4.0.1 and later versions.)
|
112 |
|
|
|
113 |
|
|
The following library components provide extra debugging
|
114 |
|
|
capabilities in debug mode:
|
115 |
|
|
|
116 |
|
|
std::basic_string (no safe iterators and see note below)
|
117 |
|
|
std::bitset
|
118 |
|
|
std::deque
|
119 |
|
|
std::list
|
120 |
|
|
std::map
|
121 |
|
|
std::multimap
|
122 |
|
|
std::multiset
|
123 |
|
|
std::set
|
124 |
|
|
std::vector
|
125 |
|
|
std::unordered_map
|
126 |
|
|
std::unordered_multimap
|
127 |
|
|
std::unordered_set
|
128 |
|
|
std::unordered_multiset
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
N.B. although there are precondition checks for some string operations,
|
132 |
|
|
e.g. operator[] ,
|
133 |
|
|
they will not always be run when using the char and
|
134 |
|
|
wchar_t specialisations (std::string and
|
135 |
|
|
std::wstring ). This is because libstdc++ uses GCC's
|
136 |
|
|
extern template extension to provide explicit instantiations
|
137 |
|
|
of std::string and std::wstring , and those
|
138 |
|
|
explicit instantiations don't include the debug-mode checks. If the
|
139 |
|
|
containing functions are inlined then the checks will run, so compiling
|
140 |
|
|
with -O1 might be enough to enable them. Alternatively
|
141 |
|
|
-D_GLIBCXX_EXTERN_TEMPLATE=0 will suppress the declarations
|
142 |
|
|
of the explicit instantiations and cause the functions to be instantiated
|
143 |
|
|
with the debug-mode checks included, but this is unsupported and not
|
144 |
|
|
guaranteed to work. For full debug-mode support you can use the
|
145 |
|
|
__gnu_debug::basic_string debugging container directly,
|
146 |
|
|
which always works correctly.
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
Using
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
Using the Debug Mode
|
157 |
|
|
|
158 |
|
|
To use the libstdc++ debug mode, compile your application with the
|
159 |
|
|
compiler flag -D_GLIBCXX_DEBUG . Note that this flag
|
160 |
|
|
changes the sizes and behavior of standard class templates such
|
161 |
|
|
as std::vector , and therefore you can only link code
|
162 |
|
|
compiled with debug mode and code compiled without debug mode if no
|
163 |
|
|
instantiation of a container is passed between the two translation
|
164 |
|
|
units.
|
165 |
|
|
|
166 |
|
|
By default, error messages are formatted to fit on lines of about
|
167 |
|
|
78 characters. The environment variable
|
168 |
|
|
GLIBCXX_DEBUG_MESSAGE_LENGTH can be used to request a
|
169 |
|
|
different length.
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
Using a Specific Debug Container
|
175 |
|
|
When it is not feasible to recompile your entire application, or
|
176 |
|
|
only specific containers need checking, debugging containers are
|
177 |
|
|
available as GNU extensions. These debugging containers are
|
178 |
|
|
functionally equivalent to the standard drop-in containers used in
|
179 |
|
|
debug mode, but they are available in a separate namespace as GNU
|
180 |
|
|
extensions and may be used in programs compiled with either release
|
181 |
|
|
mode or with debug mode. The
|
182 |
|
|
following table provides the names and headers of the debugging
|
183 |
|
|
containers:
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
187 |
|
|
Debugging Containers
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
195 |
|
|
|
|
196 |
|
|
Container
|
197 |
|
|
Header
|
198 |
|
|
Debug container
|
199 |
|
|
Debug header
|
200 |
|
|
|
201 |
|
|
|
|
202 |
|
|
203 |
|
|
|
|
204 |
|
|
std::bitset
|
205 |
|
|
|
206 |
|
|
__gnu_debug::bitset
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
|
|
210 |
|
|
std::deque
|
211 |
|
|
|
212 |
|
|
__gnu_debug::deque
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
|
216 |
|
|
std::list
|
217 |
|
|
|
218 |
|
|
__gnu_debug::list
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
|
222 |
|
|
std::map
|
223 |
|
|
|
224 |
|
|
__gnu_debug::map
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
|
|
228 |
|
|
std::multimap
|
229 |
|
|
|
230 |
|
|
__gnu_debug::multimap
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
|
234 |
|
|
std::multiset
|
235 |
|
|
|
236 |
|
|
__gnu_debug::multiset
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
|
240 |
|
|
std::set
|
241 |
|
|
|
242 |
|
|
__gnu_debug::set
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
|
246 |
|
|
std::string
|
247 |
|
|
|
248 |
|
|
__gnu_debug::string
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
|
252 |
|
|
std::wstring
|
253 |
|
|
|
254 |
|
|
__gnu_debug::wstring
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
|
258 |
|
|
std::basic_string
|
259 |
|
|
|
260 |
|
|
__gnu_debug::basic_string
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
|
264 |
|
|
std::vector
|
265 |
|
|
|
266 |
|
|
__gnu_debug::vector
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
|
270 |
|
|
|
271 |
|
|
|
|
272 |
|
|
|
273 |
|
|
In addition, when compiling in C++0x mode, these additional
|
274 |
|
|
containers have additional debug capability.
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
278 |
|
|
Debugging Containers C++0x
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
286 |
|
|
|
|
287 |
|
|
Container
|
288 |
|
|
Header
|
289 |
|
|
Debug container
|
290 |
|
|
Debug header
|
291 |
|
|
|
292 |
|
|
|
|
293 |
|
|
294 |
|
|
|
|
295 |
|
|
std::unordered_map
|
296 |
|
|
|
297 |
|
|
__gnu_debug::unordered_map
|
298 |
|
|
|
299 |
|
|
|
300 |
|
|
|
|
301 |
|
|
std::unordered_multimap
|
302 |
|
|
|
303 |
|
|
__gnu_debug::unordered_multimap
|
304 |
|
|
|
305 |
|
|
|
306 |
|
|
|
|
307 |
|
|
std::unordered_set
|
308 |
|
|
|
309 |
|
|
__gnu_debug::unordered_set
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
|
|
313 |
|
|
std::unordered_multiset
|
314 |
|
|
|
315 |
|
|
__gnu_debug::unordered_multiset
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
|
|
319 |
|
|
|
320 |
|
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
Design
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
Goals
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
The libstdc++ debug mode replaces unsafe (but efficient) standard
|
333 |
|
|
containers and iterators with semantically equivalent safe standard
|
334 |
|
|
containers and iterators to aid in debugging user programs. The
|
335 |
|
|
following goals directed the design of the libstdc++ debug mode:
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
Correctness: the libstdc++ debug mode must not change
|
340 |
|
|
the semantics of the standard library for all cases specified in
|
341 |
|
|
the ANSI/ISO C++ standard. The essence of this constraint is that
|
342 |
|
|
any valid C++ program should behave in the same manner regardless
|
343 |
|
|
of whether it is compiled with debug mode or release mode. In
|
344 |
|
|
particular, entities that are defined in namespace std in release
|
345 |
|
|
mode should remain defined in namespace std in debug mode, so that
|
346 |
|
|
legal specializations of namespace std entities will remain
|
347 |
|
|
valid. A program that is not valid C++ (e.g., invokes undefined
|
348 |
|
|
behavior) is not required to behave similarly, although the debug
|
349 |
|
|
mode will abort with a diagnostic when it detects undefined
|
350 |
|
|
behavior.
|
351 |
|
|
|
352 |
|
|
Performance: the additional of the libstdc++ debug mode
|
353 |
|
|
must not affect the performance of the library when it is compiled
|
354 |
|
|
in release mode. Performance of the libstdc++ debug mode is
|
355 |
|
|
secondary (and, in fact, will be worse than the release
|
356 |
|
|
mode).
|
357 |
|
|
|
358 |
|
|
Usability: the libstdc++ debug mode should be easy to
|
359 |
|
|
use. It should be easily incorporated into the user's development
|
360 |
|
|
environment (e.g., by requiring only a single new compiler switch)
|
361 |
|
|
and should produce reasonable diagnostics when it detects a
|
362 |
|
|
problem with the user program. Usability also involves detection
|
363 |
|
|
of errors when using the debug mode incorrectly, e.g., by linking
|
364 |
|
|
a release-compiled object against a debug-compiled object if in
|
365 |
|
|
fact the resulting program will not run correctly.
|
366 |
|
|
|
367 |
|
|
Minimize recompilation: While it is expected that
|
368 |
|
|
users recompile at least part of their program to use debug
|
369 |
|
|
mode, the amount of recompilation affects the
|
370 |
|
|
detect-compile-debug turnaround time. This indirectly affects the
|
371 |
|
|
usefulness of the debug mode, because debugging some applications
|
372 |
|
|
may require rebuilding a large amount of code, which may not be
|
373 |
|
|
feasible when the suspect code may be very localized. There are
|
374 |
|
|
several levels of conformance to this requirement, each with its
|
375 |
|
|
own usability and implementation characteristics. In general, the
|
376 |
|
|
higher-numbered conformance levels are more usable (i.e., require
|
377 |
|
|
less recompilation) but are more complicated to implement than
|
378 |
|
|
the lower-numbered conformance levels.
|
379 |
|
|
|
380 |
|
|
Full recompilation: The user must recompile his or
|
381 |
|
|
her entire application and all C++ libraries it depends on,
|
382 |
|
|
including the C++ standard library that ships with the
|
383 |
|
|
compiler. This must be done even if only a small part of the
|
384 |
|
|
program can use debugging features.
|
385 |
|
|
|
386 |
|
|
Full user recompilation: The user must recompile
|
387 |
|
|
his or her entire application and all C++ libraries it depends
|
388 |
|
|
on, but not the C++ standard library itself. This must be done
|
389 |
|
|
even if only a small part of the program can use debugging
|
390 |
|
|
features. This can be achieved given a full recompilation
|
391 |
|
|
system by compiling two versions of the standard library when
|
392 |
|
|
the compiler is installed and linking against the appropriate
|
393 |
|
|
one, e.g., a multilibs approach.
|
394 |
|
|
|
395 |
|
|
Partial recompilation: The user must recompile the
|
396 |
|
|
parts of his or her application and the C++ libraries it
|
397 |
|
|
depends on that will use the debugging facilities
|
398 |
|
|
directly. This means that any code that uses the debuggable
|
399 |
|
|
standard containers would need to be recompiled, but code
|
400 |
|
|
that does not use them (but may, for instance, use IOStreams)
|
401 |
|
|
would not have to be recompiled.
|
402 |
|
|
|
403 |
|
|
Per-use recompilation: The user must recompile the
|
404 |
|
|
parts of his or her application and the C++ libraries it
|
405 |
|
|
depends on where debugging should occur, and any other code
|
406 |
|
|
that interacts with those containers. This means that a set of
|
407 |
|
|
translation units that accesses a particular standard
|
408 |
|
|
container instance may either be compiled in release mode (no
|
409 |
|
|
checking) or debug mode (full checking), but must all be
|
410 |
|
|
compiled in the same way; a translation unit that does not see
|
411 |
|
|
that standard container instance need not be recompiled. This
|
412 |
|
|
also means that a translation unit A that contains a
|
413 |
|
|
particular instantiation
|
414 |
|
|
(say, std::vector<int> ) compiled in release
|
415 |
|
|
mode can be linked against a translation unit B that
|
416 |
|
|
contains the same instantiation compiled in debug mode (a
|
417 |
|
|
feature not present with partial recompilation). While this
|
418 |
|
|
behavior is technically a violation of the One Definition
|
419 |
|
|
Rule, this ability tends to be very important in
|
420 |
|
|
practice. The libstdc++ debug mode supports this level of
|
421 |
|
|
recompilation.
|
422 |
|
|
|
423 |
|
|
Per-unit recompilation: The user must only
|
424 |
|
|
recompile the translation units where checking should occur,
|
425 |
|
|
regardless of where debuggable standard containers are
|
426 |
|
|
used. This has also been dubbed "-g mode",
|
427 |
|
|
because the -g compiler switch works in this way,
|
428 |
|
|
emitting debugging information at a per--translation-unit
|
429 |
|
|
granularity. We believe that this level of recompilation is in
|
430 |
|
|
fact not possible if we intend to supply safe iterators, leave
|
431 |
|
|
the program semantics unchanged, and not regress in
|
432 |
|
|
performance under release mode because we cannot associate
|
433 |
|
|
extra information with an iterator (to form a safe iterator)
|
434 |
|
|
without either reserving that space in release mode
|
435 |
|
|
(performance regression) or allocating extra memory associated
|
436 |
|
|
with each iterator with new (changes the program
|
437 |
|
|
semantics).
|
438 |
|
|
|
439 |
|
|
|
440 |
|
|
|
441 |
|
|
|
442 |
|
|
|
443 |
|
|
|
444 |
|
|
Methods
|
445 |
|
|
|
446 |
|
|
|
447 |
|
|
This section provides an overall view of the design of the
|
448 |
|
|
libstdc++ debug mode and details the relationship between design
|
449 |
|
|
decisions and the stated design goals.
|
450 |
|
|
|
451 |
|
|
|
452 |
|
|
The Wrapper Model
|
453 |
|
|
The libstdc++ debug mode uses a wrapper model where the
|
454 |
|
|
debugging versions of library components (e.g., iterators and
|
455 |
|
|
containers) form a layer on top of the release versions of the
|
456 |
|
|
library components. The debugging components first verify that the
|
457 |
|
|
operation is correct (aborting with a diagnostic if an error is
|
458 |
|
|
found) and will then forward to the underlying release-mode
|
459 |
|
|
container that will perform the actual work. This design decision
|
460 |
|
|
ensures that we cannot regress release-mode performance (because the
|
461 |
|
|
release-mode containers are left untouched) and partially
|
462 |
|
|
enables mixing debug and
|
463 |
|
|
release code at link time, although that will not be
|
464 |
|
|
discussed at this time.
|
465 |
|
|
|
466 |
|
|
Two types of wrappers are used in the implementation of the debug
|
467 |
|
|
mode: container wrappers and iterator wrappers. The two types of
|
468 |
|
|
wrappers interact to maintain relationships between iterators and
|
469 |
|
|
their associated containers, which are necessary to detect certain
|
470 |
|
|
types of standard library usage errors such as dereferencing
|
471 |
|
|
past-the-end iterators or inserting into a container using an
|
472 |
|
|
iterator from a different container.
|
473 |
|
|
|
474 |
|
|
|
475 |
|
|
Safe Iterators
|
476 |
|
|
Iterator wrappers provide a debugging layer over any iterator that
|
477 |
|
|
is attached to a particular container, and will manage the
|
478 |
|
|
information detailing the iterator's state (singular,
|
479 |
|
|
dereferenceable, etc.) and tracking the container to which the
|
480 |
|
|
iterator is attached. Because iterators have a well-defined, common
|
481 |
|
|
interface the iterator wrapper is implemented with the iterator
|
482 |
|
|
adaptor class template __gnu_debug::_Safe_iterator ,
|
483 |
|
|
which takes two template parameters:
|
484 |
|
|
|
485 |
|
|
|
486 |
|
|
Iterator : The underlying iterator type, which must
|
487 |
|
|
be either the iterator or const_iterator
|
488 |
|
|
typedef from the sequence type this iterator can reference.
|
489 |
|
|
|
490 |
|
|
Sequence : The type of sequence that this iterator
|
491 |
|
|
references. This sequence must be a safe sequence (discussed below)
|
492 |
|
|
whose iterator or const_iterator typedef
|
493 |
|
|
is the type of the safe iterator.
|
494 |
|
|
|
495 |
|
|
|
496 |
|
|
|
497 |
|
|
|
498 |
|
|
Safe Sequences (Containers)
|
499 |
|
|
|
500 |
|
|
Container wrappers provide a debugging layer over a particular
|
501 |
|
|
container type. Because containers vary greatly in the member
|
502 |
|
|
functions they support and the semantics of those member functions
|
503 |
|
|
(especially in the area of iterator invalidation), container
|
504 |
|
|
wrappers are tailored to the container they reference, e.g., the
|
505 |
|
|
debugging version of std::list duplicates the entire
|
506 |
|
|
interface of std::list , adding additional semantic
|
507 |
|
|
checks and then forwarding operations to the
|
508 |
|
|
real std::list (a public base class of the debugging
|
509 |
|
|
version) as appropriate. However, all safe containers inherit from
|
510 |
|
|
the class template __gnu_debug::_Safe_sequence ,
|
511 |
|
|
instantiated with the type of the safe container itself (an instance
|
512 |
|
|
of the curiously recurring template pattern).
|
513 |
|
|
|
514 |
|
|
The iterators of a container wrapper will be
|
515 |
|
|
safe
|
516 |
|
|
iterators that reference sequences of this type and wrap the
|
517 |
|
|
iterators provided by the release-mode base class. The debugging
|
518 |
|
|
container will use only the safe iterators within its own interface
|
519 |
|
|
(therefore requiring the user to use safe iterators, although this
|
520 |
|
|
does not change correct user code) and will communicate with the
|
521 |
|
|
release-mode base class with only the underlying, unsafe,
|
522 |
|
|
release-mode iterators that the base class exports.
|
523 |
|
|
|
524 |
|
|
The debugging version of std::list will have the
|
525 |
|
|
following basic structure:
|
526 |
|
|
|
527 |
|
|
|
528 |
|
|
template<typename _Tp, typename _Allocator = allocator<_Tp>
|
529 |
|
|
class debug-list :
|
530 |
|
|
public release-list<_Tp, _Allocator>,
|
531 |
|
|
public __gnu_debug::_Safe_sequence<debug-list<_Tp, _Allocator> >
|
532 |
|
|
{
|
533 |
|
|
typedef release-list<_Tp, _Allocator> _Base;
|
534 |
|
|
typedef debug-list<_Tp, _Allocator> _Self;
|
535 |
|
|
|
536 |
|
|
public:
|
537 |
|
|
typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, _Self> iterator;
|
538 |
|
|
typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, _Self> const_iterator;
|
539 |
|
|
|
540 |
|
|
// duplicate std::list interface with debugging semantics
|
541 |
|
|
};
|
542 |
|
|
|
543 |
|
|
|
544 |
|
|
|
545 |
|
|
|
546 |
|
|
|
547 |
|
|
Precondition Checking
|
548 |
|
|
The debug mode operates primarily by checking the preconditions of
|
549 |
|
|
all standard library operations that it supports. Preconditions that
|
550 |
|
|
are always checked (regardless of whether or not we are in debug
|
551 |
|
|
mode) are checked via the __check_xxx macros defined
|
552 |
|
|
and documented in the source
|
553 |
|
|
file include/debug/debug.h . Preconditions that may or
|
554 |
|
|
may not be checked, depending on the debug-mode
|
555 |
|
|
macro _GLIBCXX_DEBUG , are checked via
|
556 |
|
|
the __requires_xxx macros defined and documented in the
|
557 |
|
|
same source file. Preconditions are validated using any additional
|
558 |
|
|
information available at run-time, e.g., the containers that are
|
559 |
|
|
associated with a particular iterator, the position of the iterator
|
560 |
|
|
within those containers, the distance between two iterators that may
|
561 |
|
|
form a valid range, etc. In the absence of suitable information,
|
562 |
|
|
e.g., an input iterator that is not a safe iterator, these
|
563 |
|
|
precondition checks will silently succeed.
|
564 |
|
|
|
565 |
|
|
The majority of precondition checks use the aforementioned macros,
|
566 |
|
|
which have the secondary benefit of having prewritten debug
|
567 |
|
|
messages that use information about the current status of the
|
568 |
|
|
objects involved (e.g., whether an iterator is singular or what
|
569 |
|
|
sequence it is attached to) along with some static information
|
570 |
|
|
(e.g., the names of the function parameters corresponding to the
|
571 |
|
|
objects involved). When not using these macros, the debug mode uses
|
572 |
|
|
either the debug-mode assertion
|
573 |
|
|
macro _GLIBCXX_DEBUG_ASSERT , its pedantic
|
574 |
|
|
cousin _GLIBCXX_DEBUG_PEDASSERT , or the assertion
|
575 |
|
|
check macro that supports more advance formulation of error
|
576 |
|
|
messages, _GLIBCXX_DEBUG_VERIFY . These macros are
|
577 |
|
|
documented more thoroughly in the debug mode source code.
|
578 |
|
|
|
579 |
|
|
|
580 |
|
|
|
581 |
|
|
Release- and debug-mode coexistence
|
582 |
|
|
The libstdc++ debug mode is the first debug mode we know of that
|
583 |
|
|
is able to provide the "Per-use recompilation" (4) guarantee, that
|
584 |
|
|
allows release-compiled and debug-compiled code to be linked and
|
585 |
|
|
executed together without causing unpredictable behavior. This
|
586 |
|
|
guarantee minimizes the recompilation that users are required to
|
587 |
|
|
perform, shortening the detect-compile-debug bug hunting cycle
|
588 |
|
|
and making the debug mode easier to incorporate into development
|
589 |
|
|
environments by minimizing dependencies.
|
590 |
|
|
|
591 |
|
|
Achieving link- and run-time coexistence is not a trivial
|
592 |
|
|
implementation task. To achieve this goal we required a small
|
593 |
|
|
extension to the GNU C++ compiler (since incorporated into the C++0x language specification, described in the GCC Manual for the C++ language as
|
594 |
|
|
namespace
|
595 |
|
|
association), and a complex organization of debug- and
|
596 |
|
|
release-modes. The end result is that we have achieved per-use
|
597 |
|
|
recompilation but have had to give up some checking of the
|
598 |
|
|
std::basic_string class template (namely, safe
|
599 |
|
|
iterators).
|
600 |
|
|
|
601 |
|
|
|
602 |
|
|
|
603 |
|
|
Compile-time coexistence of release- and debug-mode components
|
604 |
|
|
|
605 |
|
|
Both the release-mode components and the debug-mode
|
606 |
|
|
components need to exist within a single translation unit so that
|
607 |
|
|
the debug versions can wrap the release versions. However, only one
|
608 |
|
|
of these components should be user-visible at any particular
|
609 |
|
|
time with the standard name, e.g., std::list .
|
610 |
|
|
|
611 |
|
|
In release mode, we define only the release-mode version of the
|
612 |
|
|
component with its standard name and do not include the debugging
|
613 |
|
|
component at all. The release mode version is defined within the
|
614 |
|
|
namespace std . Minus the namespace associations, this
|
615 |
|
|
method leaves the behavior of release mode completely unchanged from
|
616 |
|
|
its behavior prior to the introduction of the libstdc++ debug
|
617 |
|
|
mode. Here's an example of what this ends up looking like, in
|
618 |
|
|
C++.
|
619 |
|
|
|
620 |
|
|
|
621 |
|
|
namespace std
|
622 |
|
|
{
|
623 |
|
|
template<typename _Tp, typename _Alloc = allocator<_Tp> >
|
624 |
|
|
class list
|
625 |
|
|
{
|
626 |
|
|
// ...
|
627 |
|
|
};
|
628 |
|
|
} // namespace std
|
629 |
|
|
|
630 |
|
|
|
631 |
|
|
In debug mode we include the release-mode container (which is now
|
632 |
|
|
defined in the namespace __norm ) and also the
|
633 |
|
|
debug-mode container. The debug-mode container is defined within the
|
634 |
|
|
namespace __debug , which is associated with namespace
|
635 |
|
|
std via the C++0x namespace association language feature. This
|
636 |
|
|
method allows the debug and release versions of the same component to
|
637 |
|
|
coexist at compile-time and link-time without causing an unreasonable
|
638 |
|
|
maintenance burden, while minimizing confusion. Again, this boils down
|
639 |
|
|
to C++ code as follows:
|
640 |
|
|
|
641 |
|
|
|
642 |
|
|
namespace std
|
643 |
|
|
{
|
644 |
|
|
namespace __norm
|
645 |
|
|
{
|
646 |
|
|
template<typename _Tp, typename _Alloc = allocator<_Tp> >
|
647 |
|
|
class list
|
648 |
|
|
{
|
649 |
|
|
// ...
|
650 |
|
|
};
|
651 |
|
|
} // namespace __gnu_norm
|
652 |
|
|
|
653 |
|
|
namespace __debug
|
654 |
|
|
{
|
655 |
|
|
template<typename _Tp, typename _Alloc = allocator<_Tp> >
|
656 |
|
|
class list
|
657 |
|
|
: public __norm::list<_Tp, _Alloc>,
|
658 |
|
|
public __gnu_debug::_Safe_sequence<list<_Tp, _Alloc> >
|
659 |
|
|
{
|
660 |
|
|
// ...
|
661 |
|
|
};
|
662 |
|
|
} // namespace __norm
|
663 |
|
|
|
664 |
|
|
// namespace __debug __attribute__ ((strong));
|
665 |
|
|
inline namespace __debug { }
|
666 |
|
|
}
|
667 |
|
|
|
668 |
|
|
|
669 |
|
|
|
670 |
|
|
|
671 |
|
|
Link- and run-time coexistence of release- and
|
672 |
|
|
debug-mode components
|
673 |
|
|
|
674 |
|
|
Because each component has a distinct and separate release and
|
675 |
|
|
debug implementation, there is no issue with link-time
|
676 |
|
|
coexistence: the separate namespaces result in different mangled
|
677 |
|
|
names, and thus unique linkage.
|
678 |
|
|
|
679 |
|
|
However, components that are defined and used within the C++
|
680 |
|
|
standard library itself face additional constraints. For instance,
|
681 |
|
|
some of the member functions of std::moneypunct return
|
682 |
|
|
std::basic_string . Normally, this is not a problem, but
|
683 |
|
|
with a mixed mode standard library that could be using either
|
684 |
|
|
debug-mode or release-mode basic_string objects, things
|
685 |
|
|
get more complicated. As the return value of a function is not
|
686 |
|
|
encoded into the mangled name, there is no way to specify a
|
687 |
|
|
release-mode or a debug-mode string. In practice, this results in
|
688 |
|
|
runtime errors. A simplified example of this problem is as follows.
|
689 |
|
|
|
690 |
|
|
|
691 |
|
|
Take this translation unit, compiled in debug-mode:
|
692 |
|
|
|
693 |
|
|
// -D_GLIBCXX_DEBUG
|
694 |
|
|
#include <string>
|
695 |
|
|
|
696 |
|
|
std::string test02();
|
697 |
|
|
|
698 |
|
|
std::string test01()
|
699 |
|
|
{
|
700 |
|
|
return test02();
|
701 |
|
|
}
|
702 |
|
|
|
703 |
|
|
int main()
|
704 |
|
|
{
|
705 |
|
|
test01();
|
706 |
|
|
return 0;
|
707 |
|
|
}
|
708 |
|
|
|
709 |
|
|
|
710 |
|
|
... and linked to this translation unit, compiled in release mode:
|
711 |
|
|
|
712 |
|
|
|
713 |
|
|
#include <string>
|
714 |
|
|
|
715 |
|
|
std::string
|
716 |
|
|
test02()
|
717 |
|
|
{
|
718 |
|
|
return std::string("toast");
|
719 |
|
|
}
|
720 |
|
|
|
721 |
|
|
|
722 |
|
|
For this reason we cannot easily provide safe iterators for
|
723 |
|
|
the std::basic_string class template, as it is present
|
724 |
|
|
throughout the C++ standard library. For instance, locale facets
|
725 |
|
|
define typedefs that include basic_string : in a mixed
|
726 |
|
|
debug/release program, should that typedef be based on the
|
727 |
|
|
debug-mode basic_string or the
|
728 |
|
|
release-mode basic_string ? While the answer could be
|
729 |
|
|
"both", and the difference hidden via renaming a la the
|
730 |
|
|
debug/release containers, we must note two things about locale
|
731 |
|
|
facets:
|
732 |
|
|
|
733 |
|
|
|
734 |
|
|
They exist as shared state: one can create a facet in one
|
735 |
|
|
translation unit and access the facet via the same type name in a
|
736 |
|
|
different translation unit. This means that we cannot have two
|
737 |
|
|
different versions of locale facets, because the types would not be
|
738 |
|
|
the same across debug/release-mode translation unit barriers.
|
739 |
|
|
|
740 |
|
|
They have virtual functions returning strings: these functions
|
741 |
|
|
mangle in the same way regardless of the mangling of their return
|
742 |
|
|
types (see above), and their precise signatures can be relied upon
|
743 |
|
|
by users because they may be overridden in derived classes.
|
744 |
|
|
|
745 |
|
|
|
746 |
|
|
With the design of libstdc++ debug mode, we cannot effectively hide
|
747 |
|
|
the differences between debug and release-mode strings from the
|
748 |
|
|
user. Failure to hide the differences may result in unpredictable
|
749 |
|
|
behavior, and for this reason we have opted to only
|
750 |
|
|
perform basic_string changes that do not require ABI
|
751 |
|
|
changes. The effect on users is expected to be minimal, as there are
|
752 |
|
|
simple alternatives (e.g., __gnu_debug::basic_string ),
|
753 |
|
|
and the usability benefit we gain from the ability to mix debug- and
|
754 |
|
|
release-compiled translation units is enormous.
|
755 |
|
|
|
756 |
|
|
|
757 |
|
|
|
758 |
|
|
Alternatives for Coexistence
|
759 |
|
|
|
760 |
|
|
The coexistence scheme above was chosen over many alternatives,
|
761 |
|
|
including language-only solutions and solutions that also required
|
762 |
|
|
extensions to the C++ front end. The following is a partial list of
|
763 |
|
|
solutions, with justifications for our rejection of each.
|
764 |
|
|
|
765 |
|
|
|
766 |
|
|
Completely separate debug/release libraries: This is by
|
767 |
|
|
far the simplest implementation option, where we do not allow any
|
768 |
|
|
coexistence of debug- and release-compiled translation units in a
|
769 |
|
|
program. This solution has an extreme negative affect on usability,
|
770 |
|
|
because it is quite likely that some libraries an application
|
771 |
|
|
depends on cannot be recompiled easily. This would not meet
|
772 |
|
|
our usability or minimize recompilation criteria
|
773 |
|
|
well.
|
774 |
|
|
|
775 |
|
|
Add a Debug boolean template parameter:
|
776 |
|
|
Partial specialization could be used to select the debug
|
777 |
|
|
implementation when Debug == true , and the state
|
778 |
|
|
of _GLIBCXX_DEBUG could decide whether the
|
779 |
|
|
default Debug argument is true
|
780 |
|
|
or false . This option would break conformance with the
|
781 |
|
|
C++ standard in both debug and release modes. This would
|
782 |
|
|
not meet our correctness criteria.
|
783 |
|
|
|
784 |
|
|
Packaging a debug flag in the allocators: We could
|
785 |
|
|
reuse the Allocator template parameter of containers
|
786 |
|
|
by adding a sentinel wrapper debug<> that
|
787 |
|
|
signals the user's intention to use debugging, and pick up
|
788 |
|
|
the debug<> allocator wrapper in a partial
|
789 |
|
|
specialization. However, this has two drawbacks: first, there is a
|
790 |
|
|
conformance issue because the default allocator would not be the
|
791 |
|
|
standard-specified std::allocator<T> . Secondly
|
792 |
|
|
(and more importantly), users that specify allocators instead of
|
793 |
|
|
implicitly using the default allocator would not get debugging
|
794 |
|
|
containers. Thus this solution fails the correctness
|
795 |
|
|
criteria.
|
796 |
|
|
|
797 |
|
|
Define debug containers in another namespace, and employ
|
798 |
|
|
a using declaration (or directive): This is an
|
799 |
|
|
enticing option, because it would eliminate the need for
|
800 |
|
|
the link_name extension by aliasing the
|
801 |
|
|
templates. However, there is no true template aliasing mechanism
|
802 |
|
|
in C++, because both using directives and using
|
803 |
|
|
declarations disallow specialization. This method fails
|
804 |
|
|
the correctness criteria.
|
805 |
|
|
|
806 |
|
|
Use implementation-specific properties of anonymous
|
807 |
|
|
namespaces.
|
808 |
|
|
See this post
|
809 |
|
|
|
810 |
|
|
This method fails the correctness criteria.
|
811 |
|
|
|
812 |
|
|
Extension: allow reopening on namespaces: This would
|
813 |
|
|
allow the debug mode to effectively alias the
|
814 |
|
|
namespace std to an internal namespace, such
|
815 |
|
|
as __gnu_std_debug , so that it is completely
|
816 |
|
|
separate from the release-mode std namespace. While
|
817 |
|
|
this will solve some renaming problems and ensure that
|
818 |
|
|
debug- and release-compiled code cannot be mixed unsafely, it ensures that
|
819 |
|
|
debug- and release-compiled code cannot be mixed at all. For
|
820 |
|
|
instance, the program would have two std::cout
|
821 |
|
|
objects! This solution would fails the minimize
|
822 |
|
|
recompilation requirement, because we would only be able to
|
823 |
|
|
support option (1) or (2).
|
824 |
|
|
|
825 |
|
|
Extension: use link name: This option involves
|
826 |
|
|
complicated re-naming between debug-mode and release-mode
|
827 |
|
|
components at compile time, and then a g++ extension called
|
828 |
|
|
link name to recover the original names at link time. There
|
829 |
|
|
are two drawbacks to this approach. One, it's very verbose,
|
830 |
|
|
relying on macro renaming at compile time and several levels of
|
831 |
|
|
include ordering. Two, ODR issues remained with container member
|
832 |
|
|
functions taking no arguments in mixed-mode settings resulting in
|
833 |
|
|
equivalent link names, vector::push_back() being
|
834 |
|
|
one example.
|
835 |
|
|
See link
|
836 |
|
|
name
|
837 |
|
|
|
838 |
|
|
|
839 |
|
|
Other options may exist for implementing the debug mode, many of
|
840 |
|
|
which have probably been considered and others that may still be
|
841 |
|
|
lurking. This list may be expanded over time to include other
|
842 |
|
|
options that we could have implemented, but in all cases the full
|
843 |
|
|
ramifications of the approach (as measured against the design goals
|
844 |
|
|
for a libstdc++ debug mode) should be considered first. The DejaGNU
|
845 |
|
|
testsuite includes some testcases that check for known problems with
|
846 |
|
|
some solutions (e.g., the using declaration solution
|
847 |
|
|
that breaks user specialization), and additional testcases will be
|
848 |
|
|
added as we are able to identify other typical problem cases. These
|
849 |
|
|
test cases will serve as a benchmark by which we can compare debug
|
850 |
|
|
mode implementations.
|
851 |
|
|
|
852 |
|
|
|
853 |
|
|
|
854 |
|
|
|
855 |
|
|
|
856 |
|
|
Other Implementations
|
857 |
|
|
|
858 |
|
|
|
859 |
|
|
There are several existing implementations of debug modes for C++
|
860 |
|
|
standard library implementations, although none of them directly
|
861 |
|
|
supports debugging for programs using libstdc++. The existing
|
862 |
|
|
implementations include:
|
863 |
|
|
|
864 |
|
|
SafeSTL:
|
865 |
|
|
SafeSTL was the original debugging version of the Standard Template
|
866 |
|
|
Library (STL), implemented by Cay S. Horstmann on top of the
|
867 |
|
|
Hewlett-Packard STL. Though it inspired much work in this area, it
|
868 |
|
|
has not been kept up-to-date for use with modern compilers or C++
|
869 |
|
|
standard library implementations.
|
870 |
|
|
|
871 |
|
|
STLport: STLport is a free
|
872 |
|
|
implementation of the C++ standard library derived from the SGI implementation, and
|
873 |
|
|
ported to many other platforms. It includes a debug mode that uses a
|
874 |
|
|
wrapper model (that in some ways inspired the libstdc++ debug mode
|
875 |
|
|
design), although at the time of this writing the debug mode is
|
876 |
|
|
somewhat incomplete and meets only the "Full user recompilation" (2)
|
877 |
|
|
recompilation guarantee by requiring the user to link against a
|
878 |
|
|
different library in debug mode vs. release mode.
|
879 |
|
|
|
880 |
|
|
Metrowerks CodeWarrior: The C++ standard library
|
881 |
|
|
that ships with Metrowerks CodeWarrior includes a debug mode. It is
|
882 |
|
|
a full debug-mode implementation (including debugging for
|
883 |
|
|
CodeWarrior extensions) and is easy to use, although it meets only
|
884 |
|
|
the "Full recompilation" (1) recompilation
|
885 |
|
|
guarantee.
|
886 |
|
|
|
887 |
|
|
|
888 |
|
|
|
889 |
|
|
|
890 |
|
|
|
891 |
|
|
|