include/nana/DI.h

Go to the documentation of this file.
00001 /*
00002  * DI.h -  support for invariants (assertions) using the gdb debugger.
00003  *
00004  * Copyright (c) 1997 Phil Maker
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00020  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00026  * SUCH DAMAGE.
00027  *
00028  * Id: DI.h,v 1.1.1.1 1997/11/23 11:45:50 pjm Exp 
00029  */
00030 
00031 #ifndef _DI_h_
00032 #define _DI_h_ 1
00033 
00034 #ifdef __cplusplus
00035 extern "C" {
00036 #endif
00037 
00038 #ifndef WITHOUT_NANA 
00039 
00040 /*
00041  * nana-config.h - the system wide configuration file; we put the ifndef
00042  *   around it to avoid the file 5 million times during a compile.
00043  */
00044 
00045 #ifndef _nana_config_h_
00046 #include <nana-config.h>
00047 #endif
00048 
00049 /* 
00050  * DI_LEVEL sets the level of invariant analogously to NDEBUG in assert.h
00051  *
00052  *   DI_LEVEL == 2: invariants are always evaluated.
00053  *   DI_LEVEL == 1: evaluate invariants iff they have a true GUARD.
00054  *   DI_LEVEL == 0: invariants are never evaluated.
00055  */
00056 
00057 #ifndef DI_LEVEL /* define DEFAULT for DI_LEVEL */
00058 #define DI_LEVEL 1
00059 #endif
00060 
00061 
00062 
00063 /*
00064  * DI_DEFAULT_GUARD - the default guard expression; an invariant is checked
00065  *     iff the guard is true. By default its always true.
00066  */
00067 
00068 #ifndef DI_DEFAULT_GUARD
00069 #define DI_DEFAULT_GUARD 1
00070 #endif
00071 
00072 /*
00073  * DI_DEFAULT_PARAMS - the default value to be passed as the second argument
00074  *       to the handler macro when an invariant fails.
00075  */
00076 
00077 #ifndef DI_DEFAULT_PARAMS
00078 #define DI_DEFAULT_PARAMS /* nothing */
00079 #endif
00080 
00081 /*
00082  * DI_DEFAULT_HANDLER - called when an error is detected by DI; 
00083  */
00084 
00085 #ifndef DI_DEFAULT_HANDLER /* define default handler */
00086 
00087 #define DI_DEFAULT_HANDLER(e,f,l,p) \
00088   @@echo e has failed at f:l with p\n@@ \
00089   @@where@@ /* stack backtrace */
00090 
00091 #endif /* DI_DEFAULT_HANDLER */
00092 
00093 /*
00094  * DI_MAKE_VALID_BREAKPOINT(e) - called whenever we generate a DI breakpoint;
00095  *     the aim is to make sure a breakpoint can be set at the current location
00096  *     and that any expressions will be evaluated by gdb correctly. 
00097  *     This is the portable default; an architecture specific version
00098  *     is generated by the configure script into nana-config.h
00099  */
00100 
00101 #ifndef DI_MAKE_VALID_BREAKPOINT
00102 static volatile int _di_target;
00103 
00104 #define DI_MAKE_VALID_BREAKPOINT(e) _di_target = 0
00105 #endif
00106 
00107 /*
00108  * _DIGHPS - implements the DI macros, it comes in two variants:
00109  * 
00110  * ifdefined(_NANA_FILTER_) then we are generating debugger commands
00111  * else generating C text.
00112  */
00113 
00114 #if DI_LEVEL == 2 /* always check the assertion */
00115 #ifdef _NANA_FILTER_
00116 #define _DIGHPS(e,g,h,p,s) \
00117         @@break @__FILE__:__LINE__@@ \
00118         @@condition $bpnum (!(e))@@ \
00119         @@command $bpnum@@ \
00120         @@silent@@ \
00121         h(s,f,l,p) \
00122         @@end@@
00123 #else
00124 #define _DIGHPS(e,g,h,p,s) DI_MAKE_VALID_BREAKPOINT(e)
00125 #endif /* _NANA_FILTER_ */
00126 
00127 #elif DI_LEVEL == 1 /* check it iff g is true */
00128 
00129 #ifdef _NANA_FILTER_
00130 #define _DIGHPS(e,g,h,p,s) \
00131         @@break @__FILE__:__LINE__@@ \
00132         @@condition $bpnum (g) && (!(e))@@ \
00133         @@command $bpnum@@ \
00134         @@silent@@ \
00135         h(s,f,l,p) \
00136         @@end@@
00137 #else 
00138 #define _DIGHPS(e,g,h,p,s) DI_MAKE_VALID_BREAKPOINT(e)
00139 #endif /* _NANA_FILTER_ */
00140 
00141 #elif DI_LEVEL == 0 /* no assertions so just remove them */
00142 #define _DIGHPS(e,g,h,p,s) /* nothing */
00143 #endif /* DI_LEVEL */
00144 
00145 
00146 /*
00147  * DSG(e,g), DS(e) - are used to set variables in the debugger from 
00148  *        within C programs. These convenience variables can then be
00149  *        used in invariants later on to refer to the previous state 
00150  *        of the program.
00151  * 
00152  *        DS($x = x); ....; DI($x + 10 == x); 
00153  */
00154 
00155 #if DI_LEVEL == 2
00156 #ifdef _NANA_FILTER_
00157 #define DSG(e,g) \
00158         @@break @__FILE__:__LINE__@@ \
00159         @@command@@ \
00160         @@silent@@ \
00161         @@set e@@ \
00162         @@cont@@ \
00163         @@end@@
00164 #else 
00165 #define DSG(e,g) DI_MAKE_VALID_BREAKPOINT(e)
00166 #endif
00167 
00168 #elif DI_LEVEL == 1
00169 #ifdef _NANA_FILTER_
00170 #define DSG(e,g) \
00171         @@break @__FILE__:__LINE__ if g@@ \
00172         @@command@@ \
00173         @@silent@@ \
00174         @@set e@@ \
00175         @@cont@@ \
00176         @@end@@
00177 #else
00178 #define DSG(e,g) DI_MAKE_VALID_BREAKPOINT(e)
00179 #endif /* _NANA_FILTER_ */
00180 
00181 #elif DI_LEVEL == 0
00182 #define DSG(e,g) /* nothing */
00183 #else
00184 error DI_LEVEL should be 0 or 1 or 2
00185 #endif
00186 
00187 #define DS(e) DSG(e,DI_DEFAULT_GUARD)
00188 
00189 /*
00190  * And all the user macros; these are used to put in the default arguments.
00191  * The name is used to determine the arguments; e.g. DIGH takes an expression
00192  * to check; a guard and a handler as parameters. The letters in the names
00193  * are in ascending order (i.e. DIGH(...) not DIHG(...)).
00194  *
00195  * DI[G][H][P] - it must be true (e) with an optional guard, handler and 
00196  *    parameter for the handler.
00197  * DN[G][H][P] - as for DI... except that (e) must never ever be true.
00198  */
00199 
00200 #define DI(e) \
00201   _DIGHPS(e,DI_DEFAULT_GUARD,DI_DEFAULT_HANDLER,DI_DEFAULT_PARAMS,"DI("#e")")
00202 #define DIG(e,g) \
00203   _DIGHPS(e,g,DI_DEFAULT_HANDLER,DI_DEFAULT_PARAMS,"DI("#e")")
00204 #define DIH(e,h) \
00205   _DIGHPS(e,DI_DEFAULT_GUARD,h,DI_DEFAULT_PARAMS,"DI("#e")")
00206 #define DIP(e,p) \
00207   _DIGHPS(e,DI_DEFAULT_GUARD,DI_DEFAULT_HANDLER,p,"DI("#e")")
00208 #define DIGH(e,g,h) \
00209   _DIGHPS(e,g,h,DI_DEFAULT_PARAMS,"DI("#e")")
00210 #define DIGP(e,g,p) \
00211   _DIGHPS(e,g,DI_DEFAULT_HANDLER,p,"DI("#e")")
00212 #define DIHP(e,h,p) \
00213   _DIGHPS(e,DI_DEFAULT_GUARD,h,p,"DI("#e")")
00214 #define DIGHP(e,g,h,p) \
00215   _DIGHPS(e,g,h,p,"DI("#e")")
00216 
00217 #define DN(e) \
00218   _DIGHPS((!(e)),DI_DEFAULT_GUARD,DI_DEFAULT_HANDLER,DI_DEFAULT_PARAMS,"DN("#e")")
00219 #define DNG(e,g) \
00220   _DIGHPS((!(e)),g,DI_DEFAULT_HANDLER,DI_DEFAULT_PARAMS,"DN("#e")")
00221 #define DNH(e,h) \
00222   _DIGHPS((!(e)),DI_DEFAULT_GUARD,h,DI_DEFAULT_PARAMS,"DN("#e")")
00223 #define DNP(e,p) \
00224   _DIGHPS((!(e)),DI_DEFAULT_GUARD,DI_DEFAULT_HANDLER,p,"DN("#e")")
00225 #define DNGH(e,g,h) \
00226   _DIGHPS((!(e)),g,h,DI_DEFAULT_PARAMS,"DN("#e")")
00227 #define DNGP(e,g,p) \
00228   _DIGHPS((!(e)),g,DI_DEFAULT_HANDLER,p,"DN("#e")")
00229 #define DNHP(e,h,p) \
00230   _DIGHPS((!(e)),DI_DEFAULT_GUARD,h,p,"DN("#e")")
00231 #define DNGHP(e,g,h,p) \
00232   _DIGHPS((!(e)),g,h,p,"DN("#e")")
00233 
00234 #else /* defined(WITHOUT_NANA) */
00235 
00236 #define DI(e) /* empty */
00237 #define DIG(e,g) /* empty */
00238 #define DIH(e,h) /* empty */
00239 #define DIP(e,p) /* empty */
00240 #define DIGH(e,g,h) /* empty */
00241 #define DIGP(e,g,p) /* empty */
00242 #define DIHP(e,h,p) /* empty */
00243 #define DIGHP(e,g,h,p) /* empty */
00244 
00245 #define DN(e) /* empty */
00246 #define DNG(e,g) /* empty */
00247 #define DNH(e,h) /* empty */
00248 #define DNP(e,p) /* empty */
00249 #define DNGH(e,g,h) /* empty */
00250 #define DNGP(e,g,p) /* empty */
00251 #define DNHP(e,h,p) /* empty */
00252 #define DNGHP(e,g,h,p) /* empty */
00253 
00254 #define DS(e) /* empty */
00255 #define DSG(e,g) /* empty */
00256 
00257 
00258 #endif /* !defined(WITHOUT_NANA) */
00259 #ifdef __cplusplus
00260 }
00261 #endif
00262 
00263 #endif /* _DI_h_ */
00264 
00265 
00266 

Generated on Sun Aug 21 04:18:13 2005 for Udanax-Green by doxygen1.3.4