有没有办法保存 Ruby 代码的执行过程,以便以后逐步重放以进行调试?

huangapple go评论47阅读模式
英文:

Is there a way to save execution of ruby code to debug it using step by step replay later?

问题

我有一个在Ruby代码中难以复现的错误。通过不断运行我的测试套件,我大约每10-15分钟就会遇到一次。

我想要做的是逐步记录执行过程,一旦出现错误,就能继续进行调试,了解发生了什么事情。

我知道https://rr-project.org/,我曾经用它来调试C++中的类似问题。它基本上为您提供了一个完整的gdb界面,用于回放记录的内容。类似这样的工具将非常有用。

英文:

I have a rarely reproduced bug in ruby code. By running my test suite constantly I get it once every 10-15 minutes.

What I want to do is to have the execution recorded step by step and once it fails, go ahead and debug what happened.

I know about https://rr-project.org/ and I used it to debug such things in C++. It basically gives you a complete gdb interface into a recorded replay. Something along the same lines would be great.

答案1

得分: 1

有一个录制功能:

# intermittent.rb

需要 "debug"

# 弄清楚你的中断条件
binding.b do: "catch TypeError"
binding.b do: "record on"
# 这很慢 ^

class M
  def m
    var = rand(10)
    var = "1" if rand(1000) == 666
    var
  end
end

循环 do
  M.new.m + 1
end

从未真正使用过它,但你可以后退:

$ ruby ./intermittent.rb
停在 #0  断点 - 捕捉 "TypeError"

(rdbg) 后退一步
[重播] [6, 15]  ./intermittent.rb 
[重播]      6| 
[重播]      7| class M
[重播]      8|   def m
[重播]      9|     var = rand(10)
[重播]     10|     var = "1" if rand(1000) == 666
[重播] =>  11|     var
[重播]     12|   end
[重播]     13| end
[重播]     14| 
[重播]     15| 循环 do
[重播] =>#0	M#m 在 ./intermittent.rb:11
[重播]   #1	在 <main> 中的块 在 ./intermittent.rb:16
[重播]   # 和 2 个框架 (使用 `bt' 命令查看所有框架)

(rdbg) 信息
[重播] %self = #<M:0x00007f946d07ef28>
[重播] var = "1"

(rdbg) 后退一步
(rdbg) 信息
[重播] %self = #<M:0x00007f946d07ef28>
[重播] var = 2
# 等等...

https://github.com/ruby/debug

英文:

There is a recording functionality:

# intermittent.rb

require &quot;debug&quot;

# figure out your break condition
binding.b do: &quot;catch TypeError&quot;
binding.b do: &quot;record on&quot;
# this is slow ^

class M
  def m
    var = rand(10)
    var = &quot;1&quot; if rand(1000) == 666
    var
  end
end

loop do
  M.new.m + 1
end

Never really used it, but you can step back:

$ ruby ./intermittent.rb
Stop by #0  BP - Catch  &quot;TypeError&quot;

(rdbg) step back
[replay] [6, 15] in ./intermittent.rb
[replay]      6| 
[replay]      7| class M
[replay]      8|   def m
[replay]      9|     var = rand(10)
[replay]     10|     var = &quot;1&quot; if rand(1000) == 666
[replay] =&gt;  11|     var
[replay]     12|   end
[replay]     13| end
[replay]     14| 
[replay]     15| loop do
[replay] =&gt;#0	M#m at ./intermittent.rb:11
[replay]   #1	block in &lt;main&gt; at ./intermittent.rb:16
[replay]   # and 2 frames (use `bt&#39; command for all frames)

(rdbg) info
[replay] %self = #&lt;M:0x00007f946d07ef28&gt;
[replay] var = &quot;1&quot;

(rdbg) step back
(rdbg) info
[replay] %self = #&lt;M:0x00007f946d07ef28&gt;
[replay] var = 2
# etc...

https://github.com/ruby/debug

huangapple
  • 本文由 发表于 2023年5月22日 16:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定