查看PHP錯誤信息
php.ini中error_reporting 設置爲E_ALL
PHP代碼中設置error_reporting(E_ALL)
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
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()
名稱 | 類型 | 描述 |
---|---|---|
function | 字符串 | 當前的函數名。 |
line | 整數 | 當前的行號。 |
file | 字符串 | 當前的文件名。 |
class | 字符串 | 當前的類名 |
object | 對象 | 當前對象。 |
type | 字符串 |
當前的調用類型,可能的調用:
|
args | 數組 | 如果在函數中,列出函數參數。如果在被引用的文件中,列出被引用的文件名。 |
<?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?
其實它可以做的更多:
可以對特定的系統調用或者幾組系統調用進行過濾
可以通過統計特定系統調用的調用次數、耗費的時間、成功和失敗的次數來配置(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 -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)
怎麼使用它?
$ 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
是誰偷走了時間?
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