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.

430 lines
18 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name :
  4. trc.h
  5. Abstract:
  6. Kernel-Mode Tracing Facility.
  7. This module utilizes DCL's tracing macros, originally defined in atrcapi.h,
  8. in a way that is intended to be independent of anything but NT DDK API's.
  9. Currently, rdpwd.sys and rdpdd.sys also use these shared macros, but not
  10. in a way that is independent of their respective components.
  11. Author:
  12. Revision History:
  13. --*/
  14. #pragma once
  15. #include <stdio.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define TRC_BUFFER_SIZE 256
  20. /****************************************************************************/
  21. /* Before including this file the TRC_FILE macro should be defined. This */
  22. /* is much more efficient than relying on __FILE__ to give the correct */
  23. /* filename since it includes unnecessary path info (and extension info). */
  24. /* In addition each use of __FILE__ causes a new constant string to be */
  25. /* placed in the data segment. */
  26. /****************************************************************************/
  27. #ifdef TRC_FILE
  28. #define _file_name_ (CHAR *)__filename
  29. static const CHAR __filename[] = TRC_FILE;
  30. #endif /* TRC_FILE */
  31. /****************************************************************************/
  32. /* */
  33. /* FUNCTION PROTOTYPES */
  34. /* */
  35. /****************************************************************************/
  36. BOOL TRC_ProfileTraceEnabled();
  37. VOID TRC_TraceLine( ULONG traceLevel,
  38. PCHAR traceString,
  39. CHAR separator,
  40. ULONG lineNumber,
  41. PCHAR funcName,
  42. PCHAR fileName);
  43. VOID TRC_TraceData( ULONG traceLevel,
  44. PCHAR traceString,
  45. CHAR separator,
  46. PVOID buffer,
  47. ULONG length,
  48. ULONG lineNumber,
  49. PCHAR funcName,
  50. PCHAR fileName);
  51. BOOL TRC_WillTrace( ULONG traceLevel,
  52. PCHAR fileName,
  53. ULONG line);
  54. /****************************************************************************/
  55. /* */
  56. /* CONSTANTS */
  57. /* */
  58. /****************************************************************************/
  59. /****************************************************************************/
  60. /* Define the trace level. */
  61. /* */
  62. /* TRC_LEVEL_DBG : All tracing is enabled */
  63. /* TRC_LEVEL_NRM : Debug level tracing is disabled */
  64. /* TRC_LEVEL_ALT : Normal and debug level tracing is disabled */
  65. /* TRC_LEVEL_ERR : Alert, normal and debug level tracing is */
  66. /* disabled */
  67. /* TRC_LEVEL_ASSERT : Error, alert, normal and debug level tracing */
  68. /* is disabled */
  69. /* TRC_LEVEL_DIS : All tracing is disabled. */
  70. /****************************************************************************/
  71. #define TRC_LEVEL_DBG 0
  72. #define TRC_LEVEL_NRM 1
  73. #define TRC_LEVEL_ALT 2
  74. #define TRC_LEVEL_ERR 3
  75. #define TRC_LEVEL_ASSERT 4
  76. #define TRC_LEVEL_DIS 5
  77. #define TRC_PROFILE_TRACE 8
  78. /****************************************************************************/
  79. /* Tracing can be switched off at compile time to allow for 'debug' and */
  80. /* 'retail' versions of the product. The following macros disable specific */
  81. /* trace processing. */
  82. /* */
  83. /* TRC_ENABLE_DBG - Enable debug tracing */
  84. /* TRC_ENABLE_NRM - Enable normal tracing */
  85. /* TRC_ENABLE_ALT - Enable alert tracing */
  86. /* TRC_ENABLE_ERR - Enable error tracing */
  87. /* TRC_ENABLE_ASSERT - Enable assert tracing */
  88. /* TRC_ENABLE_PRF - Enable function profile tracing */
  89. /****************************************************************************/
  90. #if (TRC_COMPILE_LEVEL == TRC_LEVEL_DBG)
  91. #define TRC_ENABLE_DBG
  92. #define TRC_ENABLE_NRM
  93. #define TRC_ENABLE_ALT
  94. #define TRC_ENABLE_ERR
  95. #define TRC_ENABLE_ASSERT
  96. #endif
  97. #if (TRC_COMPILE_LEVEL == TRC_LEVEL_NRM)
  98. #define TRC_ENABLE_NRM
  99. #define TRC_ENABLE_ALT
  100. #define TRC_ENABLE_ERR
  101. #define TRC_ENABLE_ASSERT
  102. #endif
  103. #if (TRC_COMPILE_LEVEL == TRC_LEVEL_ALT)
  104. #define TRC_ENABLE_ALT
  105. #define TRC_ENABLE_ERR
  106. #define TRC_ENABLE_ASSERT
  107. #endif
  108. #if (TRC_COMPILE_LEVEL == TRC_LEVEL_ERR)
  109. #define TRC_ENABLE_ERR
  110. #define TRC_ENABLE_ASSERT
  111. #endif
  112. #if (TRC_COMPILE_LEVEL == TRC_LEVEL_ASSERT)
  113. #define TRC_ENABLE_ASSERT
  114. #endif
  115. #ifdef TRC_COMPILE_PRF
  116. #define TRC_ENABLE_PRF
  117. #endif
  118. /****************************************************************************/
  119. /* Internal buffer sizes. */
  120. /* */
  121. /* TRC_PREFIX_LIST_SIZE : the length of the prefix string */
  122. /* TRC_FILE_NAME_SIZE : the maximum length of the fully qualified */
  123. /* trace output file name. */
  124. /****************************************************************************/
  125. #define TRC_PREFIX_LIST_SIZE 100
  126. /**MOANSOFF */
  127. #define TRC_FILE_NAME_SIZE MAX_PATH
  128. /**MOANSON */
  129. /****************************************************************************/
  130. /* Prefix constants */
  131. /* */
  132. /* TRC_MAX_PREFIX : number of prefixes supported */
  133. /* TRC_PREFIX_NAME_LEN : length of a prefix name */
  134. /****************************************************************************/
  135. #define TRC_MAX_PREFIX 20
  136. #define TRC_PREFIX_NAME_LEN 8
  137. ////////////////////////////////////////////////////////////////////////
  138. //
  139. // Typedefs
  140. //
  141. //
  142. // Least significant set bits define the number of trace messages that
  143. // are saved in-ram. If last 4 bits are set, then save 2^4 messages.
  144. //
  145. #define TRC_RamMsgMask 0x000003FF
  146. #define TRC_RamMsgMax (TRC_RamMsgMask + 1)
  147. ////////////////////////////////////////////////////////////////////////
  148. //
  149. // Typedefs
  150. //
  151. typedef struct tagTRC_PREFIX_DATA
  152. {
  153. CHAR name[TRC_PREFIX_NAME_LEN];
  154. ULONG start;
  155. ULONG end;
  156. } TRC_PREFIX_DATA, *PTRC_PREFIX_DATA;
  157. //
  158. // This structure stores information about the current trace configuration.
  159. //
  160. typedef struct tagTRC_CONFIG
  161. {
  162. ULONG TraceLevel; // The current trace level (TRC_LEVEL_DBG,
  163. // TRC_LEVEL_NRM, etc)
  164. ULONG FunctionLength; // Number of characters of the function name
  165. // traced to the output file.
  166. BOOL TraceDebugger; // If true, tracing should be done to the
  167. // debugger.
  168. BOOL TraceProfile;
  169. TRC_PREFIX_DATA Prefix[TRC_MAX_PREFIX];
  170. } TRC_CONFIG;
  171. #define TRC_CONFIG_DEFAULT { \
  172. TRC_LEVEL_ALT, \
  173. TRC_FUNCNAME_LEN, \
  174. 0xFFFFFFFF, \
  175. FALSE, \
  176. TRUE \
  177. }
  178. /****************************************************************************/
  179. /* Various trace helper definitions */
  180. /****************************************************************************/
  181. #define TB TraceBuffer, sizeof(TraceBuffer)
  182. /****************************************************************************/
  183. /* Trace format definitions. These are used for printing various parts of */
  184. /* the trace lines. */
  185. /* */
  186. /* TIME is the time in the form hours, mins, secs, hundredths. */
  187. /* DATE is the date in the form day, month, year. */
  188. /* FUNC is the module function name. This is of variable size. */
  189. /* LINE is the line number within the source file. */
  190. /* PROC is the process identifier. */
  191. /* THRD is the thread identifier. */
  192. /* */
  193. /****************************************************************************/
  194. #define TRC_TIME_FMT "%02d:%02d:%02d.%03d"
  195. #define TRC_DATE_FMT "%02d/%02d/%02d"
  196. #define TRC_FUNC_FMT "%-*.*s"
  197. #define TRC_LINE_FMT "%04d"
  198. #define TRC_PROC_FMT "%04.4lx"
  199. #define TRC_THRD_FMT "%04.4lx"
  200. #define TRC_FUNCNAME_LEN 24
  201. #define TRC_SEP_DBG ' '
  202. #define TRC_SEP_NRM ' '
  203. #define TRC_SEP_ALT '+'
  204. #define TRC_SEP_ERR '*'
  205. #define TRC_SEP_ASSERT '!'
  206. #define TRC_SEP_PROF ' '
  207. #if (TRC_COMPILE_LEVEL < TRC_LEVEL_DIS)
  208. #define TRC_FN(A) static const CHAR __fnname[] = A; \
  209. PCHAR trc_fn = (PCHAR)__fnname; \
  210. PCHAR trc_file = _file_name_; \
  211. static CHAR TraceBuffer[TRC_BUFFER_SIZE];
  212. #else
  213. #define TRC_FN(A)
  214. #endif
  215. __inline BOOL IsValid() { return TRUE; }
  216. #define BEGIN_FN(str) TRC_FN(str); TRC_ENTRY; ASSERT(IsValid());
  217. #define BEGIN_FN_STATIC(str) TRC_FN(str); TRC_ENTRY;
  218. //#define END_FN() TRC_EXIT;
  219. /****************************************************************************/
  220. /* */
  221. /* MACROS */
  222. /* */
  223. /****************************************************************************/
  224. /****************************************************************************/
  225. /* Standard trace macros */
  226. /****************************************************************************/
  227. #ifdef TRC_ENABLE_DBG
  228. #define TRC_DBG(string) TRCX(TRC_LEVEL_DBG, TRC_SEP_DBG, string)
  229. #else
  230. #define TRC_DBG(string)
  231. #endif
  232. #ifdef TRC_ENABLE_NRM
  233. #define TRC_NRM(string) TRCX(TRC_LEVEL_NRM, TRC_SEP_NRM, string)
  234. #else
  235. #define TRC_NRM(string)
  236. #endif
  237. #ifdef TRC_ENABLE_ALT
  238. #define TRC_ALT(string) TRCX(TRC_LEVEL_ALT, TRC_SEP_ALT, string)
  239. #else
  240. #define TRC_ALT(string)
  241. #endif
  242. #ifdef TRC_ENABLE_ERR
  243. #define TRC_ERR(string) TRCX(TRC_LEVEL_ERR, TRC_SEP_ERR, string)
  244. #else
  245. #define TRC_ERR(string)
  246. #endif
  247. #ifdef TRC_ENABLE_ASSERT
  248. /****************************************************************************/
  249. /* TRC_ASSERT & TRC_ABORT */
  250. /****************************************************************************/
  251. #define TRC_ASSERT(condition, string) \
  252. { \
  253. if (!(condition)) \
  254. { \
  255. TRCX(TRC_LEVEL_ERR, TRC_SEP_ASSERT, string); \
  256. DbgBreakPoint(); \
  257. } \
  258. }
  259. #define TRC_ABORT(string) \
  260. { \
  261. TRCX(TRC_LEVEL_ERR, TRC_SEP_ASSERT, string); \
  262. DbgBreakPoint(); \
  263. }
  264. #undef ASSERT
  265. #define ASSERT(condition) TRC_ASSERT(condition, (TB, #condition))
  266. #else /* TRC_ENABLE_ASSERT */
  267. /****************************************************************************/
  268. /* TRC_ASSERT & TRC_ABORT for retail builds */
  269. /****************************************************************************/
  270. #define TRC_ASSERT(condition, string)
  271. #define TRC_ABORT(string)
  272. #endif /* TRC_ENABLE_ASSERT */
  273. #ifdef TRC_ENABLE_TST
  274. #define TRC_TST TRC_DBG
  275. #else
  276. #define TRC_TST(x)
  277. #endif /* TRC_ENABLE_TST */
  278. /****************************************************************************/
  279. /* TRCX for RdpDr, driver of champions */
  280. /****************************************************************************/
  281. #define TRCX(level, separator, traceString) \
  282. { \
  283. { \
  284. _snprintf traceString; \
  285. TRC_TraceLine(level, \
  286. TraceBuffer, \
  287. separator, \
  288. (ULONG)__LINE__, \
  289. trc_fn, \
  290. trc_file); \
  291. } \
  292. }
  293. /****************************************************************************/
  294. /* Data dump trace macros */
  295. /****************************************************************************/
  296. #ifdef TRC_ENABLE_DBG
  297. #define TRC_DATA_DBG(string, buffer, length) \
  298. TRCX_DATA(TT_OUT1, TRC_SEP_DBG, string, buffer, length)
  299. #else
  300. #define TRC_DATA_DBG(string, buffer, length)
  301. #endif
  302. #ifdef TRC_ENABLE_NRM
  303. #define TRC_DATA_NRM(string, buffer, length) \
  304. TRCX_DATA(TT_OUT2, TRC_SEP_NRM, string, buffer, length)
  305. #else
  306. #define TRC_DATA_NRM(string, buffer, length)
  307. #endif
  308. #ifdef TRC_ENABLE_ALT
  309. #define TRC_DATA_ALT(string, buffer, length) \
  310. TRCX_DATA(TT_OUT3, TRC_SEP_ALT, string, buffer, length)
  311. #else
  312. #define TRC_DATA_ALT(string, buffer, length)
  313. #endif
  314. #ifdef TRC_ENABLE_ERR
  315. #define TRC_DATA_ERR(string, buffer, length) \
  316. TRCX_DATA(TT_OUT4, TRC_SEP_ERR, string, buffer, length)
  317. #else
  318. #define TRC_DATA_ERR(string, buffer, length)
  319. #endif
  320. #ifdef TRC_ENABLE_NRM
  321. #define TRC_DATA_NET(string, buffer, length) \
  322. TRCX_DATA(TT_OUT2, TRC_SEP_NRM, string, buffer, length)
  323. #else
  324. #define TRC_DATA_NET(string, buffer, length)
  325. #endif
  326. #define TRCX_DATA(level, separator, string, buffer, length) \
  327. { \
  328. { \
  329. sprintf string; \
  330. TRC_TraceData(level, \
  331. TraceBuffer, \
  332. separator, \
  333. (PVOID)buffer, \
  334. length, \
  335. (ULONG)__LINE__, \
  336. trc_fn, \
  337. trc_file); \
  338. } \
  339. }
  340. /****************************************************************************/
  341. /* Entry and exit trace macros. */
  342. /****************************************************************************/
  343. #define TRCP(string) \
  344. { \
  345. if (TRC_ProfileTraceEnabled()) \
  346. { \
  347. TRCX(TRC_PROFILE_TRACE, TRC_SEP_PROF, string) \
  348. } \
  349. }
  350. #ifdef TRC_ENABLE_PRF
  351. #define TRC_CLASS_OBJ TrcFn trc_fn_obj;
  352. #define TRC_PRF(string) TRCP(string)
  353. #else
  354. #define TRC_CLASS_OBJ
  355. #define TRC_PRF(string)
  356. #endif
  357. #define TRC_ENTRY TRC_PRF((TB, "Enter {")); TRC_CLASS_OBJ
  358. #define TRC_EXIT TRC_PRF((TB, "Exit }"));
  359. class TrcFn
  360. {
  361. ~TrcFn()
  362. {
  363. TRC_EXIT;
  364. }
  365. };
  366. #ifdef __cplusplus
  367. }
  368. #endif