PHP print trace debugging information

Time:2021-10-10

For most compiled languages, such as C, Java and C #, we can easily debug breakpoints, but PHP must install Xdebug and make complex configuration in the editor to realize the ability of breakpoint debugging. However, if you simply debug and view stack backtracking, PHP has prepared two functions for us, which can make it very convenient for us to see the call of the program when it runs.

debug_backtrace()

From the literal meaning of this method, we can see that it means debugging backtracking, and what is returned is an array of backtracking information.

function a_test($str)
{
    echo "Hi: $str", PHP_EOL;
    var_dump(debug_backtrace());
}

var_dump(debug_backtrace());

a_test("A");

//Hi: A / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP: 7:
// array(1) {
//   [0] =>
//   array(4) {
//     'file' =>
//String (93) "/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP"
//     'line' =>
//     int(12)
//     'function' =>
//     string(6) "a_test"
//     'args' =>
//     array(1) {
//       [0] =>
//       string(1) "A"
//     }
//   }
// }

This method must be called in the function, and there will be no content outside the function method. From the content, it outputs information about this function\_\_ FILE__ 、 \_\_ LINE__ 、 \_\_ FUNCTION__ 、$ Argv and other information. In fact, it is related to the function where the line is currently printed.

Of course, we can also nest several more layers of functions to see what the printed content is.

function b_test(){
    c_test();
}

function c_test(){
    a_test("b -> c -> a");
}

b_test();

// Hi: b -> c -> a
/// users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP: 7:
// array(3) {
//   [0] =>
//   array(4) {
//     'file' =>
//String (93) "/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP"
//     'line' =>
//     int(37)
//     'function' =>
//     string(6) "a_test"
//     'args' =>
//     array(1) {
//       [0] =>
//       string(11) "b -> c -> a"
//     }
//   }
//   [1] =>
//   array(4) {
//     'file' =>
//String (93) "/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP"
//     'line' =>
//     int(33)
//     'function' =>
//     string(6) "c_test"
//     'args' =>
//     array(0) {
//     }
//   }
//   [2] =>
//   array(4) {
//     'file' =>
//String (93) "/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP"
//     'line' =>
//     int(40)
//     'function' =>
//     string(6) "b_test"
//     'args' =>
//     array(0) {
//     }
//   }
// }

Yes, the output order of the array is the execution order of a stack, B_ Test () is called first, so it is at the bottom of the stack, and the corresponding output is the last element in the array.

Similar usage methods are used in classes.

class A{
    function test_a(){
        $this->test_b();
    }
    function test_b(){
        var_dump(debug_backtrace());
    }
}

$a = new A();
$a->test_a();

/// users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP: 90:
// array(2) {
//   [0] =>
//   array(7) {
//     'file' =>
//String (93) "/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP"
//     'line' =>
//     int(87)
//     'function' =>
//     string(6) "test_b"
//     'class' =>
//     string(1) "A"
//     'object' =>
//     class A#1 (0) {
//     }
//     'type' =>
//     string(2) "->"
//     'args' =>
//     array(0) {
//     }
//   }
//   [1] =>
//   array(7) {
//     'file' =>
//String (93) "/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP"
//     'line' =>
//     int(95)
//     'function' =>
//     string(6) "test_a"
//     'class' =>
//     string(1) "A"
//     'object' =>
//     class A#1 (0) {
//     }
//     'type' =>
//     string(2) "->"
//     'args' =>
//     array(0) {
//     }
//   }
// }

When used in a class, an object field will be added to the array item to display the information of the class where the method is located.

debug_ The function declaration of backtrace() is:

debug_backtrace ([ int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT [, int $limit = 0 ]] ) : array

Where \ $options are two constants that can be defined, debug_ BACKTRACE_ PROVIDE_ Object indicates whether to fill in the index of “object”; DEBUG_ BACKTRACE_ IGNORE_ Whether args ignores the index of “args”, including all function / method parameters, can save memory overhead$ Limits can be used to limit the number of stack frames returned. The default is 0 to return all stacks.

debug_ Backtrace () and debug described below_ print_ The backtrace () method supports the code in the require / include file and eval (). When embedding the file, the path of the embedded file will be output. You can try it yourself.

debug_print_backtrace()

This method can also be seen from its name. It will directly print the backtracking content, its function declaration and debug_ Backtrace () is the same, but $options defaults to debug_ BACKTRACE_ IGNORE_ Args, that is, it only prints the file where the call is located and the number of lines.

function a() {
    b();
}

function b() {
    c();
}

function c(){
    debug_print_backtrace();
}

a();

#0 C () called at [/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP: 144]
#1 b) called at [/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP: 140]
#2 a) called at [/ users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / PHP print trace debugging information. PHP: 151]

In addition, this function does not need to use var_ Dump() or print_ R () for output, and directly use this function for output. It can be very fast and convenient for us to debug. For example, in large frameworks such as laravel, we can use debug when the controller needs to view stack information_ print_ Backtrace () quickly looks at the current stack call. And debug_ If backtrace() does not specify $options, it will occupy a very large memory capacity or cannot be displayed completely.

summary

The two functions introduced today can flexibly help us debug code or understand the call of a framework. Of course, in the formal case, it is recommended to use Xdebug and editor support for breakpoint debugging, because debug is used_ With the two methods of backtrace (), we can’t see the changes of variables.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/PHP%E6%89%93%E5%8D%B0%E8%B7%9F%E8%B8%AA%E8%B0%83%E8%AF%95%E4%BF%A1%E6%81%AF.php

Reference documents:
https://www.php.net/manual/zh/function.debug-backtrace.php

https://www.php.net/manual/zh/function.debug-print-backtrace.php

Each media platform can search [hard core project manager]

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]