您如何调试长时间运行的php脚本?

问题描述:

问题在于该脚本会在很长一段时间后挂断. strace 返回类似这样的内容:

The problem is that the script hangs up after some long time. strace returns something like this and nothing else:

Process 7286 attached - interrupt to quit
restart_syscall(<... resuming interrupted call ...>) = 0
poll([{fd=13, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 0 (Timeout)
clock_gettime(CLOCK_MONOTONIC, {1817569, 74651533}) = 0
clock_gettime(CLOCK_MONOTONIC, {1817569, 74734744}) = 0
clock_gettime(CLOCK_MONOTONIC, {1817569, 74812047}) = 0
poll([{fd=13, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 1000) = 0 (Timeout)
poll([{fd=13, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 0 (Timeout)
....

在这里放置调试消息是最后的选择.

Putting here'n'there debug messages is left as a last resort..

我可以在连接了 xdebug 的情况下运行脚本,但是有没有一种方法可以向php进程发送一些POSIX信号,从而触发xdebug来转储当前的context/stacktrace/localvars?

I can run the script with xdebug attached but is there a way to send some POSIX signal to php process which would trigger xdebug to dump current context/stacktrace/localvars?

是否可以获取php脚本的事后转储"?

Is it possible to get 'postmortem dump' of php script?

Xdebug没有为此提供功能.使用pcntl_signal然后使用xdebug_print_function_stack是一个选项,但是您也可以做的是创建函数跟踪".这使得Xdebug会在每个函数调用发生时立即将其写入磁盘.这应该能够告诉您脚本的挂起位置.从我可以看到的是,这正在等待IO.

Xdebug does not have functionality for this. Using pcntl_signal and then using xdebug_print_function_stack is an option, but what you can also do is to create a "function trace". This is something that makes Xdebug write every function call to disk as soon as they happen. This should be able to tell you where your script hangs. From what I can see though is that is waiting for IO.

有关函数跟踪的更多信息,请参见 http://www.xdebug.org/docs/execution_trace

There is more information about function traces at http://www.xdebug.org/docs/execution_trace