Php debugging tool-phpdbg installation configuration-PHP source code

Source: Internet
Author: User
Tags php debugger sapi
The following is a compilation of tutorials to explain how to install and configure phpdbg, a powerful php debugging tool. PHPDBG is a php sapi module, you can control the PHP runtime environment without modifying the code or affecting the performance. Let's take a look. The following is a compilation of tutorials to explain how to install and configure phpdbg, a powerful php debugging tool. PHPDBG is a php sapi module, you can control the PHP runtime environment without modifying the code or affecting the performance. Let's take a look.

Script ec (2); script

PHPDBG aims to become a lightweight, powerful, and easy-to-use PHP debugging platform. It can be used in PHP5.4 or later versions. Php5.6 and later versions are integrated internally.

Main functions:

-Single-step debugging
-Flexible breakpoint methods (class methods, functions, files: rows, memory addresses, opcode)
-Php eval can be directly called.
-View the code currently executed
-User space API (userland/user space)
-Easy integration
-Support specifying the php configuration file
-JIT global variable
-Readline support (optional) for convenient terminal operations
-Remote debug, using java GUI
-Easy to operate (see help for details)

Install

To use phpdgb, you must first download a php source package. Download the phpdgb source code package and place it in the sapi directory of the php source code package. Finally, you can run the command to install it. An example of compilation and installation is as follows:
Suppose we have downloaded the php source code package and placed it in the/home/php Directory.

The Code is as follows:
# Cd/home/php/sapi
# Git clone https://github.com/krakjoe/phpdbg
# Cd ../
#./Buildconf -- force
#./Config. nice
# Make-j8
# Make install-phpdbg

Note:
1. If your php version is php5.6 or later, phpdbg has been integrated into the php code package and does not need to be downloaded separately.
2. Add-enable-phpdbg to the compilation parameters.
3. During compilation,-with-readline can be added selectively. If not added, phpdbg's history and other functions cannot be used.

Basic usage

Parameter Introduction

Phpdbg is an sapi of php. It can debug php using command lines. Common parameters are as follows:
The following switches are implemented (just like cli SAPI ):
-N ignore php ini
-C search for php ini in path
-Z load zend extension
-D define php ini entry
The following switches change the default behaviour of phpdbg:
-V disables quietness
-S enabled stepping
-E sets execution context
-B boring-disables use of color on the console
-I ignore. phpdbginit (default init file)
-I override. phpgdbinit location (implies-I)
-O set oplog output file
-Q do not print banner on startup
-R jump straight to run
-E enable step through eval ()
Note: passing-rr will cause phpdbg to quit after execution, rather than returning to the console

Common functions

We have previously introduced the gdb tool. In fact, phpdbg and gdb functions are very similar in some places. For example, you can set a breakpoint, or perform it in one step. But their debugging languages are different. gdb focuses on debugging c or c ++, while phpdbg focuses on debugging php. Below we will introduce some common debugging functions of phpdbg.
The code to be debugged is as follows:
The source code of the file test_phpdbg_inc.php is as follows:

The Code is as follows:

The source code of the file test_phpdgb.php is as follows:

The Code is as follows:
Func (1 );
Func ();
Phpdbg_inc_func ();
?>

Start phpdbg

After phpdbg is successfully installed, it will be in the bin directory of the installation directory. Enter the bin directory and directly enter phpdbg. As follows:

The Code is as follows:
# Phpdeg
[Welcome to phpdbg, the interactive PHP debugger, v0.4.0]
To get help using phpdbg type "help" and press enter
[Please report bugs ]
Prompt>

To load the php script to be debugged, you only need to execute the exec command. As follows:

The Code is as follows:
# Phpdbg
......
Prompt> exec./test_phpdbg.php

Of course, we can also specify the e parameter when starting phpdbg. As follows:

The Code is as follows:
# Phpdbg-e./test_phpdbg.php

View help information

If you have used other debugging tools before, you will find that phpdbg is similar to them. However, at the initial stage, you still need to obtain help information frequently. You can obtain help information through the help Command.

The Code is as follows:
......
Prompt> help

Phpdbg is a lightweight, powerful and easy to use debugging platform for PHP5.4 +
It supports the following commands:

Information
List PHP source
......

Set breakpoints
The command for setting breakpoints is the same as that for gdb. Both are break, which is abbreviated as B. However, the specific command parameters are different. Similar to the gdb breakpoint command, you can set breakpoints by file name: line number or line number. In addition, phpdbg provides some methods to set breakpoints that are unique to php. For example, set a breakpoint Based on opline and a breakpoint Based on opcode.
As we all know, php code is eventually parsed into opcode, which is then executed by the php kernel. A php statement may be parsed into multiple Opcodes. If you can set a breakpoint according to opcode, we can more accurately track the program execution process. Next, let's take a look at the specific example of how phapdbg sets breakpoints.
Set breakpoints by opline:
The opline mentioned here is the row number of the current Code starting from the method entry. For example, in the test_phpdgb.php file, the opline of Line 1 code "$ param = $ param +" baba ";" is 2.

The Code is as follows:
......
Prompt> B func #2
Prompt> r
Demo :__ construct: 5
Method func 2
[Breakpoint #0 resolved at func #2 (opline 0x7f5b230a2e38)]
[Breakpoint #0 resolved at func #2 (opline 0x7f5b230a2e38)]
[Breakpoint #0 resolved at func #2 (opline 0x7f5b230a2e38)]
[Breakpoint #0 in func () #2 at./test_phpdbg.php: 18, hits: 1]
> 00018: $ param = $ param + "baba ";
00019: echo "function func $ paramn ";;
00020 :}

......
View breakpoints
Like gdb, phpdbg uses the info break command to view breakpoints. Example:

....

The Code is as follows:
Prompt> info break
------------------------------------------------
File Breakpoints:
#1/home/hailong. xhl/test_phpdbg.php: 10
------------------------------------------------
Opline Breakpoints:
#0 7ff3219e1df0 (function breakpoint)
------------------------------------------------
Function opline Breakpoints:
#0 func opline 2

....
As shown above, we can know. The breakpoint type is also displayed in the display result of info break. # The following number is the breakpoint number. You can delete a breakpoint Based on the breakpoint number.

Delete breakpoint

Different from the gdb command. The delete breakpoint of phpdbg is not the delete command, but the break del command. Example:

The Code is as follows:
......
Prompt> break del 1
[Deleted breakpoint #1]
Prompt>

......
The number 1 after the break del is the breakpoint number.
View code
Phpdbg command to view code is also list. However, compared with gdb, it can be used in a variety of ways.
Display the code of the specified function:

The Code is as follows:

......
Prompt> l f func
00017: $ param = "ali ";
00018: $ param = $ param + "baba ";
00019: echo "function func $ paramn ";;
00020 :}
00021:
Prompt>

......
One-step execution
Phpdbg has only one command step for single-step execution. It is similar to the step command of gdb. Is a line of Execution Code. Note that phpdbg does not have the next command.

....

The Code is as follows:
Prompt> s
[Breakpoint #0 resolved at func #2 (opline 0x152ba40)]
[L19 0x152ba70 ZEND_ADD_STRING C2 @ 0./test_phpdbg.php]
> 00019: echo "function func $ paramn ";;
00020 :}
00021:

....
Continue execution
Like gdb, the command to continue executing phpdbg is also continue, abbreviated as c.
Execute php code
This is a feature of phpdbg. You can use the ev command to execute arbitrary php code during debugging. For example:

The Code is as follows:
......
Prompt> ev $ var = "val ";
Val
Prompt> ev var_dump ($ var );
String (3) "val"

......
You can modify the variable value dynamically during debugging to view the execution result.

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.