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.

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