suchen
Heimphp教程PHP开发PHP-Kernel-Analyse (6)-Opcode
PHP-Kernel-Analyse (6)-OpcodeDec 19, 2016 am 11:24 AM

摘要:这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux查看opcodephp是先把源码解析成opcode,然后再把opcode传递给zend_vm进行执行的。// 一个opcode的结构 struct _zend_op { const void *handler; // opcode ...

这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux

查看opcode

php是先把源码解析成opcode,然后再把opcode传递给zend_vm进行执行的。

01    // 一个opcode的结构    
02    struct _zend_op {    
03         const void *handler; // opcode对应的执行函数,每个opcode都有一个对应的执行函数    
04         znode_op op1;  // 执行参数的第一个元素    
05         znode_op op2;  //  执行参数的第二个元素    
06         znode_op result; // 执行结果    
07         uint32_t extended_value; // 额外扩展的字段和值    
08         uint32_t lineno; // 行数    
09         zend_uchar opcode;   // 操作码,具体操作码列表见 http://cn.php.net/manual/zh/internals2.opcodes.php    
10         zend_uchar op1_type; // 第一个元素的类型    
11         zend_uchar op2_type; // 第二个元素的类型    
12         zend_uchar result_type; // 结果的类型    
13    };

在php7中,我们能很方便用phpdbg来查看一个文件或者一个函数的opcode了。至于phpdbg的使用,现在网上介绍不多,不过好在有很详细的help文档。下面是一个最简单的opcode代码:

01    $ bin/phpdbg -f /home/xiaoju/software/php7/demo/echo.php    
02    prompt> list 100    
03    00001: <!--?php    
04    00002:    
05    00003: $a = 1;    
06    00004: $b = $a;    
07    00005: $b = $b + 1;    
08    00006: echo $b;    
09    00007:    
10    prompt--> print exec    
11    [Context /home/xiaoju/software/php7/demo/echo.php (6 ops)]    
12    L1-7 {main}() /home/xiaoju/software/php7/demo/echo.php - 0x7fe3fae63300 + 6 ops    
13    L3    #0     ASSIGN                  $a                   1    
14    L4    #1     ASSIGN                  $b                   $a    
15    L5    #2     ADD                     $b                   1                    ~2    
16    L5    #3     ASSIGN                  $b                   ~2    
17    L6    #4     ECHO                    $b    
18    L7    #5     RETURN                  1

这个php文件就做了一个最简单的加法操作。生成了6个_zend_op。所展示的每一行代表一个_zend_op

1    _zendop.lineno  op号   _zend_op.opcode       _zend_op.op1          _zend_op.op2          _zend_op.result    
2    L5              #2     ADD                     $b                   1                    ~2

这里_zend_op.opcode对应的操作在官网有文档和详细的例子可以查看:http://cn.php.net/manual/zh/internals2.opcodes.php

值得一说的是,phpdbg还有一个远端UI版本,能让我们在近端诊断服务端的php信息

gdb

但是我们的目标还是在于研究php源码,phpdbg只能分析到opcode这层,还是不够的,gdb可能是更好的选择。

gdb的使用和平时使用差不多

比如我现在有个脚本echo.php:

 1 <?php
  2
  3 $a = 1;  
 4 $b = $a;  
 5 $b = $b + 1;  
 6 echo $b;

我的php安装路径在:

/home/xiaoju/software/php7/bin/php

php源码路径在:

/home/xiaoju/webroot/php-src/php-src-master/

运行gdb

$ gdb /home/xiaoju/software/php7/bin/php

加载gdbinit:

(gdb) source /home/xiaoju/webroot/php-src/php-src-master/.gdbinit

设置断点:

(gdb) b zend_execute_scripts

运行:

(gdb) run -f /home/xiaoju/software/php7/demo/echo.php

我想在1459这行设置个断点:

01    1452          for (i = 0; i < file_count; i++) {    
02    1453               file_handle = va_arg(files, zend_file_handle *);    
03    1454               if (!file_handle) {    
04    1455                    continue;    
05    1456               }    
06    1457    
07    1458               op_array = zend_compile_file(file_handle, type);    
08    1459               if (file_handle->opened_path) {    
09    1460                    zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);    
10    1461               }    
11    
12    (gdb) b 1459

继续跑

1    (gdb) continue    
2    (gdb) s    
3    (gdb) s

打印出这个时候的op_array

1    (gdb) p *op_array    
2    $4 = {type = 2 &#39;\002&#39;, arg_flags = "\000\000", fn_flags = 134217728, function_name = 0x0, scope = 0x0,    
3      prototype = 0x0, num_args = 0, required_num_args = 0, arg_info = 0x0, refcount = 0x7ffff6002000, last = 6,    
4      opcodes = 0x7ffff6076240, last_var = 2, T = 4, vars = 0x7ffff6079030, last_live_range = 0, last_try_catch = 0,    
5      live_range = 0x0, try_catch_array = 0x0, static_variables = 0x0, filename = 0x7ffff605c2d0, line_start = 1,    
6      line_end = 7, doc_comment = 0x0, early_binding = 4294967295, last_literal = 3, literals = 0x7ffff60030c0,    
7      cache_size = 0, run_time_cache = 0x0, reserved = {0x0, 0x0, 0x0, 0x0}}

我可以优化输出:

01    (gdb) set print pretty on    
02    (gdb) p *op_array    
03    $5 = {    
04      type = 2 &#39;\002&#39;,    
05      arg_flags = "\000\000",    
06      fn_flags = 134217728,    
07      function_name = 0x0,    
08      scope = 0x0,    
09      prototype = 0x0,    
10      num_args = 0,    
11      required_num_args = 0,    
12      arg_info = 0x0,    
13      refcount = 0x7ffff6002000,    
14      last = 6,    
15      opcodes = 0x7ffff6076240,    
16      last_var = 2,    
17      T = 4,    
18      vars = 0x7ffff6079030,    
19      last_live_range = 0,    
20      last_try_catch = 0,    
21      live_range = 0x0,    
22      try_catch_array = 0x0,    
23      static_variables = 0x0,    
24      filename = 0x7ffff605c2d0,    
25      line_start = 1,    
26      line_end = 7,    
27      doc_comment = 0x0,    
28      early_binding = 4294967295,    
29      last_literal = 3,    
30      literals = 0x7ffff60030c0,    
31      cache_size = 0,    
32      run_time_cache = 0x0,    
33      reserved = {0x0, 0x0, 0x0, 0x0}    
34    }

我想打出op_array.filename.val的具体值

1    (gdb) p (op_array.filename.len)    
2    $12 = 40    
3    (gdb) p *(op_array.filename.val)@40    
4    $13 = "/home/xiaoju/software/php7/demo/echo.php"

好了,我们可以顺便研究下_zend_op_array这个结构:

01    // opcode组成的数组,编译的时候就是生成这个结构    
02    struct _zend_op_array {    
03         zend_uchar type;  // op array的类型,比如 ZEND_EVAL_CODE    
04         zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */    
05         uint32_t fn_flags;    
06         zend_string *function_name;    
07         zend_class_entry *scope;    
08         zend_function *prototype;    
09         uint32_t num_args;  // 脚本的参数    
10         uint32_t required_num_args;    
11         zend_arg_info *arg_info;    
12         /* END of common elements */    
13    
14         uint32_t *refcount; // 这个结构的引用次数    
15    
16         uint32_t last;  // opcode的个数    
17         zend_op *opcodes;  // 存储所有的opcode    
18    
19         int last_var; // php变量的个数    
20         uint32_t T;    
21         zend_string **vars; // 被编译的php变量的个数    
22    
23         int last_live_range;    
24         int last_try_catch;  // try_catch的个数    
25         zend_live_range *live_range;    
26         zend_try_catch_element *try_catch_array; //    
27    
28         /* static variables support */    
29         HashTable *static_variables; // 静态变量    
30    
31         zend_string *filename;  // 执行的脚本的文件    
32         uint32_t line_start; // 开始于第几行    
33         uint32_t line_end; // 结束于第几行    
34         zend_string *doc_comment; // 文档的注释    
35         uint32_t early_binding; /* the linked list of delayed declarations */    
36    
37         int last_literal;    
38         zval *literals;    
39    
40         int  cache_size;    
41         void **run_time_cache;    
42    
43         void *reserved[ZEND_MAX_RESERVED_RESOURCES]; // 保留字段    
44    };

 以上就是php内核分析(六)-opcode的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

EditPlus chinesische Crack-Version

EditPlus chinesische Crack-Version

Geringe Größe, Syntaxhervorhebung, unterstützt keine Code-Eingabeaufforderungsfunktion

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

Dreamweaver Mac

Dreamweaver Mac

Visuelle Webentwicklungstools