php内核分析(六)-opcode

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

查看opcode

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

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

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

$ bin/phpdbg -f /home/xiaoju/software/php7/demo/echo.php
prompt> list 100
00001: <?php
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              #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 <?php23 $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这行设置个断点:

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

继续跑

(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}}

我可以优化输出:

(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这个结构:

// opcode组成的数组,编译的时候就是生成这个结构
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;  // 脚本的参数uint32_t required_num_args;zend_arg_info *arg_info;/* END of common elements */uint32_t *refcount; // 这个结构的引用次数uint32_t last;  // opcode的个数zend_op *opcodes;  // 存储所有的opcodeint last_var; // php变量的个数uint32_t T;zend_string **vars; // 被编译的php变量的个数int last_live_range;int last_try_catch;  // try_catch的个数zend_live_range *live_range;zend_try_catch_element *try_catch_array; ///* static variables support */HashTable *static_variables; // 静态变量zend_string *filename;  // 执行的脚本的文件uint32_t line_start; // 开始于第几行uint32_t line_end; // 结束于第几行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]; // 保留字段
};本文转自轩脉刃博客园博客,原文链接:http://www.cnblogs.com/yjf512/p/6112634.html,如需转载请自行联系原作者

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278659.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue 监听路由变化

一.watch监听$route&#xff08;$router的对象&#xff09; // 监听,当路由发生变化的时候执行 watch:{$route(to,from){console.log(to.path);} },// 监听,当路由发生变化的时候执行 watch: {$route: {handler: function(val, oldVal){console.log(val);},// 深度观察监听dee…

音频剪切_音频编辑入门指南:剪切,修剪和排列

音频剪切Audacity novices often start with lofty project ideas, but sometimes they lack the basics. Knowing how to cut and trim tracks is basic audio editing and is a fundamental starting point for making more elaborate arrangements. 大胆的新手通常从崇高的项…

Mybatis自定义SQL拦截器

本博客介绍的是继承Mybatis提供的Interface接口&#xff0c;自定义拦截器&#xff0c;然后将项目中的sql拦截一下&#xff0c;打印到控制台。 先自定义一个拦截器 package com.muses.taoshop.common.core.database.config;import org.apache.commons.lang3.StringUtils; import…

搭建spring boot环境并测试一个controller

Idea搭建spring boot环境一、新建项目二、起步依赖三、编写SpringBoot引导类四、编写Controller五、热部署一、新建项目 1.新建project 2.选择SpringInitializr&#xff0c;选择jdk&#xff0c;没有则需要下载并配置(若选择Maven工程则需要自己添加pom.xml所需依赖坐标和Java…

音频噪声抑制_音频编辑入门指南:基本噪声消除

音频噪声抑制Laying down some vocals? Starting your own podcast? Here’s how to remove noise from a messy audio track in Audacity quickly and easily. 放下人声&#xff1f; 开始自己的播客&#xff1f; 这是在Audacity中快速轻松地消除杂乱音轨中噪声的方法。 Th…

Dubbo集群容错

转自dubbo官网文档http://dubbo.apache.org/zh-cn/blog/dubbo-cluster-error-handling.html Design For failure 在分布式系统中&#xff0c;集群某个某些节点出现问题是大概率事件&#xff0c;因此在设计分布式RPC框架的过程中&#xff0c;必须要把失败作为设计的一等公民来对…

Linux基础(day53)

2019独角兽企业重金招聘Python工程师标准>>> 12.21 php-fpm的pool php-fpm的pool目录概要 vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加include etc/php-fpm.d/*.confmkdir /usr/local/php/etc/php-fpm.d/cd /usr/local/php/etc/php-fpm.d/vim www.co…

Mysql+Navicat for Mysql

一、mysql 1.下载安装 Mysql官网下载地址 下载后解压 .zip &#xff08;或安装.msi&#xff09; 2.可加入全局变量mysqld &#xff08;可选&#xff09; 我的电脑->属性->高级->环境变量->Path(系统变量)&#xff0c;添加mysql下的bin目录&#xff0c;如 D:\Pr…

公钥,私钥和数字签名

一、公钥加密 假设一下&#xff0c;我找了两个数字&#xff0c;一个是1&#xff0c;一个是2。我喜欢2这个数字&#xff0c;就保留起来&#xff0c;不告诉你们(私钥&#xff09;&#xff0c;然后我告诉大家&#xff0c;1是我的公钥。 我有一个文件&#xff0c;不能让别人看&…

MySQL中的日志类型(二)-General query log

简介 General query log记录客户端的连接和断开&#xff0c;以及从客户端发来的每一个SQL语句。 日志内容格式 General query log可以记录在文件中&#xff0c;也可以记录在表中&#xff0c;格式如下&#xff1a;在文件中会记录时间、线程ID、命令类型以及执行的语句示例如下&a…

android wi-fi_如何在Android手机上查找3G或Wi-Fi速度

android wi-fiAre you curious about what kind of connection speed you are getting with your Android phone? Today we’ll take a look at how to easily check your Wi-Fi or 3G speeds with Speedtest.net’s Speed Test app. 您是否对Android手机的连接速度感到好奇&a…

vue引入全局less实现全局变量的控制

vue引入全局less1.设置全局样式变量的好处&#xff1a;2.以less为例&#xff08;sass等同原理&#xff09;1.vue-cli2搭建的项目&#xff08;1&#xff09;2.vue-cli2搭建的项目&#xff08;2&#xff09;3.vue-cli3、vue-cli43.vue-cli2和vue-cli3的区别4.vue-cli3和vue-cli4的…

如何在eclipse中对项目进行重新编译

有时由于eclipse异常关闭&#xff0c;当我们重启Eclipse&#xff0c;在启动项目时&#xff0c;会报错&#xff0c;说&#xff1a;ClassNotFound类似的错误&#xff0c;引起这种问题的原因可能是由于&#xff0c;Eclipse异常关闭引起的。 解决&#xff1a;在一个项目中&#xff…

SQL 查询数据库中包含指定字符串的相关表和相关记录

declare str varchar(100)set str我要找的 --要搜索的字符串declare s varchar(8000)declare tb cursor local forselect if exists(select 1 from [b.name] where [a.name] like %str%)print [b.name].[a.name]from syscolumns a join sysobjects b on a.idb.idwhere b.xtype…

如何在Gmail的图片中插入超链接

Adding hyperlinks is an efficient way of getting your reader to the intended web page. Though it’s no secret that you can add hyperlinks to text, Gmail also lets you add hyperlinks to images in the body of the email. Here’s how to make it happen. 添加超链…

内联元素居中

父元素&#xff1a; height:100px; line-height:100px; // 与高相同 text-align:center; 子元素: display:inline; vertical-align: middle; 适用图片、文字 <div><div class"wrapper"><span>我是文字</span></div><div class&qu…

防止html标签转义

function htmlDecode ( str ) {var ele document.createElement(span);ele.innerHTML str;return ele.textContent;} 例如body下边所有的p标签都防止转义&#xff1a; $.each($("body").find(p),function(){this.innerHTML htmlDecode(this.innerHTML);}); 转载于…

新垣结衣自拍照_如何阻止自拍照出现在iPhone的自拍照专辑中

新垣结衣自拍照Khamosh PathakKhamosh PathakThe Photos app on your iPhone automatically populates all photos from the front-facing camera in the Selfies album. But what if you don’t want a photo to appear there? Here are a couple of solutions. iPhone上的“…

前端个人笔记

前端个人笔记1.vue项目安装依赖/插件时忘记--save&#xff0c;再次install出问题并且没有报错。2.margin移动元素不显示背景色3.新知识&#xff1a;media 条件样式4.入坑&#xff1a;row和col不能分离&#xff0c;span24不能不写5.聚焦实现滚动到指定元素1.vue项目安装依赖/插件…

kernel中对文件的读写【学习笔记】【原创】

/*1. 头文件 */ #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/fs.h> #include <linux/uaccess.h>MODULE_PARM_DESC(iva…