The phpdbg_php skill of the powerful tool of PHP debugging

Source: Internet
Author: User
Tags eval php language php debugger php script php source code readline switches git clone

Phpdbg is a PHP SAPI module that allows you to control PHP's operating environment without modifying the code and without affecting performance.

Phpdbg's goal is to become a lightweight, powerful, easy-to-use PHP debugging platform. Can be used in PHP5.4 and above versions. The php5.6 and above versions are integrated internally.

Main function:

– Single-Step debugging

– Flexible way to lower breakpoints (class methods, functions, files: rows, memory addresses, opcode)

– Direct call to PHP's eval

– You can view the currently executing code

– User space APIs (userland/user spaces)

-Easy integration

– Support for specifying PHP configuration files

–jit Global Variables

–readline Support (optional), terminal operation more convenient

– Remote debug, using the Java GUI

-Easy to operate (see Help specifically)

Installation
To use PHPDGB, you first need to download a PHP source package. Then download the PHPDGB source package and place it in the SAPI directory of the PHP source package. Finally, you can perform the command installation. Compile the installation example as follows:

Let's say we've downloaded the PHP source package and placed it in the/home/php directory.

#cd/home/php/sapi
#git clone https://github.com/krakjoe/phpdbg
#cd. /
#./buildconf--force
#./config.nice
#make-j8
#make install-phpdbg

Attention:

1, if your PHP version is php5.6 or a higher version, PHPDBG has been integrated in the PHP code package, do not need to download separately.

2, compile the parameter to remember to add –enable-phpdbg.

3, compile-time parameters, –with-readline can be selectively added. Features such as phpdbg history are not available if they are not added.

Basic use
1. Parameter Introduction
Phpdbg is a SAPI of PHP that can debug PHP in a command-line fashion. 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 by colour on the console

-I ignore. Phpdbginit (default init file)

-I override Phpgdbinit location (implies-i)

-o set oplog output file

-Q Don't print banner on startup

-R Jump straight to run

-e Enable step through eval ()

Note:passing-rr'll cause phpdbg to quit after execution, rather than returning to the console

2. Common functions
We have introduced the GDB tool before. In fact, phpdbg and GDB functions are very similar in some places. For example, you can set breakpoints, you can step through, and so on. Just because they are debugging a different language, GDB focuses on debugging the C or C + + language, while phpdbg focuses on debugging the PHP language. Below we will introduce some common debugging functions of phpdbg. The code to debug is as follows:

The file test_phpdbg_inc.php source code is as follows:

<?php 
function Phpdbg_inc_func ()
{   
  echo "phpdbg_inc_func \ n"; 
} 
? >

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

<?php 
  include (DirName (__file__). " /test_phpdbg_inc.php "); 
  Class demo{public   
    function __construct () {
       echo __method__. ":". __line__. " \ n ";   
    }
    Public function func ($param) {
       $param + +;
       echo "method func $param \ n";
    }
    Public Function __destruct () {
       echo __method__. ":". __line__. " \ n ';
    }
  } 

 function func () {   
   $param = "Ali";
   $param = $param + "Baba";
   echo "function Func $param \ n";
 }

 $demo = new Demo ();
 $demo->func (1);
 Func ();
 Phpdbg_inc_func ();
? >

3, Start phpdbg

After the phpdbg installation is successful, it will be in the bin directory of the installation directory. Enter the bin directory, directly into the phpdbg can be. As follows:

#phpdeg
[Welcome to Phpdbg, the interactive PHP debugger, v0.4.0] to get help
using phpdbg type ' help ' and press Enter
[bugs to  
 

To load the PHP script you want to debug, you only need to execute the EXEC command. As follows:

#phpdbg ...
Prompt> exec./test_phpdbg.php

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

#phpdbg-E./test_phpdbg.php

4. View Help information

If you've used other debugging tools before, you'll find phpdbg similar to them. However, you will often need to get help information in the early stages of your use. The help command allows us to get helpful information.

......
Prompt> help

phpdbg are a lightweight, powerful and easy to use debugging platform for php5.4+
It supports the F ollowing commands:

information
 list   list PHP source
...

5. Set Breakpoints

The command to set breakpoints is the same as GDB. All are break, abbreviated form B. However, the specific command parameters are still different. As with GDB's breakpoint commands, they can set breakpoints by file name: line number or line number. In addition, PHPDBG provides some ways to set breakpoints specific to PHP. For example, set breakpoints according to Opline, set breakpoints according to OpCode, and so on.

As we all know, PHP code is ultimately resolved into opcode, and then by the PHP kernel to implement. A PHP statement that may be parsed into multiple opcode. If you can set breakpoints by pressing opcode, we can trace the program execution more precisely. Let's look at a concrete example of phapdbg setting breakpoints.

Press Opline to set a breakpoint:

The opline here is the line number of the current code with the method entry as the starting point. As in the test_phpdgb.php file, the 18th line of code "$param = $param +" Baba ";" The Opline is 2.

......
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 Fun C#2 (Opline 0x7f5b230a2e38)]
[Breakpoint #0 in Func () #2 at./test_phpdbg.php:18, Hits:1]
>00018:   $ param = $param + "Baba";
 00019:   echo "function func $param \ n";;
 00020:} ...

6. View Breakpoints

Like GDB, PHPDBG also uses the info Break command to view breakpoints. Examples are 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
....

Through the above display, we can know. The type of the breakpoint is also displayed in the display result of the info break. #后面的数字是断点号. We can delete breakpoints according to the breakpoint number.

7, remove the breakpoint

is not the same as the GDB command. The phpdbg deletion breakpoint is not the delete command, but the Break del command. Examples are as follows:

......
Prompt> break del 1
[Deleted breakpoint #1]
prompt>
...

The number 1 behind the Break del is the breakpoint number.

8, view the code

phpdbg the command to view the code is also the list. But compared to GDB, there are more ways to use it.

Displays the code for the specified function:

......
Prompt> L F func
 00017:   $param = "Ali";
 00018:   $param = $param + "Baba";
 00019:   echo "function func $param \ n";;
 00020:}
 00021:
prompt>
...

Single Step execution

phpdbg only one step execution. The same as GDB's step command. is a line of code execution. Note that phpdbg does not have the next command.

....
prompt> s
[breakpoint #0 resolved at Func#2 (Opline 0x152ba40)]
[L19     0x152ba70-zend_add_string C2   @0  ./test_phpdbg.php]
>00019:   echo "function func $param \ n";;
 00020:}
 00021:
.....

Continue to execute

Like GDB, Phpdbg's continued execution command 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. Such as:

......
prompt> ev $var = "Val";
Val
prompt> ev var_dump ($var);
String (3) "Val" ...

In this way, you can dynamically modify the value of the variable during debugging to see the execution effect.

Above is the entire content of this article, easy to play the debugging tool phpdbg, I hope you like.

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.