using gdb to get the c level backtrace of a ruby app

I suppose one way would be to fork the current process, then have gdb attach to it and call “backtrace” then unattach.

here is the example, in linux:

(gdb) bt
#3 0x0815e92b in vm_call_method (th=0x8350480, cfp=0xb7b66ee8, num=1,
blockptr=0x1, flag=8, id=7688, mn=0x82717b4, recv=136505600)
at vm_insnhelper.c:379
#4 0x08150eee in vm_exec_core (th=0x8350480, initial=<value optimized out>)
at insns.def:1000
#5 0x08157c82 in vm_exec (th=0x8350480) at vm.c:1079
#6 0x08159326 in loop_i () at vm.c:524

ruby 1.9.2 has the possibility of a more verbose debug output, too (somehow)– you might be able to tie into that.
ruby 1.9.2dev (2009-05-21) [i386-mingw32]

— control frame ———-
c:0015 p:—- s:0073 b:0073 l:000072 d:000072 CFUNC :join
c:0014 p:0286 s:0069 b:0069 l:00070c d:00070c METHOD c:/dev/ruby/downloads/ruby_trunk/test/ruby/envutil.rb:133
c:0013 p:0093 s:0048 b:0048 l:000047 d:000047 METHOD c:/dev/ruby/downloads/ruby_trunk/test/ruby/test_exception.rb:217

—————————
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:333:in `block in autorun’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:392:in `run’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:412:in `run_test_suites’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:412:in `each’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:413:in `block in run_test_suites’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:413:in `each’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:420:in `block (2 levels) in run_test_suites’
c:/dev/ruby/downloads/ruby_trunk/lib/minitest/unit.rb:441:in `run’
c:/dev/ruby/downloads/ruby_trunk/test/ruby/test_exception.rb:217:in `test_errat’
c:/dev/ruby/downloads/ruby_trunk/test/ruby/envutil.rb:133:in `assert_in_out_err’
c:/dev/ruby/downloads/ruby_trunk/test/ruby/envutil.rb:133:in `join’

Unfortunately, in mingw I can’t seem to get gdb attach to print out *anything.*

4 thoughts on “using gdb to get the c level backtrace of a ruby app

  1. also useful might be:

    thread apply all backtrace

    which seems to work on mingw, instead of getting:
    eval(“caller”)
    gdb: unknown target exception 0xc0000027 at 0x77c35464

    Program received signal ?, Unknown signal.
    0x77c35464 in msvcrt!_global_unwind2 () from C:\WINDOWS\system32\msvcrt.dll
    The program being debugged was signaled while in a function called from GDB.
    GDB has restored the context to what it was before the call.
    To change this behavior use “set unwindonsignal off”
    Evaluation of the expression containing the function (rb_p) will be abandoned.

  2. (gdb) eval(“caller”)
    Undefined command: “eval”. Try “help”.

    meant “you need to run source some_file_name.txt nobu’s file”

  3. here’s how it *didn’t* work with mingw (even when run from within gdb, not attached, so that’s possibly not the root problem)

    (gdb) eval(“caller”)
    (eval):1: [BUG] cfp consistency error – send
    ruby 1.9.2dev (2009-05-25 trunk 23567) [i386-mingw32]

    — control frame ———-
    c:0009 p:0009 s:0021 b:0021 l:001aec d:000020 EVAL (eval):1
    c:0008 p:—- s:0019 b:0019 l:000018 d:000018 FINISH
    c:0007 p:—- s:0017 b:0017 l:000016 d:000016 CFUNC :sleep
    c:0006 p:0011 s:0013 b:0013 l:001aec d:000012 BLOCK d:freeze_several_threads.rb:4
    c:0005 p:—- s:0011 b:0011 l:000010 d:000010 FINISH
    c:0004 p:—- s:0009 b:0009 l:000008 d:000008 CFUNC :loop
    c:0003 p:0009 s:0006 b:0006 l:001aec d:000005 BLOCK d:freeze_several_threads.rb:4
    c:0002 p:—- s:0004 b:0004 l:000003 d:000003 FINISH
    c:0001 p:—- s:0002 b:0002 l:000001 d:000001 TOP
    —————————
    d:freeze_several_threads.rb:4:in `block (2 levels) in


    d:freeze_several_threads.rb:4:in `loop’
    d:freeze_several_threads.rb:4:in `block (3 levels) in

    d:freeze_several_threads.rb:4:in `sleep’
    (eval):1:in `block (3 levels) in

    [NOTE]
    You may have encountered a bug in the Ruby interpreter or extension libraries.
    Bug reports are welcome.
    For details: http://www.ruby-lang.org/bugreport.html

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application’s support team for more information.

    Program exited with code 03.
    The program being debugged stopped while in a function called from GDB.
    When the function (rb_eval_string_protect) is done executing, GDB will silently
    stop (instead of continuing to evaluate the expression containing
    the function call).

  4. looks like even doing this in linux can cause seg faults, though….maybe because I’m applying it to all threads or what not.

    (eval):1: [BUG] Segmentation fault
    ruby 1.9.2dev (2009-05-29 trunk 23606) [i686-linux]

    — control frame ———-
    c:0011 p:0009 s:0025 b:0025 l:00021c d:000024 EVAL (eval):1
    c:0010 p:—- s:0023 b:0023 l:000022 d:000022 FINISH
    c:0009 p:0009 s:0021 b:0021 l:00021c d:000020 EVAL (eval):1
    c:0008 p:—- s:0019 b:0019 l:000018 d:000018 FINISH
    c:0007 p:—- s:0017 b:0017 l:000016 d:000016 CFUNC :sleep
    c:0006 p:0011 s:0013 b:0013 l:00021c d:000012 BLOCK freeze_several_threads.rb:4
    c:0005 p:—- s:0011 b:0011 l:000010 d:000010 FINISH
    c:0004 p:—- s:0009 b:0009 l:000008 d:000008 CFUNC :loop
    c:0003 p:0009 s:0006 b:0006 l:00021c d:000005 BLOCK freeze_several_threads.rb:4
    c:0002 p:—- s:0004 b:0004 l:000003 d:000003 FINISH
    c:0001 p:—- s:0002 b:0002 l:000001 d:000001 TOP
    —————————
    freeze_several_threads.rb:4:in `block (2 levels) in


    freeze_several_threads.rb:4:in `loop’
    freeze_several_threads.rb:4:in `block (3 levels) in

    freeze_several_threads.rb:4:in `sleep’
    (eval):1:in `block (3 levels) in

    (eval):1:in `block (3 levels) in

    — C level backtrace information ——————————————-
    ./ruby(rb_vm_bugreport+0xb5) [0x8160525]
    ./ruby [0x819b43b]
    ./ruby(rb_bug+0x28) [0x819b4c8]
    ./ruby [0x80f5545]
    [0xb7fe4440]
    [0xb7ce5fac]

    [NOTE]
    You may have encountered a bug in the Ruby interpreter or extension libraries.
    Bug reports are welcome.
    For details: http://www.ruby-lang.org/bugreport.html

    Aborted

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + 4 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>