]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/doc/xml/manual/debug_mode.xml
Inital import
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / doc / xml / manual / debug_mode.xml
1 <?xml version='1.0'?>
2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" 
3  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" 
4 [ ]>
5
6 <chapter id="manual.ext.debug_mode" xreflabel="Debug Mode">
7 <?dbhtml filename="debug_mode.html"?>
8  
9 <chapterinfo>
10   <keywordset>
11     <keyword>
12       C++
13     </keyword>
14     <keyword>
15       library
16     </keyword>
17     <keyword>
18       debug
19     </keyword>
20   </keywordset>
21 </chapterinfo>
22
23 <title>Debug Mode</title>
24
25 <sect1 id="manual.ext.debug_mode.intro" xreflabel="Intro">
26   <title>Intro</title>
27   <para>
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   </para>
41
42   <para>
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   </para>
47
48   <itemizedlist>
49     <listitem><para><emphasis>Safe iterators</emphasis>: 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.</para></listitem>
54     
55     <listitem><para><emphasis>Algorithm preconditions</emphasis>: Algorithms attempt to
56     validate their input parameters to detect errors as early as
57     possible. For instance, the <code>set_intersection</code>
58     algorithm requires that its iterator
59     parameters <code>first1</code> and <code>last1</code> form a valid
60     iterator range, and that the sequence
61     [<code>first1</code>, <code>last1</code>) is sorted according to
62     the same predicate that was passed
63     to <code>set_intersection</code>; the libstdc++ debug mode will
64     detect an error if the sequence is not sorted or was sorted by a
65     different predicate.</para></listitem>
66   </itemizedlist>
67
68 </sect1>
69
70 <sect1 id="manual.ext.debug_mode.semantics" xreflabel="Semantics">
71   <title>Semantics</title>
72   <para>
73   </para>
74
75 <para>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 <code>std::list</code> 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.</para>
86
87 <para>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 <code>std::basic_string</code> 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 <code>-pedantic</code> compiler flag) that attempts to emulate
101   the semantics guaranteed by the C++ standard. For
102   instance, constructing a <code>std::basic_string</code> 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 <code>-D_GLIBCXX_DEBUG</code>
108   and <code>-D_GLIBCXX_DEBUG_PEDANTIC</code> .
109   (N.B. In GCC 3.4.x and 4.0.0, due to a bug,
110   <code>-D_GLIBXX_DEBUG_PEDANTIC</code> was also needed. The problem has
111   been fixed in GCC 4.0.1 and later versions.) </para>
112
113 <para>The following library components provide extra debugging
114   capabilities in debug mode:</para>
115 <itemizedlist>
116   <listitem><para><code>std::basic_string</code> (no safe iterators and see note below)</para></listitem>
117   <listitem><para><code>std::bitset</code></para></listitem>
118   <listitem><para><code>std::deque</code></para></listitem>
119   <listitem><para><code>std::list</code></para></listitem>
120   <listitem><para><code>std::map</code></para></listitem>
121   <listitem><para><code>std::multimap</code></para></listitem>
122   <listitem><para><code>std::multiset</code></para></listitem>
123   <listitem><para><code>std::set</code></para></listitem>
124   <listitem><para><code>std::vector</code></para></listitem>
125   <listitem><para><code>std::unordered_map</code></para></listitem>
126   <listitem><para><code>std::unordered_multimap</code></para></listitem>
127   <listitem><para><code>std::unordered_set</code></para></listitem>
128   <listitem><para><code>std::unordered_multiset</code></para></listitem>
129 </itemizedlist>
130
131 <para>N.B. although there are precondition checks for some string operations,
132 e.g.  <code>operator[]</code>,
133 they will not always be run when using the <code>char</code> and
134 <code>wchar_t</code> specialisations (<code>std::string</code> and
135 <code>std::wstring</code>).  This is because libstdc++ uses GCC's
136 <code>extern template</code> extension to provide explicit instantiations
137 of <code>std::string</code> and <code>std::wstring</code>, 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 <code>-O1</code> might be enough to enable them.  Alternatively
141 <code>-D_GLIBCXX_EXTERN_TEMPLATE=0</code> 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 <code>__gnu_debug::basic_string</code> debugging container directly,
146 which always works correctly.
147 </para>
148
149 </sect1>
150
151 <sect1 id="manual.ext.debug_mode.using" xreflabel="Using">
152   <title>Using</title>
153   <para>
154   </para>
155 <sect2 id="debug_mode.using.mode" xreflabel="Using Mode">
156   <title>Using the Debug Mode</title>
157
158 <para>To use the libstdc++ debug mode, compile your application with the
159   compiler flag <code>-D_GLIBCXX_DEBUG</code>. Note that this flag
160   changes the sizes and behavior of standard class templates such
161   as <code>std::vector</code>, 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.</para>
165
166 <para>By default, error messages are formatted to fit on lines of about
167   78 characters.  The environment variable
168   <code>GLIBCXX_DEBUG_MESSAGE_LENGTH</code> can be used to request a
169   different length.</para>
170
171 </sect2>
172
173 <sect2 id="debug_mode.using.specific" xreflabel="Using Specific">
174   <title>Using a Specific Debug Container</title>
175 <para>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 </para>
185
186 <table frame='all'>
187 <title>Debugging Containers</title>
188 <tgroup cols='6' align='left' colsep='1' rowsep='1'>
189 <colspec colname='c1'></colspec>
190 <colspec colname='c2'></colspec>
191 <colspec colname='c3'></colspec>
192 <colspec colname='c4'></colspec>
193
194 <thead>
195   <row>
196     <entry>Container</entry>
197     <entry>Header</entry>
198     <entry>Debug container</entry>
199     <entry>Debug header</entry>
200   </row>
201 </thead>
202 <tbody>
203   <row>
204     <entry><classname>std::bitset</classname></entry>
205     <entry><filename class="headerfile">bitset</filename></entry>
206     <entry><classname>__gnu_debug::bitset</classname></entry>
207     <entry><filename class="headerfile">bitset</filename></entry>
208   </row>
209   <row>
210     <entry><classname>std::deque</classname></entry>
211     <entry><filename class="headerfile">deque</filename></entry>
212     <entry><classname>__gnu_debug::deque</classname></entry>
213     <entry><filename class="headerfile">deque</filename></entry>
214   </row>
215   <row>
216     <entry><classname>std::list</classname></entry>
217     <entry><filename class="headerfile">list</filename></entry>
218     <entry><classname>__gnu_debug::list</classname></entry>
219     <entry><filename class="headerfile">list</filename></entry>
220   </row>
221   <row>
222     <entry><classname>std::map</classname></entry>
223     <entry><filename class="headerfile">map</filename></entry>
224     <entry><classname>__gnu_debug::map</classname></entry>
225     <entry><filename class="headerfile">map</filename></entry>
226   </row>
227   <row>
228     <entry><classname>std::multimap</classname></entry>
229     <entry><filename class="headerfile">map</filename></entry>
230     <entry><classname>__gnu_debug::multimap</classname></entry>
231     <entry><filename class="headerfile">map</filename></entry>
232   </row>
233   <row>
234     <entry><classname>std::multiset</classname></entry>
235     <entry><filename class="headerfile">set</filename></entry>
236     <entry><classname>__gnu_debug::multiset</classname></entry>
237     <entry><filename class="headerfile">set</filename></entry>
238   </row>
239   <row>
240     <entry><classname>std::set</classname></entry>
241     <entry><filename class="headerfile">set</filename></entry>
242     <entry><classname>__gnu_debug::set</classname></entry>
243     <entry><filename class="headerfile">set</filename></entry>
244   </row>
245   <row>
246     <entry><classname>std::string</classname></entry>
247     <entry><filename class="headerfile">string</filename></entry>
248     <entry><classname>__gnu_debug::string</classname></entry>
249     <entry><filename class="headerfile">string</filename></entry>
250   </row>
251   <row>
252     <entry><classname>std::wstring</classname></entry>
253     <entry><filename class="headerfile">string</filename></entry>
254     <entry><classname>__gnu_debug::wstring</classname></entry>
255     <entry><filename class="headerfile">string</filename></entry>
256   </row>
257   <row>
258     <entry><classname>std::basic_string</classname></entry>
259     <entry><filename class="headerfile">string</filename></entry>
260     <entry><classname>__gnu_debug::basic_string</classname></entry>
261     <entry><filename class="headerfile">string</filename></entry>
262   </row>
263   <row>
264     <entry><classname>std::vector</classname></entry>
265     <entry><filename class="headerfile">vector</filename></entry>
266     <entry><classname>__gnu_debug::vector</classname></entry>
267     <entry><filename class="headerfile">vector</filename></entry>
268   </row>
269 </tbody>
270 </tgroup>
271 </table>
272
273 <para>In addition, when compiling in C++0x mode, these additional
274 containers have additional debug capability.
275 </para>
276
277 <table frame='all'>
278 <title>Debugging Containers C++0x</title>
279 <tgroup cols='6' align='left' colsep='1' rowsep='1'>
280 <colspec colname='c1'></colspec>
281 <colspec colname='c2'></colspec>
282 <colspec colname='c3'></colspec>
283 <colspec colname='c4'></colspec>
284
285 <thead>
286   <row>
287     <entry>Container</entry>
288     <entry>Header</entry>
289     <entry>Debug container</entry>
290     <entry>Debug header</entry>
291   </row>
292 </thead>
293 <tbody>
294     <row>
295     <entry><classname>std::unordered_map</classname></entry>
296     <entry><filename class="headerfile">unordered_map</filename></entry>
297     <entry><classname>__gnu_debug::unordered_map</classname></entry>
298     <entry><filename class="headerfile">unordered_map</filename></entry>
299   </row>
300   <row>
301     <entry><classname>std::unordered_multimap</classname></entry>
302     <entry><filename class="headerfile">unordered_map</filename></entry>
303     <entry><classname>__gnu_debug::unordered_multimap</classname></entry>
304     <entry><filename class="headerfile">unordered_map</filename></entry>
305   </row>
306   <row>
307     <entry><classname>std::unordered_set</classname></entry>
308     <entry><filename class="headerfile">unordered_set</filename></entry>
309     <entry><classname>__gnu_debug::unordered_set</classname></entry>
310     <entry><filename class="headerfile">unordered_set</filename></entry>
311   </row>
312   <row>
313     <entry><classname>std::unordered_multiset</classname></entry>
314     <entry><filename class="headerfile">unordered_set</filename></entry>
315     <entry><classname>__gnu_debug::unordered_multiset</classname></entry>
316     <entry><filename class="headerfile">unordered_set</filename></entry>
317   </row>
318 </tbody>
319 </tgroup>
320 </table>
321 </sect2>
322 </sect1>
323
324 <sect1 id="manual.ext.debug_mode.design" xreflabel="Design">
325   <title>Design</title>
326   <para>
327   </para>
328   <sect2 id="manual.ext.debug_mode.design.goals" xreflabel="Goals">
329     <title>Goals</title>
330     <para>
331     </para>
332 <para> 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:</para>
336
337   <itemizedlist>
338
339     <listitem><para><emphasis>Correctness</emphasis>: 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.</para></listitem>
351
352     <listitem><para><emphasis>Performance</emphasis>: 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).</para></listitem>
357
358     <listitem><para><emphasis>Usability</emphasis>: 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.</para></listitem>
366
367     <listitem><para><emphasis>Minimize recompilation</emphasis>: 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       <orderedlist>
380         <listitem><para><emphasis>Full recompilation</emphasis>: 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.</para></listitem>
385
386         <listitem><para><emphasis>Full user recompilation</emphasis>: 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.</para></listitem>
394
395         <listitem><para><emphasis>Partial recompilation</emphasis>: 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.</para></listitem>
402
403         <listitem><para><emphasis>Per-use recompilation</emphasis>: 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 <emphasis>A</emphasis> that contains a
413         particular instantiation
414         (say, <code>std::vector&lt;int&gt;</code>) compiled in release
415         mode can be linked against a translation unit <emphasis>B</emphasis> 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. </para></listitem>
422
423         <listitem><para><emphasis>Per-unit recompilation</emphasis>: 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 "<code>-g</code> mode",
427         because the <code>-g</code> 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 <code>new</code> (changes the program
437         semantics).</para></listitem>
438       </orderedlist>
439     </para></listitem>
440   </itemizedlist>
441   </sect2>
442
443   <sect2 id="manual.ext.debug_mode.design.methods" xreflabel="Methods">
444     <title>Methods</title>
445     <para>
446     </para>
447 <para>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.</para>
450
451   <sect3 id="debug_mode.design.methods.wrappers" xreflabel="Method Wrapper">
452     <title>The Wrapper Model</title>
453 <para>The libstdc++ debug mode uses a wrapper model where the debugging
454   versions of library components (e.g., iterators and containers) form
455   a layer on top of the release versions of the library
456   components. The debugging components first verify that the operation
457   is correct (aborting with a diagnostic if an error is found) and
458   will then forward to the underlying release-mode container that will
459   perform the actual work. This design decision ensures that we cannot
460   regress release-mode performance (because the release-mode
461   containers are left untouched) and partially enables <ulink url="#mixing">mixing debug and release code</ulink> at link time,
462   although that will not be discussed at this time.</para>
463
464 <para>Two types of wrappers are used in the implementation of the debug
465   mode: container wrappers and iterator wrappers. The two types of
466   wrappers interact to maintain relationships between iterators and
467   their associated containers, which are necessary to detect certain
468   types of standard library usage errors such as dereferencing
469   past-the-end iterators or inserting into a container using an
470   iterator from a different container.</para>
471
472   <sect4 id="debug_mode.design.methods.safe_iter" xreflabel="Method Safe Iter">
473     <title>Safe Iterators</title>
474 <para>Iterator wrappers provide a debugging layer over any iterator that
475   is attached to a particular container, and will manage the
476   information detailing the iterator's state (singular,
477   dereferenceable, etc.) and tracking the container to which the
478   iterator is attached. Because iterators have a well-defined, common
479   interface the iterator wrapper is implemented with the iterator
480   adaptor class template <code>__gnu_debug::_Safe_iterator</code>,
481   which takes two template parameters:</para>
482
483 <itemizedlist>
484   <listitem><para><code>Iterator</code>: The underlying iterator type, which must
485     be either the <code>iterator</code> or <code>const_iterator</code>
486     typedef from the sequence type this iterator can reference.</para></listitem>
487   
488   <listitem><para><code>Sequence</code>: The type of sequence that this iterator
489   references. This sequence must be a safe sequence (discussed below)
490   whose <code>iterator</code> or <code>const_iterator</code> typedef
491   is the type of the safe iterator.</para></listitem>
492 </itemizedlist>
493   </sect4>
494
495   <sect4 id="debug_mode.design.methods.safe_seq" xreflabel="Method Safe Seq">
496     <title>Safe Sequences (Containers)</title>
497
498 <para>Container wrappers provide a debugging layer over a particular
499   container type. Because containers vary greatly in the member
500   functions they support and the semantics of those member functions
501   (especially in the area of iterator invalidation), container
502   wrappers are tailored to the container they reference, e.g., the
503   debugging version of <code>std::list</code> duplicates the entire
504   interface of <code>std::list</code>, adding additional semantic
505   checks and then forwarding operations to the
506   real <code>std::list</code> (a public base class of the debugging
507   version) as appropriate. However, all safe containers inherit from
508   the class template <code>__gnu_debug::_Safe_sequence</code>,
509   instantiated with the type of the safe container itself (an instance
510   of the curiously recurring template pattern).</para>
511
512 <para>The iterators of a container wrapper will be 
513   <ulink url="#safe_iterator">safe iterators</ulink> that reference sequences
514   of this type and wrap the iterators provided by the release-mode
515   base class. The debugging container will use only the safe
516   iterators within its own interface (therefore requiring the user to
517   use safe iterators, although this does not change correct user
518   code) and will communicate with the release-mode base class with
519   only the underlying, unsafe, release-mode iterators that the base
520   class exports.</para>
521
522 <para> The debugging version of <code>std::list</code> will have the
523   following basic structure:</para>
524
525 <programlisting>
526 template&lt;typename _Tp, typename _Allocator = allocator&lt;_Tp&gt;
527   class debug-list :
528     public release-list&lt;_Tp, _Allocator&gt;,
529     public __gnu_debug::_Safe_sequence&lt;debug-list&lt;_Tp, _Allocator&gt; &gt;
530   {
531     typedef release-list&lt;_Tp, _Allocator&gt; _Base;
532     typedef debug-list&lt;_Tp, _Allocator&gt;   _Self;
533
534   public:
535     typedef __gnu_debug::_Safe_iterator&lt;typename _Base::iterator, _Self&gt;       iterator;
536     typedef __gnu_debug::_Safe_iterator&lt;typename _Base::const_iterator, _Self&gt; const_iterator;
537
538     // duplicate std::list interface with debugging semantics
539   };
540 </programlisting>
541   </sect4>
542   </sect3>
543
544   <sect3 id="debug_mode.design.methods.precond" xreflabel="Precondition check">
545     <title>Precondition Checking</title>
546 <para>The debug mode operates primarily by checking the preconditions of
547   all standard library operations that it supports. Preconditions that
548   are always checked (regardless of whether or not we are in debug
549   mode) are checked via the <code>__check_xxx</code> macros defined
550   and documented in the source
551   file <code>include/debug/debug.h</code>. Preconditions that may or
552   may not be checked, depending on the debug-mode
553   macro <code>_GLIBCXX_DEBUG</code>, are checked via
554   the <code>__requires_xxx</code> macros defined and documented in the
555   same source file. Preconditions are validated using any additional
556   information available at run-time, e.g., the containers that are
557   associated with a particular iterator, the position of the iterator
558   within those containers, the distance between two iterators that may
559   form a valid range, etc. In the absence of suitable information,
560   e.g., an input iterator that is not a safe iterator, these
561   precondition checks will silently succeed.</para>
562
563 <para>The majority of precondition checks use the aforementioned macros,
564   which have the secondary benefit of having prewritten debug
565   messages that use information about the current status of the
566   objects involved (e.g., whether an iterator is singular or what
567   sequence it is attached to) along with some static information
568   (e.g., the names of the function parameters corresponding to the
569   objects involved). When not using these macros, the debug mode uses
570   either the debug-mode assertion
571   macro <code>_GLIBCXX_DEBUG_ASSERT</code> , its pedantic
572   cousin <code>_GLIBCXX_DEBUG_PEDASSERT</code>, or the assertion
573   check macro that supports more advance formulation of error
574   messages, <code>_GLIBCXX_DEBUG_VERIFY</code>. These macros are
575   documented more thoroughly in the debug mode source code.</para>
576   </sect3>
577
578   <sect3 id="debug_mode.design.methods.coexistence" xreflabel="Coexistence">
579     <title>Release- and debug-mode coexistence</title>
580 <para>The libstdc++ debug mode is the first debug mode we know of that
581   is able to provide the "Per-use recompilation" (4) guarantee, that
582   allows release-compiled and debug-compiled code to be linked and
583   executed together without causing unpredictable behavior. This
584   guarantee minimizes the recompilation that users are required to
585   perform, shortening the detect-compile-debug bug hunting cycle
586   and making the debug mode easier to incorporate into development
587   environments by minimizing dependencies.</para>
588
589 <para>Achieving link- and run-time coexistence is not a trivial
590   implementation task. To achieve this goal we required a small
591   extension to the GNU C++ compiler (described in the GCC Manual for
592   C++ Extensions, see <ulink url="http://gcc.gnu.org/onlinedocs/gcc/Strong-Using.html">strong
593   using</ulink>), and a complex organization of debug- and
594   release-modes. The end result is that we have achieved per-use
595   recompilation but have had to give up some checking of the
596   <code>std::basic_string</code> class template (namely, safe
597   iterators).
598 </para>
599
600  <sect4 id="methods.coexistence.compile" xreflabel="Compile">
601    <title>Compile-time coexistence of release- and debug-mode components</title>
602
603 <para>Both the release-mode components and the debug-mode
604   components need to exist within a single translation unit so that
605   the debug versions can wrap the release versions. However, only one
606   of these components should be user-visible at any particular
607   time with the standard name, e.g., <code>std::list</code>. </para>
608
609 <para>In release mode, we define only the release-mode version of the
610   component with its standard name and do not include the debugging
611   component at all. The release mode version is defined within the
612   namespace <code>std</code>. Minus the namespace associations, this
613   method leaves the behavior of release mode completely unchanged from
614   its behavior prior to the introduction of the libstdc++ debug
615   mode. Here's an example of what this ends up looking like, in
616   C++.</para>
617
618 <programlisting>
619 namespace std
620 {
621   template&lt;typename _Tp, typename _Alloc = allocator&lt;_Tp&gt; &gt;
622     class list
623     {
624       // ...
625      };
626 } // namespace std
627 </programlisting>
628   
629 <para>In debug mode we include the release-mode container (which is now
630 defined in in the namespace <code>__norm</code>) and also the
631 debug-mode container. The debug-mode container is defined within the
632 namespace <code>__debug</code>, which is associated with namespace
633 <code>std</code> via the GNU namespace association extension.  This
634 method allows the debug and release versions of the same component to
635 coexist at compile-time and link-time without causing an unreasonable
636 maintenance burden, while minimizing confusion. Again, this boils down
637 to C++ code as follows:</para>
638
639 <programlisting>
640 namespace std
641 {
642   namespace __norm
643   {
644     template&lt;typename _Tp, typename _Alloc = allocator&lt;_Tp&gt; &gt;
645       class list
646       {
647         // ...
648       };
649   } // namespace __gnu_norm
650
651   namespace __debug
652   {
653     template&lt;typename _Tp, typename _Alloc = allocator&lt;_Tp&gt; &gt;
654       class list
655       : public __norm::list&lt;_Tp, _Alloc&gt;,
656         public __gnu_debug::_Safe_sequence&lt;list&lt;_Tp, _Alloc&gt; &gt;
657       {
658         // ...
659       };
660   } // namespace __norm
661
662   using namespace __debug __attribute__ ((strong));
663 }
664 </programlisting>
665  </sect4>
666
667  <sect4 id="methods.coexistence.link" xreflabel="Link">
668    <title>Link- and run-time coexistence of release- and
669     debug-mode components</title>
670
671 <para>Because each component has a distinct and separate release and
672 debug implementation, there are are no issues with link-time
673 coexistence: the separate namespaces result in different mangled
674 names, and thus unique linkage.</para>
675
676 <para>However, components that are defined and used within the C++
677 standard library itself face additional constraints. For instance,
678 some of the member functions of <code> std::moneypunct</code> return
679 <code>std::basic_string</code>. Normally, this is not a problem, but
680 with a mixed mode standard library that could be using either
681 debug-mode or release-mode <code> basic_string</code> objects, things
682 get more complicated.  As the return value of a function is not
683 encoded into the mangled name, there is no way to specify a
684 release-mode or a debug-mode string. In practice, this results in
685 runtime errors. A simplified example of this problem is as follows.
686 </para>
687
688 <para> Take this translation unit, compiled in debug-mode: </para>
689 <programlisting>
690 // -D_GLIBCXX_DEBUG
691 #include &lt;string&gt;
692
693 std::string test02();
694  
695 std::string test01()
696 {
697   return test02();
698 }
699  
700 int main()
701 {
702   test01();
703   return 0;
704 }
705 </programlisting>
706
707 <para> ... and linked to this translation unit, compiled in release mode:</para>
708
709 <programlisting>
710 #include &lt;string&gt;
711  
712 std::string
713 test02()
714 {
715   return std::string("toast");
716 }
717 </programlisting>
718
719 <para> For this reason we cannot easily provide safe iterators for
720   the <code>std::basic_string</code> class template, as it is present
721   throughout the C++ standard library. For instance, locale facets
722   define typedefs that include <code>basic_string</code>: in a mixed
723   debug/release program, should that typedef be based on the
724   debug-mode <code>basic_string</code> or the
725   release-mode <code>basic_string</code>? While the answer could be
726   "both", and the difference hidden via renaming a la the
727   debug/release containers, we must note two things about locale
728   facets:</para>
729
730 <orderedlist>
731   <listitem><para>They exist as shared state: one can create a facet in one
732   translation unit and access the facet via the same type name in a
733   different translation unit. This means that we cannot have two
734   different versions of locale facets, because the types would not be
735   the same across debug/release-mode translation unit barriers.</para></listitem>
736
737   <listitem><para>They have virtual functions returning strings: these functions
738   mangle in the same way regardless of the mangling of their return
739   types (see above), and their precise signatures can be relied upon
740   by users because they may be overridden in derived classes.</para></listitem>
741 </orderedlist>
742
743 <para>With the design of libstdc++ debug mode, we cannot effectively hide
744   the differences between debug and release-mode strings from the
745   user. Failure to hide the differences may result in unpredictable
746   behavior, and for this reason we have opted to only
747   perform <code>basic_string</code> changes that do not require ABI
748   changes. The effect on users is expected to be minimal, as there are
749   simple alternatives (e.g., <code>__gnu_debug::basic_string</code>),
750   and the usability benefit we gain from the ability to mix debug- and
751   release-compiled translation units is enormous.</para>
752  </sect4>
753
754  <sect4 id="methods.coexistence.alt" xreflabel="Alternatives">
755 <title>Alternatives for Coexistence</title>
756
757 <para>The coexistence scheme above was chosen over many alternatives,
758   including language-only solutions and solutions that also required
759   extensions to the C++ front end. The following is a partial list of
760   solutions, with justifications for our rejection of each.</para>
761
762 <itemizedlist>
763   <listitem><para><emphasis>Completely separate debug/release libraries</emphasis>: This is by
764   far the simplest implementation option, where we do not allow any
765   coexistence of debug- and release-compiled translation units in a
766   program. This solution has an extreme negative affect on usability,
767   because it is quite likely that some libraries an application
768   depends on cannot be recompiled easily. This would not meet
769   our <emphasis>usability</emphasis> or <emphasis>minimize recompilation</emphasis> criteria
770   well.</para></listitem>
771
772   <listitem><para><emphasis>Add a <code>Debug</code> boolean template parameter</emphasis>:
773   Partial specialization could be used to select the debug
774   implementation when <code>Debug == true</code>, and the state
775   of <code>_GLIBCXX_DEBUG</code> could decide whether the
776   default <code>Debug</code> argument is <code>true</code>
777   or <code>false</code>. This option would break conformance with the
778   C++ standard in both debug <emphasis>and</emphasis> release modes. This would
779   not meet our <emphasis>correctness</emphasis> criteria. </para></listitem>
780
781   <listitem><para><emphasis>Packaging a debug flag in the allocators</emphasis>: We could
782     reuse the <code>Allocator</code> template parameter of containers
783     by adding a sentinel wrapper <code>debug&lt;&gt;</code> that
784     signals the user's intention to use debugging, and pick up
785     the <code>debug&lt;&gt;</code> allocator wrapper in a partial
786     specialization. However, this has two drawbacks: first, there is a
787     conformance issue because the default allocator would not be the
788     standard-specified <code>std::allocator&lt;T&gt;</code>. Secondly
789     (and more importantly), users that specify allocators instead of
790     implicitly using the default allocator would not get debugging
791     containers. Thus this solution fails the <emphasis>correctness</emphasis>
792     criteria.</para></listitem>
793
794   <listitem><para><emphasis>Define debug containers in another namespace, and employ
795       a <code>using</code> declaration (or directive)</emphasis>: This is an
796       enticing option, because it would eliminate the need for
797       the <code>link_name</code> extension by aliasing the
798       templates. However, there is no true template aliasing mechanism
799       is C++, because both <code>using</code> directives and using
800       declarations disallow specialization. This method fails
801       the <emphasis>correctness</emphasis> criteria.</para></listitem>
802
803   <listitem><para><emphasis> Use implementation-specific properties of anonymous
804     namespaces. </emphasis>
805     See <ulink url="http://gcc.gnu.org/ml/libstdc++/2003-08/msg00004.html"> this post
806     </ulink>
807     This method fails the <emphasis>correctness</emphasis> criteria.</para></listitem>
808
809   <listitem><para><emphasis>Extension: allow reopening on namespaces</emphasis>: This would
810     allow the debug mode to effectively alias the
811     namespace <code>std</code> to an internal namespace, such
812     as <code>__gnu_std_debug</code>, so that it is completely
813     separate from the release-mode <code>std</code> namespace. While
814     this will solve some renaming problems and ensure that
815     debug- and release-compiled code cannot be mixed unsafely, it ensures that
816     debug- and release-compiled code cannot be mixed at all. For
817     instance, the program would have two <code>std::cout</code>
818     objects! This solution would fails the <emphasis>minimize
819     recompilation</emphasis> requirement, because we would only be able to
820     support option (1) or (2).</para></listitem>
821
822   <listitem><para><emphasis>Extension: use link name</emphasis>: This option involves
823     complicated re-naming between debug-mode and release-mode
824     components at compile time, and then a g++ extension called <emphasis>
825     link name </emphasis> to recover the original names at link time. There
826     are two drawbacks to this approach. One, it's very verbose,
827     relying on macro renaming at compile time and several levels of
828     include ordering. Two, ODR issues remained with container member
829     functions taking no arguments in mixed-mode settings resulting in
830     equivalent link names, <code> vector::push_back() </code> being
831     one example. 
832     See <ulink url="http://gcc.gnu.org/ml/libstdc++/2003-08/msg00177.html">link
833     name</ulink> </para></listitem>
834 </itemizedlist>
835
836 <para>Other options may exist for implementing the debug mode, many of
837   which have probably been considered and others that may still be
838   lurking. This list may be expanded over time to include other
839   options that we could have implemented, but in all cases the full
840   ramifications of the approach (as measured against the design goals
841   for a libstdc++ debug mode) should be considered first. The DejaGNU
842   testsuite includes some testcases that check for known problems with
843   some solutions (e.g., the <code>using</code> declaration solution
844   that breaks user specialization), and additional testcases will be
845   added as we are able to identify other typical problem cases. These
846   test cases will serve as a benchmark by which we can compare debug
847   mode implementations.</para>
848  </sect4>
849   </sect3>
850   </sect2>  
851
852   <sect2 id="manual.ext.debug_mode.design.other" xreflabel="Other">
853     <title>Other Implementations</title>
854     <para>
855     </para>
856 <para> There are several existing implementations of debug modes for C++
857   standard library implementations, although none of them directly
858   supports debugging for programs using libstdc++. The existing
859   implementations include:</para>
860 <itemizedlist>
861   <listitem><para><ulink url="http://www.mathcs.sjsu.edu/faculty/horstman/safestl.html">SafeSTL</ulink>:
862   SafeSTL was the original debugging version of the Standard Template
863   Library (STL), implemented by Cay S. Horstmann on top of the
864   Hewlett-Packard STL. Though it inspired much work in this area, it
865   has not been kept up-to-date for use with modern compilers or C++
866   standard library implementations.</para></listitem>
867
868   <listitem><para><ulink url="http://www.stlport.org/">STLport</ulink>: STLport is a free
869   implementation of the C++ standard library derived from the <ulink url="http://www.sgi.com/tech/stl/">SGI implementation</ulink>, and
870   ported to many other platforms. It includes a debug mode that uses a
871   wrapper model (that in some way inspired the libstdc++ debug mode
872   design), although at the time of this writing the debug mode is
873   somewhat incomplete and meets only the "Full user recompilation" (2)
874   recompilation guarantee by requiring the user to link against a
875   different library in debug mode vs. release mode.</para></listitem>
876
877   <listitem><para><ulink url="http://www.metrowerks.com/mw/default.htm">Metrowerks
878   CodeWarrior</ulink>: The C++ standard library that ships with Metrowerks
879   CodeWarrior includes a debug mode. It is a full debug-mode
880   implementation (including debugging for CodeWarrior extensions) and
881   is easy to use, although it meets only the "Full recompilation" (1)
882   recompilation guarantee.</para></listitem>
883 </itemizedlist>
884
885   </sect2>  
886 </sect1>
887
888 </chapter>