How to get opcodes of PHP?

你说的曾经没有我的故事 提交于 2019-11-27 17:14:21
Paul Dixon

Check out the Vulcan Logic Disassembler PECL extension - see author's home page for more info.

The Vulcan Logic Disassembler hooks into the Zend Engine and dumps all the opcodes (execution units) of a script. It was written as as a beginning of an encoder, but I never got the time for that. It can be used to see what is going on in the Zend Engine.

Once installed, you can use it like this:

php -d vld.active=1 -d vld.execute=0 -f yourscript.php

See also this interesting blog post on opcode extraction, and the PHP manual page listing the available opcodes.

Parsekit has parsekit_compile_string().

sudo pecl install parsekit
var_dump(parsekit_compile_string(<<<PHP
 \$show_value   = 123;
 echo 'sing_quote'.\$show_value;
 echo "double_quote{\$show_value}";
PHP
));

The output is quite verbose, so you'd need to process it to get assembler-like format.

  ["opcodes"]=>
  array(10) {
    [0]=>
    array(9) {
      ["address"]=>
      int(44682716)
      ["opcode"]=>
      int(101)
      ["opcode_name"]=>
      string(13) "ZEND_EXT_STMT"
      ["flags"]=>
      int(4294967295)
      ["result"]=>
      array(8) {
        ["type"]=>
        int(8)
        ["type_name"]=>
        string(9) "IS_UNUSED"
        ["var"]=>
        int(0)
        ["opline_num"]=>
        string(1) "0"
        ["op_array"]=>
        string(1) "0"
        ["jmp_addr"]=>
        string(1) "0"
        ["jmp_offset"]=>
        string(8) "35419039"
        ["EA.type"]=>
        int(0)
      }
      ["op1"]=>
      array(8) {
        ["type"]=>
        int(8)
        ["type_name"]=>
        string(9) "IS_UNUSED"
        ["var"]=>
        int(0)
        ["opline_num"]=>
        string(1) "0"
        ["op_array"]=>
        string(1) "0"
        ["jmp_addr"]=>
        string(1) "0"
        ["jmp_offset"]=>
        string(8) "35419039"
        ["EA.type"]=>
        int(0)
      }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!