本篇将会讲解如何使用xdebug进行断点调试。
xdebug是什么?是一个用来调试PHP的PHP扩展。既然是扩展,就应该有安装扩展的套路。在Windows下,打开pecl.php.net,找到你的PHP对应的dll文件下载解压,丢到ext目录,修改php.ini文件,重启nginx,搞定。
在Linux系统上,更常用的方式是源码编译,打开pecl.php.net,下载你喜欢的xdebug版本,解压,phpize, ./configure --with-php-config ,make, sudo make install,修改php.ini文件,重启nginx,搞定。
如果安装了pecl命令,还是可以使用pecl install xdebug进行安装。
最后运行下边命令监测xdebug是否安装成功
php --ri xdebug
输出如下信息,表示xdebug安装成功。
__ __ _ _ \ \ / / | | | | \ V / __| | ___| |__ _ _ __ _ > < / _` |/ _ \ '_ \| | | |/ _` | / . \ (_| | __/ |_) | |_| | (_| | /_/ \_\__,_|\___|_.__/ \__,_|\__, | __/ | |___/ Version => 3.1.6 Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support Enabled Features (through 'xdebug.mode' setting) Feature => Enabled/Disabled Development Helpers => ✔ enabled Coverage => ✘ disabled GC Stats => ✘ disabled Profiler => ✘ disabled Step Debugger => ✔ enabled Tracing => ✘ disabled Optional Features Compressed File Support => yes (gzip) Clock Source => clock_gettime Debugger => enabled IDE Key => Directive => Local Value => Master Value xdebug.mode => develop,debug => develop,debug xdebug.start_with_request => default => default xdebug.start_upon_error => default => default xdebug.output_dir => /tmp => /tmp xdebug.use_compression => 1 => 1 xdebug.trigger_value => no value => no value xdebug.file_link_format => no value => no value xdebug.filename_format => no value => no value xdebug.log => /tmp/xdebug.log => /tmp/xdebug.log xdebug.log_level => 7 => 7 xdebug.var_display_max_children => 128 => 128 xdebug.var_display_max_data => 512 => 512 xdebug.var_display_max_depth => 3 => 3 xdebug.max_nesting_level => 256 => 256 xdebug.cli_color => 0 => 0 xdebug.force_display_errors => Off => Off xdebug.force_error_reporting => 0 => 0 xdebug.halt_level => 0 => 0 xdebug.max_stack_frames => -1 => -1 xdebug.show_error_trace => Off => Off xdebug.show_exception_trace => Off => Off xdebug.show_local_vars => Off => Off xdebug.dump.COOKIE => no value => no value xdebug.dump.ENV => no value => no value xdebug.dump.FILES => no value => no value xdebug.dump.GET => no value => no value xdebug.dump.POST => no value => no value xdebug.dump.REQUEST => no value => no value xdebug.dump.SERVER => no value => no value xdebug.dump.SESSION => no value => no value xdebug.dump_globals => On => On xdebug.dump_once => On => On xdebug.dump_undefined => Off => Off xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p xdebug.profiler_append => Off => Off xdebug.cloud_id => no value => no value xdebug.client_host => 127.0.0.1 => 127.0.0.1 xdebug.client_port => 9999 => 9999 xdebug.discover_client_host => Off => Off xdebug.client_discovery_header => no value => no value xdebug.idekey => no value => no value xdebug.connect_timeout_ms => 200 => 200 xdebug.scream => Off => Off xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p xdebug.trace_output_name => trace.%c => trace.%c xdebug.trace_format => 0 => 0 xdebug.trace_options => 0 => 0 xdebug.collect_assignments => On => On xdebug.collect_return => Off => Off xdebug.auto_trace => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.collect_includes => (setting removed in Xdebug 3) => (setting removed in Xdebug 3) xdebug.collect_params => (setting removed in Xdebug 3) => (setting removed in Xdebug 3) xdebug.collect_vars => (setting removed in Xdebug 3) => (setting removed in Xdebug 3) xdebug.coverage_enable => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.default_enable => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.gc_stats_enable => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.gc_stats_output_dir => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.overload_var_dump => (setting removed in Xdebug 3) => (setting removed in Xdebug 3) xdebug.profiler_enable => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.profiler_enable_trigger => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.profiler_enable_trigger_value => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.profiler_output_dir => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_autostart => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_connect_back => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_enable => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_host => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_log => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_log_level => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_mode => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_port => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.remote_timeout => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.show_mem_delta => (setting removed in Xdebug 3) => (setting removed in Xdebug 3) xdebug.trace_output_dir => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.trace_enable_trigger => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3) xdebug.trace_enable_trigger_value => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3)
结合上边的输出,解释几个比较重要的配置
1 xdebug.mode mode用来控制xdebug是否开启以及开启多少功能
off 关闭
develop 这个功能,增强var_dump函数的功能,代码追踪时展示更多的信息,展示脚本运行时用到的函数的更多信息。
coverage 代码覆盖率,配合phpunit使用
debug 开启断点调试。没有这个功能,断点调试就无法成功。
gcstats 开启垃圾回收统计
profile 开启性能分析,配合KCacheGrind使用。
trace 把代码运行的堆栈信息保存到指定的文件中。
上边的功能,可以多个一起使用,比如xdebug.mode=develop,trace。根据需要自行组合。
我没有试过off跟其他配置一起使用,动手能力强的你可以试一下。
2. xdebug.start_with_request
可以设置的值有yes,no,trigger,default。
推荐你使用yes,你都安装了xdebug,还不设置为yes吗?
聪明的小伙伴会觉得设置为yes,那岂不是每次请求都会被IDE拦截。会,也不会。后边实战再仔细聊。
3 xdebug.client_host xdebug.client_port
配置调试器所在的ip地址和端口。就是你的ide会开启哪个监听端口给xdebug进行连接并进行断点调试的通信。
一般都是127.0.0.1,端口就不要用9000,因为php-fpm master进程默认监听的就是9000端口。爱抬杠的小伙伴会说,我用的是socket。好吧,当我没说。我一般都是设置为9999,不要问为什么,问就是连续四个9,肯定比输入其他端口方便。就是喜欢,就是懒。。。
主要是这四个,当然还有一些其他的重要配置,为了快速实现断点调试,先介绍到这里。
总结一下,常用的xdebug配置,想偷懒直接拿走。
[xdebug] zend_extension=xdebug xdebug.mode=develop,debug xdebug.output_dir=/tmp xdebug.log=/tmp/xdebug.log xdebug.log_level=7 xdebug.client_host=127.0.0.1 xdebug.client_port=9999 xdebug.collect_assignments=On
如果你不知道PHPstorm是什么,它就是一个IDE。
虽说PHP是世界上最好的语言成了笑谈,但是PHPstorm那一大堆兄弟(GoLand,CLion,WebStorm,IDEA),绝对是世界上最好用的IDE。平日里用惯了PHPstorm,切换到其他IDE,感觉都不会写代码了。。。
跑题了,言归正传。首先要确保你安装了PHPstorm。然后安装了xdebug,并且做好了配置。
接下来打开PHPstorm的配置,找到php,设置好php的语言等级、和php 解析器所在的位置。
然后找到debug,设置端口9999,保存。打开一个PHP页面,刷新。应该就可以进入调试了。
如果没有,请看下边截图。