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