PHP_FUNCTION(var_dump) { zval *args; int argc; int i;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) { return; }
for (i = 0; i < argc; i++) { php_var_dump(&args[i], 1); } }
开始运行 PHP 文件:
1 2 3 4 5 6 7
(gdb) r index.php Starting program: /home/ubuntu/php-7.1.0/build/bin/php index.php [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Breakpoint 1, zif_var_dump (execute_data=0x7ffffb6130d0, return_value=0x7ffffffea400) at /home/ubuntu/php-7.1.0/ext/standard/var.c:200 200 {
代码执行到 zif_var_dump 就停止了,说明命中了断点。我们可以使用 l 命令查看附近的源码:
1 2 3 4 5 6 7 8 9 10 11
(gdb) l 195 /* }}} */ 196 197 /* {{{ proto void var_dump(mixed var) 198 Dumps a string representation of variable to output */ 199 PHP_FUNCTION(var_dump) 200 { 201 zval *args; 202 int argc; 203 int i; 204
使用 bt 命令查看调用栈:
1 2 3 4 5 6 7 8 9 10 11 12 13
(gdb) bt #0 zif_var_dump (execute_data=0x7ffffb6130d0, return_value=0x7ffffffea400) at /home/ubuntu/php-7.1.0/ext/standard/var.c:200 #1 0x0000000008642151 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER () at /home/ubuntu/php-7.1.0/Zend/zend_vm_execute.h:628 #2 0x000000000864038e in execute_ex (ex=0x7ffffb613030) at /home/ubuntu/php-7.1.0/Zend/zend_vm_execute.h:429 #3 0x0000000008640d05 in zend_execute (op_array=0x7ffffb67e000, return_value=0x0) at /home/ubuntu/php-7.1.0/Zend/zend_vm_execute.h:474 #4 0x00000000085a2ca6 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /home/ubuntu/php-7.1.0/Zend/zend.c:1474 #5 0x00000000084eb1ce in php_execute_script (primary_file=0x7ffffffecd00) at /home/ubuntu/php-7.1.0/main/main.c:2533 #6 0x0000000008743462 in do_cli (argc=2, argv=0x90c1ef0) at /home/ubuntu/php-7.1.0/sapi/cli/php_cli.c:990 #7 0x000000000874483e in main (argc=2, argv=0x90c1ef0) at /home/ubuntu/php-7.1.0/sapi/cli/php_cli.c:1378
使用 u 命令让代码运行到指定的行:
1 2 3 4
(gdb) u 209 zif_var_dump (execute_data=0x7ffffb6130d0, return_value=0x7ffffffea400) at /home/ubuntu/php-7.1.0/ext/standard/var.c:209 209 for (i = 0; i < argc; i++) {
使用 p 命令打印变量 $a 的值:
1 2
(gdb) p args[0].value.str.val $1 = "h"
使用 @ 指定输出长度:
1 2
(gdb) p args[0].value.str.val@5 $2 = {"h", "e", "l", "l", "o"}