Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

Xdebug2.1.0beta1が出てた

via. PECL :: Package :: xdebug

今日で年始休みは終りです。休みは何でこんなに早いんでしょうねぇ:-(
で、正月にたまったFeedを消化していると、Xdebug2.1.0beta1がリリースされた模様。PHP5.3にも対応したようです。とりあえず、新たに追加された機能を試してみました。

xdebug_get_headers関数の追加

header関数やsetcookie関数で出力されたHTTPヘッダ情報を配列で取得できる関数です。サンプルコードはこんな感じ。

<?php
header("Content-Type: text/plain");
header("X-Test: Testing");
setcookie("TestCookie", "test-value");
var_dump(xdebug_get_headers());

これを実行すると、次のようになります。

$ php xdebug_get_headers.php
array(3) {
  [0]=>
  string(24) "Content-Type: text/plain"
  [1]=>
  string(15) "X-Test: Testing"
  [2]=>
  string(33) "Set-Cookie: TestCookie=test-value"
}
$

なんてことないような感じですが、出力のテストに使えそう。

xdebug.collect_assignmentsディレクティブ

xdebug.collect_assignmentsディレクティブを有効にすると、関数やメソッド内部の変数の値もtraceできるようになります。たとえば、次のようなスクリプト

<?php
function calc($a, $b) {
    $val1 = $a;
    $val1 *= 2;
    $val2 = $b;
    $result =  $val1 + $val2;
    return $result;
}
$a = 4;
$b = 7;
$c = calc($a, $b);

xdebug.collect_assignmentsディレクティブ付きで実行すると、以下のようになります。

$ php -d xdebug.auto_trace=1 -d xdebug.collect_params=4 -d xdebug.collect_return=1 -d xdebug.collect_assignments=1 calc.php
$ cat /tmp/trace.1209371132.xt
TRACE START [2010-01-04 00:07:37]
    0.0006      48344   -> {main}() /home/shimooka/public_html/pecl/xdebug/calc.php:0
                         => $a = 4 /home/shimooka/public_html/pecl/xdebug/calc.php:9
                         => $b = 7 /home/shimooka/public_html/pecl/xdebug/calc.php:10
    0.0006      48576     -> calc($a = 4, $b = 7) /home/shimooka/public_html/pecl/xdebug/calc.php:11
                           => $val1 = 4 /home/shimooka/public_html/pecl/xdebug/calc.php:3
                           => $val1 *= 2 /home/shimooka/public_html/pecl/xdebug/calc.php:4
                           => $val2 = 7 /home/shimooka/public_html/pecl/xdebug/calc.php:5
                           => $result = 15 /home/shimooka/public_html/pecl/xdebug/calc.php:6
                           >=> 15
                         => $c = 15 /home/shimooka/public_html/pecl/xdebug/calc.php:11
                         >=> 1
    0.0006       8268
TRACE END   [2010-01-04 00:07:37]

$ 

メソッドの場合も同様です。

$ cat class.php
<?php
class Foo
{
    private $name;
    public function __construct($name, $value = 1) {
        $this->name = $name;
        $values = array();
        $values[] = $value;
    }
    public function execute() {
        $str = "name is {$this->name}";
        print $str;
    }
}

$obj = new Foo('bar');
$obj->execute();
$ php -d xdebug.auto_trace=1 -d xdebug.collect_params=4 -d xdebug.collect_assignments=1 class.php
name is bar[shimooka@centocat /tmp/trace.1209371132.xt
TRACE START [2010-01-04 00:15:27]
    0.0011      50788   -> {main}() /home/shimooka/public_html/pecl/xdebug/class.php:0
    0.0015      51104     -> Foo->__construct($name = 'bar', $value = ???) /home/shimooka/public_html/pecl/xdebug/class.php:16
                           => $this->name = 'bar' /home/shimooka/public_html/pecl/xdebug/class.php:6
                           => $values = array () /home/shimooka/public_html/pecl/xdebug/class.php:7
                           => $values[] = 1 /home/shimooka/public_html/pecl/xdebug/class.php:8
                         => $obj = class Foo { private $name = 'bar' } /home/shimooka/public_html/pecl/xdebug/class.php:16
    0.0020      51152     -> Foo->execute() /home/shimooka/public_html/pecl/xdebug/class.php:17
                           => $str = 'name is bar' /home/shimooka/public_html/pecl/xdebug/class.php:11
    0.0034       8268
TRACE END   [2010-01-04 00:15:27]

$ 

xdebug.screamディレクティブ

以前、scream拡張モジュールについてエントリしましたが、機能的にXdebugに組み込まれたようです。要は、エラー制御演算子("@"演算子)を無効にして、エラー時にメッセージを出力するようになります。

<?php
$handle = @fopen('not_exist_file.txt', 'r');

通常は、これを実行すると、

$ php xdebug_scream.php
$ 

のようにエラーが出力されませんが、xdebug.screamを有効にすると、

$ php -d xdebug.scream=1 xdebug_scream.php
PHP Warning:  fopen(not_exist_file.txt): failed to open stream: No such file or directory in /path/to/xdebug_scream.php on line 5
PHP Stack trace:
PHP   1. {main}() /path/to/xdebug_scream.php:0
PHP   2. fopen() /path/to/xdebug_scream.php:5

Warning: fopen(not_exist_file.txt): failed to open stream: No such file or directory in /path/to/xdebug_scream.php on line 5

Call Stack:
    0.0001      46456   1. {main}() /path/to/xdebug_scream.php:0
    0.0001      46572   2. fopen() /path/to/xdebug_scream.php:5

$

となります。古〜いコードをメンテナンスする際には有効かも知れません。