Windows NT 4.0 source code leak
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.

320 lines
8.8 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. Debug.h
  6. Abstract:
  7. New debug services for spooler.
  8. Author:
  9. Albert Ting (AlbertT) 15-Jan-1995
  10. Revision History:
  11. --*/
  12. #ifndef _DBGLOG_H
  13. #define _DBGLOG_H
  14. /********************************************************************
  15. Setting up the debug support:
  16. =============================
  17. Define a MODULE prefix string. Since this will be printed as
  18. a prefix to all debug output, it should be concise and unique.
  19. In your global header file:
  20. #define MODULE "prtlib:"
  21. Define a MODULE_DEBUG variable. This is the actual symbol
  22. that the library will use to indicate debugging level.
  23. This DWORD is broken into two bitfield WORDs: the low word
  24. indicates which levels to print to the debugger; the high word
  25. breaks into the debugger. The library takes the DebugLevel from
  26. a debug message, then ANDs it with the debug level. If the bit
  27. is on, the corresponding action (print or break) is taken.
  28. In your global header file:
  29. #define MODULE_DEBUG PrtlibDebug
  30. Finally, the actual debug variable must be defined and initialized
  31. to a default debug level. This must be done in exactly one
  32. *.c translation unit:
  33. In one of your source files:
  34. MODULE_DEBUG_LEVEL( {LevelsToPrint}, {LevelsToBreak} );
  35. Adding logging to source code:
  36. ==============================
  37. The general format for debug message is:
  38. DBGMSG( {DebugLevel}, ( {args to printf} ));
  39. The DebugLevel dictates whether the level should print, break
  40. into the debugger, or just log to memory (logging always done).
  41. The args to printf must be placed in an extra set of parens,
  42. and should assume everything is ANSI. To print LPTSTRs, use
  43. the TSTR macro:
  44. DBGMSG( DBG_WARN,
  45. ( "LPTSTR "TSTR", LPSTR %s, LPWSTR %ws\n",
  46. TEXT("hello"), "hello", L"hello" ));
  47. Viewing DBGMSGs:
  48. ================
  49. Messages will print to the debugger (usermode, or kernel debugger
  50. if no usermode debugger is available) for all printable levels.
  51. To change the level, you can edit the MODULE_DEBUG variable
  52. (PrtlibDebug in the above example).
  53. By default, DBG_ERROR and DBG_WARNING messages a logged to the
  54. error log (stored at gpbterrlog). All others are stored in the
  55. trace log (gpbttracelog). These currently log to memory in
  56. a circular buffer. Use the splx.dll extension to dump these
  57. logs.
  58. At compile time, you can switch these logs to go to file rather
  59. than memory. They will be stored as the PID + index number in
  60. the default directory of the process.
  61. ********************************************************************/
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. //
  66. // These values are strictly debug, but must be defined in the free
  67. // build because the TStatus error checking uses them as the first ctr
  68. // parameter. (During inlining they are discarded.)
  69. //
  70. #define DBG_NONE 0x0000
  71. #define DBG_INFO 0x0001
  72. #define DBG_WARN 0x0002
  73. #define DBG_WARNING 0x0002
  74. #define DBG_ERROR 0x0004
  75. #define DBG_TRACE 0x0008
  76. #define DBG_SECURITY 0x0010
  77. #define DBG_EXEC 0x0020
  78. #define DBG_PORT 0x0040
  79. #define DBG_NOTIFY 0x0080
  80. #define DBG_PAUSE 0x0100
  81. #define DBG_THREADM 0x0400
  82. #define DBG_MIN 0x0800
  83. #define DBG_TIME 0x1000
  84. #define DBG_FOLDER 0x2000
  85. #define DBG_NOHEAD 0x8000
  86. #if DBG
  87. BOOL
  88. SplLibInit(
  89. VOID
  90. );
  91. extern DWORD MODULE_DEBUG;
  92. //
  93. // This should be used exactly once in a C file. It defines
  94. // the Debug variable, and also the DbgMsg function.
  95. //
  96. // If we are statically linking with SplLib (SplLib is a library, not
  97. // a Dll), then we will get the definition from SplLib, so don't define
  98. // it here.
  99. //
  100. #ifdef LINK_SPLLIB
  101. #define MODULE_DEBUG_INIT( print, break ) \
  102. DWORD MODULE_DEBUG = (DBG_PRINT( print ) | DBG_BREAK( break ))
  103. #else
  104. #define MODULE_DEBUG_INIT( print, break ) \
  105. VOID \
  106. DbgMsg( \
  107. LPCSTR pszMsgFormat, \
  108. ... \
  109. ) \
  110. { \
  111. CHAR szMsgText[1024]; \
  112. va_list vargs; \
  113. \
  114. va_start( vargs, pszMsgFormat ); \
  115. wvsprintfA( szMsgText, pszMsgFormat, vargs ); \
  116. va_end( vargs ); \
  117. \
  118. if( szMsgText[0] && szMsgText[0] != ' ' ){ \
  119. OutputDebugStringA( MODULE ); \
  120. } \
  121. OutputDebugStringA( szMsgText ); \
  122. } \
  123. DWORD MODULE_DEBUG = (DBG_PRINT( print ) | DBG_BREAK( break ))
  124. #endif
  125. #define DBGSTR( str ) \
  126. ((str) ? (str) : TEXT("(NULL)"))
  127. #ifdef UNICODE
  128. #define TSTR "%ws"
  129. #else
  130. #define TSTR "%s"
  131. #endif
  132. #define DBG_PRINT_MASK 0xffff
  133. #define DBG_BREAK_SHIFT 16
  134. #define DBG_PRINT(x) (x)
  135. #define DBG_BREAK(x) (((x) << DBG_BREAK_SHIFT)|(x))
  136. #define SPLASSERT(expr) \
  137. if (!(expr)) { \
  138. DbgMsg( "Failed: %s\nLine %d, %s\n", \
  139. #expr, \
  140. __LINE__, \
  141. __FILE__ ); \
  142. DebugBreak(); \
  143. }
  144. VOID
  145. vDbgSingleThreadReset(
  146. PDWORD pdwThreadId
  147. );
  148. VOID
  149. vDbgSingleThread(
  150. PDWORD pdwThreadId
  151. );
  152. VOID
  153. vDbgSingleThreadNot(
  154. PDWORD pdwThreadId
  155. );
  156. VOID
  157. DbgMsg(
  158. LPCSTR pszMsgFormat,
  159. ...
  160. );
  161. #ifdef DBGLOG
  162. #define DBGMSG( uDbgLevel, argsPrint ) \
  163. vDbgLogError( MODULE_DEBUG, \
  164. uDbgLevel, \
  165. __LINE__, \
  166. __FILE__, \
  167. MODULE, \
  168. pszDbgAllocMsgA argsPrint )
  169. LPSTR
  170. pszDbgAllocMsgA(
  171. LPCSTR pszMsgFormatA,
  172. ...
  173. );
  174. VOID
  175. vDbgLogError(
  176. UINT uDbg,
  177. UINT uDbgLevel,
  178. UINT uLine,
  179. LPCSTR pszFileA,
  180. LPCSTR pszModuleA,
  181. LPCSTR pszMsgA
  182. );
  183. #else
  184. VOID
  185. DbgBreakPoint(
  186. VOID
  187. );
  188. #define DBGMSG( Level, MsgAndArgs ) \
  189. { \
  190. if( ( (Level) & 0xFFFF ) & MODULE_DEBUG ){ \
  191. DbgMsg MsgAndArgs; \
  192. } \
  193. if( ( (Level) << 16 ) & MODULE_DEBUG ) \
  194. DbgBreakPoint(); \
  195. }
  196. #endif
  197. #define SINGLETHREAD_VAR(var) \
  198. DWORD dwSingleThread_##var
  199. #define SINGLETHREAD(var) \
  200. vDbgSingleThread(&dwSingleThread_##var)
  201. #define SINGLETHREADNOT(var) \
  202. vDbgSingleThreadNot(&dwSingleThread_##var)
  203. #define SINGLETHREADRESET(var) \
  204. vDbgSingleThreadReset(&dwSingleThread_##var)
  205. #else
  206. #define MODULE_DEBUG_INIT( print, break )
  207. #define DBGMSG( uDbgLevel, argsPrint )
  208. #define SPLASSERT(exp)
  209. #define SINGLETHREAD_VAR(var)
  210. #define SINGLETHREAD(var)
  211. #define SINGLETHREADNOT(var)
  212. #define SINGLETHREADRESET(var)
  213. #endif
  214. //
  215. // Automatic checking if an object is valid.
  216. //
  217. #if DBG
  218. VOID
  219. vWarnInvalid(
  220. PVOID pvObject,
  221. UINT uDbg,
  222. UINT uLine,
  223. LPCSTR pszFileA,
  224. LPCSTR pszModuleA
  225. );
  226. #define VALID_PTR(x) \
  227. ((( x ) && (( x )->bValid( ))) ? \
  228. TRUE : \
  229. ( vWarnInvalid( (PVOID)(x), MODULE_DEBUG, __LINE__, __FILE__, MODULE ), FALSE ))
  230. #define VALID_OBJ(x) \
  231. ((( x ).bValid( )) ? \
  232. TRUE : \
  233. ( vWarnInvalid( (PVOID)&(x), MODULE_DEBUG, __LINE__, __FILE__, MODULE ), FALSE ))
  234. #define VALID_BASE(x) \
  235. (( x::bValid( )) ? \
  236. TRUE : \
  237. ( vWarnInvalid( (PVOID)this, MODULE_DEBUG, __LINE__, __FILE__, MODULE ), FALSE ))
  238. #else
  239. #define VALID_PTR(x) \
  240. (( x ) && (( x )->bValid()))
  241. #define VALID_OBJ(x) \
  242. (( x ).bValid())
  243. #define VALID_BASE(x) \
  244. ( x::bValid( ))
  245. #endif
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #endif // _DBGLOG_H