在支持 DTrace 動態(tài)跟蹤的平臺上,可以配置 PHP 打開 DTrace 靜態(tài)探針。
請參考操作系統(tǒng)文檔獲取啟用操作系統(tǒng) DTrace 支持的信息。 例如,在 Oracle Linux 系統(tǒng)上啟動 UEK3 內核,并進行如下操作:
# modprobe fasttrap
# chmod 666 /dev/dtrace/helper
除了 chmod
命令,您也可以使用 ACL 包規(guī)則來限制特定用戶對于設備的訪問權限。
使用 --enable-dtrace
配置參數(shù)構建 PHP:
# ./configure --enable-dtrace ...
# make
# make install
這樣就啟用了 PHP 核心的靜態(tài)探針。對于提供了自有探針的 PHP 擴展需要分別構建。
探針名稱 | 探針描述 | 探針參數(shù) |
---|---|---|
request-startup |
請求開始時觸發(fā)。 | char *file, char *request_uri, char *request_method |
request-shutdown |
請求關閉時觸發(fā)。 | char *file, char *request_uri, char *request_method |
compile-file-entry |
腳本開始編譯時觸發(fā)。 | char *compile_file, char *compile_file_translated |
compile-file-return |
腳本完成編譯時觸發(fā)。 | char *compile_file, char *compile_file_translated |
execute-entry |
操作數(shù)數(shù)組開始執(zhí)行時觸發(fā)。例如:函數(shù)調用,文件包含以及生成器恢復時會被觸發(fā)。 | char *request_file, int lineno |
execute-return |
操作數(shù)數(shù)組執(zhí)行完畢之后觸發(fā)。 | char *request_file, int lineno |
function-entry |
PHP 引擎進入 PHP 函數(shù)或者方法調用時觸發(fā)。 | char *function_name, char *request_file, int lineno, char *classname, char *scope |
function-return |
PHP 引擎從 PHP 函數(shù)或者方法調用返回后觸發(fā)。. | char *function_name, char *request_file, int lineno, char *classname, char *scope |
exception-thrown |
有異常拋出時觸發(fā)。 | char *classname |
exception-caught |
有異常被捕獲時觸發(fā)。 | char *classname |
error |
無論 error_reporting 的設定如何,在發(fā)生錯誤時都會觸發(fā)。 | char *errormsg, char *request_file, int lineno |
PHP 擴展可以擁有額外的靜態(tài)探針。
要列出 PHP 中可用的靜態(tài)探針,開啟一個 PHP 進程,然后運行:
# dtrace -l
輸出信息類似如下所示:
ID PROVIDER MODULE FUNCTION NAME [ . . . ] 4 php15271 php dtrace_compile_file compile-file-entry 5 php15271 php dtrace_compile_file compile-file-return 6 php15271 php zend_error error 7 php15271 php ZEND_CATCH_SPEC_CONST_CV_HANDLER exception-caught 8 php15271 php zend_throw_exception_internal exception-thrown 9 php15271 php dtrace_execute_ex execute-entry 10 php15271 php dtrace_execute_internal execute-entry 11 php15271 php dtrace_execute_ex execute-return 12 php15271 php dtrace_execute_internal execute-return 13 php15271 php dtrace_execute_ex function-entry 14 php15271 php dtrace_execute_ex function-return 15 php15271 php php_request_shutdown request-shutdown 16 php15271 php php_request_startup request-startup
Provider 一列由 php
和當前進程 id 的組成。
如果運行的是 Apache web 服務器,那么模塊名稱可能是 libphp5.so, 并且可能會出現(xiàn)多塊信息,每個運行中的 Apache 進程對應一個輸出塊。
Function Name 一列表示 Provider 對應的 PHP 內部 C 實現(xiàn)函數(shù)名稱。
如果沒有運行任何 PHP 進程,那么就不會顯示任何 PHP 探針。
下例展示了基本的 DTrace D 腳本。
示例 #1 all_probes.d for tracing all PHP Static Probes with DTrace
#!/usr/sbin/dtrace -Zs #pragma D option quiet php*:::compile-file-entry { printf("PHP compile-file-entry\n"); printf(" compile_file %s\n", copyinstr(arg0)); printf(" compile_file_translated %s\n", copyinstr(arg1)); } php*:::compile-file-return { printf("PHP compile-file-return\n"); printf(" compile_file %s\n", copyinstr(arg0)); printf(" compile_file_translated %s\n", copyinstr(arg1)); } php*:::error { printf("PHP error\n"); printf(" errormsg %s\n", copyinstr(arg0)); printf(" request_file %s\n", copyinstr(arg1)); printf(" lineno %d\n", (int)arg2); } php*:::exception-caught { printf("PHP exception-caught\n"); printf(" classname %s\n", copyinstr(arg0)); } php*:::exception-thrown { printf("PHP exception-thrown\n"); printf(" classname %s\n", copyinstr(arg0)); } php*:::execute-entry { printf("PHP execute-entry\n"); printf(" request_file %s\n", copyinstr(arg0)); printf(" lineno %d\n", (int)arg1); } php*:::execute-return { printf("PHP execute-return\n"); printf(" request_file %s\n", copyinstr(arg0)); printf(" lineno %d\n", (int)arg1); } php*:::function-entry { printf("PHP function-entry\n"); printf(" function_name %s\n", copyinstr(arg0)); printf(" request_file %s\n", copyinstr(arg1)); printf(" lineno %d\n", (int)arg2); printf(" classname %s\n", copyinstr(arg3)); printf(" scope %s\n", copyinstr(arg4)); } php*:::function-return { printf("PHP function-return\n"); printf(" function_name %s\n", copyinstr(arg0)); printf(" request_file %s\n", copyinstr(arg1)); printf(" lineno %d\n", (int)arg2); printf(" classname %s\n", copyinstr(arg3)); printf(" scope %s\n", copyinstr(arg4)); } php*:::request-shutdown { printf("PHP request-shutdown\n"); printf(" file %s\n", copyinstr(arg0)); printf(" request_uri %s\n", copyinstr(arg1)); printf(" request_method %s\n", copyinstr(arg2)); } php*:::request-startup { printf("PHP request-startup\n"); printf(" file %s\n", copyinstr(arg0)); printf(" request_uri %s\n", copyinstr(arg1)); printf(" request_method %s\n", copyinstr(arg2)); }
此腳本在 dtrace 命令中使用了 -Z
選項。
此選項保證即使在沒有任何 PHP 進程運行的時候腳本也能夠正確執(zhí)行。
如果省略了此選項,當沒有任何探針可監(jiān)控的時候,腳本會立即終止執(zhí)行。
在運行此腳本的過程中,它將監(jiān)控全部 PHP 核心靜態(tài)探針。 運行 D 腳本:
# ./all_probes.d
再運行一個 PHP 腳本或者 PHP 應用,用來進行監(jiān)控的 D 腳本會輸出每個探針被觸發(fā)時所攜帶的參數(shù)。
監(jiān)控完成之后,使用
^C
來終止 D 腳本的執(zhí)行。
在多 CPU 的主機上,探針的顯示順序可能不是連續(xù)的,這取決于哪顆 CPU 執(zhí)行探針以及多個 CPU 之間的線程遷移情況。 可以通過顯示探針時間戳來減少混淆,例如:
php*:::function-entry { printf("%lld: PHP function-entry ", walltimestamp); [ . . .] }