7

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.

0

2 Answers 2

3

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

1
  • Thanks @mosch. I've updated my description to add some clarification. Dec 5, 2011 at 18:32
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.