Study Notes for debugging PHP programs using xdebug

Source: Internet
Author: User
Tags memory usage php code zts blank page

Xdebug installation steps

Xdebug is a module of php and needs to be compiled and installed. php installed with lnmp is installed to/usr/local/php by default, and a hard link to/usr/bin is made.

Compile xdebug first

The code is as follows: Copy code
Wget http://www.xdebug.org/files/xdebug-2.2.3.tgz
Tar xzf xdebug-2.2.3.tgz
Cd xdebug-2.2.3
/Usr/bin/phpize
./Configure -- with-php-config =/usr/local/php/bin/php-config

Modify php. ini configuration

Add the following

The code is as follows: Copy code

; No-debug-non-zts-20090626 this folder name and php version are one-to-one correspondence
Zend_extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xdebug. so"
Xdebug. default_enable = On
Xdebug. show_exception_trace = On
Xdebug. show_local_vars = 1
Xdebug. max_nesting_level = 50
Xdebug. var_display_max_depth = 6

Xdebug. dump_once = On
Xdebug. dump_globals = On
Xdebug. dump_undefined = On
Xdebug. dump. REQUEST = *
Xdebug. cli_color = 2

Use Xdebug to make debugging information more beautiful
After the Xdebug extension is loaded, Xdebug will overwrite some of the original PHP functions for better debugging. For example, the var_dump () function,
We usually need to add "<pre>... </Pre> "to make the output variable information more beautiful and readable.
However, after Xdebug is loaded, you no longer need to do so. Xdebug not only automatically adds the <pre> label to the variable, but also adds color to the variable.
Example:

The code is as follows: Copy code
<? Php
$ ArrTest = array (
"Test" => "abc ",
"Test2" => "abc2"
);
Var_dump ($ arrTest );
?>


Execute the script using Xdebug
Xdebug_time_index () to display the time

The code is as follows: Copy code

Echo xdebug_time_index ();
Sleep (3); echo "<br> ";
Echo xdebug_time_index ();


Memory occupied by scripts
I want to know how much memory the program occupies when it is executed to a specific stage. For this reason, PHP provides the memory_get_usage () function ().
This function is valid only when the -- enable-memory-limit parameter is used during PHP compilation.
Xdebug also provides the xdebug_memory_usage () function to implement this function,
In addition, xdebug provides an xdebug_peak_memory_usage () function to view the peak memory usage.

The code is as follows: Copy code

Echo "<br> ";
Echo xdebug_memory_usage ();
Echo "<br> ";
Echo xdebug_peak_memory_usage ();


Detect deficiencies in the code
Sometimes the code has no obvious writing errors and no error information (such as error, warning, or notice) is displayed, but this does not indicate that the code is correct.
Sometimes it may take too long for a piece of code to be executed, and the memory usage is too large to affect the efficiency of the entire system. There is no way to see which part of the code is faulty.
At this time, we hope to monitor the running status of each stage of the code, write it to the log file, and analyze it after running for a period of time to locate the problem.
Edit the php. Ini file
Join

The code is as follows: Copy code
[Xdebug]
Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "I: Projectsxdebug"
Xdebug. profiler_output_dir = "I: Projectsxdebug"

These lines are used to write the execution analysis file to the "I: Projectsxdebug" directory (which can be replaced with any directory you want to set ).
If you open the corresponding directory after executing a program, you can find that a bunch of files are generated, such as files named in the format of cachegrind. out.1169585776.
These are the analysis files generated by Xdebug. You can view the details of a lot of program running in the editor, but it is obviously too tired to view them. You need to use graphical software to view them.
On Windows, you can use WinCacheGrind (Download address http://sourceforge.net/projects/wincachegrind/) to open these files.
You can visually and beautifully display the content:

The code is as follows: Copy code

TestXdebug ();
Function testXdebug (){
RequireFile ();
}

Function requireFile (){
Require_once ('ABC. Php ');
}

The index is displayed intuitively. php calls a function testXdebug (), and testXdebug () calls the requireFile () function. in this way, you can easily view the program structure of the entire script.

Restart php-fpm, write the wrong php code at will, and refresh the browser to see the error prompt.

An episode encountered during use

A: slow access response

B: access to ultra-fast response, but it is a blank page.

Problem A has many solutions, but there are still Solutions. Problem B is probably A thread problem. Maybe you can Debug php. the ini configuration may not be completed. It is difficult to clarify that the thread issue may be related to the xdebug version.

Solution


Find the memory_limit parameter in PHP. ini and increase it!

Change the browser! I used Google Chrome to debug the program, and then started to slow down until every page became loaded in 6 seconds. Firefox and IE do not solve this problem. (This method is easy to use and can take effect immediately !)

Use the xdebug. profiler_enable_trigger configuration to run the xdebug function probe program.

The value of xdebug. remote_host should be the same as the IP address of your server. For example, if you access it through localhost, write localhost here. If you access it through 127.0.0.1, write it to 127.0.0.1.

Check whether your xdebug. profiler_output_dir Directory has reached several GB? (After a set of e-commerce programs are continuously developed for more than 10 hours, the xdebug file in the xdebug. profiler_output_dir directory reaches several GB !)

Close xdebug when you do not need it!

The code is as follows: Copy code

Xdebug. remote_enable = 0
Xdebug. profiler_enable = 0
Xdebug. remote_autostart = false

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.