次の方法で共有


DTrace プログラミング

DTrace では、D プログラミング言語がサポートされています。 このトピックでは、DTrace スクリプトの記述と使用を開始する方法について説明します。

Windows 上の DTrace の一般的な情報については、「 DTrace」を参照してください。

DTraceの詳細については、ケンブリッジ大学のOpenDTrace Specification version 1.0を参照してください。

DTrace は、バージョン 18980 以降の Windows の Insider ビルドと Windows Server ビルド 18975 でサポートされています。

その他のサンプル スクリプト

Windows シナリオに適用できる追加の D スクリプトは、DTrace ソース コードの samples ディレクトリで入手できます。

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

https://github.com/opendtrace/toolkitでは、一連の opentrace ツールキット スクリプトを使用できます。

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 ライブ ダンプ