GDB调试PHP代码入门

1、源码编译安装php,带–enable-debug参数

wget -O /opt/apps/phpbrew/distfiles/php-7.1.0.tar.bz2 “http://ftp.ntu.edu.tw/pub/php/distributions/php-7.1.0.tar.bz2”

phpbrew –debug install -j $(nproc) 7.1.0 +everything — \

–with-iconv=/usr/local \

–with-gd \

–with-openssl \

–enable-debug

2、编写简单php代码

➜ source cat zval.php

<?php

$a = 10;

echo $a;

$b = 20.1;

echo $b;

$c = $a;

echo $c;

3、gdb调试

gdb php

a、加断点

b ZEND_ECHO_SPEC_CV_HANDLER

b、执行代码

r zval.php

c、查询当前定义的变量

(gdb) info locals

z = 0x7fffecc13080

(gdb) ptype z

type = struct _zval_struct {

zend_value value;

union {

struct {…} v;

uint32_t type_info;

} u1;

union {

uint32_t next;

uint32_t cache_slot;

uint32_t lineno;

uint32_t num_args;

uint32_t fe_pos;

uint32_t fe_iter_idx;

uint32_t access_flags;

uint32_t property_guard;

} u2;

} *

d、打印变量

(gdb) p *(zval *)z

$3 = {

value = {

lval = 10,

dval = 4.9406564584124654e-323,

counted = 0xa,

str = 0xa,

arr = 0xa,

obj = 0xa,

res = 0xa,

ref = 0xa,

ast = 0xa,

zv = 0xa,

ptr = 0xa,

ce = 0xa,

func = 0xa,

ww = {

w1 = 10,

w2 = 0

}

},

u1 = {

v = {

type = 4 ‘\004’,

type_flags = 0 ‘\000’,

const_flags = 0 ‘\000’,

reserved = 0 ‘\000’

},

type_info = 4

},

u2 = {

next = 0,

cache_slot = 0,

lineno = 0,

num_args = 0,

fe_pos = 0,

fe_iter_idx = 0,

—Type <return> to continue, or q <return> to quit—

access_flags = 0,

property_guard = 0

}

}

(gdb) p *(zval *)&(zval *)z.value

$4 = {

value = {

lval = 10,

dval = 4.9406564584124654e-323,

counted = 0xa,

str = 0xa,

arr = 0xa,

obj = 0xa,

res = 0xa,

ref = 0xa,

ast = 0xa,

zv = 0xa,

ptr = 0xa,

ce = 0xa,

func = 0xa,

ww = {

w1 = 10,

w2 = 0

}

},

u1 = {

v = {

type = 4 ‘\004’,

type_flags = 0 ‘\000’,

const_flags = 0 ‘\000’,

reserved = 0 ‘\000’

},

type_info = 4

},

u2 = {

next = 0,

cache_slot = 0,

lineno = 0,

num_args = 0,

fe_pos = 0,

fe_iter_idx = 0,

—Type <return> to continue, or q <return> to quit—

access_flags = 0,

property_guard = 0

}

}

对照PHP源码:

Zend/zend_types.h-302-/* regular data types */

Zend/zend_types.h-303-#define IS_UNDEF 0

Zend/zend_types.h-304-#define IS_NULL 1

Zend/zend_types.h-305-#define IS_FALSE 2

Zend/zend_types.h-306-#define IS_TRUE 3

Zend/zend_types.h:307:#define IS_LONG 4

Zend/zend_types.h-308-#define IS_DOUBLE 5

Zend/zend_types.h-309-#define IS_STRING 6

Zend/zend_types.h-310-#define IS_ARRAY 7

Zend/zend_types.h-311-#define IS_OBJECT 8

Zend/zend_types.h-312-#define IS_RESOURCE 9

Zend/zend_types.h-313-#define IS_REFERENCE 10

u1.type 为 4时,变量值取 value.lval ( 10 )

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>