多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境。 PHPDBG的目标是成为一个轻量级、强大、易用的PHP调试平台。可以在PHP5.4和之上版本中使用。在php5.6和之上版本将内部集成。如; ![](https://img.kancloud.cn/bf/9d/bf9df91378469686ca7670d3557a7be5_733x278.png) **主要功能:** – 单步调试 – 灵活的下断点方式(类方法、函数、文件:行、内存地址、opcode) – 可直接调用php的eval – 可以查看当前执行的代码 – 用户空间API(userland/user space) – 方便集成 – 支持指定php配置文件 – JIT全局变量 – readline支持(可选),终端操作更方便 – 远程debug,使用java GUI – 操作简便(具体看help) 安装 ``` #cd /home/php/sapi #git clone htt[ps](http://www.111cn.net/fw/photo.html)://github.com/krakjoe/phpdbg #cd ../ #./buildconf --force #./config.nice #make -j8 #make install-phpdbg ``` 在PHP5.6和之上版本将内部集成 ## 运行时配置 这些功能的行为受php.ini中的设置影响。 **phpdbg配置选项** | 名称 | 默认 | 多变 | 变更日志 |描述| | --- | --- | --- | --- | --- | | [phpdbg.eol](https://www.php.net/manual/en/phpdbg.configuration.php#ini.phpdbg.eol) | 2 | PHP\_INI\_ALL | 自PHP 7.0.0起可用 |用于输出的行尾的种类。要设置该值,必须使用字符串别名之一| | [phpdbg.path](https://www.php.net/manual/en/phpdbg.configuration.php#ini.phpdbg.path) |   | 6 | 自PHP 5.6.3起可用 | | # 预定义常量 下面的常量由该扩展定义,并且仅当该扩展已编译为PHP或在运行时动态加载时才可用。 PHPDBG_VERSION(string) PHPDBG_FILE (int) 从PHP 7.3.0开始删除。 PHPDBG_METHOD (int) 从PHP 7.3.0开始删除。 PHPDBG_LINENO (int) 从PHP 7.3.0开始删除。 PHPDBG_FUNC (int) 从PHP 7.3.0开始删除。 PHPDBG_COLOR_PROMPT (int) PHPDBG_COLOR_NOTICE (int) PHPDBG_COLOR_ERROR (int) # 函数: * [phpdbg\_break\_file](https://www.php.net/manual/en/function.phpdbg-break-file.php)—在文件的一行插入一个断点 * [phpdbg\_break\_function](https://www.php.net/manual/en/function.phpdbg-break-function.php)—在函数入口处插入一个断点 * [phpdbg\_break\_method](https://www.php.net/manual/en/function.phpdbg-break-method.php)—在方法入口处插入一个断点 * [phpdbg\_break\_next](https://www.php.net/manual/en/function.phpdbg-break-next.php)—在下一个操作码处插入一个断点 * [phpdbg\_clear](https://www.php.net/manual/en/function.phpdbg-clear.php)—清除所有断点 * [phpdbg\_color](https://www.php.net/manual/en/function.phpdbg-color.php)—设置某些元素的颜色 * [phpdbg\_end\_oplog-](https://www.php.net/manual/en/function.phpdbg-end-oplog.php)说明 * [phpdbg\_exec](https://www.php.net/manual/en/function.phpdbg-exec.php)—尝试设置执行上下文 * [phpdbg\_get\_executable](https://www.php.net/manual/en/function.phpdbg-get-executable.php)—说明 * [phpdbg\_prompt-](https://www.php.net/manual/en/function.phpdbg-prompt.php)设置命令提示符 * [phpdbg\_start\_oplog-](https://www.php.net/manual/en/function.phpdbg-start-oplog.php)说明 **命令行选项** | Option | Example Argument | Description | | --- | --- | --- | | \-c | \-c/my/php.ini | Set php.ini file to load | | \-d | \-dmemory\_limit=4G | Set a php.ini directive | | \-n |   | Disable default php.ini | | \-q |   | Suppress welcome banner | | \-v |   | Enable oplog output | | \-b |   | Disable color | | \-i | \-imy.init | Set .phpdbginit file | | \-I |   | Ignore default .phpdbginit | | \-O | \-Omy.oplog | Set oplog output file | | \-r |   | Run execution context | | \-rr |   | Run execution context and quit after execution (not respecting breakpoints) | | \-e |   | Generate extended information for debugger/profiler | | \-E |   | Enable step through eval, careful! | | \-s | \-s=, -s=foo | Read code to execute from stdin with an optional delimiter | | \-S | \-Scli | Override SAPI name, careful! | | \-l | \-l4000 | Setup remote console ports | | \-a | \-a192.168.0.3 | Setup remote console bind address | | \-x |   | Enable xml output (instead of normal text output) | | \-p | \-p, -p=func, -p\* | Output opcodes and quit | | \-h |   | Print the help overview | | \-V |   | Print version number | | \-- | \-- arg1 arg2 | Use to delimit phpdbg arguments and php $argv; append any $argv argument after it | 文件test\_phpdbg\_inc.php源代码如下: ``` function phpdbg_inc_func() { echo "phpdbg_inc_func \n"; } ``` 文件test_phpdgb.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(); ``` **启动phpdbg** phpdbg安装成功后,会在安装目录的bin目录下(windows是与php.exe同级目录)。进入bin目录,直接输入phpdbg即可。如下: ![](https://img.kancloud.cn/5e/20/5e20c80796150d7ed8f03cd928ff77de_590x141.png) 要想加载要调试的php脚本,只需要执行exec命令即可(win下需要绝对路径?相对失败了)。如下: ``` exec ./test_phpdbg.php //exec D:\phpstudy_pro\WWW\www.test.com\audit\test_phpdgb.php ``` ![](https://img.kancloud.cn/c9/b0/c9b0fab8d8732c0b77c0d7367dec382e_573x72.png) 当然我们也可以在启动phpdbg的时候,指定e参数。如下: ``` phpdbg -e ./test_phpdbg.php ``` **设置断点** 设置断点的命令和gdb一样。都是break,简写形式为b。不过具体的命令参数还是有所差异的。和gdb的断点命令相同之处,它们都可以“按文件名:行号” 或者 行号的方式设置断点。除此之外,phpdbg还提供了一些针对php特有的设置断点的方式。如,根据opline设置断点,根据opcode设置断点等。 众所周知,php代码最终是解析成opcode,然后由php内核一条条执行。一条php语句,可能会解析成多条opcode。如果可以按opcode设置断点,我们就可以更精确的跟踪程序执行过程。下面我们来看看phapdbg设置断点的具体示例。 按opline设置断点: 这里所说的opline,就是以方法入口作为起点,当前代码的行号。如test\_phpdgb.php文件中,第18行的代码“$param = $param + “baba”;”的opline就是 2。 ``` prompt> b func#2 ``` ``` prompt> r ``` ![](https://img.kancloud.cn/59/93/5993f0f9181904dc2139889f24707374_743x240.png) **查看断点:** ``` info break ``` ![](https://img.kancloud.cn/ac/dc/acdc58d8fccdcbabdffebff9bb2bf076_400x117.png) info break的显示结果中会把断点的类型也给显示出来。#后面的数字是断点号。我们可以根据断点号删除断点 **删除断点:** ``` break del 0 ``` ![](https://img.kancloud.cn/f8/21/f82164e7468335c08a8dd125d73d7144_225x46.png) **查看代码** 显示指定函数的代码: ...... l f func ...... ![](https://img.kancloud.cn/f6/8b/f68b07900a04712363c0a75ef0003903_369x97.png) 单步执行 step , 简写s。 .... s .... 继续执行命令是continue,简写形式为c。 .... c .... 执行php代码 可以在调试的过程中使用ev命令执行任意的php代码。如: ``` prompt> ev $var = "val"; val prompt> ev var_dump($var); string(3) "val" ``` 可以通过这种方式,在调试过程中动态的修改变量值,查看执行效果。 ***** **查看帮助信息获取更多用法** 通过help命令我们可以获取帮助信息 ``` help ```