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.

886 lines
30 KiB

  1. //====== Assertion/Debug output APIs =================================
  2. #include <platform.h> // for __endexcept
  3. #if defined(DECLARE_DEBUG) && defined(DEBUG)
  4. //
  5. // Declare module-specific debug strings
  6. //
  7. // When including this header in your private header file, do not
  8. // define DECLARE_DEBUG. But do define DECLARE_DEBUG in one of the
  9. // source files in your project, and then include this header file.
  10. //
  11. // You may also define the following:
  12. //
  13. // SZ_DEBUGINI - the .ini file used to set debug flags
  14. // SZ_DEBUGSECTION - the section in the .ini file specific to
  15. // the module component.
  16. // SZ_MODULE - ansi version of the name of your module.
  17. //
  18. //
  19. // For string constants that are always wide
  20. #define __TEXTW(x) L##x
  21. #define TEXTW(x) __TEXTW(x)
  22. // (These are deliberately CHAR)
  23. EXTERN_C const CHAR FAR c_szCcshellIniFile[] = SZ_DEBUGINI;
  24. EXTERN_C const CHAR FAR c_szCcshellIniSecDebug[] = SZ_DEBUGSECTION;
  25. #if 0
  26. EXTERN_C const WCHAR FAR c_wszTrace[] = L"trc " TEXTW(SZ_MODULE) L" ";
  27. EXTERN_C const WCHAR FAR c_wszErrorDbg[] = L"err " TEXTW(SZ_MODULE) L" ";
  28. EXTERN_C const WCHAR FAR c_wszWarningDbg[] = L"wrn " TEXTW(SZ_MODULE) L" ";
  29. EXTERN_C const WCHAR FAR c_wszGeneralDbg[] = L"gen " TEXTW(SZ_MODULE) L" ";
  30. EXTERN_C const WCHAR FAR c_wszFuncDbg[] = L"fnc " TEXTW(SZ_MODULE) L" ";
  31. EXTERN_C const WCHAR FAR c_wszMemLeakDbg[] = L"mem " TEXTW(SZ_MODULE) L" ";
  32. #else
  33. EXTERN_C const WCHAR FAR c_wszTrace[] = TEXTW(SZ_MODULE) L"-[Trc] ";
  34. EXTERN_C const WCHAR FAR c_wszErrorDbg[] = TEXTW(SZ_MODULE) L"-[Err] ";
  35. EXTERN_C const WCHAR FAR c_wszWarningDbg[] = TEXTW(SZ_MODULE) L"-[Wrn] ";
  36. EXTERN_C const WCHAR FAR c_wszGeneralDbg[] = TEXTW(SZ_MODULE) L"-[Gen] ";
  37. EXTERN_C const WCHAR FAR c_wszFuncDbg[] = TEXTW(SZ_MODULE) L"-[Fnc] ";
  38. EXTERN_C const WCHAR FAR c_wszMemLeakDbg[] = TEXTW(SZ_MODULE) L"-[Mem] ";
  39. #endif
  40. EXTERN_C const WCHAR FAR c_wszAssertMsg[] = TEXTW(SZ_MODULE) L" Assert: ";
  41. EXTERN_C const WCHAR FAR c_wszAssertFailed[] = TEXTW(SZ_MODULE) L" Assert %ls, line %d: (%ls)\r\n";
  42. EXTERN_C const WCHAR FAR c_wszRip[] = TEXTW(SZ_MODULE) L" RIP in %s at %s, line %d: (%s)\r\n";
  43. EXTERN_C const WCHAR FAR c_wszRipNoFn[] = TEXTW(SZ_MODULE) L" RIP at %s, line %d: (%s)\r\n";
  44. // (These are deliberately CHAR)
  45. #if 0
  46. EXTERN_C const CHAR FAR c_szTrace[] = "trc " SZ_MODULE " ";
  47. EXTERN_C const CHAR FAR c_szErrorDbg[] = "err " SZ_MODULE " ";
  48. EXTERN_C const CHAR FAR c_szWarningDbg[] = "wrn " SZ_MODULE " ";
  49. EXTERN_C const CHAR FAR c_szGeneralDbg[] = "gen " SZ_MODULE " ";
  50. EXTERN_C const CHAR FAR c_szFuncDbg[] = "fnc " SZ_MODULE " ";
  51. EXTERN_C const CHAR FAR c_szMemLeakDbg[] = "mem " SZ_MODULE " ";
  52. #else
  53. EXTERN_C const CHAR FAR c_szTrace[] = SZ_MODULE "-[Trc] ";
  54. EXTERN_C const CHAR FAR c_szErrorDbg[] = SZ_MODULE "-[Err] ";
  55. EXTERN_C const CHAR FAR c_szWarningDbg[] = SZ_MODULE "-[Wrn] ";
  56. EXTERN_C const CHAR FAR c_szGeneralDbg[] = SZ_MODULE "-[Gen] ";
  57. EXTERN_C const CHAR FAR c_szFuncDbg[] = SZ_MODULE "-[Fnc] ";
  58. EXTERN_C const CHAR FAR c_szMemLeakDbg[] = SZ_MODULE "-[Mem] ";
  59. #endif
  60. EXTERN_C const CHAR FAR c_szAssertMsg[] = SZ_MODULE " Assert: ";
  61. EXTERN_C const CHAR FAR c_szAssertFailed[] = SZ_MODULE " Assert %s, line %d: (%s)\r\n";
  62. EXTERN_C const CHAR FAR c_szRip[] = SZ_MODULE " RIP in %s at %s, line %d: (%s)\r\n";
  63. EXTERN_C const CHAR FAR c_szRipNoFn[] = SZ_MODULE " RIP at %s, line %d: (%s)\r\n";
  64. #endif // DECLARE_DEBUG && DEBUG
  65. #if defined(DECLARE_DEBUG) && defined(PRODUCT_PROF)
  66. EXTERN_C const CHAR FAR c_szCcshellIniFile[] = SZ_DEBUGINI;
  67. EXTERN_C const CHAR FAR c_szCcshellIniSecDebug[] = SZ_DEBUGSECTION;
  68. #endif
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. #if !defined(DECLARE_DEBUG)
  73. //
  74. // Debug macros and validation code
  75. //
  76. // Undefine the macros that we define in case some other header
  77. // might have tried defining these commonly-named macros.
  78. #undef Assert
  79. #undef AssertE
  80. #undef AssertMsg
  81. #undef AssertStrLen
  82. #undef DebugMsg
  83. #undef FullDebugMsg
  84. #undef ASSERT
  85. #undef EVAL
  86. #undef ASSERTMSG // catch people's typos
  87. #undef DBEXEC
  88. #ifdef _ATL_NO_DEBUG_CRT
  89. #undef _ASSERTE // we substitute this ATL macro
  90. #endif
  91. // Access these globals to determine which debug flags are set.
  92. // These globals are modified by CcshellGetDebugFlags(), which
  93. // reads an .ini file and sets the appropriate flags.
  94. //
  95. // g_dwDumpFlags - bits are application specific. Typically
  96. // used for dumping structures.
  97. // g_dwBreakFlags - uses BF_* flags. The remaining bits are
  98. // application specific. Used to determine
  99. // when to break into the debugger.
  100. // g_dwTraceFlags - uses TF_* flags. The remaining bits are
  101. // application specific. Used to display
  102. // debug trace messages.
  103. // g_dwFuncTraceFlags - bits are application specific. When
  104. // TF_FUNC is set, CcshellFuncMsg uses this
  105. // value to determine which function traces
  106. // to display.
  107. // g_dwProtoype - bits are application specific. Use it for
  108. // anything.
  109. // g_dwProfileCAP - bits are application specific. Used to
  110. // control ICECAP profiling.
  111. //
  112. extern DWORD g_dwDumpFlags;
  113. extern DWORD g_dwBreakFlags;
  114. extern DWORD g_dwTraceFlags;
  115. #ifdef DEBUG
  116. extern DWORD g_dwPrototype;
  117. #else
  118. #define g_dwPrototype 0
  119. #endif
  120. extern DWORD g_dwFuncTraceFlags;
  121. #if defined(DEBUG) || defined(PRODUCT_PROF)
  122. BOOL CcshellGetDebugFlags(void);
  123. #else
  124. #define CcshellGetDebugFlags() 0
  125. #endif
  126. // Break flags for g_dwBreakFlags
  127. #define BF_ASSERT 0x00000001 // Break on assertions
  128. #define BF_ONAPIENTER 0x00000002 // Break on entering an API
  129. #define BF_ONERRORMSG 0x00000004 // Break on TF_ERROR
  130. #define BF_ONWARNMSG 0x00000008 // Break on TF_WARNING
  131. #define BF_THR 0x00000100 // Break when THR() receives a failure
  132. #define BF_RIP 0x00000200 // Break on RIPs
  133. #define BF_ASSERTPOPUP 0x00000400 // Break on RIPs
  134. // Trace flags for g_dwTraceFlags
  135. #define TF_ALWAYS 0xFFFFFFFF
  136. #define TF_NEVER 0x00000000
  137. #define TF_WARNING 0x00000001
  138. #define TF_ERROR 0x00000002
  139. #define TF_GENERAL 0x00000004 // Standard messages
  140. #define TF_FUNC 0x00000008 // Trace function calls
  141. #define TF_ATL 0x00000008 // Since TF_FUNC is so-little used, I'm overloading this bit
  142. #define TF_EVENT 0x00000010
  143. #define TF_MEMORY_LEAK 0x00000020
  144. // (Upper 28 bits reserved for custom use per-module)
  145. // Old, archaic debug flags.
  146. // Issue (scotth): the following flags will be phased out over time.
  147. #ifdef DM_TRACE
  148. #undef DM_TRACE
  149. #undef DM_WARNING
  150. #undef DM_ERROR
  151. #endif
  152. #define DM_TRACE TF_GENERAL // OBSOLETE Trace messages
  153. #define DM_WARNING TF_WARNING // OBSOLETE Warning
  154. #define DM_ERROR TF_ERROR // OBSOLETE Error
  155. // Use this macro to declare message text that will be placed
  156. // in the CODE segment (useful if DS is getting full)
  157. //
  158. // Ex: DEBUGTEXT(szMsg, "Invalid whatever: %d");
  159. //
  160. #define DEBUGTEXT(sz, msg) /* ;Internal */ \
  161. static const TCHAR sz[] = msg
  162. #ifndef NOSHELLDEBUG // Others have own versions of these.
  163. #ifdef DEBUG
  164. #ifdef _X86_
  165. // Use int 3 so we stop immediately in the source
  166. #define DEBUG_BREAK do { _try { _asm int 3 } _except (EXCEPTION_EXECUTE_HANDLER) {;} } while (0)
  167. #else
  168. #define DEBUG_BREAK do { _try { DebugBreak(); } _except (EXCEPTION_EXECUTE_HANDLER) {;} __endexcept } while (0)
  169. #endif
  170. BOOL AttachDebugger(DWORD pid);
  171. // Prototypes for debug functions
  172. void CcshellStackEnter(void);
  173. void CcshellStackLeave(void);
  174. void CDECL CcshellFuncMsgW(DWORD mask, LPCSTR pszMsg, ...);
  175. void CDECL CcshellFuncMsgA(DWORD mask, LPCSTR pszMsg, ...);
  176. void CDECL _AssertMsgA(BOOL f, LPCSTR pszMsg, ...);
  177. void CDECL _AssertMsgW(BOOL f, LPCWSTR pszMsg, ...);
  178. void _AssertStrLenA(LPCSTR pszStr, int iLen);
  179. void _AssertStrLenW(LPCWSTR pwzStr, int iLen);
  180. #ifdef UNICODE
  181. #define CcshellFuncMsg CcshellFuncMsgW
  182. #define CcshellAssertMsg CcshellAssertMsgW
  183. #define _AssertMsg _AssertMsgW
  184. #define _AssertStrLen _AssertStrLenW
  185. #else
  186. #define CcshellFuncMsg CcshellFuncMsgA
  187. #define CcshellAssertMsg CcshellAssertMsgA
  188. #define _AssertMsg _AssertMsgA
  189. #define _AssertStrLen _AssertStrLenA
  190. #endif
  191. #endif // DEBUG
  192. // ASSERT(f)
  193. //
  194. // Generates a "Assert file.c, line x (eval)" message if f is NOT true.
  195. //
  196. // Use ASSERT() to check for logic invariance. These are typically considered
  197. // fatal problems, and falls into the 'this should never ever happen'
  198. // category.
  199. //
  200. // Do *not* use ASSERT() to verify successful API calls if the APIs can
  201. // legitimately fail due to low resources. For example, LocalAlloc can
  202. // legally fail, so you shouldn't assert that it will never fail.
  203. //
  204. // The BF_ASSERT bit in g_dwBreakFlags governs whether the function
  205. // performs a DebugBreak().
  206. //
  207. // Default Behavior-
  208. // Retail builds: nothing
  209. // Debug builds: spew and break
  210. // Full debug builds: spew and break
  211. //
  212. #ifdef DEBUG
  213. BOOL CcshellAssertFailedA(LPCSTR szFile, int line, LPCSTR pszEval, BOOL bBreakInside, BOOL bAssertPopup);
  214. BOOL CcshellAssertFailedW(LPCWSTR szFile, int line, LPCWSTR pwszEval, BOOL bBreakInside, BOOL bAssertPopup);
  215. #ifdef UNICODE
  216. #define CcshellAssertFailed CcshellAssertFailedW
  217. #else
  218. #define CcshellAssertFailed CcshellAssertFailedA
  219. #endif
  220. #define ASSERT(f) \
  221. { \
  222. DEBUGTEXT(szFile, TEXT(__FILE__)); \
  223. if (!(f) && CcshellAssertFailed(szFile, __LINE__, TEXT(#f), FALSE, TRUE)) \
  224. DEBUG_BREAK; \
  225. }
  226. #define ASSERTPRIVATE(f) \
  227. { \
  228. DEBUGTEXT(szFile, TEXT(__FILE__)); \
  229. if (!(f) && CcshellAssertFailed(szFile, __LINE__, TEXT(#f), FALSE, FALSE)) \
  230. DEBUG_BREAK; \
  231. }
  232. // The old Win95 code used to use "Assert()". We discourage the use
  233. // of this macro now because it is not msdev-friendly.
  234. #ifdef DISALLOW_Assert
  235. #define Assert(f) Dont_use_Assert___Use_ASSERT
  236. #define AssertPrivate(f) Dont_use_AssertPrivate___Use_ASSERT
  237. #else
  238. #define Assert(f) ASSERT(f)
  239. #define AssertPrivate(f) ASSERTPRIVATE(f)
  240. #endif
  241. #else // DEBUG
  242. #define ASSERT(f)
  243. #define ASSERTPRIVATE(f)
  244. #define Assert(f)
  245. #define AssertPrivate(f)
  246. #endif // DEBUG
  247. // ASSERTMSG(f, szFmt, args...)
  248. //
  249. // Behaves like ASSERT, except it prints the wsprintf-formatted message
  250. // instead of the file and line number.
  251. //
  252. // The sz parameter is always ANSI; AssertMsg correctly converts it
  253. // to unicode if necessary. This is so you don't have to wrap your
  254. // debug strings with TEXT().
  255. //
  256. // The BF_ASSERT bit in g_dwBreakFlags governs whether the function
  257. // performs a DebugBreak().
  258. //
  259. // Default Behavior-
  260. // Retail builds: nothing
  261. // Debug builds: spew and break
  262. // Full debug builds: spew and break
  263. //
  264. #ifdef DEBUG
  265. void CDECL CcshellAssertMsgW(BOOL bAssert, LPCSTR pszMsg, ...);
  266. void CDECL CcshellAssertMsgA(BOOL bAssert, LPCSTR pszMsg, ...);
  267. #ifdef UNICODE
  268. #define CcshellAssertMsg CcshellAssertMsgW
  269. #else
  270. #define CcshellAssertMsg CcshellAssertMsgA
  271. #endif
  272. #define ASSERTMSG CcshellAssertMsg
  273. #else // DEBUG
  274. #define ASSERTMSG 1 ? (void)0 : (void)
  275. #endif // DEBUG
  276. // EVAL(f)
  277. //
  278. // Behaves like ASSERT(). Evaluates the expression (f). The expression
  279. // is always evaluated, even in retail builds. But the macro only asserts
  280. // in the debug build. This macro may be used on logical expressions, eg:
  281. //
  282. // if (EVAL(exp))
  283. // // do something
  284. //
  285. // Do *not* use EVAL() to verify successful API calls if the APIs can
  286. // legitimately fail due to low resources. For example, LocalAlloc can
  287. // legally fail, so you shouldn't assert that it will never fail.
  288. //
  289. // The BF_ASSERT bit in g_dwBreakFlags governs whether the function
  290. // performs a DebugBreak().
  291. //
  292. // Default Behavior-
  293. // Retail builds: nothing
  294. // Debug builds: spew and break
  295. // Full debug builds: spew and break
  296. //
  297. #ifdef DEBUG
  298. #define EVAL(exp) \
  299. ((exp) || (CcshellAssertFailed(TEXT(__FILE__), __LINE__, TEXT(#exp), TRUE, FALSE), 0))
  300. #else // DEBUG
  301. #define EVAL(exp) ((exp) != 0)
  302. #endif // DEBUG
  303. // RIP(f)
  304. //
  305. // Generates a "RIP at file.c, line x (eval)" message if f is NOT true.
  306. //
  307. // Use RIP() to perform parameter validation, especially when you
  308. // know the function or method may be called by a 3rd party app.
  309. // Typically, RIPs are used to indicate the caller passed in an invalid
  310. // parameter, so the problem is really not in the code itself.
  311. //
  312. // Do *not* use RIP() to verify successful API calls if the APIs can
  313. // legitimately fail due to low resources. For example, LocalAlloc can
  314. // legally fail, so you shouldn't assert that it will never fail.
  315. //
  316. // RIP performs a debugbreak only in the following processes:
  317. //
  318. // explore.exe
  319. // iexplore.exe
  320. // rundll32.exe
  321. // welcome.exe
  322. //
  323. // In any other process, this just spews the debug message, but doesn't stop.
  324. //
  325. // Setting the BF_RIP bit in g_dwBreakFlags will cause the macro to perform
  326. // a DebugBreak() even in non-shell processes.
  327. //
  328. // Default Behavior-
  329. // Retail builds: nothing
  330. // Debug builds: spew (other processes), spew and break (shell processes)
  331. // Full debug builds: spew (other processes), spew and break (shell processes)
  332. //
  333. #ifdef DEBUG
  334. BOOL CcshellRipA(LPCSTR pszFile, int line, LPCSTR pszEval, BOOL bBreakInside);
  335. BOOL CcshellRipW(LPCWSTR pszFile, int line, LPCWSTR pwszEval, BOOL bBreakInside);
  336. #ifdef UNICODE
  337. #define CcshellRip CcshellRipW
  338. #else
  339. #define CcshellRip CcshellRipA
  340. #endif
  341. #define RIP(f) \
  342. { \
  343. DEBUGTEXT(szFile, TEXT(__FILE__)); \
  344. if (!(f) && CcshellRip(szFile, __LINE__, TEXT(#f), FALSE)) \
  345. { \
  346. DEBUG_BREAK; \
  347. } \
  348. } \
  349. #define RIPMSG(f, msg) \
  350. { \
  351. DEBUGTEXT(szFile, TEXT(__FILE__)); \
  352. if (!(f) && CcshellRip(szFile, __LINE__, TEXT(#msg), FALSE)) \
  353. { \
  354. DEBUG_BREAK; \
  355. } \
  356. } \
  357. #else // DEBUG
  358. #define RIP(f)
  359. #define RIPMSG(f, msg)
  360. #endif // DEBUG
  361. // TraceMsg(dwMask, sz, args...)
  362. //
  363. // Generate wsprintf-formatted message using the specified trace dwMask.
  364. // dwMask may be one of the predefined bits:
  365. //
  366. // TF_ERROR - display "err <MODULE> <string>"
  367. // TF_WARNING - display "wn <MODULE> <string>"
  368. // TF_GENERAL - display "t <MODULE> <string>"
  369. // TF_ALWAYS - display "t <MODULE> <string>" regardless of g_dwTraceFlags.
  370. //
  371. // or it may be a custom bit (any of the upper 28 bits).
  372. //
  373. // The g_dwTraceFlags global governs whether the message is displayed (based
  374. // upon the dwMask parameter).
  375. //
  376. // The sz parameter is always ANSI; TraceMsg correctly converts it
  377. // to unicode if necessary. This is so you don't have to wrap your
  378. // debug strings with TEXT().
  379. //
  380. // In addition to squirting the trace message, you may optionally cause
  381. // the trace message to stop if you need to trace down the source of
  382. // an error. The BF_ONERRORMSG and BF_ONWARNMSG bits may be set in
  383. // g_dwBreakFlags to make TraceMsg stop when a TF_ERROR or TF_WARNING
  384. // message is displayed. But typically these bits are disabled.
  385. //
  386. // Default Behavior-
  387. // Retail builds: nothing
  388. // Debug builds: only TF_ALWAYS and TF_ERROR messages spew
  389. // Full debug builds: spew
  390. //
  391. #ifdef DEBUG
  392. void CDECL CcshellDebugMsgW(DWORD mask, LPCSTR pszMsg, ...);
  393. void CDECL CcshellDebugMsgA(DWORD mask, LPCSTR pszMsg, ...);
  394. void CDECL _DebugMsgA(DWORD flag, LPCSTR psz, ...);
  395. void CDECL _DebugMsgW(DWORD flag, LPCWSTR psz, ...);
  396. #ifdef UNICODE
  397. #define CcshellDebugMsg CcshellDebugMsgW
  398. #define _DebugMsg _DebugMsgW
  399. #else
  400. #define CcshellDebugMsg CcshellDebugMsgA
  401. #define _DebugMsg _DebugMsgA
  402. #endif
  403. #define TraceMsgW CcshellDebugMsgW
  404. #define TraceMsgA CcshellDebugMsgA
  405. #define TraceMsg CcshellDebugMsg
  406. // Use TraceMsg instead of DebugMsg. DebugMsg is obsolete.
  407. #ifdef DISALLOW_DebugMsg
  408. #define DebugMsg Dont_use_DebugMsg___Use_TraceMsg
  409. #else
  410. #define DebugMsg _DebugMsg
  411. #endif
  412. #else // DEBUG
  413. #define TraceMsgA 1 ? (void)0 : (void)
  414. #define TraceMsgW 1 ? (void)0 : (void)
  415. #define TraceMsg 1 ? (void)0 : (void)
  416. #define DebugMsg 1 ? (void)0 : (void)
  417. #endif // DEBUG
  418. // THR(pfn)
  419. // TBOOL(pfn)
  420. // TINT(pfn)
  421. // TPTR(pfn)
  422. // TW32(pfn)
  423. //
  424. // These macros are useful to trace failed calls to functions that return
  425. // HRESULTs, BOOLs, ints, or pointers. An example use of this is:
  426. //
  427. // {
  428. // ...
  429. // hres = THR(CoCreateInstance(CLSID_Bar, NULL, CLSCTX_INPROC_SERVER,
  430. // IID_IBar, (LPVOID*)&pbar));
  431. // if (SUCCEEDED(hres))
  432. // ...
  433. // }
  434. //
  435. // If CoCreateInstance failed, you would see spew similar to:
  436. //
  437. // err MODULE THR: Failure of "CoCreateInstance(CLSID_Bar, NULL, CLSCTX_INPROC_SERVER, IID_IBar, (LPVOID*)&pbar)" at foo.cpp, line 100 (0x80004005)
  438. //
  439. // THR keys off of the failure code of the hresult.
  440. // TBOOL considers FALSE to be a failure case.
  441. // TINT considers -1 to be a failure case.
  442. // TPTR considers NULL to be a failure case.
  443. // TW32 keys off the failure code of the Win32 error code.
  444. //
  445. // Set the BF_THR bit in g_dwBreakFlags to stop when these macros see a failure.
  446. //
  447. // Default Behavior-
  448. // Retail builds: nothing
  449. // Debug builds: nothing
  450. // Full debug builds: spew on error
  451. //
  452. #ifdef DEBUG
  453. EXTERN_C HRESULT TraceHR(HRESULT hrTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  454. EXTERN_C BOOL TraceBool(BOOL bTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  455. EXTERN_C int TraceInt(int iTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  456. EXTERN_C LPVOID TracePtr(LPVOID pvTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  457. EXTERN_C DWORD TraceWin32(DWORD dwTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  458. #define THR(x) (TraceHR((x), #x, __FILE__, __LINE__))
  459. #define TBOOL(x) (TraceBool((x), #x, __FILE__, __LINE__))
  460. #define TINT(x) (TraceInt((x), #x, __FILE__, __LINE__))
  461. #define TPTR(x) (TracePtr((x), #x, __FILE__, __LINE__))
  462. #define TW32(x) (TraceWin32((x), #x, __FILE__, __LINE__))
  463. #else // DEBUG
  464. #define THR(x) (x)
  465. #define TBOOL(x) (x)
  466. #define TINT(x) (x)
  467. #define TPTR(x) (x)
  468. #define TW32(x) (x)
  469. #endif // DEBUG
  470. // DBEXEC(flg, expr)
  471. //
  472. // under DEBUG, does "if (flg) expr;" (w/ the usual safe syntax)
  473. // under !DEBUG, does nothing (and does not evaluate either of its args)
  474. //
  475. #ifdef DEBUG
  476. #define DBEXEC(flg, expr) ((flg) ? (expr) : 0)
  477. #else // DEBUG
  478. #define DBEXEC(flg, expr) /*NOTHING*/
  479. #endif // DEBUG
  480. // string and buffer whacking functions
  481. //
  482. #ifdef DEBUG
  483. EXTERN_C void DEBUGWhackPathBufferA(LPSTR psz, UINT cch);
  484. EXTERN_C void DEBUGWhackPathBufferW(LPWSTR psz, UINT cch);
  485. EXTERN_C void DEBUGWhackPathStringA(LPSTR psz, UINT cch);
  486. EXTERN_C void DEBUGWhackPathStringW(LPWSTR psz, UINT cch);
  487. #ifdef UNICODE
  488. #define DEBUGWhackPathBuffer DEBUGWhackPathBufferW
  489. #define DEBUGWhackPathString DEBUGWhackPathStringW
  490. #else
  491. #define DEBUGWhackPathBuffer DEBUGWhackPathBufferA
  492. #define DEBUGWhackPathString DEBUGWhackPathStringA
  493. #endif
  494. #else // DEBUG
  495. #define DEBUGWhackPathBuffer(psz, cch)
  496. #define DEBUGWhackPathString(psz, cch)
  497. #endif // DEBUG
  498. // Some trickery to map ATL debug macros to ours, so ATL code that stops
  499. // or spews in our code will look like the rest of our squirties.
  500. #ifdef DEBUG
  501. #ifdef _ATL_NO_DEBUG_CRT
  502. // ATL uses _ASSERTE. Map it to ours.
  503. #define _ASSERTE(f) ASSERT(f)
  504. // We map ATLTRACE macros to our functions
  505. void _cdecl ShellAtlTraceA(LPCSTR lpszFormat, ...);
  506. void _cdecl ShellAtlTraceW(LPCWSTR lpszFormat, ...);
  507. #ifdef UNICODE
  508. #define ShellAtlTrace ShellAtlTraceW
  509. #else
  510. #define ShellAtlTrace ShellAtlTraceA
  511. #endif
  512. #define ATLTRACE ShellAtlTrace
  513. #endif
  514. #else // DEBUG
  515. #ifdef _ATL_NO_DEBUG_CRT
  516. // ATL uses _ASSERTE. Map it to ours.
  517. #define _ASSERTE(f)
  518. // We map ATLTRACE macros to our functions
  519. #define ATLTRACE 1 ? (void)0 : (void)
  520. #endif
  521. #endif // DEBUG
  522. // ------ Stay away from these macros below ----------
  523. // Issue (scotth): remove these by 8/15/98. They should not be used anymore.
  524. #ifdef DEBUG
  525. #define AssertE(f) ASSERT(f)
  526. #define AssertMsg _AssertMsg
  527. #define AssertStrLen _AssertStrLen
  528. #define AssertStrLenA _AssertStrLenA
  529. #define AssertStrLenW _AssertStrLenW
  530. #ifdef FULL_DEBUG
  531. #define FullDebugMsg _DebugMsg
  532. #else
  533. #define FullDebugMsg 1 ? (void)0 : (void)
  534. #endif
  535. #define ASSERT_MSGW CcshellAssertMsgW
  536. #define ASSERT_MSGA CcshellAssertMsgA
  537. #define ASSERT_MSG CcshellAssertMsg
  538. #else // DEBUG
  539. #define AssertE(f) (f)
  540. #define AssertMsg 1 ? (void)0 : (void)
  541. #define AssertStrLen(lpStr, iLen)
  542. #define FullDebugMsg 1 ? (void)0 : (void)
  543. #define ASSERT_MSGA 1 ? (void)0 : (void)
  544. #define ASSERT_MSGW 1 ? (void)0 : (void)
  545. #define ASSERT_MSG 1 ? (void)0 : (void)
  546. #endif // DEBUG
  547. // ------ Stay away from these macros above ----------
  548. // It's necessary to find when classes that were designed to be single threaded are used
  549. // across threads so they can be fixed to be multithreaded. These asserts will point
  550. // out such cases.
  551. #ifdef DEBUG
  552. #define ASSERT_SINGLE_THREADED AssertMsg(_dwThreadIDForSingleThreadedAssert == GetCurrentThreadId(), TEXT("MULTI-THREADED BUG: This class is being used by more than one thread, but it's not thread safe."))
  553. #define INIT_SINGLE_THREADED_ASSERT _dwThreadIDForSingleThreadedAssert = GetCurrentThreadId();
  554. #define SINGLE_THREADED_MEMBER_VARIABLE DWORD _dwThreadIDForSingleThreadedAssert;
  555. #else // DEBUG
  556. #define ASSERT_SINGLE_THREADED NULL;
  557. #define INIT_SINGLE_THREADED_ASSERT NULL;
  558. #define SINGLE_THREADED_MEMBER_VARIABLE
  559. #endif // DEBUG
  560. #ifdef DEBUG
  561. #define Dbg_SafeStrA(psz) (SAFECAST(psz, LPCSTR), (psz) ? (psz) : "NULL string")
  562. #define Dbg_SafeStrW(psz) (SAFECAST(psz, LPCWSTR), (psz) ? (psz) : L"NULL string")
  563. #ifdef UNICODE
  564. #define Dbg_SafeStr Dbg_SafeStrW
  565. #else
  566. #define Dbg_SafeStr Dbg_SafeStrA
  567. #endif
  568. #define FUNC_MSG CcshellFuncMsg
  569. // Helpful macro for mapping manifest constants to strings. Assumes
  570. // return string is pcsz. You can use this macro in this fashion:
  571. //
  572. // LPCSTR Dbg_GetFoo(FOO foo)
  573. // {
  574. // LPCTSTR pcsz = TEXT("Unknown <foo>");
  575. // switch (foo)
  576. // {
  577. // STRING_CASE(FOOVALUE1);
  578. // STRING_CASE(FOOVALUE2);
  579. // ...
  580. // }
  581. // return pcsz;
  582. // }
  583. //
  584. #define STRING_CASE(val) case val: pcsz = TEXT(#val); break
  585. // Debug function enter
  586. // DBG_ENTER(flag, fn) -- Generates a function entry debug spew for
  587. // a function
  588. //
  589. #define DBG_ENTER(flagFTF, fn) \
  590. (FUNC_MSG(flagFTF, " > " #fn "()"), \
  591. CcshellStackEnter())
  592. // DBG_ENTER_TYPE(flag, fn, dw, pfnStrFromType) -- Generates a function entry debug
  593. // spew for functions that accept <type>.
  594. //
  595. #define DBG_ENTER_TYPE(flagFTF, fn, dw, pfnStrFromType) \
  596. (FUNC_MSG(flagFTF, " < " #fn "(..., %s, ...)", (LPCTSTR)pfnStrFromType(dw)), \
  597. CcshellStackEnter())
  598. // DBG_ENTER_SZ(flag, fn, sz) -- Generates a function entry debug spew for
  599. // a function that accepts a string as one of its
  600. // parameters.
  601. //
  602. #define DBG_ENTER_SZ(flagFTF, fn, sz) \
  603. (FUNC_MSG(flagFTF, " > " #fn "(..., \"%s\",...)", Dbg_SafeStr(sz)), \
  604. CcshellStackEnter())
  605. // Debug function exit
  606. // DBG_EXIT(flag, fn) -- Generates a function exit debug spew
  607. //
  608. #define DBG_EXIT(flagFTF, fn) \
  609. (CcshellStackLeave(), \
  610. FUNC_MSG(flagFTF, " < " #fn "()"))
  611. // DBG_EXIT_TYPE(flag, fn, dw, pfnStrFromType) -- Generates a function exit debug
  612. // spew for functions that return <type>.
  613. //
  614. #define DBG_EXIT_TYPE(flagFTF, fn, dw, pfnStrFromType) \
  615. (CcshellStackLeave(), \
  616. FUNC_MSG(flagFTF, " < " #fn "() with %s", (LPCTSTR)pfnStrFromType(dw)))
  617. // DBG_EXIT_INT(flag, fn, us) -- Generates a function exit debug spew for
  618. // functions that return an INT.
  619. //
  620. #define DBG_EXIT_INT(flagFTF, fn, n) \
  621. (CcshellStackLeave(), \
  622. FUNC_MSG(flagFTF, " < " #fn "() with %d", (int)(n)))
  623. // DBG_EXIT_BOOL(flag, fn, b) -- Generates a function exit debug spew for
  624. // functions that return a boolean.
  625. //
  626. #define DBG_EXIT_BOOL(flagFTF, fn, b) \
  627. (CcshellStackLeave(), \
  628. FUNC_MSG(flagFTF, " < " #fn "() with %s", (b) ? (LPTSTR)TEXT("TRUE") : (LPTSTR)TEXT("FALSE")))
  629. // DBG_EXIT_UL(flag, fn, ul) -- Generates a function exit debug spew for
  630. // functions that return a ULONG.
  631. //
  632. #ifdef _WIN64
  633. #define DBG_EXIT_UL(flagFTF, fn, ul) \
  634. (CcshellStackLeave(), \
  635. FUNC_MSG(flagFTF, " < " #fn "() with %#016I64x", (ULONG_PTR)(ul)))
  636. #else
  637. #define DBG_EXIT_UL(flagFTF, fn, ul) \
  638. (CcshellStackLeave(), \
  639. FUNC_MSG(flagFTF, " < " #fn "() with %#08lx", (ULONG)(ul)))
  640. #endif // _WIN64
  641. #define DBG_EXIT_DWORD DBG_EXIT_UL
  642. // DBG_EXIT_HRES(flag, fn, hres) -- Generates a function exit debug spew for
  643. // functions that return an HRESULT.
  644. //
  645. #define DBG_EXIT_HRES(flagFTF, fn, hres) DBG_EXIT_TYPE(flagFTF, fn, hres, Dbg_GetHRESULTName)
  646. #else // DEBUG
  647. #define Dbg_SafeStr 1 ? (void)0 : (void)
  648. #define FUNC_MSG 1 ? (void)0 : (void)
  649. #define DBG_ENTER(flagFTF, fn)
  650. #define DBG_ENTER_TYPE(flagFTF, fn, dw, pfn)
  651. #define DBG_ENTER_SZ(flagFTF, fn, sz)
  652. #define DBG_EXIT(flagFTF, fn)
  653. #define DBG_EXIT_INT(flagFTF, fn, n)
  654. #define DBG_EXIT_BOOL(flagFTF, fn, b)
  655. #define DBG_EXIT_UL(flagFTF, fn, ul)
  656. #define DBG_EXIT_DWORD DBG_EXIT_UL
  657. #define DBG_EXIT_TYPE(flagFTF, fn, dw, pfn)
  658. #define DBG_EXIT_HRES(flagFTF, fn, hres)
  659. #endif // DEBUG
  660. // COMPILETIME_ASSERT(f)
  661. //
  662. // Generates a build break at compile time if the constant expression
  663. // is not true. Unlike the "#if" compile-time directive, the expression
  664. // in COMPILETIME_ASSERT() is allowed to use "sizeof".
  665. //
  666. // Compiler magic! If the expression "f" is FALSE, then you get the
  667. // compiler error "Duplicate case expression in switch statement".
  668. //
  669. #define COMPILETIME_ASSERT(f) switch (0) case 0: case f:
  670. #endif // NOSHELLDEBUG
  671. //
  672. // Debug dump helper functions
  673. //
  674. #ifdef DEBUG
  675. LPCTSTR Dbg_GetCFName(UINT ucf);
  676. LPCTSTR Dbg_GetHRESULTName(HRESULT hr);
  677. LPCTSTR Dbg_GetREFIIDName(REFIID riid);
  678. LPCTSTR Dbg_GetVTName(VARTYPE vt);
  679. #else
  680. #define Dbg_GetCFName(ucf) (void)0
  681. #define Dbg_GetHRESULTName(hr) (void)0
  682. #define Dbg_GetREFIIDName(riid) (void)0
  683. #define Dbg_GetVTName(vt) (void)0
  684. #endif // DEBUG
  685. // I'm a lazy typist...
  686. #define Dbg_GetHRESULT Dbg_GetHRESULTName
  687. // Parameter validation macros
  688. #include "validate.h"
  689. #endif // DECLARE_DEBUG
  690. #ifdef PRODUCT_PROF
  691. int __stdcall StartCAP(void); // start profiling
  692. int __stdcall StopCAP(void); // stop profiling until StartCAP
  693. int __stdcall SuspendCAP(void); // suspend profiling until ResumeCAP
  694. int __stdcall ResumeCAP(void); // resume profiling
  695. int __stdcall StartCAPAll(void); // process-wide start profiling
  696. int __stdcall StopCAPAll(void); // process-wide stop profiling
  697. int __stdcall SuspendCAPAll(void); // process-wide suspend profiling
  698. int __stdcall ResumeCAPAll(void); // process-wide resume profiling
  699. void __stdcall MarkCAP(long lMark); // write mark to MEA
  700. extern DWORD g_dwProfileCAP;
  701. #else
  702. #define StartCAP() 0
  703. #define StopCAP() 0
  704. #define SuspendCAP() 0
  705. #define ResumeCAP() 0
  706. #define StartCAPAll() 0
  707. #define StopCAPAll() 0
  708. #define SuspendCAPAll() 0
  709. #define ResumeCAPAll() 0
  710. #define MarkCAP(n) 0
  711. #define g_dwProfileCAP 0
  712. #endif
  713. #ifdef __cplusplus
  714. };
  715. #endif