Learning notes for debugging PHP programs using xdebug-PHP source code

Source: Internet
Author: User
Tags zts
Xdebug is a tool commonly used to debug the performance of php programs. The following article describes how to install and configure xdebug in the lnmp System and the final simple debugging method, there are also some solutions to problems encountered during the debugging process. Xdebug is a tool commonly used to debug the performance of php programs. The following article describes how to install and configure xdebug in the lnmp System and the final simple debugging method, there are also some solutions to problems encountered during the debugging process.

Script ec (2); script

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:
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:

; 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"

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
Label to add color to the variable.
Example:

The Code is as follows:
$ 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:

Echo xdebug_time_index ();
Sleep (3); echo"
";
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:

Echo"
";
Echo xdebug_memory_usage ();
Echo"
";
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:
[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 (http://sourceforge.net/projects/wincachegrind/) to open these files.
You can visually and beautifully display the content:

The Code is as follows:

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:

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.