参考了IBM的一些文章: http://www.ibm.com/developerworks/cn/opensource/os-php-read/#de3
一、出错信息的显示配置
php配置(php.ini):
display_error = on
error_reporting = E_ALL
看php.ini里面的注释,error_reporting的值为以下的按位或:
; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings (doesn't include E_STRICT)
; E_ERROR - fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_STRICT - run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message
有这样几个我觉得可能有用的例子:
; - Show all errors, except for notices and coding standards warnings
;
;error_reporting = E_ALL & ~E_NOTICE
;
; - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE | E_STRICT
同样,也可以利用error_reporting函数在php代码中设置错误信息级别:
int error_reporting ([ int $level ] )
二、使用var_dump、print类的函数来输出调试信息
三、Advanced PHP Debugger
参考: http://www.phpx.com/man/php-zh/ref.apd.html, http://www.linuxjournal.com/article/7213
安装: 使用pear(pecl)安装
sudo pecl install apd
配置: (php.ini)
添加以下3行:
zend_extension = /absolute/path/to/apd.so apd.dumpdir = /absolute/path/to/trace/directory apd.statement_tracing = 0
使用:
要针对哪个文件进行分析,就在文件第一行加入以下php函数:
apd_set_pprof_trace();
查看结果:
之后运行它,就会在apd.dumpdir 下生成类似pprof.25802的文件。其中25802是进程号。直接查看该文件是没用意义的,使用php自带的pprofp命令来查看,可以获得详尽的信息。
为了正确的运行pprofp命令,可能需要修改php.ini的include_path,把/usr/local/php/lib/php/加入进去,因为pprofp中require了下面的文件。
pprofp -u /var/log/php-apd/pprof.25802
[chengyi@localhost apdResult]$ pprofp -u pprof.21133.0
The report above shows time and memory usage on a per-function basis, sorted by user-time as directed by the -u option. The first few columns are execution time in seconds. The Calls column is a count of the number of times that function was executed by the script. secs/call is the average execution time of each call to that function, while cumm s/call is the cumulative time spent on that function. Then it lists memory usage and finally the name of the function itself. Notice that function call reports are truncated to 15 functions by default.