Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 584
Will Sulzer Avatar asked Dec 05 '11 06:12

Will Sulzer


1 Answers

Whenever you call the Kernel method caller, you will get the current call stack as an Array.

like image 109
moritz Avatar answered Sep 25 '22 10:09

moritz