使锟斤拷 PHP 锟斤拷 DTrace

锟斤拷支锟斤拷 DTrace 锟斤拷态锟斤拷锟劫碉拷平台锟较o拷锟斤拷锟斤拷锟斤拷锟斤拷 PHP 锟斤拷 DTrace 锟斤拷态探锟诫。

PHP DTrace 锟斤拷态探锟斤拷锟斤拷锟斤拷

锟斤拷慰锟斤拷锟斤拷锟较低筹拷牡锟斤拷锟饺★拷锟斤拷貌锟斤拷锟较低� DTrace 支锟街碉拷锟斤拷息锟斤拷 锟斤拷锟界,锟斤拷 Oracle Linux 系统锟斤拷锟斤拷锟斤拷 UEK3 锟节核o拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰诧拷锟斤拷锟斤拷

# modprobe fasttrap
# chmod 666 /dev/dtrace/helper

锟斤拷锟斤拷 chmod 锟斤拷锟筋,锟斤拷也锟斤拷锟斤拷使锟斤拷 ACL 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截讹拷锟矫伙拷锟斤拷锟斤拷锟借备锟侥凤拷锟斤拷权锟睫★拷

使锟斤拷 --enable-dtrace 锟斤拷锟矫诧拷锟斤拷锟斤拷锟斤拷 PHP锟斤拷

# ./configure --enable-dtrace ...
# make
# make install

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 PHP 锟斤拷锟侥的撅拷态探锟诫。锟斤拷锟斤拷锟结供锟斤拷锟斤拷锟斤拷探锟斤拷锟� PHP 锟斤拷展锟斤拷要锟街别构斤拷锟斤拷

PHP 锟斤拷锟侥碉拷 DTrace 锟斤拷态探锟斤拷

PHP 锟叫帮拷锟斤拷锟斤拷锟铰匡拷锟矫的撅拷态探锟斤拷
探锟斤拷锟斤拷锟斤拷 探锟斤拷锟斤拷锟斤拷 探锟斤拷锟斤拷锟�
request-startup 锟斤拷锟斤拷始时锟斤拷锟斤拷锟斤拷 char *file, char *request_uri, char *request_method
request-shutdown 锟斤拷锟斤拷乇锟绞憋拷锟斤拷锟斤拷锟� char *file, char *request_uri, char *request_method
compile-file-entry 锟脚憋拷锟斤拷始锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷 char *compile_file, char *compile_file_translated
compile-file-return 锟脚憋拷锟斤拷杀锟斤拷锟绞憋拷锟斤拷锟斤拷锟� char *compile_file, char *compile_file_translated
execute-entry 锟斤拷锟斤拷锟斤拷锟斤拷锟介开始执锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟界:锟斤拷锟斤拷锟斤拷锟矫o拷锟侥硷拷锟斤拷锟斤拷锟皆硷拷锟斤拷锟斤拷锟斤拷锟街革拷时锟结被锟斤拷锟斤拷锟斤拷 char *request_file, int lineno
execute-return 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷执锟斤拷锟斤拷锟街拷蟠シ锟斤拷锟� char *request_file, int lineno
function-entry PHP 锟斤拷锟斤拷锟斤拷锟� PHP 锟斤拷锟斤拷锟斤拷锟竭凤拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷 char *function_name, char *request_file, int lineno, char *classname, char *scope
function-return PHP 锟斤拷锟斤拷锟� PHP 锟斤拷锟斤拷锟斤拷锟竭凤拷锟斤拷锟斤拷锟矫凤拷锟截后触凤拷锟斤拷. char *function_name, char *request_file, int lineno, char *classname, char *scope
exception-thrown 锟斤拷锟届常锟阶筹拷时锟斤拷锟斤拷锟斤拷 char *classname
exception-caught 锟斤拷锟届常锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷 char *classname
error 锟斤拷锟斤拷 error_reporting 锟斤拷锟借定锟斤拷危锟斤拷诜锟斤拷锟斤拷锟斤拷锟绞憋拷锟斤拷岽ワ拷锟斤拷锟� char *errormsg, char *request_file, int lineno

PHP 锟斤拷展锟斤拷锟斤拷拥锟叫讹拷锟斤拷木锟教斤拷搿�

锟叫筹拷 PHP 锟叫匡拷锟矫的撅拷态探锟斤拷

要锟叫筹拷 PHP 锟叫匡拷锟矫的撅拷态探锟诫,锟斤拷锟斤拷一锟斤拷 PHP 锟斤拷锟教o拷然锟斤拷锟斤拷锟叫o拷

# 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锟斤拷 锟斤拷锟揭匡拷锟杰伙拷锟斤拷侄锟斤拷锟斤拷息锟斤拷每锟斤拷锟斤拷锟斤拷锟叫碉拷 Apache 锟斤拷锟教讹拷应一锟斤拷锟斤拷锟斤拷椤�

Function Name 一锟叫憋拷示 Provider 锟斤拷应锟斤拷 PHP 锟节诧拷 C 实锟街猴拷锟斤拷锟斤拷锟狡★拷

锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷魏锟� PHP 锟斤拷锟教o拷锟斤拷么锟酵诧拷锟斤拷锟斤拷示锟轿猴拷 PHP 探锟诫。

PHP DTrace 示锟斤拷

锟斤拷锟斤拷展示锟剿伙拷锟斤拷锟斤拷 DTrace D 锟脚憋拷锟斤拷

Example #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 锟斤拷锟斤拷锟斤拷锟叫碉拷时锟斤拷疟锟揭诧拷芄锟斤拷锟饺分达拷小锟� 锟斤拷锟绞★拷锟斤拷舜锟窖★拷睿拷锟矫伙拷锟斤拷魏锟教斤拷锟缴硷拷氐锟绞憋拷颍疟锟斤拷锟斤拷锟斤拷锟斤拷锟街怪达拷小锟�

锟斤拷锟斤拷锟叫此脚憋拷锟侥癸拷锟斤拷锟叫o拷锟斤拷锟斤拷锟斤拷锟饺拷锟� PHP 锟斤拷锟侥撅拷态探锟诫。 锟斤拷锟斤拷 D 锟脚憋拷锟斤拷

# ./all_probes.d

锟斤拷锟斤拷锟斤拷一锟斤拷 PHP 锟脚憋拷锟斤拷锟斤拷 PHP 应锟矫o拷锟斤拷锟斤拷锟斤拷锟叫硷拷氐锟� D 锟脚憋拷锟斤拷锟斤拷锟矫匡拷锟教斤拷氡伙拷锟斤拷锟绞憋拷锟叫拷锟斤拷牟锟斤拷锟斤拷锟�

锟斤拷锟斤拷锟斤拷之锟斤拷使锟斤拷 ^C 锟斤拷锟斤拷止 D 锟脚憋拷锟斤拷执锟叫★拷

锟节讹拷 CPU 锟斤拷锟斤拷锟斤拷锟较o拷探锟斤拷锟斤拷锟绞舅筹拷锟斤拷锟杰诧拷锟斤拷锟斤拷锟斤拷锟侥o拷锟斤拷取锟斤拷锟斤拷锟侥匡拷 CPU 执锟斤拷探锟斤拷锟皆硷拷锟斤拷锟� CPU 之锟斤拷锟斤拷叱锟角拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷通锟斤拷锟斤拷示探锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷倩锟斤拷锟斤拷锟斤拷锟斤拷纾�

php*:::function-entry
{
      printf("%lld: PHP function-entry ", walltimestamp);
      [ . . .]
}