# 查看PHP错误信息 1、修改php.ini配置 ~~~ php.ini中display_errors / display_startup_errors 设置为On php.ini中error_reporting 设置为E_ALL PHP代码中设置error_reporting(E_ALL) ~~~ 2、PHP中打开报错 ~~~ error_reporting(E_ALL); ini_set ('display_errors', 'On'); ~~~ 有些情况下php.ini配置中关闭了错误显示,需要修改php.ini打开错误信息,或者错误信息被导出到了日志文件,这种情况可以直接tailf php_error.log来看错误信息。 ## 多个版本的php或php-cli与php-fpm加载不同的配置 ~~~ which php #得到反馈 /usr/bin/php,查看PHP安装在哪 ~~~ ~~~ php -i |grep php.ini #得到加载那个php.ini,如下 Configuration File (php.ini) Path => /usr/local/etc/php/5.6 Loaded Configuration File => /usr/local/etc/php/5.6/php.ini ~~~ ## 打印单步调试 ### 1、PHP的getTrace fiel:发生异常的PHP程序文件名称; line:发生异常的代码所在的行号; function:发生异常的函数或方法; class:发生异常的函数或方法所在的类; type:调用发生异常的函数或方法的类型(“::”调用静态类成员,“->”调用实例化对象); args:发生异常的函数或方法所接受的参数。 ~~~ <?php $path = "www.phpdo.net"; try { file_open($path); } catch(Exception $e) { echo "异常信息".$e->getMessage()."\n"; echo "异常代码".$e->getCode()."\n"; echo "异常文件".$e->getFile()."\n"; echo "异常代码所在行".$e->getLine()."\n"; echo "传递路线:"; print_r($e->getTrace()); //以数组的形式返回跟踪异常的每一部的传递路线 echo $e->getTraceAsString();//返回格式化成字符串的getTrace函数信息 } function file_open($path) { if(!file_exists($path)) { throw new exception("www.phpdo.net这个文件不存在",1); } if(!fopen($path,"r")) { throw new exception("www.phpdo.net这个文件无法打开",2); } } ?> 结果: 异常信息www.phpdo.net 这个文件不存在 异常代码1 异常文件E:\xampp\htdocs\php\Test\10.2.6.php 异常代码所在行22 传递路线: Array ( [0] => Array ( [file] => E:\xampp\htdocs\php\Test\10.2.6.php [line] => 5 [function] => file_open [args] => Array ( [0] => www.phpdo.net ) ) ) #0 E:\xampp\htdocs\php\Test\10.2.6.php(5): file_open(‘www.phpdo.net’) #1 {main} ~~~ ### 2、PHP debug_backtrace() <table class="dataintable " style="margin:10px 0px 0px; padding:0px; border:1px solid rgb(170,170,170); border-collapse:collapse; width:709px; color:rgb(0,0,0); font-family:Verdana,Arial,宋体; font-size:12px; background-color:rgb(249,249,249)"><tbody style="margin:0px; padding:0px; border:0px"><tr style="margin:0px; padding:0px; border:0px"><th style="margin:0px; padding:5px 15px 5px 6px; border:1px solid rgb(170,170,170); vertical-align:baseline; background-color:rgb(213,213,213)">名称</th><th style="margin:0px; padding:5px 15px 5px 6px; border:1px solid rgb(170,170,170); vertical-align:baseline; background-color:rgb(213,213,213)">类型</th><th style="margin:0px; padding:5px 15px 5px 6px; border:1px solid rgb(170,170,170); vertical-align:baseline; background-color:rgb(213,213,213)">描述</th></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">function</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">字符串</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">当前的函数名。</td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">line</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">整数</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">当前的行号。</td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">file</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">字符串</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">当前的文件名。</td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">class</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">字符串</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">当前的类名</td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">object</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">对象</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">当前对象。</td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">type</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">字符串</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">当前的调用类型,可能的调用:<ul class="listintable" style="margin:20px; padding:0px; border:0px; list-style-type:none"><li style="margin:0px; padding:0px; border:0px; list-style-type:disc">返回: "-&gt;"  - 方法调用</li><li style="margin:0px; padding:0px; border:0px; list-style-type:disc">返回: "::"  - 静态方法调用</li><li style="margin:0px; padding:0px; border:0px; list-style-type:disc">返回 nothing - 函数调用</li></ul></td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">args</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">数组</td><td style="margin:0px; padding:6px 15px 6px 6px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">如果在函数中,列出函数参数。如果在被引用的文件中,列出被引用的文件名。</td></tr></tbody></table> ~~~ <?php function one($str1, $str2) { two("Glenn", "Quagmire"); } function two($str1, $str2) { three("Cleveland", "Brown"); } function three($str1, $str2) { print_r(debug_backtrace()); } one("Peter", "Griffin"); ?> ~~~ ### 3、其他(var_dump,die(),exit()等) ## PHP进程跟踪查看 ### 什么是strace? strace是一个非常简单的工具,它可以跟踪系统调用的执行。最简单的方式,它可以从头到尾跟踪binary的执行,然后以一行文本输出系统调用的名字,参数和返回值。 其实它可以做的更多: 可以对特定的系统调用或者几组系统调用进行过滤 可以通过统计特定系统调用的调用次数、耗费的时间、成功和失败的次数来配置(profile)系统调用的使用I 跟踪发送给进程的信号量 可以通过pid附着(attach)到任何运行的进程 如果你使用的是其它Unix系统,它类似于"truss"。其它更复杂的是Sun的Dtrace. 调用:  ~~~ strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] …  [ -ofile ] [ -ppid ] … [ -sstrsize ] [ -uusername ] [ command [ arg … ] ] strace -c [ -eexpr ] … [ -Ooverhead ] [ -Ssortby ] [ command [ arg … ] ]  ~~~ 功能:  跟踪程式执行时的系统调用和所接收的信号.通常的用法是strace执行一直到commande结束.  使用strace php test.php,或者strace -p 进程ID。strace就可以帮助你透过现象看本质,掌握程序执行的过程。 ~~~ 如: strace -p PID分析php-fpm进程 at("/data/tdocs/webroot/go/./Widget/boutique.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory) lstat("/data/tdocs/webroot/go/./Widget/openapi.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory) lstat("/data/tdocs/webroot/go/./Widget/groupnew.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory) lstat("/data/tdocs/webroot/go/./Widget/groupStatistical.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory) lstat("/usr/share/pear/Widget/groupStatistical.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory) lstat("/usr/share/php/Widget/groupStatistical.inc.php", 0x7fffc74cb320) = -1 ENOENT (No such file or directory) ~~~ ### 怎么使用它? 找出程序在startup的时候读取的哪个config文件? ~~~ $ strace php 2>&1 | grep php.ini open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/local/lib/php.ini", O_RDONLY) = 4 lstat64("/usr/local/lib/php.ini", {st_mode=S_IFLNK|0777, st_size=27, ...}) = 0 readlink("/usr/local/lib/php.ini", "/usr/local/Zend/etc/php.ini", 4096) = 27 lstat64("/usr/local/Zend/etc/php.ini", {st_mode=S_IFREG|0664, st_size=40971, ...}) = 0 ~~~ ~~~ strace -e open php 2>&1 | grep php.ini open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/local/lib/php.ini", O_RDONLY) = 4 ~~~ 某个进程现在在做什么? ~~~ strace -p 15427 Process 15427 attached - interrupt to quit futex(0x402f4900, FUTEX_WAIT, 2, NULL Process 15427 detached ~~~ 是谁偷走了时间? 你可以重新编译app,打开profiling,以获取精确的信息。但是通常利用strace附着(attach)一个进程以快速地看一下当前时间花费在哪里非常有用。可以看下是否90%的CPU用在真正的工作,或者用在其它方面了。 ~~~ strace -c -p 11084 Process 11084 attached - interrupt to quit Process 11084 detached % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 94.59 0.001014 48 21 select 2.89 0.000031 1 21 getppid 2.52 0.000027 1 21 time ------ ----------- ----------- --------- --------- ---------------- 100.00 0.001072 63 total ~~~ ## 通信过程如何分析-tcpdump? 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。 tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。 参考:http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html