Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
6.7 KiB

  1. /****************************************************************************
  2. Copyright (c) Microsoft Corporation 1997
  3. All rights reserved
  4. File: DEBUG.H
  5. Debugging utilities header
  6. ***************************************************************************/
  7. #ifndef _DEBUG_H_
  8. #define _DEBUG_H_
  9. #if DBG==1
  10. // Globals
  11. extern DWORD g_TraceMemoryIndex;
  12. extern DWORD g_dwCounter;
  13. extern DWORD g_dwTraceFlags;
  14. extern const TCHAR g_szTrue[];
  15. extern const TCHAR g_szFalse[];
  16. // Trace Flags
  17. enum {
  18. TF_ALWAYS = 0xFFFFFFFF,
  19. TF_NEVER = 0x00000000,
  20. TF_QUERYINTERFACE = 0x00000001,
  21. TF_FUNC = 0x00000002,
  22. TF_CALLS = 0x00000004,
  23. TF_MEMORYALLOCS = 0x00000008
  24. };
  25. // Macros
  26. #define DEFINE_MODULE( _module ) static const TCHAR g_szModule[] = TEXT(_module);
  27. #define __MODULE__ g_szModule
  28. // #define DEBUG_BREAK do { _try { _asm int 3 } _except (EXCEPTION_EXECUTE_HANDLER) {;} } while (0)
  29. #define DEBUG_BREAK { _asm int 3 }
  30. #define INITIALIZE_TRACE_MEMORY g_TraceMemoryIndex = TlsAlloc( ); TlsSetValue( g_TraceMemoryIndex, NULL)
  31. #define UNINITIALIZE_TRACE_MEMORY DebugMemoryCheck( )
  32. #ifdef Assert
  33. #undef Assert
  34. #endif
  35. #define Assert( _fn ) \
  36. if ( !(_fn) && AssertMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), !!(_fn) ) ) DEBUG_BREAK
  37. #ifdef AssertMsg
  38. #undef AssertMsg
  39. #endif
  40. #define AssertMsg( _fn, _msg ) \
  41. if ( !(_fn) && AssertMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT(_msg), !!(_fn) ) ) DEBUG_BREAK
  42. #define TraceAlloc( _flags, _size ) DebugAlloc( TEXT(__FILE__), __LINE__, g_szModule, _flags, _size, TEXT(#_size) )
  43. #define TraceFree( _hmem ) DebugFree( _hmem )
  44. // Tracing
  45. #define TraceFunc( _msg ) \
  46. g_dwCounter++; \
  47. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT(_msg) ); \
  48. g_dwCounter--; \
  49. #define TraceClsFunc( _msg ) \
  50. g_dwCounter++; \
  51. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("%s::%s"), TEXT(SZTHISCLASS), TEXT(_msg) );\
  52. g_dwCounter--; \
  53. #define TraceDo( _fn ) {\
  54. g_dwCounter++; \
  55. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_CALLS, TEXT("+ %s\n"), TEXT(#_fn) ); \
  56. _fn; \
  57. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_CALLS, TEXT("V\n") ); \
  58. g_dwCounter--; \
  59. }
  60. #define TraceMsgDo( _fn, _msg ) {\
  61. g_dwCounter++; \
  62. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_CALLS, TEXT("+ %s\n"), TEXT(#_fn) ); \
  63. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_CALLS, TEXT(_msg), _fn ); \
  64. g_dwCounter--; \
  65. }
  66. #define DebugDo( _fn ) {\
  67. g_dwCounter++; \
  68. DebugMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT("+ %s\n"), TEXT(#_fn) ); \
  69. _fn; \
  70. DebugMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT("V\n") ); \
  71. g_dwCounter--; \
  72. }
  73. #define DebugMsgDo( _fn, _msg ) {\
  74. g_dwCounter++; \
  75. DebugMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT("+ %s\n"), TEXT(#_fn) ); \
  76. DebugMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT(_msg), _fn); \
  77. g_dwCounter--; \
  78. }
  79. // HRESULT testing
  80. #define THR( _fn ) \
  81. TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), _fn )
  82. #define RRETURN( _fn ) \
  83. return TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), _fn )
  84. #define QIRETURN( _fn ) \
  85. if ( !!( TF_QUERYINTERFACE & g_dwTraceFlags ) )\
  86. return TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), _fn ); \
  87. else if ( hr ) \
  88. DebugMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT("QueryInterface() failed()") ); \
  89. return _fn
  90. #define RRETURN1( _fn, _ok ) {\
  91. TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), \
  92. ( ( _fn == _ok ) ? S_OK : _fn ) ); \
  93. return _fn;\
  94. }
  95. // Thread-safe inc/decrements
  96. #define InterlockDecrement( _var ) {\
  97. --_var;\
  98. g_dwCounter++; \
  99. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_QUERYINTERFACE, TEXT("Decremented %s = %u\n"), TEXT(#_var), _var );\
  100. g_dwCounter--; \
  101. }
  102. #define InterlockIncrement( _var ) {\
  103. ++_var;\
  104. g_dwCounter++; \
  105. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_QUERYINTERFACE, TEXT("Incremented %s = %u\n"), TEXT(#_var), _var );\
  106. g_dwCounter--; \
  107. }
  108. // Other
  109. #define BOOLTOSTRING( _fBool ) ( !!(_fBool) ? g_szTrue : g_szFalse )
  110. // Functions
  111. void
  112. TraceMsg(
  113. DWORD dwCheckFlags,
  114. LPCTSTR pszFormat,
  115. ... );
  116. void
  117. DebugMsg(
  118. LPCTSTR pszFormat,
  119. ... );
  120. void
  121. TraceMessage(
  122. LPCTSTR pszFile,
  123. UINT uLine,
  124. LPCTSTR pszModule,
  125. DWORD dwCheckFlags,
  126. LPCTSTR pszFormat,
  127. ... );
  128. void
  129. DebugMessage(
  130. LPCTSTR pszFile,
  131. UINT uLine,
  132. LPCTSTR pszModule,
  133. LPCTSTR pszFormat,
  134. ... );
  135. BOOL
  136. AssertMessage(
  137. LPCTSTR pszFile,
  138. UINT uLine,
  139. LPCTSTR pszModule,
  140. LPCTSTR pszfn,
  141. BOOL fTrue );
  142. HRESULT
  143. TraceHR(
  144. LPCTSTR pszFile,
  145. UINT uLine,
  146. LPCTSTR pszModule,
  147. LPCTSTR pszfn,
  148. HRESULT hr );
  149. // Memory Functions
  150. HGLOBAL
  151. DebugAlloc(
  152. LPCTSTR pszFile,
  153. UINT uLine,
  154. LPCTSTR pszModule,
  155. UINT uFlags,
  156. DWORD dwBytes,
  157. LPCTSTR pszComment );
  158. HGLOBAL
  159. DebugFree(
  160. HGLOBAL hMem );
  161. HGLOBAL
  162. DebugMemoryAdd(
  163. HGLOBAL hglobal,
  164. LPCTSTR pszFile,
  165. UINT uLine,
  166. LPCTSTR pszModule,
  167. UINT uFlags,
  168. DWORD dwBytes,
  169. LPCTSTR pszComment );
  170. #define DebugMemoryAddHandle( _handle ) \
  171. DebugMemoryAdd( _handle, TEXT(__FILE__), __LINE__, __MODULE__, GMEM_MOVEABLE, 0, TEXT("_handle") );
  172. #define DebugMemoryAddAddress( _pv ) \
  173. DebugMemoryAdd( _pv, TEXT(__FILE__), __LINE__, __MODULE__, GMEM_FIXED, 0, TEXT("_pv") );
  174. void
  175. DebugMemoryDelete(
  176. HGLOBAL hglobal );
  177. void
  178. DebugMemoryCheck( );
  179. //
  180. //
  181. #else // it's RETAIL ******************************************************
  182. //
  183. //
  184. // Debugging -> NOPs
  185. #define Assert( _fn )
  186. #define DebugDo( _fn )
  187. #define DebugMsgDo( _fn, _msg )
  188. #define DEFINE_MODULE( _module )
  189. #define AssertMsg (void)
  190. #define TraceMsg (void)
  191. #define DebugMsg (void)
  192. #define TraceMessage (void)
  193. #define DebugMessage (void)
  194. #define AssertMessage (void)
  195. #define TraceHR (void)
  196. #define DebugMemoryAddHandle( _handle )
  197. #define DebugMemoryAddAddress( _pv )
  198. // Tracing -> just do operation
  199. #define TraceDo( _flag, _fn ) _fn
  200. #define TraceMsgDo( _flag, _fn, _msg ) _fn
  201. // HRESULT testing -> do retail
  202. #define THR
  203. #define RRETURN( _fn ) return _fn
  204. #define RRETURN( _fn, _ok ); return _fn
  205. // Thread-safe inc/decrements -> do retail
  206. #define InterlockDecrement( _var ) --_var
  207. #define InterlockIncrement( _var ) ++_var
  208. // Memory Functions -> do retail
  209. #define TraceAlloc( _flags, _size ) GlobalAlloc( _flags, _size )
  210. #define TraceFree( _pv ) GlobalFree( _pv )
  211. #endif // DBG==1
  212. #endif // _DEBUG_H_