Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
dbtrace.h
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 #ifndef _MCW_DEBUGTRACE_
00008 #define _MCW_DEBUGTRACE_
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #ifdef USE_TRACING
00021 
00022 #define DEBUG_MAX_DEPTH 256  
00023 
00024 #define DBG_label DBG_labels[DBG_trace]
00025 
00026 
00027 
00028 #ifdef DONT_USE_MCW_MALLOC
00029 
00030 # define MCHECK 
00031 # define MPROBE 
00032 
00033 #else
00034 
00035 # define MCHECK                                            \
00036    do{ char * mc = mcw_malloc_status(__LINE__,__FILE__) ;  \
00037         if( mc != NULL ) printf("** Memory usage: %s\n",mc) ; } while(0)
00038 
00039 # define MPROBE do{ if( !DBG_trace ) mcw_malloc_status(__FILE__,__LINE__) ; } while(0)
00040 
00041 #endif
00042 
00043 
00044 #ifdef MAIN
00045    char * DBG_rout[DEBUG_MAX_DEPTH] = { "Bottom of Debug Stack" } ;
00046    int DBG_num   = 1 ;
00047    int DBG_trace = 0 ;   
00048 
00049    char * DBG_labels[3] = { "Debug=OFF " , "Debug=LOW " , "Debug=HIGH" } ;
00050 
00051 #include <signal.h>
00052 void DBG_sigfunc(int sig)   
00053 {
00054    char * sname ; int ii ;
00055    switch(sig){
00056       default:      sname = "unknown" ; break ;
00057 
00058       case SIGPIPE: sname = "SIGPIPE" ; break ;
00059       case SIGSEGV: sname = "SIGSEGV" ; break ;
00060       case SIGBUS:  sname = "SIGBUS"  ; break ;
00061       case SIGINT:  sname = "SIGINT"  ; break ;
00062    }
00063    fprintf(stderr,"\nFatal Signal %d (%s) received\n",sig,sname) ;
00064    if( DBG_num >= 0 ){
00065       for( ii=DBG_num-1; ii >= 0 ; ii-- )
00066          fprintf(stderr,"%*.*s%s\n",ii+1,ii+1," ",DBG_rout[ii]) ;
00067    } else {
00068       fprintf(stderr,"[No debug tracing stack: DBG_num=%d]\n",DBG_num) ;
00069    }
00070    fprintf(stderr,"*** Program Abort ***\n") ; fflush(stderr) ;
00071    MPROBE ; exit(1) ;
00072 }
00073 #define DBG_SIGNALS ( signal(SIGPIPE,DBG_sigfunc) , \
00074                       signal(SIGSEGV,DBG_sigfunc) , \
00075                       signal(SIGINT ,DBG_sigfunc) , \
00076                       signal(SIGBUS ,DBG_sigfunc)  )
00077 
00078 #else 
00079    extern char * DBG_rout[DEBUG_MAX_DEPTH] ;
00080    extern int DBG_num ;
00081    extern int DBG_trace ;
00082    extern char * DBG_labels[3] ;
00083 #endif 
00084 
00085 
00086 
00087 
00088 #define DBG_LEADER_IN  "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
00089 #define DBG_LEADER_OUT "-----------------------------------------------------------"
00090 
00091 #define ENTRY(rout) do{ static char * rrr = (rout) ;  DBG_rout[DBG_num++] = rrr ; \
00092                         if( DBG_trace ){                                         \
00093                           printf("%*.*s%s [%d]: ENTRY (file=%s line=%d)\n",     \
00094                                  DBG_num,DBG_num,DBG_LEADER_IN,rrr,DBG_num,    \
00095                                  __FILE__ , __LINE__ ) ;                      \
00096                           MCHECK ; fflush(stdout) ; } } while(0)
00097 
00098 #define DBROUT      DBG_rout[DBG_num-1]
00099 
00100 #define DBEXIT      do{ if( DBG_trace ){                                      \
00101                           printf("%*.*s%s [%d]: EXIT (file=%s line=%d)\n",     \
00102                                  DBG_num,DBG_num,DBG_LEADER_OUT,DBROUT,DBG_num, \
00103                                  __FILE__ , __LINE__ );                        \
00104                           MCHECK ; fflush(stdout) ; }                         \
00105                         DBG_num = (DBG_num>1) ? DBG_num-1 : 1 ; } while(0)
00106 
00107 #define PRINT_TRACING (DBG_trace > 1)
00108 
00109 #define STATUS(str)                                               \
00110   do{ if(PRINT_TRACING){                                           \
00111         printf("%*.*s%s -- %s\n",DBG_num,DBG_num," ",DBROUT,(str)); \
00112         fflush(stdout) ; } } while(0)
00113 
00114 
00115 #else 
00116 
00117 #  define ENTRY(rout)   
00118 #  define DBEXIT        
00119 #  define DBROUT        
00120 #  define STATUS(str)   
00121 #  define DBG_SIGNALS   
00122 #  define MCHECK        
00123 #  define MPROBE        
00124 #  define PRINT_TRACING 0
00125 #  define DBG_trace     0          
00126 
00127 #endif 
00128 
00129 
00130 #define RETURN(val) do{ DBEXIT ; return (val) ; } while(0)
00131 #define EXRETURN    do{ DBEXIT ; return ; } while(0)
00132 
00133 
00134 
00135 #ifndef MCHECK
00136 # define MCHECK 
00137 # define MPROBE 
00138 #endif
00139 
00140 
00141 #endif