设为首页 收藏本站
查看: 445|回复: 0

[经验分享] (官方文档)php的gdb调试

[复制链接]

尚未签到

发表于 2017-3-28 14:38:38 | 显示全部楼层 |阅读模式
Generating a gdb backtrace

Noticing PHP crashes
  There's no absolute way to know that PHP is crashing, but there may be signs. Typically, if you access a page that is always supposed to generate output (has a leading HTML block, for example), and suddenly get "Document contains no data" from your browser, it may mean that PHP crashes somewhere along the execution of the script. Another way to tell that PHP is crashing is by looking at the Apache error logs, and looking for SEGV (Apache 1.2) or Segmentation Fault (Apache 1.3).

Important!
  To get a backtrace with correct information you must have PHP configured with --enable-debug!

If you don't have a core file yet:


  • Remove any limits you may have on core dump size from your shell:

    • tcsh: unlimit coredumpsize
    • bash/sh: ulimit -c unlimited


  • Ensure that the directory in which you're running PHP, or the PHP-enabled httpd, has write permissions for the user who's running PHP.
  • Cause PHP to crash:

    • PHP CGI: Simply run php with the script that crashes it
    • PHP Apache Module: Run httpd -X, and access the script that crashes PHP



Generic way to get a core on Linux


  • Set up the core pattern (run this command as root):

    • echo "<cores dir>/core-%e.%p" > /proc/sys/kernel/core_pattern
    • make sure the directory is writable by PHP


  • Set the ulimit (see above how to do it).
  • Restart/rerun PHP.
  After that any process crashing in your system, including PHP, will leave its core file in the directory you've specified in core_pattern.

Once you have the core file:


  • Run gdb with the path to the PHP or PHP-enabled httpd binary, and path to the core file. Some examples:


    • gdb /usr/local/apache/sbin/httpd /usr/local/apache/sbin/core

    • gdb /home/user/dev/php-snaps/sapi/cli/php /home/user/dev/testing/core


  • At the gdb prompt, run:


    • (gdb) bt



If you can't get a core file:


  • Run httpd -X under gdb with something like:


    • gdb /usr/local/apache/sbin/httpd
    • (gdb) run -X


  • Then use your web browser and access your server to force the crash. You should see a gdb prompt appear and some message indicating that there was a crash. At this gdb prompt, type:


    • (gdb) bt


    • or, running from the commandline

      • gdb /home/user/dev/php-snaps/sapi/cli/php


        • (gdb) run /path/to/script.php

        • (gdb) bt






  This should generate a backtrace, that you should submit in the bug report, along with any other details you can give us about your setup, and offending script.

Locating which function call caused a segfault:
  You can locate the function call that caused a segfault, easily, with gdb. First, you need a core file or to generate a segfault under gdb as described above.
  In PHP, each function is executed by an internal function called execute() and has its own stack. Each line generated by the bt command represents a function call stack. Typically, you will see several execute() lines when you issue bt. You are interested in the last execute() stack (i.e. smallest frame number). You can move the current working stack with the up, down or frame commands. Below is an example gdb session that can be used as a guideline on how to handle your segfault.


  • Sample gdb session

    (gdb) bt
    #0  0x080ca21b in _efree (ptr=0xbfffdb9b) at zend_alloc.c:240
    #1  0x080d691a in _zval_dtor (zvalue=0x8186b94) at zend_variables.c:44
    #2  0x080cfab3 in _zval_ptr_dtor (zval_ptr=0xbfffdbfc) at zend_execute_API.c:274
    #3  0x080f1cc4 in execute (op_array=0x816c670) at ./zend_execute.c:1605
    #4  0x080f1e06 in execute (op_array=0x816c530) at ./zend_execute.c:1638
    #5  0x080f1e06 in execute (op_array=0x816c278) at ./zend_execute.c:1638
    #6  0x080f1e06 in execute (op_array=0x8166eec) at ./zend_execute.c:1638
    #7  0x080d7b93 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at zend.c:810
    #8  0x0805ea75 in php_execute_script (primary_file=0xbffff650) at main.c:1310
    #9  0x0805cdb3 in main (argc=2, argv=0xbffff6fc) at cgi_main.c:753
    #10 0x400c91be in __libc_start_main (main=0x805c580 , argc=2, ubp_av=0xbffff6fc,
    init=0x805b080 <_init>, fini=0x80f67b4 <_fini>, rtld_fini=0x4000ddd0 <_dl_fini>,
    stack_end=0xbffff6ec) at ../sysdeps/generic/libc-start.c:129
    (gdb) frame 3
    #3  0x080f1cc4 in execute (op_array=0x816c670) at ./zend_execute.c:1605
    (gdb) print (char *)(executor_globals.function_state_ptr->function)->common.function_name
    $14 = 0x80fa6fa "pg_result_error"
    (gdb) print (char *)executor_globals.active_op_array->function_name
    $15 = 0x816cfc4 "result_error"
    (gdb) print (char *)executor_globals.active_op_array->filename
    $16 = 0x816afbc "/home/yohgaki/php/DEV/segfault.php"
    (gdb)
  In this session, frame 3 is the last execute() call. The frame 3 command moves the current working stack to the proper frame.
print (char *)(executor_globals.function_state_ptr->function)->common.function_name
prints the function name. In the sample gdb session, the pg_result_error() call is causing the segfault. You can print any internal data that you like, if you know the internal data structure. Please do not ask how to use gdb or about the internal data structure. Refer to gdb manual for gdb usage and to the PHP source for the internal data structure.
  You may not see execute if the segfault happens without calling any functions.
  有个作者自己写的脚本gdb调试的文章: http://hi.baidu.com/thinkinginlamp/blog/item/5e8062d9744f012511df9b93.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-356632-1-1.html 上篇帖子: PHP验证码程序:session生成图片 下篇帖子: Php学习《五》 ------ 基本知识摘录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表