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.

900 lines
30 KiB

  1. //====== Assertion/Debug output APIs =================================
  2. #include <platform.h> // for __endexcept
  3. #pragma warning (disable:4096) // '__cdecl' must be used with '...'
  4. #pragma warning (disable:4201) // nonstandard extension used : nameless struct/union
  5. #pragma warning (disable:4115) // named type definition in parentheses
  6. #if defined(DECLARE_DEBUG) && defined(DEBUG)
  7. //
  8. // Declare module-specific debug strings
  9. //
  10. // When including this header in your private header file, do not
  11. // define DECLARE_DEBUG. But do define DECLARE_DEBUG in one of the
  12. // source files in your project, and then include this header file.
  13. //
  14. // You may also define the following:
  15. //
  16. // SZ_DEBUGINI - the .ini file used to set debug flags
  17. // SZ_DEBUGSECTION - the section in the .ini file specific to
  18. // the module component.
  19. // SZ_MODULE - ansi version of the name of your module.
  20. //
  21. //
  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. EXTERN_C const WCHAR FAR c_wszTrace[] = L"t " TEXTW(SZ_MODULE) L" ";
  26. EXTERN_C const WCHAR FAR c_wszErrorDbg[] = L"err " TEXTW(SZ_MODULE) L" ";
  27. EXTERN_C const WCHAR FAR c_wszWarningDbg[] = L"wn " TEXTW(SZ_MODULE) L" ";
  28. EXTERN_C const WCHAR FAR c_wszAssertMsg[] = TEXTW(SZ_MODULE) L" Assert: ";
  29. EXTERN_C const WCHAR FAR c_wszAssertFailed[] = TEXTW(SZ_MODULE) L" Assert %ls, line %d: (%ls)\r\n";
  30. EXTERN_C const WCHAR FAR c_wszRip[] = TEXTW(SZ_MODULE) L" RIP in %s at %s, line %d: (%s)\r\n";
  31. EXTERN_C const WCHAR FAR c_wszRipNoFn[] = TEXTW(SZ_MODULE) L" RIP at %s, line %d: (%s)\r\n";
  32. // (These are deliberately CHAR)
  33. EXTERN_C const CHAR FAR c_szTrace[] = "t " SZ_MODULE " ";
  34. EXTERN_C const CHAR FAR c_szErrorDbg[] = "err " SZ_MODULE " ";
  35. EXTERN_C const CHAR FAR c_szWarningDbg[] = "wn " SZ_MODULE " ";
  36. EXTERN_C const CHAR FAR c_szAssertMsg[] = SZ_MODULE " Assert: ";
  37. EXTERN_C const CHAR FAR c_szAssertFailed[] = SZ_MODULE " Assert %s, line %d: (%s)\r\n";
  38. EXTERN_C const CHAR FAR c_szRip[] = SZ_MODULE " RIP in %s at %s, line %d: (%s)\r\n";
  39. EXTERN_C const CHAR FAR c_szRipNoFn[] = SZ_MODULE " RIP at %s, line %d: (%s)\r\n";
  40. EXTERN_C const CHAR FAR c_szRipMsg[] = SZ_MODULE " RIP: ";
  41. #endif // DECLARE_DEBUG && DEBUG
  42. #if defined(DECLARE_DEBUG) && defined(PRODUCT_PROF)
  43. EXTERN_C const CHAR FAR c_szCcshellIniFile[] = SZ_DEBUGINI;
  44. EXTERN_C const CHAR FAR c_szCcshellIniSecDebug[] = SZ_DEBUGSECTION;
  45. #endif
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #if !defined(DECLARE_DEBUG)
  50. //
  51. // Debug macros and validation code
  52. //
  53. #if !defined(UNIX) || (defined(UNIX) && !defined(NOSHELLDEBUG))
  54. // Undefine the macros that we define in case some other header
  55. // might have tried defining these commonly-named macros.
  56. #undef Assert
  57. #undef AssertE
  58. #undef AssertMsg
  59. #undef AssertStrLen
  60. #undef DebugMsg
  61. #undef FullDebugMsg
  62. #undef ASSERT
  63. #undef EVAL
  64. #undef ASSERTMSG // catch people's typos
  65. #undef DBEXEC
  66. #ifdef _ATL_NO_DEBUG_CRT
  67. #undef _ASSERTE // we substitute this ATL macro
  68. #endif
  69. #endif // !UNIX
  70. // Access these globals to determine which debug flags are set.
  71. // These globals are modified by CcshellGetDebugFlags(), which
  72. // reads an .ini file and sets the appropriate flags.
  73. //
  74. // g_dwDumpFlags - bits are application specific. Typically
  75. // used for dumping structures.
  76. // g_dwBreakFlags - uses BF_* flags. The remaining bits are
  77. // application specific. Used to determine
  78. // when to break into the debugger.
  79. // g_qwTraceFlags - uses TF_* flags. The remaining bits are
  80. // application specific. Used to display
  81. // debug trace messages.
  82. // g_dwFuncTraceFlags - bits are application specific. When
  83. // TF_FUNC is set, CcshellFuncMsg uses this
  84. // value to determine which function traces
  85. // to display.
  86. // g_dwProtoype - bits are application specific. Use it for
  87. // anything.
  88. // g_dwProfileCAP - bits are application specific. Used to
  89. // control ICECAP profiling.
  90. //
  91. extern DWORD g_dwDumpFlags;
  92. extern DWORD g_dwBreakFlags;
  93. extern ULONGLONG g_qwTraceFlags;
  94. #ifdef DEBUG
  95. extern DWORD g_dwPrototype;
  96. #else
  97. #define g_dwPrototype 0
  98. #endif
  99. extern DWORD g_dwFuncTraceFlags;
  100. #if defined(DEBUG) || defined(PRODUCT_PROF)
  101. BOOL CcshellGetDebugFlags(void);
  102. #else
  103. #define CcshellGetDebugFlags() 0
  104. #endif
  105. // Break flags for g_dwBreakFlags
  106. #define BF_ASSERT 0x00000001 // Break on assertions
  107. #define BF_ONAPIENTER 0x00000002 // Break on entering an API
  108. #define BF_ONERRORMSG 0x00000004 // Break on TF_ERROR
  109. #define BF_ONWARNMSG 0x00000008 // Break on TF_WARNING
  110. #define BF_THR 0x00000100 // Break when THR() receives a failure
  111. #define BF_RIP 0x00000200 // Break on RIPs
  112. #define BF_LEAKS 0x80000000 // Break on detecting a leak
  113. // Trace flags for g_qwTraceFlags
  114. #define TF_ALWAYS 0xFFFFFFFFFFFFFFFF
  115. #define TF_NEVER 0x00000000
  116. #define TF_WARNING 0x00000001
  117. #define TF_ERROR 0x00000002
  118. #define TF_GENERAL 0x00000004 // Standard messages
  119. #define TF_FUNC 0x00000008 // Trace function calls
  120. #define TF_ATL 0x00000008 // Since TF_FUNC is so-little used, I'm overloading this bit
  121. #define TF_MEMUSAGE 0x0000000100000000
  122. #define TF_KEEP_ALLOCATION_STACKS 0x0000000200000000
  123. // (Upper 28 bits reserved for custom use per-module)
  124. #define TF_CUSTOM1 0x40000000 // Custom messages #1
  125. #define TF_CUSTOM2 0x80000000 // Custom messages #2
  126. // Old, archaic debug flags.
  127. // APPCOMPAT (scotth): the following flags will be phased out over time.
  128. #ifdef DM_TRACE
  129. #undef DM_TRACE
  130. #undef DM_WARNING
  131. #undef DM_ERROR
  132. #endif
  133. #define DM_TRACE TF_GENERAL // OBSOLETE Trace messages
  134. #define DM_WARNING TF_WARNING // OBSOLETE Warning
  135. #define DM_ERROR TF_ERROR // OBSOLETE Error
  136. // Use this macro to declare message text that will be placed
  137. // in the CODE segment (useful if DS is getting full)
  138. //
  139. // Ex: DEBUGTEXT(szMsg, "Invalid whatever: %d");
  140. //
  141. #define DEBUGTEXT(sz, msg) /* ;Internal */ \
  142. static const TCHAR sz[] = msg
  143. #ifndef NOSHELLDEBUG // Others have own versions of these.
  144. #ifdef DEBUG
  145. #ifdef _X86_
  146. // Use int 3 so we stop immediately in the source
  147. #define DEBUG_BREAK \
  148. { \
  149. static BOOL gAlwaysAssert = FALSE; \
  150. do \
  151. { \
  152. _try \
  153. { \
  154. _asm int 3 \
  155. } \
  156. _except (EXCEPTION_EXECUTE_HANDLER) \
  157. { \
  158. } \
  159. } while (gAlwaysAssert); \
  160. }
  161. #else
  162. #define DEBUG_BREAK \
  163. { \
  164. static BOOL gAlwaysAssert = FALSE; \
  165. do \
  166. { \
  167. _try \
  168. { \
  169. DebugBreak(); \
  170. } \
  171. _except (EXCEPTION_EXECUTE_HANDLER) \
  172. { \
  173. } \
  174. __endexcept \
  175. } while (gAlwaysAssert); \
  176. }
  177. #endif
  178. // Prototypes for debug functions
  179. void CcshellStackEnter(void);
  180. void CcshellStackLeave(void);
  181. void CDECL CcshellFuncMsgW(ULONGLONG mask, LPCSTR pszMsg, ...);
  182. void CDECL CcshellFuncMsgA(ULONGLONG mask, LPCSTR pszMsg, ...);
  183. void CDECL _AssertMsgA(BOOL f, LPCSTR pszMsg, ...);
  184. void CDECL _AssertMsgW(BOOL f, LPCWSTR pszMsg, ...);
  185. void _AssertStrLenA(LPCSTR pszStr, int iLen);
  186. void _AssertStrLenW(LPCWSTR pwzStr, int iLen);
  187. #ifdef UNICODE
  188. #define CcshellFuncMsg CcshellFuncMsgW
  189. #define CcshellAssertMsg CcshellAssertMsgW
  190. #define _AssertMsg _AssertMsgW
  191. #define _AssertStrLen _AssertStrLenW
  192. #else
  193. #define CcshellFuncMsg CcshellFuncMsgA
  194. #define CcshellAssertMsg CcshellAssertMsgA
  195. #define _AssertMsg _AssertMsgA
  196. #define _AssertStrLen _AssertStrLenA
  197. #endif
  198. #endif // DEBUG
  199. // ASSERT(f)
  200. //
  201. // Generates a "Assert file.c, line x (eval)" message if f is NOT true.
  202. //
  203. // Use ASSERT() to check for logic invariance. These are typically considered
  204. // fatal problems, and falls into the 'this should never ever happen'
  205. // category.
  206. //
  207. // Do *not* use ASSERT() to verify successful API calls if the APIs can
  208. // legitimately fail due to low resources. For example, LocalAlloc can
  209. // legally fail, so you shouldn't assert that it will never fail.
  210. //
  211. // The BF_ASSERT bit in g_dwBreakFlags governs whether the function
  212. // performs a DebugBreak().
  213. //
  214. // Default Behavior-
  215. // Retail builds: nothing
  216. // Debug builds: spew and break
  217. // Full debug builds: spew and break
  218. //
  219. #ifdef DEBUG
  220. BOOL CcshellAssertFailedA(LPCSTR szFile, int line, LPCSTR pszEval, BOOL bBreakInside);
  221. BOOL CcshellAssertFailedW(LPCWSTR szFile, int line, LPCWSTR pwszEval, BOOL bBreakInside);
  222. #ifdef UNICODE
  223. #define CcshellAssertFailed CcshellAssertFailedW
  224. #else
  225. #define CcshellAssertFailed CcshellAssertFailedA
  226. #endif
  227. #define ASSERT(f) \
  228. { \
  229. DEBUGTEXT(szFile, TEXT(__FILE__)); \
  230. if (!(f) && CcshellAssertFailed(szFile, __LINE__, TEXT(#f), FALSE)) \
  231. DEBUG_BREAK; \
  232. }
  233. // The old Win95 code used to use "Assert()". We discourage the use
  234. // of this macro now because it is not msdev-friendly.
  235. #ifdef DISALLOW_Assert
  236. #define Assert(f) Dont_use_Assert___Use_ASSERT
  237. #else
  238. #define Assert(f) ASSERT(f)
  239. #endif
  240. #else // DEBUG
  241. #define ASSERT(f)
  242. #define Assert(f)
  243. #endif // DEBUG
  244. // ASSERTMSG(f, szFmt, args...)
  245. //
  246. // Behaves like ASSERT, except it prints the wsprintf-formatted message
  247. // instead of the file and line number.
  248. //
  249. // The sz parameter is always ANSI; AssertMsg correctly converts it
  250. // to unicode if necessary. This is so you don't have to wrap your
  251. // debug strings with TEXT().
  252. //
  253. // The BF_ASSERT bit in g_dwBreakFlags governs whether the function
  254. // performs a DebugBreak().
  255. //
  256. // Default Behavior-
  257. // Retail builds: nothing
  258. // Debug builds: spew and break
  259. // Full debug builds: spew and break
  260. //
  261. #ifdef DEBUG
  262. void CDECL CcshellAssertMsgW(BOOL bAssert, LPCSTR pszMsg, ...);
  263. void CDECL CcshellAssertMsgA(BOOL bAssert, LPCSTR pszMsg, ...);
  264. #ifdef UNICODE
  265. #define CcshellAssertMsg CcshellAssertMsgW
  266. #else
  267. #define CcshellAssertMsg CcshellAssertMsgA
  268. #endif
  269. #define ASSERTMSG CcshellAssertMsg
  270. #else // DEBUG
  271. #define ASSERTMSG 1 ? (void)0 : (void)
  272. #endif // DEBUG
  273. // EVAL(f)
  274. //
  275. // Behaves like ASSERT(). Evaluates the expression (f). The expression
  276. // is always evaluated, even in retail builds. But the macro only asserts
  277. // in the debug build. This macro may be used on logical expressions, eg:
  278. //
  279. // if (EVAL(exp))
  280. // // do something
  281. //
  282. // Do *not* use EVAL() to verify successful API calls if the APIs can
  283. // legitimately fail due to low resources. For example, LocalAlloc can
  284. // legally fail, so you shouldn't assert that it will never fail.
  285. //
  286. // The BF_ASSERT bit in g_dwBreakFlags governs whether the function
  287. // performs a DebugBreak().
  288. //
  289. // Default Behavior-
  290. // Retail builds: nothing
  291. // Debug builds: spew and break
  292. // Full debug builds: spew and break
  293. //
  294. #ifdef DEBUG
  295. #define EVAL(exp) \
  296. ((exp) || (CcshellAssertFailed(TEXT(__FILE__), __LINE__, TEXT(#exp), TRUE), 0))
  297. #else // DEBUG
  298. #define EVAL(exp) ((exp) != 0)
  299. #endif // DEBUG
  300. // RIP(f)
  301. //
  302. // Generates a "RIP at file.c, line x (eval)" message if f is NOT true.
  303. //
  304. // Use RIP() to perform parameter validation, especially when you
  305. // know the function or method may be called by a 3rd party app.
  306. // Typically, RIPs are used to indicate the caller passed in an invalid
  307. // parameter, so the problem is really not in the code itself.
  308. //
  309. // Do *not* use RIP() to verify successful API calls if the APIs can
  310. // legitimately fail due to low resources. For example, LocalAlloc can
  311. // legally fail, so you shouldn't assert that it will never fail.
  312. //
  313. // RIP performs a debugbreak only in the following processes:
  314. //
  315. // explore.exe
  316. // iexplore.exe
  317. // rundll32.exe
  318. // welcome.exe
  319. //
  320. // In any other process, this just spews the debug message, but doesn't stop.
  321. //
  322. // Setting the BF_RIP bit in g_dwBreakFlags will cause the macro to perform
  323. // a DebugBreak() even in non-shell processes.
  324. //
  325. // Default Behavior-
  326. // Retail builds: nothing
  327. // Debug builds: spew (other processes), spew and break (shell processes)
  328. // Full debug builds: spew (other processes), spew and break (shell processes)
  329. //
  330. #ifdef DEBUG
  331. BOOL CcshellRipA(LPCSTR pszFile, int line, LPCSTR pszEval, BOOL bBreakInside);
  332. BOOL CcshellRipW(LPCWSTR pszFile, int line, LPCWSTR pwszEval, BOOL bBreakInside);
  333. BOOL CDECL CcshellRipMsgA(BOOL bRip, LPCSTR pszMsg, ...);
  334. BOOL CDECL CcshellRipMsgW(BOOL bRip, LPCSTR pszMsg, ...);
  335. #ifdef UNICODE
  336. #define CcshellRip CcshellRipW
  337. #define CcshellRipMsg CcshellRipMsgW
  338. #else
  339. #define CcshellRip CcshellRipA
  340. #define CcshellRipMsg CcshellRipMsgA
  341. #endif
  342. #define RIP(f) \
  343. { \
  344. DEBUGTEXT(szFile, TEXT(__FILE__)); \
  345. if (!(f) && CcshellRip(szFile, __LINE__, TEXT(#f), FALSE)) \
  346. { \
  347. DEBUG_BREAK; \
  348. } \
  349. } \
  350. #define RIPMSG CcshellRipMsg
  351. #else // DEBUG
  352. #define RIP(f)
  353. #define RIPMSG 1 ? (void)0 : (void)
  354. #endif // DEBUG
  355. // TraceMsg(dwMask, sz, args...)
  356. //
  357. // Generate wsprintf-formatted message using the specified trace dwMask.
  358. // dwMask may be one of the predefined bits:
  359. //
  360. // TF_ERROR - display "err <MODULE> <string>"
  361. // TF_WARNING - display "wn <MODULE> <string>"
  362. // TF_GENERAL - display "t <MODULE> <string>"
  363. // TF_ALWAYS - display "t <MODULE> <string>" regardless of g_qwTraceFlags.
  364. //
  365. // or it may be a custom bit (any of the upper 28 bits).
  366. //
  367. // The g_qwTraceFlags global governs whether the message is displayed (based
  368. // upon the dwMask parameter).
  369. //
  370. // The sz parameter is always ANSI; TraceMsg correctly converts it
  371. // to unicode if necessary. This is so you don't have to wrap your
  372. // debug strings with TEXT().
  373. //
  374. // In addition to squirting the trace message, you may optionally cause
  375. // the trace message to stop if you need to trace down the source of
  376. // an error. The BF_ONERRORMSG and BF_ONWARNMSG bits may be set in
  377. // g_dwBreakFlags to make TraceMsg stop when a TF_ERROR or TF_WARNING
  378. // message is displayed. But typically these bits are disabled.
  379. //
  380. // Default Behavior-
  381. // Retail builds: nothing
  382. // Debug builds: only TF_ALWAYS and TF_ERROR messages spew
  383. // Full debug builds: spew
  384. //
  385. #ifdef DEBUG
  386. UINT GetStack(UINT nDepth, CHAR *szBuffer, UINT nBufferLength);
  387. void CDECL CcshellDebugMsgW(ULONGLONG mask, LPCSTR pszMsg, ...);
  388. void CDECL CcshellDebugMsgA(ULONGLONG mask, LPCSTR pszMsg, ...);
  389. void CDECL _DebugMsgA(ULONGLONG flag, LPCSTR psz, ...);
  390. void CDECL _DebugMsgW(ULONGLONG flag, LPCWSTR psz, ...);
  391. #ifdef UNICODE
  392. #define CcshellDebugMsg CcshellDebugMsgW
  393. #define _DebugMsg _DebugMsgW
  394. #else
  395. #define CcshellDebugMsg CcshellDebugMsgA
  396. #define _DebugMsg _DebugMsgA
  397. #endif
  398. #define TraceMsgW CcshellDebugMsgW
  399. #define TraceMsgA CcshellDebugMsgA
  400. #define TraceMsg CcshellDebugMsg
  401. // Use TraceMsg instead of DebugMsg. DebugMsg is obsolete.
  402. #ifdef DISALLOW_DebugMsg
  403. #define DebugMsg Dont_use_DebugMsg___Use_TraceMsg
  404. #else
  405. #define DebugMsg _DebugMsg
  406. #endif
  407. #else // DEBUG
  408. #define TraceMsgA 1 ? (void)0 : (void)
  409. #define TraceMsgW 1 ? (void)0 : (void)
  410. #define TraceMsg 1 ? (void)0 : (void)
  411. #define DebugMsg 1 ? (void)0 : (void)
  412. #endif // DEBUG
  413. // THR(pfn)
  414. // TBOOL(pfn)
  415. // TINT(pfn)
  416. // TPTR(pfn)
  417. // TW32(pfn)
  418. //
  419. // These macros are useful to trace failed calls to functions that return
  420. // HRESULTs, BOOLs, ints, or pointers. An example use of this is:
  421. //
  422. // {
  423. // ...
  424. // hres = THR(CoCreateInstance(CLSID_Bar, NULL, CLSCTX_INPROC_SERVER,
  425. // IID_IBar, (LPVOID*)&pbar));
  426. // if (SUCCEEDED(hres))
  427. // ...
  428. // }
  429. //
  430. // If CoCreateInstance failed, you would see spew similar to:
  431. //
  432. // err MODULE THR: Failure of "CoCreateInstance(CLSID_Bar, NULL, CLSCTX_INPROC_SERVER, IID_IBar, (LPVOID*)&pbar)" at foo.cpp, line 100 (0x80004005)
  433. //
  434. // THR keys off of the failure code of the hresult.
  435. // TBOOL considers FALSE to be a failure case.
  436. // TINT considers -1 to be a failure case.
  437. // TPTR considers NULL to be a failure case.
  438. // TW32 keys off the failure code of the Win32 error code.
  439. //
  440. // Set the BF_THR bit in g_dwBreakFlags to stop when these macros see a failure.
  441. //
  442. // Default Behavior-
  443. // Retail builds: nothing
  444. // Debug builds: nothing
  445. // Full debug builds: spew on error
  446. //
  447. #ifdef DEBUG
  448. EXTERN_C HRESULT TraceHR(HRESULT hrTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  449. EXTERN_C BOOL TraceBool(BOOL bTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  450. EXTERN_C int TraceInt(int iTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  451. EXTERN_C LPVOID TracePtr(LPVOID pvTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  452. EXTERN_C DWORD TraceWin32(DWORD dwTest, LPCSTR pszExpr, LPCSTR pszFile, int iLine);
  453. #define THR(x) (TraceHR((x), #x, __FILE__, __LINE__))
  454. #define TBOOL(x) (TraceBool((x), #x, __FILE__, __LINE__))
  455. #define TINT(x) (TraceInt((x), #x, __FILE__, __LINE__))
  456. #define TPTR(x) (TracePtr((x), #x, __FILE__, __LINE__))
  457. #define TW32(x) (TraceWin32((x), #x, __FILE__, __LINE__))
  458. #else // DEBUG
  459. #define THR(x) (x)
  460. #define TBOOL(x) (x)
  461. #define TINT(x) (x)
  462. #define TPTR(x) (x)
  463. #define TW32(x) (x)
  464. #endif // DEBUG
  465. // DBEXEC(flg, expr)
  466. //
  467. // under DEBUG, does "if (flg) expr;" (w/ the usual safe syntax)
  468. // under !DEBUG, does nothing (and does not evaluate either of its args)
  469. //
  470. #ifdef DEBUG
  471. #define DBEXEC(flg, expr) ((flg) ? (expr) : 0)
  472. #else // DEBUG
  473. #define DBEXEC(flg, expr) /*NOTHING*/
  474. #endif // DEBUG
  475. // string and buffer whacking functions
  476. //
  477. #ifdef DEBUG
  478. EXTERN_C void DEBUGWhackPathBufferA(LPSTR psz, UINT cch);
  479. EXTERN_C void DEBUGWhackPathBufferW(LPWSTR psz, UINT cch);
  480. EXTERN_C void DEBUGWhackPathStringA(LPSTR psz, UINT cch);
  481. EXTERN_C void DEBUGWhackPathStringW(LPWSTR psz, UINT cch);
  482. #else // DEBUG
  483. #define DEBUGWhackPathBufferA(psz, cch)
  484. #define DEBUGWhackPathBufferW(psz, cch)
  485. #define DEBUGWhackPathStringA(psz, cch)
  486. #define DEBUGWhackPathStringW(psz, cch)
  487. #endif // DEBUG
  488. #ifdef UNICODE
  489. #define DEBUGWhackPathBuffer DEBUGWhackPathBufferW
  490. #define DEBUGWhackPathString DEBUGWhackPathStringW
  491. #else
  492. #define DEBUGWhackPathBuffer DEBUGWhackPathBufferA
  493. #define DEBUGWhackPathString DEBUGWhackPathStringA
  494. #endif
  495. // Some trickery to map ATL debug macros to ours, so ATL code that stops
  496. // or spews in our code will look like the rest of our squirties.
  497. #ifdef DEBUG
  498. #ifdef _ATL_NO_DEBUG_CRT
  499. // ATL uses _ASSERTE. Map it to ours.
  500. #define _ASSERTE(f) ASSERT(f)
  501. // We map ATLTRACE macros to our functions
  502. void _cdecl ShellAtlTraceA(LPCSTR lpszFormat, ...);
  503. void _cdecl ShellAtlTraceW(LPCWSTR lpszFormat, ...);
  504. #ifdef UNICODE
  505. #define ShellAtlTrace ShellAtlTraceW
  506. #else
  507. #define ShellAtlTrace ShellAtlTraceA
  508. #endif
  509. // These are turned off because they normally don't give
  510. // feedback of error cases and so many fire that they
  511. // swamp out other useful debug spew.
  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. // APPCOMPAT (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. #else // NOSHELLDEBUG
  671. #ifdef UNIX
  672. #include <crtdbg.h>
  673. #define ASSERT(f) _ASSERT(f)
  674. #include <mainwin.h>
  675. #define TraceMsg(type, sformat) DebugMessage(0, sformat)
  676. #define TraceMSG(type, sformat, args) DebugMessage(0, sformat, args)
  677. #endif
  678. #endif // NOSHELLDEBUG
  679. //
  680. // Debug dump helper functions
  681. //
  682. #ifdef DEBUG
  683. LPCTSTR Dbg_GetCFName(UINT ucf);
  684. LPCTSTR Dbg_GetHRESULTName(HRESULT hr);
  685. LPCTSTR Dbg_GetREFIIDName(REFIID riid);
  686. LPCTSTR Dbg_GetVTName(VARTYPE vt);
  687. #else
  688. #define Dbg_GetCFName(ucf) (void)0
  689. #define Dbg_GetHRESULTName(hr) (void)0
  690. #define Dbg_GetREFIIDName(riid) (void)0
  691. #define Dbg_GetVTName(vt) (void)0
  692. #endif // DEBUG
  693. // I'm a lazy typist...
  694. #define Dbg_GetHRESULT Dbg_GetHRESULTName
  695. // Parameter validation macros
  696. #include "validate.h"
  697. #endif // DECLARE_DEBUG
  698. #ifdef PRODUCT_PROF
  699. int __stdcall StartCAP(void); // start profiling
  700. int __stdcall StopCAP(void); // stop profiling until StartCAP
  701. int __stdcall SuspendCAP(void); // suspend profiling until ResumeCAP
  702. int __stdcall ResumeCAP(void); // resume profiling
  703. int __stdcall StartCAPAll(void); // process-wide start profiling
  704. int __stdcall StopCAPAll(void); // process-wide stop profiling
  705. int __stdcall SuspendCAPAll(void); // process-wide suspend profiling
  706. int __stdcall ResumeCAPAll(void); // process-wide resume profiling
  707. void __stdcall MarkCAP(long lMark); // write mark to MEA
  708. extern DWORD g_dwProfileCAP;
  709. #else
  710. #define StartCAP() 0
  711. #define StopCAP() 0
  712. #define SuspendCAP() 0
  713. #define ResumeCAP() 0
  714. #define StartCAPAll() 0
  715. #define StopCAPAll() 0
  716. #define SuspendCAPAll() 0
  717. #define ResumeCAPAll() 0
  718. #define MarkCAP(n) 0
  719. #define g_dwProfileCAP 0
  720. #endif
  721. #ifdef __cplusplus
  722. };
  723. #endif