打印PHP调用堆栈

问题描述 投票:228回答:15

我正在寻找一种在PHP中打印调用堆栈的方法。

如果函数刷新IO缓冲区,则为奖励积分。

php stack-trace error-logging
15个回答
119
投票

如果你想生成一个回溯,你正在寻找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自行完成。

(参见第一个手册页,找出为什么“和/或”;-))


3
投票

使用debug_backtrace来获取已调用的函数和方法的回溯以及包含哪些文件导致debug_backtrace被调用的点。


2
投票

请看看这个utils类,可能会有所帮助:

用法:

<?php
/* first caller */
 Who::callme();

/* list the entire list of calls */
Who::followme();

来源类:https://github.com/augustowebd/utils/blob/master/Who.php


1
投票

debug_backtrace()


1
投票

你可能想看看debug_backtrace,或者debug_print_backtrace


1
投票

Walltearer的解决方案非常出色,特别是如果封装在'pre'标签中:

<pre>
<?php debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); ?>
</pre>

- 在单独的线路上设置呼叫,整齐编号


0
投票

我已经调整了Don Briggs's answer above以使用内部错误记录而不是公共打印,这可能是您在实时服务器上工作时最关心的问题。此外,添加了一些修改,例如选项包括完整文件路径而不是基本名称(因为,可能存在不同路径中具有相同名称的文件),以及(对于需要它的人)一个完整的节点堆栈输出:

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());

521
投票

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}"

38
投票

记录跟踪

$e = new Exception;
error_log(var_export($e->getTraceAsString(), true));

谢谢@Tobias


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++;
        }
    } 
}

你这样称呼它:

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)
==================================================

30
投票

奇怪,没有人发布这样的方式:

debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

这实际上打印回溯没有垃圾 - 只是调用什么方法和在哪里。


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;
} 

这将返回一个格式如下的堆栈跟踪:

#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() 

8
投票
var_dump(debug_backtrace());

这样做你想要的吗?


6
投票

debug_print_backtrace。我想你可以随后打电话给flush


4
投票

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 
© www.soinside.com 2019 - 2024. All rights reserved.