i am trying to listen to ETW kernel events.
Step 1: Call OpenTrace
, specifying the EventCallback
and optional BufferCallback
functions that will be called during my call to ProcessTrace
:
var
logFile: EVENT_TRACE_LOGFILE;
currentTrace: TRACEHANDLE;
begin
ZeroMemory(@logFile, sizeof(logFile));
logFile.LoggerName := KERNEL_LOGGER_NAME;
logFile.LogFileName := 'C:\Users\Ian\foo.etl';
logFile.ProcessTraceMode := 0;
logFile.EventCallback := RealtimeEventCallback;
logFile.BufferCallback := BufferCallback; //optional
currentTrace := OpenTrace(@logFile);
if (currentTrace = INVALID_PROCESSTRACE_HANDLE) or (currentTrace = -1) then
RaiseLastWin32Error();
Step 2: Enable kernel events. This is done by calling StartTrace
. In my case i want to trace kernel interrupts (EVENT_TRACE_FLAG_INTERRUPT
) and deferred procedure calls (EVENT_TRACE_FLAG_DPC
):
var
sessionProperties: PEVENT_TRACE_PROPERTIES;
bufferSize: Int64;
th: TRACEHANDLE;
loggerName: string;
logFilePath: string;
begin
loggerName := KERNEL_LOGGER_NAME;
logFilePath := 'C:\Users\Ian\foo.etl';
bufferSize := sizeof(EVENT_TRACE_PROPERTIES)
+ 1024 //maximum session name is 1024 characters
+ 1024; //maximum log file name is 1024 characters
sessionProperties := AllocMem(bufferSize);
ZeroMemory(sessionProperties, bufferSize);
sessionProperties.Wnode.BufferSize := bufferSize;
sessionProperties.Wnode.ClientContext := 1; //QPC clock resolution
sessionProperties.Wnode.Flags := WNODE_FLAG_TRACED_GUID;
sessionProperties.Wnode.Guid := SystemTraceControlGuid;
sessionProperties.EnableFlags := EVENT_TRACE_FLAG_INTERRUPT or EVENT_TRACE_FLAG_DPC;
sessionProperties.LogFileMode := EVENT_TRACE_FILE_MODE_CIRCULAR;
sessionProperties.MaximumFileSize := 5; // 5 MB
sessionProperties.LoggerNameOffset := sizeof(EVENT_TRACE_PROPERTIES);
sessionProperties.LogFileNameOffset := sizeof(EVENT_TRACE_PROPERTIES)+1024;
//Copy LoggerName to the offset address
MoveMemory(Pointer(Cardinal(sessionProperties)+sessionProperties.LoggerNameOffset), PChar(loggerName), Length(loggerName)+1);
//Copy LogFilePath to the offset address
MoveMemory(Pointer(Cardinal(sessionProperties)+sessionProperties.LogFileNameOffset), PChar(logFilePath), Length(logFilePath)+1);
hr := StartTrace({var}th, PChar(loggerName), sessionProperties);
if (hr <> ERROR_SUCCESS) then
raise EWin32Error.Create(SysErrorMessage(hr));
And the log is started sucessfully (i can see foo.etl
begin to grow to its 5 MB circuluar limit).
Step 3: Call ProcessTrace
, which blocks until it has delivered all pending events to the EventCallback
handler specified in Step 1:
var
res: LongWord;
begin
res := EventTrace.ProcessTrace(@currentTrace, 1, nil, nil);
if (res <> ERROR_SUCCESS) then
raise EWin32Error.Create(SysErrorMessage(res));
Except that ProcessTrace
repeatedly returns immediately, and no callback is called - even though the etl file is present and growing.
If i change the logging from File Based to Realtime logging:
Step 1 - OpenTrace
changes to support realtime:
logFile.ProcessTraceMode := PROCESS_TRACE_MODE_REAL_TIME;
Step 2 - StartTrace
changes to support realtime:
sessionProperties.LogFileMode := EVENT_TRACE_REAL_TIME_MODE;
In this case ProcessTrace
never returns, but neither EventCallback
or BufferCallback
are ever called.
What am i doing wrong?
Update: My callback functions:
function BufferCallback(Logfile: PEVENT_TRACE_LOGFILE): LongWord; stdcall;
begin
ShowMessage('BufferCallback');
Result := 1; //return true to keep processing rows
end;
procedure RealtimeEventCallback(pEvent: PEVENT_TRACE); stdcall;
begin
ShowMessage('EventCallback');
nEvents := nEvents+1;
end;
i found the problem.
The headers i was using were not quad-word aligned.
In Delphi parlance, the keyword packed
was being used.