转自http://hlee.javaeye.com/blog/361405
本篇主要是为了说明如何进行Rails调试的.但ruby-debug本身不是Rails的插件,也就是说ruby-debug是调试ruby程序.
1. 安装ruby-debug
那么,安装自然,就不是Rails的插件安装,下载gem包,或者直接gem安装如下:
gem install ruby-debug -y
2. 在rails中如何配置
修改环境配置文件:
# config/environments/development.rb config.breakpoint_server = true require "ruby-debug"
增加断点
在需要调试的代码部分增加debugger
def new @story = Story.new(params[:story]) @story.user = @current_user if request.post? and @story.save debugger @story.tag_with params[:tags] if params[:tags] flash[:notice] = "Story submission succeeded" redirect_to :action => 'index' end end
启动运行
Ruby代码
- ruby script/server -e development
调试运行界面
基本命令详解
1. help
第一个,最重要的命令
(rdb:5) help ruby-debug help v0.10.3
你可以用help cmd看cmd命令的内容详情
这里说明我的ruby-debug是0.10.3版.这个版本的一些命令已经和之前版本的命令差很多了.
2. list
用来浏览代码列表和目前断点的位置
(rdb:2) list [54, 63] in ./script/../config/../app/controllers/user_controller.rb 54 end 55 end 56 57 def login 58 debugger => 59 user = User.auth(@params['login'], @params['pwd']) 60 if user 61 @session[USER_PARAM] = user 62 set_filter(user) 63 else
多次打list可以看下面的代码.
l[ist] #列出当前代码,再次输入,列出后面代码 l[ist] - #列出当前代码,往前的代码 l[ist] = #列出当前行代码 l[ist] nn-mm #列出给定行的代码
3. where
用于查看当前程序运行的堆栈情况
(rdb:2) where --> #1 ./script/../config/../app/controllers/user_controller.rb:59:in `login' #2 /usr/.../action_controller/base.rb:910:in `perform_action_without_filters' #3 /usr/.../action_controller/filters.rb:368:in `perform_action_without_benchmark' #4 /usr/.../action_controller/benchmarking.rb:69:in `measure' #5 /usr/.../action_controller/benchmarking.rb:69:in `perform_action_without_rescue' #6 /usr/.../action_controller/rescue.rb:82:in `perform_action' ...
4. up/down
使用up和down命令可以在之前的where显示的堆栈中进行调试.并可以结合list和察看变量的方法,进行动态的调试.
如下:
(rdb:2) up 2 #3 /usr/.../action_controller/filters.rb:368:in `perform_action_without_benchmark' (rdb:2) l [363, 372] in /usr/.../action_controller/filters.rb 363 364 def perform_action_with_filters 365 before_action_result = before_action 366 367 unless before_action_result == false || performed? => 368 perform_action_without_filters 369 after_action 370 end 371 372 @before_filter_chain_aborted = (before_action_result == false) (rdb:2) before_action_result [:verify_login, :verify_access] (rdb:2) performed? false (rdb:2) down 2
5. step/next
s[tep][+-]?[ nnn] #nnn 表示次数 #'+' 强制向其它线程 #'-' 和上面相反并且禁用掉force_stepping setting.
单步执行,使用next命令向下执行而不进入命令本身.这两个都支持一个数字的参数表明执行多少:
(rdb:2) s script/../config/../app/models/user.rb:27: find :first, (rdb:2) l [22, 31] in script/../config/../app/models/user.rb 22 def status_name 23 STATUS_NAMES[self.status] 24 end 25 26 def self.auth(login, pwd) => 27 find :first, 28 :conditions => ["login = ? AND pwd = ? AND status = ?", 29 login, pwd, ACTIVE] 30 end 31 32 def is_admin?
6. Thread
th[read] l[ist] # 列出所有的线程 th[read] stop # 停止指定线程 th[read] resume # 恢复指定线程 th[read] [sw[itch]] # 切换执行环境到指定线程 th[read] [cur[rent]] # 显示当前线程
用列出和切换线程,示例如下:
(rdb:2) thread list 1 # /usr/local/lib/ruby/1.8/webrick/server.rb:91 +2 # script/../config/../app/models/user.rb:27 31 # /usr/local/lib/ruby/1.8/drb/drb.rb:944 (rdb:2) By the way, the debugger prompt also shows the current thread number (rdb:2).
7. var
v[ar] cl[ass] #显示当前上下文的所有类变量 v[ar] c[onst] #显示当前上下文的常数对象 v[ar] g[lobal] #显示当前上下文的全局变量 v[ar] i[nstance]#显示当前上下文的当前对象的实例变量 v[ar] l[ocal] #显示当前上下文的所有局部变量
这个命令相当的强大,比起breakpoint的params来说,也是经常用到的.显示当前上下文的变量参数情况.例子如下:
(rdb:2) var local login => "admin" pwd => "letmein" (rdb:2) var global $! => nil $" => ["rubygems.rb", "rbconfig.rb", "rubygems/rubygems_version.rb", ...
8. breakpoint
b[reak] file:line [if expr] #在expr的条件下,设在文件的第几行设置断点 b[reak] class(.|#)method [if expr] #在expr成立条件下,设置断点在类的方法下
在特点条件的断点,也是很方便的地方.示例如下:
#以下代码由夜鸣猪@javaeye奉献 (rdb:1) l #list的简写,显示当前位置 26 #breakpoint 27 @current_item = @cart.add_product(product) 28 debugger => 29 respond_to do |format| 30 format.js if request.xhr? 31 format.html {redirect_to_index} 32 end 33 rescue ActiveRecord::RecordNotFound (rdb:1) where --> #0 StoreController.add_to_cart at line D:/RORWS/depot_t/app/controllers/store_controller.rb:29 Warning: saved frames may be incomplete; compare with caller(0). (rdb:1) b 30 #在第30行设置断点 Breakpoint 1 file D:/RORWS/depot_t/app/controllers/store_controller.rb, line 30 (rdb:1) c #continue的缩写,推出debug环境执行 Breakpoint 1 at store_controller.rb:30 #断点调试,中断了 D:/RORWS/depot_t/app/controllers/store_controller.rb:30 format.js if request.xhr? (rdb:1) l #再看一下位置 [25, 34] in D:/RORWS/depot_t/app/controllers/store_controller.rb 25 product = Product.find(params[:id]) 26 #breakpoint 27 @current_item = @cart.add_product(product) 28 debugger 29 respond_to do |format| => 30 format.js if request.xhr? 31 format.html {redirect_to_index} 32 end 33 rescue ActiveRecord::RecordNotFound 34 logger.error("Attempt to access invalid product #{params[:id]}") (rdb:2) b 72 if params['user'] == 'admin' #在admin的条件下设置断点 Set breakpoint 1 at ./script/.../controllers/user_controller.rb:69 To list all breakpoints use break command without parameters: (rdb:1) info break # 显示当前都设置了什么断点 Num Enb What 1 y at store_controller.rb:30 breakpoint already hit 1 time
9. continue
这个简单继续执行
(rdb:2) cont 127.0.0.1 - - [11/07/2006:15:09 EDT] "POST /user/login HTTP/1.1" 302 96 http://localhost:3000/bug/list -> /user/login 127.0.0.1 - - [11/07/2006:15:12 EDT] "GET /bug/list HTTP/1.1" 200 3830 http://localhost:3000/bug/list -> /bug/list
10. delete
del[ete][ nnn...] #删除指定或者所有breakpoints
11. save
save [FILE]
将当前debugger的状态保存成为文件,保存的状态,包括所有设定breakpoints,设定, 捕获的断点.不写文件名会假定生成一个.
使用source命令,得到相关信息.
12. catch
cat[ch] #等同与 "info catch" cat[ch] #拦截所指定的异常
13. backtrace
bt|backtrace #where - display的别名 (rdb:8) backtrace --> #0 StoreController.add_to_cart at line D:/RORWS/depot_t/app/controllers/store_controller.rb:29 #1 Kernel.send at line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253 #2 ActionController::Base.perform_action_without_filters at line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253 #3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...) at line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:617
显示所有堆栈的执行信息.使用"up" and "down" 可以调试和改变在堆栈的执行.当前的位置使用-->符号表明.
14. edit
修改特定文件.如果没有任何参数,则显示当前最近修改的行.用FILE:LINENUM的格式指定修改的文件和行号.以便一即使修改
15. quit
q[uit] [unconditionally] #从调试环境推出. exit #就是quit命令的别名
通常情况下,退出之前会有,确认提示. 然而,如果quit命令使用了参数"unconditionally",将不会有提示信息.
16. set
设定ruby-debug的环境,Boolean变量可以设定为on off或者1 0.设定变量可以用show显示
set annotate # 设定注释等级 set args # 设定变量列表,用来传递给运行环境 set autoeval # 在不能直接输出的表达式,进行eval计算 set autolist # 在每个breakpoint时执行list set autoirb # 任何时候只要stop则执行irb set autoreload # 当代码有修改的时候,从新load set basename # 设定basename只显示文件名 set callstyle # 设定显示变量格式 set debuggertesting # 用于测试debugger自身 set forcestep # 保证'next/step'命令总是能向新行移动 set fullpath # 在frames中显示文件的完整路径名 set history # Generic command for setting command history parameters set keep-frame-bindings # Save frame binding on each call set linetrace+ # Set line execution tracing to show different lines set linetrace # Set line execution tracing set listsize # Set number of source lines to list by default set trace # Display stack trace when 'eval' raises exception set width # Number of characters the debugger thinks are in a line
17. info
显示相关信息
info args # Argument variables of current stack frame info breakpoints # 显示当前所有断点的状态 info catch # 可以被捕获的Exceptions,通过catch命令设定 info display # 程序结束时的输出 info file # 关于读取文件的内容 info files # 关于读取文件的时间和名字等信息 info global_variables # 所有全局变量 info instance_variables # 当前frame的示例变量 info line # 当前文件的当前行有关信息 info locals # 局部变量信息 info program # 程序执行状态信息 info stack # 相关stack信息 info thread # Thread相关信息 info threads # Thread相关信息 info variables # 局部变量示例变量信息
18. show
show annotate # 显示注释级别 show args # 显示当程序要执行时的参数列表 show autoeval # 是否显示当遇到不识别命令 show autolist # 显示是否自动list的设定 show autoirb # 显示是否有设定autoirb show autoreload # 显示是否有设定autoreload show basename # Show if basename used in reporting files show callstyle # Show paramater style used showing call frames show commands # Show the history of commands you typed show forcestep # Show if sure 'next/step' forces move to a new line show fullpath # Show if full file names are displayed in frames show history # Generic command for showing command history parameters show keep-frame-bindings # Save frame binding on each call show linetrace # 显示行执行信息 show linetrace+ # Show if consecutive lines should be different are shown in tracing show listsize # Show number of source lines to list by default show port # Show server port show post-mortem # Show whether we go into post-mortem debugging on an uncaught exception show trace # Show if a stack trace is displayed when 'eval' raises exception show version # 显示debug版本号 show width # 显示调试信息的行字数
19. condition
Condition breakpoint-number expression
给特定的brakepoint添加,执行条件.如果,expression为空,那么断点执行的约束,取消
20. eval
e[val] expression # 计算表达式的值并输出 # 可以做为p别名.
* 提示 - 如果,想要设定为自动调用eval进行,表达式计算.需要设定autoeval 使用命令
use 'set autoeval'
21. reload
r[eload] forces source code reloading
22. ps
ps expression #计算数组,集合,序列,表达式组的值,并序列化输出
23. undisplay
undisp[lay][ nnn]
取消一些表达式的输出
引用
Arguments are the code numbers of the expressions to stop displaying.
No argument means cancel all automatic-display expressions.
"delete display" has the same effect as this command.
Do "info display" to see current list of code numbers.
24. irb
irb # 打开一个(IRB) 环境
25. finish
fin[ish] [frame-number] #Execute until selected stack frame returns.
引用
If no frame number is given, we run until the currently selected frame
returns. The currently selected frame starts out the most-recent
frame or 0 if no frame positioning (e.g "up", "down" or "frame") has
been performed. If a frame number is given we run until that frame
returns.
累了,假装写完了