# Q: How to get backtrace of another thread?

**URL:** <https://rubytalk.org/t/q-how-to-get-backtrace-of-another-thread/21385>\
**Category:** ruby-talk\
**Created:** [6 October 2005 19:56 UTC](https://rubytalk.org/t/q-how-to-get-backtrace-of-another-thread/21385 "2005-10-06T19:56:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rutger\_Nijlunsing](https://avatars.discourse-cdn.com/v4/letter/r/aeb1de/32.png) [@Rutger\_Nijlunsing](https://rubytalk.org/u/Rutger_Nijlunsing)\
**Post date:** [6 October 2005 19:56 UTC](https://rubytalk.org/t/q-how-to-get-backtrace-of-another-thread/21385/1 "2005-10-06T19:56:07Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;otherThread.raise("Gotcha!")  
&nbsp;&nbsp;rescue RuntimeError  
&nbsp;&nbsp;&nbsp;&nbsp;\<do an eval in the otherThread (but how?)\>  
&nbsp;&nbsp;&nbsp;&nbsp;otherThread.resume  
&nbsp;&nbsp;end  
&nbsp;&nbsp;...raises the exception in otherThread, not the calling thread.

- Kernel.set\_trace\_func() .  
&nbsp;&nbsp;...doesn't seem to keep track of Threads(?).  
&nbsp;&nbsp;...doesn't say on which function it is blocking.

Regards,  
Rutger.

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

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [7 October 2005 04:13 UTC](https://rubytalk.org/t/q-how-to-get-backtrace-of-another-thread/21385/2 "2005-10-07T04:13:12Z")

</div>

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  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;rescue Exeption =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;puts e.backtrace  
&nbsp;&nbsp;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  
&nbsp;&nbsp;puts backtrace  
end

with the obvious meaning...

> **···**
>
> ruby-lang@wingding.demon.nl wrote:
> 
> > How do I get a backtrace of another (possibly running) Thread?
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
