gpt4 book ai didi

在 gdb 中暂停的 Ruby 程序在使用 rb_eval_string 写入任何 IO 后无法恢复

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:28 25 4
gpt4 key购买 nike

我正在使用 gdb 来尝试追踪 ruby​​ 程序中的内存泄漏。我正在尝试打印一些调试数据(或将其写入文件),但似乎每次向任何 IO 打印任何内容时,程序在我分离后都无法恢复。

简单的测试用例是如下程序:

#!/usr/bin/env ruby

counter = 0
loop do
puts "#{counter}\n"
counter += 1
sleep 1
end

然后我通过 gdb -p PID 将 gdb 附加到进程并运行 p rb_eval_string("$stderr.puts(\"hi\\n\")") 在 gdb 控制台上。导致detach后无法resume。如果我运行 p rb_eval_string("a = 1") 并分离,ruby 进程将恢复正常。如果我尝试写入文件或 $stdout,则会发生同样的问题。

程序恢复失败时的回溯如下:

(gdb) bt
#0 0x00007fcfb6617414 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib/x86_64-linux-gnu/libpthread.so.0
#1 0x0000000000543773 in native_cond_wait (mutex=0x15fcf30, cond=<optimized out>) at thread_pthread.c:309
#2 gvl_acquire_common (vm=0x15fcf20) at thread_pthread.c:64
#3 gvl_acquire (th=0x15fd520, vm=0x15fcf20) at thread_pthread.c:82
#4 native_sleep (timeout_tv=<synthetic pointer>, th=0x15fd520) at thread_pthread.c:918

我在带有 ruby​​ 1.9.3 的 Ubuntu Trusty 上使用 gdb 7.7 版。

谁能建议如何让程序恢复?

谢谢!

最佳答案

我还不明白发生了什么,但这可能是因为 Ruby 的 sleeprb_thread_sleep 使用 native_sleep 的方式(我可以在本地重现您的问题)。

我没有使用sleep,而是考虑使用一种高成本的方法,它会消耗将近一秒钟的时间,所以我决定只使用未优化的fibonacci :- )

代码如下:

# fibo.rb
def fibonacci(n)
return n if (0..1).include?(n)
fibonacci(n-1) + fibonacci(n-2)
end

i = 0
loop do
fibonacci(30)
i += 1
puts "#### i = #{i}"
end

现在,您应该能够(请注意我使用的是 lldb 而不是 gdb,但它们是同一回事(几乎 :-)):

➜  ~  lldb ruby
Current executable set to 'ruby' (x86_64).
(lldb) r foo.rb
Process 41134 launched: '/Users/xxx/.rubies/ruby-2.1.0/bin/ruby' (x86_64)
#### i = 1
Process 41134 stopped
* thread #1: tid = 0x72502, 0x00000001000c4820 ruby`range_include + 256, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
frame #0: 0x00000001000c4820 ruby`range_include + 256
ruby`range_include + 256:
-> 0x1000c4820: movq 0x16a691(%rip), %rsi ; id_cmp
0x1000c4827: movq %rbx, %rdi
0x1000c482a: movl $0x1, %edx
0x1000c482f: movq %r12, %rcx
(lldb) call (void)rb_eval_string("puts 42")
42
(lldb) c
Process 41134 resuming
#### i = 2
#### i = 3
...

关于在 gdb 中暂停的 Ruby 程序在使用 rb_eval_string 写入任何 IO 后无法恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25902230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com