1

I have a ruby script, apparently correct, that sometimes stops working (probably on some calls to Postgresql through the pg gem). The problem is that it freezes but doesn't produce any error, so I can't see the line number and I always have to isolate the line by using puts "ok1", puts "ok2", etc. and see where the script stops.

Is there any better way to see the current line being executed (without changing the script)? And maybe the current stack?

collimarco
  • 34,231
  • 36
  • 108
  • 142

1 Answers1

1

You could investigate ruby-debug a project that has been rewritten several times for several different versions of ruby, should allow you to step through your code line by line. I personally prefer printf debugging in a lot of cases though. Also, if I had to take an absolutely random guess at your problem, I might investigate whether or not you're running into a race condition and/or deadlock in your DB.

user12341234
  • 6,573
  • 6
  • 23
  • 48
  • I've already considered `ruby-debug`, but unfortunately there's no way to see the current line being executed while a script is running. The only way would be to set a breakpoint somewhere, but this is not what I want. I simply want to run the script and at some time print the current line being executed, whatever it is. – collimarco May 09 '15 at 12:16
  • 2
    What I'm about to suggest is terrifying, but desperate times and all that... What about some combination of the following: http://stackoverflow.com/a/3236342/3277393 and http://stackoverflow.com/a/12175847/3277393 . You can create a hook into every method call (because almost everything in ruby is a method call) and print the line number once it starts executing. Since this is like drinking from a fire hose, you could also consider filtering the output to only display line numbers from the source file you're interested in. – user12341234 May 09 '15 at 12:23
  • The suggestion of using `set_trace_func` from prior comment worked for me. Dumped every call that was executing intermixed with strace output of every system call that the process was doing and managed to track down the offending lines of code. It ain't stupid if it works. – lamont Feb 07 '22 at 22:44