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.

463 lines
15 KiB

  1. #pragma once
  2. // this is incompatible with stidebug.h, so don't include stidebug.h
  3. #define _STIDEBUG_H_
  4. #undef ASSERT
  5. #undef REQUIRE
  6. #undef DPRINTF
  7. #undef DPRINTF2
  8. #undef DPRINTF_NOINFO
  9. //
  10. // predefined bits in debug flags
  11. //
  12. // something is really wrong, should not go unnoticed
  13. #define COREDBG_ERRORS 0x00000001
  14. // something that may be of interest to debugging person
  15. #define COREDBG_WARNINGS 0x00000002
  16. // trace random low-priority things with DBG_TRC
  17. #define COREDBG_TRACES 0x00000004
  18. // trace function entries, exits (if so equipped)
  19. // with DBG_FN
  20. #define COREDBG_FNS 0x00000008
  21. // break on errors
  22. #define COREDBG_BREAK_ON_ERRORS 0x80000000
  23. // log to file (default)
  24. #define COREDBG_DONT_LOG_TO_FILE 0x40000000
  25. // log to debugger (default)
  26. #define COREDBG_DONT_LOG_TO_DEBUGGER 0x20000000
  27. // debug log is saved to this file
  28. #define COREDBG_FILE_NAME "%userprofile%\\wiadebug.log"
  29. // registry key location
  30. #define COREDBG_FLAGS_REGKEY "System\\CurrentControlSet\\Control\\StillImage\\Debug"
  31. // registry DWORD value name
  32. #define COREDBG_FLAGS_REGVAL "DebugFlags"
  33. // registry DWORD for max log file size
  34. #define COREDBG_REGVAL_FILE_SIZE_LIMIT "DebugFileSizeLimit"
  35. #define COREDBG_FILE_SIZE_LIMIT (512 * 1024) // bytes
  36. #ifdef DEBUG
  37. // by default log errors only in debug builds
  38. #define COREDBG_DEFAULT_FLAGS COREDBG_ERRORS
  39. #else
  40. // by default log errors only in free builds
  41. #define COREDBG_DEFAULT_FLAGS COREDBG_ERRORS
  42. #endif
  43. /****************************************************************************
  44. HOW TO USE WIA CORE DEBUG (main macros)
  45. ======================================
  46. - DBG_INIT(hInstance)
  47. Call from WinMain or DllMain to enable debug flags on a per module
  48. basis. If you don't call it, all DLLs will inherit the debug flags
  49. of the process that creates them.
  50. - DBG_ERR(("Something happened, hr = 0x%x", hr));
  51. Use when an error condition occurred.
  52. - DBG_WRN(("Warning, something happening, Value=%d", iValue));
  53. Use in a situation warranting a warning.
  54. - DBG_TRC(("Random trace statement, Value=%s", szValue));
  55. Use sparingly to trace certain parts of your code. Minimize spew!!!
  56. - DBG_PRT(("Output without standard File,Line,ThreadID info, Value=%d", iValue));
  57. Same as DBG_TRC, but doesn't output the File,Line,ThreadID line.
  58. ***Use this only if you are doing some special formatting (use sparingly)***
  59. - DBG_FN(FnName)
  60. Tracks entry and exits from a given scope.
  61. - CHECK_NOERR (VarName)
  62. CHECK_NOERR2 (VarName, (YourMsg,...))
  63. Does GetLastError and if not 0, outputs error.
  64. - CHECK_S_OK (hr)
  65. CHECK_S_OK2 (hr, (YourMsg,...))
  66. Checks if hr == S_OK, if not, outputs error.
  67. - CHECK_SUCCESS (lResult)
  68. CHECK_SUCCESS2(lResult, (YourMsg,...))
  69. Checks if lResult == ERROR_SUCCESS, if not, outputs error.
  70. - REQUIRE_NOERR (VarName)
  71. REQUIRE_NOERR2 (VarName, (YourMsg,...))
  72. Same as equivalent CHECK_* macros above, but calls "goto Cleanup" as well
  73. - REQUIRE_S_OK (hr)
  74. REQUIRE_S_OK2 (hr, (YourMsg,...))
  75. Same as equivalent CHECK_* macros above, but calls "goto Cleanup" as well
  76. - REQUIRE_SUCCESS (lResult)
  77. REQUIRE_SUCCESS2(lResult, (YourMsg,...))
  78. Same as equivalent CHECK_* macros above, but calls "goto Cleanup" as well
  79. HOW TO TURN ON WIA CORE DEBUG (3 ways)
  80. ======================================
  81. 1) Set registry HKLM\System\CurrentControlSet\Control\StillImage\Debug\<ModuleName>,
  82. DWORD value "DebugFlags" to an OR'd value of above COREDBG_* flags.
  83. Need to restart app to pick up new settings. Key is auto created the first time
  84. the app is run. (Note: <ModuleName> above is the name
  85. of your DLL or EXE. e.g. wiavusd.dll has a registry key of
  86. "HKLM\System\CurrentControlSet\Control\StillImage\Debug\wiavusd.dll")
  87. OR
  88. 2) In the debugger, set g_dwDebugFlags to OR'd value of COREDBG_* flags above.
  89. You can do this anytime during the debug session.
  90. OR
  91. 3) Call in your code WIA_SET_FLAGS(COREDBG_ERRORS | COREDBG_WARNINGS | COREDBG_TRACES);
  92. or any combo of the COREDBG_* flags.
  93. *****************************************************************************/
  94. #define DBG_INIT(x) DINIT(x)
  95. #define DBG_TERM() DTERM()
  96. #define DBG_ERR(x) DPRINTF(COREDBG_ERRORS, x)
  97. #define DBG_WRN(x) DPRINTF(COREDBG_WARNINGS, x)
  98. #define DBG_TRC(x) DPRINTF(COREDBG_TRACES, x)
  99. #define DBG_PRT(x) DPRINTF_NOINFO(COREDBG_TRACES, x)
  100. #define DBG_SET_FLAGS(x) g_dwDebugFlags = (x)
  101. #ifdef __cplusplus
  102. extern "C" {
  103. #endif
  104. //
  105. // accessible to your startup code and at runtime in debugger
  106. // defined in wia\common\stirt\coredbg.cpp
  107. //
  108. extern DWORD g_dwDebugFlags;
  109. extern HANDLE g_hDebugFile;
  110. extern DWORD g_dwDebugFileSizeLimit;
  111. extern BOOL g_bDebugInited;
  112. void __stdcall CoreDbgTrace(LPCSTR fmt, ...);
  113. void __stdcall CoreDbgTraceWithTab(LPCSTR fmt, ...);
  114. void __stdcall CoreDbgInit(HINSTANCE hInstance);
  115. void __stdcall CoreDbgTerm();
  116. #ifdef DEBUG
  117. #define DINIT(x) CoreDbgInit(x)
  118. #define DTERM() CoreDbgTerm()
  119. #define ASSERT(x) \
  120. if(!(x)) { \
  121. DWORD threadId = GetCurrentThreadId(); \
  122. CoreDbgTrace("WIA: [%s(%d): Thread 0x%X (%d)]", __FILE__, __LINE__, threadId, threadId, #x); \
  123. CoreDbgTraceWithTab("ASSERT FAILED. '%s'", #x); \
  124. DebugBreak(); \
  125. }
  126. #define VERIFY(x) ASSERT(x)
  127. #define REQUIRE(x) ASSERT(x)
  128. #define DPRINTF(flags, x) \
  129. if(!g_bDebugInited) \
  130. { \
  131. CoreDbgInit(NULL); \
  132. } \
  133. if(flags & g_dwDebugFlags) { \
  134. DWORD threadId = GetCurrentThreadId(); \
  135. CoreDbgTrace("WIA: [%s(%d): Thread 0x%X (%d)]", __FILE__, __LINE__, threadId, threadId); \
  136. CoreDbgTraceWithTab x; \
  137. if((flags & COREDBG_ERRORS) && (g_dwDebugFlags & COREDBG_BREAK_ON_ERRORS)) { \
  138. DebugBreak(); \
  139. } \
  140. }
  141. #define DPRINTF2(flags, x, y) \
  142. if(!g_bDebugInited) \
  143. { \
  144. CoreDbgInit(NULL); \
  145. } \
  146. if (flags & g_dwDebugFlags) { \
  147. DWORD threadId = GetCurrentThreadId(); \
  148. CoreDbgTrace("WIA: [%s(%d): Thread 0x%X (%d)]", __FILE__, __LINE__, threadId, threadId); \
  149. CoreDbgTraceWithTab x; \
  150. CoreDbgTraceWithTab y; \
  151. if((flags & COREDBG_ERRORS) && (g_dwDebugFlags & COREDBG_BREAK_ON_ERRORS)) { \
  152. DebugBreak(); \
  153. } \
  154. }
  155. #define DPRINTF_NOINFO(flags, x) \
  156. if(!g_bDebugInited) \
  157. { \
  158. CoreDbgInit(NULL); \
  159. } \
  160. if (flags & g_dwDebugFlags) { \
  161. CoreDbgTraceWithTab x; \
  162. if((flags & COREDBG_ERRORS) && (g_dwDebugFlags & COREDBG_BREAK_ON_ERRORS)) { \
  163. DebugBreak(); \
  164. } \
  165. }
  166. #ifdef __cplusplus
  167. #define DBG_FN(x) CoreDbgFn __CoreDbgFnObject(#x)
  168. #else
  169. #define DBG_FN(x)
  170. #endif
  171. #else // begin NODEBUG
  172. #define DINIT(x) CoreDbgInit(x)
  173. #define DTERM() CoreDbgTerm()
  174. #define ASSERT(x)
  175. #define VERIFY(x) x
  176. #define REQUIRE(x) x
  177. #define DPRINTF(flags, x) \
  178. if(!g_bDebugInited) \
  179. { \
  180. CoreDbgInit(NULL); \
  181. } \
  182. if(flags & g_dwDebugFlags) { \
  183. CoreDbgTraceWithTab x; \
  184. }
  185. #define DPRINTF2(flags, x, y) \
  186. if(!g_bDebugInited) \
  187. { \
  188. CoreDbgInit(NULL); \
  189. } \
  190. if(flags & g_dwDebugFlags) { \
  191. CoreDbgTraceWithTab x; \
  192. CoreDbgTraceWithTab y; \
  193. }
  194. #define DPRINTF_NOINFO(flags, x) \
  195. if(!g_bDebugInited) \
  196. { \
  197. CoreDbgInit(NULL); \
  198. } \
  199. if(flags & g_dwDebugFlags) { \
  200. CoreDbgTraceWithTab x; \
  201. }
  202. #ifdef __cplusplus
  203. #define DBG_FN(x) CoreDbgFn __CoreDbgFnObject(#x)
  204. #else
  205. #define DBG_FN(x)
  206. #endif
  207. #endif // end NODEBUG
  208. #define COREDBG_MFMT_FLAGS (FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | \
  209. FORMAT_MESSAGE_MAX_WIDTH_MASK)
  210. #define REQUIRE_NOERR(x) \
  211. if(!(x)) { \
  212. DWORD __dwCoreDbgLastError = GetLastError(); \
  213. CHAR szError[80] = {0}; \
  214. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __dwCoreDbgLastError, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  215. { \
  216. wsprintfA(szError, "Unknown error"); \
  217. } \
  218. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  219. DBG_ERR(("ERROR: %s = %d (0x%08X), '%s'", #x, __dwCoreDbgLastError, __dwCoreDbgLastError, szError)); \
  220. goto Cleanup; \
  221. }
  222. #define REQUIRE_NOERR2(x, y) \
  223. if(!(x)) { \
  224. DWORD __dwCoreDbgLastError = GetLastError(); \
  225. CHAR szError[80] = {0}; \
  226. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __dwCoreDbgLastError, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  227. { \
  228. wsprintfA(szError, "Unknown error"); \
  229. } \
  230. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  231. DPRINTF2(COREDBG_ERRORS, ("ERROR: %s = %d (0x%08X), '%s'", #x, __dwCoreDbgLastError, __dwCoreDbgLastError, szError), y); \
  232. goto Cleanup; \
  233. }
  234. #define REQUIRE_S_OK(x) { \
  235. HRESULT __hrCoreDbg = S_OK; \
  236. __hrCoreDbg = (x); \
  237. if(__hrCoreDbg != S_OK) { \
  238. CHAR szError[80] = {0}; \
  239. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __hrCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  240. { \
  241. wsprintfA(szError, "Unknown hr"); \
  242. } \
  243. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  244. DBG_ERR(("ERROR: %s = 0x%08X, '%s'", #x, __hrCoreDbg, szError)); \
  245. goto Cleanup; \
  246. } \
  247. }
  248. #define REQUIRE_S_OK2(x,y) { \
  249. HRESULT __hrCoreDbg = S_OK; \
  250. __hrCoreDbg = (x); \
  251. if(__hrCoreDbg != S_OK) { \
  252. CHAR szError[80] = {0}; \
  253. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __hrCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  254. { \
  255. wsprintfA(szError, "Unknown hr"); \
  256. } \
  257. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  258. DPRINTF2(COREDBG_ERRORS, ("ERROR: %s = 0x%08X, '%s'", #x, __hrCoreDbg, szError), y); \
  259. goto Cleanup; \
  260. } \
  261. }
  262. #define REQUIRE_SUCCESS(x) { \
  263. UINT __resultCoreDbg = (x); \
  264. if(__resultCoreDbg != ERROR_SUCCESS) { \
  265. CHAR szError[80] = {0}; \
  266. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __resultCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  267. { \
  268. wsprintfA(szError, "Unknown error"); \
  269. } \
  270. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  271. DBG_ERR(("ERROR: %s = 0x%08X, '%s'", #x, __resultCoreDbg, szError)); \
  272. goto Cleanup; \
  273. } \
  274. }
  275. #define REQUIRE_SUCCESS2(x, y) { \
  276. UINT __resultCoreDbg = (x); \
  277. if(__resultCoreDbg != ERROR_SUCCESS) { \
  278. CHAR szError[80] = {0}; \
  279. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __resultCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  280. { \
  281. wsprintfA(szError, "Unknown error"); \
  282. } \
  283. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  284. DPRINTF2(COREDBG_ERRORS, ("ERROR: %s = 0x%08X, '%s'", #x, __resultCoreDbg, szError), y); \
  285. goto Cleanup; \
  286. } \
  287. }
  288. #define CHECK_NOERR(x) \
  289. if(!(x)) { \
  290. DWORD __dwCoreDbgLastError = GetLastError(); \
  291. CHAR szError[80] = {0}; \
  292. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __dwCoreDbgLastError, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  293. { \
  294. wsprintfA(szError, "Unknown error"); \
  295. } \
  296. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  297. DBG_ERR(("ERROR: %s = %d (0x%08X), '%s'", #x, __dwCoreDbgLastError, __dwCoreDbgLastError, szError)); \
  298. }
  299. #define CHECK_NOERR2(x, y) \
  300. if(!(x)) { \
  301. DWORD __dwCoreDbgLastError = GetLastError(); \
  302. CHAR szError[80] = {0}; \
  303. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __dwCoreDbgLastError, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  304. { \
  305. wsprintfA(szError, "Unknown error"); \
  306. } \
  307. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  308. DPRINTF2(COREDBG_ERRORS, ("ERROR: %s = %d (0x%08X), '%s'", #x, __dwCoreDbgLastError, __dwCoreDbgLastError, szError), y); \
  309. }
  310. #define CHECK_S_OK(x) { \
  311. HRESULT __hrCoreDbg = S_OK; \
  312. __hrCoreDbg = (x); \
  313. if(__hrCoreDbg != S_OK) { \
  314. CHAR szError[80] = {0}; \
  315. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __hrCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  316. { \
  317. wsprintfA(szError, "Unknown hr"); \
  318. } \
  319. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  320. DBG_ERR(("ERROR: %s = 0x%08X, '%s'", #x, __hrCoreDbg, szError)); \
  321. } \
  322. }
  323. #define CHECK_S_OK2(x,y) { \
  324. HRESULT __hrCoreDbg = S_OK; \
  325. __hrCoreDbg = (x); \
  326. if(__hrCoreDbg != S_OK) { \
  327. CHAR szError[80] = {0}; \
  328. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __hrCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  329. { \
  330. wsprintfA(szError, "Unknown hr"); \
  331. } \
  332. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  333. DPRINTF2(COREDBG_ERRORS, ("ERROR: %s = 0x%08X, '%s'", #x, __hrCoreDbg, szError), y); \
  334. } \
  335. }
  336. #define CHECK_SUCCESS(x) { \
  337. UINT __resultCoreDbg = (x); \
  338. if(__resultCoreDbg != ERROR_SUCCESS) { \
  339. CHAR szError[80] = {0}; \
  340. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __resultCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  341. { \
  342. wsprintfA(szError, "Unknown error"); \
  343. } \
  344. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  345. DBG_ERR(("ERROR: %s = 0x%08X, '%s'", #x, __resultCoreDbg, szError)); \
  346. } \
  347. }
  348. #define CHECK_SUCCESS2(x, y) { \
  349. UINT __resultCoreDbg = (x); \
  350. if(__resultCoreDbg != ERROR_SUCCESS) { \
  351. CHAR szError[80] = {0}; \
  352. if(!FormatMessageA(COREDBG_MFMT_FLAGS, NULL, __resultCoreDbg, 0, szError, sizeof(szError) / sizeof(szError[0]) - 1, NULL)) \
  353. { \
  354. wsprintfA(szError, "Unknown error"); \
  355. } \
  356. szError[sizeof(szError) / sizeof(szError[0]) - 1] = '\0'; \
  357. DPRINTF2(COREDBG_ERRORS, ("ERROR: %s = 0x%08X, '%s'", #x, __resultCoreDbg, szError), y); \
  358. } \
  359. }
  360. #ifdef __cplusplus
  361. class CoreDbgFn {
  362. private:
  363. LPCSTR m_fn;
  364. DWORD m_threadId;
  365. public:
  366. CoreDbgFn(LPCSTR fn)
  367. {
  368. m_fn = fn;
  369. m_threadId = GetCurrentThreadId();
  370. if(!g_bDebugInited)
  371. {
  372. CoreDbgInit(NULL);
  373. }
  374. if(g_dwDebugFlags & COREDBG_FNS)
  375. {
  376. CoreDbgTraceWithTab("WIA: Thread 0x%X (%d) Entering %s", m_threadId, m_threadId, m_fn);
  377. }
  378. }
  379. ~CoreDbgFn()
  380. {
  381. if(g_dwDebugFlags & COREDBG_FNS)
  382. {
  383. CoreDbgTraceWithTab("WIA: Thread 0x%X (%d) Leaving %s", m_threadId, m_threadId, m_fn);
  384. }
  385. }
  386. };
  387. #endif
  388. #ifdef __cplusplus
  389. }
  390. #endif