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.

625 lines
15 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. tracep.h
  5. Abstract:
  6. Trace Consumer header file
  7. Author:
  8. 07-May-2002 Melur Raghuraman
  9. Revision History:
  10. --*/
  11. #define INITGUID
  12. #include "wmiump.h"
  13. #include "traceump.h"
  14. #include "evntrace.h"
  15. #include "ntperf.h"
  16. #define DEFAULT_LOG_BUFFER_SIZE 1024
  17. #define DEFAULT_REALTIME_BUFFER_SIZE 32768
  18. #define MAXBUFFERS 1024
  19. #define MAX_TRACE_BUFFER_CACHE_SIZE 29
  20. extern ULONG WmipTraceDebugLevel;
  21. #define ETW_LEVEL_MIN 0
  22. #define ETW_LEVEL_API 1
  23. #define ETW_LEVEL_BUFFER 2
  24. #define ETW_LEVEL_EVENT 3
  25. #define ETW_LEVEL_MAX 4
  26. #define ETW_LOG_ERROR() (WmipTraceDebugLevel >> ETW_LEVEL_MIN)
  27. #define ETW_LOG_API() (WmipTraceDebugLevel >> ETW_LEVEL_API)
  28. #define ETW_LOG_BUFFER() (WmipTraceDebugLevel >> ETW_LEVEL_BUFFER)
  29. #define ETW_LOG_EVENT() (WmipTraceDebugLevel >> ETW_LEVEL_EVENT)
  30. #define ETW_LOG_MAX() (WmipTraceDebugLevel >> ETW_LEVEL_MAX)
  31. //
  32. // Kernel Events are logged with SYSTEM_TRACE_HEADER or PERFINFO_TRACE_HEADER.
  33. // These headers have a GroupType and not a Guid in the header. In post-processing
  34. // we map the Grouptype to Guid transparently to the consumer.
  35. // The mapping between GroupType and Guid is maintained by this structure.
  36. //
  37. typedef struct _TRACE_GUID_MAP { // used to map GroupType to Guid
  38. ULONG GroupType; // Group & Type
  39. GUID Guid; // Guid
  40. } TRACE_GUID_MAP, *PTRACE_GUID_MAP;
  41. //
  42. // In W2K and WinXP, TraceEventInstance API replaced the Guids in the header
  43. // with GuidHandle values. In postprocessing we need to replace the GuidHandle
  44. // back to Guid transparently to the consumer. The mapping between GuidHandle
  45. // and Guid is maintained by EVENT_GUID_MAP structure. This is obsolete in
  46. // .NET and above.
  47. //
  48. typedef struct _EVENT_GUID_MAP {
  49. LIST_ENTRY Entry;
  50. ULONGLONG GuidHandle;
  51. GUID Guid;
  52. } EVENT_GUID_MAP, *PEVENT_GUID_MAP;
  53. //
  54. // Callback routines wired through SetTraceCallback API is maintained by
  55. // the EVENT_TRACE_CALLBACK structure. This is global for the process at the
  56. // moment mainly due to the way the API is designed.
  57. //
  58. typedef struct _EVENT_TRACE_CALLBACK {
  59. LIST_ENTRY Entry;
  60. GUID Guid;
  61. PEVENT_CALLBACK CallbackRoutine;
  62. } EVENT_TRACE_CALLBACK, *PEVENT_TRACE_CALLBACK;
  63. //
  64. // If the tracelog instance is a realtime data feed instead of from a
  65. // tracefile, TRACELOG_REALTIME_CONTEXT is used to maintain the real time
  66. // buffers in a buffer pool.
  67. //
  68. typedef struct _TRACE_BUFFER_SPACE {
  69. ULONG Reserved; // amount of memory reserved
  70. ULONG Committed;
  71. PVOID Space;
  72. LIST_ENTRY FreeListHead;
  73. } TRACE_BUFFER_SPACE, *PTRACE_BUFFER_SPACE;
  74. typedef struct _TRACELOG_REALTIME_CONTEXT {
  75. ULONG BuffersProduced; // Number of Buffers to read
  76. ULONG BufferOverflow; // Number of Buffers missed by the consumer
  77. GUID InstanceGuid; // Logger Instance Guid
  78. HANDLE MoreDataEvent; // Event to signal there is more data in this stream
  79. PTRACE_BUFFER_SPACE EtwpTraceBufferSpace;
  80. PWNODE_HEADER RealTimeBufferPool[MAXBUFFERS];
  81. } TRACELOG_REALTIME_CONTEXT, *PTRACELOG_REALTIME_CONTEXT;
  82. //
  83. // RealTime Free Buffer Pool is chained up as TRACE_BUFFER_HEADER
  84. //
  85. typedef struct _TRACE_BUFFER_HEADER {
  86. WNODE_HEADER Wnode;
  87. LIST_ENTRY Entry;
  88. } TRACE_BUFFER_HEADER, *PTRACE_BUFFER_HEADER;
  89. typedef struct _TRACERT_BUFFER_LIST_ENTRY {
  90. ULONG Size;
  91. LIST_ENTRY Entry;
  92. LIST_ENTRY BufferListHead;
  93. } TRACERT_BUFFER_LIST_ENTRY, *PTRACERT_BUFFER_LIST_ENTRY;
  94. typedef struct _TRACE_BUFFER_CACHE_ENTRY {
  95. LONG Index;
  96. PVOID Buffer;
  97. } TRACE_BUFFER_CACHE_ENTRY, *PTRACE_BUFFER_CACHE_ENTRY;
  98. struct _TRACE_BUFFER_LIST_ENTRY;
  99. typedef struct _TRACE_BUFFER_LIST_ENTRY {
  100. struct _TRACE_BUFFER_LIST_ENTRY *Next;
  101. LONG FileOffset; // Offset in File of this Buffer
  102. ULONG BufferOffset; // Offset in Buffer for the current event
  103. ULONG Flags; // Flags on status of this buffer
  104. ULONG EventSize;
  105. ULONG ClientContext; // Alignment, ProcessorNumber
  106. ULONG TraceType; // Current Event Type
  107. EVENT_TRACE Event; // CurrentEvent of this Buffer
  108. } TRACE_BUFFER_LIST_ENTRY, *PTRACE_BUFFER_LIST_ENTRY;
  109. typedef struct _TRACELOG_CONTEXT {
  110. LIST_ENTRY Entry; // Keeps track of storage allocations.
  111. //
  112. // This implements a caching scheme for Sequential files with repeated
  113. // call to ProcessTrace.
  114. //
  115. ULONGLONG OldMaxReadPosition; // Maximum Read Position for the file.
  116. // Only valid for Sequential, used for
  117. // Read Behind.
  118. // Upon Exit From ProcessTrace, this
  119. // value can be cached to avoid rescanning.
  120. LONGLONG LastTimeStamp;
  121. // Fields from HandleListEntry
  122. EVENT_TRACE_LOGFILEW Logfile;
  123. TRACEHANDLE TraceHandle;
  124. ULONG ConversionFlags; // Indicates event processing options
  125. LONG BufferBeingRead;
  126. OVERLAPPED AsynchRead;
  127. //
  128. // Fields Below this will be reset upon ProcessTrace exit.
  129. //
  130. BOOLEAN fProcessed;
  131. USHORT LoggerId; // Logger Id of this DataFeed.
  132. UCHAR IsRealTime; // Flag to tell if this feed is RT.
  133. UCHAR fGuidMapRead;
  134. LIST_ENTRY GuidMapListHead; // This is LogFile specific property
  135. //
  136. // For using PerfClock, we need to save startTime, Freq
  137. //
  138. ULONG UsePerfClock;
  139. ULONG CpuSpeedInMHz;
  140. LARGE_INTEGER PerfFreq; // Frequency from the LogFile
  141. LARGE_INTEGER StartTime; // Start Wall clock time
  142. LARGE_INTEGER StartPerfClock; // Start PerfClock value
  143. union
  144. {
  145. HANDLE Handle; // NT handle to logfile
  146. PTRACELOG_REALTIME_CONTEXT RealTimeCxt; // Ptr to Real Time Context
  147. };
  148. ULONG EndOfFile; // Flag to show whether this stream is still active.
  149. ULONG BufferSize;
  150. ULONG BufferCount;
  151. ULONG StartBuffer; // Start of the Circular Buffers
  152. ULONG FirstBuffer; // Jump off point to start reading
  153. ULONG LastBuffer; // last of the buffers in the boundary
  154. PTRACE_BUFFER_LIST_ENTRY Root;
  155. PTRACE_BUFFER_LIST_ENTRY BufferList;
  156. PVOID BufferCacheSpace;
  157. TRACE_BUFFER_CACHE_ENTRY BufferCache[MAX_TRACE_BUFFER_CACHE_SIZE];
  158. LIST_ENTRY StreamListHead; // Used to free the Stream datastructures
  159. ULONGLONG MaxReadPosition;
  160. PERFINFO_GROUPMASK PerfGroupMask;
  161. ULONG CbCount;
  162. } TRACELOG_CONTEXT, *PTRACELOG_CONTEXT;
  163. //
  164. // Each LogFile supplied to ProcessTrace consists of a number
  165. // of streams. Each Stream has the following structure.
  166. //
  167. typedef struct _TRACE_STREAM_CONTEXT {
  168. LIST_ENTRY Entry; // SortList Entry
  169. LIST_ENTRY AllocEntry;// Used to free storage
  170. EVENT_TRACE CurrentEvent;
  171. ULONG EventCount; // Number of Events detected in current buffer
  172. ULONG CbCount;
  173. ULONG ScanDone; // For circular logfiles
  174. BOOLEAN bActive; // Is this Stream still active?
  175. USHORT Type; // StreamType
  176. ULONG ProcessorNumber; // Processor Number for this stream
  177. ULONG CurrentOffset; // Offset into the buffer
  178. ULONGLONG ReadPosition; // BufferCount starts with 0 for first buffer
  179. PTRACELOG_CONTEXT pContext; // back pointer to the LogFileContext
  180. // Need this only for GetNextBuffer?
  181. PVOID StreamBuffer; // CurrentBuffer for this stream
  182. } TRACE_STREAM_CONTEXT, *PTRACE_STREAM_CONTEXT;
  183. //
  184. // this structure is used only by EtwpGetBuffersWrittenFromQuery() and
  185. // EtwpCheckForRealTimeLoggers()
  186. //
  187. typedef struct _ETW_QUERY_PROPERTIES {
  188. EVENT_TRACE_PROPERTIES TraceProp;
  189. WCHAR LoggerName[MAXSTR];
  190. WCHAR LogFileName[MAXSTR];
  191. } ETW_QUERY_PROPERTIES, *PETW_QUERY_PROPERTIES;
  192. extern ETW_QUERY_PROPERTIES QueryProperties;
  193. extern PLIST_ENTRY EventCallbackListHead;
  194. extern ULONG WmiTraceAlignment;
  195. //
  196. // This TraceHandleListHeadPtr should be the only real global
  197. // for ProcessTrace to be multithreaded
  198. //
  199. extern PLIST_ENTRY TraceHandleListHeadPtr;
  200. extern PTRACE_GUID_MAP EventMapList; // Array mapping the Grouptype to Guids
  201. #define EtwpNtStatusToDosError(Status) ((ULONG)((Status == STATUS_SUCCESS)?ERROR_SUCCESS:RtlNtStatusToDosError(Status)))
  202. extern
  203. ULONG
  204. WMIAPI
  205. EtwNotificationRegistrationW(
  206. IN LPGUID Guid,
  207. IN BOOLEAN Enable,
  208. IN PVOID DeliveryInfo,
  209. IN ULONG_PTR DeliveryContext,
  210. IN ULONG Flags
  211. );
  212. extern
  213. ULONG
  214. WMIAPI
  215. EtwControlTraceA(
  216. IN TRACEHANDLE LoggerHandle,
  217. IN LPCSTR LoggerName,
  218. IN OUT PEVENT_TRACE_PROPERTIES Properties,
  219. IN ULONG Control
  220. );
  221. extern
  222. ULONG
  223. WMIAPI
  224. EtwControlTraceW(
  225. IN TRACEHANDLE LoggerHandle,
  226. IN LPCWSTR LoggerName,
  227. IN OUT PEVENT_TRACE_PROPERTIES Properties,
  228. IN ULONG Control
  229. );
  230. __inline __int64 EtwpGetSystemTime()
  231. {
  232. LARGE_INTEGER SystemTime;
  233. //
  234. // Read system time from shared region.
  235. //
  236. do {
  237. SystemTime.HighPart = USER_SHARED_DATA->SystemTime.High1Time;
  238. SystemTime.LowPart = USER_SHARED_DATA->SystemTime.LowPart;
  239. } while (SystemTime.HighPart != USER_SHARED_DATA->SystemTime.High2Time);
  240. return SystemTime.QuadPart;
  241. }
  242. extern
  243. BOOL
  244. EtwpSynchReadFile(
  245. HANDLE LogFile,
  246. LPVOID Buffer,
  247. DWORD NumberOfBytesToRead,
  248. LPDWORD NumberOfBytesRead,
  249. LPOVERLAPPED Overlapped
  250. );
  251. extern
  252. __inline
  253. ULONG
  254. EtwpSetDosError(
  255. IN ULONG DosError
  256. );
  257. extern
  258. PVOID
  259. EtwpMemCommit(
  260. IN PVOID Buffer,
  261. IN SIZE_T Size
  262. );
  263. extern
  264. ULONG
  265. EtwpMemFree(
  266. IN PVOID Buffer
  267. );
  268. extern
  269. PVOID
  270. EtwpMemReserve(
  271. IN SIZE_T Size
  272. );
  273. __inline Move64(
  274. IN PLARGE_INTEGER pSrc,
  275. OUT PLARGE_INTEGER pDest
  276. )
  277. {
  278. pDest->LowPart = pSrc->LowPart;
  279. pDest->HighPart = pSrc->HighPart;
  280. }
  281. #ifdef DBG
  282. void
  283. EtwpDumpEvent(
  284. PEVENT_TRACE pEvent
  285. );
  286. void
  287. EtwpDumpGuid(
  288. LPGUID
  289. );
  290. void
  291. EtwpDumpCallbacks();
  292. #endif
  293. ULONG
  294. EtwpConvertEnumToTraceType(
  295. WMI_HEADER_TYPE eTraceType
  296. );
  297. WMI_HEADER_TYPE
  298. EtwpConvertTraceTypeToEnum(
  299. ULONG TraceType
  300. );
  301. ULONG
  302. EtwpCheckForRealTimeLoggers(
  303. PEVENT_TRACE_LOGFILEW *Logfiles,
  304. ULONG LogfileCount,
  305. ULONG Unicode
  306. );
  307. ULONG
  308. EtwpLookforRealTimeBuffers(
  309. PEVENT_TRACE_LOGFILEW logfile
  310. );
  311. ULONG
  312. EtwpRealTimeCallback(
  313. IN PWNODE_HEADER Wnode,
  314. IN ULONG_PTR Context
  315. );
  316. void
  317. EtwpFreeRealTimeContext(
  318. PTRACELOG_REALTIME_CONTEXT RTCxt
  319. );
  320. ULONG
  321. EtwpSetupRealTimeContext(
  322. PTRACEHANDLE HandleArray,
  323. PEVENT_TRACE_LOGFILEW *Logfiles,
  324. ULONG LogfileCount
  325. );
  326. PVOID
  327. EtwpAllocTraceBuffer(
  328. PTRACELOG_REALTIME_CONTEXT RTCxt,
  329. ULONG BufferSize
  330. );
  331. VOID
  332. EtwpFreeTraceBuffer(
  333. PTRACELOG_REALTIME_CONTEXT RTCxt,
  334. PVOID Buffer
  335. );
  336. ULONG
  337. EtwpProcessRealTimeTraces(
  338. PTRACEHANDLE HandleArray,
  339. PEVENT_TRACE_LOGFILEW *Logfiles,
  340. ULONG LogfileCount,
  341. LONGLONG StartTime,
  342. LONGLONG EndTime,
  343. ULONG Unicode
  344. );
  345. //
  346. // Routines used in this file only
  347. //
  348. ULONG
  349. EtwpDoEventCallbacks(
  350. PEVENT_TRACE_LOGFILEW logfile,
  351. PEVENT_TRACE pEvent
  352. );
  353. ULONG
  354. EtwpCreateGuidMapping(void);
  355. LPGUID
  356. EtwpGuidMapHandleToGuid(
  357. PLIST_ENTRY GuidMapListHeadPtr,
  358. ULONGLONG GuidHandle
  359. );
  360. void
  361. EtwpCleanupGuidMapList(
  362. PLIST_ENTRY GuidMapListHeadPtr
  363. );
  364. PTRACELOG_CONTEXT
  365. EtwpLookupTraceHandle(
  366. TRACEHANDLE TraceHandle
  367. );
  368. void
  369. EtwpCleanupTraceLog(
  370. PTRACELOG_CONTEXT pEntry,
  371. BOOLEAN bSaveLastOffset
  372. );
  373. VOID
  374. EtwpGuidMapCallback(
  375. PLIST_ENTRY GuidMapListHeadPtr,
  376. PEVENT_TRACE pEvent
  377. );
  378. ULONG
  379. EtwpProcessGuidMaps(
  380. PEVENT_TRACE_LOGFILEW *Logfiles,
  381. ULONG LogfileCount,
  382. ULONG Unicode
  383. );
  384. PEVENT_TRACE_CALLBACK
  385. EtwpGetCallbackRoutine(
  386. LPGUID pGuid
  387. );
  388. VOID
  389. EtwpFreeCallbackList();
  390. ULONG
  391. EtwpProcessLogHeader(
  392. PTRACEHANDLE HandleArray,
  393. PEVENT_TRACE_LOGFILEW *Logfiles,
  394. ULONG LogfileCount,
  395. ULONG Unicode,
  396. ULONG bFree
  397. );
  398. ULONG
  399. EtwpProcessTraceLog(
  400. PTRACEHANDLE HandleArray,
  401. PEVENT_TRACE_LOGFILEW *Logfiles,
  402. ULONG LogfileCount,
  403. LONGLONG StartTime,
  404. LONGLONG EndTime,
  405. ULONG Unicode
  406. );
  407. ULONG
  408. EtwpProcessTraceLogEx(
  409. PTRACEHANDLE HandleArray,
  410. PEVENT_TRACE_LOGFILEW *Logfiles,
  411. ULONG LogfileCount,
  412. LONGLONG StartTime,
  413. LONGLONG EndTime,
  414. ULONG Unicode
  415. );
  416. ULONG
  417. EtwpParseTraceEvent(
  418. IN PTRACELOG_CONTEXT pContext,
  419. IN PVOID LogBuffer,
  420. IN ULONG Offset,
  421. IN WMI_HEADER_TYPE HeaderType,
  422. IN OUT PVOID EventInfo,
  423. IN ULONG EventInfoSize
  424. );
  425. ULONG
  426. EtwpGetBuffersWrittenFromQuery(
  427. LPWSTR LoggerName
  428. );
  429. VOID
  430. EtwpCopyLogHeader (
  431. IN PTRACE_LOGFILE_HEADER pLogFileHeader,
  432. IN PVOID MofData,
  433. IN ULONG MofLength,
  434. IN PWCHAR *LoggerName,
  435. IN PWCHAR *LogFileName,
  436. IN ULONG Unicode
  437. );
  438. ULONG
  439. EtwpSetupLogFileStreams(
  440. PLIST_ENTRY pStreamListHead,
  441. PTRACEHANDLE HandleArray,
  442. PEVENT_TRACE_LOGFILEW *Logfiles,
  443. ULONG LogfileCount,
  444. LONGLONG StartTime,
  445. LONGLONG EndTime,
  446. ULONG Unicode
  447. );
  448. ULONG
  449. EtwpGetLastBufferWithMarker(
  450. PTRACELOG_CONTEXT pContext
  451. );
  452. ULONG
  453. EtwpAddTraceStream(
  454. PLIST_ENTRY pStreamListHead,
  455. PTRACELOG_CONTEXT pContext,
  456. USHORT StreamType,
  457. LONGLONG StartTime,
  458. LONGLONG EndTime,
  459. ULONG ProcessorNumber
  460. );
  461. ULONG
  462. EtwpGetNextBuffer(
  463. PTRACE_STREAM_CONTEXT pStream
  464. );
  465. ULONG
  466. EtwpAdvanceToNewEventEx(
  467. PLIST_ENTRY pStreamListHead,
  468. PTRACE_STREAM_CONTEXT pStream
  469. );
  470. ULONG
  471. EtwpGetNextEventOffsetType(
  472. PUCHAR pBuffer,
  473. ULONG Offset,
  474. PULONG RetSize
  475. );
  476. ULONG
  477. EtwpCopyCurrentEvent(
  478. PTRACELOG_CONTEXT pContext,
  479. PVOID pHeader,
  480. PEVENT_TRACE pEvent,
  481. ULONG TraceType,
  482. PWMI_BUFFER_HEADER LogBuffer
  483. );
  484. LPGUID
  485. EtwpGroupTypeToGuid(
  486. ULONG GroupType
  487. );
  488. VOID
  489. EtwpCalculateCurrentTime (
  490. OUT PLARGE_INTEGER DestTime,
  491. IN PLARGE_INTEGER TimeValue,
  492. IN PTRACELOG_CONTEXT pContext
  493. );