Home  >  Q&A  >  body text

Why does debug_backtrace() return empty?

<p>I have a piece of PHP code running on PHP 7.4. <br /><br />Part of it is a top-level PHP script (/home/path/a.php), which contains a series of try-catch blocks and contains pairs of objects and include files. various calls. <br /><br />These try-catch blocks will throw exceptions when encountering customer errors and runtime errors. <br /><br />In the throw statement, an error log is included, including a debug_backtrace() printed to the error log. </p><p><code></code></p> <pre class="brush:php;toolbar:false;">// some code use some/path/to/objects; $database = new Database(); include "some/file/reference.php"; try { // various things including $_SESSION data if (empty($_SESSION['b']) || empty($_POST['d'])) { if(empty($_SESSION['b'])) { error_log("session appears empty. No b"); } if(empty($_POST['d'])) { error_log("POST appears empty. No d"); } throw new RuntimeException('Incorrect validation/form details given.'); } } catch (RuntimeException | Exception $ex) { error_log("Login Page: ".$_SESSION['message']); error_log("debug: ".print_r(debug_backtrace(),true)); }</pre> <p>However, this code runs and collects errors correctly, but the error log only shows this: </p> <blockquote> <p>[31-Jul-2023 18:42:17 Europe/London] Some custom feedback message from $_SESSION['message']</p><p> [31-Jul-2023 18:42:17 Europe/London] Array</p><p> (<br /> )</p> </blockquote> <p>Why is debug_backtrace() empty here? I expected it to at least display the parameters and variables provided by the page, or SESSION data or environment data. <br /><br />I have used this method elsewhere and (as far as I recall) it worked fine. Did I miss something? </p><p><br /></p>
P粉204136428P粉204136428417 days ago391

reply all(1)I'll reply

  • P粉291886842

    P粉2918868422023-08-01 00:11:59

    debug_backtrace() provides a call stack that can trace where you are, but you are already in the outermost (i.e. global) scope and have not made any calls, so no call stack is available.

    print_r(debug_backtrace());
    

    The result produced is:

    Array
    (
    )
    

    If you put the code in a function, you will get a call stack containing one item:

    function foo()
    {
        print_r(debug_backtrace());
    }
    foo();
    

    The result produced is:

    Array
    (
        [0] => Array
            (
                [file] => ...
                [line] => 6
                [function] => foo
                [args] => Array
                    (
                    )
    
            )
    
    )
    

    If you want to get some (minimal) debugging information in the global scope, you can wrap all the code in a closure and call it inline immediately:

    (function() {
        // all your code here
        print_r(debug_backtrace());
    })();
    

    result

    Array
    (
        [0] => Array
            (
                [file] => ...
                [line] => 5
                [function] => {closure}
                [args] => Array
                    (
                    )
            )
    )
    

    Regarding session/environment data, they will only be displayed if you pass them as parameters:

    foo($_ENV);
    

    result:

    Array
    (
        [0] => Array
            (
                [file] => ...
                [line] => 6
                [function] => foo
                [args] => Array
                    (
                        [0] => Array
                            (
                                [TERM] => xterm
                                [PATH] => /usr/bin:/bin
                                [LANG] => C
                                [SHELL] => /bin/sh
                                [MAIL] => /var/mail/nobody
                                [LOGNAME] => nobody
                                [USER] => nobody
                                [HOME] => /tmp
                            )
                    )
            )
    )
    

    If you want to log session/environment variables like this, the best way is probably to output them explicitly:

    error_log(print_r($_ENV, true));
    error_log(print_r($_SESSION, true)); 

    reply
    0
  • Cancelreply