Printing PHP executing a call stack from outside of PHP

Is there any stack trace function in PHP using this PID? (For anyone also writing Java, I mean jstack.)

I got some background PHP processes that they freeze from time to time on some unknown lines. I can just kill them all and restart, but that will not stop the event from repeating.

Is there an API capable of spying on the stack and telling? How is the jstack utility provided by the JDK?

+2
source share
1 answer

You have several options in terms of debugging unknown errors.

  • The first method is simple, but it requires a PHP extension.
  • The second method offers a more complex practical approach, but is definitely worth it if you are in the low-level internal workings of the PHP interpreter. In addition, you will need to configure linux and PHP with --enable-debug .
  • The third and, in my opinion, the easiest way does not require the use of any external programs or added PHP extensions.

  • Xdebug

    • This is a PHP extension that you can install and is available for any site / page.
    • A quick list of features from the site:
      • Auto stack trace on error
      • Function Call Logging
      • Display functions such as var_dump () extended output and code coverage information.
    • If you see that you need to debug scripts many times, this might be the best option. A prettier display of errors is also a good thumb up in my book.

  • Use gdb to run a file that splits and parses backtracking.

    • This is an intermediate-advanced approach that requires PHP to be configured with --enable-debug , linux machine Apache and a strong desire / ability to understand how the software works at a lower level.
    • Launch gdb using Apache :
      gdb /usr/lib/httpd
    • Then you have two options:
      • Launch apache as a server and upload the file through the browser, as usual:
        (gdb) run -X
        ... now in your browser - access the page that crashes and switch to gdb : (gdb) backtrace
        ... full printout will be printed
      • Or use gdb to run the script itself:
        (gdb) run /path/to/the/script.php
        (gdb) backtrace
        ... full printout will be printed
    • For more information on gdb check out the quick reference guide .

  • Create a custom error handler that prints a stack trace on error.

    • To be sure to catch all errors, you can catch PHP errors, exceptions, and completion events.
    • To debug a single page, it is as simple as creating a function to handle the error, and then pasting that function to the top of the page you are facing. One more step, I have an error handling class that is contained in a separate file and just include it if necessary (simplified version below). You can customize each method according to your display needs or even handle errors based on the number of errors.
    • Then, to use this, just add require('ErrorHandler.php'); to the top of the page, and he should automatically register to handle any errors. Be sure to update the include-path to point to the actual file, of course.

ErrorHandler.php:

 <?php class ErrorHandler { public static function captureError($err_no, $message, $file, $line) { echo '<strong>Error (#' . $err_no . '):</strong> ' . $message . ' in ' . $file . ' on line #' . $line . '<br />'; debug_print_backtrace(); } public static function captureException($exception) { echo '<pre>' . print_r($exception, true) . '</pre>'; } public static function captureShutdown() { if (($error = error_get_last()) !== null) { debug_print_backtrace(); } } } set_error_handler(array('ErrorHandler', 'captureError')); set_exception_handler(array('ErrorHandler', 'captureException')); register_shutdown_function(array('ErrorHandler', 'captureShutdown')); ?> 
0
source

Source: https://habr.com/ru/post/1437221/


All Articles