php内核分析(六)-opcode
這里閱讀的php版本為PHP-7.1.0 RC3,閱讀代碼的平臺為linux
查看opcode
php是先把源碼解析成opcode,然后再把opcode傳遞給zend_vm進行執(zhí)行的。
// 一個opcode的結(jié)構(gòu) struct _zend_op {const void *handler; // opcode對應(yīng)的執(zhí)行函數(shù),每個opcode都有一個對應(yīng)的執(zhí)行函數(shù)znode_op op1; // 執(zhí)行參數(shù)的第一個元素znode_op op2; // 執(zhí)行參數(shù)的第二個元素znode_op result; // 執(zhí)行結(jié)果uint32_t extended_value; // 額外擴展的字段和值uint32_t lineno; // 行數(shù)zend_uchar opcode; // 操作碼,具體操作碼列表見 http://cn.php.net/manual/zh/internals2.opcodes.phpzend_uchar op1_type; // 第一個元素的類型zend_uchar op2_type; // 第二個元素的類型zend_uchar result_type; // 結(jié)果的類型 };在php7中,我們能很方便用phpdbg來查看一個文件或者一個函數(shù)的opcode了。至于phpdbg的使用,現(xiàn)在網(wǎng)上介紹不多,不過好在有很詳細的help文檔。下面是一個最簡單的opcode代碼:
$ bin/phpdbg -f /home/xiaoju/software/php7/demo/echo.php prompt> list 100 00001: 00002: 00003: $a = 1; 00004: $b = $a; 00005: $b = $b + 1; 00006: echo $b; 00007: prompt> print exec [Context /home/xiaoju/software/php7/demo/echo.php (6 ops)] L1-7 {main}() /home/xiaoju/software/php7/demo/echo.php - 0x7fe3fae63300 + 6 ops L3 #0 ASSIGN $a 1 L4 #1 ASSIGN $b $a L5 #2 ADD $b 1 ~2 L5 #3 ASSIGN $b ~2 L6 #4 ECHO $b L7 #5 RETURN 1這個php文件就做了一個最簡單的加法操作。生成了6個_zend_op。所展示的每一行代表一個_zend_op
_zendop.lineno op號 _zend_op.opcode _zend_op.op1 _zend_op.op2 _zend_op.result L5這里_zend_op.opcode對應(yīng)的操作在官網(wǎng)有文檔和詳細的例子可以查看:http://cn.php.net/manual/zh/internals2.opcodes.php
值得一說的是,phpdbg還有一個遠端UI版本,能讓我們在近端診斷服務(wù)端的php信息
gdb
但是我們的目標還是在于研究php源碼,phpdbg只能分析到opcode這層,還是不夠的,gdb可能是更好的選擇。
gdb的使用和平時使用差不多
比如我現(xiàn)在有個腳本echo.php:
1 23 $a = 1;4 $b = $a;5 $b = $b + 1;6 echo $b;我的php安裝路徑在:
xiaojuphp7phpphp源碼路徑在:
xiaojuphp-src運行g(shù)db
$ gdb xiaojuphp7php加載gdbinit:
(gdb) source xiaojuphp-src.gdbinit設(shè)置斷點:
(gdb) b zend_execute_scripts運行:
(gdb) run -f xiaojuphp7echo.php我想在1459這行設(shè)置個斷點:
1452 for (i = 0; i < file_count; i++) { 1453 file_handle = va_arg(files, zend_file_handle *); 1454 if (!file_handle) { 1455 continue; 1456 } 1457 1458 op_array = zend_compile_file(file_handle, type); 1459 if (file_handle->opened_path) { 1460 zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path); 1461 }(gdb) b 1459繼續(xù)跑
(gdb) continue (gdb) s (gdb) s打印出這個時候的op_array
(gdb) p *op_array $4 = {type = 2 '\002', arg_flags = "\000\000", fn_flags = 134217728, function_name = 0x0, scope = 0x0,prototype = 0x0, num_args = 0, required_num_args = 0, arg_info = 0x0, refcount = 0x7ffff6002000, last = 6,opcodes = 0x7ffff6076240, last_var = 2, T = 4, vars = 0x7ffff6079030, last_live_range = 0, last_try_catch = 0,live_range = 0x0, try_catch_array = 0x0, static_variables = 0x0, filename = 0x7ffff605c2d0, line_start = 1,line_end = 7, doc_comment = 0x0, early_binding = 4294967295, last_literal = 3, literals = 0x7ffff60030c0,cache_size = 0, run_time_cache = 0x0, reserved = {0x0, 0x0, 0x0, 0x0}}我可以優(yōu)化輸出:
(gdb) set print pretty on (gdb) p *op_array $5 = {type = 2 '\002',arg_flags = "\000\000",fn_flags = 134217728,function_name = 0x0,scope = 0x0,prototype = 0x0,num_args = 0,required_num_args = 0,arg_info = 0x0,refcount = 0x7ffff6002000,last = 6,opcodes = 0x7ffff6076240,last_var = 2,T = 4,vars = 0x7ffff6079030,last_live_range = 0,last_try_catch = 0,live_range = 0x0,try_catch_array = 0x0,static_variables = 0x0,filename = 0x7ffff605c2d0,line_start = 1,line_end = 7,doc_comment = 0x0,early_binding = 4294967295,last_literal = 3,literals = 0x7ffff60030c0,cache_size = 0,run_time_cache = 0x0,reserved = {0x0, 0x0, 0x0, 0x0} }我想打出op_array.filename.val的具體值
(gdb) p (op_array.filename.len) $12 = 40 (gdb) p *(op_array.filename.val)@40 $13 = "/home/xiaoju/software/php7/demo/echo.php"好了,我們可以順便研究下_zend_op_array這個結(jié)構(gòu):
// opcode組成的數(shù)組,編譯的時候就是生成這個結(jié)構(gòu) struct _zend_op_array {zend_uchar type; // op array的類型,比如 ZEND_EVAL_CODEzend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */uint32_t fn_flags;zend_string *function_name;zend_class_entry *scope;zend_function *prototype;uint32_t num_args; // 腳本的參數(shù)uint32_t required_num_args;zend_arg_info *arg_info;/* END of common elements */uint32_t *refcount; // 這個結(jié)構(gòu)的引用次數(shù)uint32_t last; // opcode的個數(shù)zend_op *opcodes; // 存儲所有的opcodeint last_var; // php變量的個數(shù)uint32_t T;zend_string **vars; // 被編譯的php變量的個數(shù)int last_live_range;int last_try_catch; // try_catch的個數(shù)zend_live_range *live_range;zend_try_catch_element *try_catch_array; ///* static variables support */HashTable *static_variables; // 靜態(tài)變量zend_string *filename; // 執(zhí)行的腳本的文件uint32_t line_start; // 開始于第幾行uint32_t line_end; // 結(jié)束于第幾行zend_string *doc_comment; // 文檔的注釋uint32_t early_binding; /* the linked list of delayed declarations */int last_literal;zval *literals;int cache_size;void **run_time_cache;void *reserved[ZEND_MAX_RESERVED_RESOURCES]; // 保留字段 };本文轉(zhuǎn)自軒脈刃博客園博客,原文鏈接:http://www.cnblogs.com/yjf512/p/6112634.html,如需轉(zhuǎn)載請自行聯(lián)系原作者總結(jié)
以上是生活随笔為你收集整理的php内核分析(六)-opcode的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: yarn安装依赖包报错 error A
- 下一篇: mac安装vue-cli脚手架;脚手架安