4 ms·
Anybody knows if it's possible in Ruby? Just being able to have a live irb from a process ID would be pretty awesome
by dorianm 10y ago
Anybody knows if it's possible in Ruby? Just being able to have a live irb from a process ID would be pretty awesome
- jkm3141 10y agoWhile not exactly the same it's still quite useful. https://github.com/Mon-Ouie/pry-remote https://github.com/Mon-Ouie/pry-remote
- dorianm 10y agoThanks, I guess I could just have some special key that triggers pry-remote (E.g trap ctrl-D or something)
- vidarh 10y ago"sudo gdb -p [process id]" will attach to the running process. If you attach that to a Ruby process, you can do: call rb_eval_string("puts 'hi from the Ruby process'") (The latter will output to the ruby scripts STDOUT so if you can't see the stdout from the Ruby interpreter, it won't produce anything - test it by just starting a ruby interpreter and find it's pid) You can use that to (try to) load code. For example try loading pry and setting up a pry-remote connection. You can also use any other part of the MRI extension API - gdb can do quite complicated calls. Note that any uncaught error may easily cause the Ruby script you're connected to to terminate, and there are plenty of opportunities to cause problems since when you're connecting gdp to the process, the Ruby interpreter will be paused at an arbitrary point, which may leave the interpreter in an inconsistent state. To wrap the above up into anything reasonable, you'd want to first ensure the interpreter is in a decent state by setting a breakpoint somewhere "sane" in the interpreter, continue execution, and then execute some very carefully crafted code to let you inject the code you need without triggering any errors that mess up for the script, or changing variables etc. the script depends on. It probably could be wrapped up into something quite nice combined with pry/pry-remote, though.
- dorianm 10y agoThanks, I will probably make a wrapper gem around that (e.g. maybe just a `call rb_eval_string ("binding.pry")`). In my case I get "No symbol "rb_backtrace" in current context.", I will look deeper into it. Also, there is a great gist about Ruby GDB debugging: https://gist.github.com/mmullis/6211061 https://gist.github.com/mmullis/6211061.