扩展编译好用,通过php编码测试报“段错误",如果是c语言都是用gdb进行设置,那php扩展要如何进行调试呢?搜索了下,虽然是php扩展但是core是php 的core不是单个so扩展的coredump
这里使用ulimit -c unlimited来开启core文件,使用gdb来对core文件进行调试演示一下。
 
root@debian:~/php# php a.php 
段错误
root@debian:~/php# ulimit -c unlimited
root@debian:~/php# php a.php
段错误 (core dumped)
root@debian:~/php# ls
a.php core
root@debian:~/php# apt-get install gdb
root@debian:~/php# gdb php -c core
Core was generated by `php a.php'.
Program terminated with signal 11, Segmentation fault.
#0 0xb6a7bfb8 in zif_smtpmail_connect (ht=5, return_value=0xb6f149dc, return_value_ptr=0x0, this_ptr=0x0, return_value_used=1) at /root/php/php-5.4.7/ext/smtpmail/smtpmail.c:281
281 if(strcmp(substring(lastmessage, 1, 3), hen) !=0 || strlen(lastmessage)==0) {
(gdb) (gdb) source ./php-5.4.7/.gdbinit
(gdb) zbacktrace
[0xb6efb030] smtpmail_connect("smtp.qq.com", "xxxx", "xxx", "xxxx@qq.com", 25) /root/php/a.php:5

PHP的代码包中提供了一个 .gdbinit 的gdb脚本文件,里面提供了20多个 gdb 的自定义命令,用于方便PHP的调试,下面举几个例子:
测试脚本a.php:

<?php
$a = "AAA";
$b = "BBB";
test("phpor");
function test($name) {
$m = "MMM";
$n = "NNN";
sleep(1);
echo$name;
}
1
2
3
4
5
6
7
8
9
10
<?php
$a = "AAA";
$b = "BBB";
test("phpor");
function test($name) {
        $m = "MMM";
        $n = "NNN";
        sleep(1);
        echo$name;
}

gdb 调试命令:
———————–
gdb php
set args a.php
break sleep
r

———————–

1. print_cvs 打印当前执行环境中已编译的PHP变量, 如:

(gdb) print_cvs
Compiled variables count: 3
0 = name
[0x9543f7c] (refcount=2) string(5): "phpor"
1 = m
[0x9543f98] (refcount=1) string(3): "MMM"
2 = n
[0x9543fb4] (refcount=1) string(3): "NNN"
(gdb)
1
2
3
4
5
6
7
8
9
(gdb) print_cvs
Compiled variables count: 3
0 = name
[0x9543f7c] (refcount=2) string(5): "phpor"
1 = m
[0x9543f98] (refcount=1) string(3): "MMM"
2 = n
[0x9543fb4] (refcount=1) string(3): "NNN"
(gdb)


2. printzv 打印指定的PHP变量, 需要指定地址, 如:

(gdb) printzv 0x9543f98
[0x9543f98] (refcount=1) string(3): “MMM”
(gdb)

3. 打印PHP的函数调用栈, 如:
(gdb) zbacktrace
[0x95770a4] sleep(1) /usr/home/junjie2/a.php:11
[0x9576fe0] test(“phpor”) /usr/home/junjie2/a.php:7
(gdb)

4. print_ft 打印函数表( HashTable )
(gdb) set $eg = executor_globals
(gdb) print $eg.function_table  
$6 = (HashTable *) 0xa5bd450
(gdb) print_ft $eg.function_table
[0xa5bd450] {
“zend_version\0″ => “zend_version”
“func_num_args\0″ => “func_num_args”
“func_get_arg\0″ => “func_get_arg”
“func_get_args\0″ => “func_get_args”
“strlen\0″ => “strlen”
“strcmp\0″ => “strcmp”
“strncmp\0″ => “strncmp”
“strcasecmp\0″ => “strcasecmp”
“strncasecmp\0″ => “strncasecmp”
“each\0″ => “each”

学习
1. 通过阅读 print_cvs 命令的实现,可以知道可以通过prev_execute_data来打印上一执行空间的PHP变量,如:
(gdb) printzv *executor_globals.current_execute_data->prev_execute_data->CVs[1]  
[0x9543408] (refcount=1) string(3): “AAA”
(gdb) printzv *executor_globals.current_execute_data->prev_execute_data->CVs[2]  
[0x95433ec] (refcount=1) string(3): “BBB”
(gdb)

2. 通过 op_array->vars 来找到对应的变量的名字
(gdb) printf “%s\n” ,executor_globals.current_execute_data->prev_execute_data->op_array->vars[1].name  
a
(gdb) printf “%s\n” ,executor_globals.current_execute_data->prev_execute_data->op_array->vars[2].name  
b

3. 修改了一下 print_cvs 命令,通过参数来获取每个执行空间的PHP的已编译变量

使用方法:
———————
(gdb) zbacktrace  #查看PHP的调用栈
[0x8ce2078] sleep(1) /usr/home/junjie2/a.php:9
[0x8ce1fe0] test(“phpor”) /usr/home/junjie2/a.php:5
(gdb) print_cvs #查看当前执行空间中的PHP变量
Compiled variables count: 3
0 = name
[0x8cae3d0] (refcount=2) string(5): “phpor”
1 = m
[0x8cae93c] (refcount=1) string(3): “MMM”
2 = n
[0x8cae958] (refcount=1) string(3): “NNN”
(gdb) print_cvs 2 # 查看指定执行空间中的PHP变量, 这个参数给大了也没关系,最多打印最外层的PHP变量
Compiled variables count: 2
0 = a
[0x8cae408] (refcount=1) string(3): “AAA”
1 = b
[0x8cae3ec] (refcount=1) string(3): “BBB”
(gdb)
———————

4. 有时候需要借助环境变量,如:
(gdb) print_ft (HashTable *)0xa5bd450
A syntax error in expression, near `’.
(gdb) set $phpor=(HashTable *)0xa5bd450
(gdb) print_ft $phpor
[0xa5bd450] {
“zend_version\0″ => “zend_version”
“func_num_args\0″ => “func_num_args”
“func_get_arg\0″ => “func_get_arg”
“func_get_args\0″ => “func_get_args”
“strlen\0″ => “strlen”
“strcmp\0″ => “strcmp”
“strncmp\0″ => “strncmp”

5. 打印指定的PHP变量

define print_pval
____executor_globals
set $p = $eg.current_execute_data.CVs
set $c = $eg.current_execute_data.op_array.last_var
set $v = $eg.current_execute_data.op_array.vars
set $i = 0
set $name = $arg0

#printf "search php var: %s c:%d\n", $name, $c
while $i < $c
#printf "name: %s\n", $name
set $n = $v[$i].name
# use strcmp but ==
if strcmp($n, $name) == 0
if $p[$i] != 0
printzv *$p[$i]
# use loop_break but break
loop_break
end
end
#printf "i %d, %s\n", $i, $v[$i].name
set $i = $i + 1
end
if $i == $c
printf "no found var named: %s\n", $name
end
end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
define print_pval
    ____executor_globals
    set $p = $eg.current_execute_data.CVs
    set $c = $eg.current_execute_data.op_array.last_var
    set $v = $eg.current_execute_data.op_array.vars
    set $i = 0
    set $name = $arg0
 
    #printf "search php var: %s c:%d\n", $name, $c
    while $i < $c
        #printf "name: %s\n", $name
        set $n = $v[$i].name
        # use strcmp but ==
        if strcmp($n, $name) == 0
            if $p[$i] != 0
                printzv *$p[$i]
                # use loop_break but break
                loop_break
            end    
        end    
        #printf "i %d, %s\n", $i, $v[$i].name
        set $i = $i + 1
    end
    if $i == $c
        printf "no found var named: %s\n", $name
    end
end

最新文章

  1. Qt实现端口扫描器
  2. python 输出十六进制中文乱码
  3. opencv6.2-imgproc图像处理模块之图像尺寸上的操作及阈值
  4. jq实现楼层切换效果
  5. 从零开始之ecshop基础篇(17)
  6. POJ3009——Curling 2.0(DFS)
  7. (四)《Java编程思想》——可变参数列表
  8. ASP.NET WebForm 通过 PagedDataSource 实现 Repeater 的分页
  9. Linux搭建Apache+Tomcat实现负载均衡
  10. CrashHandler
  11. HBase篇(1)-特性与应用场景
  12. 自动化测试-13.selenium执行JS处理滚动条
  13. Selenium2自动化测试实战(基于Python语言)— 编写第一个自动化脚本
  14. bit,Byte,Word,DWORD(DOUBLE WORD,DW)
  15. c++ primer plus 第四章 课后题答案
  16. 常用的SLAM解决方案
  17. cf499A-Watching a movie
  18. springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能
  19. 谁说 JavaScript 很简单了?
  20. Python自然语言处理系列之模拟退火算法

热门文章

  1. poj2378(dfs,树形dp)
  2. div垂直水平居中的四种方法总结
  3. Mac OS X显示隐藏文件命令
  4. GZip压缩与解压缩
  5. PAT1018——最短路加DFS
  6. ubuntu14.04安装pyspider
  7. his移植问题
  8. C#去除数组空格
  9. Python3中的http.client模块
  10. tomcat 并发配置优化