How do I print a ruby ​​1.9 runtime stack trace?

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

EDIT: I'm interested in using an external tool to create a stack trace (doesn't work in the same memory space as the Ruby process).

As @mosch noted, the Kernal#caller method works as part of an ongoing Ruby process.

You can even create support for your Ruby code that processes process signals and prints a stack trace:

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

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

I could create this functionality in my code, but I would prefer to use a more generalized external solution. Thanks.

+7
source share
2 answers

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

+3
source

Today I came up with an idea. Ice799's pstack used gdb to call the ruby ​​internal function to reset the backtrace. However, it did not work at 1.9 and demanded a patch. This is not so convenient.

With luck, we hotpatch this powerful tool. It can inject .so into the current process, we can add the necessary function through .so injections, pstack will do the rest. Injectable, of course. They should also be binary, compatible with a working ruby ​​core, but we can assume that this part is very stable now.

0
source

All Articles