PHPDBG, a powerful tool for PHP Debugging

Source: Internet
Author: User
Tags spl php debugger sapi
Powerful PHP debugging tool phpdbg 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:

 



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

 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.

Additional reading

PHP advanced Advanced Knowledge Series technical articles

PHPDBG, a powerful tool for 1PHP debugging

2PHP Performance Analysis (1): XHProf & XHGui introduction

3. write PHP now. you should know this.

FastCGI and mod_php in 4PHP

5PHP performance analysis and experiment: Micro-performance analysis

6PHP configuration file php. ini (full)

7PHP command line command User Guide

8. how to hide the PHP version number on a Linux server

9PHP SOCKET programming

Summary of socket series functions in 10php

11 correct PHP match UTF-8 Chinese regular expression

Tutorial on installing and using 12PHP performance analysis tool XHProf

13 Baidu engineers explain the implementation principle and performance analysis of PHP functions (3)

14 Baidu engineers explain the implementation principle and performance analysis of PHP functions (2)

15 Baidu engineers explain the implementation principle and performance analysis of PHP functions (I)

16php file system permission issues and solutions when running in fastCGI mode

Streams in 17PHP

SplQueue and SplPriorityQueue of the 18 php spl standard library)

19xss attack knowledge

Data structure dual-chain table (spldoubly1_list) in the 20PHP SPL standard library)

Data structure fixed-length array (SplFixedArray) in 21PHP SPL standard library)

22php uses reflection to implement plug-in mechanism

How to compress and decompress strings in 23php

24PHP built-in Session risks (blocking caused by session file exclusive lock)

Difference between 25 cookies and sessions-a good summary

26 11 PHP frameworks recommended to developers

27xhprof installation & use

28PHP multi-thread internal multi-thread instance analysis

29PHP uses QPM to implement multi-process parallel task processing program

30PHP multi-process processing parallel processing task instance

31PHP multi-thread programming-analysis of pipeline communication instances

32PHP daemon instance

33php solutions to memory insufficiency caused by querying a large volume of mysql data

34PHP exception handling

How to enable multi-process in 35php

36. how does HHVM improve PHP performance?

37 PHP developers should know about 24 awesome PHP libraries (microframeworks)

38php's solution to the iconv Chinese truncation problem

Detailed description of supporting PATH_INFO under 39nginx

Get_headers function with timeout implemented by 40PHP

Share IP addresses and geographic locations in 41PHP

42 use XDebug to analyze the PHP program and find out the performance bottleneck

43PHP developers should know five tips for Composer

Using session in 44php to prevent users from logging on to the background illegally

45PHP dependency management tool Composer Getting Started Tutorial

46PHP implements the method of saving browsing history page URLs to cookies

How to use imagick to generate PSD file thumbnails in 47PHP

48 friend Gateway's PHP code related to QQ (study QQ's excellent materials)

Usage of the 49PHP callback function and precautions

50PHP Session may cause concurrency problems

Example of 51smarty user-defined function htmlcheckboxes usage

Example of weather forecast code collected by 52php from the Central Meteorological Station covering the whole country

53session mechanism explanation and session-related applications

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.