PHPDBG, a powerful PHP debugging tool, and phpdbg_PHP tutorials

Source: Internet
Author: User
Tags php server php debugger sapi
PHPDBG, the powerful tool for PHP debugging, and phpdbg. PHPDBG is a powerful PHP debugging tool. phpdbgPHPDBG is a php sapi module that can control the PHP runtime environment without modifying code or affecting performance. PHPDBG, PHPDBG, a powerful PHP debugging tool

PHPDBG is a php sapi module that can control the PHP runtime environment without modifying code or affecting performance.

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.

#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
1. 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

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

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

The source code of 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 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:

#phpdeg[Welcome to phpdbg, the interactive PHP debugger, v0.4.0]To get help using phpdbg type "help" and press enter[Please report bugs to 
 

To load the php script to be debugged, 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 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.

......prompt> helpphpdbg is a lightweight, powerful and easy to use debugging platform for PHP5.4+It supports the following commands:Information list   list PHP source......

5. 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.

......prompt> b func#2prompt> rdemo::__construct:5method 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 $param\n";; 00020: }......

6. View breakpoints

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

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

7. delete a breakpoint

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

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

The number 1 after the break del is the breakpoint number.

8. view the 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:

......prompt> l f func 00017:   $param = "ali"; 00018:   $param = $param + "baba"; 00019:   echo "function func $param\n";; 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.

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

......prompt> ev $var = "val";valprompt> ev var_dump($var);string(3) "val"......

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

The above is all the content in this article. it is easy to play with the debugging tool PHPDBG, and I hope you will like it.

Articles you may be interested in:
  • Summary of debugging technology for PHP programmers
  • Using NetBeans + Xdebug to Debug PHP programs
  • FirePHP recommends a PHP debugging tool.
  • PHP debugging tool Debug Tools
  • Php performance optimization analysis tool XDebug large website debugging tool
  • Resolve phpstorm + xdebug remote breakpoint debugging
  • How to Debug PHP using FireFox plug-in FirePHP
  • Use Zend Studio + PHPnow + Zend Debugger to build a PHP server debugging environment
  • Phpstorm configuration Xdebug debugging PHP Tutorial
  • Solve ThinkPHP errors reported when the debug mode is disabled.

PHPDBG is a php sapi module that can control the PHP runtime environment without modifying code or affecting performance. PHPDBG...

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.