debug.h
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 14k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //====== Assertion/Debug output APIs =================================
  2. #if defined(DECLARE_DEBUG) && defined(DEBUG)
  3. //
  4. // Declare module-specific debug strings
  5. //
  6. //   When including this header in your private header file, do not
  7. //   define DECLARE_DEBUG.  But do define DECLARE_DEBUG in one of the
  8. //   source files in your project, and then include this header file.
  9. //
  10. //   You may also define the following:
  11. //
  12. //      SZ_DEBUGINI     - the .ini file used to set debug flags
  13. //      SZ_DEBUGSECTION - the section in the .ini file specific to
  14. //                        the module component.
  15. //      SZ_MODULE       - ansi version of the name of your module.
  16. //
  17. //
  18. // (These are deliberately CHAR)
  19. EXTERN_C const CHAR FAR c_szCcshellIniFile[] = SZ_DEBUGINI;
  20. EXTERN_C const CHAR FAR c_szCcshellIniSecDebug[] = SZ_DEBUGSECTION;
  21. EXTERN_C const WCHAR FAR c_wszTrace[] = L"t " TEXTW(SZ_MODULE) L"  ";
  22. EXTERN_C const WCHAR FAR c_wszErrorDbg[] = L"err " TEXTW(SZ_MODULE) L"  ";
  23. EXTERN_C const WCHAR FAR c_wszWarningDbg[] = L"wn " TEXTW(SZ_MODULE) L"  ";
  24. EXTERN_C const WCHAR FAR c_wszAssertMsg[] = TEXTW(SZ_MODULE) L"  Assert: ";
  25. EXTERN_C const WCHAR FAR c_wszAssertFailed[] = TEXTW(SZ_MODULE) L"  Assert %s, line %d: (%s)rn";
  26. // (These are deliberately CHAR)
  27. EXTERN_C const CHAR  FAR c_szTrace[] = "t " SZ_MODULE "  ";
  28. EXTERN_C const CHAR  FAR c_szErrorDbg[] = "err " SZ_MODULE "  ";
  29. EXTERN_C const CHAR  FAR c_szWarningDbg[] = "wn " SZ_MODULE "  ";
  30. EXTERN_C const CHAR  FAR c_szAssertMsg[] = SZ_MODULE "  Assert: ";
  31. EXTERN_C const CHAR  FAR c_szAssertFailed[] = SZ_MODULE "  Assert %s, line %d: (%s)rn";
  32. #endif  // DECLARE_DEBUG && DEBUG
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #if !defined(DECLARE_DEBUG)
  37. //
  38. // Debug macros and validation code
  39. //
  40. #undef Assert
  41. #undef AssertE
  42. #undef AssertMsg
  43. #undef AssertStrLen
  44. #undef DebugMsg
  45. #undef FullDebugMsg
  46. #undef ASSERT
  47. #undef EVAL
  48. // Access these globals to determine which debug flags are set.
  49. // These globals are modified by CcshellGetDebugFlags(), which
  50. // reads an .ini file and sets the appropriate flags.
  51. //
  52. //   g_dwDumpFlags  - bits are application specific.  Typically 
  53. //                    used for dumping structures.
  54. //   g_dwBreakFlags - uses BF_* flags.  The remaining bits are
  55. //                    application specific.  Used to determine
  56. //                    when to break into the debugger.
  57. //   g_dwTraceFlags - uses TF_* flags.  The remaining bits are
  58. //                    application specific.  Used to display
  59. //                    debug trace messages.
  60. //   g_dwFuncTraceFlags - bits are application specific.  When
  61. //                    TF_FUNC is set, CcshellFuncMsg uses this
  62. //                    value to determine which function traces
  63. //                    to display.
  64. //   g_dwProtoype   - bits are application specific.  Use it for
  65. //                    anything.
  66. //
  67. extern DWORD g_dwDumpFlags;
  68. extern DWORD g_dwBreakFlags;
  69. extern DWORD g_dwTraceFlags;
  70. #ifdef DEBUG
  71. extern DWORD g_dwPrototype;
  72. #else
  73. #define g_dwPrototype   0
  74. #endif
  75. extern DWORD g_dwFuncTraceFlags;
  76. BOOL CcshellGetDebugFlags(void);
  77. // Break flags for g_dwBreakFlags
  78. #define BF_ONVALIDATE       0x00000001      // Break on assertions or validation
  79. #define BF_ONAPIENTER       0x00000002      // Break on entering an API
  80. #define BF_ONERRORMSG       0x00000004      // Break on TF_ERROR
  81. #define BF_ONWARNMSG        0x00000008      // Break on TF_WARNING
  82. // Trace flags for g_dwTraceFlags
  83. #define TF_ALWAYS           0xFFFFFFFF
  84. #define TF_NEVER            0x00000000
  85. #define TF_WARNING          0x00000001
  86. #define TF_ERROR            0x00000002
  87. #define TF_GENERAL          0x00000004      // Standard messages
  88. #define TF_FUNC             0x00000008      // Trace function calls
  89. // (Upper 28 bits reserved for custom use per-module)
  90. // Old, archaic debug flags.  
  91. // BUGBUG (scotth): the following flags will be phased out over time.
  92. #ifdef DM_TRACE
  93. #undef DM_TRACE
  94. #undef DM_WARNING
  95. #undef DM_ERROR
  96. #endif
  97. #define DM_TRACE            TF_GENERAL      // OBSOLETE Trace messages
  98. #define DM_WARNING          TF_WARNING      // OBSOLETE Warning
  99. #define DM_ERROR            TF_ERROR        // OBSOLETE Error
  100. // Use this macro to declare message text that will be placed
  101. // in the CODE segment (useful if DS is getting full)
  102. //
  103. // Ex: DEBUGTEXT(szMsg, "Invalid whatever: %d");
  104. //
  105. #define DEBUGTEXT(sz, msg)      /* ;Internal */ 
  106.     static const TCHAR sz[] = msg;
  107. #ifndef NOSHELLDEBUG    // Others have own versions of these.
  108. #ifdef DEBUG
  109. #ifdef _X86_
  110. // Use int 3 so we stop immediately in the source
  111. #define DEBUG_BREAK        do { _try { _asm int 3 } _except (EXCEPTION_EXECUTE_HANDLER) {;} } while (0)
  112. #else
  113. #define DEBUG_BREAK        do { _try { DebugBreak(); } _except (EXCEPTION_EXECUTE_HANDLER) {;} } while (0)
  114. #endif
  115. // Prototypes for debug functions
  116. void CcshellStackEnter(void);
  117. void CcshellStackLeave(void);
  118. BOOL CcshellAssertFailedA(LPCSTR szFile, int line, LPCSTR pszEval, BOOL bBreak);
  119. BOOL CcshellAssertFailedW(LPCWSTR szFile, int line, LPCWSTR pwszEval, BOOL bBreak);
  120. void CDECL CcshellDebugMsgW(DWORD mask, LPCSTR pszMsg, ...);
  121. void CDECL CcshellDebugMsgA(DWORD mask, LPCSTR pszMsg, ...);
  122. void CDECL CcshellFuncMsgW(DWORD mask, LPCSTR pszMsg, ...);
  123. void CDECL CcshellFuncMsgA(DWORD mask, LPCSTR pszMsg, ...);
  124. void CDECL CcshellAssertMsgW(BOOL bAssert, LPCSTR pszMsg, ...);
  125. void CDECL CcshellAssertMsgA(BOOL bAssert, LPCSTR pszMsg, ...);
  126. void CDECL _AssertMsgA(BOOL f, LPCSTR pszMsg, ...);
  127. void CDECL _AssertMsgW(BOOL f, LPCWSTR pszMsg, ...);
  128. void CDECL _DebugMsgA(DWORD flag, LPCSTR psz, ...);
  129. void CDECL _DebugMsgW(DWORD flag, LPCWSTR psz, ...);
  130. void _AssertStrLenA(LPCSTR pszStr, int iLen);
  131. void _AssertStrLenW(LPCWSTR pwzStr, int iLen);
  132. #ifdef UNICODE
  133. #define CcshellAssertFailed     CcshellAssertFailedW
  134. #define CcshellDebugMsg         CcshellDebugMsgW
  135. #define CcshellFuncMsg          CcshellFuncMsgW
  136. #define CcshellAssertMsg        CcshellAssertMsgW
  137. #define _AssertMsg              _AssertMsgW
  138. #define _AssertStrLen           _AssertStrLenW
  139. #define _DebugMsg               _DebugMsgW
  140. #else
  141. #define CcshellAssertFailed     CcshellAssertFailedA
  142. #define CcshellDebugMsg         CcshellDebugMsgA
  143. #define CcshellFuncMsg          CcshellFuncMsgA
  144. #define CcshellAssertMsg        CcshellAssertMsgA
  145. #define _AssertMsg              _AssertMsgA
  146. #define _AssertStrLen           _AssertStrLenA
  147. #define _DebugMsg               _DebugMsgA
  148. #endif
  149. // Explanation of debug macros:
  150. //
  151. // ----
  152. // Assert(f)
  153. // ASSERT(f)
  154. //
  155. //   Generates a "Assert file.c, line x (eval)" message if f is NOT true.
  156. //   The g_dwBreakFlags global governs whether the function DebugBreaks.
  157. //
  158. // ----
  159. // AssertE(f)
  160. //
  161. //   Works like Assert, except (f) is also executed in the retail 
  162. //   version as well.
  163. //
  164. // ----
  165. // EVAL(f)
  166. //
  167. //   Evaluates the expression (f).  The expression is always evaluated,
  168. //   even in retail builds.  But the macro only asserts in the debug
  169. //   build.  This macro may only be used on logical expressions, eg:
  170. //
  171. //          if (EVAL(exp))
  172. //              // do something
  173. //
  174. // ----
  175. // TraceMsg(mask, sz, args...) 
  176. //
  177. //   Generate wsprintf-formatted msg using specified trace mask.  
  178. //   The g_dwTraceFlags global governs whether message is displayed.
  179. //
  180. //   The sz parameter is always ANSI; TraceMsg correctly converts it
  181. //   to unicode if necessary.  This is so you don't have to wrap your
  182. //   debug strings with TEXT().
  183. //
  184. // ----
  185. // DebugMsg(mask, sz, args...) 
  186. //
  187. //   OBSOLETE!  
  188. //   Like TraceMsg, except you must wrap the sz parameter with TEXT().
  189. //
  190. // ----
  191. // AssertMsg(bAssert, sz, args...)
  192. //
  193. //   Generate wsprintf-formatted msg if the assertion is false.  
  194. //   The g_dwBreakFlags global governs whether the function DebugBreaks.
  195. //
  196. //   The sz parameter is always ANSI; AssertMsg correctly converts it
  197. //   to unicode if necessary.  This is so you don't have to wrap your
  198. //   debug strings with TEXT().
  199. //
  200. #define ASSERT(f)                                 
  201.     {                                             
  202.         DEBUGTEXT(szFile, TEXT(__FILE__));              
  203.         if (!(f) && CcshellAssertFailed(szFile, __LINE__, TEXT(#f), FALSE)) 
  204.             DEBUG_BREAK;       
  205.     }
  206. #ifdef DISALLOW_Assert
  207. #define Assert(f)        Dont_use_Assert___Use_ASSERT
  208. #else
  209. #define Assert(f)           ASSERT(f)
  210. #endif
  211. #define AssertE(f)          ASSERT(f)
  212. #define EVAL(exp)   
  213.     ((exp) || (CcshellAssertFailed(TEXT(__FILE__), __LINE__, TEXT(#exp), TRUE), 0))
  214. // Use TraceMsg instead of DebugMsg.  DebugMsg is obsolete.
  215. #define AssertMsg           _AssertMsg
  216. #define AssertStrLen        _AssertStrLen
  217. #define AssertStrLenA       _AssertStrLenA
  218. #define AssertStrLenW       _AssertStrLenW
  219. #ifdef DISALLOW_DebugMsg
  220. #define DebugMsg            Dont_use_DebugMsg___Use_TraceMsg
  221. #else
  222. #define DebugMsg            _DebugMsg
  223. #endif
  224. #ifdef FULL_DEBUG
  225. #define FullDebugMsg        _DebugMsg
  226. #else
  227. #define FullDebugMsg        1 ? (void)0 : (void)
  228. #endif
  229. #define Dbg_SafeStrA(psz)   (SAFECAST(psz, LPCSTR), (psz) ? (psz) : "NULL string")
  230. #define Dbg_SafeStrW(psz)   (SAFECAST(psz, LPCWSTR), (psz) ? (psz) : L"NULL string")
  231. #ifdef UNICODE
  232. #define Dbg_SafeStr         Dbg_SafeStrW
  233. #else
  234. #define Dbg_SafeStr         Dbg_SafeStrA
  235. #endif
  236. #define ASSERT_MSGW         CcshellAssertMsgW
  237. #define ASSERT_MSGA         CcshellAssertMsgA
  238. #define ASSERT_MSG          CcshellAssertMsg
  239. #define TraceMsgW           CcshellDebugMsgW
  240. #define TraceMsgA           CcshellDebugMsgA
  241. #define TraceMsg            CcshellDebugMsg
  242. #define FUNC_MSG            CcshellFuncMsg
  243. // Debug function enter
  244. // DBG_ENTER(flag, fn)  -- Generates a function entry debug spew for
  245. //                          a function
  246. //
  247. #define DBG_ENTER(flagFTF, fn)                  
  248.         (FUNC_MSG(flagFTF, " > " #fn "()"), 
  249.          CcshellStackEnter())
  250. // DBG_ENTER_TYPE(flag, fn, dw, pfnStrFromType)  -- Generates a function entry debug
  251. //                          spew for functions that accept <type>.
  252. //
  253. #define DBG_ENTER_TYPE(flagFTF, fn, dw, pfnStrFromType)                   
  254.         (FUNC_MSG(flagFTF, " < " #fn "(..., %s, ...)", (LPCTSTR)pfnStrFromType(dw)), 
  255.          CcshellStackEnter())
  256. // DBG_ENTER_SZ(flag, fn, sz)  -- Generates a function entry debug spew for
  257. //                          a function that accepts a string as one of its
  258. //                          parameters.
  259. //
  260. #define DBG_ENTER_SZ(flagFTF, fn, sz)                  
  261.         (FUNC_MSG(flagFTF, " > " #fn "(..., "%s",...)", Dbg_SafeStr(sz)), 
  262.          CcshellStackEnter())
  263. // Debug function exit
  264. // DBG_EXIT(flag, fn)  -- Generates a function exit debug spew
  265. //
  266. #define DBG_EXIT(flagFTF, fn)                              
  267.         (CcshellStackLeave(), 
  268.          FUNC_MSG(flagFTF, " < " #fn "()"))
  269. // DBG_EXIT_TYPE(flag, fn, dw, pfnStrFromType)  -- Generates a function exit debug
  270. //                          spew for functions that return <type>.
  271. //
  272. #define DBG_EXIT_TYPE(flagFTF, fn, dw, pfnStrFromType)                   
  273.         (CcshellStackLeave(), 
  274.          FUNC_MSG(flagFTF, " < " #fn "() with %s", (LPCTSTR)pfnStrFromType(dw)))
  275. // DBG_EXIT_INT(flag, fn, us)  -- Generates a function exit debug spew for
  276. //                          functions that return an INT.
  277. //
  278. #define DBG_EXIT_INT(flagFTF, fn, n)                       
  279.         (CcshellStackLeave(), 
  280.          FUNC_MSG(flagFTF, " < " #fn "() with %d", (int)(n)))
  281. // DBG_EXIT_BOOL(flag, fn, b)  -- Generates a function exit debug spew for
  282. //                          functions that return a boolean.
  283. //
  284. #define DBG_EXIT_BOOL(flagFTF, fn, b)                      
  285.         (CcshellStackLeave(), 
  286.          FUNC_MSG(flagFTF, " < " #fn "() with %s", (b) ? (LPTSTR)TEXT("TRUE") : (LPTSTR)TEXT("FALSE")))
  287. // DBG_EXIT_UL(flag, fn, ul)  -- Generates a function exit debug spew for
  288. //                          functions that return a ULONG.
  289. //
  290. #define DBG_EXIT_UL(flagFTF, fn, ul)                   
  291.         (CcshellStackLeave(), 
  292.          FUNC_MSG(flagFTF, " < " #fn "() with %#08lx", (ULONG)(ul)))
  293. #define DBG_EXIT_DWORD      DBG_EXIT_UL
  294. // DBG_EXIT_HRES(flag, fn, hres)  -- Generates a function exit debug spew for
  295. //                          functions that return an HRESULT.
  296. //
  297. #define DBG_EXIT_HRES(flagFTF, fn, hres)     DBG_EXIT_TYPE(flagFTF, fn, hres, Dbg_GetHRESULTName)
  298. #else   // DEBUG
  299. #define Assert(f)
  300. #define AssertE(f)      (f)
  301. #define ASSERT(f)
  302. #define AssertMsg       1 ? (void)0 : (void)
  303. #define AssertStrLen(lpStr, iLen)
  304. #define DebugMsg        1 ? (void)0 : (void)
  305. #define FullDebugMsg    1 ? (void)0 : (void)
  306. #define EVAL(exp)       ((exp) != 0)
  307. #define Dbg_SafeStr     1 ? (void)0 : (void)
  308. #define TraceMsgA       1 ? (void)0 : (void)
  309. #define TraceMsgW       1 ? (void)0 : (void)
  310. #ifdef UNICODE
  311. #define TraceMsg        TraceMsgW
  312. #else
  313. #define TraceMsg        TraceMsgA
  314. #endif
  315. #define FUNC_MSG        1 ? (void)0 : (void)
  316. #define ASSERT_MSGA     TraceMsgA
  317. #define ASSERT_MSGW     TraceMsgW
  318. #define ASSERT_MSG      TraceMsg
  319. #define DBG_ENTER(flagFTF, fn)
  320. #define DBG_ENTER_TYPE(flagFTF, fn, dw, pfn)
  321. #define DBG_ENTER_SZ(flagFTF, fn, sz)
  322. #define DBG_EXIT(flagFTF, fn)
  323. #define DBG_EXIT_INT(flagFTF, fn, n)
  324. #define DBG_EXIT_BOOL(flagFTF, fn, b)
  325. #define DBG_EXIT_UL(flagFTF, fn, ul)
  326. #define DBG_EXIT_DWORD      DBG_EXIT_UL
  327. #define DBG_EXIT_TYPE(flagFTF, fn, dw, pfn)
  328. #define DBG_EXIT_HRES(flagFTF, fn, hres)
  329. #endif  // DEBUG
  330. #endif  // NOSHELLDEBUG
  331. // 
  332. // Debug dump helper functions
  333. //
  334. #ifdef DEBUG
  335. LPCTSTR DebugGUIDName(REFGUID rguid, LPCTSTR szKey, LPTSTR pchName);
  336. LPCTSTR DebugIIDName(REFIID riid, LPTSTR pchName);
  337. LPCTSTR DebugCLSIDName(REFIID riid, LPTSTR pchName);
  338. #else
  339. #endif // DEBUG
  340. // Parameter validation macros
  341. #include "validate.h"
  342. #endif // DECLARE_DEBUG
  343. #ifdef __cplusplus
  344. };
  345. #endif