Q: How to get backtrace of another thread?

R

ruby-lang

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.
 
J

Joel VanderWerf

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

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top