[PATCH] libdebugger: Add a targte break call to suspending all running threads

chrisj at rtems.org chrisj at rtems.org
Mon Oct 17 20:35:16 UTC 2022


From: Chris Johns <chrisj at rtems.org>

- Optionally wait if there is no remote debugger connected and break
  when the remote connects

Closes #4740
---
 .../rtems/debugger/rtems-debugger-server.h    |  1 +
 cpukit/include/rtems/rtems-debugger.h         |  8 +++
 cpukit/libdebugger/rtems-debugger-server.c    | 72 ++++++++++++++-----
 3 files changed, 65 insertions(+), 16 deletions(-)

diff --git a/cpukit/include/rtems/debugger/rtems-debugger-server.h b/cpukit/include/rtems/debugger/rtems-debugger-server.h
index 2189aac873..4d0407a951 100644
--- a/cpukit/include/rtems/debugger/rtems-debugger-server.h
+++ b/cpukit/include/rtems/debugger/rtems-debugger-server.h
@@ -103,6 +103,7 @@ extern "C" {
 #define RTEMS_DEBUGGER_FLAG_MULTIPROCESS (1 << 4)
 #define RTEMS_DEBUGGER_FLAG_VERBOSE_LOCK (1 << 5)
 #define RTEMS_DEBUGGER_FLAG_VERBOSE_CMDS (1 << 6)
+#define RTEMS_DEBUGGER_FLAG_BREAK_WAITER (1 << 7)
 
 /**
  * Forward decl for the threads and targets.
diff --git a/cpukit/include/rtems/rtems-debugger.h b/cpukit/include/rtems/rtems-debugger.h
index 1fc8b3d522..7627e83382 100644
--- a/cpukit/include/rtems/rtems-debugger.h
+++ b/cpukit/include/rtems/rtems-debugger.h
@@ -53,6 +53,14 @@ extern int rtems_debugger_start(const char*          remote,
                                 rtems_task_priority  priority,
                                 const rtems_printer* printer);
 
+/**
+ * Suspend all running threads including the caller if not
+ * excluded. Returns when the debugger has connected and continued.
+ *
+ * If wait is true and there is no remote connected wait then break.
+ */
+extern int rtems_debugger_break(bool wait);
+
 /**
  * Stop the Debugger.
  */
diff --git a/cpukit/libdebugger/rtems-debugger-server.c b/cpukit/libdebugger/rtems-debugger-server.c
index 9de9421b6b..b7b9727d84 100644
--- a/cpukit/libdebugger/rtems-debugger-server.c
+++ b/cpukit/libdebugger/rtems-debugger-server.c
@@ -1678,18 +1678,30 @@ rtems_debugger_events(rtems_task_argument arg)
 
   rtems_debugger_target_enable();
 
-  while (rtems_debugger_server_events_running()) {
-    rtems_debugger_server_events_wait();
-    if (rtems_debugger_verbose())
-      rtems_debugger_printf("rtems-db: event woken\n");
-    if (!rtems_debugger_server_events_running())
-      break;
+  if (rtems_debugger_server_flag(RTEMS_DEBUGGER_FLAG_BREAK_WAITER)) {
+    rtems_debugger->flags &= ~RTEMS_DEBUGGER_FLAG_BREAK_WAITER;
     r = rtems_debugger_thread_system_suspend();
-    if (r < 0)
-      break;
-    r = remote_stop_reason(NULL, 0);
-    if (r < 0)
-      break;
+    if (rtems_debugger_verbose())
+      rtems_debugger_printf("rtems-db: break waiter\n");
+    rtems_debugger_server_events_signal();
+    if (rtems_debugger_verbose())
+      rtems_debugger_printf("rtems-db: break waiter: signalled\n");
+  }
+
+  if (r == 0) {
+    while (rtems_debugger_server_events_running()) {
+      rtems_debugger_server_events_wait();
+      if (rtems_debugger_verbose())
+        rtems_debugger_printf("rtems-db: event woken\n");
+      if (!rtems_debugger_server_events_running())
+        break;
+      r = rtems_debugger_thread_system_suspend();
+      if (r < 0)
+        break;
+      r = remote_stop_reason(NULL, 0);
+      if (r < 0)
+        break;
+    }
   }
 
   if (r < 0)
@@ -1972,6 +1984,30 @@ rtems_debugger_server_crash(void)
   rtems_debugger->remote->end(rtems_debugger->remote);
 }
 
+int
+rtems_debugger_break(bool wait)
+{
+  int r = 0;
+  if (!rtems_debugger_running()) {
+    errno = EIO;
+    r = -1;
+  } else {
+    rtems_debugger_lock();
+    if (rtems_debugger_server_events_running()) {
+      rtems_debugger_server_events_signal();
+    } else if (
+      wait && !rtems_debugger_server_flag(RTEMS_DEBUGGER_FLAG_BREAK_WAITER)) {
+      rtems_debugger->flags |= RTEMS_DEBUGGER_FLAG_BREAK_WAITER;
+      rtems_debugger_server_events_wait();
+    } else {
+      errno = EIO;
+      r = -1;
+    }
+    rtems_debugger_unlock();
+  }
+  return r;
+}
+
 int
 rtems_debugger_stop(void)
 {
@@ -1998,10 +2034,14 @@ rtems_debugger_set_verbose(bool on)
 int
 rtems_debugger_remote_debug(bool state)
 {
-  rtems_debugger_lock();
-  rtems_debugger->remote_debug = state;
-  rtems_debugger_printf("rtems-db: remote-debug is %s\n",
-                        rtems_debugger->remote_debug ? "on" : "off");
-  rtems_debugger_unlock();
+  if (rtems_debugger_running()) {
+    rtems_debugger_lock();
+    rtems_debugger->remote_debug = state;
+    rtems_debugger_printf("rtems-db: remote-debug is %s\n",
+                          rtems_debugger->remote_debug ? "on" : "off");
+    rtems_debugger_unlock();
+  } else {
+    rtems_debugger_printf("rtems-db: debug server not running\n");
+  }
   return 0;
 }
-- 
2.37.1



More information about the devel mailing list