如何配置ruby以在Ctrl-C(SIGINT)上进入调试器? [英] How do I configure ruby to enter the debugger on Ctrl-C (SIGINT)?

查看:167
本文介绍了如何配置ruby以在Ctrl-C(SIGINT)上进入调试器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在键入ctrl-C(或发送SIGINT)时进入调试器。我有安装调试器(我正在运行Ruby 1.9.3)并验证它是否正常工作。我已将其添加到我的安装文件(这是针对Padrino,但是我认为Rails类似):

I'd like to enter the debugger upon typing ctrl-C (or sending a SIGINT). I have installed the debugger (I'm running Ruby 1.9.3) and verified that it works. I've added this to my setup files (this is for Padrino, but I assume it would be similar for Rails):

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

...但是输入Ctrl-C不会调用调试器。实际上,如果我用 put 替换看到中断!,输入Ctrl-C不会导致

... but typing Ctrl-C does not invoke the debugger. In fact, if I replace debugger with puts "saw an interrupt!", typing Ctrl-C doesn't cause a print to happen either.

以下这个建议来自 Mike Dunlavey ,我试图从调试器中明确地调用 catch Interrupt

Following this suggestion from Mike Dunlavey, I tried explicitly calling catch Interrupt from within the debugger:

$ 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> 

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

No joy -- interrupt did not enter the debugger.

我缺少什么?

推荐答案

如果要在控制台中运行时捕获SIGINT,是:你不能,除非你的猴子补丁IRB。每个使用该控制台的Ruby应用程序(不论是padrino,还是rails或者whatnot)都将最终调用 usr / lib / ruby​​ / 1.9.1 / irb.rb code> IRB.start ,它是:

If you want to trap SIGINT while running in the console, the short answer is: you cannot unless you monkey-patch IRB. Every Ruby app (whether padrino, or rails or whatnot) that uses the console will end up calling usr/lib/ruby/1.9.1/irb.rb, and in IRB.start, it does:

trap("SIGINT") do
  irb.signal_handle
end

...进入主循环之前。这将覆盖您可能在启动代码中的任何陷阱(SIGINT)。

... just before entering the main loop. This will override any trap("SIGINT") you might have put in your startup code.

但是,如果要将SIGINT陷入脚本文件中(例如,如果您想要按照 Mike Dunlavey这里所描述的代码),您可以创建一个脚本文件,例如:

But if you want to trap SIGINT in a script file (for example, if you want to profile your code as described by Mike Dunlavey here), you can create a script file such as:

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

然后按如下方式调用:

$ ruby profile_complex_operation.rb

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

Now, when you hit ^C (or send SIGINT from another process), it will enter the debugger.

这篇关于如何配置ruby以在Ctrl-C(SIGINT)上进入调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆