看到还有开发人员读到这篇,起到了一些作用,并且回复了,实在是感动。一转眼,3年多过去了。想想写这篇的时候,已经今非昔比,感慨啊。不废话了,回来说rails调试
当前的情况是,Ruby1.9.3-194p Rails3.2.8
流行的调试工具gem是 debugger
这个debugger更新的目的是,解决ruby1.9后的更新问题,兼容debugger和rvm的问题
未来的趋势的 pry
rails部分用 pry-nav
- gem 'pry'
- gem 'pry-remote'
- gem 'pry-nav'
基本上的使用非常简单,所有的alias都在,怎么用debugger就怎么用pry,当然,pry有些advance功能。
回头看看有没有好的pry总结, 先留个链接,有时间了我就更新一个介绍。
不知道下次再更新这篇的时候。。。。。
祝福,看到本篇的ruby攻城狮,前程似锦
-------------------------时空小分割-------------------------
快写完的时候想说
今天是清明节,大放假的,我基本上没干什么别的.希望诸位,看到这篇文章的人们,能够有点收获吧. 这样我还欣慰点, 我的时间就没有白白浪费.
这真是一篇又臭又长的文章啊.一边调试一边写已经,好几个,好几个小时了...
基本上可以用来当rails调试中文指南,或者ruby-dubug的中文手册了.
本篇主要是为了说明如何进行Rails调试的.但ruby-debug本身不是Rails的插件,也就是说ruby-debug是调试ruby程序.
1. 安装ruby-debug
那么,安装自然,就不是Rails的插件安装,下载gem包,或者直接gem安装如下:
- gem install ruby-debug -y
2. 在rails中如何配置
修改环境配置文件:
-
- 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 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
用于查看当前程序运行的堆栈情况
4. up/down
使用up和down命令可以在之前的where显示的堆栈中进行调试.并可以结合list和察看变量的方法,进行动态的调试.
如下:
- (rdb:2) up 2
-
- (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
单步执行,使用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 <nnn>
- th[read] resume <nnn>
- th[read] [sw[itch]] <nnn>
- th[read] [cur[rent]]
用列出和切换线程,示例如下:
- (rdb:2) thread list
- 1
- +2
- 31
- (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] <object>
- v[ar] g[lobal]
- v[ar] i[nstance] <object>
- 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]
- b[reak] class(.|
在特点条件的断点,也是很方便的地方.示例如下:
-
- (rdb:1) l
- 26
- 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
- -->
- 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
- Breakpoint 1 file D:/RORWS/depot_t/app/controllers/store_controller.rb, line 30
- (rdb:1) c
- 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
- 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'
- 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
11. save
将当前debugger的状态保存成为文件,保存的状态,包括所有设定breakpoints,设定, 捕获的断点.不写文件名会假定生成一个.
使用source命令,得到相关信息.
12. catch
- cat[ch]
- cat[ch] <exception-name>
13. backtrace
- bt|backtrace
- (rdb:8) backtrace
- -->
- at line D:/RORWS/depot_t/app/controllers/store_controller.rb:29
-
- at line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253
-
- at line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253
-
- 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命令使用了参数"unconditionally",将不会有提示信息.
16. set
设定ruby-debug的环境,Boolean变量可以设定为on off或者1 0.设定变量可以用show显示
- set annotate
- set args
- set autoeval
- set autolist
- set autoirb
- set autoreload
- set basename
- set callstyle
- set debuggertesting
- set forcestep
- set fullpath
- set history
- set keep-frame-bindings
- set linetrace+
- set linetrace
- set listsize
- set trace
- set width
17. info
显示相关信息
- info args
- info breakpoints
- info catch
- info display
- info file
- info files
- info global_variables
- info instance_variables
- info line
- info locals
- info program
- info stack
- info thread
- info threads
- info variables
18. show
- show annotate
- show args
- show autoeval
- show autolist
- show autoirb
- show autoreload
- show basename
- show callstyle
- show commands
- show forcestep
- show fullpath
- show history
- show keep-frame-bindings
- show linetrace
- show linetrace+
- show listsize
- show port
- show post-mortem
- show trace
- show version
- show width
19. condition
- Condition breakpoint-number expression
给特定的brakepoint添加,执行条件.如果,expression为空,那么断点执行的约束,取消
20. eval
* 提示 - 如果,想要设定为自动调用eval进行,表达式计算.需要设定autoeval 使用命令
21. reload
- r[eload] forces source code reloading
22. ps
23. undisplay
取消一些表达式的输出
引用
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
25. finish
引用
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.