The stop reply message we send can include a lot of extra information
and a bunch is mode dependant. Extract the construction into a helper
and add specialised versions for system and user mode.

The correct response for system mode should be of the form:

  T05core:N;

Where N is the core ID.

Signed-off-by: Alex Bennée <[email protected]>
---
 gdbstub/internals.h |  7 +++++++
 gdbstub/gdbstub.c   | 15 ++++++---------
 gdbstub/system.c    | 11 +++++++++++
 gdbstub/user.c      | 12 ++++++++++++
 4 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/gdbstub/internals.h b/gdbstub/internals.h
index 92466b28c18..3134a6e8eb2 100644
--- a/gdbstub/internals.h
+++ b/gdbstub/internals.h
@@ -237,4 +237,11 @@ void gdb_breakpoint_remove_all(CPUState *cs);
 int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
                                uint8_t *buf, int len, bool is_write);
 
+/**
+ * gdb_build_stop_packet() - craft the stop packet
+ * @cs: CPUState
+ */
+
+void gdb_build_stop_packet(CPUState *cs);
+
 #endif /* GDBSTUB_INTERNALS_H */
diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c
index d4db7ba30cc..c82fb5ad324 100644
--- a/gdbstub/gdbstub.c
+++ b/gdbstub/gdbstub.c
@@ -1431,11 +1431,10 @@ static void handle_v_attach(GArray *params, void 
*user_ctx)
         gdbserver_state.g_cpu = cpu;
         gdbserver_state.c_cpu = cpu;
 
-    if (gdbserver_state.allow_stop_reply) {
-        g_string_printf(gdbserver_state.str_buf, "T%02xthread:", 
GDB_SIGNAL_TRAP);
-        gdb_append_thread_id(cpu, gdbserver_state.str_buf);
-        g_string_append_c(gdbserver_state.str_buf, ';');
-        gdbserver_state.allow_stop_reply = false;
+        if (gdbserver_state.allow_stop_reply) {
+            gdb_build_stop_packet(cpu);
+            gdbserver_state.allow_stop_reply = false;
+        }
     }
 
     gdb_put_strbuf();
@@ -2037,11 +2036,9 @@ static void handle_gen_set(GArray *params, void 
*user_ctx)
 static void handle_target_halt(GArray *params, void *user_ctx)
 {
     if (gdbserver_state.allow_stop_reply) {
-        g_string_printf(gdbserver_state.str_buf, "T%02xthread:", 
GDB_SIGNAL_TRAP);
-        gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
-        g_string_append_c(gdbserver_state.str_buf, ';');
-        gdb_put_strbuf();
+        gdb_build_stop_packet(gdbserver_state.c_cpu);
         gdbserver_state.allow_stop_reply = false;
+        gdb_put_strbuf();
     }
     /*
      * Remove all the breakpoints when this query is issued,
diff --git a/gdbstub/system.c b/gdbstub/system.c
index 79f80256e3a..6963c930b01 100644
--- a/gdbstub/system.c
+++ b/gdbstub/system.c
@@ -662,3 +662,14 @@ void gdb_breakpoint_remove_all(CPUState *cs)
         ops->remove_all_breakpoints(cs);
     }
 }
+
+/*
+ * The minimal system-mode stop reply packet is:
+ *   T05core:{id};
+ */
+
+void gdb_build_stop_packet(CPUState *cs)
+{
+    g_string_printf(gdbserver_state.str_buf,
+                    "T%02xcore:%02x;", GDB_SIGNAL_TRAP, gdb_get_cpu_index(cs));
+}
diff --git a/gdbstub/user.c b/gdbstub/user.c
index a2327c61352..cc71cda3e33 100644
--- a/gdbstub/user.c
+++ b/gdbstub/user.c
@@ -974,3 +974,15 @@ void gdb_handle_query_xfer_siginfo(GArray *params, void 
*user_ctx)
     gdb_put_packet_binary(gdbserver_state.str_buf->str,
                           gdbserver_state.str_buf->len, true);
 }
+
+/*
+ * The minimal user-mode stop reply packet is:
+ *   T05thread:{id};
+ */
+
+void gdb_build_stop_packet(CPUState *cs)
+{
+    g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
+    gdb_append_thread_id(cs, gdbserver_state.str_buf);
+    g_string_append_c(gdbserver_state.str_buf, ';');
+}
-- 
2.47.3


Reply via email to