Home > Advent Calendar 2013 | PHP > PHP 5.6 に採用されるデバッガ phpdbg を使ってみた

PHP 5.6 に採用されるデバッガ phpdbg を使ってみた

この記事の所要時間: 952

Shin x blog Advent Calendar 2013 の 21 日目です。

phpdbg_php_debugger

第 12 回関西 PHP 勉強会 にて、PHP 5.6 に採用予定の phpdbg をひと足先に PHP 5.5.7 で触ってみました。

phpdbg

phpdbg は、gdb ライクな PHP 用のデバッガです。ブレークポイントを設定して、その時点のコンテキストを確認したり、ステップ実行などができます。

phpdbg | php debugger

インストール

PHP 5.6 から同梱される予定の phpdbg ですが、これ自体はすでにリリースされており、PHP 5.4 から利用することが可能です。インストールには、PHP のソースコードが必要になるので、PHP も ソースからインストールします。

$ sudo yum -y groupinstall "Development Tools"
$ sudo yum -y install libxml2-devel
$ curl -L -o php-5.5.7.tar.bz2 http://jp1.php.net/get/php-5.5.7.tar.bz2/from/this/mirror
$ tar xvf php-5.5.7.tar.bz2
$ cd php-5.5.7
$ ./configure --with-readline
$ make
$ sudo make install

次に phpdbg をインストールします。ソースコードを git clone して、ビルドします。

$ cd /path/to/php-5.5.7/sapi
$ git clone https://github.com/krakjoe/phpdbg
$ cd ../
$ ./buildconf --force
$ ./config.nice
$ make -j8
$ sudo make install-phpdbg

Vagrantfile を gist をアップしたので、試してみたい方はどうぞ。

インストールすると phpdbg コマンドが実行できるようになります。phpdbg コマンドを実行すると、phpdbg のコンソールが表示されます。quit で phpdbg を終了できます。

phpdbg

help コマンドでコマンドのヘルプが表示されます。help run のように、help の後にコマンドを指定すると、そのコマンドのヘルプが表示されます。

phpdbg> help
[Welcome to phpdbg, the interactive PHP debugger, v0.3.0]
To get help regarding a specific command type "help command"
[Commands]
       exec     set execution context
    compile     attempt compilation
       step     step through execution
       next     continue execution
        run     attempt execution
       eval     evaluate some code
(snip)

phpdbg> help run
[Welcome to phpdbg, the interactive PHP debugger, v0.3.0]
Execute the current context inside the phpdbg vm

[Examples]
        phpdbg> run
        phpdbg> r
        Will cause execution of the context, if it is set.

Note: The execution context must be set, but not necessarily compiled before execution occurs
[Please report bugs to ]

簡単なサンプル

簡単な動作サンプルとして、下記の PHP ソースを phpdbg で実行してみます。

phpdbg コマンドに -e オプションを付けることで、デバッガで実行する PHP スクリプトを指定します。

$ phpdbg -e sample.php

まず、この PHP スクリプトのオペコードを表示してみます。compile コマンドで PHP コードをコンパイルします。

phpdbg> compile
[Attempting compilation of /vagrant/sample.php]
[Success]

print exec コマンドを実行するとオペコードが表示されます。exec オプション以外にも classmethod などを指定することができます。

phpdbg> print exec
[Context /vagrant/sample.php]
    L0-0 {main}() /vagrant/sample.php
        L2      0x7fb5dfd24c68 ZEND_ADD                       C0                   C1                   @0
        L2      0x7fb5dfd24c98 ZEND_ASSIGN                    $a                   @0                   @1
        L3      0x7fb5dfd24cc8 ZEND_ECHO                      $a                                

次にブレイクポイントを設定してみます。break line コマンドを使って、3 行目にブレイクポイントを設定します。ブレイクポイントには、line 以外にも、funcmethod 、また特定のオペコードが呼ばれたタイミングでブレイクする op などを指定することができます。

phpdbg> break line 3
[Breakpoint #0 added at line 3]

コードを実行して、ブレイクポイントで停止するか確認します。コードを実行するには、run コマンドを実行します。ちゃんと設定した 3 行目で実行が停止しました。

phpdbg> run
[Attempting compilation of /vagrant/sample.php]
[Success]
[Breakpoint #0 at /vagrant/sample.php:3, hits: 1]
 00002: $a = 1 + 2;
 >00003: e

停止した時点での変数やリテラルを確認することができます。

phpdbg> info vars
[Variables in /vagrant/sample.php (1)]
Address         Refs    Type            Variable
0x7fc36cac3060  1       (integer)       $a

phpdbg> info literal
[Literal Constants in /vagrant/sample.php (2)]
|-------- C0 -------> [1]
|-------- C1 -------> [2]
|-------- C2 -------> [1]

next コマンドを実行するとコードの実行が再開されます。実行はオペコード単位になっているようなので、next コマンドを 2 回実行するとコードが終了します。

phpdbg> next
[Breakpoint #0 at /vagrant/sample.php:3, hits: 2]
 00002: $a = 1 + 2;
 >00003: echo $a . PHP_EOL;
  00004:
phpdbg> next
3

ジェネレータの動きを見る

次にジェネレータの動きを phpdbg で見てみます。PHP コードは下記です。

phpdbg コマンドを実行します。ブレイクポイントには、yield 文のオペコードである ZEND_YIELD を設定します。phpdbg のコマンドは、省略系で指定することができ、下記では、break op コマンドを省略して、b O としています。

$ phpdbg -e generator.php
(snip)

phpdbg> b O ZEND_YIELD
[Breakpoint #0 added at ZEND_YIELD]

run で実行します。設定したブレイクポイントで処理が停止します。

phpdbg> run
[Attempting compilation of /vagrant/generator.php]
[Success]
[Breakpoint #0 in ZEND_YIELD at /vagrant/generator.php:4, hits: 1]
 00003:     for ($i = $min ; $i <= $max ; $i++) {
>00004:         yield $i;
 00005:     }

next コマンドを実行するとステップ実行を行います。コマンドを入力せずにエンターだけを押すと、前回実行したコマンドがそのまま実行されます。この状態で、エンターを繰り返すと next コマンドが実行されます。

phpdbg> n
1
[Breakpoint #0 in ZEND_YIELD at /vagrant/generator.php:4, hits: 2]
 00003:     for ($i = $min ; $i <= $max ; $i++) {
>00004:         yield $i;
 00005:     }
phpdbg>
2
[Breakpoint #0 in ZEND_YIELD at /vagrant/generator.php:4, hits: 3]
 00003:     for ($i = $min ; $i <= $max ; $i++) {
>00004:         yield $i;
 00005:     }
phpdbg>
3
phpdbg>

さいごに

これまでも gdb や xdebug でデバッグを行うことができましたが、別途インストールや設定が必要でした。

PHP 5.6 から、phpdbg が同梱されることで、PHP が入っている環境では、手軽にこのデバッガを利用できるようになります。リモートデバッグにも対応しており、いずれ PhpStorm のような IDE でも対応されるかもしれません。

phpdbg 自体は 5.4 からインストール可能ですので、ちょっと試してみようという方はインストールしてみてください。

Pocket

follow us in feedly

Home > Advent Calendar 2013 | PHP > PHP 5.6 に採用されるデバッガ phpdbg を使ってみた

検索
フィード
メタ情報

Return to page top