Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

debug.h

Go to the documentation of this file.
00001 
00010 #ifndef DBG_H
00011 #define DBG_H
00012 
00013 /* Make sure at least one of DBG_KERNELMODE or DBG_USERMODE is defined */
00014 #if !defined(DBG_KERNELMODE) && !defined(DBG_USERMODE)
00015    #error DBG_KERNELMODE or DBG_USERMODE must be specified!
00016 #endif
00017 
00018 /* Make sure DBG_KERNELMODE and DBG_USERMODE are not defined simultaneously */
00019 #if defined(DBG_KERNELMODE) && defined(DBG_USERMODE)
00020    #error Only one of DBG_KERNELMODE and DBG_USERMODE must be specified!
00021 #endif
00022 
00023 #ifndef DBG
00024  #ifdef _DEBUG
00025   #define DBG 1
00026   #define __FUNCTION__ ""
00027  #endif
00028 #endif
00029 
00030 #if DBG
00031 
00032        /* as dumping memory is used regularly when debugging,
00033           we give a debug helper function for this */
00034 
00035        extern void dbg_memdump(const char *Where,
00036            const unsigned char *InputBuffer,
00037            const unsigned int Count);
00038 
00039        #define DBG_MEMDUMP(_Where, _Buffer, _Count) dbg_memdump(_Where, _Buffer, _Count)
00040 
00041        #define DBG_MAX_BUFFERLEN 4096
00042 
00043 #ifdef DBG_KERNELMODE
00044 
00045        // the maximum number of buffers to be used for debugging
00046        #define DBG_MAX_BUFFER 32 /* This *MUST* be a power of 2! */
00047 
00048 #else
00049 
00050        #define DBG_MAX_BUFFER 1
00051 
00052 #endif // #ifdef DBG_KERNELMODE
00053 
00054        /* define the various flags for debugging */
00055 
00057        #define DBGF_BREAK   0x80000000
00058 
00060        #define DBGF_ENTER   0x40000000
00061 
00063        #define DBGF_LEAVE   0x20000000
00064 
00067        #define DBGF_LEAVE_FAILURE 0x10000000
00068 
00070        #define DBGF_PARAM   0x08000000
00071 
00072 #ifdef DBG_KERNELMODE
00073 
00075        #define DBGF_IEC     0x04000000
00076 
00078        #define DBGF_IRQ     0x02000000
00079 
00081        #define DBGF_ASSERTIRQL  0x01000000
00082 
00084        #define DBGF_PORT    0x00800000
00085 
00087        #define DBGF_THREAD  0x00400000
00088 
00090        #define DBGF_IRPPATH 0x00200000
00091 
00093        #define DBGF_IRP     0x00100000
00094 
00096        #define DBGF_DPC     0x00080000
00097 
00098 
00100        #define DBGF_DBGMEMBUF 0x00020000
00101 
00103        #define DBGF_DBGPRINT 0x00010000
00104 
00105 #endif // #ifdef DBG_KERNELMODE
00106 
00108        #define DBGF_PPORT   0x0010
00109 
00111        #define DBGF_SUCCESS 0x0008
00112 
00114        #define DBGF_WARNING 0x0004
00115 
00117        #define DBGF_ERROR   0x0002
00118 
00120        #define DBGF_ASSERT  0x0001
00121 
00122 #ifdef DBG_DLL
00123 /* Make the linker happy if the DLL is to be generated */
00124 int __cdecl main(int argc, char *argv[])
00125 {
00126 }
00127 #endif
00128 
00129 #ifdef DBG_KERNELMODE
00130 
00132        extern const UCHAR *DebugNtStatus(NTSTATUS Value);
00133 
00134 #else
00135        #define DbgBreakPoint DebugBreak
00136 #endif // #ifdef DBG_KERNELMODE
00137 
00138 
00139        extern unsigned char DbgBuffer[DBG_MAX_BUFFER][DBG_MAX_BUFFERLEN];
00140        extern          int  DbgBufferPos[];
00141        extern unsigned long DbgFlags;
00142 
00143        extern void DbgOutputIntoBuffer(unsigned long BufferNumber, const char * const Format, ...);
00144 
00145 
00146        /* Some makros for handling the various debug conditions */
00147 
00148        #define ISDBG_BREAK()          (DbgFlags & DBGF_BREAK)
00149        #define ISDBG_ENTER()          (DbgFlags & DBGF_ENTER)
00150        #define ISDBG_LEAVE()          (DbgFlags & DBGF_LEAVE)
00151        #define ISDBG_LEAVE_FAILURE()  (DbgFlags & (DBGF_LEAVE|DBGF_LEAVE_FAILURE))
00152        #define ISDBG_PARAM()          (DbgFlags & DBGF_PARAM)
00153 #ifdef DBG_KERNELMODE
00154        #define ISDBG_IEC()            (DbgFlags & DBGF_IEC)
00155        #define ISDBG_IRQ()            (DbgFlags & DBGF_IRQ)
00156        #define ISDBG_ASSERTIRQL()     (DbgFlags & DBGF_ASSERTIRQL)
00157        #define ISDBG_PORT()           (DbgFlags & DBGF_PORT)
00158        #define ISDBG_THREAD()         (DbgFlags & DBGF_THREAD)
00159        #define ISDBG_IRPPATH()        (DbgFlags & DBGF_IRPPATH)
00160        #define ISDBG_IRP()            (DbgFlags & DBGF_IRP)
00161        #define ISDBG_DPC()            (DbgFlags & DBGF_DPC)
00162        #define ISDBG_DBGMEMBUF()      (DbgFlags & DBGF_DBGMEMBUF)
00163        #define ISDBG_DBGPRINT()       (DbgFlags & DBGF_DBGPRINT)
00164 #endif // #ifdef DBG_KERNELMODE
00165        #define ISDBG_PPORT()          (DbgFlags & DBGF_PPORT)
00166        #define ISDBG_SUCCESS()        (DbgFlags & DBGF_SUCCESS)
00167        #define ISDBG_WARN()           (DbgFlags & DBGF_WARNING)
00168        #define ISDBG_ERROR()          (DbgFlags & DBGF_ERROR)
00169        #define ISDBG_ASSERT()         (DbgFlags & DBGF_ASSERT)
00170        #define ISDBG_PANIC()          (1)
00171 
00172 /* Now, abstract from some differences between user-mode and kernel-mode */
00173 
00174 #ifdef DBG_KERNELMODE
00175 
00176               extern VOID DbgInit(VOID);
00177               extern VOID DbgAllocateMemoryBuffer(VOID);
00178               extern VOID DbgFreeMemoryBuffer(VOID);
00179               extern VOID DbgOutputMemoryBuffer(const char *String);
00180 
00182               #define _DBG_PERFORM(_xxx) \
00183                     if (ISDBG_DBGMEMBUF()) \
00184                     { \
00185                         DbgOutputMemoryBuffer(_xxx); \
00186                     } \
00187                     if (ISDBG_DBGPRINT()) \
00188                     { \
00189                         DbgPrint("%s", _xxx); \
00190                     }
00191 
00193               #define FUNC_DEF           ULONG DebugBufferNo = 0;
00194 
00197 #define _DBG_START_ADD  { ULONG BufferUsed; \
00198        do { \
00199            DebugBufferNo = InterlockedIncrement(&DebugBufferCounter) & (DBG_MAX_BUFFER-1); \
00200            BufferUsed = InterlockedExchange(&DebugBufferUsed[DebugBufferNo], 1); \
00201        } while (BufferUsed);  }
00202 
00205 #define _DBG_END_ADD InterlockedExchange(&DebugBufferUsed[DebugBufferNo], 0);
00206 
00207               extern LONG DebugBufferCounter;
00208               extern LONG DebugBufferUsed[];
00209 
00212               #define DEBUG_BUFFER_NO DebugBufferNo
00213 
00214 #else // #ifdef DBG_KERNELMODE
00215 
00216               #include <windows.h>
00217 
00219               #define _DBG_PERFORM(_xxx) OutputDebugString(_xxx);
00220 
00222               #define FUNC_DEF
00223 
00226               #define _DBG_START_ADD
00227 
00230               #define _DBG_END_ADD
00231 
00234               #define DEBUG_BUFFER_NO    0
00235 
00236 #endif // #ifdef DBG_KERNELMODE
00237 
00240        #define DBG_PREFIX         DEBUG_BUFFER_NO,
00241 
00243        #define _DBGO(_xxx) DbgOutputIntoBuffer _xxx
00244 
00245 #ifdef DBG_KERNELMODE
00246 
00247        #define _DBG_START() _DBG_START_ADD DbgBufferPos[DEBUG_BUFFER_NO] = 0; DbgOutputIntoBuffer(DBG_PREFIX "%s(%u,%02x)," __FUNCTION__ "(%u): ", DBG_PROGNAME, CbmGetCurrentProcessorNumber(), DebugBufferNo, __LINE__)
00248 #else // #if DBG_KERNELMODE
00249        #define _DBG_START() _DBG_START_ADD DbgBufferPos[DEBUG_BUFFER_NO] = 0; DbgOutputIntoBuffer(DBG_PREFIX "%s," __FUNCTION__ "(%u): ", DBG_PROGNAME, __LINE__)
00250 #endif // #if DBG_KERNELMODE
00251        #define _DBG_END()   DbgOutputIntoBuffer(DBG_PREFIX "\n"); _DBG_PERFORM(&DbgBuffer[DEBUG_BUFFER_NO][0]) _DBG_END_ADD 
00252 
00253        #define DBGO(_xxx) { _DBG_START(); _DBGO(_xxx); _DBG_END() }
00254 
00256        #define DBG_BREAKPOINT() { if (ISDBG_BREAK()) { DbgBreakPoint(); }; }
00257 
00259        #define FUNC_ENTER()             FUNC_DEF { if (ISDBG_ENTER()) { DBGO(( DBG_PREFIX "Entering %s", __FUNCTION__ )); } }
00260 
00261        #define FUNC_LEAVE()             { if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s", __FUNCTION__ )); } return; }
00262 
00263        #define FUNC_PARAM( _xxx )       { if (ISDBG_PARAM()) { _DBG_START(); _DBGO(( DBG_PREFIX "Parameter for %s: ", __FUNCTION__ )); _DBGO( _xxx ); _DBG_END() } }
00264 
00266        #define FUNC_LEAVE_BOOL(   _xxx ) { const BOOL    _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with BOOL=%s",   __FUNCTION__, (_OUT_)?"TRUE":"FALSE" )); } return _OUT_; }
00267 
00268        #define FUNC_LEAVE_BOOLEAN(_xxx ) { const BOOLEAN _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with BOOL=%s",   __FUNCTION__, (_OUT_)?"TRUE":"FALSE" )); } return _OUT_; }
00269 
00270        #define FUNC_LEAVE_INT(    _xxx ) { const int     _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with INT=%u (%d)",    __FUNCTION__, (unsigned int)(_OUT_), (signed int)(_OUT_) )); } return _OUT_; }
00271 
00272        #define FUNC_LEAVE_USHORT( _xxx ) { const USHORT  _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with INT=%u (%d)",    __FUNCTION__, (unsigned int)(_OUT_), (signed int)(_OUT_) )); } return _OUT_; }
00273 
00274        #define FUNC_LEAVE_UCHAR(  _xxx ) { const UCHAR   _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with UCHAR=%u",  __FUNCTION__, (unsigned int)(_OUT_) )); }  return _OUT_; }
00275 
00276        #define FUNC_LEAVE_HANDLE( _xxx ) { const HANDLE  _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with HANDLE=%u", __FUNCTION__, (unsigned int)(_OUT_) )); }  return _OUT_; }
00277 
00278        #define FUNC_LEAVE_STRING( _xxx ) { const char *  _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with '%s'",      __FUNCTION__,           (_OUT_) )); }      return _xxx;  }
00279 
00280        #define FUNC_LEAVE_ULONG(  _xxx ) { const ULONG   _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with ULONG=%ul", __FUNCTION__, (ULONG)   (_OUT_) )); }      return _OUT_; }
00281 
00282        #define FUNC_LEAVE_TYPE(  _xxx, _TYPE, _FORMAT) { _TYPE _OUT_ = _xxx; if (ISDBG_LEAVE()) { DBGO(( DBG_PREFIX "Leaving  %s with " #_TYPE "=" _FORMAT, __FUNCTION__, (_OUT_) )); }      return _OUT_; }
00283        #define FUNC_LEAVE_PTR(  _xxx, _TYPE )  FUNC_LEAVE_TYPE( _xxx, _TYPE, "0x%p")
00284 
00285        #ifdef DBG_KERNELMODE
00286 
00287               #define FUNC_LEAVE_NTSTATUS_CONST( _xxx ) { if (ISDBG_LEAVE() || (ISDBG_LEAVE_FAILURE() && (_xxx != STATUS_SUCCESS))) { DBGO(( DBG_PREFIX "Leaving  %s with NTSTATUS=%s", __FUNCTION__, #_xxx )); } return _xxx; }
00288 
00289               #define FUNC_LEAVE_NTSTATUS(       _xxx ) { NTSTATUS _OUT_ = _xxx; if (ISDBG_LEAVE() || (ISDBG_LEAVE_FAILURE() && (_xxx != STATUS_SUCCESS))) { DBGO(( DBG_PREFIX "Leaving  %s with NTSTATUS=%s", __FUNCTION__, DebugNtStatus(_OUT_) )); } return _OUT_; }
00290 
00291               #define DBG_IRQL(  _xxx ) { if (!(KeGetCurrentIrql() _xxx)) if (ISDBG_ASSERTIRQL()) { DBGO(( DBG_PREFIX "***IRQL ASSERTION FAILED!***: '%s' in %s:%s(%u)", #_xxx, __FILE__, __FUNCTION__, __LINE__ )); DBG_BREAKPOINT(); } }
00292        #endif
00293 
00295        #define DBG_PPORT(   _xxx ) { if (ISDBG_PPORT()){  DBGO(  _xxx  ); } }
00296 
00297        #define DBG_SUCCESS( _xxx ) { if (ISDBG_SUCCESS()) { _DBG_START(); _DBGO( _xxx ); _DBGO((DBG_PREFIX " SUCCESS")); _DBG_END(); } }
00298 
00299        #define DBG_WARN(    _xxx ) { if (ISDBG_WARN())    { _DBG_START(); _DBGO(( DBG_PREFIX "Warning: ")); _DBGO( _xxx ); _DBG_END(); } }
00300 
00301        #define DBG_ERROR(   _xxx ) { if (ISDBG_ERROR())   { _DBG_START(); _DBGO(( DBG_PREFIX "***ERROR***: ")); _DBGO( _xxx ); _DBG_END(); } }
00302 
00303        #define DBG_PANIC(   _xxx ) { if (ISDBG_PANIC())   { _DBG_START(); _DBGO(( DBG_PREFIX "***PANIC***: ")); _DBGO( _xxx ); _DBG_END(); } }
00304 
00305        #define DBG_ASSERT(  _xxx ) { if (!(_xxx)) if (ISDBG_ASSERT()) { DBGO(( DBG_PREFIX "***ASSERTION FAILED!***: %s in %s:%s(%u)", #_xxx, __FILE__, __FUNCTION__, __LINE__ )); DBG_BREAKPOINT(); } }
00306 
00307        #define DBG_PRINT(   _xxx ) { DBGO( _xxx ); }
00308 
00309        #define DBG_VERIFY(  _xxx ) DBG_ASSERT( _xxx )
00310 
00312        #define DBGDO( _xxx ) _xxx
00313 
00314        #ifdef DBG_KERNELMODE
00315 
00317               #define DBG_INIT() DbgInit()
00318 
00319 
00321               #define DBG_IEC(     _xxx ) { if (ISDBG_IEC())  {  DBGO( _xxx ); } }
00322 
00323               #define DBG_IRQ(     _xxx ) { if (ISDBG_IRQ())  {  DBGO(( DBG_PREFIX _xxx )); } }
00324 
00325               #define DBG_PORT(    _xxx ) { if (ISDBG_PORT()) {  _DBG_START(); _DBGO((DBG_PREFIX "Port Command: ")); _DBGO(_xxx); _DBG_END(); } }
00326 
00327               #define DBG_THREAD(  _xxx ) /* { if (ISDBG_THREAD())   DBGO(( _xxx )); } */
00328 
00329               #define DBG_IRPPATH( _xxx ) { if (ISDBG_IRPPATH()) { DBGO(_xxx); } }
00330 
00331               #define DBG_IRP(     _xxx ) { if (ISDBG_IRP()) { DBGO(( DBG_PREFIX "Got IRP: " #_xxx )); } }
00332 
00333               #define DBG_DPC(     _xxx ) { if (ISDBG_DPC()) { DBGO( _xxx ); } }
00334        #endif
00335 
00336 #else  // #if DBG
00337 
00338        #define DBG_MEMDUMP(_Where, _Buffer, _Count)
00339 
00341        #define DBG_BREAKPOINT
00342 
00344        #define FUNC_ENTER()
00345 
00347        #define FUNC_LEAVE(       ) return
00348 
00350        #define FUNC_PARAM(  _xxx )
00351 
00353        #define FUNC_LEAVE_BOOL(   _xxx ) return _xxx
00354 
00356        #define FUNC_LEAVE_BOOLEAN(   _xxx ) return _xxx
00357 
00359        #define FUNC_LEAVE_INT(    _xxx ) return _xxx
00360 
00362        #define FUNC_LEAVE_USHORT(  _xxx ) return _xxx
00363 
00365        #define FUNC_LEAVE_UCHAR(  _xxx ) return _xxx
00366 
00368        #define FUNC_LEAVE_HANDLE( _xxx ) return _xxx
00369 
00371        #define FUNC_LEAVE_STRING( _xxx ) return _xxx
00372 
00374        #define FUNC_LEAVE_ULONG(  _xxx ) return _xxx
00375 
00377        #define FUNC_LEAVE_TYPE(  _xxx, _TYPE, _FORMAT) return _xxx
00378 
00380        #define FUNC_LEAVE_PTR(    _xxx, _yyy ) return _xxx
00381 
00382        #ifdef DBG_KERNELMODE
00383 
00384               #define FUNC_LEAVE_NTSTATUS_CONST( _xxx )  return _xxx
00385 
00387               #define FUNC_LEAVE_NTSTATUS(       _xxx )  return _xxx
00388        #endif
00389 
00391        #define DBG_PREFIX
00392 
00394        #define DBG_PPORT(   _xxx )
00395 
00397        #define DBG_SUCCESS( _xxx )
00398 
00400        #define DBG_WARN(    _xxx )
00401 
00403        #define DBG_ERROR(   _xxx )
00404 
00406        #define DBG_PANIC(   _xxx )
00407 
00409        #define DBG_ASSERT(  _xxx )
00410 
00412        #define DBG_PRINT(   _xxx )
00413 
00415        #define DBGDO(       _xxx )
00416 
00418        #define DBG_VERIFY(  _xxx ) _xxx
00419 
00420        #ifdef DBG_KERNELMODE
00421 
00423               #define DBG_INIT()
00424 
00426               #define DBG_IEC(     _xxx )
00427 
00429               #define DBG_IRQ(     _xxx )
00430 
00432               #define DBG_IRQL(    _xxx )
00433 
00435               #define DBG_PORT(    _xxx )
00436 
00438               #define DBG_THREAD(  _xxx )
00439 
00441               #define DBG_IRPPATH( _xxx )
00442 
00444               #define DBG_IRP(     _xxx )
00445 
00447               #define DBG_DPC(     _xxx )
00448 
00449        #endif
00450 
00451 #endif // #if DBG
00452 
00454 #define DBG_IRPPATH_PROCESS( _Where_ )  DBG_IRPPATH((DBG_PREFIX "IrpPath: + Processing IRP %08x in " _Where_, (char*)Irp))
00455 
00457 #define DBG_IRPPATH_COMPLETE( _Where_ ) DBG_IRPPATH((DBG_PREFIX "IrpPath: - Completing IRP %08x in " _Where_ " with ntStatus = %s", (char*)Irp, (char*)DebugNtStatus(ntStatus)))
00458 
00460 #define DBG_IRPPATH_CANCEL( _Where_ )   DBG_IRPPATH((DBG_PREFIX "IrpPath: - CANCELLING IRP %08x in " _Where_, (char*)Irp))
00461 
00463 #define DBG_IRPPATH_EXECUTE( _Where_ )  DBG_IRPPATH((DBG_PREFIX "IrpPath: = Executing IRP %08x in " _Where_, (char*)Irp))
00464 
00465 #endif // #ifndef DBG_H

Generated on Sun Apr 30 18:45:48 2006 for opencbm by  doxygen 1.4.2