1*404b540aSrobert<?xml version="1.0" encoding="ISO-8859-1"?> 2*404b540aSrobert<!DOCTYPE html 3*404b540aSrobert PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 4*404b540aSrobert "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5*404b540aSrobert 6*404b540aSrobert<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 7*404b540aSrobert<head> 8*404b540aSrobert <meta name="AUTHOR" content="gregod@cs.rpi.edu (Doug Gregor)" /> 9*404b540aSrobert <meta name="KEYWORDS" content="C++, GCC, libstdc++, g++, debug" /> 10*404b540aSrobert <meta name="DESCRIPTION" content="Design of the libstdc++ debug mode." /> 11*404b540aSrobert <meta name="GENERATOR" content="vi and eight fingers" /> 12*404b540aSrobert <title>Design of the libstdc++ debug mode</title> 13*404b540aSrobert<link rel="StyleSheet" href="lib3styles.css" /> 14*404b540aSrobert</head> 15*404b540aSrobert<body> 16*404b540aSrobert 17*404b540aSrobert<h1 class="centered"><a name="top">Design of the libstdc++ debug mode</a></h1> 18*404b540aSrobert 19*404b540aSrobert<p class="fineprint"><em> 20*404b540aSrobert The latest version of this document is always available at 21*404b540aSrobert <a href="http://gcc.gnu.org/onlinedocs/libstdc++/debug_mode.html"> 22*404b540aSrobert http://gcc.gnu.org/onlinedocs/libstdc++/debug_mode.html</a>. 23*404b540aSrobert</em></p> 24*404b540aSrobert 25*404b540aSrobert<p><em> 26*404b540aSrobert To the <a href="http://gcc.gnu.org/libstdc++/">libstdc++-v3 homepage</a>. 27*404b540aSrobert</em></p> 28*404b540aSrobert 29*404b540aSrobert 30*404b540aSrobert<!-- ####################################################### --> 31*404b540aSrobert 32*404b540aSrobert<hr /> 33*404b540aSrobert<h1>Debug mode design</h1> 34*404b540aSrobert<p> The libstdc++ debug mode replaces unsafe (but efficient) standard 35*404b540aSrobert containers and iterators with semantically equivalent safe standard 36*404b540aSrobert containers and iterators to aid in debugging user programs. The 37*404b540aSrobert following goals directed the design of the libstdc++ debug mode:</p> 38*404b540aSrobert 39*404b540aSrobert <ul> 40*404b540aSrobert 41*404b540aSrobert <li><b>Correctness</b>: the libstdc++ debug mode must not change 42*404b540aSrobert the semantics of the standard library for all cases specified in 43*404b540aSrobert the ANSI/ISO C++ standard. The essence of this constraint is that 44*404b540aSrobert any valid C++ program should behave in the same manner regardless 45*404b540aSrobert of whether it is compiled with debug mode or release mode. In 46*404b540aSrobert particular, entities that are defined in namespace std in release 47*404b540aSrobert mode should remain defined in namespace std in debug mode, so that 48*404b540aSrobert legal specializations of namespace std entities will remain 49*404b540aSrobert valid. A program that is not valid C++ (e.g., invokes undefined 50*404b540aSrobert behavior) is not required to behave similarly, although the debug 51*404b540aSrobert mode will abort with a diagnostic when it detects undefined 52*404b540aSrobert behavior.</li> 53*404b540aSrobert 54*404b540aSrobert <li><b>Performance</b>: the additional of the libstdc++ debug mode 55*404b540aSrobert must not affect the performance of the library when it is compiled 56*404b540aSrobert in release mode. Performance of the libstdc++ debug mode is 57*404b540aSrobert secondary (and, in fact, will be worse than the release 58*404b540aSrobert mode).</li> 59*404b540aSrobert 60*404b540aSrobert <li><b>Usability</b>: the libstdc++ debug mode should be easy to 61*404b540aSrobert use. It should be easily incorporated into the user's development 62*404b540aSrobert environment (e.g., by requiring only a single new compiler switch) 63*404b540aSrobert and should produce reasonable diagnostics when it detects a 64*404b540aSrobert problem with the user program. Usability also involves detection 65*404b540aSrobert of errors when using the debug mode incorrectly, e.g., by linking 66*404b540aSrobert a release-compiled object against a debug-compiled object if in 67*404b540aSrobert fact the resulting program will not run correctly.</li> 68*404b540aSrobert 69*404b540aSrobert <li><b>Minimize recompilation</b>: While it is expected that 70*404b540aSrobert users recompile at least part of their program to use debug 71*404b540aSrobert mode, the amount of recompilation affects the 72*404b540aSrobert detect-compile-debug turnaround time. This indirectly affects the 73*404b540aSrobert usefulness of the debug mode, because debugging some applications 74*404b540aSrobert may require rebuilding a large amount of code, which may not be 75*404b540aSrobert feasible when the suspect code may be very localized. There are 76*404b540aSrobert several levels of conformance to this requirement, each with its 77*404b540aSrobert own usability and implementation characteristics. In general, the 78*404b540aSrobert higher-numbered conformance levels are more usable (i.e., require 79*404b540aSrobert less recompilation) but are more complicated to implement than 80*404b540aSrobert the lower-numbered conformance levels. 81*404b540aSrobert <ol> 82*404b540aSrobert <li><b>Full recompilation</b>: The user must recompile his or 83*404b540aSrobert her entire application and all C++ libraries it depends on, 84*404b540aSrobert including the C++ standard library that ships with the 85*404b540aSrobert compiler. This must be done even if only a small part of the 86*404b540aSrobert program can use debugging features.</li> 87*404b540aSrobert 88*404b540aSrobert <li><b>Full user recompilation</b>: The user must recompile 89*404b540aSrobert his or her entire application and all C++ libraries it depends 90*404b540aSrobert on, but not the C++ standard library itself. This must be done 91*404b540aSrobert even if only a small part of the program can use debugging 92*404b540aSrobert features. This can be achieved given a full recompilation 93*404b540aSrobert system by compiling two versions of the standard library when 94*404b540aSrobert the compiler is installed and linking against the appropriate 95*404b540aSrobert one, e.g., a multilibs approach.</li> 96*404b540aSrobert 97*404b540aSrobert <li><b>Partial recompilation</b>: The user must recompile the 98*404b540aSrobert parts of his or her application and the C++ libraries it 99*404b540aSrobert depends on that will use the debugging facilities 100*404b540aSrobert directly. This means that any code that uses the debuggable 101*404b540aSrobert standard containers would need to be recompiled, but code 102*404b540aSrobert that does not use them (but may, for instance, use IOStreams) 103*404b540aSrobert would not have to be recompiled.</li> 104*404b540aSrobert 105*404b540aSrobert <li><b>Per-use recompilation</b>: The user must recompile the 106*404b540aSrobert parts of his or her application and the C++ libraries it 107*404b540aSrobert depends on where debugging should occur, and any other code 108*404b540aSrobert that interacts with those containers. This means that a set of 109*404b540aSrobert translation units that accesses a particular standard 110*404b540aSrobert container instance may either be compiled in release mode (no 111*404b540aSrobert checking) or debug mode (full checking), but must all be 112*404b540aSrobert compiled in the same way; a translation unit that does not see 113*404b540aSrobert that standard container instance need not be recompiled. This 114*404b540aSrobert also means that a translation unit <em>A</em> that contains a 115*404b540aSrobert particular instantiation 116*404b540aSrobert (say, <code>std::vector<int></code>) compiled in release 117*404b540aSrobert mode can be linked against a translation unit <em>B</em> that 118*404b540aSrobert contains the same instantiation compiled in debug mode (a 119*404b540aSrobert feature not present with partial recompilation). While this 120*404b540aSrobert behavior is technically a violation of the One Definition 121*404b540aSrobert Rule, this ability tends to be very important in 122*404b540aSrobert practice. The libstdc++ debug mode supports this level of 123*404b540aSrobert recompilation. </li> 124*404b540aSrobert 125*404b540aSrobert <li><b>Per-unit recompilation</b>: The user must only 126*404b540aSrobert recompile the translation units where checking should occur, 127*404b540aSrobert regardless of where debuggable standard containers are 128*404b540aSrobert used. This has also been dubbed "<code>-g</code> mode", 129*404b540aSrobert because the <code>-g</code> compiler switch works in this way, 130*404b540aSrobert emitting debugging information at a per--translation-unit 131*404b540aSrobert granularity. We believe that this level of recompilation is in 132*404b540aSrobert fact not possible if we intend to supply safe iterators, leave 133*404b540aSrobert the program semantics unchanged, and not regress in 134*404b540aSrobert performance under release mode because we cannot associate 135*404b540aSrobert extra information with an iterator (to form a safe iterator) 136*404b540aSrobert without either reserving that space in release mode 137*404b540aSrobert (performance regression) or allocating extra memory associated 138*404b540aSrobert with each iterator with <code>new</code> (changes the program 139*404b540aSrobert semantics).</li> 140*404b540aSrobert </ol> 141*404b540aSrobert </li> 142*404b540aSrobert </ul> 143*404b540aSrobert 144*404b540aSrobert<h2><a name="other">Other implementations</a></h2> 145*404b540aSrobert<p> There are several existing implementations of debug modes for C++ 146*404b540aSrobert standard library implementations, although none of them directly 147*404b540aSrobert supports debugging for programs using libstdc++. The existing 148*404b540aSrobert implementations include:</p> 149*404b540aSrobert<ul> 150*404b540aSrobert <li><a 151*404b540aSrobert href="http://www.mathcs.sjsu.edu/faculty/horstman/safestl.html">SafeSTL</a>: 152*404b540aSrobert SafeSTL was the original debugging version of the Standard Template 153*404b540aSrobert Library (STL), implemented by Cay S. Horstmann on top of the 154*404b540aSrobert Hewlett-Packard STL. Though it inspired much work in this area, it 155*404b540aSrobert has not been kept up-to-date for use with modern compilers or C++ 156*404b540aSrobert standard library implementations.</li> 157*404b540aSrobert 158*404b540aSrobert <li><a href="http://www.stlport.org/">STLport</a>: STLport is a free 159*404b540aSrobert implementation of the C++ standard library derived from the <a 160*404b540aSrobert href="http://www.sgi.com/tech/stl/">SGI implementation</a>, and 161*404b540aSrobert ported to many other platforms. It includes a debug mode that uses a 162*404b540aSrobert wrapper model (that in some way inspired the libstdc++ debug mode 163*404b540aSrobert design), although at the time of this writing the debug mode is 164*404b540aSrobert somewhat incomplete and meets only the "Full user recompilation" (2) 165*404b540aSrobert recompilation guarantee by requiring the user to link against a 166*404b540aSrobert different library in debug mode vs. release mode.</li> 167*404b540aSrobert 168*404b540aSrobert <li><a href="http://www.metrowerks.com/mw/default.htm">Metrowerks 169*404b540aSrobert CodeWarrior</a>: The C++ standard library that ships with Metrowerks 170*404b540aSrobert CodeWarrior includes a debug mode. It is a full debug-mode 171*404b540aSrobert implementation (including debugging for CodeWarrior extensions) and 172*404b540aSrobert is easy to use, although it meets only the "Full recompilation" (1) 173*404b540aSrobert recompilation guarantee.</li> 174*404b540aSrobert</ul> 175*404b540aSrobert 176*404b540aSrobert<h2><a name="design">Debug mode design methodology</a></h2> 177*404b540aSrobert<p>This section provides an overall view of the design of the 178*404b540aSrobert libstdc++ debug mode and details the relationship between design 179*404b540aSrobert decisions and the stated design goals.</p> 180*404b540aSrobert 181*404b540aSrobert<h3><a name="wrappers">The wrapper model</a></h3> 182*404b540aSrobert<p>The libstdc++ debug mode uses a wrapper model where the debugging 183*404b540aSrobert versions of library components (e.g., iterators and containers) form 184*404b540aSrobert a layer on top of the release versions of the library 185*404b540aSrobert components. The debugging components first verify that the operation 186*404b540aSrobert is correct (aborting with a diagnostic if an error is found) and 187*404b540aSrobert will then forward to the underlying release-mode container that will 188*404b540aSrobert perform the actual work. This design decision ensures that we cannot 189*404b540aSrobert regress release-mode performance (because the release-mode 190*404b540aSrobert containers are left untouched) and partially enables <a 191*404b540aSrobert href="#mixing">mixing debug and release code</a> at link time, 192*404b540aSrobert although that will not be discussed at this time.</p> 193*404b540aSrobert 194*404b540aSrobert<p>Two types of wrappers are used in the implementation of the debug 195*404b540aSrobert mode: container wrappers and iterator wrappers. The two types of 196*404b540aSrobert wrappers interact to maintain relationships between iterators and 197*404b540aSrobert their associated containers, which are necessary to detect certain 198*404b540aSrobert types of standard library usage errors such as dereferencing 199*404b540aSrobert past-the-end iterators or inserting into a container using an 200*404b540aSrobert iterator from a different container.</p> 201*404b540aSrobert 202*404b540aSrobert<h4><a name="safe_iterator">Safe iterators</a></h4> 203*404b540aSrobert<p>Iterator wrappers provide a debugging layer over any iterator that 204*404b540aSrobert is attached to a particular container, and will manage the 205*404b540aSrobert information detailing the iterator's state (singular, 206*404b540aSrobert dereferenceable, etc.) and tracking the container to which the 207*404b540aSrobert iterator is attached. Because iterators have a well-defined, common 208*404b540aSrobert interface the iterator wrapper is implemented with the iterator 209*404b540aSrobert adaptor class template <code>__gnu_debug::_Safe_iterator</code>, 210*404b540aSrobert which takes two template parameters:</p> 211*404b540aSrobert 212*404b540aSrobert<ul> 213*404b540aSrobert <li><code>Iterator</code>: The underlying iterator type, which must 214*404b540aSrobert be either the <code>iterator</code> or <code>const_iterator</code> 215*404b540aSrobert typedef from the sequence type this iterator can reference.</li> 216*404b540aSrobert 217*404b540aSrobert <li><code>Sequence</code>: The type of sequence that this iterator 218*404b540aSrobert references. This sequence must be a safe sequence (discussed below) 219*404b540aSrobert whose <code>iterator</code> or <code>const_iterator</code> typedef 220*404b540aSrobert is the type of the safe iterator.</li> 221*404b540aSrobert</ul> 222*404b540aSrobert 223*404b540aSrobert<h4><a name="safe_sequence">Safe sequences (containers)</a></h4> 224*404b540aSrobert<p>Container wrappers provide a debugging layer over a particular 225*404b540aSrobert container type. Because containers vary greatly in the member 226*404b540aSrobert functions they support and the semantics of those member functions 227*404b540aSrobert (especially in the area of iterator invalidation), container 228*404b540aSrobert wrappers are tailored to the container they reference, e.g., the 229*404b540aSrobert debugging version of <code>std::list</code> duplicates the entire 230*404b540aSrobert interface of <code>std::list</code>, adding additional semantic 231*404b540aSrobert checks and then forwarding operations to the 232*404b540aSrobert real <code>std::list</code> (a public base class of the debugging 233*404b540aSrobert version) as appropriate. However, all safe containers inherit from 234*404b540aSrobert the class template <code>__gnu_debug::_Safe_sequence</code>, 235*404b540aSrobert instantiated with the type of the safe container itself (an instance 236*404b540aSrobert of the curiously recurring template pattern).</p> 237*404b540aSrobert 238*404b540aSrobert<p>The iterators of a container wrapper will be 239*404b540aSrobert <a href="#safe_iterator">safe iterators</a> that reference sequences 240*404b540aSrobert of this type and wrap the iterators provided by the release-mode 241*404b540aSrobert base class. The debugging container will use only the safe 242*404b540aSrobert iterators within its own interface (therefore requiring the user to 243*404b540aSrobert use safe iterators, although this does not change correct user 244*404b540aSrobert code) and will communicate with the release-mode base class with 245*404b540aSrobert only the underlying, unsafe, release-mode iterators that the base 246*404b540aSrobert class exports.</p> 247*404b540aSrobert 248*404b540aSrobert<p> The debugging version of <code>std::list</code> will have the 249*404b540aSrobert following basic structure:</p> 250*404b540aSrobert 251*404b540aSrobert<pre> 252*404b540aSroberttemplate<typename _Tp, typename _Allocator = allocator<_Tp> 253*404b540aSrobert class debug-list : 254*404b540aSrobert public release-list<_Tp, _Allocator>, 255*404b540aSrobert public __gnu_debug::_Safe_sequence<debug-list<_Tp, _Allocator> > 256*404b540aSrobert { 257*404b540aSrobert typedef release-list<_Tp, _Allocator> _Base; 258*404b540aSrobert typedef debug-list<_Tp, _Allocator> _Self; 259*404b540aSrobert 260*404b540aSrobert public: 261*404b540aSrobert typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, _Self> iterator; 262*404b540aSrobert typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, _Self> const_iterator; 263*404b540aSrobert 264*404b540aSrobert // duplicate std::list interface with debugging semantics 265*404b540aSrobert }; 266*404b540aSrobert</pre> 267*404b540aSrobert 268*404b540aSrobert<h3><a name="precondition">Precondition checking</a></h3> 269*404b540aSrobert<p>The debug mode operates primarily by checking the preconditions of 270*404b540aSrobert all standard library operations that it supports. Preconditions that 271*404b540aSrobert are always checked (regardless of whether or not we are in debug 272*404b540aSrobert mode) are checked via the <code>__check_xxx</code> macros defined 273*404b540aSrobert and documented in the source 274*404b540aSrobert file <code>include/debug/debug.h</code>. Preconditions that may or 275*404b540aSrobert may not be checked, depending on the debug-mode 276*404b540aSrobert macro <code>_GLIBCXX_DEBUG</code>, are checked via 277*404b540aSrobert the <code>__requires_xxx</code> macros defined and documented in the 278*404b540aSrobert same source file. Preconditions are validated using any additional 279*404b540aSrobert information available at run-time, e.g., the containers that are 280*404b540aSrobert associated with a particular iterator, the position of the iterator 281*404b540aSrobert within those containers, the distance between two iterators that may 282*404b540aSrobert form a valid range, etc. In the absence of suitable information, 283*404b540aSrobert e.g., an input iterator that is not a safe iterator, these 284*404b540aSrobert precondition checks will silently succeed.</p> 285*404b540aSrobert 286*404b540aSrobert<p>The majority of precondition checks use the aforementioned macros, 287*404b540aSrobert which have the secondary benefit of having prewritten debug 288*404b540aSrobert messages that use information about the current status of the 289*404b540aSrobert objects involved (e.g., whether an iterator is singular or what 290*404b540aSrobert sequence it is attached to) along with some static information 291*404b540aSrobert (e.g., the names of the function parameters corresponding to the 292*404b540aSrobert objects involved). When not using these macros, the debug mode uses 293*404b540aSrobert either the debug-mode assertion 294*404b540aSrobert macro <code>_GLIBCXX_DEBUG_ASSERT</code> , its pedantic 295*404b540aSrobert cousin <code>_GLIBCXX_DEBUG_PEDASSERT</code>, or the assertion 296*404b540aSrobert check macro that supports more advance formulation of error 297*404b540aSrobert messages, <code>_GLIBCXX_DEBUG_VERIFY</code>. These macros are 298*404b540aSrobert documented more thoroughly in the debug mode source code.</p> 299*404b540aSrobert 300*404b540aSrobert<h3><a name="coexistence">Release- and debug-mode coexistence</a></h3> 301*404b540aSrobert<p>The libstdc++ debug mode is the first debug mode we know of that 302*404b540aSrobert is able to provide the "Per-use recompilation" (4) guarantee, that 303*404b540aSrobert allows release-compiled and debug-compiled code to be linked and 304*404b540aSrobert executed together without causing unpredictable behavior. This 305*404b540aSrobert guarantee minimizes the recompilation that users are required to 306*404b540aSrobert perform, shortening the detect-compile-debug bughunting cycle 307*404b540aSrobert and making the debug mode easier to incorporate into development 308*404b540aSrobert environments by minimizing dependencies.</p> 309*404b540aSrobert 310*404b540aSrobert<p>Achieving link- and run-time coexistence is not a trivial 311*404b540aSrobert implementation task. To achieve this goal we required a small 312*404b540aSrobert extension to the GNU C++ compiler (described in the GCC Manual for 313*404b540aSrobert C++ Extensions, see <a 314*404b540aSrobert href="http://gcc.gnu.org/onlinedocs/gcc/Strong-Using.html">strong 315*404b540aSrobert using</a>), and a complex organization of debug- and 316*404b540aSrobert release-modes. The end result is that we have achieved per-use 317*404b540aSrobert recompilation but have had to give up some checking of the 318*404b540aSrobert <code>std::basic_string</code> class template (namely, safe 319*404b540aSrobert iterators). 320*404b540aSrobert</p> 321*404b540aSrobert 322*404b540aSrobert<h4><a name="compile_coexistence">Compile-time coexistence of release- and 323*404b540aSrobert debug-mode components</a></h4> 324*404b540aSrobert<p>Both the release-mode components and the debug-mode 325*404b540aSrobert components need to exist within a single translation unit so that 326*404b540aSrobert the debug versions can wrap the release versions. However, only one 327*404b540aSrobert of these components should be user-visible at any particular 328*404b540aSrobert time with the standard name, e.g., <code>std::list</code>. </p> 329*404b540aSrobert 330*404b540aSrobert<p>In release mode, we define only the release-mode version of the 331*404b540aSrobert component with its standard name and do not include the debugging 332*404b540aSrobert component at all. The release mode version is defined within the 333*404b540aSrobert namespace <code>std</code>. Minus the namespace associations, this 334*404b540aSrobert method leaves the behavior of release mode completely unchanged from 335*404b540aSrobert its behavior prior to the introduction of the libstdc++ debug 336*404b540aSrobert mode. Here's an example of what this ends up looking like, in 337*404b540aSrobert C++.</p> 338*404b540aSrobert 339*404b540aSrobert<pre> 340*404b540aSrobertnamespace std 341*404b540aSrobert{ 342*404b540aSrobert template<typename _Tp, typename _Alloc = allocator<_Tp> > 343*404b540aSrobert class list 344*404b540aSrobert { 345*404b540aSrobert // ... 346*404b540aSrobert }; 347*404b540aSrobert} // namespace std 348*404b540aSrobert</pre> 349*404b540aSrobert 350*404b540aSrobert<p>In debug mode we include the release-mode container (which is now 351*404b540aSrobertdefined in in the namespace <code>__norm</code>) and also the 352*404b540aSrobertdebug-mode container. The debug-mode container is defined within the 353*404b540aSrobertnamespace <code>__debug</code>, which is associated with namespace 354*404b540aSrobert<code>std</code> via the GNU namespace association extension. This 355*404b540aSrobertmethod allows the debug and release versions of the same component to 356*404b540aSrobertcoexist at compile-time and link-time without causing an unreasonable 357*404b540aSrobertmaintenance burden, while minimizing confusion. Again, this boils down 358*404b540aSrobertto C++ code as follows:</p> 359*404b540aSrobert 360*404b540aSrobert<pre> 361*404b540aSrobertnamespace std 362*404b540aSrobert{ 363*404b540aSrobert namespace __norm 364*404b540aSrobert { 365*404b540aSrobert template<typename _Tp, typename _Alloc = allocator<_Tp> > 366*404b540aSrobert class list 367*404b540aSrobert { 368*404b540aSrobert // ... 369*404b540aSrobert }; 370*404b540aSrobert } // namespace __gnu_norm 371*404b540aSrobert 372*404b540aSrobert namespace __debug 373*404b540aSrobert { 374*404b540aSrobert template<typename _Tp, typename _Alloc = allocator<_Tp> > 375*404b540aSrobert class list 376*404b540aSrobert : public __norm::list<_Tp, _Alloc>, 377*404b540aSrobert public __gnu_debug::_Safe_sequence<list<_Tp, _Alloc> > 378*404b540aSrobert { 379*404b540aSrobert // ... 380*404b540aSrobert }; 381*404b540aSrobert } // namespace __norm 382*404b540aSrobert 383*404b540aSrobert using namespace __debug __attribute__ ((strong)); 384*404b540aSrobert} 385*404b540aSrobert</pre> 386*404b540aSrobert 387*404b540aSrobert<h4><a name="mixing">Link- and run-time coexistence of release- and 388*404b540aSrobert debug-mode components</a></h4> 389*404b540aSrobert 390*404b540aSrobert<p>Because each component has a distinct and separate release and 391*404b540aSrobertdebug implementation, there are are no issues with link-time 392*404b540aSrobertcoexistence: the separate namespaces result in different mangled 393*404b540aSrobertnames, and thus unique linkage.</p> 394*404b540aSrobert 395*404b540aSrobert<p>However, components that are defined and used within the C++ 396*404b540aSrobertstandard library itself face additional constraints. For instance, 397*404b540aSrobertsome of the member functions of <code> std::moneypunct</code> return 398*404b540aSrobert<code>std::basic_string</code>. Normally, this is not a problem, but 399*404b540aSrobertwith a mixed mode standard library that could be using either 400*404b540aSrobertdebug-mode or release-mode <code> basic_string</code> objects, things 401*404b540aSrobertget more complicated. As the return value of a function is not 402*404b540aSrobertencoded into the mangled name, there is no way to specify a 403*404b540aSrobertrelease-mode or a debug-mode string. In practice, this results in 404*404b540aSrobertruntime errors. A simplified example of this problem is as follows. 405*404b540aSrobert</p> 406*404b540aSrobert 407*404b540aSrobert<p> Take this translation unit, compiled in debug-mode: </p> 408*404b540aSrobert<pre> 409*404b540aSrobert// -D_GLIBCXX_DEBUG 410*404b540aSrobert#include <string> 411*404b540aSrobert 412*404b540aSrobertstd::string test02(); 413*404b540aSrobert 414*404b540aSrobertstd::string test01() 415*404b540aSrobert{ 416*404b540aSrobert return test02(); 417*404b540aSrobert} 418*404b540aSrobert 419*404b540aSrobertint main() 420*404b540aSrobert{ 421*404b540aSrobert test01(); 422*404b540aSrobert return 0; 423*404b540aSrobert} 424*404b540aSrobert</pre> 425*404b540aSrobert 426*404b540aSrobert<p> ... and linked to this translation unit, compiled in release mode:</p> 427*404b540aSrobert 428*404b540aSrobert<pre> 429*404b540aSrobert#include <string> 430*404b540aSrobert 431*404b540aSrobertstd::string 432*404b540aSroberttest02() 433*404b540aSrobert{ 434*404b540aSrobert return std::string("toast"); 435*404b540aSrobert} 436*404b540aSrobert</pre> 437*404b540aSrobert 438*404b540aSrobert<p> For this reason we cannot easily provide safe iterators for 439*404b540aSrobert the <code>std::basic_string</code> class template, as it is present 440*404b540aSrobert throughout the C++ standard library. For instance, locale facets 441*404b540aSrobert define typedefs that include <code>basic_string</code>: in a mixed 442*404b540aSrobert debug/release program, should that typedef be based on the 443*404b540aSrobert debug-mode <code>basic_string</code> or the 444*404b540aSrobert release-mode <code>basic_string</code>? While the answer could be 445*404b540aSrobert "both", and the difference hidden via renaming a la the 446*404b540aSrobert debug/release containers, we must note two things about locale 447*404b540aSrobert facets:</p> 448*404b540aSrobert 449*404b540aSrobert<ol> 450*404b540aSrobert <li>They exist as shared state: one can create a facet in one 451*404b540aSrobert translation unit and access the facet via the same type name in a 452*404b540aSrobert different translation unit. This means that we cannot have two 453*404b540aSrobert different versions of locale facets, because the types would not be 454*404b540aSrobert the same across debug/release-mode translation unit barriers.</li> 455*404b540aSrobert 456*404b540aSrobert <li>They have virtual functions returning strings: these functions 457*404b540aSrobert mangle in the same way regardless of the mangling of their return 458*404b540aSrobert types (see above), and their precise signatures can be relied upon 459*404b540aSrobert by users because they may be overridden in derived classes.</li> 460*404b540aSrobert</ol> 461*404b540aSrobert 462*404b540aSrobert<p>With the design of libstdc++ debug mode, we cannot effectively hide 463*404b540aSrobert the differences between debug and release-mode strings from the 464*404b540aSrobert user. Failure to hide the differences may result in unpredictable 465*404b540aSrobert behavior, and for this reason we have opted to only 466*404b540aSrobert perform <code>basic_string</code> changes that do not require ABI 467*404b540aSrobert changes. The effect on users is expected to be minimal, as there are 468*404b540aSrobert simple alternatives (e.g., <code>__gnu_debug::basic_string</code>), 469*404b540aSrobert and the usability benefit we gain from the ability to mix debug- and 470*404b540aSrobert release-compiled translation units is enormous.</p> 471*404b540aSrobert 472*404b540aSrobert<h4><a name="coexistence_alt">Alternatives for Coexistence</a></h4> 473*404b540aSrobert<p>The coexistence scheme above was chosen over many alternatives, 474*404b540aSrobert including language-only solutions and solutions that also required 475*404b540aSrobert extensions to the C++ front end. The following is a partial list of 476*404b540aSrobert solutions, with justifications for our rejection of each.</p> 477*404b540aSrobert 478*404b540aSrobert<ul> 479*404b540aSrobert <li><em>Completely separate debug/release libraries</em>: This is by 480*404b540aSrobert far the simplest implementation option, where we do not allow any 481*404b540aSrobert coexistence of debug- and release-compiled translation units in a 482*404b540aSrobert program. This solution has an extreme negative affect on usability, 483*404b540aSrobert because it is quite likely that some libraries an application 484*404b540aSrobert depends on cannot be recompiled easily. This would not meet 485*404b540aSrobert our <b>usability</b> or <b>minimize recompilation</b> criteria 486*404b540aSrobert well.</li> 487*404b540aSrobert 488*404b540aSrobert <li><em>Add a <code>Debug</code> boolean template parameter</em>: 489*404b540aSrobert Partial specialization could be used to select the debug 490*404b540aSrobert implementation when <code>Debug == true</code>, and the state 491*404b540aSrobert of <code>_GLIBCXX_DEBUG</code> could decide whether the 492*404b540aSrobert default <code>Debug</code> argument is <code>true</code> 493*404b540aSrobert or <code>false</code>. This option would break conformance with the 494*404b540aSrobert C++ standard in both debug <em>and</em> release modes. This would 495*404b540aSrobert not meet our <b>correctness</b> criteria. </li> 496*404b540aSrobert 497*404b540aSrobert <li><em>Packaging a debug flag in the allocators</em>: We could 498*404b540aSrobert reuse the <code>Allocator</code> template parameter of containers 499*404b540aSrobert by adding a sentinel wrapper <code>debug<></code> that 500*404b540aSrobert signals the user's intention to use debugging, and pick up 501*404b540aSrobert the <code>debug<></code> allocator wrapper in a partial 502*404b540aSrobert specialization. However, this has two drawbacks: first, there is a 503*404b540aSrobert conformance issue because the default allocator would not be the 504*404b540aSrobert standard-specified <code>std::allocator<T></code>. Secondly 505*404b540aSrobert (and more importantly), users that specify allocators instead of 506*404b540aSrobert implicitly using the default allocator would not get debugging 507*404b540aSrobert containers. Thus this solution fails the <b>correctness</b> 508*404b540aSrobert criteria.</li> 509*404b540aSrobert 510*404b540aSrobert <li><em>Define debug containers in another namespace, and employ 511*404b540aSrobert a <code>using</code> declaration (or directive)</em>: This is an 512*404b540aSrobert enticing option, because it would eliminate the need for 513*404b540aSrobert the <code>link_name</code> extension by aliasing the 514*404b540aSrobert templates. However, there is no true template aliasing mechanism 515*404b540aSrobert is C++, because both <code>using</code> directives and using 516*404b540aSrobert declarations disallow specialization. This method fails 517*404b540aSrobert the <b>correctness</b> criteria.</li> 518*404b540aSrobert 519*404b540aSrobert <li><em> Use implementation-specific properties of anonymous 520*404b540aSrobert namespaces. </em> 521*404b540aSrobert See <a 522*404b540aSrobert href="http://gcc.gnu.org/ml/libstdc++/2003-08/msg00004.html"> this post 523*404b540aSrobert </a> 524*404b540aSrobert This method fails the <b>correctness</b> criteria.</li> 525*404b540aSrobert 526*404b540aSrobert <li><em>Extension: allow reopening on namespaces</em>: This would 527*404b540aSrobert allow the debug mode to effectively alias the 528*404b540aSrobert namespace <code>std</code> to an internal namespace, such 529*404b540aSrobert as <code>__gnu_std_debug</code>, so that it is completely 530*404b540aSrobert separate from the release-mode <code>std</code> namespace. While 531*404b540aSrobert this will solve some renaming problems and ensure that 532*404b540aSrobert debug- and release-compiled code cannot be mixed unsafely, it ensures that 533*404b540aSrobert debug- and release-compiled code cannot be mixed at all. For 534*404b540aSrobert instance, the program would have two <code>std::cout</code> 535*404b540aSrobert objects! This solution would fails the <b>minimize 536*404b540aSrobert recompilation</b> requirement, because we would only be able to 537*404b540aSrobert support option (1) or (2).</li> 538*404b540aSrobert 539*404b540aSrobert <li><em>Extension: use link name</em>: This option involves 540*404b540aSrobert complicated re-naming between debug-mode and release-mode 541*404b540aSrobert components at compile time, and then a g++ extension called <em> 542*404b540aSrobert link name </em> to recover the original names at link time. There 543*404b540aSrobert are two drawbacks to this approach. One, it's very verbose, 544*404b540aSrobert relying on macro renaming at compile time and several levels of 545*404b540aSrobert include ordering. Two, ODR issues remained with container member 546*404b540aSrobert functions taking no arguments in mixed-mode settings resulting in 547*404b540aSrobert equivalent link names, <code> vector::push_back() </code> being 548*404b540aSrobert one example. 549*404b540aSrobert See <a 550*404b540aSrobert href="http://gcc.gnu.org/ml/libstdc++/2003-08/msg00177.html">link 551*404b540aSrobert name</a> </li> 552*404b540aSrobert</ul> 553*404b540aSrobert 554*404b540aSrobert<p>Other options may exist for implementing the debug mode, many of 555*404b540aSrobert which have probably been considered and others that may still be 556*404b540aSrobert lurking. This list may be expanded over time to include other 557*404b540aSrobert options that we could have implemented, but in all cases the full 558*404b540aSrobert ramifications of the approach (as measured against the design goals 559*404b540aSrobert for a libstdc++ debug mode) should be considered first. The DejaGNU 560*404b540aSrobert testsuite includes some testcases that check for known problems with 561*404b540aSrobert some solutions (e.g., the <code>using</code> declaration solution 562*404b540aSrobert that breaks user specialization), and additional testcases will be 563*404b540aSrobert added as we are able to identify other typical problem cases. These 564*404b540aSrobert test cases will serve as a benchmark by which we can compare debug 565*404b540aSrobert mode implementations.</p> 566*404b540aSrobert 567*404b540aSrobert<!-- ####################################################### --> 568*404b540aSrobert 569*404b540aSrobert<hr /> 570*404b540aSrobert<p class="fineprint"><em> 571*404b540aSrobertSee <a href="17_intro/license.html">license.html</a> for copying conditions. 572*404b540aSrobertComments and suggestions are welcome, and may be sent to 573*404b540aSrobert<a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>. 574*404b540aSrobert</em></p> 575*404b540aSrobert 576*404b540aSrobert 577*404b540aSrobert</body> 578*404b540aSrobert</html> 579