<?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 => 'ru',
  ),
  'this' => 
  array (
    0 => 'features.dtrace.bpftrace.php',
    1 => 'Работа утилиты bpftrace со статическими зондами PHP DTrace',
    2 => 'Работа утилиты bpftrace со статическими зондами PHP DTrace',
  ),
  'up' => 
  array (
    0 => 'features.dtrace.php',
    1 => 'Динамическая трассировка DTrace',
  ),
  'prev' => 
  array (
    0 => 'features.dtrace.systemtap.php',
    1 => 'Работа средства SystemTap со статическими зондами PHP DTrace',
  ),
  'next' => 
  array (
    0 => 'funcref.php',
    1 => 'Справочник функций',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ru',
    '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.bpftrace" class="sect1">
  <h2 class="title">Работа утилиты bpftrace со статическими зондами PHP DTrace</h2>
  <p class="simpara">
   В дистрибутивах Linux с ядром, которое поддерживает eBPF,
   утилита bpftrace умеет подключаться к зондам PHP DTrace USDT напрямую,
   без необходимости использовать SystemTap.
  </p>
  <div class="sect2" id="features.dtrace.bpftrace-install">
   <h3 class="title">Установка bpftrace</h3>
   <p class="para">
    Установите bpftrace через менеджер пакетов дистрибутива. Например,
    в Oracle Linux, RHEL или Fedora:
    <div class="informalexample">
     <div class="example-contents">
<div class="shellcode"><pre class="shellcode"># dnf install bpftrace</pre>
</div>
     </div>

    </div>
    Или в Debian либо Ubuntu:
    <div class="informalexample">
     <div class="example-contents">
<div class="shellcode"><pre class="shellcode"># apt install bpftrace</pre>
</div>
     </div>

    </div>
   </p>
   <p class="simpara">
    Приведённые ниже примеры предполагают, что целевой исполняемый файл PHP
    установлен в <var class="filename">/usr/bin/php</var>.
   </p>
   <p class="simpara">
    Те же зонды USDT предоставляют и другие SAPI, которые собрали из того же дерева исходного кода,
    поэтому целью зондов вместо этого выступает либо модуль Apache
    (<var class="filename">libphp.so</var>), либо исполняемый файл диспетчера процессов FastCGI
    (<var class="filename">php-fpm</var>); подставьте подходящий
    путь или подключитесь по PID-идентификатору через ключ <code class="literal">-p</code> при необходимости.
   </p>
   <p class="simpara">
    Убедитесь, что целевой исполняемый файл собрали с поддержкой DTrace и что окружение настроено правильно.
    Подробности описывает раздел <a href="features.dtrace.dtrace.php#features.dtrace.install" class="link">Конфигурирование PHP со статическими зондами DTrace</a>.
   </p>
   <p class="para">
    Статические зонды PHP умеет выводить команда
    <strong class="command">bpftrace</strong>:
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
# bpftrace -l &#039;usdt:/usr/bin/php:php:*&#039;
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    Команда выводит:
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
usdt:/usr/bin/php:php:compile__file__entry
usdt:/usr/bin/php:php:compile__file__return
usdt:/usr/bin/php:php:error
usdt:/usr/bin/php:php:exception__caught
usdt:/usr/bin/php:php:exception__thrown
usdt:/usr/bin/php:php:execute__entry
usdt:/usr/bin/php:php:execute__return
usdt:/usr/bin/php:php:function__entry
usdt:/usr/bin/php:php:function__return
usdt:/usr/bin/php:php:request__shutdown
usdt:/usr/bin/php:php:request__startup
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    <div class="example" id="example-1">
     <p><strong>Пример #1 Скрипт <var class="filename">all_probes.bt</var> — трассировка всех статических PHP-зондов через bpftrace</strong></p>
     <div class="example-contents">
<div class="shellcode"><pre class="shellcode">#!/usr/bin/env bpftrace

usdt:/usr/bin/php:php:compile__file__entry
{
    printf(&quot;Probe compile__file__entry\n&quot;);
    printf(&quot;  compile_file %s\n&quot;, str(arg0));
    printf(&quot;  compile_file_translated %s\n&quot;, str(arg1));
}
usdt:/usr/bin/php:php:compile__file__return
{
    printf(&quot;Probe compile__file__return\n&quot;);
    printf(&quot;  compile_file %s\n&quot;, str(arg0));
    printf(&quot;  compile_file_translated %s\n&quot;, str(arg1));
}
usdt:/usr/bin/php:php:error
{
    printf(&quot;Probe error\n&quot;);
    printf(&quot;  errormsg %s\n&quot;, str(arg0));
    printf(&quot;  request_file %s\n&quot;, str(arg1));
    printf(&quot;  lineno %d\n&quot;, (int32)arg2);
}
usdt:/usr/bin/php:php:exception__caught
{
    printf(&quot;Probe exception__caught\n&quot;);
    printf(&quot;  classname %s\n&quot;, str(arg0));
}
usdt:/usr/bin/php:php:exception__thrown
{
    printf(&quot;Probe exception__thrown\n&quot;);
    printf(&quot;  classname %s\n&quot;, str(arg0));
}
usdt:/usr/bin/php:php:execute__entry
{
    printf(&quot;Probe execute__entry\n&quot;);
    printf(&quot;  request_file %s\n&quot;, str(arg0));
    printf(&quot;  lineno %d\n&quot;, (int32)arg1);
}
usdt:/usr/bin/php:php:execute__return
{
    printf(&quot;Probe execute__return\n&quot;);
    printf(&quot;  request_file %s\n&quot;, str(arg0));
    printf(&quot;  lineno %d\n&quot;, (int32)arg1);
}
usdt:/usr/bin/php:php:function__entry
{
    printf(&quot;Probe function__entry\n&quot;);
    printf(&quot;  function_name %s\n&quot;, str(arg0));
    printf(&quot;  request_file %s\n&quot;, str(arg1));
    printf(&quot;  lineno %d\n&quot;, (int32)arg2);
    printf(&quot;  classname %s\n&quot;, str(arg3));
    printf(&quot;  scope %s\n&quot;, str(arg4));
}
usdt:/usr/bin/php:php:function__return
{
    printf(&quot;Probe function__return\n&quot;);
    printf(&quot;  function_name %s\n&quot;, str(arg0));
    printf(&quot;  request_file %s\n&quot;, str(arg1));
    printf(&quot;  lineno %d\n&quot;, (int32)arg2);
    printf(&quot;  classname %s\n&quot;, str(arg3));
    printf(&quot;  scope %s\n&quot;, str(arg4));
}
usdt:/usr/bin/php:php:request__shutdown
{
    printf(&quot;Probe request__shutdown\n&quot;);
    printf(&quot;  file %s\n&quot;, str(arg0));
    printf(&quot;  request_uri %s\n&quot;, str(arg1));
    printf(&quot;  request_method %s\n&quot;, str(arg2));
}
usdt:/usr/bin/php:php:request__startup
{
    printf(&quot;Probe request__startup\n&quot;);
    printf(&quot;  file %s\n&quot;, str(arg0));
    printf(&quot;  request_uri %s\n&quot;, str(arg1));
    printf(&quot;  request_method %s\n&quot;, str(arg2));
}</pre>
</div>
     </div>

    </div>
   </p>

   <p class="para">
    Приведённый скрипт трассирует все статические зонды ядра PHP
    на всём протяжении работы PHP-скрипта. Утилите bpftrace
    требуются привилегии суперпользователя:
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
# USE_ZEND_DTRACE=1 bpftrace -c &#039;/usr/bin/php test.php&#039; all_probes.bt
</pre></div>
     </div>

    </div>
   </p>

   <p class="para">
    Чтобы трассировать уже запущенный PHP-процесс (например,
    рабочий процесс <var class="filename">php-fpm</var> или процесс Apache, который загрузил
    <var class="filename">libphp.so</var>), подключитесь по PID-идентификатору:
    <div class="informalexample">
     <div class="example-contents">
<div class="cdata"><pre>
# bpftrace -p $PID all_probes.bt
</pre></div>
     </div>

    </div>
    Путь цели <code class="literal">usdt:</code> в скрипте обязан совпадать с исполняемым файлом
    запущенного процесса; скорректируйте <code class="literal">usdt:/usr/bin/php</code> до
    исполняемого файла <var class="filename">php-fpm</var> или <var class="filename">libphp.so</var> при необходимости.
  </p>
  </div>
 </div><?php manual_footer($setup); ?>