How can I print the runtime stack trace of a Ruby 1.9 process?

Is there any way to print the runtime stack trace of a Ruby 1.9.x process? I know that there was a utility called pstack for Ruby 1.8, but the project appears to have been abandoned a couple years ago: https://github.com/ice799/pstack. Does anything like this exist for Ruby 1.9? Thanks a lot!

EDIT: I'm interested in using an external tool to generate the stack trace (not running in the same memory space as the Ruby process).

As @mosch pointed out, the Kernal#caller method works from within the running Ruby process.

You could even build in support to your Ruby code that traps process signals and prints a stack trace:

Signal.trap("SIGTERM") { p caller }

Reference: http://www.ruby-doc.org/core-1.9.3/Signal.html

I could build this functionality into my code, but I'd prefer to use a more generalized, external solution. Thanks.


无论何时调用Kernel方法caller ,都会将当前调用堆栈作为一个Array。


I came up with an idea on this today. The pstack from ice799 took advantage of gdb to call a ruby internal function to dump the backtrace. However, it didn't work for 1.9 and required a patch. It's not such convenient.

With luck, we have hotpatch this powerful tool. It can inject a .so to a running process, we can add the required function via .so injection, pstack will do all the rest. Of course the injected .so need be binary compatible with the running ruby core, but we can assume this part is very stable now.

链接地址: http://www.djcxy.com/p/10152.html

上一篇: 替代基于Java / Spring的Web服务

下一篇: 我如何打印Ruby 1.9进程的运行时堆栈跟踪?