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.

302 lines
8.7 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. etwtrace.h (ETW tracelogging)
  5. Abstract:
  6. This file contrains the Event Tracer for Windows (ETW)
  7. tracing class.
  8. TODO:
  9. This Wrapper class must do the following.
  10. 1. Able to handle WIN9x, NT, Win2K requirements (DONE)
  11. 2. Lossless Logger
  12. 3. TLS capability. THREAD_DETACH cleanup.
  13. 4. Withstand Dll Load/Unload (DONE)
  14. 5. Generate Transaction ID from the Wrapper Class
  15. 6. Synchronize in the Callback function (DONE)
  16. 7. Synchronize Multiple Threads registering/Unregistering (DONE)
  17. 8. Optionally register during first call to TraceEvent
  18. Author:
  19. Melur Raghuraman (mraghu) 08-May-2001
  20. Revision History:
  21. --*/
  22. #ifndef _ETWTRACER_HXX_
  23. #define _ETWTRACER_HXX_
  24. #include <windows.h>
  25. #include <shellapi.h>
  26. #include <tchar.h>
  27. #include <ntverp.h>
  28. #include <fcntl.h>
  29. #include <initguid.h>
  30. #include <wmistr.h>
  31. #include <guiddef.h>
  32. #include <evntrace.h>
  33. #define ETW_TRACER_BUILD 2195 // Earliest Build ETW Tracing works on
  34. #define ETWMAX_TRACE_LEVEL 4 // Maximum Number of Trace Levels supported
  35. #define ETW_LEVEL_MIN 0 // Basic Logging of inbound/outbound traffic
  36. #define ETW_LEVEL_CP 1 // Capacity Planning Tracing
  37. #define ETW_LEVEL_DBG 2 // Performance Analysis or Debug Tracing
  38. #define ETW_LEVEL_MAX 3 // Very Detailed Debugging trace
  39. //
  40. //--> Define this Provider's Event Types.
  41. //
  42. #define ETW_TYPE_START 0x01
  43. #define ETW_TYPE_END 0x02
  44. #define ETW_TYPE_IIS_STATIC_FILE 0x0A
  45. #define ETW_TYPE_IIS_CGI_REQUEST 0x0B
  46. #define ETW_TYPE_IIS_ISAPI_REQUEST 0x0C
  47. #define ETW_TYPE_IIS_OOP_ISAPI_REQUEST 0x0D
  48. //
  49. //--> Define this Provider's Control Guid here.
  50. //
  51. DEFINE_GUID ( /* 3a2a4e84-4c21-4981-ae10-3fda0d9b0f83 */
  52. IISControlGuid,
  53. 0x3a2a4e84,
  54. 0x4c21,
  55. 0x4981,
  56. 0xae, 0x10, 0x3f, 0xda, 0x0d, 0x9b, 0x0f, 0x83
  57. );
  58. DEFINE_GUID ( /* a1c2040e-8840-4c31-ba11-9871031a19ea */
  59. IsapiControlGuid,
  60. 0xa1c2040e,
  61. 0x8840,
  62. 0x4c31,
  63. 0xba, 0x11, 0x98, 0x71, 0x03, 0x1a, 0x19, 0xea
  64. );
  65. DEFINE_GUID ( /* 1fbecc45-c060-4e7c-8a0e-0dbd6116181b */
  66. StrmFiltControlGuid,
  67. 0x1fbecc45,
  68. 0xc060,
  69. 0x4e7c,
  70. 0x8a, 0x0e, 0x0d, 0xbd, 0x61, 0x16, 0x18, 0x1b
  71. );
  72. DEFINE_GUID ( /* 14b0dfd1-8410-45b7-a402-aba8ff9adcfc */
  73. W3WpControlGuid,
  74. 0x14b0dfd1,
  75. 0x8410,
  76. 0x45b7,
  77. 0xa4, 0x02, 0xab, 0xa8, 0xff, 0x9a, 0xdc, 0xfc
  78. );
  79. DEFINE_GUID ( /* 06b94d9a-b15e-456e-a4ef-37c984a2cb4b */
  80. AspControlGuid,
  81. 0x06b94d9a,
  82. 0xb15e,
  83. 0x456e,
  84. 0xa4, 0xef, 0x37, 0xc9, 0x84, 0xa2, 0xcb, 0x4b
  85. );
  86. //
  87. //--> Define any transaction Guids used
  88. //
  89. DEFINE_GUID ( /* d42cf7ef-de92-473e-8b6c-621ea663113a */
  90. IISEventGuid,
  91. 0xd42cf7ef,
  92. 0xde92,
  93. 0x473e,
  94. 0x8b, 0x6c, 0x62, 0x1e, 0xa6, 0x63, 0x11, 0x3a
  95. );
  96. DEFINE_GUID ( /* 00237f0d-73eb-4bcf-a232-126693595847 */
  97. IISFilterGuid,
  98. 0x00237f0d,
  99. 0x73eb,
  100. 0x4bcf,
  101. 0xa2, 0x32, 0x12, 0x66, 0x93, 0x59, 0x58, 0x47
  102. );
  103. DEFINE_GUID ( /* 2e94e6c7-eda0-4b73-9010-2529edce1c27 */
  104. IsapiEventGuid,
  105. 0x2e94e6c7,
  106. 0xeda0,
  107. 0x4b73,
  108. 0x90, 0x10, 0x25, 0x29, 0xed, 0xce, 0x1c, 0x27
  109. );
  110. DEFINE_GUID ( /* e2e55403-0d2e-4609-a470-be0da04013c0 */
  111. CgiEventGuid,
  112. 0xe2e55403,
  113. 0x0d2e,
  114. 0x4609,
  115. 0xa4, 0x70, 0xbe, 0x0d, 0xa0, 0x40, 0x13, 0xc0
  116. );
  117. DEFINE_GUID ( /* 0ecf983b-7115-4b77-a543-95d138ee4400 */
  118. StrmFiltEventGuid,
  119. 0x0ecf983b,
  120. 0x7115,
  121. 0x4b77,
  122. 0xa5, 0x43, 0x95, 0xd1, 0x38, 0xee, 0x44, 0x00
  123. );
  124. DEFINE_GUID ( /* 08b2b0ea-674b-4459-9b56-5f4051039083 */
  125. FiltProcessRead,
  126. 0x08b2b0ea,
  127. 0x674b,
  128. 0x4459,
  129. 0x9b, 0x56, 0x5f, 0x40, 0x51, 0x03, 0x90, 0x83
  130. );
  131. DEFINE_GUID ( /* 6d9a9ffd-27cf-4d8b-a9af-029a45155510 */
  132. FiltProcessWrite,
  133. 0x6d9a9ffd,
  134. 0x27cf,
  135. 0x4d8b,
  136. 0xa9, 0xaf, 0x02, 0x9a, 0x45, 0x15, 0x55, 0x10
  137. );
  138. DEFINE_GUID ( /* d353dc2d-3e55-4b88-a4ac-183c368362a3 */
  139. SslHandshake,
  140. 0xd353dc2d,
  141. 0x3e55,
  142. 0x4b88,
  143. 0xa4, 0xac, 0x18, 0x3c, 0x36, 0x83, 0x62, 0xa3
  144. );
  145. DEFINE_GUID ( /* 1514e887-9815-4fc5-88c4-64cb410083a4 */
  146. W3WpEvent,
  147. 0x1514e887,
  148. 0x9815,
  149. 0x4fc5,
  150. 0x88, 0xc4, 0x64, 0xcb, 0x41, 0x00, 0x83, 0xa4
  151. );
  152. DEFINE_GUID ( /* 1fc299fa-3fc4-4c37-910d-de5b911d0270 */
  153. AspEventGuid,
  154. 0x1fc299fa,
  155. 0x3fc4,
  156. 0x4c37,
  157. 0x91, 0x0d, 0xde, 0x5b, 0x91, 0x1d, 0x02, 0x70
  158. );
  159. class CEtwTracer {
  160. private:
  161. BOOL m_fTraceEnabled; // Set by the control Callback function
  162. BOOL m_fTraceSupported; // True if tracing is supported
  163. // (currently only W2K or above)
  164. BOOL m_fTraceInitialized; // True if we have initialized
  165. LONG m_lnRegistered; // How many calls to startup
  166. TRACEHANDLE m_hProviderReg; // Registration Handle to unregister
  167. TRACEHANDLE m_hTraceLogger; // Handle to Event Trace Logger
  168. ULONG m_ulEnableFlags; // Used to set various options
  169. ULONG m_ulEnableLevel; // used to control the level
  170. GUID m_guidProvider; // Control Guid for the Provider
  171. //
  172. // Additional BOOLEANs for TLS allocation, User Mode Buffering and
  173. // Lossless logging
  174. //
  175. public:
  176. /* Initialize Function
  177. * Desc: Registers provider guid with the event
  178. * tracer.
  179. * Ret: Returns the return value of RegisterTraceGuids
  180. ***********************************************/
  181. IRTL_DLLEXP ULONG Register(const GUID * ControlGuid,
  182. LPWSTR ImagePath,
  183. LPWSTR MofResourceName);
  184. /* DeInitialize Function
  185. * Desc: Unregisters the provider GUID with the
  186. * event tracer.
  187. * Ret: Return value of UnregisterTraceGuids.
  188. ***********************************************/
  189. IRTL_DLLEXP ULONG UnRegister();
  190. /* Send some event to Wmi
  191. * Desc: This function is essentially a wrapper to the
  192. * TraceEvent() call.
  193. * Ret: Returns the return code of TraceEvent()
  194. ***********************************************/
  195. IRTL_DLLEXP ULONG EtwTraceEvent(LPCGUID pGuid, ULONG EventType, ...);
  196. /* Class Constructor
  197. * Desc: Inits private members and guids
  198. ***********************************************/
  199. IRTL_DLLEXP CEtwTracer();
  200. /* Class Destructor
  201. * Desc: Does nothing
  202. ***********************************************/
  203. IRTL_DLLEXP ~CEtwTracer();
  204. /* ETW control callback
  205. * Desc: This function handles the ETW control
  206. * callback. It enables or disables tracing.
  207. * On enable, it also reads the flag and level
  208. * passed in by ETW, and does some error checking
  209. * to ensure that the parameters can be fulfilled.
  210. * Is protected in a Crit Sec
  211. * Ret: ERROR_SUCCESS on success
  212. * ERROR_INVALID_HANDLE if a bad handle is passed from ETW
  213. * ERROR_INVALID_PARAMETER if an invalid parameter is send by ETW
  214. ***********************************************/
  215. ULONG CtrlCallback(
  216. WMIDPREQUESTCODE RequestCode,
  217. PVOID Context,
  218. ULONG *InOutBufferSize,
  219. PVOID Buffer);
  220. /* Check if tracing is enabled
  221. * Desc: Returns the value of m_fTraceEnabled
  222. ***********************************************/
  223. IRTL_DLLEXP BOOL TraceEnabled() { return m_ulEnableLevel; };
  224. IRTL_DLLEXP BOOL TraceEnabled(ULONG Level)
  225. {
  226. ULONG IsEnabled = ((Level < ETWMAX_TRACE_LEVEL) ?
  227. (m_ulEnableLevel >> Level) :
  228. (m_ulEnableLevel >> ETWMAX_TRACE_LEVEL) );
  229. return (IsEnabled != 0);
  230. };
  231. IRTL_DLLEXP TRACEHANDLE QueryTraceHandle() { return m_hTraceLogger; }
  232. IRTL_DLLEXP ULONG GetEtwFlags() { return m_ulEnableFlags; };
  233. };
  234. //
  235. // Map CEtwTracer's CtrlCallback function into C callable function
  236. //
  237. extern "C" {
  238. ULONG WINAPI ControlCallback(
  239. WMIDPREQUESTCODE RequestCode,
  240. PVOID Context,
  241. ULONG *InOutBufferSize,
  242. PVOID Buffer);
  243. }
  244. //
  245. // The ONE and only ONE global instantiation of this class
  246. //
  247. extern CEtwTracer * g_pEtwTracer;
  248. #define ETW_IS_TRACE_ON(level) ( (g_pEtwTracer != NULL) && (g_pEtwTracer->TraceEnabled(level)) )
  249. #endif //_ETWTRACER_HXX_