From 4ad663597abdac1d9b64d5f6cd40e6db903529f4 Mon Sep 17 00:00:00 2001
From: Bilal Akhtar <bilal.akhtar@uwaterloo.ca>
Date: Mon, 27 Aug 2018 17:33:52 -0400
Subject: [PATCH] Restore all threads in inferior, not just selected one

---
 src/libfibre/libfibre.so-gdb.py | 42 ++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/src/libfibre/libfibre.so-gdb.py b/src/libfibre/libfibre.so-gdb.py
index b82c43e..efff11a 100644
--- a/src/libfibre/libfibre.so-gdb.py
+++ b/src/libfibre/libfibre.so-gdb.py
@@ -15,12 +15,8 @@ class FibreSupport():
             return
         FibreSupport.list = []
         FibreSupport.active = {}
+        FibreSupport.threads = {}
         FibreSupport.saved = True
-        # save register context for later continuation
-        FibreSupport.rsp = str(gdb.parse_and_eval("$rsp")).split(None, 1)[0]
-        FibreSupport.rbp = str(gdb.parse_and_eval("$rbp")).split(None, 1)[0]
-        FibreSupport.rip = str(gdb.parse_and_eval("$rip")).split(None, 1)[0]
-        FibreSupport.currStack = str(gdb.parse_and_eval("Context::currStack"))
         # traverse runtime stack list to build internal list of fibres
         _globalStackList = gdb.parse_and_eval("_globalStackList")
         first = _globalStackList['anchor'].address
@@ -34,21 +30,39 @@ class FibreSupport():
             currStack = str(gdb.parse_and_eval("Context::currStack"))
             # Cache the registers for this thread, in case it represents
             # a fibre
+            rsp = str(gdb.parse_and_eval("$rsp")).split(None, 1)[0]
+            rbp = str(gdb.parse_and_eval("$rbp")).split(None, 1)[0]
+            rip = str(gdb.parse_and_eval("$rip")).split(None, 1)[0]
+            FibreSupport.threads[thread.num] = {
+                    'rsp': rsp,
+                    'rbp': rbp,
+                    'rip': rip,
+                    'currStack': currStack
+            }
             FibreSupport.active[currStack] = {
-                    'rsp': str(gdb.parse_and_eval("$rsp")).split(None, 1)[0],
-                    'rbp': str(gdb.parse_and_eval("$rbp")).split(None, 1)[0],
-                    'rip': str(gdb.parse_and_eval("$rip")).split(None, 1)[0]
+                    'rsp': rsp,
+                    'rbp': rbp,
+                    'rip': rip
             }
         orig_thread.switch()
 
     # restore() is hooked to continue events via script hooks to 'fibre reset'
     def restore():
-        FibreSupport.prep_frame()
-        # restore original register context
-        gdb.execute("set $rsp = " + str(FibreSupport.rsp))
-        gdb.execute("set $rbp = " + str(FibreSupport.rbp))
-        gdb.execute("set $rip = " + str(FibreSupport.rip))
-        gdb.execute("set Context::currStack = " + str(FibreSupport.currStack))
+        orig_thread = gdb.selected_thread()
+        for thread in gdb.selected_inferior().threads():
+            thread.switch()
+            rsp = FibreSupport.threads[thread.num]['rsp']
+            rbp = FibreSupport.threads[thread.num]['rbp']
+            rip = FibreSupport.threads[thread.num]['rip']
+            currStack = FibreSupport.threads[thread.num]['currStack']
+
+            FibreSupport.prep_frame()
+            # restore original register context
+            gdb.execute("set $rsp = " + str(rsp))
+            gdb.execute("set $rbp = " + str(rbp))
+            gdb.execute("set $rip = " + str(rip))
+            gdb.execute("set Context::currStack = " + str(currStack))
+        orig_thread.switch()
         FibreSupport.saved = False
 
     def prep_frame():
-- 
GitLab