Skip to content

Instantly share code, notes, and snippets.

@kivikakk
Created June 30, 2016 16:14
Show Gist options
  • Select an option

  • Save kivikakk/d472d0ec2670d2bc80076bcb3ade0f07 to your computer and use it in GitHub Desktop.

Select an option

Save kivikakk/d472d0ec2670d2bc80076bcb3ade0f07 to your computer and use it in GitHub Desktop.
find the file:line of the current ruby thread that crashed
m_pos = int(gdb.parse_and_eval("ruby_current_thread->cfp->pc - ruby_current_thread->cfp->iseq->iseq_encoded"))
if m_pos > 0:
m_pos -= 1
size = int(gdb.parse_and_eval("ruby_current_thread->cfp->iseq->line_info_size"))
if size == 0:
res = 0
elif size == 1:
res = int(gdb.parse_and_eval("ruby_current_thread->cfp->iseq->line_info_table[0].line_no"))
else:
ix = 1
while ix < size:
pos = int(gdb.parse_and_eval("ruby_current_thread->cfp->iseq->line_info_table[{}].position".format(ix)))
if pos == m_pos:
break
elif pos > m_pos:
ix -= 1
break
ix += 1
if ix == size:
ix -= 1
res = int(gdb.parse_and_eval("ruby_current_thread->cfp->iseq->line_info_table[{}].line_no".format(ix)))
path = gdb.parse_and_eval("RSTRING_PTR(ruby_current_thread->cfp->iseq->location.path)").string()
gdb.write("{}:{}\n".format(path, res))
@kivikakk

Copy link
Copy Markdown
Author

Usage:

(gdb) bt 5
#0  0x00007f5adab06035 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007f5adab0979b in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007f5adc0c7f4c in rb_bug (fmt=0x7f5adc0f863c "Segmentation fault at %p") at error.c:346
#3  0x00007f5adbfe88d3 in sigsegv (sig=<optimized out>, info=0x7f5ade111bf0, ctx=<optimized out>) at signal.c:824
#4  <signal handler called>
(More stack frames follow...)
(gdb) source find-line-no.py
/super/secret/file.rb:50
(gdb)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment