<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.dtrace.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'features.dtrace.dtrace.php',
    1 => 'PHP および DTrace の使用',
    2 => 'PHP および DTrace の使用',
  ),
  'up' => 
  array (
    0 => 'features.dtrace.php',
    1 => 'DTrace 動的トレーシング',
  ),
  'prev' => 
  array (
    0 => 'features.dtrace.introduction.php',
    1 => 'PHP および DTrace への入門',
  ),
  'next' => 
  array (
    0 => 'features.dtrace.systemtap.php',
    1 => 'PHP DTrace 静的プローブとともに SystemTap を使用',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'features/dtrace.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.dtrace.dtrace" class="sect1">
  <h2 class="title">PHP および DTrace の使用</h2>
  <p class="para">
   DTrace 動的トレーシングをサポートするプラットフォームでは、
   DTrace 静的プローブ付きで PHP を構成できます。
  </p>

  <div class="sect2" id="features.dtrace.install">
   <h3 class="title">DTrace 静的プローブのために PHP を構成</h3>

   <p class="para">
    OS の DTrace サポートを有効にするには、
    プラットフォーム固有の外部のドキュメントを参照ください。
    例えば、Oracle Linux では UEK3 を起動して下記を行います。
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"># modprobe fasttrap<br /># chmod 666 /dev/dtrace/helper</span></code></div>
     </div>

    </div>
   </p>
   <p class="para">
    <code class="literal">chmod</code> を使用する代わりに、
    デバイスのアクセスを特定のユーザーに制限するために
    ACL パッケージ・ルールを使用できるでしょう。
   </p>

   <p class="para">
    <code class="literal">--enable-dtrace</code> 構成パラメータとともに PHP をビルドします。
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"># ./configure --enable-dtrace ...<br /># make<br /># make install</span></code></div>
     </div>

    </div>
   </p>
   <p class="para">
    これは、 コア PHP で静的プローブを利用可能にします。
    それら自身のプローブを提供するどの PHP 拡張モジュールも、
    共用拡張モジュールとして別々にビルドするべきです。
   </p>
   <p class="para">
    静的プローブを有効にするには、<strong class="option unknown">USE_ZEND_DTRACE=1</strong>環境変数を対象となるPHPプロセスにセットします。
   </p>
  </div>

  <div class="sect2" id="features.dtrace.static-probes">
  <h3 class="title">コア PHP の DTrace 静的プローブ</h3>
  <table class="doctable table">
   <caption><strong>PHP では、以下の静的プローブが使用できます</strong></caption>
   
    <thead>
     <tr>
      <th>プローブ名</th>
      <th>プローブの説明</th>
      <th>プローブの引数</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td><code class="literal">request-startup</code></td>
      <td>リクエスト開始時に発射されます。</td>
      <td>char *<var class="varname">file</var>, char *<var class="varname">request_uri</var>, char *<var class="varname">request_method</var></td>
     </tr>

     <tr>
      <td><code class="literal">request-shutdown</code></td>
      <td>リクエスト・シャットダウン時に発射されます。</td>
      <td>char *<var class="varname">file</var>, char *<var class="varname">request_uri</var>, char *<var class="varname">request_method</var></td>
     </tr>

     <tr>
      <td><code class="literal">compile-file-entry</code></td>
      <td>スクリプトのコンパイル開始時に発射されます。</td>
      <td>char *<var class="varname">compile_file</var>, char *<var class="varname">compile_file_translated</var></td>
     </tr>

     <tr>
      <td><code class="literal">compile-file-return</code></td>
      <td>スクリプトのコンパイル終了時に発射されます。</td>
      <td>char *<var class="varname">compile_file</var>, char *<var class="varname">compile_file_translated</var></td>
     </tr>

     <tr>
      <td><code class="literal">execute-entry</code></td>
      <td>op コードの配列が実行される際に発射されます。
      例えば、関数の呼び出し、インクルードおよびジェネレータ続行時に発射されます。</td>
      <td>char *<var class="varname">request_file</var>, int <var class="varname">lineno</var></td>
     </tr>

     <tr>
      <td><code class="literal">execute-return</code></td>
      <td>op コードの配列の実行後に発射されます。</td>
      <td>char *<var class="varname">request_file</var>, int <var class="varname">lineno</var></td>
     </tr>

     <tr>
      <td><code class="literal">function-entry</code></td>
      <td>PHP エンジンが、PHP の関数またはメソッドの呼び出しに入り込む際に発射されます。</td>
      <td>char *<var class="varname">function_name</var>, char *<var class="varname">request_file</var>, int <var class="varname">lineno</var>, char *<var class="varname">classname</var>, char *<var class="varname">scope</var></td>
     </tr>

     <tr>
      <td><code class="literal">function-return</code></td>
      <td>PHP エンジンが、PHP の関数またはメソッドの呼び出しから戻る際に発射されます。</td>
      <td>char *<var class="varname">function_name</var>, char *<var class="varname">request_file</var>, int <var class="varname">lineno</var>, char *<var class="varname">classname</var>, char *<var class="varname">scope</var></td>
     </tr>

     <tr>
      <td><code class="literal">exception-thrown</code></td>
      <td>例外がスローされる際に発射されます。</td>
      <td>char *<var class="varname">classname</var></td>
     </tr>

     <tr>
      <td><code class="literal">exception-caught</code></td>
      <td>例外が捕捉される際に発射されます。</td>
      <td>char *<var class="varname">classname</var></td>
     </tr>

     <tr>
      <td><code class="literal">error</code></td>
      <td><a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> レベルにかかわらず、
      エラー発生時に発射されます。</td>
      <td>char *<var class="varname">errormsg</var>, char *<var class="varname">request_file</var>, int <var class="varname">lineno</var></td>
     </tr>

    </tbody>
   
  </table>

  <p class="para">
   PHP 拡張モジュールには、追加の静的プローブもあるかもしれません。
  </p>
  </div>

  <div class="sect2" id="features.dtrace.list-probes">
   <h3 class="title">PHP の DTrace 静的プローブ一覧</h3>
   <p class="para">
    使用できるプローブを一覧表示するには、
    PHP プロセスを開始して以下のように実行します。
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
# dtrace -l
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    出力は以下のようになります。
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
   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
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    PROVIDER 列の値は、<code class="literal">php</code> および
    現在実行中の PHP プロセスのプロセス ID から構成されます。
   </p>

   <p class="para">
    Apache Web サーバーが実行中の場合、モジュール名は、例えば
    <var class="filename">libphp5.so</var> かもしれません。
    そして、実行中の Apache プロセスごとに 1 つ、
    一覧表の複数のブロックが存在するかもしれません。
   </p>

   <p class="para">
    FUNCTION 列は、各プロバイダが配置された PHP 内部の C 実装関数名を参照します。
   </p>

   <p class="para">
    PHP プロセスが実行されていない場合、PHP プローブは表示されません。
   </p>
  </div>

  <div class="sect2" id="features.dtrace.examples">
   <h3 class="title">PHP についての DTrace の例</h3>
   <p class="para">
    この例では、DTrace の D スクリプト言語の基本を示します。
    <div class="example" id="example-1">
     <p><strong>例1 
      DTrace を使用してすべての PHP 静的プローブをトレースするための<var class="filename">all_probes.d</var>
     </strong></p>
     <div class="example-contents">
<div class="cdata"><pre>
#!/usr/sbin/dtrace -Zs

#pragma D option quiet

php*:::compile-file-entry
{
    printf(&quot;PHP compile-file-entry\n&quot;);
    printf(&quot;  compile_file              %s\n&quot;, copyinstr(arg0));
    printf(&quot;  compile_file_translated   %s\n&quot;, copyinstr(arg1));
}

php*:::compile-file-return
{
    printf(&quot;PHP compile-file-return\n&quot;);
    printf(&quot;  compile_file              %s\n&quot;, copyinstr(arg0));
    printf(&quot;  compile_file_translated   %s\n&quot;, copyinstr(arg1));
}

php*:::error
{
    printf(&quot;PHP error\n&quot;);
    printf(&quot;  errormsg                  %s\n&quot;, copyinstr(arg0));
    printf(&quot;  request_file              %s\n&quot;, copyinstr(arg1));
    printf(&quot;  lineno                    %d\n&quot;, (int)arg2);
}

php*:::exception-caught
{
    printf(&quot;PHP exception-caught\n&quot;);
    printf(&quot;  classname                 %s\n&quot;, copyinstr(arg0));
}

php*:::exception-thrown
{
    printf(&quot;PHP exception-thrown\n&quot;);
    printf(&quot;  classname                 %s\n&quot;, copyinstr(arg0));
}

php*:::execute-entry
{
    printf(&quot;PHP execute-entry\n&quot;);
    printf(&quot;  request_file              %s\n&quot;, copyinstr(arg0));
    printf(&quot;  lineno                    %d\n&quot;, (int)arg1);
}

php*:::execute-return
{
    printf(&quot;PHP execute-return\n&quot;);
    printf(&quot;  request_file              %s\n&quot;, copyinstr(arg0));
    printf(&quot;  lineno                    %d\n&quot;, (int)arg1);
}

php*:::function-entry
{
    printf(&quot;PHP function-entry\n&quot;);
    printf(&quot;  function_name             %s\n&quot;, copyinstr(arg0));
    printf(&quot;  request_file              %s\n&quot;, copyinstr(arg1));
    printf(&quot;  lineno                    %d\n&quot;, (int)arg2);
    printf(&quot;  classname                 %s\n&quot;, copyinstr(arg3));
    printf(&quot;  scope                     %s\n&quot;, copyinstr(arg4));
}

php*:::function-return
{
    printf(&quot;PHP function-return\n&quot;);
    printf(&quot;  function_name             %s\n&quot;, copyinstr(arg0));
    printf(&quot;  request_file              %s\n&quot;, copyinstr(arg1));
    printf(&quot;  lineno                    %d\n&quot;, (int)arg2);
    printf(&quot;  classname                 %s\n&quot;, copyinstr(arg3));
    printf(&quot;  scope                     %s\n&quot;, copyinstr(arg4));
}

php*:::request-shutdown
{
    printf(&quot;PHP request-shutdown\n&quot;);
    printf(&quot;  file                      %s\n&quot;, copyinstr(arg0));
    printf(&quot;  request_uri               %s\n&quot;, copyinstr(arg1));
    printf(&quot;  request_method            %s\n&quot;, copyinstr(arg2));
}

php*:::request-startup
{
    printf(&quot;PHP request-startup\n&quot;);
    printf(&quot;  file                      %s\n&quot;, copyinstr(arg0));
    printf(&quot;  request_uri               %s\n&quot;, copyinstr(arg1));
    printf(&quot;  request_method            %s\n&quot;, copyinstr(arg2));
}
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    このスクリプトは、<var class="filename">dtrace</var> に対して
    <code class="literal">-Z</code> オプションを使用します。
    それにより、実行中の PHP プロセスがない場合に実行できるようになります。
    もしこのオプションが書き落とされた場合、
    モニターされているプローブがどれも存在しないことがわかっているので、
    スクリプトは即座に終了します。
   </p>

   <p class="para">
    スクリプトは、PHP スクリプト実行中の期間全体にわたって、
    すべてのコア PHP 静的プローブ・ポイントをトレースします。
    D スクリプトを実行します。
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
# ./all_probes.d
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    PHP スクリプトまたはアプリケーションを実行します。
    モニタリングの D スクリプトは、各プローブが発射されるたびに
    その引数を出力します。
   </p>

   <p class="para">
    モニタリングが完了した際には、<kbd class="keycombo"><kbd class="keycap">CTRL</kbd>+<kbd class="keycap">C</kbd></kbd> を使用して
    D スクリプトを終わらせることができます。
   </p>

   <p class="para">
    マルチ CPU マシンでは、プローブの順序は規則的に連続して表示されないことがあります。
    これは、どの CPU がプローブを処理し、
    CPU の間をスレッドがどのように移動したかに依存します。
    プローブのタイムスタンプを表示すると、
    混乱を減らすことができます。
    その例です。
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
php*:::function-entry
{
      printf(&quot;%lld: PHP function-entry &quot;, walltimestamp);
      [ . . .]
}
</pre></div>
     </div>

    </div>
   </p>
  </div>

  <div class="sect2" id="features.dtrace.references">
   <h3 class="title">参考</h3>
   <ul class="simplelist">
    <li><a href="oci8.dtrace.php" class="link">OCI8 および DTrace 動的トレーシング</a></li>
   </ul>
  </div>
 </div><?php manual_footer($setup); ?>