    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Techniques for Debugging in C++</title>
        <link>https://members.accu.org/index.php/articles/423</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #46 - Dec 2001</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c158/">46</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-158/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+158/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Techniques for Debugging in C++</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 December 2001 16:46:08 +00:00 or Wed, 26 December 2001 16:46:08 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>What happens when programs die? Do they go to digital heaven?
Are they reincarnated (if they're really bad, perhaps they come
back as a daemon)? Or do they leave a legacy for their children - a
guide on how to live a better life?</p>
<p>In this article I present a debugging library 'dbg' I have
written that has been specifically designed to integrate well with
modern C++ programming idioms. I also describe a number of common
debugging methods that can employ these utilities.</p>
<p>Before I present the library, we'll cover a little
background.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e26" id="d0e26"></a>Traditional
mechanisms</h2>
</div>
<p>People have been debugging ever since the first program was
written. In fact, it's been going on even before this. So how do we
debug in these enlightened times? Not much more methodically than
when we did it ten years ago.</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e31" id="d0e31"></a>Debuggers</h2>
</div>
<p>When talking about debugging techniques here I'm going to
deliberately avoid mention of a debugger. Why? Well, a debugger is
not always present on every platform (I've recently been playing
with some embedded environments where a debugger was not easily
available). Nothing can compare to the power of using a debugger to
&quot;get into&quot; program execution, but when a debugger is not available
we still need an arsenal of techniques to allow us to debug
effectively.</p>
<p>Besides, oranges are not the only fruit - a debugger is not the
only tool to aid debugging. It's good for dissecting a program that
doesn't work properly, but it's not too handy to pre-emptively
detect code that could cause problems later down the line.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e38" id=
"d0e38"></a>Constraints</h2>
</div>
<p>In this article I focus more on constraint-based techniques for
ensuring our code is bug free. That is, writing some extra code
(that can be compiled out at build-time, if necessary) that
enforces a set of logical constraints. These constraints define
whether or not the program is operating correctly.</p>
<p>If constraints are added in at the smallest granularity in the
code-base then the maximum benefit is achieved - most of the code
is sanity tested. Of course there is a possible downside to this;
the extra code may make your program run slower. Whether this is
significant is a moot point, and will depend to a large extent on
the platform and target for your code.</p>
<p>Judicious use of these constraints in your code can leave you
satisfied in the correct behaviour and can help identify problem
situations early, before they escalate into major problems.</p>
<p>The traditional language mechanism for doing this is the
standard C library assert macro. Any programmer who knows a for
from a while knows that assert is a simple macro used to enforce a
run time constraint in the manner described above.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e49" id="d0e49"></a>Logging</h2>
</div>
<p>Another mechanism used to aid the debugging process is logging -
for example the Win32 RETAILMSG system, or the Unix syslog. Good
logging mechanisms allow us to trace execution, report program
state, and are flexible enough to allow us to divert output to
different destinations, perhaps also prioritising the significance
of certain messages.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e54" id="d0e54"></a>Defensive
programming</h2>
</div>
<p>Of course, other good debugging (or bug avoiding) practices are
the set of defensive programming techniques. This library and
article were inspired by my C Vu Professionalism in Programming
column on defensive programming <a href=
"#Goodcliffe">[Goodcliffe]</a>. We won't cover the same ground as
in that article.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e62" id="d0e62"></a>Moving
forward</h2>
</div>
<p>Constraints are a very valuable method for getting your code to
work and keeping it working. However, <tt class=
"literal">assert</tt> is not really satisfactory. This is mainly
because of what it is: a C-style macro. Why was it ever allowed
into the standard library as it is - in lower case is anyone's
guess. It should really have been an upper case macro.</p>
<p>If we can avoid use of macros (and the pre-processor as a whole)
we should - it jars with the modern C++ idiom. As Stroustrup says,
&quot;The first rule about macros is: Don't use them unless you have
to.&quot; <a href="#Stroustrup7.8">[Stroustrup7.8]</a>. So do we have
to?</p>
<p>As far as logging is concerned, we need a high-quality portable
logging mechanism. Perhaps it can fall back to the existing
platform-dependant logging mechanisms at the implementation level.
However, you can't write platform independent code without a good
logging abstraction.</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e77" id="d0e77"></a>What we'd like
to achieve in a good debugging library</h2>
</div>
<p>There are a number of requirements for a good &quot;debugging&quot;
library that I worked from:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Constraints must be better than assert in all areas</p>
</li>
<li>
<p>Must compile out to nothing in production builds, if
required</p>
</li>
<li>
<p>Minimal performance overhead whilst running</p>
</li>
<li>
<p>Easy to use, non-intrusive code</p>
</li>
<li>
<p>Avoids the evil pre-processor</p>
</li>
<li>
<p>Must integrate well with the existing standard library and
modern C++ idioms</p>
</li>
<li>
<p>Obviously, must be completely portable across compilers and
OSes</p>
</li>
</ul>
</div>
<p>There are a number of secondary feature requirements I was
aiming for:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Ability to differentiate different diagnostic levels - and
enable/disable them independently.</p>
</li>
<li>
<p>Control at run time whether constraints are fatal (i.e. cause an
abort(), throw exceptions or are disabled completely).</p>
</li>
<li>
<p>Differentiate different logging sources, and can selectively
enable/disable output for a single device driver in an entire
project, for example.</p>
</li>
<li>
<p>Provide execution tracing capabilities.</p>
</li>
<li>
<p>Support writing post conditions (this is hard to model in
C++)</p>
</li>
<li>
<p>Provide an easy mechanism for printing tracing diagnostics out
when needed</p>
</li>
</ul>
</div>
<p>There are a number of very common constraints traditionally
tested for with the assert macro. These cases are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>execution reaches unimplemented functionality</p>
</li>
<li>
<p>execution reaches a place where it &quot;shouldn't get&quot;</p>
</li>
<li>
<p>a pointer value is invalid i.e. it equals zero</p>
</li>
<li>
<p>array access bounds are out of range</p>
</li>
<li>
<p>finally, any other general Boolean constraint (assertion)</p>
</li>
</ul>
</div>
<p>The library should provide different versions of an assert-like
function that makes the intent of the constraints listed above
explicit. When you come across something like <tt class=
"literal">check_pointer_isnt_null(p-ptrval)</tt> it's immediately
clear what is meant, instead of something like <tt class=
"literal">assert(0 != p-ptrval)</tt>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e151" id=
"d0e151"></a>Motivation</h2>
</div>
<p>So why write a new debugging library?</p>
<p>There are already a number of other debugging libraries
available. However, they don't all fulfil the requirements laid out
above. Some don't compile out to nothing in a production build.
Some make very heavy use of the pre-processor. Others don't fit
naturally into modern C++, or the debugging commands are too
intrusive, or they don't read easily in the code. I've yet to find
the perfect tool for the job.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e158" id="d0e158"></a>About the dbg
library</h2>
</div>
<p>Having set the scene in some detail we can now look at the dbg
library. To a large extent it meets the ideals set out above. It
does so in a manner that fits in well with modern C++ idioms. We'll
see the basics of how to use it, and then discuss some of the
design decisions involved.</p>
<p>As you've probably guessed, the main facilities provided by the
dbg library are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Expressive and easy to use constraint utilities</p>
</li>
<li>
<p>Flexible integrated logging support based on C++ streams</p>
</li>
</ul>
</div>
<p>The dbg library aims to be something that could credibly be
bundled with the standard C++ library. That is, it should be of the
highest quality possible and follow standard library conventions
where possible.</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e174" id="d0e174"></a>Constraints
using dbg</h2>
</div>
<p>Firstly, the dbg library defines a number of debugging levels,
used to describe the severity of a constraint. The levels
specifically related to constraints are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><tt class="literal">info</tt> - Not a problem, just a message
for interest</p>
</li>
<li>
<p><tt class="literal">warning</tt> - Potentially bad things that
can occur, but can be recovered from</p>
</li>
<li>
<p><tt class="literal">error</tt> - For errors that can't be
recovered from</p>
</li>
<li>
<p><tt class="literal">fatal</tt> - Errors at this level will cause
the dbg library to abort execution</p>
</li>
</ul>
</div>
<p>The different types of constraint provided by the dbg library
are:</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e202" id="d0e202"></a>1.
check_ptr</h2>
</div>
<p>One of the most common constraint checks is that pointers are
non-zero. I often wonder in C++ how valid a check this is, since
new does not return zero any more (unless you ask it to, of
course). However, when integrating with legacy code you may get
invalid zero-pointers flying around. Checking that a pointer
parameter is non-null is still a very handy check<sup>[<a name=
"d0e207" href="#ftn.d0e207" id="d0e207">1</a>]</sup>.</p>
<pre class="programlisting">
void example(int *ptr) { dbg::check_ptr(ptr, DBG_HERE); // <sup>[<a name="d0e213"
href="#ftn.d0e213" id="d0e213">2</a>]</sup> // do something }
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e217" id="d0e217"></a>2.
check_bounds</h2>
</div>
<p>This checks your array access indexes to ensure they are in
bounds. If the array is in scope, the dbg library can deduce the
size of the array so you don't have to supply the bounds manually.
This can be very valuable when array sizes change frequently.</p>
<pre class="programlisting">
int array[10] int index = 10; dbg::check_bounds(index, array, DBG_HERE); array[index] = 5;
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e224" id="d0e224"></a>3.
sentinel</h2>
</div>
<p>You should put this directly after a &quot;should never get here&quot;
comment, just to ensure that you never get there.</p>
<pre class="programlisting">
switch (variable) { ... default: { // Should never get here dbg::sentinel(DBG_HERE); } }
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e231" id="d0e231"></a>4.
unimplemented</h2>
</div>
<p>When you start to develop code, you'll probably stub
functionality to be implemented later. Use of this constraint can
prove when areas of code that are not finished off get called.</p>
<pre class="programlisting">
switch (variable) { ... case SOMETHING: { dbg::unimplemented( dbg::warning, DBG_HERE); break; // just in case constraint behaviour is // non-fatal } ... }
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e238" id="d0e238"></a>5.
assertion</h2>
</div>
<p>Finally, the general purpose assertion, which allows you to put
an arbitrary Boolean expression as a test. This is like the C-style
<tt class="literal">assert</tt>.</p>
<pre class="programlisting">
int i = 5; dbg::assertion(DBG_ASSERTION(i != 6));
</pre></div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e248" id="d0e248"></a>Assertion
behaviour</h2>
</div>
<p>The behaviour of the dbg library constraints can be set with
<tt class="literal">dbg::set_assertion_behaviour</tt> to be one
of:</p>
<div class="variablelist">
<dl>
<dt><span class="term">assertions_abort:</span></dt>
<dd>
<p>Assertions cause a program abort</p>
</dd>
<dt><span class="term">assertions_throw:</span></dt>
<dd>
<p>Assertions cause a <tt class="literal">dbg_exception</tt> to be
thrown</p>
</dd>
<dt><span class="term">assertions_continue:</span></dt>
<dd>
<p>Assertions cause the standard diagnostic printout to occur (as
it does with the above behaviours) but execution continues
regardless</p>
</dd>
</dl>
</div>
<p>Constraint activity and the logging output can be
enabled/disabled at run time, as well as being compiled out in its
entirety.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e280" id=
"d0e280"></a>assertion_period</h2>
</div>
<p>As well as specifying the <tt class=
"literal">assertion_behaviour</tt>, you can set an assertion
period. This is helpful for constraints that get triggered very
frequently. If you <tt class="literal">set_assertion_period</tt> of
one second, then no matter how many times a constraint fails, it
will only produce output once a second. However, it will produce
information on how many times it has really triggered for
reference.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e291" id="d0e291"></a>Advanced
logging with dbg</h2>
</div>
<p>The dbg logging support is very flexible. You can attach and
detach arbitrary <tt class="literal">ostream</tt>s to/from each
different diagnostic level. You can attach any number of <tt class=
"literal">ostream</tt>s to each level. Initially, <tt class=
"literal">std::cerr</tt> is attached to the <tt class=
"literal">dbg::error</tt> and <tt class="literal">dbg::fatal</tt>
diagnostic levels.</p>
<p>At any point in your code you can log diagnostic information in
the usual C++ ostream style. You just write</p>
<pre class="programlisting">
dbg::out(dbg::info) &quot;Hello this is some information\n&quot;;
</pre>
<p>There are a number of other neat logging features. For example,
you can enable a number of diagnostic prefixes with <tt class=
"literal">enable_level_prefix</tt> and <tt class=
"literal">enable_time_prefix</tt>. Doing so would produce output
like this:</p>
<pre class="programlisting">
*** Tue Jul 31 09:37:08 2001: info: sentinel reached 
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e325" id="d0e325"></a>Separating
diagnostic sources</h2>
</div>
<p>Well-structured code-bases split code into distinct functional
units, like libraries, layers, or separate device drivers. The use
of the dbg library can compliment this. For each constraint, you
can describe a diagnostic source (e.g. <tt class=
"literal">foo_lib</tt>) and then at run time enable/disable each
source selectively. This allows you to pinpoint diagnostics from
only particular sections of your code.</p>
<p>For example if you have a number of device drivers, with one
called &quot;disk&quot; and one called &quot;keyboard&quot; then in the first you might
write a constraint like</p>
<pre class="programlisting">
dbg::check_ptr(&quot;disk_drv&quot;, disk-address);
</pre>
<p>and in the second</p>
<pre class="programlisting">
dbg::check_ptr(&quot;keyboard_drv&quot;, keyboard-event_queue);
</pre>
<p>Now if the keyboard driver appears to be doing odd things, you
can switch on its diagnostics by putting this in your main
code:</p>
<pre class="programlisting">
// Enable the keyboard diagnostics at all levels dbg::enable(dbg::all, &quot;keyboard_drv&quot;, true);
</pre>
<p>The keyboard driver code will now produce diagnostic output, and
the constraints will be made active, whilst the disk driver will
still have all diagnostics dormant.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e347" id="d0e347"></a>Design
decisions</h2>
</div>
<p>That was a pretty quick tutorial on the use of the dbg library.
See the online documentation for more information. Now that we know
how to use the library, let's see how dbg achieves the goals set
out for it.</p>
<p>I present this library partly as a call for peer review. Whilst
I have used dbg successfully for some time, if there are comments
from the ACCU members that would cause the library to improve I'd
be interested in hearing them.</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e354" id="d0e354"></a>Avoiding
macros</h2>
</div>
<p>This is done by making the constraint utilities real functions,
not macros. If you do a build that disables the dbg library, then
the functions are replaced by empty inline stubs that optimise away
to nothing.</p>
<p>This requires a little template magic to produce a credible
ostream substitute class, see the <tt class="literal">dbg.h</tt>
header file for details.</p>
<p>Do we really need to avoid macros? I believe that in modern C++
code it makes more sense to see a line like:</p>
<pre class="programlisting">
dbg::check_ptr(p);
</pre>
<p>rather than something like:</p>
<pre class="programlisting">
DBG_CHECK_PTR(p);
</pre>
<p>It reads naturally - in the same way that standard algorithms
express intent so much more clearly than a for loop.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e374" id="d0e374"></a>Why is the
general purpose constraint called 'assertion' not 'assert'?</h2>
</div>
<p>This is one of my annoyances. I can't call the general purpose
constraint assert since if someone inadvertently mixes <tt class=
"literal">dbg.h</tt> with <tt class="literal">assert.h</tt> nasty
things happen. This is a perfectly good reason for not wanting all
of our constraints to be macros as well.</p>
<p>Perhaps this is a good argument for a scope-respecting
pre-processor? :-)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e387" id="d0e387"></a>How to
implement differentiated diagnostic sources</h2>
</div>
<p>This is perhaps the least satisfactory part of the design. Of
course, you could remove the facility and remove the problem
altogether, but it is genuinely useful.</p>
<p>The reason I'm not happy with the implementation is that it
seems to make production builds grow, since the source is
identified as a character string; most optimisers don't seem to be
able to remove the string if an inline function doesn't use it.
This is very annoying.</p>
<p>Other alternatives to using a string to identify the source
would be to use integer source codes - but that would require the
programmer to have some kind of shared error code header file - a
maintenance nightmare. Alternatively, some source definition object
could be created and referenced in each dbg call. For diagnostic
sources that can span multiple files, this can become clumsy too -
you'd have to declare the object in a header file to share it.
That's too intrusive.</p>
<p>I'm open to suggestions on this one.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e398" id="d0e398"></a>Post
conditions</h2>
</div>
<p>I've done what I can with the post conditions, but it's still
not a full and general post condition implementation. The problem
here is with the C++ language itself. In order to create a safe
post condition you need to use the RAII idiom <a href=
"#Stroustrup14.4.1">[Stroustrup14.4.1]</a>. When you create a class
to run the post condition you can't include arbitrary code. You
can't even do this with a disgusting macro construct
unfortunately.</p>
<p>Perhaps we should all give up and use Eiffel? Either that or be
happy writing unsafe code...</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e408" id="d0e408"></a>Non-debug
constraint functions don't have the same signature as the debug
versions</h2>
</div>
<p>This has been mentioned to me as an undesirable feature of the
library. I don't personally have a big religious problem with it,
and can't see a better way of implementing what this does.</p>
<p>The mechanism used has the overwhelming advantage that it is
very simple, and does (on the whole) compile out to nothing.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e415" id="d0e415"></a>Tracing needs
a named object</h2>
</div>
<p>You have to write:</p>
<pre class="programlisting">
dbg::trace trace_object(DBG_HERE);
</pre>
<p>and can't miss out the variable name. If you do, the variable is
anonymous and gets destroyed at the same place it is created,
rather than when the variable goes out of scope. Unfortunately we
have no way of enforcing this in the code, which may lead to
confusing results when used incorrectly.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e424" id="d0e424"></a>Not every
platform provides a __FUNCTION__ like definition</h2>
</div>
<p>It's a shame, and it should be in the standard IMHO. On
platforms without a <tt class="literal">__FUNCTION__</tt> the
diagnostic output is not so helpful.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e432" id="d0e432"></a>How useful
are assertions throwing?</h2>
</div>
<p>It's neat to be able to select a &quot;throwing&quot; style assertion, but
what happens in code not expecting to have to handle exceptions? It
may cause more trouble than it's worth. It's certainly debatable
whether dbg exceptions should be caught, since they can be compiled
out.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e437" id=
"d0e437"></a>Performance</h2>
</div>
<p>In order to have some confidence that the dbg library really
does compile away to nothing we need concrete proof. I have used
the library on a number of platforms, and the results are shown
below. Note that here we are not comparing image sizes across
compilers - I may not have configured each compiler to generate the
smallest code possible (certainly the gcc 3.00 code is very large,
and this <span class="emphasis"><em>can</em></span> be reduced
greatly). The first two code size columns are for a test program
built with debugging support enabled and disabled (i.e.
conditionally compiled out) respectively. When disabled, the dbg
library is not linked to the executable so we can expect a dramatic
size reduction<sup>[<a name="d0e445" href="#ftn.d0e445" id=
"d0e445">3</a>]</sup>. The final column shows the size of this test
program when no dbg APIs are called at all. In an ideal world this
size will be identical to the <span class="emphasis"><em>release
build</em></span> column.</p>
<div class="table"><a name="d0e452" id="d0e452"></a>
<p class="title c2">Table 1. Table of compiled image sizes</p>
<table summary="Table of compiled image sizes" border="1">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td rowspan="2">compiler/version/ platform/options</td>
<td colspan="3">compiled image sizes</td>
</tr>
<tr>
<td>-DDBG</td>
<td>release build</td>
<td>removed completely</td>
</tr>
<tr>
<td>gcc 2.96 Linux (-O0)</td>
<td>69381</td>
<td>21590</td>
<td>15978</td>
</tr>
<tr>
<td>gcc 2.96 Linux (-O1)</td>
<td>66411</td>
<td>15883</td>
<td>15725</td>
</tr>
<tr>
<td>gcc 2.96 Linux (-O2)</td>
<td>66635</td>
<td>15851</td>
<td>15661</td>
</tr>
<tr>
<td>gcc 2.96 Linux (-O3)</td>
<td>66635</td>
<td>15851</td>
<td>15693</td>
</tr>
<tr>
<td>gcc 2.96 Linux (-O4)</td>
<td>66635</td>
<td>15851</td>
<td>15693</td>
</tr>
<tr>
<td>gcc 3.00 Linux (-O3)</td>
<td>588147</td>
<td>129680</td>
<td>129584</td>
</tr>
<tr>
<td>bcc32 WinNT 5.5.1</td>
<td>201728</td>
<td>138752</td>
<td>138240</td>
</tr>
<tr>
<td>MSVC 6.0 WinNT (release, 0=max speed)</td>
<td>54593</td>
<td>11662</td>
<td>11496</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>We can see that in the general case the library performs just as
we'd hope - there is no significant overhead associated with its
use for non-debug builds. However, when we employ the string
literal diagnostic source definitions there is a slight overhead
(since the optimiser does not remove the string literal when it
removes the empty inline function).</p>
<p>I have not performed any investigations into the overhead of the
dbg library in terms of runtime speed. Empirically, it has always
run far better than satisfactorily, so it has not been necessary.
Perhaps an Overload reader would care to generate this data?</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e550" id=
"d0e550"></a>Extensions</h2>
</div>
<p>There are still some areas the library could be expanded in. Top
of my list are:</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e555" id="d0e555"></a>Assertions
breaking out into the debugger</h2>
</div>
<p>Another <tt class="literal">assertion_behaviour</tt> could be
added to break out to the debugger when a constraint is broken.</p>
<p>It's a nice idea, but unfortunately far too platform specific -
it would require the library to be ported to each new platform,
which is undesirable.</p>
<p>Another approach I investigated was to add an <tt class=
"literal">on_assert</tt> callback facility akin to <tt class=
"literal">atexit</tt>. In there you can register your own debugger
trap. However, I didn't like this. When the debugger opens, you
have to step back through your function, then some internal dbg
library logic, before reaching the point of constraint failure -
not too neat.</p>
<p>There is an internal dbg library function <tt class=
"literal">do_assertion_behaviour</tt>. You can set a breakpoint
here to get the same effect anyway.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e578" id="d0e578"></a>Only works
with ostreams</h2>
</div>
<p>The dbg library logging only currently works for <tt class=
"literal">ostreams</tt>. It would be nice to extend it to all
<tt class="literal">basic_ostreams</tt>.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e589" id=
"d0e589"></a>Conclusion</h2>
</div>
<p>The dbg library is a powerful implementation of the constraint
defensive programming technique, with a number of other useful
capabilities including a flexible and integrated logging
mechanism.</p>
<p>To be effective you really need to employ the technique from the
start of a project - it's very hard to retrofit constraints on at a
later date. You never have the time or impetus to be rigorous
enough to make it fully valuable.</p>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e596" id=
"d0e596"></a>Availability</h2>
</div>
<p>The dbg library is available along with online documentation
from <a href="http://cthree.org/dbg/" target=
"_top">http://cthree.org/dbg/</a>. Future developments will be
posted there. The API documentation is available from <a href=
"http://cthree.org/dbg/kdoc/dbg.html" target=
"_top">http://cthree.org/dbg/kdoc/dbg.html</a>.</p>
</div>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e607" id="d0e607"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Goodcliffe" id="Goodcliffe"></a>
<p class="bibliomixed">[Goodcliffe] Pete Goodliffe. Professionalism
in Programming #8: Defensive Programming. In: C Vu 13.3. ISSN:
1354-3164</p>
</div>
<div class="bibliomixed"><a name="Stroustrup7.8" id=
"Stroustrup7.8"></a>
<p class="bibliomixed">[Stroustrup7.8] Bjarne Stroustrup. The C++
Programming Language. 3rd Edition. ISBN: 0-201-88954-4. Section
7.8: Macros.</p>
</div>
<div class="bibliomixed"><a name="Stroustrup14.4.1" id=
"Stroustrup14.4.1"></a>
<p class="bibliomixed">[Stroustrup14.4.1] Bjarne Stroustrup. The
C++ Programming Language. 3rd Edition. ISBN: 0-201-88954-4. Section
14.4.1: Using Constructors and Destructors.</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e207" href="#d0e207" id=
"ftn.d0e207">1</a>]</sup> Zero is a special case of a set of
invalid pointer values. At least in the C/C++ standard it is
defined to be a pointer to nothing. However, on most platforms 1
and 3 are also invalid - does testing for zero give us a false
sense of security?</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e213" href="#d0e213" id=
"ftn.d0e213">2</a>]</sup> DBG_HERE is a helpful macro that
describes a source file position - OK we were trying to avoid the
preprocessor, but this is unavoidable.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e445" href="#d0e445" id=
"ftn.d0e445">3</a>]</sup> At least for a small test program. For
larger programs this reduction in size will quickly diminish in
significance.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
