获取挂起的PHP脚本的堆栈跟踪

Cor*_*rdi 9 php

我有一个脚本,每晚从一个cron工作.最近,它在剧本几分钟后开始完全冻结,我无法弄清楚原因.如果这是Java,我可以简单地运行kill -3 PID它会在stdout中打印一个线程转储.在PHP中是否有任何等价物,我可以在正在运行的PHP脚本上获取当前堆栈跟踪(以及理想情况下的内存信息)的转储?

joh*_*nes 7

你可以做的最好的事情就是--enable-debug在使用期间自己编译PHP configure.如果该过程仍然挂起,您可以使用gdb和一些宏来使用以下步骤获取PHP级别的堆栈跟踪:

$ gdb -p $PHP_PID
(gdb) bt     # Get a system-level stacktrace, might already give some info
(gdb) source /path/to/php-src/.gdbinit # Load some useful macros
(gdb) dump_bt executor_globals.current_execute_data
            # Macro from PHP's .gbinit giving PHP stack trace
            # If you for whatever reason are using a thread-safe PHP build you have to do this:
(gdb) ____executor_globals
(gdb) dump_bt $eg.current_execute_data
Run Code Online (Sandbox Code Playgroud)

然后调试:-)

请注意,要使其工作,您必须拥有带符号信息的PHP二进制文件,以--enable-debug确保这一点.