勉強日記

チラ裏

WSL2 + Emacs + docker環境でPHPのリモートデバッグ

この組み合わせの資料が見当たらず、若干詰まったのでメモ書き


サンプルリポジトリ

  • アプリケーション

github.com

  • docker環境

github.com

リモートデバッグを動かすまで

俯瞰図

f:id:wand_ta:20200303002002p:plain

WSL2のIPアドレス取得

  • Xdebugのクライアントがphp-fpmから見て127.0.0.1じゃないので、調べて設定する必要がある
# on WSL2
ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.30.58.210  netmask 255.255.240.0  broadcast 172.30.63.255
...
  • 必要なところだけ抽出
ifconfig eth0 | grep 'inet ' | awk '{print $2}'
172.30.58.210
  • これはWSL2を起動するたびに毎回変わる

Xdebugの設定

[xdebug]
zend_extension=xdebug.so
xdebug.remote_enable=${XDEBUG_REMOTE_ENABLE}
xdebug.remote_autostart=off
xdebug.remote_handler=dbgp
xdebug.remote_host=${XDEBUG_REMOTE_ADDRESS}
xdebug.remote_port=9000
xdebug.remote_log=/var/log/php/xdebug.log

.env

...
XDEBUG_REMOTE_ENABLE=1
XDEBUG_REMOTE_ADDRESS=172.30.58.210
...

リモートデバッグが動くことを確認

  • http://localhost:10080/?XDEBUG_SESSION_START=1 にアクセス

f:id:wand_ta:20200303003334p:plain

🎉

ストレスなくデバッグを進めるための設定

f:id:wand_ta:20200303003429p:plain

f:id:wand_ta:20200303002320p:plain

  • しかし、デバッグセッションが開始するたびに
    ~/.emacs/geben/<毎回変わる番号>/
    下にphp一時ファイルが生成されるため、この一時ファイルにあらかじめブレークポイントを張っておくことは不可能
  • これを解決するには、geben-path-mappings変数を使う

geben-path-mappingsの設定

f:id:wand_ta:20200303002535p:plain

  • M-x customize-variable RET geben-path-mappings
  • WSL2ローカル上のプロジェクトルートと、dockerコンテナ上のリモートプロジェクトルートを設定する

f:id:wand_ta:20200303002834p:plain

  • ローカルファイル上でgeben-add-current-line-to-predefined-breakpoints (長い) を実行しておくと

f:id:wand_ta:20200303002932p:plain

f:id:wand_ta:20200303003033p:plain