从 PHP 外部打印 PHP 运行调用堆栈

发布于 2024-12-19 04:48:01 字数 187 浏览 0 评论 0原文

PHP 中是否有通过给定 PID 进行运行时堆栈跟踪功能? (对于那些也写Java的人来说,我指的是jstack。)

我有几个PHP后台进程,它们偶尔会在一些未知的行上冻结。我可以简单地杀死他们并重新启动,但这并不能阻止它再次发生。

有没有一个 API 能够监视堆栈并告诉我们?就像 JDK 提供的 jstack 实用程序一样?

Is there any runtime stack trace feature in PHP, by a given PID of it?
(For whom also write Java, I mean jstack.)

I got few PHP background process that they are freeze once a while on some unknown lines. I can simply kill them all and restart but that don't prevent it happen again.

Is there a API able to spy the stack and tell? like the jstack utility provided from JDK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-12-26 04:48:01

在调试未知错误方面,您有几种选择。

  • 第一种方法很简单,但需要安装 PHP 扩展。
  • 第二种方法提供了更困难的实践方法,但如果您了解 PHP 解释器的低级内部工作原理,那么绝对值得。此外,这还需要使用 --enable-debug 配置 linux 和 PHP。
  • 第三种方法(我个人认为是最简单的方法)不需要使用任何外部程序或添加 PHP 扩展。

  1. Xdebug

    • 这是一个 PHP 扩展,您可以安装并可用于任何网站/页面。< /里>
    • 网站功能快速列表:
      • 出现错误时自动堆栈跟踪
      • 函数调用记录
      • 显示增强的 var_dump() 输出和代码覆盖率信息等功能。
    • 如果您发现自己需要经常调试脚本,那么这可能是最好的选择。在我的书中,更漂亮的错误显示也是一个很好的点赞。
  2. 使用gdb 运行崩溃的文件并分析回溯。

    • 这是一种中级到高级的方法,需要使用 --enable-debug 配置 PHP、运行 Apache 的 Linux 机器以及强烈的愿望/能够理解软件在较低级别上的工作方式。
    • 使用 Apache 运行 gdb
      gdb /usr/lib/httpd
    • 然后,您有两个选择:
      • 像平常一样运行 apache 作为服务器并通过浏览器加载文件:
        (gdb) 运行 -X
        ...现在,在您的浏览器中 - 正常访问崩溃的页面并切换回 gdb
        (gdb) 回溯
        ...将打印完整的回溯
      • 或者,使用 gdb 运行脚本本身:
        (gdb) 运行 /path/to/the/script.php
        (gdb) 回溯
        ...将打印完整的回溯
    • 有关更多 gdb 信息,请查看 快速参考指南
  3. 创建自定义错误处理程序 当抛出错误时打印堆栈跟踪。

    • 为了确保捕获所有错误,您可以捕获错误、异常和 PHP 的关闭事件。
    • 要调试单个页面,只需创建一个函数来处理错误,然后将该函数粘贴到出现问题的页面顶部即可。更进一步,我有一个“错误处理”类,它位于一个单独的文件中,只需在需要时包含它即可(下面的简化版本)。您可以调整每种方法以满足您的显示需求,甚至可以根据每个错误编号处理错误!
    • 然后,要使用它,只需将 require('ErrorHandler.php'); 添加到页面顶部,它就会自动注册以处理任何错误。当然,请务必更新包含路径以指向实际文件。

错误处理程序.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'));
?>

You have a few options in terms of debugging unknown errors.

  • The first method is easy, but requires a PHP extension to be installed.
  • The second method offers a more difficult hands-on approach, but is definitely worth it if you're into the low-level inner-workings of the PHP interpreter. Also, this will require linux and PHP to be configured with --enable-debug.
  • The third and, in my personal opinion the easiest, method doesn't require the use of any external programs or added PHP extensions.

  1. Xdebug

    • This is a PHP extension you can install and have available for any site/page.
    • A quick-list of features from the website:
      • Automatic stack trace upon error
      • Function call logging
      • Display features such as enhanced var_dump() output and code coverage information.
    • If you see yourself needing to debug your scripts a lot, this one might be the best option. The prettier-display of errors is also a good thumbs-up in my book.
  2. Use gdb to run the file that crashes and analyze the backtrace.

    • This is an intermediate-to-advanced approach that requires PHP to be configured with --enable-debug, a linux machine running Apache, and a strong desire/ability to understand the way software works on a lower-level.
    • Run gdb with Apache:
      gdb /usr/lib/httpd
    • Then, you have two options.:
      • Run apache as a server and load the file through a browser, as normal:
        (gdb) run -X
        ... now, in your browser - access the page that crashes as normal and switch back to gdb:
        (gdb) backtrace
        ... the full backtrace will be printed
      • Or, use gdb to run the script itself:
        (gdb) run /path/to/the/script.php
        (gdb) backtrace
        ... the full backtrace will be printed
    • For more gdb info, check out the quick-reference guide.
  3. Create a custom error handler that prints the stack trace when an error is thrown.

    • To be sure to catch all errors, you can catch Errors, Exceptions, and PHP's Shutdown events.
    • To debug a single page, it's as simple as creating a function to handle the error and then pasting that function into the top of the page you're having issues on. One step further, I have an "error handling" class that's in a separate file and just include it whenever needed (simplified-version below). You can tweak each method to suit your display needs, or even handle errors on a per-error-number basis!
    • Then, to use this, just add require('ErrorHandler.php'); to the top of your page and it should auto-register itself 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'));
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文