home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_06 / 2n06021a < prev    next >
Text File  |  1991-04-30  |  6KB  |  160 lines

  1. /* ----------------------------------------------------------
  2.  *  macros and declarations for assert-style debugging system
  3.  *
  4.  *  Source code Copyright (c) 1991 T.W. Nelson.
  5.  *  May be used only for non-commercial purposes with
  6.  *  appropriate acknowledgement.
  7.  * ------------------------------------------------------- */
  8.  
  9. #ifndef SNOOPER_H
  10. #define SNOOPER_H
  11.  
  12. /* pre-defined debugger device handles ... */
  13. #define __SCRN__    0           /* debugger screen */
  14. #define __CON__     1           /* console device */
  15. #define __FIL0__    2           /* disk files on cwd */
  16. #define __FIL1__    3
  17. #define __FIL2__    4
  18. #define __FIL3__    5
  19. #define __LPT1__    6           /* parallel devices */
  20. #define __LPT2__    7
  21. #define __LPT3__    8
  22. #define __COM1__    9           /* serial devices */
  23. #define __COM2__    10
  24. #define MAX_DEVICES 11
  25.  
  26. #define MAX_AREA_MARKERS    15
  27.  
  28. /* Debugger cmd line arguments ........
  29.  * Cmd <+help> always prints out explanation strings.
  30.  * Last member of argument table must be { 0, 0, 0 };
  31.  */
  32. typedef struct debug_arguments {
  33.     char *arg;          /* -> cmd-line argument string */
  34.     int *var;           /* -> boolean variable to set */
  35.     char *help;         /* -> explanation string */
  36.     }DEBUG_ARGS;
  37.  
  38. /* prototypes ....... */
  39. void  d_break(int dh, const char *file, int linenum);
  40. void  d_exec(void);
  41. void  d_printf( const char *format, ... );
  42. int  d_putc( int ch );
  43. void  d_echo(int mode);
  44. void  *d_malloc(const char *file, int lineno, size_t nbytes);
  45. void  *d_calloc(const char *file, int lineno,
  46.                                 size_t nitems, size_t size);
  47. void  *d_realloc(const char *file, int lineno,
  48.                            void *oldp, size_t nbytes );
  49. void  d_free(const char *file, int lineno, void *pheap );
  50. void  d_getargs( int *argc, char **argv, DEBUG_ARGS *argtab);
  51. void d_begmark(int id);
  52. void d_endmark( int id );
  53. void d_profile(void);
  54.  
  55. /* Unconditional breakpoint. Executes user-supplied function
  56.  * <fn>, writing to device handle <dh> ...*/
  57. #ifndef NDEBUG
  58. #define __BREAK(dh,fn)  { d_break(dh,__FILE__,__LINE__); \
  59.                                             fn; d_exec(); }
  60. #else
  61. #define __BREAK(dh,fn)  ((void) 0)
  62. #endif
  63.  
  64. /* Conditional watch macro. Activates debugger when <expr>
  65.  * evaluates true (non-0).... */
  66. #ifndef NDEBUG
  67. #define __WATCH(expr,dh,fn)     if(expr) __BREAK(dh,fn)
  68. #else
  69. #define __WATCH(expr,dh,fn)    ((void) 0)
  70. #endif
  71.  
  72. /* Macro to implement a pass-count activated break point.
  73.  * Break occurs on the nth pass thru the macro. Resets
  74.  * counter when debugger is activated .... */
  75. #ifndef NDEBUG
  76. #define __PBREAK(n,dh,fn)  { static int counter = n; \
  77.              __WATCH( --counter == 0, dh,(fn,counter = n)) }
  78. #else
  79. #define __PBREAK(n,dh,fn)   ((void)0)
  80. #endif
  81.  
  82. /* Macro that associates a pass count with a watch condition.
  83.  * Break occurs on the nth time the expression evaluates true
  84.  * (non-0) ....*/
  85. #ifndef NDEBUG
  86. #define __PWATCH(expr,n,dh,fn)  if(expr) __PBREAK(n,dh,fn)
  87. #else
  88. #define __PWATCH(expr,n,dh,fn)  ((void)0)
  89. #endif
  90.  
  91. /* Assert-style, truth-or-consequences macro that writes to
  92.  * debugger screen only. Activates debugger when <expr>
  93.  * evaluates false (0) ...*/
  94. #ifndef NDEBUG
  95. #define istrue(expr)    __WATCH(!(expr),__SCRN__,\
  96.                  d_printf("Assertion <%s> failed\n",#expr))
  97. #else
  98. #define istrue(expr) ((void) 0)
  99. #endif
  100.  
  101. /* Enable extended error checking for malloc() et al.
  102.  * #defining NDEBUG or NO_EXT_MALLOC disables these macros.
  103.  * NOTE: If enabled, "snooper.h" must be placed AFTER any
  104.  * #include files that declare prototypes for these functions.
  105.  */
  106. #ifndef NDEBUG
  107. #ifndef NO_EXT_MALLOC
  108. #define malloc(size)    d_malloc(__FILE__,__LINE__,size)
  109. #define calloc(n,size)  d_calloc(__FILE__,__LINE__,n,size)
  110. #define realloc(p,size) d_realloc(__FILE__,__LINE__,p,size)
  111. #define free(p)         d_free(__FILE__,__LINE__,p)
  112. #endif
  113. #endif
  114.  
  115. /* Extract debugger command-line arguments. Macro
  116.  * __GETARGS assumes placement within body of
  117.  * main(argc,**argv).  Macro __EXTERN_DECL allows conditional
  118.  * extern reference to global (int) variables defined in
  119.  * argtab[] .... */
  120. #ifndef NDEBUG
  121. #define __GETARGS(argtab)     d_getargs(&argc,argv,argtab)
  122. #define __EXTERN_DECL(var)    extern int var
  123. #else
  124. #define __GETARGS(argtab)     ((void) 0)
  125. #define __EXTERN_DECL(var)    ((void) 0)
  126. #endif
  127.  
  128. /* Execution profiling macros. __START() and __STOP()
  129.  * define area markers for a section of code identified
  130.  * by 'id'.  Macros with '__W_' prefix allow conditional
  131.  * execution of base macro.... */
  132. #if !defined(NDEBUG) && !defined(NPROFILE)
  133. #define __START(id)         d_begmark(id)
  134. #define __STOP(id)          d_endmark(id)
  135. #define __PROFILE(dh)       __BREAK(dh,d_profile())
  136. #define __W_START(expr,id)  if(expr) __START(id)
  137. #define __W_STOP(expr,id)   if(expr) __STOP(id)
  138. #define __W_PROFILE(expr,dh) __WATCH(expr,dh,d_profile())
  139. #else
  140. #define __START(id)             ((void) 0)
  141. #define __STOP(id)              ((void) 0)
  142. #define __PROFILE(dh)           ((void) 0)
  143. #define __W_START(expr,id)      ((void) 0)
  144. #define __W_STOP(expr,id)       ((void) 0)
  145. #define __W_PROFILE(expr,dh)    ((void) 0)
  146. #endif
  147.  
  148. /* Macros to control __SCRN__ echo mode... */
  149. #define ECHO_OFF    0       /* Echo mode values */
  150. #define ECHO_ON     1
  151. #ifndef NDEBUG
  152. #define __ECHO(mode)        d_echo(mode)
  153. #define __W_ECHO(expr,mode) if(expr) __ECHO(mode)
  154. #else
  155. #define __ECHO(mode)        ((void) 0)
  156. #define __W_ECHO(expr,mode) ((void) 0)
  157. #endif
  158.  
  159. #endif  /* SNOOPER_H */
  160.