PHPをXDebugとVimでデバッグする (Ubuntu 8.10編とCentOS 5.4/4.5編)   

こんな感じでVim (GVimでもOK)でデバッグできるのでめちゃくちゃ便利。
端末上でできるのもかなりうれしい。

PHP5/Ubuntu 8.10編

XDebugをインストールする

sudo apt-get -y install php5-xdebug

XDebugの設定

sudo vim /etc/php5/apache2/conf.d/xdebug.ini

xdebug.iniに以下を追加

[debug]
; Remote settings
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

; General
xdebug.auto_trace=off
xdebug.collect_includes=on
xdebug.collect_params=off
xdebug.collect_return=off
xdebug.default_enable=on
xdebug.extended_info=1
xdebug.manual_url=http://www.php.net
xdebug.show_local_vars=0
xdebug.show_mem_delta=0
xdebug.max_nesting_level=100
;xdebug.idekey=

; Trace options
xdebug.trace_format=0
xdebug.trace_output_dir=/tmp
xdebug.trace_options=0
xdebug.trace_output_name=crc32

; Profiling
xdebug.profiler_append=0
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=0
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=crc32

アパッチを再起動

sudo /etc/init.d/apache2 restart

PHP5.1.6/CentOS 5.4編

必要なパッケージをインストール

yum -y groupinstall "Development Tools"
yum -y install vim-enhanced php php-devel php-pear

XDebugをインストール

pecl install Xdebug

PHPの設定ファイルを作成

vim /etc/php.d/xdebug.ini

その設定ファイルに以下を追加

zend_extension=/usr/lib/php/modules/xdebug.so

[debug]
; Remote settings
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000
; General
xdebug.auto_trace=off
xdebug.collect_includes=on
xdebug.collect_params=off
xdebug.collect_return=off
xdebug.default_enable=on
xdebug.extended_info=1
xdebug.manual_url=http://www.php.net
xdebug.show_local_vars=0
xdebug.show_mem_delta=0
xdebug.max_nesting_level=100
;xdebug.idekey=
; Trace options
xdebug.trace_format=0
xdebug.trace_output_dir=/tmp
xdebug.trace_options=0
xdebug.trace_output_name=crc32
; Profiling
xdebug.profiler_append=0
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=0
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=crc32

アパッチを再起動

service httpd restart

PHP4.3.8/CentOS 4.5編

Xdebugをインストール

cd /usr/local/src/
wget http://pecl.php.net/get/xdebug-2.0.4.tgz
tar zxvf xdebug-2.0.4.tgz
cd xdebug-2.0.4
phpize
./configure
make
cd modules
cp xdebug.so /usr/lib/php/extensions

php.iniの"Dynamic Extension"の欄にxdebug.soを追加

zend_extension=/usr/lib/php/extensions/xdebug.so

さらに以下のxdebugの細かい設定をphp.iniに追加

[debug]
; Remote settings
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

; General
xdebug.auto_trace=off
xdebug.collect_includes=on
xdebug.collect_params=off
xdebug.collect_return=off
xdebug.default_enable=on
xdebug.extended_info=1
xdebug.manual_url=http://www.php.net
xdebug.show_local_vars=0
xdebug.show_mem_delta=0
xdebug.max_nesting_level=100
;xdebug.idekey=

; Trace options
xdebug.trace_format=0
xdebug.trace_output_dir=/tmp
xdebug.trace_options=0
xdebug.trace_output_name=crc32

; Profiling
xdebug.profiler_append=0
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=0
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=crc32

アパッチを再起動

service httpd restart

さらにUbuntu編 & CentOS編 共通設定

VimプラグインのDBGpを使ってデバッグをするので、これを以下からダウンロード

ダウンロードの例

wget --content-disposition http://www.vim.org/scripts/download_script.php?src_id=7285

Zipファイルを解凍すると中にdebugger.pyとdebugger.vimの2つのファイルある事を確認

unzip debugger.zip
ls plugin

vimプラグイン用のフォルダーを作成しここに2つのファイルをコピーする。

mkdir -p ~/.vim/plugin
mv plugin/debugger* ~/.vim/plugin

これで全ての準備が完了。

デバッグ方法

1. vimまたはgvimを起動する。

vim

2. を押してVimデバッグモードに入る。

3.ブラウザでデバッグしたいURLをロードする。

ただしURLに以下のように"?XDEBUG_SESSION_START=1"を追加してデバッグする事を明記する必要あり。

http://localhost/test.php?XDEBUG_SESSION_START=1

[注] を押してから5秒以内にブラウザでロードする事

[注] Firefoxアドオンの「Xdebug Helper」を使うとURLを変えなくてもデバックモードにクリックするだけでできる。かなりお勧め。

4.デバッグ開始

キー 概要
step into (中へシングルステップ)
step over (シングルステップ)
step out (外にステップアウト)
run (実行)
get all context (全てのデータをダンプ)

ブレークポイントはブレークしたい場所に移動してコマンドモードで「:Bp」と入力すればOK。あとはでrunすればブレークポイントまで飛ぶ。

「,e」と打ってevaluateモードになって変数などを続いて入力すればその値を表示してくれる。gdbのprintみたいなもの。