Commands for generating, reading and controlling Event Tracing for Windows
Windows includes a facility, Event Tracing for Windows (ETW), that allows the kernel and applications to log events that can either be written to a file or consumed in real time. This module provides access to this facility.
In ETW, events are written to an event trace by event providers under the control of event controllers which manage the mapping between the two. Event consumers read events from one or more such event traces for display to the user or any other purpose. TWAPI provides support for implementing all of the above.
There are two different technologies on which ETW is based. All modern (post-Windows XP up to Windows 7) versions of Windows support ETW based on Managed Object Format (MOF) event definitions. Windows Vista and later versions also implement a newer version of ETW based on XML manifests. TWAPI currently supports only MOF based event tracing.
An event trace consists of a set of buffers, and optionally an associated file, to which events are written. The trace has other associated parameters such as the time stamping granularity, access control information and buffer limits. These are configured through event controllers.
An event controller manages event traces and their association with event providers. The following TWAPI commands can be used to implement an event controller:
Note that the above commands can be used with any trace, not just those created using etw_start_trace. For example, traces created with the Windows logman or tracelog command can be controlled with the above command and vice versa.
Note that only processes running with elevated administrative privileges, or under accounts in the Performance Log Users group, and services running as LocalSystem, LocalService, NetworkService can start or control event traces.
Event providers are components that write events to an event trace. Association with a specific trace is not under the control of the provider; that is a controller function. Of course, a single application may host both an event provider as well as an event controller. The list of providers on a system can be retrieved through the etw_get_providers command. The command etw_get_provider_guid can be used to map the provider name to its GUID.
Controllers send configuration information to providers in the form of enable flags and the enable level. The interpretation of these is entirely to the discretion of the event provider. Neither Windows, nor TWAPI, interpret these in any way. The TWAPI commands etw_provider_enable_flags and etw_provider_enable_level can be used to obtain the current values.
By convention, applications treat the enable flags as a bit mask that indicates the class(es) of events to log. For example, each bit might correspond to a specific subsystem. Note that a value of 0 does not mean the provider is disabled. It is up to the application as to how a value of 0 is interpreted. To determine whether a provider is actually enabled in the sense of being attached to an event trace, use the etw_trace_enabled command.
Similarly, the enable level is generally treated as a severity level, but again, this is not mandated.
Before any controller can add a provider to an event trace, the provider has to register itself. TWAPI includes a predefined provider that can be registered through the etw_twapi_provider_register call. Correspondingly, the command etw_unregister_provider unregisters the provider. Note that this must be done after the provider is detached from any event traces else Windows exhibits anomalous but harmless behaviour where the provider's calls to write events to the event trace still work but the provider is not notified of changes in the enable flags and level or when the trace is stopped.
Once the TWAPI provider is registered and attached to an event trace, events can be logged to the trace with the etw_log_message command.
ETW event consumers read events from event traces in real time or from a log file. An example of an event consumers is the command line program tracerpt which comes with Windows. The ETW interface allows a consumer to read events from multiple traces and merges them as needed.
TWAPI provides the high-level etw_dump and etw_dump_to_file commands which can read and format events.
For more control over the processing, call etw_open_file or etw_open_session to open each log file or real time event trace of interest. Then invoke the commands etw_process_events and etw_format_events to process the events.
event traces opened with etw_open_session should be closed with etw_close_session after processing.
Events written via ETW can have arbitrary binary formats. In order for consumers to be able to parse and extract data, they need to know the event definition. Providers use either Managed Object Format or XML manifests (Vista and later) to provide consumers with this information. When reading events, TWAPI will automatically look up event definitions as needed.
In order for event consumers to correctly parse events, TWAPI also has to make the MOF definitions for its ETW events available. This is done by calling the etw_install_twapi_mof command. This must be done on the system where the event consumer runs. It is not required on the system where the events are generated.
There is a special event trace associated with the Windows kernel. The kernel logs events such as network events, process events, disk I/O etc. to this trace when enabled. This trace can be started and configured through the etw_start_kernel_trace command. The specific events to be logged are also specified as options to this command as the etw_enable_provider cannot be used with kernel traces.
See MSDN for the structure and description of the various events logged by the Windows kernel. The corresponding field names are returned by etw_format_events.
TWAPI includes commands etw_variable_tracker, etw_command_tracker and etw_execution_tracker that aid in logging Tcl variable, command and execution traces, respectively, to an ETW event trace.
set hprovider [etw_twapi_provider_register] # Assume provider is added to event trace by some other means # Add an execution trace to proc p trace add execution p {enter leave enterstep leavestep} [list twapi::etw_execution_tracker $hprovider]
This section describes the commands that allow implementation of an ETW controller.
buffer_size | Size of the buffers for the event trace. |
buffers_written | Number of buffers flushed. |
clock_resolution | One of qpc, system or cpucycle as described in etw_start_trace. |
enable_flags | The provider enable mask for the event trace. This is valid only for kernel traces in which case it contains the enable mask for the kernel components. |
events_lost | Number of events lost in the event trace that were not recorded. |
flush_timer | Number of seconds after which buffers are automatically flushed. |
free_buffers | Number of free buffers in the event trace. |
log_buffers_lost | Number of buffers lost without being consumed. |
logfile | The log file associated with the event trace. |
logfile_mode | Bit flags indicating logging mode for the event trace. |
logger_tid | Thread id of thread doing the logging for the event trace. |
max_buffers | Maximum number of buffers that can be allocated for the event trace. |
max_file_size | Maximum size specified for the file in megabytes. |
min_buffers | Minimum number of buffers that can be allocated for the event trace. |
buffer_count | Number of buffers in use. |
real_time_buffers_lost | Number of buffers used for real time tracing that could not be written to the log file. |
trace_guid | The GUID associated with the event trace. |
trace_name | The name of the event trace. |
alpc | Advanced local procedure calls (Vista and later) |
contextswitch | Thread context switches (Vista and later) |
dbgprint | Kernel DbgPrint calls |
diskfileio | File I/O |
diskio | Disk I/O |
dispatcher | Thread transitions to ready state (Windows 7 and later) |
dpc | Device deferred procedure calls (Vista and later) |
driver | Device driver requests (Vista and later) |
hardfault | Hard page faults |
imageload | Image loading |
interrupt | Interrupt service routines (Vista and later) |
pagefault | Page faults |
process | Processes events |
processcounter | Process counters (Vista and later) |
profile | Processor profiling samples (Vista and later) |
registry | Registry access |
splitio | Split I/O events (Vista and later) |
sysconfig | Includes events describing system configuration. These are collected when the trace is stopped. |
systemcall | System calls (Vista and later) |
tcpip | TCP and UDP events |
thread | Thread events |
vamap | Virtual address map and unmap events (Windows 8/Windows Server 2012 and later) |
virtualalloc | Virtual memory allocation (Windows 7/Windows Server 2008 R2 and later) |
-buffersize BUFFERSIZE | Specifies the size in kilobytes of the buffers used in the event trace. This is only an requested size and will be adjusted by ETW. | ||||||||
-clockresolution RESOLUTION | The clock resolution to use when timestamping events in the event trace. Specify RESOLUTION as qpc to use the query performance counter. This is the highest resolution but is relatively expensive. Specify system to use system time. This is lower resolution but is cheaper in run time cost and is the default. Specify cpucycle to use the CPU cycle counter. This has the highest resolution and is the cheapest to obtain. However it can be very inaccurate as it is affected by power and temperature management features of the CPU. | ||||||||
-flushtimer SECONDS | Normally, ETW buffers are flushed as they become full. Use this option to force flushing of buffers every SECONDS seconds even when they are not full. However note that ETW flushes entire buffers to log files so flushing half empty buffers can greatly increase the log file size. Generally, use this option only for real time event traces. | ||||||||
-filemode FILEMODE | Specifies the file logging mode. FILEMODE must be one of the following values:
|
||||||||
-logfile FILEPATH | The name of the log file in which to write events. This option need not be specified if the event trace is only a real time trace. FILEPATH may be relative or absolute but should have the .etl extension. Depending on the other options specified, the actual file name may be modified. | ||||||||
-maxbuffers MINBUFS | Specifies the minimum number of buffers to allocate. | ||||||||
-minbuffers MAXBUFS | Specifies the maximum number of buffers to allocate. | ||||||||
-maxfilesize MAXMB | The maximum size of the log file in megabytes. | ||||||||
-paged BOOLEAN | If true, paged memory is used for the event trace buffers. If false (default), non-paged memory is used. Kernel-mode providers cannot log to paged buffers. If these are not used in the trace, it is recommended to set this option to true as non-paged memory is a limited resource. | ||||||||
-preallocate BOOLEAN | If true, any required file space is preallocated. Default is false. | ||||||||
-private BOOLEAN | If true, creates a private event trace to which only threads within the provider's process can log events. There can be only one private event trace per process. On Windows Vista and later, only processes running as LocalSystem and elevated processes can create private event traces. On earlier versions of Windows, processes in the Performance Log Users group could also create private traces. When multiple processes register the provider, any log files created are appended with the PID of the process except for the one created by the first process. This option cannot be used with -realtime. On Windows versions prior to Windows 7, this option cannot be used if -filemode is specified as rotate. | ||||||||
-privateinproc BOOLEAN | If true, creates an in-process event trace and enforces that only the process that registered a provider guid can start a trace with that guid. Unlike the -private option, processes need not be running in privileges accounts and up to three traces can be created. Default is false. Requires Vista or later. | ||||||||
-realtime BOOLEAN | If true, events are delivered to any subscribed consumers in real time when event buffers are flushed. Certain guidelines have to be followed when real time mode is requested. Please refer to Windows SDK ETW documentation. This option cannot be used with -private and -privateinproc. Default is false. | ||||||||
-secure BOOLEAN | If true, logging to the event trace requires the TRACELOG_LOG_EVENT permission. Default is false.Requires Vista or later. | ||||||||
-sequence SEQUENCETYPE | Events logged with the TraceMessage Windows API can contain a sequence number. If SEQUENCETYPE is none (default), no sequence number is added to the event. If SEQUENCETYPE is global, the added sequence number is global across multiple event traces and if local, it is unique to a trace. | ||||||||
-traceguid GUID | The GUID to assign to the event trace. If not specified, Windows will internally generate the GUID for the trace. The primary purpose for specifying this is to allow for setting access control on the trace. |
-enableflags FLAGS | Changes the enable flags for the event trace. Only applies if the trace is a "NT Kernel Logger" trace. |
-flushtimer TIMERVAL | If non-0, changes the number of seconds after which buffers are automatically flushed. |
-logfile FILEPATH | If specified, changes the log file associated with the trace. |
-maxbuffers COUNT | If non-0, changes the maximum number of buffers that can be allocated for the trace. |
-realtime BOOLEAN | If true, turns on realtime mode, else turns it off. Note if the command is called to update other options, and this option is not specified, it results in the real time mode being turned off. |
This section describes the commands that allow implementation of an ETW provider.
This section describes the commands that deal with consuming events from ETW.
-format FORMAT | Specifies the output format. FORMAT must be list or csv. The CSV format requires that the Tcl package csv be available. |
-limit LIMIT | Stops after LIMIT events are written. |
-output OUTPUT | Specifies where to write the events. OUTPUT may be a channel or a file path. If the latter, the file must not exist. The default is standard output. |
-separator SEPARATOR | Specifies the separator character to use. Only used for CSV format. Defaults to the comma character. |
-channel | The numeric identifier for the event log channel to which the event was logged. |
-channelname | The name of the event log channel to which the event was logged. |
-eventguid | The GUID identifying the event. For manifest based events, this is empty. |
-eventid | The event identifier. |
-kerneltime | The elapsed execution time for kernel-mode instructions in 100ns units. If the event was logged from a private trace, this will be 0 as all time will be counted towards -usertime. |
-keywordmask | A bit mask corresponding to the keywords associated with the event. |
-keywords | The list of keywords associated with the event. |
-level | The numeric level at which the event was logged. Higher numeric levels are lower priority events. |
-levelname | The name of the logging level at which the event was logged. Since this is localized, -level should be used for comparing event logging levels. |
-message | The message associated with the event. This may contain string inserts for event parameters. Use the etw_format_event_message to retrieve the fully constructed message. |
-opcode | The numeric opcode for the event, generally identifying one step in a sequence of operations. |
-opcodename | The string name for the numeric opcode. For MOF based events, this is the event type. |
-pid | The process identifier of the process that logged the event. This will be -1 if the event was not associated with any process. |
-properties | A dictionary containing additional event parameters. The keys of the dictionary are dependent on the specific event type. |
-providerguid | The GUID identifying the provider. |
-providername | The name of the provider. For classical MOF based provider, this is the name of the MOF class. |
-sid | The SID of the logging process or an empty string if not available. |
-task | The numeric task identifying the general area within the application. |
-taskname | The string name associated with task. |
-tid | The thread identifier of the process that logged the event. This will be -1 if the event was not associated with any thread. |
-timecreated | The time that the event was logged in system time format. Use large_system_time_to_secs_since_1970 to convert this to Tcl clock format. |
-usertime | The elapsed execution time for user-mode instructions in 100ns units. |
-version | The version of the event. Together with id, this identifies a specific event within a provider. |
return -code breakThe options -start and -end can be specified to limit the time interval for which events are processed. Both STARTTIME and ENDTIME must be specified in the same format as get_system_time. You can use secs_since_1970_to_large_system_time to convert the format used by Tcl's clock command to this format.
Copyright © 2012 Ashok P. Nadkarni