我正在尝试听ETW内核事件。


步骤1:调用OpenTrace,指定EventCallback和可选的BufferCallback函数,这些函数将在我对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();

步骤2:Enable kernel events。这是通过调用StartTrace完成的。就我而言,我想跟踪内核interruptsEVENT_TRACE_FLAG_INTERRUPT)和deferred procedure callsEVENT_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));


并且日志成功启动(我可以看到foo.etl开始增长到其5 MB的循环限制)。
步骤3:调用ProcessTrace,它将阻塞直到将所有未决事件传递到步骤1中指定的EventCallback处理程序:

var
   res: LongWord;
begin
   res := EventTrace.ProcessTrace(@currentTrace, 1, nil, nil);
   if (res <> ERROR_SUCCESS) then
      raise EWin32Error.Create(SysErrorMessage(res));



除非ProcessTrace立即立即重复返回,并且没有调用任何回调-即使etl文件存在并且正在增长。



如果我将日志记录从基于文件的日志更改为实时日志:


步骤1-OpenTrace更改为支持实时:

logFile.ProcessTraceMode := PROCESS_TRACE_MODE_REAL_TIME;

步骤2-更改StartTrace以支持实时:

sessionProperties.LogFileMode := EVENT_TRACE_REAL_TIME_MODE;



在这种情况下,ProcessTrace永不返回,但不会调用EventCallbackBufferCallback

我究竟做错了什么?



更新:我的回调函数:

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;

最佳答案

我发现了问题。

我使用的标头不是四字对齐的。

用Delphi的话来说,正在使用关键字packed

09-04 16:52