深入了解PHP:用gdb调试源码.docx

上传人:安*** 文档编号:71086165 上传时间:2023-02-01 格式:DOCX 页数:13 大小:20.06KB
返回 下载 相关 举报
深入了解PHP:用gdb调试源码.docx_第1页
第1页 / 共13页
深入了解PHP:用gdb调试源码.docx_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《深入了解PHP:用gdb调试源码.docx》由会员分享,可在线阅读,更多相关《深入了解PHP:用gdb调试源码.docx(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、深入了解PHP:用gdb调试源码bobyzhang腾讯IEG运营开发工程师php编译时有一个debug形式这个形式会关闭内存优化提示内存泄露屏蔽调用栈优化可以让我们看到完好的phpc层面的调用栈。通常我会编译两个php版一个正常一个翻开debug在不同的目录通过export决定使用哪个。通过php-config命令可以看到configure-options修改其中的prefix以及with-config-file-path到新的目录然后添加-enable-debug命令yongkbmasterphp-configUsage:/data/env/runtime/php-7.1.33-debug/

2、bin/php-configOPTIONOptions:-prefix/data/env/runtime/php-7.1.33-debug-includes-I/data/env/runtime/php-7.1.33-debug/include/php-I/data/env/runtime/php-7.1.33-debug/include/php/main-I/data/env/runtime/php-7.1.33-debug/include/php/TSRM-I/data/env/runtime/php-7.1.33-debug/include/php/Zend-I/data/env/run

3、time/php-7.1.33-debug/include/php/ext-I/data/env/runtime/php-7.1.33-debug/include/php/ext/date/lib-ldflags-libs-lcrypt-lz-lexslt-lresolv-lcrypt-lrt-lldap-llber-lpng-lz-ljpeg-lcurl-lbz2-lz-lrt-lm-ldl-lnsl-lxml2-lz-lm-ldl-lgssapi_krb5-lkrb5-lk5crypto-lcom_err-lssl-lcrypto-lcurl-lxml2-lz-lm-ldl-lfreety

4、pe-lxml2-lz-lm-ldl-lxml2-lz-lm-ldl-lcrypt-lxml2-lz-lm-ldl-lxml2-lz-lm-ldl-lxml2-lz-lm-ldl-lxml2-lz-lm-ldl-lxslt-lxml2-lz-ldl-lm-lssl-lcrypto-lcrypt-extension-dir/data/env/runtime/php-7.1.33-debug/lib/php/extensions/debug-non-zts-20160303-include-dir/data/env/runtime/php-7.1.33-debug/include/php-man-

5、dir/data/env/runtime/php-7.1.33-debug/php/man-php-binary/data/env/runtime/php-7.1.33-debug/bin/php-php-sapisclifpmphpdbgcgi-configure-options-prefix/data/env/runtime/php-7.1.33-debug-enable-debug-enable-phpdbg-debug-with-config-file-path/data/env/runtime/php-7.1.33-debug/etc-with-curl-with-freetype-

6、dir-with-gd-with-gettext-with-iconv-dir-with-kerberos-with-libdirlib64-with-libxml-dir-with-mysqli-with-openssl-with-pcre-regex-with-pdo-mysql-with-pdo-sqlite-with-pear-with-png-dir-with-jpeg-dir-with-xmlrpc-with-xsl-with-zlib-with-bz2-with-mhash-enable-fpm-enable-bcmath-enable-libxml-enable-inline-

7、optimization-enable-gd-native-ttf-enable-mbregex-enable-mbstring-enable-opcache-enable-pcntl-enable-shmop-enable-soap-enable-sockets-enable-sysvsem-enable-sysvshm-enable-xml-enable-zip-with-ldap-version7.1.33-vernum70133修改之后大概是这样子然后编译安装就可以得到debug版本了-prefix/data/env/runtime/php-7.1.33-debug-enable-de

8、bug-enable-phpdbg-debug-with-config-file-path/data/env/runtime/php-7.1.33-debug/etc-with-curl-with-freetype-dir-with-gd-with-gettext-with-iconv-dir-with-kerberos-with-libdirlib64-with-libxml-dir-with-mysqli-with-openssl-with-pcre-regex-with-pdo-mysql-with-pdo-sqlite-with-pear-with-png-dir-with-jpeg-

9、dir-with-xmlrpc-with-xsl-with-zlib-with-bz2-with-mhash-enable-fpm-enable-bcmath-enable-libxml-enable-inline-optimization-enable-gd-native-ttf-enable-mbregex-enable-mbstring-enable-opcache-enable-pcntl-enable-shmop-enable-soap-enable-sockets-enable-sysvsem-enable-sysvshm-enable-xml-enable-zip-with-ld

10、apphp-version中看到DEBUG就可以了yongkbmaster/data/env/runtime/php-7.1.33-debug/bin/php-versionPHP7.1.33(cli)(built:Dec29202019:16:50)(NTSDEBUG)Copyright(c)1997-2018ThePHPGroupZendEnginev3.1.0,Copyright(c)1998-2018ZendTechnologie注意Debug版本的扩展需要再次编译安装不能拷贝正常版本的so安装方式以及普通扩展一致一般不需要开额外的debug参数。假如你需要调试扩展比方swoole你需

11、要设置扩展的debug参数可以参考扩展的./configure文件讲明。gdb使用这里简单介绍一下gdb的根本使用更详细的使用方法可以自行google。启动gdb捕获进程gdb-ppidrun方法启动gdbphpruntest3.php通过core文件gdb-ccore.8451断点breakn:在第n行处设置断点可以带上代码途径以及代码名称/注意这里只能断点c代码php文件不行的var.c:201在php-7.1.33是var_dump的入口breakvar.c:201bfn1ifab条件断点设置breakfuncbreak缩写为b在函数func()的入口处设置断点/大局部php的方法在c层

12、面的方法名都是zif_php方法名。例如var_dump在c的方法名叫zif_var_dumpbreakzif_var_dumpdelete断点号n删除第n个断点disable断点号n暂停第n个断点enable断点号n开启第n个断点clear行号n去除第n行的断点infobinfobreakpoints显示当前程序的断点设置情况deletebreakpoints去除所有断点其他list简写l其作用就是列出程序的源代码默认每次显示10行。list行号将显示当前文件以“行号为中心的前后10行代码printa将显示a的值continue简写c继续执行到下一个断点处或者运行完毕。设置断点后需要按这个。

13、next简写n当前函数下一行step简写s跳入函数内部where/bt当前运行的堆栈列表phpgdb小工具这里就是本文的重点了php为gdb提供了一组小工具在源代码目录下的.gdbinit文件中它可以帮助我们更好的gdbphp源代码。准备为了更好的演示我这里准备一个php文件。?phpconstAtestconstconstBtestconstBclassBpublic$atestpublicfunctionfunB()var_dump(testfunBclassCextendsBpublicfunctionfunC()var_dump(testfunC$atest$ba11,a2$cnewB

14、();$dA,B;$enewC();$f$b;var_dump($a,$b,$c,$d,$e,$f);get_object_vars($e);启动gdb设置2个断点。gdbphp/注意这里要用debug版本的(gdb)breakvar.c:211Breakpoint1at0x76e717:file/data/env/runtime/php-7.1.33-src/ext/standard/var.c,line211.(gdb)breakzend_object_handlers.c:492Breakpoint2at0x86ce9d:file/data/env/runtime/php-7.1.33-

15、src/Zend/zend_object_handlers.c,line492.(gdb)rtest4.php然后载入小工具source/data/env/runtime/php-7.1.33-src/.gdbinit使用zbacktrace显示当前的php调用栈(gdb)zbacktrace0x7ffff1614200var_dump(test,array(2)0x7ffff1614260,object0x7ffff1614270,array(2)0x7ffff1614280,object0x7ffff1614290,array(2)0x7ffff16142a0)internalfuncti

16、on0x7ffff1614030(main)/root/test4.php:26dump_bt查看当前调用栈以及zbacktrace类似(gdb)dump_btexecutor_globals.current_execute_data0x7ffff1614200var_dump(test,array(2)0x7ffff1614260,object0x7ffff1614270,array(2)0x7ffff1614280,object0x7ffff1614290,array(2)0x7ffff16142a0)internalfunction0x7ffff1614030(main)/root/te

17、st4.php:26printzv输出zendvalue的情况(gdb)printzvargs00x7ffff1614250(refcount0)string:testprint_global_vars输出全局变量(gdb)print_global_varsHash(13)0x11bf0d0:0_GET0x7ffff1657100(refcount2)array:1_POST0x7ffff1657120(refcount2)array:2_COOKIE0x7ffff1657140(refcount2)array:3_FILES0x7ffff1657160(refcount2)array:4ar

18、gv0x7ffff1657180(refcount2)array:5argc0x7ffff16571a0long:16_SERVER0x7ffff16571c0(refcount2)array:7a0x7ffff16571e0indirect:0x7ffff1613080(refcount0)string:test8b0x7ffff1657200indirect:0x7ffff1613090(refcount5)array:9c0x7ffff1657220indirect:0x7ffff16130a0(refcount2)object(B)#210d0x7ffff1657240indirect

19、:0x7ffff16130b0(refcount2)array:11e0x7ffff1657260indirect:0x7ffff16130c0(refcount2)object(C)#312f0x7ffff1657280indirect:0x7ffff16130d0(refcount5)array:print_const_table输出定义的常量(gdb)print_const_tableexecutor_globals.zend_constants0x14e8380Hash(2340)0x14e8380:0E_ERROR0x14fd660long:11E_RECOVERABLE_ERROR

20、0x14fe8a0long:40962E_WARNING0x14fe900long:23E_PARSE0x14fe960long:44E_NOTICE0x14fe9c0long:85E_STRICT0x14fea20long:20486E_DEPRECATED0x14fea80long:81927E_CORE_ERROR0x14feae0long:168E_CORE_WARNING0x14feb40long:329E_COMPILE_ERROR0x14feba0long:6410E_COMPILE_WARNING0x14fec10long:12811E_USER_ERROR0x14fec70l

21、ong:25612E_USER_WARNING0x14fecd0long:51213E_USER_NOTICE0x14fed30long:102414E_USER_DEPRECATED0x14feda0long:1638415E_ALL0x14fee00long:3276716DEBUG_BACKTRACE_PROVIDE_OBJECT0x14fee70long:117DEBUG_BACKTRACE_IGNORE_ARGS0x14feee0long:218true0x14fef70bool:true19false0x14ff000bool:false20ZEND_THREAD_SAFE0x14

22、ff070bool:false21ZEND_DEBUG_BUILD0x14ff0e0bool:true22null0x14ff170NULL23PHP_VERSION0x1500380(refcount1)string:7.1.33.print_zstr输出zendstring(gdb)print_zstrargs0string(4)test(gdb)print_zstrargs02string(4)te.(gdb)print_zstrargs04string(4)testprint_cvs打印已编译的变量及其值它需要传入一个zend_execute_data类型的值。可以先bt看一下调用栈。

23、(gdb)bt/这里看到#2层这里是zend_vm_execute的执行入口这里有zend_execute_data类型的值。#0zif_var_dump(execute_data0x7ffff1614120,return_value0x7fffffffa9b0)at/data/env/runtime/php-7.1.33-src/ext/standard/var.c:209#10x0000000000ab08d4inZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER()at/data/env/runtime/php-7.1.33-src/Zend/zend_vm

24、_execute.h:628#20x0000000000ab01c3inexecute_ex(ex0x7ffff1614030)at/data/env/runtime/php-7.1.33-src/Zend/zend_vm_execute.h:429#30x0000000000ab02d5inzend_execute(op_array0x7ffff1672d00,return_value0x0)at/data/env/runtime/php-7.1.33-src/Zend/zend_vm_execute.h:474#40x0000000000a510f9inzend_execute_scrip

25、ts(type8,retval0x0,file_count3)at/data/env/runtime/php-7.1.33-src/Zend/zend.c:1482#50x00000000009c02f4inphp_execute_script(primary_file0x7fffffffdf30)at/data/env/runtime/php-7.1.33-src/main/main.c:2577#60x0000000000b31387indo_cli(argc2,argv0x14e7f30)at/data/env/runtime/php-7.1.33-src/sapi/cli/php_cl

26、i.c:993#70x0000000000b32346inmain(argc2,argv0x14e7f30)at/data/env/runtime/php-7.1.33-src/sapi/cli/php_cli.c:1381(gdb)f2/跳到#2这一层#20x0000000000ab01c3inexecute_ex(ex0x7ffff1614030)at/data/env/runtime/php-7.1.33-src/Zend/zend_vm_execute.h:429429(opcode_handler_t)OPLINE-handler)(ZEND_OPCODE_HANDLER_ARGS_

27、PASSTHRU);(gdb)print_cvsex/输出Compiledvariablescount:60a0x7ffff1614080(refcount0)string:test1b0x7ffff1614090(refcount5)array:Hash(2)0x7ffff170e300:0a10x7ffff1793e20long:11a20x7ffff1793e40long:22c0x7ffff16140a0(refcount2)object(B)#2PropertiesHash(1)0x7ffff170e480:0a0x7ffff1793f60indirect:0x7ffff170e38

28、8(refcount4)string:test3d0x7ffff16140b0(refcount2)array:Packed(2)0x7ffff170e3c0:000x7ffff1793688(refcount1)string:testconst110x7ffff17936a8(refcount1)string:testconstB4e0x7ffff16140c0(refcount2)object(C)#3PropertiesHash(1)0x7ffff170e4e0:0a0x7ffff17940a0indirect:0x7ffff170e448(refcount4)string:test5f

29、0x7ffff16140d0(refcount5)array:Hash(2)0x7ffff170e300:0a10x7ffff1793e20long:11a20x7ffff1793e40long:2print_ht输出HashTable,HashTable是php底层一个重要的数据构造是phparray的实现方式你可以理解为是C层面的phparray在php源码中也大量使用HashTable存储各类kv构造或者数组构造。(gdb)print_htargs1.valueHash(2)0x7ffff170e300:0a10x7ffff1793e20long:11a20x7ffff1793e40lo

30、ng:2(gdb)print_htargs3.valuePacked(2)0x7ffff170e3c0:000x7ffff1793688(refcount1)string:testconst110x7ffff17936a8(refcount1)string:testconstBprint_htptr以及print_ht类似它输出的是zval的地址不是zval的值(gdb)print_htptrargs1.valueHash(2)0x7ffff170e300:0a10x7ffff1793e201a20x7ffff1793e40print_htstr以及print_ht类似只是HashTable中

31、存的不是zval而是cchar但是这种情况在源码中好似很少见了大局部存字符串的情况会直接用zendstring我找了一圈在php_cli_server_mime_type_ctor有一处使用(gdb)print_htstrserver-extension_mime_typesHash(2)0x11b9228:0ezapplication/andrew-inset1awapplication/applixwareprint_ft以及print_ht类似只是HashTable中存的是zend_function的地址(gdb)print_ftargs2.value.obj.ce.function_t

32、ableHash(1)0x7ffff1783210:0funbfunBprint_inh输出class相关信息(gdb)print_inhargs4.value.obj.ceclassCextendsBclassBprint_pi输出对象中属性相关信息它需要传入一个zend_property_info类型的地址在zend_object_handlers.c:492中有使用php中可以用get_object_vars($e)触发。(gdb)cContinuing.Breakpoint2,zend_check_property_access(zobj0x7ffff170e420,prop_info_name0x7ffff173c2c0)at/data/env/runtime/php-7.1.33-src/Zend/zend_object_handlers.c:492492zend_string_release(member);(gdb)print_piproperty_info0x7ffff17833c8offset0x28ce0x7ffff17831d0Bflags0x100(ZEND_ACC_PUBLIC)namestring(1)adefaultvalue:0x7ffff17690c0(refcount4)string:test视频号最新视频腾讯技术工程

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 工程图纸

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁