如何使用 gdb 从不同线程获取 ruby 回溯
How to use gdb to get ruby backtrace from different thread
我有一个生成两个线程的 ruby 程序。很少,它似乎挂在主线程上,我正在尝试使用 gdb 找出原因。
使用博客 post here,我使用 gdb 附加到进程,并调用 rb_backtrace()
获取回溯。
问题是,回溯总是来自生成的工作线程之一,而不是我遇到挂起的主线程。
有没有办法从特定线程获取回溯?
我曾尝试使用 gdb 命令 info threads
和 thread 2
来更改活动线程,但没有效果。我也曾尝试在 gdb 中使用 ruby_eval
定义,如 here 所述,但每当我尝试评估一行 ruby 代码时,我都会收到错误 No symbol table is loaded. Use the "file" command.
因为我我在嵌入式系统上工作,我不能轻易地用 gdb 符号重新编译 ruby。
rb_backtrace()
指的是您可以覆盖的全局 ruby_current_thread
变量:
# Make a note of the old thread pointer so you can put it back.
(gdb) p ruby_current_thread
= (rb_thread_t *) 0x5619dfe485e0
(gdb) set ruby_current_thread = 0x5619efb7d3a0
(gdb) p ruby_current_thread
= (rb_thread_t *) 0x5619efb7d3a0
# Write the backtrace to stderr
(gdb) call (void) rb_backtrace()
可以在您感兴趣的本机线程的 GDB 回溯中找到 ruby 线程指针(上面的 0x5619efb7d3a0
),例如
#7 0x00005619de86cf96 in vm_exec (th=th@entry=0x5619efb7d3a0) at vm.c:1693
在让进程恢复之前,请务必将 ruby_current_thread
设置回其原始值。在 Ruby 2.3.8.
上测试
我有一个生成两个线程的 ruby 程序。很少,它似乎挂在主线程上,我正在尝试使用 gdb 找出原因。
使用博客 post here,我使用 gdb 附加到进程,并调用 rb_backtrace()
获取回溯。
问题是,回溯总是来自生成的工作线程之一,而不是我遇到挂起的主线程。
有没有办法从特定线程获取回溯?
我曾尝试使用 gdb 命令 info threads
和 thread 2
来更改活动线程,但没有效果。我也曾尝试在 gdb 中使用 ruby_eval
定义,如 here 所述,但每当我尝试评估一行 ruby 代码时,我都会收到错误 No symbol table is loaded. Use the "file" command.
因为我我在嵌入式系统上工作,我不能轻易地用 gdb 符号重新编译 ruby。
rb_backtrace()
指的是您可以覆盖的全局 ruby_current_thread
变量:
# Make a note of the old thread pointer so you can put it back.
(gdb) p ruby_current_thread
= (rb_thread_t *) 0x5619dfe485e0
(gdb) set ruby_current_thread = 0x5619efb7d3a0
(gdb) p ruby_current_thread
= (rb_thread_t *) 0x5619efb7d3a0
# Write the backtrace to stderr
(gdb) call (void) rb_backtrace()
可以在您感兴趣的本机线程的 GDB 回溯中找到 ruby 线程指针(上面的 0x5619efb7d3a0
),例如
#7 0x00005619de86cf96 in vm_exec (th=th@entry=0x5619efb7d3a0) at vm.c:1693
在让进程恢复之前,请务必将 ruby_current_thread
设置回其原始值。在 Ruby 2.3.8.