Skip to contentSkip to conversation

Quality of life patches for qemu coroutine debugging

PATCH In review Maxim Levitsky 2 participants 6 messages Dec 17, 2020 – Jan 7, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover Quality of life patches for qemu coroutine debugging

These are two patches that improve a bit the qemu gdb scripts in regard to coroutines.

First patch fixes a bug in 'qemu coroutine' in regard to usage on non topmost stack frame.

Second patch implements 'qemu bt' as Stefan suggested.

V2, because after hitting 'send' I noticed a few bugs:
1. Switch to try/catch around bt, so that when regular bt fails, we print the same message as original bt.
2. Print the correct co-routine address

Best regards,
Maxim Levitsky

Maxim Levitsky (2):
scripts/gdb: fix 'qemu coroutine' when users selects a non topmost
stack frame
scripts/gdb: implement 'qemu bt'

 scripts/qemu-gdb.py          |  1 +
 scripts/qemugdb/coroutine.py | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/2 scripts/gdb: fix 'qemu coroutine' when users selects a non topmost stack frame

The code that dumps the stack frame works like that:
* save current registers
* overwrite current registers (including rip/rsp) with coroutine snapshot in the jmpbuf
* print backtrace
* restore the saved registers.

If the user has currently selected a non topmost stack frame in gdb, the above code will still restore the selected frame registers, but the gdb will then lose the selected frame index, which makes it impossible to switch back to frame 0, to continue debugging the executable.

Therefore switch temporarily to the topmost frame of the stack for the above code.

---
 scripts/qemugdb/coroutine.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/qemugdb/coroutine.py +7 -0
index db61389022..e1399211e6 100644
@@ -70,6 +70,11 @@ def bt_jmpbuf(jmpbuf):
     regs = get_jmpbuf_regs(jmpbuf)
     old = dict()
 
+    # remember current stack frame and select the topmost
+    # so that register modifications don't wreck it
+    selected_frame = gdb.selected_frame()
+    gdb.newest_frame().select()
+
     for i in regs:
         old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
 
@@ -81,6 +86,8 @@ def bt_jmpbuf(jmpbuf):
     for i in regs:
         gdb.execute('set $%s = %s' % (i, old[i]))
 
+    selected_frame.select()
+
 def coroutine_to_jmpbuf(co):
     coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer())
     return coroutine_pointer['env']['__jmpbuf']
Signature · 1 line
2.26.2
#2 lore raw

PATCH v2 2/2 scripts/gdb: implement 'qemu bt'

In reply to Maxim Levitsky

This script first runs the regular gdb's 'bt' command, and then if we are in a coroutine it prints the coroutines backtraces in the order in which they were called.

---
 scripts/qemu-gdb.py          |  1 +
 scripts/qemugdb/coroutine.py | 28 +++++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
scripts/qemu-gdb.py +1 -0
index e0bfa7b5a4..4d2a9f6c43 100644
@@ -40,6 +40,7 @@ timers.TimersCommand()
 
 coroutine.CoroutineSPFunction()
 coroutine.CoroutinePCFunction()
+coroutine.CoroutineBt()
 
 # Default to silently passing through SIGUSR1, because QEMU sends it
 # to itself a lot.
scripts/qemugdb/coroutine.py +27 -1
index e1399211e6..7db46d4b68 100644
@@ -88,8 +88,11 @@ def bt_jmpbuf(jmpbuf):
 
     selected_frame.select()
 
+def co_cast(co):
+    return co.cast(gdb.lookup_type('CoroutineUContext').pointer())
+
 def coroutine_to_jmpbuf(co):
-    coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer())
+    coroutine_pointer = co_cast(co)
     return coroutine_pointer['env']['__jmpbuf']
 
 
@@ -107,6 +110,29 @@ class CoroutineCommand(gdb.Command):
 
         bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
 
+class CoroutineBt(gdb.Command):
+    '''Display backtrace including coroutine switches'''
+    def __init__(self):
+        gdb.Command.__init__(self, 'qemu bt', gdb.COMMAND_STACK,
+                             gdb.COMPLETE_NONE)
+
+    def invoke(self, arg, from_tty):
+
+        gdb.execute("bt")
+
+        if gdb.parse_and_eval("qemu_in_coroutine()") == False:
+            return
+
+        co_ptr = gdb.parse_and_eval("qemu_coroutine_self()")
+
+        while True:
+            co = co_cast(co_ptr)
+            co_ptr = co["base"]["caller"]
+            if co_ptr == 0:
+                break
+            gdb.write("Coroutine at " + str(co_ptr) + ":\n")
+            bt_jmpbuf(coroutine_to_jmpbuf(co_ptr))
+
 class CoroutineSPFunction(gdb.Function):
     def __init__(self):
         gdb.Function.__init__(self, 'qemu_coroutine_sp')
Signature · 1 line
2.26.2
#3 lore raw
In reply to Maxim Levitsky
Quoted 16 lines · Maxim Levitsky

These are two patches that improve a bit the qemu gdb scripts in regard to coroutines.

First patch fixes a bug in 'qemu coroutine' in regard to usage on non topmost stack frame.

Second patch implements 'qemu bt' as Stefan suggested.

V2, because after hitting 'send' I noticed a few bugs:
1. Switch to try/catch around bt, so that when regular bt fails, we print the same message as original bt.
2. Print the correct co-routine address

Best regards,
Maxim Levitsky

Quoted 14 lines

Maxim Levitsky (2):
scripts/gdb: fix 'qemu coroutine' when users selects a non topmost
stack frame
scripts/gdb: implement 'qemu bt'

 scripts/qemu-gdb.py          |  1 +
 scripts/qemugdb/coroutine.py | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2

Any update on this?
Best regards,
Maxim Levitsky

#4 lore raw
Quoted 26 lines · Maxim Levitsky
Quoted 25 lines · Maxim Levitsky

These are two patches that improve a bit the qemu gdb scripts in regard to coroutines.

First patch fixes a bug in 'qemu coroutine' in regard to usage on non topmost stack frame.

Second patch implements 'qemu bt' as Stefan suggested.

V2, because after hitting 'send' I noticed a few bugs:
1. Switch to try/catch around bt, so that when regular bt fails, we print the same message as original bt.
2. Print the correct co-routine address

Best regards,
Maxim Levitsky

Maxim Levitsky (2):
scripts/gdb: fix 'qemu coroutine' when users selects a non topmost
stack frame
scripts/gdb: implement 'qemu bt'

  scripts/qemu-gdb.py          |  1 +
  scripts/qemugdb/coroutine.py | 35 ++++++++++++++++++++++++++++++++++-
  2 files changed, 35 insertions(+), 1 deletion(-)
Quoted 10 lines
Quoted 5 lines
Signature · 1 line
2.26.2

Any update on this?
Best regards,
Maxim Levitsky

I'll queue them myself, thanks.

Paolo

#5 lore raw
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Maxim Levitsky
Quoted 3 lines · Maxim Levitsky

These are two patches that improve a bit the qemu gdb scripts in regard to coroutines.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

First patch fixes a bug in 'qemu coroutine' in regard to usage on non topmost stack frame.

Second patch implements 'qemu bt' as Stefan suggested.

V2, because after hitting 'send' I noticed a few bugs:
1. Switch to try/catch around bt, so that when regular bt fails, we print the same message as original bt.

Note that I forgot to 'git add' this bit, but it is only a cosmetic thing, so I'll guess I'll just leave it like that.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

2. Print the correct co-routine address

Best regards,
Maxim Levitsky

Maxim Levitsky (2):
scripts/gdb: fix 'qemu coroutine' when users selects a non topmost
stack frame
scripts/gdb: implement 'qemu bt'

  scripts/qemu-gdb.py          |  1 +
  scripts/qemugdb/coroutine.py | 35 ++++++++++++++++++++++++++++++++++-
  2 files changed, 35 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2
Quoted 7 lines
Quoted 5 lines

Any update on this?
Best regards,
Maxim Levitsky

I'll queue them myself, thanks.

Thanks!

Best regards,
Maxim Levitsky

Quoted 3 lines

Paolo