DTrace 支援 D 程式語言。 本主題說明如何開始撰寫和使用 DTrace 指令碼。
如需 Windows 上 DTrace 的一般資訊,請參閱 DTrace。
如需 DTrace 的詳細資訊,請參閱劍橋大學的 OpenDTrace 規格 1.0 版 。
備註
DTrace 在 18980 版和 Windows Server 組建 18975 之後的 Windows 測試人員組建中受支援。
其他範例指令碼
適用於 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 程式碼範例。