From 3077eac9cfe11d28a71eedc5c513182ce7305bc5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Louis-Francis=20Ratt=C3=A9-Boulianne?= <lfrb@collabora.com>
Date: Fri, 16 Dec 2016 03:06:31 -0500
Subject: [PATCH] Wait for the GDB server to be listening before doing anything
 else
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We were trying to connect the GDB client even before we got the
listening event. The debugger server thread is now looping forever.

Signed-off-by: Louis-Francis Ratté-Boulianne <lfrb@collabora.com>
Reviewed-by: Sjoerd Simons <sjoerd.simons@collabora.co.uk>
Differential Revision: https://phabricator.apertis.org/D5327
---
 tools/ade | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/tools/ade b/tools/ade
index 6c5c6be..47f4428 100755
--- a/tools/ade
+++ b/tools/ade
@@ -177,19 +177,39 @@ class TargetTriplet:
 
 class DebuggerServerThread(threading.Thread):
 
-    def __init__(self, target, port, app, *args):
+    def __init__(self, cond, target, port, app, *args):
         super().__init__()
         self.target = target
         self.port = port
         self.app = app
         self.args = args
 
+        self._cond = cond
+        self._exception = None
+        self._listening = False
+
     def run(self):
-        self._data = self.target.start_gdbserver(self.port, self.app, *self.args)
+        try:
+            self._data = self.target.start_gdbserver(self.port, self.app, *self.args)
+            with self._cond:
+                self._listening = True
+                self._cond.notify()
+            while True:
+                pass
+        except Exception as e:
+            with self._cond:
+                self._exception = e
+                self._cond.notify()
 
     def stop(self):
         self.target.stop_gdbserver(self._data)
 
+    def is_listening(self):
+        return self._listening
+
+    def get_exception(self):
+        return self._exception
+
 
 class DebuggerServer:
 
@@ -201,8 +221,14 @@ class DebuggerServer:
         self._thread = None
 
     def __enter__(self):
-        self._thread = DebuggerServerThread(self.target, self.port, self.app, *self.args)
+        cond = threading.Condition()
+        self._thread = DebuggerServerThread(cond, self.target, self.port, self.app, *self.args)
         self._thread.start()
+        with cond:
+            while not self._thread.is_listening() and not self._thread.get_exception():
+                cond.wait()
+        if self._thread.get_exception():
+            raise self._thread.get_exception()
         return self
 
     def __exit__(self, et, ev, tb):
-- 
GitLab