Share via


DTrace 程式設計

DTrace 支援 D 程式設計語言。 本主題描述如何開始撰寫和使用 DTrace 腳本。

如需 Windows 上 DTrace 的一般資訊,請參閱 DTrace

如需有關 DTrace 的詳細資訊,請參閱 University Of 一家大學 1.0 版的 OpenDTrace 規格

注意

在 18980 版和 Windows Server 組建 18975 之後,Windows 測試人員組建支援 DTrace。

其他範例腳本

適用于 Windows 案例的其他 D 腳本可在 DTrace 原始程式碼的範例目錄中取得。

https://github.com/microsoft/DTrace-on-Windows/tree/windows/samples/windows

有一組 opentrace 工具組腳本可在 取得 https://github.com/opendtrace/toolkit

Hello World

DTrace 腳本是包含命令和 D 程式設計腳本元素的簡單文字檔。

dtrace:::BEGIN
{
  trace("Hello World from DTrace!");
  exit(0);
}

將檔案儲存為 helloworld.d。

以系統管理員身分開啟命令提示字元視窗,並使用 -s 選項執行腳本。

dtrace -s helloworld.d
dtrace: script '.\helloworld.d' matched 1 probe
CPU     ID                    FUNCTION:NAME
  0      1                           :BEGIN   Hello World from DTrace!

NtCreateUserProcess 傳回時間

您可以撰寫 DTrace 腳本,以追蹤跨多個函式/事件所花費的時間。 以下是在建立程式的專案/傳回之間追蹤 NtCreateUserProcess 函式的簡單範例。


syscall::NtCreateUserProcess:entry
{
    self->ts = timestamp;
}

syscall::NtCreateUserProcess:return
{
    printf(" [Caller %s]: Time taken to return from create process is %d MicroSecond \n", execname, (timestamp - self->ts)/ 1000);
}

將檔案儲存為 ntcreatetime.d,並使用 -s 選項來執行測試腳本。

C:\Windows\system32>dtrace -s ntcreatetime.d
dtrace: script 'ntcreatetime.d' matched 2 probes
CPU     ID                    FUNCTION:NAME
  0    183       NtCreateUserProcess:return  [Caller svchost.exe]: Time taken to return from create process is 51191 MicroSecond

  0    183       NtCreateUserProcess:return  [Caller SearchIndexer.]: Time taken to return from create process is 84418 MicroSecond

  0    183       NtCreateUserProcess:return  [Caller SearchIndexer.]: Time taken to return from create process is 137961 MicroSecond

檔案刪除追蹤器

此範例腳本會使用 syscall 提供者在專案上檢測 NtOpenFile,並檢查傳遞的旗標 (引數 #5) ,以追蹤整個系統的刪除。

將下列腳本複製到 filedeletetracker.d。

ERROR{exit(0);}

struct ustr{uint16_t buffer[256];};

syscall::NtOpenFile:entry
{
   this->deleted = arg5 & 0x00001000; /* & with FILE_DELETE_ON_CLOSE */

  if (this->deleted) {
        this->attr = (nt`_OBJECT_ATTRIBUTES*)
            copyin(arg2, sizeof(nt`_OBJECT_ATTRIBUTES));

        if (this->attr->ObjectName) {
            this->objectName = (nt`_UNICODE_STRING*)
                copyin((uintptr_t)this->attr->ObjectName,
                       sizeof(nt`_UNICODE_STRING));
          
            this->fname = (uint16_t*)
                copyin((uintptr_t)this->objectName->Buffer,
                       this->objectName->Length);

            printf("Process %s PID %d deleted file %*ws \n", execname,pid, 
			this->objectName->Length / 2, 
			 ((struct ustr*)this->fname)->buffer);
        }
    }
}

使用 -s 選項來執行測試腳本。

建立或找出您想要刪除的檔案。 將檔案移至回收站,然後清空回收站。 刪除檔案並引發 事件時,將會顯示檔案刪除的相關資訊。

C:\Windows\system32>dtrace -s filedeletetracker.d
dtrace: script 'filedeletetracker.d' matched 8 probes
CPU     ID                    FUNCTION:NAME
  0    512                 NtOpenFile:entry Process explorer.exe PID 4684 deleted file \??\C:\$Recycle.Bin\S-1-12-1-3310478672-1302480547-4207937687-2985363607\$ROSR3FA.txt

此程式的設計目的是要繼續監視檔案刪除。 按 CTRL+C 結束。

如需其他較大的程式碼範例,請參閱下一個主題 DTrace Windows 程式碼範例

另請參閱

Windows 上的 DTrace

DTrace ETW

DTrace Windows 程式碼範例

DTrace Live Dump