Source code of Windows XP (NT5)
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.

430 lines
16 KiB

  1. /*
  2. * _DEBUG.H
  3. *
  4. * Purpose:
  5. * RICHEDIT debugging support--commented out in ship builds
  6. *
  7. * History: <nl>
  8. * 7/29/98 KeithCu Wrote it stealing much from Rich Arneson's code
  9. *
  10. * Copyright (c) 1995-1998, Microsoft Corporation. All rights reserved.
  11. */
  12. #ifndef _DEBUG_H
  13. #define _DEBUG_H
  14. #define DllExport __declspec(dllexport)
  15. #if !defined(NOFULLDEBUG) && (defined(DEBUG) || defined(_RELEASE_ASSERTS_))
  16. #define ASSERTDATA static char _szFile[] = __FILE__;
  17. BOOL WINAPI DebugMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved);
  18. #else // DEBUG
  19. #define ASSERTDATA
  20. #define DebugMain(hDll, dwReason, lpReserved)
  21. #endif // DEBUG, else
  22. //This is the buffer length used for building messages
  23. #define MAXDEBUGSTRLEN (MAX_PATH + MAX_PATH)
  24. //The following constants are used to specify and interpret
  25. //packed values in the DWORD flags parameter passed to TraceMsg.
  26. //Each of these is held in a 4-bit field in the DWORD.
  27. //Subsystem field values
  28. #define TRCSUBSYSNONE 0x0
  29. #define TRCSUBSYSDISP 0x1
  30. #define TRCSUBSYSWRAP 0x2
  31. #define TRCSUBSYSEDIT 0x3
  32. #define TRCSUBSYSTS 0x4
  33. #define TRCSUBSYSTOM 0x5
  34. #define TRCSUBSYSOLE 0x6
  35. #define TRCSUBSYSBACK 0x7
  36. #define TRCSUBSYSSEL 0x8
  37. #define TRCSUBSYSHOST 0x9
  38. #define TRCSUBSYSDTE 0xa
  39. #define TRCSUBSYSUNDO 0xb
  40. #define TRCSUBSYSRANG 0xc
  41. #define TRCSUBSYSUTIL 0xd
  42. #define TRCSUBSYSNOTM 0xe
  43. #define TRCSUBSYSRTFR 0xf
  44. #define TRCSUBSYSRTFW 0x10
  45. #define TRCSUBSYSPRT 0x11
  46. #define TRCSUBSYSFE 0x12
  47. #define TRCSUBSYSFONT 0x13
  48. //Severity field values
  49. #define TRCSEVNONE 0x0
  50. #define TRCSEVWARN 0x1
  51. #define TRCSEVERR 0x2
  52. #define TRCSEVASSERT 0x3
  53. #define TRCSEVINFO 0x4
  54. #define TRCSEVMEM 0x5
  55. //Scope field values
  56. #define TRCSCOPENONE 0x0
  57. #define TRCSCOPEEXTERN 0x1
  58. #define TRCSCOPEINTERN 0x2
  59. //Data field values
  60. #define TRCDATANONE 0x0
  61. #define TRCDATAHRESULT 0x1
  62. #define TRCDATASTRING 0x2
  63. #define TRCDATAPARAM 0x3
  64. #define TRCDATADEFAULT 0x4
  65. //Debug option flags. See the macros in this header for setting and testing
  66. //these option flags.
  67. #define OPTUSEDEFAULTS 0x00000001 //Use defaults from win.ini
  68. //(used only with InitDebugServices).
  69. #define OPTLOGGINGON 0x00000008 //Logging of trace output
  70. #define OPTVERBOSEON 0x00000010 //Subsys, Scope & PID/TID
  71. #define OPTINFOON 0x00000020 //Informational messages
  72. #define OPTTRACEON 0x00000040 //All function tracing on
  73. #define OPTTRACEEXT 0x00000080 //Function tracing only for external functions
  74. #define OPTMEMORYON 0x00000100 //Memory alloc/free tracing on
  75. //The following options allow tracing to be enabled for one or more
  76. //specific subsystems. If OPTTRACEON is set, these will have no effect.
  77. //if OPTTRACEEXT is set, they will enable tracing for all functions in
  78. //the designated subsystem in addition to external functions.
  79. //The SETOPT and ISOPTSET macros should be used for setting and checking
  80. //these options. INITDEBUGSERVICES can also be used.
  81. #define OPTTRACEDISP 0x00001000 //Function tracing for Display subsystem
  82. #define OPTTRACEWRAP 0x00002000 //Function tracing for Wrapper subsystem
  83. #define OPTTRACEEDIT 0x00004000 //Function tracing for Edit subsystem
  84. #define OPTTRACETS 0x00008000 //Function tracing for TextServices subsystem
  85. #define OPTTRACETOM 0x00010000 //Function tracing for TOM subsystem
  86. #define OPTTRACEOLE 0x00020000 //Function tracing for OLE support subsystem
  87. #define OPTTRACEBACK 0x00040000 //Function tracing for Backing Store subsystem
  88. #define OPTTRACESEL 0x00080000 //Function tracing for Selection subsystem
  89. #define OPTTRACEHOST 0x00100000 //Function tracing for WinHost subsystem
  90. #define OPTTRACEDTE 0x00200000 //Function tracing for DataXfer subsystem
  91. #define OPTTRACEUNDO 0x00400000 //Function tracing for Muli-undo subsystem
  92. #define OPTTRACERANG 0x00800000 //Function tracing for Range subsystem
  93. #define OPTTRACEUTIL 0x01000000 //Function tracing for Utility subsystem
  94. #define OPTTRACENOTM 0x02000000 //Function tracing for Notification Mgr subsystem
  95. #define OPTTRACERTFR 0x04000000 //Function tracing for RTF reader subsystem
  96. #define OPTTRACERTFW 0x08000000 //Function tracing for RTF writer subsystem
  97. #define OPTTRACEPRT 0x10000000 //Function tracing for Printing subsystem
  98. #define OPTTRACEFE 0x20000000 //Function tracing for East Asia subsystem
  99. #define OPTTRACEFONT 0x40000000 //Function tracing for Font Cache
  100. #if !defined(NOFULLDEBUG) && (defined(DEBUG) || defined(_RELEASE_ASSERTS_))
  101. #ifndef _RELEASE_ASSERTS_
  102. //Union for handling tracing flags
  103. //This union is used to decode the
  104. //packed DWORD passed to TraceMsg.
  105. typedef union
  106. {
  107. struct
  108. {
  109. unsigned uData2 :4;
  110. unsigned uData1 :4;
  111. unsigned uScope :4;
  112. unsigned uSeverity :4;
  113. unsigned uSubSystem :8;
  114. unsigned uUnused1 :4;
  115. unsigned uUnused2 :4;
  116. } fields;
  117. DWORD dw;
  118. } TrcFlags;
  119. //Exported classes and functions.
  120. //Generally, these should not be used directly by the user.
  121. //They should be used via the macros defined in this header.
  122. //This helps to ensure that the parameter lists are well
  123. //formed and keeps references to them from showing up in
  124. //in non-debug builds.
  125. //This class is used to implement the function Entry/Exit
  126. //tracing. By declaring it on the stack at the beginning
  127. //of a function, Entry and Exit messages are automatically
  128. //generated by the constructor and destructor.
  129. class CTrace
  130. {
  131. public:
  132. CTrace(DWORD, DWORD, DWORD, LPSTR);
  133. ~CTrace();
  134. private:
  135. TrcFlags trcf;
  136. char szFileName[MAXDEBUGSTRLEN];
  137. char szFuncName[80];
  138. };
  139. extern DWORD dwDebugOptions;
  140. extern void SetLogging(BOOL);
  141. void Tracef(DWORD, LPSTR szFmt, ...);
  142. void TraceError(LPSTR sz, LONG sc);
  143. #endif //!_RELEASE_ASSERTS_
  144. typedef BOOL (CALLBACK * PFNASSERTHOOK)(LPSTR, LPSTR, int*);
  145. typedef BOOL (CALLBACK * PFNTRACEHOOK)(DWORD*, DWORD*, DWORD*, LPSTR, int*);
  146. extern PFNTRACEHOOK pfnTrace;
  147. extern PFNASSERTHOOK pfnAssert;
  148. void AssertSzFn(LPSTR, LPSTR, int);
  149. void TraceMsg(DWORD, DWORD, DWORD, LPSTR, int);
  150. DllExport void WINAPI InitDebugServices(DWORD, PFNASSERTHOOK, PFNTRACEHOOK);
  151. //Assert based on boolean f.
  152. #define Assert(f) AssertSz((f), NULL)
  153. //Assert based on boolean f in debug, resolve to f in non-debug.
  154. #define SideAssert(f) AssertSz((f), NULL)
  155. //Assert based on boolean f and use string sz in assert message.
  156. #define AssertSz(f, sz) (!(f) ? AssertSzFn(sz, __FILE__, __LINE__) : 0)
  157. //Set an assert or trace hook function. The function specified will be called
  158. //before the default functionality executes. Pointers to all parameters are passed
  159. //to the hook to allow it to modify them. If the hook function returns false,
  160. //default functionality is terminated. If the hook function returns true, default
  161. //functionality continues with the potentially modified parameters. pfn can
  162. //be NULL (default functionality only).
  163. #define SETASSERTFN(pfn) (pfnAssert = (pfn))
  164. //The following macros provide access to the debug services in this dll.
  165. //Assert macros pop a dialog. Trace macros output to debug output and
  166. //logfile if enabled.
  167. //Macro for InitDebugServices
  168. #define INITDEBUGSERVICES(f, pfnA, pfnT) InitDebugServices(f, pfnA, pfnT)
  169. //This is a utility macro for internal use. The user should not need this.
  170. #define MAKEFLAGS(ss, sv, sc, d1, d2) ((ss << 16) + (sv << 12) + (sc << 8)\
  171. + (d1 << 4) + (d2))
  172. #ifndef _RELEASE_ASSERTS_
  173. //Assert only on debug builds, not on _RELEASE_ASSERTS_ builds
  174. //This is for asserts that contain debug only code
  175. #ifndef AssertNr
  176. #define AssertNr(f) AssertSz((f), NULL)
  177. #endif
  178. #ifndef AssertNrSz
  179. #define AssertNrSz(f, sz) (!(f) ? AssertSzFn(sz, __FILE__, __LINE__) : 0)
  180. #endif
  181. //Macro for TraceError
  182. #define TRACEERRSZSC(sz, sc) TraceError(sz, sc)
  183. //Warning based on GetLastError or default message if no last error.
  184. #define TRACEWARN TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVWARN,\
  185. TRCSCOPENONE, TRCDATADEFAULT, TRCDATANONE),\
  186. (DWORD)0, (DWORD)0, __FILE__, __LINE__)
  187. //Error based on GetLastError or default message if no last error.
  188. #define TRACEERROR TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVERR,\
  189. TRCSCOPENONE, TRCDATADEFAULT, TRCDATANONE),\
  190. (DWORD)0, (DWORD)0, __FILE__, __LINE__)
  191. //Warning based on HRESULT hr
  192. #define TRACEWARNHR(hr) TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVWARN,\
  193. TRCSCOPENONE, TRCDATAHRESULT, TRCDATANONE),\
  194. (DWORD)(hr), (DWORD)0, __FILE__, __LINE__)
  195. //Test for a failure HR && warn
  196. #define TESTANDTRACEHR(hr) if( hr < 0 ) { TRACEWARNHR(hr); }
  197. //Error based on HRESULT hr
  198. #define TRACEERRORHR(hr) TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVERR,\
  199. TRCSCOPENONE, TRCDATAHRESULT, TRCDATANONE),\
  200. (DWORD)(hr), (DWORD)0, __FILE__, __LINE__)
  201. //Warning using string sz
  202. #define TRACEWARNSZ(sz) TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVWARN,\
  203. TRCSCOPENONE, TRCDATASTRING, TRCDATANONE),\
  204. (DWORD)(DWORD_PTR)(sz), (DWORD)0, __FILE__, __LINE__)
  205. //Trace based on Assert, user passes file name and line
  206. #define TRACEASSERT(szFile, iLine) TraceMsg (MAKEFLAGS(TRCSUBSYSNONE,\
  207. TRCSEVASSERT, TRCSCOPENONE,\
  208. TRCDATANONE, TRCDATANONE),\
  209. (DWORD)0, (DWORD)0, szFile, iLine)
  210. //Trace based on Assert, user passes file name and line
  211. #define TRACEASSERTSZ(sz, szFile, iLine) TraceMsg (MAKEFLAGS(TRCSUBSYSNONE,\
  212. TRCSEVASSERT, TRCSCOPENONE,\
  213. TRCDATASTRING, TRCDATANONE),\
  214. (DWORD)(DWORD_PTR)sz, (DWORD)0, szFile, iLine)
  215. //Error using string sz
  216. #define TRACEERRORSZ(sz) TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVERR,\
  217. TRCSCOPENONE, TRCDATASTRING, TRCDATANONE),\
  218. (DWORD)(DWORD_PTR)(sz), (DWORD)0, __FILE__, __LINE__)
  219. //Error using string sz
  220. #define TRACEINFOSZ(sz) TraceMsg(MAKEFLAGS(TRCSUBSYSNONE, TRCSEVINFO,\
  221. TRCSCOPENONE, TRCDATASTRING, TRCDATANONE),\
  222. (DWORD)(DWORD_PTR)(sz), (DWORD)0, __FILE__, __LINE__)
  223. //Initiate tracing. This declares an instance of the CTtrace class
  224. //on the stack. Subsystem (ss), Scope (sc), and the function name
  225. //(sz) must be specifed. ss and sc are specified using the macros
  226. //defined in this header (i.e. - TRCSUBSYSTOM, TRCSCOPEEXTERN, etc.).
  227. //sz can be a static string.
  228. #define TRACEBEGIN(ss, sc, sz) CTrace trc(MAKEFLAGS((ss), TRCSEVNONE,\
  229. (sc), TRCDATASTRING, TRCDATANONE),\
  230. (DWORD)(DWORD_PTR)(sz), (DWORD)0, __FILE__)
  231. //Same as TRACEBEGIN but it takes the additional param which is interpreted
  232. //by TraceMsg as a Text Message request.
  233. #define TRACEBEGINPARAM(ss, sc, sz, param) \
  234. CTrace trc(MAKEFLAGS((ss), TRCSEVNONE,\
  235. (sc), TRCDATASTRING, TRCDATAPARAM),\
  236. (DWORD)(DWORD_PTR)(sz), (DWORD)(param), __FILE__)
  237. //Set logging to on (f = TRUE) or off (f = FALSE)
  238. #define SETLOGGING(f) SetLogging(f)
  239. //Set output of process & thread IDs to on (f = TRUE) or off (f = FALSE)
  240. #define SETVERBOSE(f) ((f) ? (dwDebugOptions |= OPTVERBOSEON) :\
  241. (dwDebugOptions &= ~OPTVERBOSEON))
  242. //Set information messages to on (f = TRUE) or off (f = FALSE)
  243. #define SETINFO(f) ((f) ? (dwDebugOptions |= OPTINFOON) :\
  244. (dwDebugOptions &= ~OPTINFOON))
  245. //Set information messages to on (f = TRUE) or off (f = FALSE)
  246. #define SETMEMORY(f) ((f) ? (dwDebugOptions |= OPTMEMORYON) :\
  247. (dwDebugOptions &= ~OPTMEMORYON))
  248. //Set tracing for all functions to on (f = TRUE) or off (f = FALSE).
  249. //If this is set to "on", external and subsystem level tracing
  250. //has no effect since all function traces are enabled. If it is off,
  251. //external and subsystem level tracing remain in whatever state they
  252. //have been set to.
  253. #define SETTRACING(f) ((f) ? (dwDebugOptions |= OPTTRACEON) :\
  254. (dwDebugOptions &= ~OPTTRACEON))
  255. //Set tracing for EXTERNAL scope calls only to on (f = TRUE)
  256. //or off (f = FALSE). This is only effective if OPTTRACEON has not
  257. //been set.
  258. #define SETTRACEEXT(f) ((f) ? (dwDebugOptions |= OPTTRACEEXT) :\
  259. (dwDebugOptions &= ~OPTTRACEEXT))
  260. //This macro turns all function tracing off.
  261. #define SETALLTRACEOFF (dwDebugOptions &= ~(OPTTRACEEXT | OPTTRACEON | 0xfffff000))
  262. //This macro sets a given option or options (if they are or'ed together)
  263. //to on (f = TRUE), or off (f = FALSE). It cannot be used to set logging.
  264. #define SETOPT(opt, f) ((f) ? (dwDebugOptions |= (opt)) :\
  265. (dwDebugOptions &= (~(opt))))
  266. //This macro determines the state of a given option.
  267. #define ISOPTSET(opt) ((opt) & dwDebugOptions)
  268. //Set an assert or trace hook function. The function specified will be called
  269. //before the default functionality executes. Pointers to all parameters are passed
  270. //to the hook to allow it to modify them. If the hook function returns false,
  271. //default functionality is terminated. If the hook function returns true, default
  272. //functionality continues with the potentially modified parameters. pfn can
  273. //be NULL (default functionality only).
  274. #define SETTRACEFN(pfn) (pfnTrace = (pfn))
  275. //The following option tests are explicitly defined for convenience.
  276. #define fLogging (OPTLOGGINGON & dwDebugOptions)
  277. #define fVerbose (OPTVERBOSEON & dwDebugOptions)
  278. #define fInfo (OPTINFOON & dwDebugOptions)
  279. #define fTrace (OPTTRACEON & dwDebugOptions)
  280. #define fTraceExt (OPTTRACEEXT & dwDebugOptions)
  281. #else //_RELEASE_ASSERTS_
  282. //Functions not used by release build with asserts
  283. #ifndef AssertNr
  284. #define AssertNr(f)
  285. #endif
  286. #ifndef AssertNrSz
  287. #define AssertNrSz(f, sz)
  288. #endif
  289. #define Tracef ;/##/
  290. #define TRACEERRSZSC(sz, sc)
  291. #define TRACEWARN
  292. #define TRACEERROR
  293. #define TRACEWARNHR(hr)
  294. #define TESTANDTRACEHR(hr)
  295. #define TRACEERRORHR(hr)
  296. #define TRACEWARNSZ(sz)
  297. #define TRACEASSERT(szFile, iLine)
  298. #define TRACEASSERTSZ(sz, szFile, iLine)
  299. #define TRACEERRORSZ(sz)
  300. #define TRACEINFOSZ(sz)
  301. #define TRACEBEGIN(ss, sc, sz)
  302. #define TRACEBEGINPARAM(ss, sc, sz, param)
  303. #define SETLOGGING(f)
  304. #define SETVERBOSE(f)
  305. #define SETINFO(f)
  306. #define SETMEMORY(f)
  307. #define SETTRACING(f)
  308. #define SETTRACEEXT(f)
  309. #define SETALLTRACEOFF
  310. #define SETOPT(opt, f)
  311. #define ISOPTSET(opt)
  312. #define SETTRACEFN(pfn)
  313. #define TraceError(_sz, _sc)
  314. #endif //_RELEASE_ASSERTS_
  315. #else // !(!defined(NOFULLDEBUG) && (defined(DEBUG) || defined(_RELEASE_ASSERTS_)))
  316. #if defined(NOFULLDEBUG) && defined(DEBUG)
  317. __inline void Tracef(DWORD, LPSTR, ...) {};
  318. __inline void Assert(bool) {};
  319. __inline void AssertSz(bool, char *) {};
  320. typedef BOOL (CALLBACK * PFNASSERTHOOK)(LPSTR, LPSTR, int*);
  321. extern PFNASSERTHOOK pfnAssert;
  322. #else // !defined(NOFULLDEBUG)
  323. #define Tracef ;/##/
  324. #ifndef Assert
  325. #define Assert(f)
  326. #endif
  327. #ifndef AssertSz
  328. #define AssertSz(f, sz)
  329. #endif
  330. #endif
  331. #define INITDEBUGSERVICES(f, pfnA, pfnT)
  332. #define TRACEERRSZSC(sz, sc)
  333. #ifndef SideAssert
  334. #define SideAssert(f) (f)
  335. #endif
  336. #ifndef AssertNr
  337. #define AssertNr(f)
  338. #endif
  339. #ifndef AssertNrSz
  340. #define AssertNrSz(f, sz)
  341. #endif
  342. #define TRACEWARN
  343. #define TRACEERROR
  344. #define TRACEWARNHR(hr)
  345. #define TESTANDTRACEHR(hr)
  346. #define TRACEERRORHR(hr)
  347. #define TRACEWARNSZ(sz)
  348. #define TRACEASSERT(szFile, iLine)
  349. #define TRACEASSERTSZ(sz, szFile, iLine)
  350. #define TRACEERRORSZ(sz)
  351. #define TRACEINFOSZ(sz)
  352. #define TRACEBEGIN(ss, sc, sz)
  353. #define TRACEBEGINPARAM(ss, sc, sz, param)
  354. #define SETLOGGING(f)
  355. #define SETVERBOSE(f)
  356. #define SETINFO(f)
  357. #define SETMEMORY(f)
  358. #define SETTRACING(f)
  359. #define SETTRACEEXT(f)
  360. #define SETALLTRACEOFF
  361. #define SETOPT(opt, f)
  362. #define ISOPTSET(opt)
  363. #define SETASSERTFN(pfn)
  364. #define SETTRACEFN(pfn)
  365. #define AssertSzFn(sz, __FILE__, __LINE__)
  366. #define TraceError(_sz, _sc)
  367. #endif
  368. #endif //DEBUG_H