Skip to contentSkip to conversation

qemugdb: coroutine backtrace for coredump

PATCH In review Vladimir Sementsov-Ogievskiy 3 participants 9 messages Mar 28, 2018 – Apr 4, 2018

lore
Share & tools
lore
#0 lore raw

cover qemugdb: coroutine backtrace for coredump

Hi all. Here are some qemugdb enhancements around qemu coroutine command. The main feature is a backtrace for coroutine, when debugging with a coredump file.

The problem is that we can't get coroutine backtrace through obvious way
- set regs
- bt
- restore regs
when debugging a coredump.
So, let's go hard way: clone current coredump file, patch regs in it and execute a subprocess gdb to get backtrace from this patched coredump.

Vladimir Sementsov-Ogievskiy (4):
scripts/qemugdb: get pthread_self from "info threads" command scripts/qemugdb: improve "qemu coroutine" command scripts/qemugdb: add coredump.py for coredump patching scripts/qemugdb: backtraces for coroutines in coredump

 scripts/qemugdb/coredump.py  | 51 ++++++++++++++++++++++++++++++++++++++++++
 scripts/qemugdb/coroutine.py | 53 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 99 insertions(+), 5 deletions(-)
 create mode 100644 scripts/qemugdb/coredump.py
Signature · 1 line
2.11.1
#1 lore raw

PATCH 1/4 scripts/qemugdb: get pthread_self from "info threads" command

When debugging a coredump, pthread_self can't be obtained from function arch_prctl. Moreover if qemu crashed in coroutine, we can't find 'start_thread' in current stack-trace. So, add a method, actually proposed in 1138f24645e9e, which should work for gdb version >= 7.3.

---
 scripts/qemugdb/coroutine.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
scripts/qemugdb/coroutine.py +12 -1
index ab699794ab..ffaa45c464 100644
@@ -14,6 +14,7 @@
 # GNU GPL, version 2 or (at your option) any later version.
 
 import gdb
+import re
 
 VOID_PTR = gdb.lookup_type('void').pointer()
 
@@ -28,7 +29,17 @@ def get_fs_base():
     return fs_base
 
 def pthread_self():
-    '''Fetch pthread_self() from the glibc start_thread function.'''
+    # Try read pthread_self from gdb command 'info threads'.
+    # Will fail for old gdb.
+    try:
+        threads = gdb.execute('info threads', False, True)
+        m = re.search('^\* 1    Thread (0x[0-9a-f]+)', threads, re.MULTILINE)
+        return int(m.group(1), 16)
+    except TypeError:
+        # gdb doesn't support third parameter for execute
+        pass
+
+    # Try fetch pthread_self() from the glibc start_thread function.
     f = gdb.newest_frame()
     while f.name() != 'start_thread':
         f = f.older()
Signature · 1 line
2.11.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] scripts/qemugdb: get pthread_self from "info threads" command

Quoted 7 lines · Vladimir Sementsov-Ogievskiy

When debugging a coredump, pthread_self can't be obtained from function arch_prctl. Moreover if qemu crashed in coroutine, we can't find 'start_thread' in current stack-trace. So, add a method, actually proposed in 1138f24645e9e, which should work for gdb version >= 7.3.

New-enough gdb can also use gdb.selected_thread()

Quoted 17 lines
---
 scripts/qemugdb/coroutine.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
scripts/qemugdb/coroutine.py +1 -0
index ab699794ab..ffaa45c464 100644
@@ -14,6 +14,7 @@
 # GNU GPL, version 2 or (at your option) any later version.
 
 import gdb
+import re
 
 VOID_PTR = gdb.lookup_type('void').pointer()
 

scripts/qemugdb/coroutine.py def get_fs_base(): · line 36

Quoted 9 lines
scripts/qemugdb/coroutine.py +5 -1
@@ -28,7 +29,17 @@ def get_fs_base():
     return fs_base
 
 def pthread_self():
-    '''Fetch pthread_self() from the glibc start_thread function.'''
+    # Try read pthread_self from gdb command 'info threads'.
+    # Will fail for old gdb.
+    try:
+        threads = gdb.execute('info threads', False, True)
+        m = re.search('^\* 1    Thread (0x[0-9a-f]+)', threads, re.MULTILINE)

I don't think hard-coding "1" works here, and the spacing of the table might differ as well. However, looking for the asterisk seems safe from a quick look at gdb source, and "Thread" looks like it isn't localized.

Paolo

Quoted 10 lines
scripts/qemugdb/coroutine.py +6 -0
+        return int(m.group(1), 16)
+    except TypeError:
+        # gdb doesn't support third parameter for execute
+        pass
+
+    # Try fetch pthread_self() from the glibc start_thread function.
     f = gdb.newest_frame()
     while f.name() != 'start_thread':
         f = f.older()
Reviewed-by on v1 1/4
#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] scripts/qemugdb: get pthread_self from "info threads" command

In reply to Vladimir Sementsov-Ogievskiy
Quoted 10 lines · Vladimir Sementsov-Ogievskiy

When debugging a coredump, pthread_self can't be obtained from function arch_prctl. Moreover if qemu crashed in coroutine, we can't find 'start_thread' in current stack-trace. So, add a method, actually proposed in 1138f24645e9e, which should work for gdb version >= 7.3.

---
 scripts/qemugdb/coroutine.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
#4 lore raw

PATCH 2/4 scripts/qemugdb: improve "qemu coroutine" command

In reply to Vladimir Sementsov-Ogievskiy

- print regs
- catch exception for coredump debugging

---
 scripts/qemugdb/coroutine.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
scripts/qemugdb/coroutine.py +14 -4
index ffaa45c464..7070a592f3 100644
@@ -80,9 +80,8 @@ def get_jmpbuf_regs(jmpbuf):
         'r15': jmpbuf[JB_R15],
         'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }
 
-def bt_jmpbuf(jmpbuf):
-    '''Backtrace a jmpbuf'''
-    regs = get_jmpbuf_regs(jmpbuf)
+def bt_regs(regs):
+    '''Backtrace with specified regs'''
     old = dict()
 
     for i in regs:
@@ -113,7 +112,18 @@ class CoroutineCommand(gdb.Command):
             gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
             return
 
-        bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
+        jmpbuf = coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0]))
+        regs = get_jmpbuf_regs(jmpbuf)
+        for k, v in regs.iteritems():
+            gdb.write('%s: 0x%x\n' %(k,v))
+
+        gdb.write('\n')
+
+        try:
+            bt_regs(regs)
+        except gdb.error:
+            print "Coroutine backtrace can't be obtained without " \
+                  "a process to debug."
 
 class CoroutineSPFunction(gdb.Function):
     def __init__(self):
Signature · 1 line
2.11.1
#6 lore raw

PATCH 3/4 scripts/qemugdb: add coredump.py for coredump patching

In reply to Vladimir Sementsov-Ogievskiy

The main function is write_regs_to_coredump, which opens coredump file, searches for 'CORE' sign. The first one should correspond to PRSTATUS note for first thread. Patch register values in elf_prstatus structure, going after header with 'CORE' sign.

---
 scripts/qemugdb/coredump.py | 51 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 scripts/qemugdb/coredump.py
scripts/qemugdb/coredump.py new file +51 -0
new file mode 100644
index 0000000000..8915461886
@@ -0,0 +1,51 @@
+# Coredump patching
+#
+# Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
+#
+# Authors:
+#  Vladimir Sementsov-Ogievskiy <[email protected]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import struct
+import shutil
+
+def write_regs_to_coredump(fname, set_regs):
+    # asm/ptrace.h
+    pt_regs = ['r15', 'r14', 'r13', 'r12', 'rbp', 'rbx', 'r11', 'r10',
+               'r9', 'r8', 'rax', 'rcx', 'rdx', 'rsi', 'rdi', 'orig_rax',
+               'rip', 'cs', 'eflags', 'rsp', 'ss']
+
+    with open(fname, 'r+b') as f:
+        print 'patching core file "%s"' % fname
+
+        while f.read(4) != 'CORE':
+            pass
+
+        print 'found "CORE" at 0x%x' % f.tell()
+        f.seek(4, 1) # go to elf_prstatus
+        f.seek(112, 1) # offsetof(struct elf_prstatus, pr_reg)
+
+        print 'assume pt_regs at 0x%x' % f.tell()
+        for reg in pt_regs:
+            if reg in set_regs:
+                print 'write %s at 0x%x' % (reg, f.tell())
+                f.write(struct.pack('q', set_regs[reg]))
+            else:
+                f.seek(8, 1)
+
+def clone_coredump(source, target, set_regs):
+    shutil.copyfile(source, target)
+    write_regs_to_coredump(target, set_regs)
Signature · 1 line
2.11.1
#7 lore raw

PATCH 4/4 scripts/qemugdb: backtraces for coroutines in coredump

In reply to Vladimir Sementsov-Ogievskiy

We can't get coroutine backtrace through obvious way
- set regs
- bt
- restore regs
when debugging a coredump.
So, let's go hard way: clone current coredump file, patch regs in it and execute a subprocess gdb to get backtrace from this patched coredump.

---
 scripts/qemugdb/coroutine.py | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
scripts/qemugdb/coroutine.py +24 -2
index 7070a592f3..2a05851e24 100644
@@ -15,9 +15,32 @@
 
 import gdb
 import re
+import tempfile
+import subprocess
+import os
+import coredump
 
 VOID_PTR = gdb.lookup_type('void').pointer()
 
+def bt_regs_static(regs):
+    files = gdb.execute('info files', False, True).split('\n')
+    executable = re.match('^Symbols from "(.*)".$', files[0]).group(1)
+    dump = re.search("`(.*)'", files[2]).group(1)
+
+    with tempfile.NamedTemporaryFile(dir='/tmp', delete=False) as f:
+        temp = f.name
+
+    coredump.clone_coredump(dump, temp, regs)
+
+    cmd = ['gdb', '-batch', '-ex', "python print '----split----'",
+           '-ex', 'bt', executable, temp]
+    out = subprocess.check_output(cmd)
+    out = out.split('----split----')[1]
+
+    os.remove(temp)
+
+    print out
+
 def get_fs_base():
     '''Fetch %fs base value using arch_prctl(ARCH_GET_FS).  This is
        pthread_self().'''
@@ -122,8 +145,7 @@ class CoroutineCommand(gdb.Command):
         try:
             bt_regs(regs)
         except gdb.error:
-            print "Coroutine backtrace can't be obtained without " \
-                  "a process to debug."
+            bt_regs_static(regs)
 
 class CoroutineSPFunction(gdb.Function):
     def __init__(self):
Signature · 1 line
2.11.1
#8 lore raw

Re: [Qemu-devel] [PATCH 4/4] scripts/qemugdb: backtraces for coroutines in coredump

Quoted 8 lines · Vladimir Sementsov-Ogievskiy

We can't get coroutine backtrace through obvious way
- set regs
- bt
- restore regs
when debugging a coredump.
So, let's go hard way: clone current coredump file, patch regs in it and execute a subprocess gdb to get backtrace from this patched coredump.

I have CCed you on a patch that uses 'select-frame' to temporarily switch stacks. This works in a coredump and does not require cloning/patching the coredump file.

Please let me know if 'select-frame' works for you, it would be preferrable to cloning/patching the coredump file.

Stefan