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.

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