Leaked source code of windows server 2003
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.

326 lines
9.9 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. // Trace Flags
  10. #define TF_ALWAYS 0xFFFFFFFF
  11. #define TF_NEVER 0x00000000
  12. #define TF_QUERYINTERFACE 0x00000001 // Query Interface details
  13. #define TF_FUNC 0x00000002 // Functions entrances w/parameters
  14. #define TF_CALLS 0x00000004 // Function calls
  15. #define TF_MEMORYALLOCS 0x00000008 // Memory Allocations
  16. #define TF_DLL 0x00000010 // DLL specific
  17. #define TF_WM 0x00000020 // Window Messages
  18. #define TF_SCP 0x00000030 // SCP objects
  19. #define TF_HRESULTS 0x80000000 // Trace HRESULTs active
  20. #ifdef DEBUG
  21. #pragma message("BUILD: DEBUG macros being built")
  22. // Globals
  23. extern DWORD g_TraceMemoryIndex;
  24. extern DWORD g_dwCounter;
  25. extern DWORD g_dwTraceFlags;
  26. extern const TCHAR g_szTrue[];
  27. extern const TCHAR g_szFalse[];
  28. // Macros
  29. #define DEFINE_MODULE( _module ) static const TCHAR g_szModule[] = TEXT(_module);
  30. #define __MODULE__ g_szModule
  31. #define DEFINE_THISCLASS( _class ) static const TCHAR g_szClass[] = TEXT(_class);
  32. #define __THISCLASS__ g_szClass
  33. #define DEFINE_SUPER( _super ) static const TCHAR g_szSuper[] = TEXT(_super);
  34. #define __SUPER__ g_szSuper
  35. #define DEBUG_BREAK DebugBreak( );
  36. #define INITIALIZE_TRACE_MEMORY_PROCESS \
  37. g_TraceMemoryIndex = TlsAlloc( ); \
  38. TlsSetValue( g_TraceMemoryIndex, NULL); \
  39. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_DLL, TEXT("Thread Memory tracing initialize.\n") )
  40. #define INITIALIZE_TRACE_MEMORY_THREAD \
  41. TlsSetValue( g_TraceMemoryIndex, NULL); \
  42. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_DLL, TEXT("Thread Memory tracing initialize.\n") )
  43. #define UNINITIALIZE_TRACE_MEMORY \
  44. DebugMemoryCheck( ); \
  45. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_DLL, TEXT("Memory tracing terminated.\n") )
  46. #ifdef Assert
  47. #undef Assert
  48. #endif
  49. #define Assert( _fn ) \
  50. if ( !(_fn) && AssertMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), !!(_fn) ) ) DEBUG_BREAK
  51. #ifdef AssertMsg
  52. #undef AssertMsg
  53. #endif
  54. #define AssertMsg( _fn, _msg ) \
  55. if ( !(_fn) && AssertMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT(_msg), !!(_fn) ) ) DEBUG_BREAK
  56. #define TraceAlloc( _flags, _size ) DebugAlloc( TEXT(__FILE__), __LINE__, g_szModule, _flags, _size, TEXT(#_size) )
  57. #define TraceFree( _hmem ) DebugFree( _hmem )
  58. //
  59. // Tracing Macros
  60. //
  61. // All functions that begin with "Trace" are in both DEBUG and RETAIL, but
  62. // in RETAIL they do not spew output.
  63. //
  64. // Displays file, line number, module and "_msg" only if the TF_FUNC is set
  65. // in g_dwTraceFlags.
  66. #define TraceFunc( _msg ) \
  67. InterlockIncrement(g_dwCounter); \
  68. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("+ ") TEXT(_msg) );
  69. // Displays file, line number, module, class name and "_msg" only if the
  70. // TF_FUNC is set in g_dwTraceFlags.
  71. #define TraceClsFunc( _msg ) \
  72. InterlockIncrement(g_dwCounter); \
  73. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("+ %s::%s"), g_szClass, TEXT(_msg) );
  74. // Return macro for TraceFunc() and TraceClsFunc()
  75. #define TraceFuncExit() { \
  76. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("V*\n") ); \
  77. InterlockDecrement(g_dwCounter); \
  78. return; \
  79. }
  80. #define RETURN( _rval ) { \
  81. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("V\n") ); \
  82. InterlockDecrement(g_dwCounter); \
  83. return _rval; \
  84. }
  85. // If the value is not S_OK, it will display it.
  86. #define HRETURN( _hr ) { \
  87. if ( _hr != S_OK ) \
  88. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("V hr = 0x%08x\n"), _hr ); \
  89. else \
  90. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_FUNC, TEXT("V\n") ); \
  91. InterlockDecrement(g_dwCounter); \
  92. return _hr; \
  93. }
  94. // Displays the file, line number, module and function call and return from the
  95. // function call (no return value displayed) for "_fn" only if the TF_CALLS is
  96. // set in g_dwTraceFlags.
  97. #define TraceDo( _fn ) {\
  98. InterlockIncrement(g_dwCounter); \
  99. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_CALLS, TEXT("+ %s\n"), TEXT(#_fn) ); \
  100. _fn; \
  101. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_CALLS, TEXT("V\n") ); \
  102. InterlockDecrement(g_dwCounter); \
  103. }
  104. // This functions only asserts if the result is ZERO.
  105. #define TraceAssertIfZero( _fn ) \
  106. if ( !(_fn) && AssertMessage( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), !!(_fn) ) ) DEBUG_BREAK
  107. #define TraceMsgGUID( _flag, _guid ) \
  108. TraceMsg( _flag, TEXT("{%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x}"), \
  109. _guid.Data1, _guid.Data2, _guid.Data3, \
  110. _guid.Data4[0], _guid.Data4[1], _guid.Data4[2], _guid.Data4[3], \
  111. _guid.Data4[4], _guid.Data4[5], _guid.Data4[6], _guid.Data4[7] )
  112. #define ErrorMsg( _fmt, _arg ) \
  113. TraceMessage( TEXT(__FILE__), __LINE__, g_szModule, TF_ALWAYS, TEXT(_fmt), _arg );
  114. //
  115. // Debug Macros
  116. //
  117. // These calls are only compiled in DEBUG. They are a NOP in RETAIL (not even
  118. // compiled in.
  119. //
  120. //
  121. // HRESULT testing macros
  122. //
  123. // These functions check HRESULT return values and display UI if conditions
  124. // warrant only in DEBUG.
  125. //
  126. // Warning is display if HRESULT is anything but S_OK (0).
  127. #define THR( _fn ) \
  128. TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), _fn )
  129. // Warning is display if HRESULT is anything but S_OK (0).
  130. #define RRETURN( _fn ) { \
  131. RETURN( TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_fn), _fn ) ); \
  132. }
  133. // Warning is display if HRESULT is not S_OK (0) or "_ok".
  134. #define RRETURN1( _hr, _ok ) {\
  135. RETURN(TraceHR( TEXT(__FILE__), __LINE__, g_szModule, TEXT(#_hr), \
  136. ( ( _hr == _ok ) ? S_OK : _hr ) ) ); \
  137. }
  138. //
  139. // Other
  140. //
  141. #define BOOLTOSTRING( _fBool ) ( !!(_fBool) ? g_szTrue : g_szFalse )
  142. //
  143. // Trace/Debug Functions - these do not exist in RETAIL.
  144. //
  145. void
  146. TraceMsg(
  147. DWORD dwCheckFlags,
  148. LPCSTR pszFormat,
  149. ... );
  150. void
  151. TraceMsg(
  152. DWORD dwCheckFlags,
  153. LPCWSTR pszFormat,
  154. ... );
  155. void
  156. DebugMsg(
  157. LPCSTR pszFormat,
  158. ... );
  159. void
  160. TraceMessage(
  161. LPCTSTR pszFile,
  162. const int uLine,
  163. LPCTSTR pszModule,
  164. DWORD dwCheckFlags,
  165. LPCTSTR pszFormat,
  166. ... );
  167. BOOL
  168. AssertMessage(
  169. LPCTSTR pszFile,
  170. const int uLine,
  171. LPCTSTR pszModule,
  172. LPCTSTR pszfn,
  173. BOOL fTrue );
  174. HRESULT
  175. TraceHR(
  176. LPCTSTR pszFile,
  177. const int uLine,
  178. LPCTSTR pszModule,
  179. LPCTSTR pszfn,
  180. HRESULT hr );
  181. //
  182. // Memory tracing functions - these are remapped to the "Global" memory
  183. // functions when in RETAIL.
  184. //
  185. HGLOBAL
  186. DebugAlloc(
  187. LPCTSTR pszFile,
  188. const int uLine,
  189. LPCTSTR pszModule,
  190. UINT uFlags,
  191. DWORD dwBytes,
  192. LPCTSTR pszComment );
  193. HGLOBAL
  194. DebugFree(
  195. HGLOBAL hMem );
  196. // The memory functions don't exist in RETAIL.
  197. HGLOBAL
  198. DebugMemoryAdd(
  199. HGLOBAL hglobal,
  200. LPCTSTR pszFile,
  201. const int uLine,
  202. LPCTSTR pszModule,
  203. UINT uFlags,
  204. DWORD dwBytes,
  205. LPCTSTR pszComment );
  206. #define DebugMemoryAddHandle( _handle ) \
  207. DebugMemoryAdd( _handle, TEXT(__FILE__), __LINE__, __MODULE__, GMEM_MOVEABLE, 0, TEXT(#_handle) );
  208. #define DebugMemoryAddAddress( _pv ) \
  209. DebugMemoryAdd( _pv, TEXT(__FILE__), __LINE__, __MODULE__, GMEM_FIXED, 0, TEXT(#_pv) );
  210. #define TraceStrDup( _sz ) \
  211. DebugMemoryAdd( StrDup( _sz ), TEXT(__FILE__), __LINE__, __MODULE__, GMEM_FIXED, 0, TEXT("StrDup(") TEXT(#_sz) TEXT(")") );
  212. void
  213. DebugMemoryDelete(
  214. HGLOBAL hglobal );
  215. void
  216. DebugMemoryCheck( );
  217. #ifdef __cplusplus
  218. extern void* __cdecl operator new( size_t nSize, LPCTSTR pszFile, const int iLine, LPCTSTR pszModule );
  219. #define new new( TEXT(__FILE__), __LINE__, __MODULE__ )
  220. #endif
  221. //
  222. //
  223. #else // it's RETAIL ******************************************************
  224. //
  225. //
  226. // Debugging -> NOPs
  227. #define Assert( _fn )
  228. #define DebugDo( _fn )
  229. #define DEFINE_MODULE( _module )
  230. #define DEFINE_THISCLASS( _class )
  231. #define DEFINE_SUPER( _super )
  232. #define BOOLTOSTRING( _fBool ) NULL
  233. #define AssertMsg 1 ? (void)0 : (void)
  234. #define TraceMsg 1 ? (void)0 : (void)
  235. #define TraceMsgGUID( _f, _g )
  236. #define DebugMsg 1 ? (void)0 : (void)
  237. #define ErrorMsg 1 ? (void)0 : (void)
  238. #define TraceMessage 1 ? (void)0 : (void)
  239. #define AssertMessage 1 ? (void)0 : (void)
  240. #define TraceHR 1 ? (void)0 : (void)
  241. #define TraceFunc 1 ? (void)0 : (void)
  242. #define TraceClsFunc 1 ? (void)0 : (void)
  243. #define TraceFuncExit()
  244. #define DebugMemoryAddHandle( _handle )
  245. #define DebugMemoryAddAddress( _pv )
  246. #define INITIALIZE_TRACE_MEMORY_PROCESS
  247. #define INITIALIZE_TRACE_MEMORY_THREAD
  248. #define UNINITIALIZE_TRACE_MEMORY
  249. #define DebugMemoryDelete( _h )
  250. // Tracing -> just do operation
  251. #define TraceDo( _fn ) _fn
  252. #define TraceAssertIfZero( _fn ) _fn
  253. // RETURN testing -> do retail
  254. #define THR
  255. #define RETURN( _fn ) return _fn
  256. #define RRETURN( _fn ) return _fn
  257. #define HRETURN( _hr ) return _hr
  258. #define QIRETURN( _qi, _riid ) return _qi
  259. // Memory Functions -> do retail
  260. #define TraceAlloc( _flags, _size ) GlobalAlloc( _flags, _size )
  261. #define TraceFree( _pv ) GlobalFree( _pv )
  262. #define TraceStrDup( _sz ) StrDup( _sz )
  263. #endif // DBG==1
  264. #endif // _DEBUG_H_