# Getting gdb backtrace from ruby exception

**URL:** <https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038>\
**Category:** ruby-talk\
**Created:** [12 July 2007 14:10 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038 "2007-07-12T14:10:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Fenton](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Alex\_Fenton](https://rubytalk.org/u/Alex_Fenton)\
**Post date:** [12 July 2007 14:10 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/1 "2007-07-12T14:10:01Z")

</div>

Hi

I'm working on a C extension and somewhere in itsomething is raising a NoMethodError. The exception is reported with the line of ruby code that triggered the bad call in the C extension.

As the C code is large and complex (it's a big SWIG project), I'm having trouble finding the problem in the C code. I would use gdb or Microsoft's debugger to look at the stack if it was a C error - but when a Ruby exception is raised, the process exits and there's no stack to inspect.

Ruby's debugger isn't helpful because it only knows about ruby written in ruby.

Is there any way I can get at the C stack at the point of ruby 'raise' from a C extension?

Thanks  
alex

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [12 July 2007 14:26 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/2 "2007-07-12T14:26:34Z")

</div>

There was a way how to run ruby under gdb with the ability to debug at  
"ruby level" while still having the "C view" at hand.

[http://eigenclass.org/hiki.rb?ruby+live+process+introspection](http://eigenclass.org/hiki.rb?ruby+live+process+introspection)  
[http://weblog.jamisbuck.org/2006/9/25/gdb-wrapper-for-ruby](http://weblog.jamisbuck.org/2006/9/25/gdb-wrapper-for-ruby)  
[http://redhanded.hobix.com/inspect/theRubyGdbArmsRaceNowAtAStandoff.html](http://redhanded.hobix.com/inspect/theRubyGdbArmsRaceNowAtAStandoff.html)

J.

> **···**
>
> On 7/12/07, Alex Fenton \<aff28@deleteme.cam.ac.uk\> wrote:
> 
> > Hi
> > 
> > I'm working on a C extension and somewhere in itsomething is raising a  
> > NoMethodError. The exception is reported with the line of ruby code that  
> > triggered the bad call in the C extension.
> > 
> > As the C code is large and complex (it's a big SWIG project), I'm having  
> > trouble finding the problem in the C code. I would use gdb or  
> > Microsoft's debugger to look at the stack if it was a C error - but when  
> > a Ruby exception is raised, the process exits and there's no stack to  
> > inspect.
> > 
> > Ruby's debugger isn't helpful because it only knows about ruby written  
> > in ruby.
> > 
> > Is there any way I can get at the C stack at the point of ruby 'raise'  
> > from a C extension?
> > 
> > Thanks  
> > alex

---

<div class="post-metadata">

**Author:** ![Detlef\_Reichl](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Detlef\_Reichl](https://rubytalk.org/u/Detlef_Reichl)\
**Post date:** [12 July 2007 14:27 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/3 "2007-07-12T14:27:29Z")

</div>

Hi,

i don't know how to build stack traces, but you can start ruby in gdb.

first call gdb with ruby as argument. On liux it looks like:

&nbsp;&nbsp;det@datengrab:~/Desktop$ gdb /usr/bin/ruby

and then in gdb start your ruby program with "run"

&nbsp;&nbsp;(gdb) run ./my-prg.rb

Cheers  
detlef

> **···**
>
> Am Donnerstag, den 12.07.2007, 23:10 +0900 schrieb Alex Fenton:
> 
> > Hi
> > 
> > I'm working on a C extension and somewhere in itsomething is raising a  
> > NoMethodError. The exception is reported with the line of ruby code that  
> > triggered the bad call in the C extension.
> > 
> > As the C code is large and complex (it's a big SWIG project), I'm having  
> > trouble finding the problem in the C code. I would use gdb or  
> > Microsoft's debugger to look at the stack if it was a C error - but when  
> > a Ruby exception is raised, the process exits and there's no stack to  
> > inspect.
> > 
> > Ruby's debugger isn't helpful because it only knows about ruby written  
> > in ruby.
> > 
> > Is there any way I can get at the C stack at the point of ruby 'raise'  
> > \>from a C extension?
> > 
> > Thanks  
> > alex

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [12 July 2007 21:00 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/4 "2007-07-12T21:00:42Z")

</div>

cfp:~ \> cat core.rb  
BEGIN{ at\_exit{ Process.kill 'SIGSEGV', Process.pid if $! } }

cfp:~ \> cat a.rb  
#! /usr/bin/env ruby  
require 'core'  
p 42  
cfp:~ \> ruby a.rb  
42

cfp:~ \> cat b.rb  
#! /usr/bin/env ruby  
require 'core'  
raise

cfp:~ \> ruby b.rb  
./core.rb:1: [BUG] Segmentation fault  
ruby 1.8.6 (2007-03-13) [i686-darwin8.9.1]

Abort trap (core dumped)

-a

> **···**
>
> On Jul 12, 2007, at 8:10 AM, Alex Fenton wrote:
> 
> > Hi
> > 
> > I'm working on a C extension and somewhere in itsomething is raising a NoMethodError. The exception is reported with the line of ruby code that triggered the bad call in the C extension.
> > 
> > As the C code is large and complex (it's a big SWIG project), I'm having trouble finding the problem in the C code. I would use gdb or Microsoft's debugger to look at the stack if it was a C error - but when a Ruby exception is raised, the process exits and there's no stack to inspect.
> > 
> > Ruby's debugger isn't helpful because it only knows about ruby written in ruby.
> > 
> > Is there any way I can get at the C stack at the point of ruby 'raise' from a C extension?
> > 
> > Thanks  
> > alex
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Alex\_Fenton2](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Alex\_Fenton2](https://rubytalk.org/u/Alex_Fenton2)\
**Post date:** [12 July 2007 20:15 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/5 "2007-07-12T20:15:00Z")

</div>

Jano Svitok wrote:

> > Is there any way I can get at the C stack at the point of ruby 'raise'  
> > from a C extension?

> There was a way how to run ruby under gdb with the ability to debug at  
> "ruby level" while still having the "C view" at hand.
> 
> [http://eigenclass.org/hiki.rb?ruby+live+process+introspection](http://eigenclass.org/hiki.rb?ruby+live+process+introspection)  
> [Buckblog: GDB wrapper for Ruby](http://weblog.jamisbuck.org/2006/9/25/gdb-wrapper-for-ruby)  
> [http://redhanded.hobix.com/inspect/theRubyGdbArmsRaceNowAtAStandoff.html](http://redhanded.hobix.com/inspect/theRubyGdbArmsRaceNowAtAStandoff.html)

Thanks for the pointer - that's really interesting stuff. I got round it a bit more prosaically by simply setting a breakpoint in gdb where ruby calls the exception raising class

(gdb) b rb\_exc\_raise  
Breakpoint 1, rb\_exc\_raise (mesg=3057353380) at eval.c:4583  
4583 rb\_longjmp(TAG\_RAISE, mesg);  
(gdb) whe

.... tasty stacktrace

cheers  
alex

> **···**
>
> > On 7/12/07, Alex Fenton \<aff28@deleteme.cam.ac.uk\> wrote:

---

<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:** [12 July 2007 22:25 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/6 "2007-07-12T22:25:40Z")

</div>

ara.t.howard wrote:

> > Hi
> > 
> > I'm working on a C extension and somewhere in itsomething is raising a NoMethodError. The exception is reported with the line of ruby code that triggered the bad call in the C extension.
> > 
> > As the C code is large and complex (it's a big SWIG project), I'm having trouble finding the problem in the C code. I would use gdb or Microsoft's debugger to look at the stack if it was a C error - but when a Ruby exception is raised, the process exits and there's no stack to inspect.
> > 
> > Ruby's debugger isn't helpful because it only knows about ruby written in ruby.
> > 
> > Is there any way I can get at the C stack at the point of ruby 'raise' from a C extension?
> > 
> > Thanks  
> > alex
> 
> cfp:~ \> cat core.rb  
> BEGIN{ at\_exit{ Process.kill 'SIGSEGV', Process.pid if $! } }
> 
> cfp:~ \> cat a.rb  
> #! /usr/bin/env ruby  
> require 'core'  
> p 42  
> cfp:~ \> ruby a.rb  
> 42
> 
> cfp:~ \> cat b.rb  
> #! /usr/bin/env ruby  
> require 'core'  
> raise
> 
> cfp:~ \> ruby b.rb  
> ./core.rb:1: [BUG] Segmentation fault  
> ruby 1.8.6 (2007-03-13) [i686-darwin8.9.1]
> 
> Abort trap (core dumped)

Doesn't that only give you the C stack at the time the ruby process exits, not at the time the exception is raised?

> **···**
>
> > On Jul 12, 2007, at 8:10 AM, Alex Fenton wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [12 July 2007 23:31 UTC](https://rubytalk.org/t/getting-gdb-backtrace-from-ruby-exception/39038/7 "2007-07-12T23:31:27Z")

</div>

yeah - it's a short leap though...

-a

> **···**
>
> On Jul 12, 2007, at 4:25 PM, Joel VanderWerf wrote:
> 
> > Doesn't that only give you the C stack at the time the ruby process exits, not at the time the exception is raised?
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama
