Q: How to get backtrace of another thread?

How do I get a backtrace of another (possibly running) Thread?

I want to get an idea of what is blocking in other threads, and
therefore want to periodically dump a list of threads with their
backtraces to get a feeling what they're up to.

Ideas which did not work:

- begin
    otherThread.raise("Gotcha!")
  rescue RuntimeError
    <do an eval in the otherThread (but how?)>
    otherThread.resume
  end
  ...raises the exception in otherThread, not the calling thread.

- Kernel.set_trace_func() .
  ...doesn't seem to keep track of Threads(?).
  ...doesn't say on which function it is blocking.

Regards,
Rutger.

···

--
Rutger Nijlunsing ----------------------------------------------------
never attribute to a conspiracy which can be explained by incompetence
----------------------------------------------------------------------

I was asking the same question just a few days ago, but I didn't come up
with anything either.

The otherThread.raise approach isn't completely useless. It's
destructive, but it will tell you what the thread is doing (or what it
was doing before going to sleep). You just have to make sure to catch
the exception in otherThread's outermost block (or somewhere):

otherThread = Thread.new do
  begin
    # ...
  rescue Exeption => e
    puts e.backtrace
  end
end

Any reason why it would be hard (in terms of the ruby thread
implementation) to take a "snapshot" of another thread's backtrace?

It would be nice to have something like

otherThread.trap MyTestException do
  puts backtrace
end

with the obvious meaning...

···

ruby-lang@wingding.demon.nl wrote:

How do I get a backtrace of another (possibly running) Thread?

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407