起因
最近在看PHP7的源码,想要看一下一个zval一个执行流程,此时需要用到gdb调试,然后按照相关的步骤进行
每一个zval的字节数是16,zval的结构类型
struct _zval_struct {
zend_value value; //8个字节
union u1; //4个字节
union u2; //4个字节
}
具体的,这个是php-7.4.19中的
struct _zval_struct {
zend_value value; /* value */ //8个字节
union { //4个字节
struct {
ZEND_ENDIAN_LOHI_3(
zend_uchar type, /* active type */ //通过type值来区分类型
zend_uchar type_flags,
union {
uint16_t extra; /* not further specified */
} u)
} v;
uint32_t type_info;
} u1;
union {//4个字节
uint32_t next; /* hash collision chain */ //解决哈希冲突的
uint32_t cache_slot; /* cache slot (for RECV_INIT) */ //运行时缓存
uint32_t opline_num; /* opline number (for FAST_CALL) */ //
uint32_t lineno; /* line number (for ast nodes) */ //php的哪一行
uint32_t num_args; /* arguments number for EX(This) */ //参数的个数
uint32_t fe_pos; /* foreach position */
uint32_t fe_iter_idx; /* foreach iterator index */ //游标的索引位置
uint32_t access_flags; /* class constant access flags */ //prviate protected public
uint32_t property_guard; /* single property guard */ //防止类中魔术方法的循环引用
uint32_t constant_flags; /* constant flags */
uint32_t extra; /* not further specified */
} u2;
};
gdb php
b ZEND_ECHO_SPEC_CV_HANDLER #设置断点
r zval.php
n
n
p z
p *z
.
.
.
zval.php
<?php
$a = 2;
echo $a;
$b = 1.1;
echo $b;
$c = null;
echo $c;
$d = true;
echo $d;
$e = false;
echo $e;
$f = 'string';
echo $f;
$g = [1, 2, 3];
echo $g;
$h = new stdClass();
echo $h;// FATAL ERROR