简体   繁体   English

在Ubuntu调试PHP中使用Xdebug2的分析数据

[英]To use Xdebug2's profiling data in debugging PHP by Ubuntu

My question is based on this article . 我的问题是基于这篇文章

How can you make the profiling data about a PHP code by Xdubug2 and then put it to an app like KCacheGrind? 如何通过Xdubug2创建有关PHP代码的分析数据,然后将其放入诸如KCacheGrind的应用程序中?

I have used successfully Xdebug in my Ubuntu, since it highlights my error messages in the browser. 我已经在Ubuntu中成功使用Xdebug,因为它在浏览器中突出显示了我的错误消息。 However, I have not find any terminal tool like xdebug . 但是,我没有找到任何终端工具,例如xdebug

I would like to have a visual view of my PHP code like this 我想以这种方式查看我的PHP代码

替代文字

Add this line to your php.ini 将此行添加到您的php.ini中

xdebug.profiler_enable = 1 xdebug.profiler_enable = 1

Then, if you happen to run PHP through a webserver you need to restart the webserver, otherwise the conf change is not picked up. 然后,如果您恰巧通过Web服务器运行PHP,则需要重新启动Web服务器,否则将不会获取conf更改。

If you are running PHP through cli, then of course no restart is necessary. 如果您通过cli运行PHP,则当然无需重新启动。

Now, when you run your PHP script, a cachegrind.out.PID file is created in the directory specified by xdebug.profiler_output_dir php.ini setting. 现在,当您运行PHP脚本时,将在xdebug.profiler_output_dir php.ini设置指定的目录中创建一个cachegrind.out.PID文件。 It is /tmp by default. 默认为/ tmp。

That files is the one kcachegrind is able to load. 该文件是kcachegrind能够加载的文件。

There are other means to invoke this profile generation, you can read about them at http://www.xdebug.org/docs/all_settings#profiler_enable 还有其他方法可以调用此配置文件生成,您可以在http://www.xdebug.org/docs/all_settings#profiler_enable中了解它们

看看WebGrind( http://code.google.com/p/webgrind/

I ran into a similar situation where I only had access to a terminal and not visual environment on which to test. 我遇到了类似的情况,我只能访问终端,而不能在可视环境中进行测试。 Even worse, I was using a windows machine and Putty. 更糟糕的是,我正在使用Windows机器和Putty。

The solutions available are 可用的解决方案是

  1. Installing WebGrind ( http://code.google.com/p/webgrind/ ) 安装WebGrind( http://code.google.com/p/webgrind/
  2. Running Xdebug and copying the cachegrind.out files onto a machine where you have a cachegrind viewer. 运行Xdebug并将cachegrind.out文件复制到具有cachegrind查看器的计算机上。

For me the answer was to SCP the cachegrind files onto my local windows machine, and using WinCachegrind to look at them. 对我来说,答案是将cachegrind文件SCP放在我的本地Windows机器上,然后使用WinCachegrind查看它们。 You could SCP them onto your linux box and run KCacheGrind on the files. 您可以将它们SCP放入Linux盒子,然后在文件上运行KCacheGrind。 The downside to this, is that you may not have the same file structure, so you won't be able to view the sourcecode. 不利的一面是,您可能没有相同的文件结构,因此您将无法查看源代码。 If you have the source also on your local machine, or can get it there, you can fix this too. 如果您的本地计算机上也有该源,或者可以在该计算机上找到源,则也可以修复此源。 Open up the cachegrind files in vim (or other editor) and do a global search and replace on the paths to change them to the correct source path on your local machine. 在vim(或其他编辑器)中打开cachegrind文件,然后进行全局搜索并替换路径,以将其更改为本地计算机上的正确源路径。

I hope this is what you were looking for. 我希望这是您想要的。

EDIT to address comment: 编辑以解决评论:

If you are working to get a callgrind file that is somewhat different. 如果您要获取的Callgrind文件有所不同。 For this, you need to be running in Linux (which I think you are) and have the callgrind and valgrind programs available. 为此,您需要在Linux(我认为是)中运行,并具有callgrind和valgrind程序。 The last thing to assume here is that you are running PHP as an Apache mod and not in some other fashion. 这里要假定的最后一件事是您将PHP作为Apache mod运行,而不是以其他方式运行。 Use the callgrind tool against the starting of apache and then run the request in the browser. 针对Apache的启动使用callgrind工具,然后在浏览器中运行请求。 This will give you detailed information not only on the php call tree, but also on many things in Apache that may be causing trouble. 这不仅会为您提供有关php调用树的详细信息,而且还会为您提供有关Apache中可能引起麻烦的许多内容的详细信息。

here is an example of the 这是一个例子

sudo callgrind --dump-instr=yes --trace-jump=yes -v /usr/sbin/httpd -X

The -X will start apache in debug mode with only one thread. -X将仅以一个线程在调试模式下启动apache。 From here open a web browser and hit the php script you want. 从这里打开网络浏览器,然后单击所需的php脚本。 Then go back and shutdown apache. 然后返回并关闭apache。 This should also end the callgrind parse. 这也应该结束callgrind解析。

If you do not need apache or a web browser, you can try running callgrind with just the php command 如果您不需要apache或Web浏览器,则可以尝试仅使用php命令运行callgrind

sudo callgrind --dump-instr=yes --trace-jump=yes -v /usr/sbin/php my_php_script.php

That should give you the same results but without all the apache stuff. 那应该给你相同的结果,但是没有所有的apache东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM