-->

如何配置红宝石就按Ctrl-C(SIGINT)进入调试?(How do I configure ru

2019-07-21 04:28发布

我想在按下Ctrl-C(或发送SIGINT)进入调试器。 我已经安装调试器 (我运行的Ruby 1.9.3),并验证了它的工作原理。 我把这个添加到我的设置文件(这是Padrino,但我相信它会为Rails类似):

# file: config/boot.rb
Padrino.before_load do
  trap("SIGINT") { debugger } if Padrino.env == :development
end

...但按Ctrl-C不调用调试器。 事实上,如果我更换debuggerputs "saw an interrupt!" ,按Ctrl-C不会导致打印到发生。

更新

下面这个建议从麦克Dunlavey ,我尝试显式调用catch Interrupt在调试器中:

$ rdebug `which padrino` console
^Z^Z$HOME/usr/bin/padrino:9
require 'rubygems'
(rdb:1) catch Interrupt
Catch exception Interrupt.
(rdb:1) c
=> Loading development console (Padrino v.0.10.7)
=> Loading Application BlueDotAe
=> Loading Application Admin
irb(main):001:0>   C-c C-c^C
irb(main):001:0> 

没有快乐 - 中断没有进入调试器。

我在想什么?

Answer 1:

如果你想捕获SIGINT在控制台运行时,简单的答案是:你不能,除非你猴子补丁IRB。 每个Ruby应用程序(无论是padrino或者轨道或诸如此类的东西),使用控制台将最终调用usr/lib/ruby/1.9.1/irb.rb ,并在IRB.start ,它的作用:

trap("SIGINT") do
  irb.signal_handle
end

......刚刚进入主循环之前。 这将覆盖任何陷阱(“SIGINT”),你可能已经把你的启动代码。

但是如果你想捕获SIGINT在脚本文件(例如,如果要分析所描述的代码由Mike Dunlavey这里 ),你可以创建一个脚本文件如:

# File: profile_complex_operation.rb
trap("SIGINT") { debugger }
MyApp.complex_operation

然后调用它,如:

$ ruby profile_complex_operation.rb

现在,当你打^ C(或发送从另一个进程SIGINT),将进入调试器。



Answer 2:

您可以尝试使用GDB的包装为Ruby ( GitHub的 )。

通过安装在Linux上:

sudo apt-get install gdb python-dev ncurses-dev ruby-rvm
gem install gdb.rb

基本用法:

require 'gdb'

# create a new GDB::Ruby instance and attach it to
# pid 12345
gdb = GDB::Ruby.new(12345)

# print the (ruby) backtrace of the remote process
gdb.backtrace.each { |line| puts line }

# show the current local variables, and their values
p gdb.local_variables

# evaluate arbitrary ruby code in the remote process
p gdb.eval('%(pid #{$$})')

# show how many instances of each class exist in the
# remote process
p gdb.object_space

# raise an exception in the remote process
gdb.raise Exception, "go boom!"

# close the connection to the remote process
gdb.quit

或调试挂起进程,通过将其附加:

rvmsudo gdb.rb PID

然后:

# in gdb get a ruby stacktrace with file names and line numbers
# here I'm filtering by files that are actually in my app dir
(gdb) ruby eval caller.select{|l| l =~ /app\//}

来源: 使用gdb来检查挂起的Ruby进程

一些备选方案:

  • rbtrace -像strace的,但对于红宝石代码(用法: rbtrace -p <PID> --firehose )。
  • debug.rb脚本由TMM1(gdb.rb的作者),它可以帮助调试使用strace的/ GDB的过程。

也可以看看:

  • 调试的Ruby工具
  • 检查为什么Ruby脚本挂起


文章来源: How do I configure ruby to enter the debugger on Ctrl-C (SIGINT)?