Tob*_*nik 531
比debug_backtrace()以下更易读:
$e = new \Exception;
var_dump($e->getTraceAsString());
#2 /usr/share/php/PHPUnit/Framework/TestCase.php(626): SeriesHelperTest->setUp()
#3 /usr/share/php/PHPUnit/Framework/TestResult.php(666): PHPUnit_Framework_TestCase->runBare()
#4 /usr/share/php/PHPUnit/Framework/TestCase.php(576): PHPUnit_Framework_TestResult->run(Object(SeriesHelperTest))
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(757): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 /usr/share/php/PHPUnit/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->runTest(Object(SeriesHelperTest), Object(PHPUnit_Framework_TestResult))
#7 /usr/share/php/PHPUnit/TextUI/TestRunner.php(305): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#8 /usr/share/php/PHPUnit/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#9 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#10 /usr/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#11 {main}"
Run Code Online (Sandbox Code Playgroud)
Pas*_*TIN 121
如果你想生成一个回溯,你正在寻找debug_backtrace和/或debug_print_backtrace.
例如,第一个将为您提供类似这样的数组(引用手册):
array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}
Run Code Online (Sandbox Code Playgroud)
它们显然不会刷新I/O缓冲区,但您可以自己使用flush和/或执行此操作ob_flush.
(参见第一个手册页,找出为什么"和/或";-))
Syd*_*ell 37
记录跟踪
$e = new Exception;
error_log(var_export($e->getTraceAsString(), true));
Run Code Online (Sandbox Code Playgroud)
谢谢@Tobiasz
Don*_*ggs 33
Backtrace会丢弃你不需要的大量垃圾.这需要很长时间,难以阅读.你通常想要的只是"从什么地方叫什么?" 这是一个简单的静态函数解决方案.我通常将它放在一个名为'debug'的类中,它包含我的所有调试实用程序函数.
class debugUtils {
public static function callStack($stacktrace) {
print str_repeat("=", 50) ."\n";
$i = 1;
foreach($stacktrace as $node) {
print "$i. ".basename($node['file']) .":" .$node['function'] ."(" .$node['line'].")\n";
$i++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你这样称呼它:
debugUtils::callStack(debug_backtrace());
Run Code Online (Sandbox Code Playgroud)
它产生如下输出:
==================================================
1. DatabaseDriver.php::getSequenceTable(169)
2. ClassMetadataFactory.php::loadMetadataForClass(284)
3. ClassMetadataFactory.php::loadMetadata(177)
4. ClassMetadataFactory.php::getMetadataFor(124)
5. Import.php::getAllMetadata(188)
6. Command.php::execute(187)
7. Application.php::run(194)
8. Application.php::doRun(118)
9. doctrine.php::run(99)
10. doctrine::include(4)
==================================================
Run Code Online (Sandbox Code Playgroud)
Wal*_*rer 30
很奇怪,没有人发布这样的方式:
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
Run Code Online (Sandbox Code Playgroud)
这实际上打印回溯没有垃圾 - 只是调用什么方法和在哪里.
Tro*_*ven 10
如果你想要一个堆栈跟踪看起来非常类似于php格式化异常堆栈跟踪而不是使用这个函数,我写道:
function debug_backtrace_string() {
$stack = '';
$i = 1;
$trace = debug_backtrace();
unset($trace[0]); //Remove call to this function from stack trace
foreach($trace as $node) {
$stack .= "#$i ".$node['file'] ."(" .$node['line']."): ";
if(isset($node['class'])) {
$stack .= $node['class'] . "->";
}
$stack .= $node['function'] . "()" . PHP_EOL;
$i++;
}
return $stack;
}
Run Code Online (Sandbox Code Playgroud)
这将返回一个格式如下的堆栈跟踪:
#1 C:\Inetpub\sitename.com\modules\sponsors\class.php(306): filePathCombine()
#2 C:\Inetpub\sitename.com\modules\sponsors\class.php(294): Process->_deleteImageFile()
#3 C:\Inetpub\sitename.com\VPanel\modules\sponsors\class.php(70): Process->_deleteImage()
#4 C:\Inetpub\sitename.com\modules\sponsors\process.php(24): Process->_delete()
Run Code Online (Sandbox Code Playgroud)
phptrace是一个很棒的工具,可以随时随地打印 PHP 堆栈,而无需安装任何扩展。
phptrace有两个主要功能:一是打印不需要安装任何东西的PHP调用栈;二是跟踪php执行流程,需要安装它提供的扩展。
如下:
$ ./phptrace -p 3130 -s # phptrace -p <PID> -s
phptrace 0.2.0 release candidate, published by infra webcore team
process id = 3130
script_filename = /home/xxx/opt/nginx/webapp/block.php
[0x7f27b9a99dc8] sleep /home/xxx/opt/nginx/webapp/block.php:6
[0x7f27b9a99d08] say /home/xxx/opt/nginx/webapp/block.php:3
[0x7f27b9a99c50] run /home/xxx/opt/nginx/webapp/block.php:10
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152202 次 |
| 最近记录: |