Debugging Ruby Seg Faults

Ever been editing a ruby script that talks to a C library and the whole damn thing crashes saying “Illegal instruction” or “Bus error” or some other such nonhelpful message, with of course, no line number.

At this point, to figure out what the hell happened, you could break out your trusty “debug by printf’ing” strategy from your C days – OR - you could cheat.

What I do is put this line in my program at any point that I know for sure still executes :

    require 'Debugger'

and that’s it, next time I run my program I get a ton of messages like this :
turning on debugger
in ./chizzle/FileTreeView.rb:
return:29
in ./chizzle/Debugger.rb:
end:1
in ./chizzle/FileTreeView.rb:
line:30
call:157
line:158
c-call:158
c-call:158

the last line that my program was at before the seg fault is the culprit, simple as that.
——The Script——
What’s really cool is how simple this was to do in ruby. I think it took me about 10 minutes to write, and has since saved me countless hours. Let’s take a look at Debugger.rb :

    class Debugger
        def Debugger.turn_on
            puts 'turning on debugger'
 
            old_filename = ''
            set_trace_func(proc {|event_name, filename, line_number, object_id, binding, class_name|
                if filename != old_filename
                    puts "in #{filename}:"
                    old_filename = filename
                end
                puts "\t#{event_name}:#{line_number}"
            })
        end
    end
 
    Debugger.turn_on

Tags:

Leave a Reply