我想在键入 ctrl-C(或发送 SIGINT)后进入调试器。我有 installed the debugger (我正在运行 Ruby 1.9.3)并验证它是否有效。我已将它添加到我的安装文件中(这是针对 Padrino,但我认为它对于 Rails 来说也是类似的):
# file: config/boot.rb
Padrino.before_load do
trap("SIGINT") { debugger } if Padrino.env == :development
end
...但键入 Ctrl-C 不会调用调试器。事实上,如果我将 debugger
替换为 puts "saw an interrupt!"
,则键入 Ctrl-C 也不会导致打印。
更新
正在关注 this suggestion来自 Mike 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>
没有喜悦——中断没有进入调试器。
我错过了什么?
最佳答案
如果您想在控制台中运行时捕获 SIGINT,简短的回答是:除非您对 IRB 进行猴子修补,否则您做不到。每个使用控制台的 Ruby 应用程序(无论是 padrino、rails 还是诸如此类)最终都会调用 usr/lib/ruby/1.9.1/irb.rb
,并在 IRB.start
,它确实:
trap("SIGINT") do
irb.signal_handle
end
... 就在进入主循环之前。这将覆盖您可能已放入启动代码中的任何陷阱(“SIGINT”)。
但是如果你想在脚本文件中捕获 SIGINT(例如,如果你想按照 by Mike Dunlavey here 的描述分析你的代码),你可以创建一个脚本文件,例如:
# File: profile_complex_operation.rb
trap("SIGINT") { debugger }
MyApp.complex_operation
然后像这样调用它:
$ ruby profile_complex_operation.rb
现在,当您点击 ^C(或从另一个进程发送 SIGINT)时,它将进入调试器。
关于ruby - 如何配置 ruby 以通过 Ctrl-C (SIGINT) 进入调试器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15278134/