打印 PHP 调用堆栈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1423157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print PHP Call Stack
提问by Justin
I'm looking for a way to print the call stack in PHP.
我正在寻找一种在 PHP 中打印调用堆栈的方法。
Bonus points if the function flushes the IO buffer.
如果函数刷新 IO 缓冲区,则加分。
采纳答案by Pascal MARTIN
If you want to generate a backtrace, you are looking for debug_backtraceand/or debug_print_backtrace.
如果您想生成回溯,您正在寻找debug_backtrace和/或debug_print_backtrace.
The first one will, for instance, get you an array like this one (quoting the manual):
例如,第一个将为您提供一个这样的数组(引用手册):
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"
}
}
They will apparently not flush the I/O buffer, but you can do that yourself, with flushand/or ob_flush.
它们显然不会刷新 I/O 缓冲区,但您可以使用flush和/或ob_flush.
(see the manual page of the first one to find out why the "and/or" ;-) )
(请参阅第一个的手册页以了解为什么“和/或”;-))
回答by Tobiasz Cudnik
More readable than debug_backtrace():
比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}"
回答by Sydwell
To log the trace
记录跟踪
$e = new Exception;
error_log(var_export($e->getTraceAsString(), true));
Thanks @Tobiasz
谢谢@Tobiasz
回答by Don Briggs
Backtrace dumps a whole lot of garbage that you don't need. It takes is very long, difficult to read. All you usuall ever want is "what called what from where?" Here is a simple static function solution. I usually put it in a class called 'debug', which contains all of my debugging utility functions.
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++;
}
}
}
You call it like this:
你这样称呼它:
debugUtils::callStack(debug_backtrace());
And it produces output like this:
它产生这样的输出:
==================================================
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)
==================================================
回答by AbstractVoid
Strange that noone posted this way:
奇怪的是没有人以这种方式发布:
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
This actually prints backtrace without the garbage - just what method was called and where.
这实际上打印了没有垃圾的回溯 - 只是调用了什么方法以及在哪里调用。
回答by TroySteven
If you want a stack trace which looks very similar to how php formats the exception stack trace than use this function I wrote:
如果你想要一个堆栈跟踪,它看起来与 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;
}
This will return a stack trace formatted like this:
这将返回一个格式如下的堆栈跟踪:
#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()
回答by inkedmn
var_dump(debug_backtrace());
Does that do what you want?
这样做你想要的吗?
回答by Martin Geisler
See debug_print_backtrace. I guess you can call flushafterwards if you want.
见debug_print_backtrace。flush如果你愿意,我想你可以事后打电话。
回答by renenglish
phptraceis a great tool to print PHP stack anytime when you want without installing any extensions.
phptrace是一个很棒的工具,可以随时随地打印 PHP 堆栈,而无需安装任何扩展。
There are two major function of phptrace: first, print call stack of PHP which need not install anything, second, trace php execution flows which needs to install the extension it supplies.
phptrace有两个主要功能:一是打印不需要安装任何东西的PHP调用栈;二是跟踪php执行流程,需要安装它提供的扩展。
as follows:
如下:
$ ./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
回答by Gumbo
Use debug_backtraceto get a backtrace of what functions and methods had been called and what files had been included that led to the point where debug_backtracehas been called.
使用debug_backtrace得到一个什么样的功能和方法已被调用,哪些文件已被纳入,导致在点回溯debug_backtrace被调用。

