debug_print_backtrace

(PHP 5, PHP 7, PHP 8)

debug_print_backtrace β€” Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ ΠΎΠ±Ρ€Π°Ρ‚Π½ΡƒΡŽ трассировку Π²Ρ‹Π·ΠΎΠ²ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ

ОписаниС

function debug_print_backtrace(int $options = 0, int $limit = 0): void

Ѐункция debug_print_backtrace() Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹ΠΉ стСк Π²Ρ‹Π·ΠΎΠ²ΠΎΠ² PHP-Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ. Ѐункция Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ Π²Ρ‹Π·ΠΎΠ²Ρ‹ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ, названия Ρ„Π°ΠΉΠ»ΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΠ»ΠΈ выраТСниями include ΠΈΠ»ΠΈ require, ΠΈ Π΄Ρ€ΡƒΠ³ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ вычислили Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ eval().

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

options

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ Π±ΠΈΡ‚ΠΎΠ²ΡƒΡŽ маску ΠΈΠ· ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… ΠΎΠΏΡ†ΠΈΠΉ:

ΠžΠΏΡ†ΠΈΠΈ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ debug_print_backtrace()
DEBUG_BACKTRACE_IGNORE_ARGS ΠžΠΏΡ†ΠΈΡ устанавливаСт Π±ΠΈΡ‚, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ опрСдСляСт, ΠΈΡΠΊΠ»ΡŽΡ‡Π°Ρ‚ΡŒ Π»ΠΈ ΠΊΠ»ΡŽΡ‡ "args" ΠΈΠ· Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠ³ΠΎ массива ΠΈ вмСстС с Π½ΠΈΠΌ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Ρ‹ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ ΠΈ ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ², Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΡƒΠΌΠ΅Π½ΡŒΡˆΠΈΡ‚ΡŒ расход памяти.
limit

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°Π΅Ρ‚ количСство ΠΊΠ°Π΄Ρ€ΠΎΠ² стСка Π² Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠΌ массивС. Π‘ΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ β€” limit=0 β€” возвращаСтся вСсь стСк Π²Ρ‹Π·ΠΎΠ²ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π½Π΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ значСния послС выполнСния.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Π²Ρ‹Π²ΠΎΠ΄Π° ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΉ трассировки Π²Ρ‹Π·ΠΎΠ²ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ debug_print_backtrace()

<?php

// Π€Π°ΠΉΠ»: include.php
function a()
{
b();
}

function
b()
{
c();
}

function
c()
{
debug_print_backtrace();
}

a();

?>
<?php

// Π€Π°ΠΉΠ» test.php
// Π­Ρ‚ΠΎ Ρ„Π°ΠΉΠ», ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ потрСбуСтся Π·Π°ΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ
include 'include.php';

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

#0  c() called at [/tmp/include.php:10]
#1  b() called at [/tmp/include.php:6]
#2  a() called at [/tmp/include.php:17]
#3  include(/tmp/include.php) called at [/tmp/test.php:3]

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • debug_backtrace() - Π“Π΅Π½Π΅Ρ€ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΎΠ±Ρ€Π°Ρ‚Π½ΡƒΡŽ трассировку Π²Ρ‹Π·ΠΎΠ²ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
91
bishop ΒΆ
16 years ago
Another way to manipulate and print a backtrace, without using output buffering:

<?php
// print backtrace, getting rid of repeated absolute path on each file
$e = new Exception();
print_r(str_replace('/path/to/code/', '', $e->getTraceAsString()));
?>
up
5
sun at drupal dot org ΒΆ
1 year ago
If you see string arguments and parameters getting cut off like this:

#0 hook.php(324): output_notice('checkout_before...')
#1 hook.php(348): invoke_hook('checkout_before...', Array)

You can increase the maximum length of arguments and parameters in the printed trace through an PHP INI setting:

<?php
ini_set('zend.exception_string_param_max_len', 100);
debug_print_backtrace();
?>

…so you can read the full arguments:

#0 hook.php(324): output_notice('checkout_before_payment')
#1 hook.php(348): invoke_hook('checkout_before_payment', Array)

In edge cases, it might even uncover nested traces that you didn't notice before.
up
25
dany dot dylan at gmail dot com ΒΆ
17 years ago
I like the output of debug_print_backtrace() but I sometimes want it as a string.

bortuzar's solution to use output buffering is great, but I'd like to factorize that into a function.  Doing that however always results in whatever function name I use appearing at the top of the stack which is redundant.

Below is my noddy (simple) solution.  If you don't care for renumbering the call stack, omit the second preg_replace().

<?php
    function debug_string_backtrace() {
        ob_start();
        debug_print_backtrace();
        $trace = ob_get_contents();
        ob_end_clean();

        // Remove first item from backtrace as it's this function which
        // is redundant.
        $trace = preg_replace ('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1);

        // Renumber backtrace items.
        $trace = preg_replace ('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace);

        return $trace;
    }
?>
up
-3
David Spector ΒΆ
5 years ago
If your show your error messages in HTML (with suitable safety using entities), this function won't work nicely because it uses newlines for formatting.

Here is a function that works similarly, but using <BR> tags. Insert it near the beginning of your program to add a stack to Warning output only, or modify it as you like:

// Here is code for error stack output in HTML:
function error_handler_callback($errno,$message,$file,$line,$context)
    {
    if ($errno === E_WARNING)
        echo "Stack, innermost first:<br>".nl2br((new Exception())->getTraceAsString());
    return false; // to execute the regular error handler
    }
set_error_handler("error_handler_callback");