Xdebug関連メソッドの詳細解説

設定パラメータ

xdebug.remote_enable = on
xdebug.profiler_enable = on
xdebug.profiler_enable_trigger = on
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir = "/var/tmp/xdebug"

関連関数

xdebug_var_dump

変数の詳細情報を表示します。配列は再帰的に探索され、参照カウント情報を含みます。

<?php
$x = ['name' => 'Alice', 'age' => 30];
$y = &$x['age'];
xdebug_var_dump($x, 'y');
?>
array(2) {
  'name' => string(5) "Alice"
  'age' => (refcount=2, is_ref=1)=int(30)
}
y: (refcount=2, is_ref=1)=int(30)

xdebug_break

デバッグクライアントにブレークポイントを送信します。

<?php
if ($debug_mode) {
    xdebug_break();
}
?>

xdebug_call_file

現在の関数を呼び出したファイル名を返します。

<?php
function process_data() {
    echo "Called from " . xdebug_call_file() . "\n";
}
process_data();
?>
Called from /var/www/script.php

xdebug_get_code_coverage

コードカバレッジ情報を取得します。

<?php
xdebug_start_code_coverage();
function calc($n) {
    return $n * 2;
}
calc(10);
var_dump(xdebug_get_code_coverage());
?>
array(1) {
  "/var/www/script.php" => array(2) {
    4 => int(1)
    5 => int(1)
  }
}

xdebug_start_function_monitor

指定関数の呼び出し位置を監視します。

<?php
xdebug_start_function_monitor(['array_map', 'json_encode']);
$processed = array_map('strtoupper', ['a', 'b', 'c']);
echo json_encode($processed);
xdebug_stop_function_monitor();
?>
array(2) {
  [0] => array(3) {
    "function" => string(8) "array_map"
    "filename" => string(22) "/var/www/monitor.php"
    "lineno" => int(5)
  }
  [1] => array(3) {
    "function" => string(10) "json_encode"
    "filename" => string(22) "/var/www/monitor.php"
    "lineno" => int(6)
  }
}

xdebug_start_trace

関数呼び出しトレースを開始します。

<?php
xdebug_start_trace('/var/log/trace', XDEBUG_TRACE_HTML);
process_data();
xdebug_stop_trace();
?>

トレースファイル: /var/log/trace.xt (HTML形式)

xdebug_get_headers

PHPのheader()関数で設定されたヘッダーを取得します。

<?php
header('X-Custom-Header', 'value');
setcookie('test', 'data');
var_dump(xdebug_get_headers());
?>
array(2) {
  [0] => string(17) "X-Custom-Header"
  [1] => string(23) "Set-Cookie: test=data"
}

xdebug_memory_usage

現在のメモリ使用量を返します。

<?php
echo "Memory: " . xdebug_memory_usage() . " bytes";
?>

タグ: xdebug PHP code-coverage debugging profiler

6月3日 19:23 投稿