我正在寻找一种在 PHP 中打印调用堆栈的方法。
如果函数刷新 IO 缓冲区,则加分。
比以下更具可读性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}"
如果您想生成回溯,您正在寻找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"
}
}
它们显然不会刷新 I/O 缓冲区,但您可以自己使用flush
和/或ob_flush
.
(请参阅第一个的手册页以找出“和/或”的原因;-))
奇怪的是没有人以这种方式发布:
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
这实际上打印了没有垃圾的回溯——只是调用了什么方法以及在哪里调用。
记录跟踪
$e = new Exception;
error_log(var_export($e->getTraceAsString(), true));
谢谢@Tobiasz
Backtrace 会转储大量您不需要的垃圾。它需要很长,很难阅读。您通常想要的只是“从哪里来的什么叫什么?” 这是一个简单的静态函数解决方案。我通常将它放在一个名为“调试”的类中,其中包含我所有的调试实用程序功能。
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++;
}
}
}
你这样称呼它:
debugUtils::callStack(debug_backtrace());
它产生如下输出:
==================================================
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)
==================================================
如果你想要一个看起来非常类似于 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;
}
这将返回格式如下的堆栈跟踪:
#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()
var_dump(debug_backtrace());
这样做是你想要的吗?
见debug_print_backtrace
。如果你愿意,我想你可以flush
稍后打电话。
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
用于debug_backtrace
获取调用了哪些函数和方法以及包含哪些文件导致debug_backtrace
调用点的回溯。
请看一下这个 utils 类,可能会有所帮助:
用法:
<?php
/* first caller */
Who::callme();
/* list the entire list of calls */
Who::followme();
源码类:https ://github.com/augustowebd/utils/blob/master/Who.php
Walltearer 的解决方案非常好,特别是如果包含在“pre”标签中:
<pre>
<?php debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); ?>
</pre>
- 在不同的线路上列出呼叫,整齐地编号
debug_backtrace()
你可能想调查一下debug_backtrace
,或者也许debug_print_backtrace
。
我已经修改了Don Briggs 的上述答案,以使用内部错误日志记录而不是公共打印,这可能是您在实时服务器上工作时最关心的问题。此外,还添加了一些修改,例如包含完整文件路径而不是基本名称的选项(因为不同路径中可能存在具有相同名称的文件),以及(对于需要它的人)完整的节点堆栈输出:
class debugUtils {
public static function callStack($stacktrace) {
error_log(str_repeat("=", 100));
$i = 1;
foreach($stacktrace as $node) {
// uncomment next line to debug entire node stack
// error_log(print_r($node, true));
error_log( $i . '.' . ' file: ' .$node['file'] . ' | ' . 'function: ' . $node['function'] . '(' . ' line: ' . $node['line'] . ')' );
$i++;
}
error_log(str_repeat("=", 100));
}
}
// call debug stack
debugUtils::callStack(debug_backtrace());
如果一个人只是对调用的文件感兴趣 - 您可以使用以下内容:
print_r(array_column(debug_backtrace(),'file'));
同样,您可以file
用不同的键替换以查看该数据。