LoginSignup
6

More than 3 years have passed since last update.

posted at

updated at

Organization

opcodeダンプするのにvldもphpdbgも要らなくなってた

PHP 7.1からopcodeダンプがopcacheで出来るようになってたみたいですね。

test.php
<?php
$a = 1;
$b = $a * 2;

var_dump($b * $b);

上記プログラムのopcodeダンプは次のようにすれば取得できます。

$ php -dopcache.enable_cli=1 -dopcache.opt_debug_level=0x10000 /tmp/test.php

$_main: ; (lines=8, args=0, vars=2, tmps=5)
    ; (before optimizer)
    ; /private/tmp/test.php:1-6
L0:     ASSIGN CV0($a) int(1)
L1:     T3 = MUL CV0($a) int(2)
L2:     ASSIGN CV1($b) T3
L3:     INIT_FCALL 1 96 string("var_dump")
L4:     T5 = MUL CV1($b) CV1($b)
L5:     SEND_VAL T5 1
L6:     DO_ICALL
L7:     RETURN int(1)
int(4)

コマンドラインオプションの0x10000は最適化前を表すフラグなんだそうです。各最適化フェーズの出力例は「PHP 7.1からOPcacheに増えた設定値「opcache.opt_debug_level」とは何か - Qiita」を参照のこと。

下記は同じopcodeダンプをphpdbgで取得したものです。

› phpdbg '-p*' /tmp/test.php
function name: (null)
L1-6 {main}() /private/tmp/test.php - 0x110ab21b0 + 8 ops
 L2    #0     ASSIGN                  $a                   1
 L3    #1     MUL                     $a                   2                    ~0
 L3    #2     ASSIGN                  $b                   ~0
 L5    #3     INIT_FCALL              96                   "var_dump"
 L5    #4     MUL                     $b                   $b                   ~0
 L5    #5     SEND_VAL                ~0                   1
 L5    #6     DO_ICALL
 L6    #7     RETURN                  1
[Script ended normally]

両者を見比べてみると1:1で対応していますね。opcacheの出力の方が断然可読性が高いので逆にopcodeダンプに見えないんですが、中の人(nikicさん)はこちらを推奨しているようなので、今後はこれに慣れていった方がいいのかもしれません。

参考URL

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
6