IV. APD 高级 PHP 调试器

简介

APD 是 Advanced PHP Debugger,即高级 PHP 调试器。是用来给 PHP 代码提供规划与纠错的能力,以及提供了显示整个堆栈追踪的能力。APD 支持交互式纠错,但默认是将数据写入跟踪文件。它还提供了基于事件的日志,因此不同级别的信息(包括函数调用,参数传递,计时等)可以对个别的脚本打开或关闭。

小心

APD 是一个 Zend 扩展库,修改了 PHP 内部处理函数调用的方式,因此有可能与其它 Zend 扩展库(例如 Zend Optimizer)不兼容。

安装

APD 目前作为一个 PECL 扩展,可以从 http://pecl.php.net/package/apd 得到。确保安装了 CGI 版的 PHP 并且在当前目录下可以运行,以及可以运行 phpize 脚本。

运行以下命令以下载,编译并安装最新稳定版的 APD:

pear install apd

这将自动把 APD Zend 扩展安装到 PHP 扩展库目录中。不是一定要放到那里,可以将此模块存放到任何 PHP 可以读取的目录中,只要正确设定了相应的 zend_extension 参数即可。

Windows 用户可以从 http://snaps.php.net/win32/PECL_STABLE/ 下载扩展库的 DLL 文件 php_apd.dll

在 INI 文件中,加入下面几行:

zend_extension = /absolute/path/to/apd.so
apd.dumpdir = /absolute/path/to/trace/directory
apd.statement_tracing = 0

根据 PHP 版本,zend_extension 指令可以是以下之一:

zend_extension              (non ZTS, non debug build)
zend_extension_ts           (    ZTS, non debug build)
zend_extension_debug        (non ZTS,     debug build)
zend_extension_debug_ts     (    ZTS,     debug build)

在 Win32 下编译

要在 Windows 下编译 APD,需要一个在 http://php.net/ 中说明的可用 PHP 编译环境――基本上,需要有 Microsoft Visual C++,win32build.zip,bison/flex,以及相关知识。另外确保 adp.dsp 具有 DOS 行结束符,如果是 Unix 的行结束符,Microsoft Visual C++ 会觉得有问题。

运行时配置

这些函数的行为受 php.ini 的影响。

表 1. APD 配置选项

名称默认值可修改范围更新记录
apd.dumpdirNULLPHP_INI_ALL 
apd.statement_tracing"0"PHP_INI_ALL 
有关 PHP_INI_* 常量进一步的细节与定义参见附录 H

以下是配置选项的简要解释。

apd.dumpdir string

设定 APD 写入调试输出文件的目录。可以指定绝对路径或相对路径。

可以在 apd_set_pprof_trace() 中以参数指定一个不同目录。

apd.statement_tracing boolean

指定是否进行每行的跟踪。将此项打开(设为 1)将影响到程序的性能。

资源类型

本扩展模块未定义任何资源类型。

预定义常量

本扩展模块未定义任何常量。

怎样在脚本中使用 PHP-APD

  1. 在 PHP 脚本的第一行调用 apd_set_pprof_trace() 函数来启动跟踪:

    apd_set_pprof_trace();

    可以在脚本中任何一行插入这一行,但是如果不是从脚本开头开始跟踪的话,则丢失了部分数据,有可能造成性能上的瓶颈。

  2. 然后运行脚本。输出将被写入到 apd.dumpdir/pprof_pid.ext

    提示: 如果运行的是 CGI 版的 PHP,需要加入 '-e' 标记启用扩展信息以使 APD 正常工作。例如: php -e -f script.php

  3. 要显示格式化的调试数据,运行 pprofp 命令并加上自己选择的排序和显示选项。格式化的输出类似于:

    bash-2.05b$ pprofp -R /tmp/pprof.22141.0
    
    Trace for /home/dan/testapd.php
    Total Elapsed Time = 0.00
    Total System Time  = 0.00
    Total User Time    = 0.00
    
    
    Real         User        System             secs/    cumm
    %Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
    --------------------------------------------------------------------------------------
    100.0 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0000   0.0009            0 main
    56.9 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0005   0.0005            0 apd_set_pprof_trace
    28.0 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 preg_replace
    14.3 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 str_replace

    上例中用的 -R 选项将调试数据表格以脚本执行每个函数所花的时间来排序。"cumm call" 一列显示了每个函数被调用的次数,"s/call" 一列显示了每个函数每次调用平均所花的秒数。

  4. 要生成可以导入到 KCacheGrind 调试分析系统中的调用树文件,运行 pprof2calltree 命令。

联系信息

如果有注解,错误修正,提高或者想帮助开发此扩展,请发送邮件到 apd@mail.communityconnect.com。欢迎任何帮助。

目录
apd_breakpoint -- Stops the interpreter and waits on a CR from the socket
apd_callstack -- Returns the current call stack as an array
apd_clunk -- Throw a warning and a callstack
apd_continue -- Restarts the interpreter
apd_croak -- Throw an error, a callstack and then exit
apd_dump_function_table -- Outputs the current function table
apd_dump_persistent_resources -- Return all persistent resources as an array
apd_dump_regular_resources -- Return all current regular resources as an array
apd_echo -- Echo to the debugging socket
apd_get_active_symbols -- Get an array of the current variables names in the local scope
apd_set_pprof_trace -- Starts the session debugging
apd_set_session_trace -- Starts the session debugging
apd_set_session -- Changes or sets the current debugging level
apd_set_socket_session_trace -- Starts the remote session debugging
override_function -- Overrides built-in functions
rename_function -- Renames orig_name to new_name in the global function table