Skip to contentSkip to conversation

gdbstub and TCG plugin improvements

PATCH Applied Akihiko Odaki 3 participants 18 messages Sep 12, 2023 – Sep 14, 2023

Subsystems (2)

v3 of 11 Applied Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover gdbstub and TCG plugin improvements

This series extracts fixes and refactorings that can be applied independently from "[PATCH RESEND v5 00/26] plugins: Allow to read registers" as suggested by Nicholas Piggin.

Patch "target/ppc: Remove references to gdb_has_xml" is also updated to remove some dead code I missed earlier and thus the Reviewed-by tag is dropped.

V2 -> V3:
Added patch "plugins: Check if vCPU is realized".

V1 -> V2:
Rebased.
Added patch "gdbstub: Fix target_xml initialization". Added patch "gdbstub: Fix target.xml response". Added patch "gdbstub: Replace gdb_regs with an array".

Akihiko Odaki (12):
gdbstub: Fix target_xml initialization
gdbstub: Fix target.xml response
plugins: Check if vCPU is realized contrib/plugins: Use GRWLock in execlog
gdbstub: Introduce GDBFeature structure target/arm: Move the reference to arm-core.xml hw/core/cpu: Return static value with gdb_arch_name()
gdbstub: Use g_markup_printf_escaped() target/arm: Remove references to gdb_has_xml target/ppc: Remove references to gdb_has_xml
gdbstub: Remove gdb_has_xml variable
gdbstub: Replace gdb_regs with an array

 MAINTAINERS               |  2 +-
 meson.build               |  2 +-
 gdbstub/internals.h       |  2 -
 include/exec/gdbstub.h    | 17 +++----
 include/hw/core/cpu.h     |  4 +-
 target/ppc/internal.h     |  2 +-
 contrib/plugins/execlog.c | 16 ++++---
 gdbstub/gdbstub.c         | 94 +++++++++++++++++++--------------------
 gdbstub/softmmu.c         |  2 +-
 plugins/core.c            |  2 +-
 stubs/gdbstub.c           |  6 +--
 target/arm/cpu.c          |  9 ++--
 target/arm/cpu64.c        |  4 +-
 target/arm/gdbstub.c      | 32 +------------
 target/i386/cpu.c         |  6 +--
 target/loongarch/cpu.c    |  8 ++--
 target/ppc/gdbstub.c      | 24 ++--------
 target/riscv/cpu.c        |  6 +--
 target/s390x/cpu.c        |  4 +-
 target/tricore/cpu.c      |  4 +-
 scripts/feature_to_c.py   | 48 ++++++++++++++++++++
 scripts/feature_to_c.sh   | 69 ----------------------------
 22 files changed, 146 insertions(+), 217 deletions(-)
 create mode 100755 scripts/feature_to_c.py
 delete mode 100644 scripts/feature_to_c.sh
Signature · 1 line
2.42.0
#1 lore raw

PATCH v3 1/12 gdbstub: Fix target_xml initialization

target_xml is no longer a fixed-length array but a pointer to a variable-length memory.

Fixes 56e534bd11 ("gdbstub: refactor get_feature_xml")
---
 gdbstub/softmmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub/softmmu.c +1 -1
index 9f0b8b5497..42645d2220 100644
@@ -292,7 +292,7 @@ static int find_cpu_clusters(Object *child, void *opaque)
         assert(cluster->cluster_id != UINT32_MAX);
         process->pid = cluster->cluster_id + 1;
         process->attached = false;
-        process->target_xml[0] = '\0';
+        process->target_xml = NULL;
 
         return 0;
     }
Signature · 1 line
2.42.0
Reviewed-by on v3 1/12
#2 lore raw

Re: [PATCH v3 01/12] gdbstub: Fix target_xml initialization

Quoted 14 lines · Akihiko Odaki

target_xml is no longer a fixed-length array but a pointer to a variable-length memory.

Fixes 56e534bd11 ("gdbstub: refactor get_feature_xml")
---
  gdbstub/softmmu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub/softmmu.c +0 -0
index 9f0b8b5497..42645d2220 100644

gdbstub/softmmu.c static int find_cpu_clusters(Object *child, void *opaque) · line 298

Quoted 8 lines
gdbstub/softmmu.c +1 -1
@@ -292,7 +292,7 @@ static int find_cpu_clusters(Object *child, void *opaque)
          assert(cluster->cluster_id != UINT32_MAX);
          process->pid = cluster->cluster_id + 1;
          process->attached = false;
-        process->target_xml[0] = '\0';
+        process->target_xml = NULL;
 
          return 0;
      }
#3 lore raw

PATCH v3 2/12 gdbstub: Fix target.xml response

In reply to Akihiko Odaki

It was failing to return target.xml after the first request.

Fixes 56e534bd11 ("gdbstub: refactor get_feature_xml")
---
 gdbstub/gdbstub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub/gdbstub.c +1 -1
index 349d348c7b..384191bcb0 100644
@@ -396,8 +396,8 @@ static const char *get_feature_xml(const char *p, const char **newp,
             g_string_append(xml, "</target>");
 
             process->target_xml = g_string_free(xml, false);
-            return process->target_xml;
         }
+        return process->target_xml;
     }
     /* Is it dynamically generated by the target? */
     if (cc->gdb_get_dynamic_xml) {
Signature · 1 line
2.42.0
#4 lore raw

PATCH v3 3/12 plugins: Check if vCPU is realized

In reply to Akihiko Odaki

The created member of CPUState tells if the vCPU thread is started, and will be always false for the user space emulation that manages threads independently. Use the realized member of DeviceState, which is valid for both of the system and user space emulation.

Fixes 54cb65d858 ("plugin: add core code")
---
 plugins/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
plugins/core.c +1 -1
index 3c4e26c7ed..fcd33a2bff 100644
@@ -64,7 +64,7 @@ static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
     CPUState *cpu = container_of(k, CPUState, cpu_index);
     run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
 
-    if (cpu->created) {
+    if (DEVICE(cpu)->realized) {
         async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
     } else {
         plugin_cpu_update__async(cpu, mask);
Signature · 1 line
2.42.0
#5 lore raw

Re: [PATCH v3 03/12] plugins: Check if vCPU is realized

Quoted 3 lines · Akihiko Odaki

The created member of CPUState tells if the vCPU thread is started, and will be always false for the user space emulation that manages threads independently.

Per the docstring:

/**
* CPUState:

* @created: Indicates whether the CPU thread has been
* successfully created.

Each CPU DeviceClass's DeviceRealize() handler which calls qemu_init_vcpu(). Ah, what we miss is:

-- >8 --

accel/tcg/user-exec-stub.c +1 -1
@@ -14,6 +14,7 @@ void cpu_remove_sync(CPUState *cpu)
 
 void qemu_init_vcpu(CPUState *cpu)
 {
+    cpu->created = true;
 }
---
 

Missed in commit c7f0f3b1c8 ("qtest: add test framework").

Does that help?

Quoted 22 lines

Use the realized member of DeviceState, which is valid for both of the system and user space emulation.

Fixes 54cb65d858 ("plugin: add core code")
---
  plugins/core.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
plugins/core.c +1 -1
index 3c4e26c7ed..fcd33a2bff 100644
@@ -64,7 +64,7 @@ static void plugin_cpu_update__locked(gpointer k, gpointer v, gpointer udata)
      CPUState *cpu = container_of(k, CPUState, cpu_index);
      run_on_cpu_data mask = RUN_ON_CPU_HOST_ULONG(*plugin.mask);
 
-    if (cpu->created) {
+    if (DEVICE(cpu)->realized) {
          async_run_on_cpu(cpu, plugin_cpu_update__async, mask);
      } else {
          plugin_cpu_update__async(cpu, mask);
#6 lore raw

Re: [PATCH v3 03/12] plugins: Check if vCPU is realized

Quoted 27 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Akihiko Odaki

The created member of CPUState tells if the vCPU thread is started, and will be always false for the user space emulation that manages threads independently.

Per the docstring:

 /**
  * CPUState:

  * @created: Indicates whether the CPU thread has been *           successfully created.

Each CPU DeviceClass's DeviceRealize() handler which calls qemu_init_vcpu(). Ah, what we miss is:

-- >8 --

accel/tcg/user-exec-stub.c +1 -1
@@ -14,6 +14,7 @@ void cpu_remove_sync(CPUState *cpu)
 
  void qemu_init_vcpu(CPUState *cpu)
  {
+    cpu->created = true;
  }
---
 
Quoted 1 line

Missed in commit c7f0f3b1c8 ("qtest: add test framework").

I think the member is never set for user space emulation since it was introduced with commit d6dc3d424e ("qemu: introduce iothread (Marcelo Tosatti)")

Quoted 2 lines

Does that help?

That will work, but I don't think it makes sense to set this member while the other members related to vCPU thread like the "thread" member are unset.

#7 lore raw

PATCH v3 4/12 contrib/plugins: Use GRWLock in execlog

In reply to Akihiko Odaki

execlog had the following comment:

Quoted 4 lines

As we could have multiple threads trying to do this we need to serialise the expansion under a lock. Threads accessing already created entries can continue without issue even if the ptr array gets reallocated during resize.

However, when the ptr array gets reallocated, the other threads may have a stale reference to the old buffer. This results in use-after-free.

Use GRWLock to properly fix this issue.

Fixes 3d7caf145e ("contrib/plugins: add execlog to log instruction execution and memory access")
---
 contrib/plugins/execlog.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
contrib/plugins/execlog.c +10 -6
index 7129d526f8..82dc2f584e 100644
@@ -19,7 +19,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
 
 /* Store last executed instruction on each vCPU as a GString */
 static GPtrArray *last_exec;
-static GMutex expand_array_lock;
+static GRWLock expand_array_lock;
 
 static GPtrArray *imatches;
 static GArray *amatches;
@@ -28,18 +28,16 @@ static GArray *amatches;
  * Expand last_exec array.
  *
  * As we could have multiple threads trying to do this we need to
- * serialise the expansion under a lock. Threads accessing already
- * created entries can continue without issue even if the ptr array
- * gets reallocated during resize.
+ * serialise the expansion under a lock.
  */
 static void expand_last_exec(int cpu_index)
 {
-    g_mutex_lock(&expand_array_lock);
+    g_rw_lock_writer_lock(&expand_array_lock);
     while (cpu_index >= last_exec->len) {
         GString *s = g_string_new(NULL);
         g_ptr_array_add(last_exec, s);
     }
-    g_mutex_unlock(&expand_array_lock);
+    g_rw_lock_writer_unlock(&expand_array_lock);
 }
 
 /**
@@ -51,8 +49,10 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t info,
     GString *s;
 
     /* Find vCPU in array */
+    g_rw_lock_reader_lock(&expand_array_lock);
     g_assert(cpu_index < last_exec->len);
     s = g_ptr_array_index(last_exec, cpu_index);
+    g_rw_lock_reader_unlock(&expand_array_lock);
 
     /* Indicate type of memory access */
     if (qemu_plugin_mem_is_store(info)) {
@@ -80,10 +80,14 @@ static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
     GString *s;
 
     /* Find or create vCPU in array */
+    g_rw_lock_reader_lock(&expand_array_lock);
     if (cpu_index >= last_exec->len) {
+        g_rw_lock_reader_unlock(&expand_array_lock);
         expand_last_exec(cpu_index);
+        g_rw_lock_reader_lock(&expand_array_lock);
     }
     s = g_ptr_array_index(last_exec, cpu_index);
+    g_rw_lock_reader_unlock(&expand_array_lock);
 
     /* Print previous instruction in cache */
     if (s->len) {
Signature · 1 line
2.42.0
#8 lore raw

PATCH v3 5/12 gdbstub: Introduce GDBFeature structure

In reply to Akihiko Odaki

Before this change, the information from a XML file was stored in an array that is not descriptive. Introduce a dedicated structure type to make it easier to understand and to extend with more fields.

---
 MAINTAINERS             |  2 +-
 meson.build             |  2 +-
 include/exec/gdbstub.h  |  9 ++++--
 gdbstub/gdbstub.c       |  6 ++--
 stubs/gdbstub.c         |  6 ++--
 scripts/feature_to_c.py | 48 ++++++++++++++++++++++++++++
 scripts/feature_to_c.sh | 69 -----------------------------------------
 7 files changed, 63 insertions(+), 79 deletions(-)
 create mode 100755 scripts/feature_to_c.py
 delete mode 100644 scripts/feature_to_c.sh
MAINTAINERS +1 -1
index 6111b6b4d9..61ff9234d3 100644
@@ -2826,7 +2826,7 @@ F: include/exec/gdbstub.h
 F: include/gdbstub/*
 F: gdb-xml/
 F: tests/tcg/multiarch/gdbstub/
-F: scripts/feature_to_c.sh
+F: scripts/feature_to_c.py
 F: scripts/probe-gdb-support.py
 
 Memory API
meson.build +1 -1
index 98e68ef0b1..5c633f7e01 100644
@@ -3683,7 +3683,7 @@ common_all = static_library('common',
                             dependencies: common_all.dependencies(),
                             name_suffix: 'fa')
 
-feature_to_c = find_program('scripts/feature_to_c.sh')
+feature_to_c = find_program('scripts/feature_to_c.py')
 
 if targetos == 'darwin'
   entitlement = find_program('scripts/entitlement.sh')
include/exec/gdbstub.h +7 -2
index 16a139043f..705be2c5d7 100644
@@ -10,6 +10,11 @@
 #define GDB_WATCHPOINT_READ      3
 #define GDB_WATCHPOINT_ACCESS    4
 
+typedef struct GDBFeature {
+    const char *xmlname;
+    const char *xml;
+} GDBFeature;
+
 
 /* Get or set a register.  Returns the size of the register.  */
 typedef int (*gdb_get_reg_cb)(CPUArchState *env, GByteArray *buf, int reg);
@@ -48,7 +53,7 @@ void gdb_set_stop_cpu(CPUState *cpu);
  */
 bool gdb_has_xml(void);
 
-/* in gdbstub-xml.c, generated by scripts/feature_to_c.sh */
-extern const char *const xml_builtin[][2];
+/* in gdbstub-xml.c, generated by scripts/feature_to_c.py */
+extern const GDBFeature gdb_static_features[];
 
 #endif
gdbstub/gdbstub.c +3 -3
index 384191bcb0..12f4d07046 100644
@@ -408,11 +408,11 @@ static const char *get_feature_xml(const char *p, const char **newp,
         }
     }
     /* Is it one of the encoded gdb-xml/ files? */
-    for (int i = 0; xml_builtin[i][0]; i++) {
-        const char *name = xml_builtin[i][0];
+    for (int i = 0; gdb_static_features[i].xmlname; i++) {
+        const char *name = gdb_static_features[i].xmlname;
         if ((strncmp(name, p, len) == 0) &&
             strlen(name) == len) {
-            return xml_builtin[i][1];
+            return gdb_static_features[i].xml;
         }
     }
 
stubs/gdbstub.c +3 -3
index 2b7aee50d3..580e20702b 100644
@@ -1,6 +1,6 @@
 #include "qemu/osdep.h"
-#include "exec/gdbstub.h"       /* xml_builtin */
+#include "exec/gdbstub.h"       /* gdb_static_features */
 
-const char *const xml_builtin[][2] = {
-  { NULL, NULL }
+const GDBFeature gdb_static_features[] = {
+  { NULL }
 };
scripts/feature_to_c.py new file +48 -0

48 lines not on this page · raw message

scripts/feature_to_c.sh deleted +0 -69

69 lines not on this page · raw message

Signature · 1 line
2.42.0
#9 lore raw

PATCH v3 6/12 target/arm: Move the reference to arm-core.xml

In reply to Akihiko Odaki

Some subclasses overwrite gdb_core_xml_file member but others don't. Always initialize the member in the subclasses for consistency.

This especially helps for AArch64; in a following change, the file specified by gdb_core_xml_file is always looked up even if it's going to be overwritten later. Looking up arm-core.xml results in an error as it will not be embedded in the AArch64 build.

---
 target/arm/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/arm/cpu.c +2 -1
index 0bb0585441..6ff6ff2d55 100644
@@ -2389,7 +2389,6 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
     cc->sysemu_ops = &arm_sysemu_ops;
 #endif
     cc->gdb_num_core_regs = 26;
-    cc->gdb_core_xml_file = "arm-core.xml";
     cc->gdb_arch_name = arm_gdb_arch_name;
     cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
     cc->gdb_stop_before_watchpoint = true;
@@ -2411,8 +2410,10 @@ static void arm_cpu_instance_init(Object *obj)
 static void cpu_register_class_init(ObjectClass *oc, void *data)
 {
     ARMCPUClass *acc = ARM_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(acc);
 
     acc->info = data;
+    cc->gdb_core_xml_file = "arm-core.xml";
 }
 
 void arm_cpu_register(const ARMCPUInfo *info)
Signature · 1 line
2.42.0
#10 lore raw

PATCH v3 7/12 hw/core/cpu: Return static value with gdb_arch_name()

In reply to Akihiko Odaki

All implementations of gdb_arch_name() returns dynamic duplicates of static strings. It's also unlikely that there will be an implementation of gdb_arch_name() that returns a truly dynamic value due to the nature of the function returning a well-known identifiers. Qualify the value gdb_arch_name() with const and make all of its implementations return static strings.

---
 include/hw/core/cpu.h  | 2 +-
 target/ppc/internal.h  | 2 +-
 gdbstub/gdbstub.c      | 3 +--
 target/arm/cpu.c       | 6 +++---
 target/arm/cpu64.c     | 4 ++--
 target/i386/cpu.c      | 6 +++---
 target/loongarch/cpu.c | 8 ++++----
 target/ppc/gdbstub.c   | 6 +++---
 target/riscv/cpu.c     | 6 +++---
 target/s390x/cpu.c     | 4 ++--
 target/tricore/cpu.c   | 4 ++--
 11 files changed, 25 insertions(+), 26 deletions(-)
include/hw/core/cpu.h +1 -1
index fdcbe87352..4f5c7eb04e 100644
@@ -164,7 +164,7 @@ struct CPUClass {
     vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
 
     const char *gdb_core_xml_file;
-    gchar * (*gdb_arch_name)(CPUState *cpu);
+    const gchar * (*gdb_arch_name)(CPUState *cpu);
     const char * (*gdb_get_dynamic_xml)(CPUState *cpu, const char *xmlname);
 
     void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
target/ppc/internal.h +1 -1
index 57acb3212c..974b37aa60 100644
@@ -221,7 +221,7 @@ void destroy_ppc_opcodes(PowerPCCPU *cpu);
 
 /* gdbstub.c */
 void ppc_gdb_init(CPUState *cs, PowerPCCPUClass *ppc);
-gchar *ppc_gdb_arch_name(CPUState *cs);
+const gchar *ppc_gdb_arch_name(CPUState *cs);
 
 /**
  * prot_for_access_type:
gdbstub/gdbstub.c +1 -2
index 12f4d07046..9db4af41c1 100644
@@ -380,10 +380,9 @@ static const char *get_feature_xml(const char *p, const char **newp,
                             "<target>");
 
             if (cc->gdb_arch_name) {
-                g_autofree gchar *arch = cc->gdb_arch_name(cpu);
                 g_string_append_printf(xml,
                                        "<architecture>%s</architecture>",
-                                       arch);
+                                       cc->gdb_arch_name(cpu));
             }
             g_string_append(xml, "<xi:include href=\"");
             g_string_append(xml, cc->gdb_core_xml_file);
target/arm/cpu.c +3 -3
index 6ff6ff2d55..a13c609249 100644
@@ -2316,15 +2316,15 @@ static Property arm_cpu_properties[] = {
     DEFINE_PROP_END_OF_LIST()
 };
 
-static gchar *arm_gdb_arch_name(CPUState *cs)
+static const gchar *arm_gdb_arch_name(CPUState *cs)
 {
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
 
     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
-        return g_strdup("iwmmxt");
+        return "iwmmxt";
     }
-    return g_strdup("arm");
+    return "arm";
 }
 
 #ifndef CONFIG_USER_ONLY
target/arm/cpu64.c +2 -2
index 96158093cc..6b91aab6b7 100644
@@ -743,9 +743,9 @@ static void aarch64_cpu_finalizefn(Object *obj)
 {
 }
 
-static gchar *aarch64_gdb_arch_name(CPUState *cs)
+static const gchar *aarch64_gdb_arch_name(CPUState *cs)
 {
-    return g_strdup("aarch64");
+    return "aarch64";
 }
 
 static void aarch64_cpu_class_init(ObjectClass *oc, void *data)
target/i386/cpu.c +3 -3
index 00f913b638..5678b52472 100644
@@ -5914,12 +5914,12 @@ static void x86_cpu_load_model(X86CPU *cpu, X86CPUModel *model)
     memset(&env->user_features, 0, sizeof(env->user_features));
 }
 
-static gchar *x86_gdb_arch_name(CPUState *cs)
+static const gchar *x86_gdb_arch_name(CPUState *cs)
 {
 #ifdef TARGET_X86_64
-    return g_strdup("i386:x86-64");
+    return "i386:x86-64";
 #else
-    return g_strdup("i386");
+    return "i386";
 #endif
 }
 
target/loongarch/cpu.c +4 -4
index 27fc6e1f33..f88cfa93ce 100644
@@ -763,9 +763,9 @@ static void loongarch_cpu_class_init(ObjectClass *c, void *data)
 #endif
 }
 
-static gchar *loongarch32_gdb_arch_name(CPUState *cs)
+static const gchar *loongarch32_gdb_arch_name(CPUState *cs)
 {
-    return g_strdup("loongarch32");
+    return "loongarch32";
 }
 
 static void loongarch32_cpu_class_init(ObjectClass *c, void *data)
@@ -777,9 +777,9 @@ static void loongarch32_cpu_class_init(ObjectClass *c, void *data)
     cc->gdb_arch_name = loongarch32_gdb_arch_name;
 }
 
-static gchar *loongarch64_gdb_arch_name(CPUState *cs)
+static const gchar *loongarch64_gdb_arch_name(CPUState *cs)
 {
-    return g_strdup("loongarch64");
+    return "loongarch64";
 }
 
 static void loongarch64_cpu_class_init(ObjectClass *c, void *data)
target/ppc/gdbstub.c +3 -3
index 2ad11510bf..778ef73bd7 100644
@@ -589,12 +589,12 @@ static int gdb_set_vsx_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
     return 0;
 }
 
-gchar *ppc_gdb_arch_name(CPUState *cs)
+const gchar *ppc_gdb_arch_name(CPUState *cs)
 {
 #if defined(TARGET_PPC64)
-    return g_strdup("powerpc:common64");
+    return "powerpc:common64";
 #else
-    return g_strdup("powerpc:common");
+    return "powerpc:common";
 #endif
 }
 
target/riscv/cpu.c +3 -3
index 6b93b04453..ef8faaaff5 100644
@@ -1939,17 +1939,17 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static gchar *riscv_gdb_arch_name(CPUState *cs)
+static const gchar *riscv_gdb_arch_name(CPUState *cs)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
 
     switch (riscv_cpu_mxl(env)) {
     case MXL_RV32:
-        return g_strdup("riscv:rv32");
+        return "riscv:rv32";
     case MXL_RV64:
     case MXL_RV128:
-        return g_strdup("riscv:rv64");
+        return "riscv:rv64";
     default:
         g_assert_not_reached();
     }
target/s390x/cpu.c +2 -2
index df167493c3..cf4b5e43f2 100644
@@ -284,9 +284,9 @@ static void s390_cpu_initfn(Object *obj)
 #endif
 }
 
-static gchar *s390_gdb_arch_name(CPUState *cs)
+static const gchar *s390_gdb_arch_name(CPUState *cs)
 {
-    return g_strdup("s390:64-bit");
+    return "s390:64-bit";
 }
 
 static Property s390x_cpu_properties[] = {
target/tricore/cpu.c +2 -2
index 133a9ac70e..44e3ba6f0e 100644
@@ -29,9 +29,9 @@ static inline void set_feature(CPUTriCoreState *env, int feature)
     env->features |= 1ULL << feature;
 }
 
-static gchar *tricore_gdb_arch_name(CPUState *cs)
+static const gchar *tricore_gdb_arch_name(CPUState *cs)
 {
-    return g_strdup("tricore");
+    return "tricore";
 }
 
 static void tricore_cpu_set_pc(CPUState *cs, vaddr value)
Signature · 1 line
2.42.0
#11 lore raw

PATCH v3 8/12 gdbstub: Use g_markup_printf_escaped()

In reply to Akihiko Odaki

g_markup_printf_escaped() is a safer alternative to simple printf() as it automatically escapes values.

---
 gdbstub/gdbstub.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
gdbstub/gdbstub.c +21 -15
index 9db4af41c1..a4f2bf3723 100644
@@ -373,28 +373,34 @@ static const char *get_feature_xml(const char *p, const char **newp,
     if (strncmp(p, "target.xml", len) == 0) {
         if (!process->target_xml) {
             GDBRegisterState *r;
-            GString *xml = g_string_new("<?xml version=\"1.0\"?>");
+            g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
 
-            g_string_append(xml,
-                            "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
-                            "<target>");
+            g_ptr_array_add(
+                xml,
+                g_strdup("<?xml version=\"1.0\"?>"
+                         "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
+                         "<target>"));
 
             if (cc->gdb_arch_name) {
-                g_string_append_printf(xml,
-                                       "<architecture>%s</architecture>",
-                                       cc->gdb_arch_name(cpu));
+                g_ptr_array_add(
+                    xml,
+                    g_markup_printf_escaped("<architecture>%s</architecture>",
+                                            cc->gdb_arch_name(cpu)));
             }
-            g_string_append(xml, "<xi:include href=\"");
-            g_string_append(xml, cc->gdb_core_xml_file);
-            g_string_append(xml, "\"/>");
+            g_ptr_array_add(
+                xml,
+                g_markup_printf_escaped("<xi:include href=\"%s\"/>",
+                                        cc->gdb_core_xml_file));
             for (r = cpu->gdb_regs; r; r = r->next) {
-                g_string_append(xml, "<xi:include href=\"");
-                g_string_append(xml, r->xml);
-                g_string_append(xml, "\"/>");
+                g_ptr_array_add(
+                    xml,
+                    g_markup_printf_escaped("<xi:include href=\"%s\"/>",
+                                            r->xml));
             }
-            g_string_append(xml, "</target>");
+            g_ptr_array_add(xml, g_strdup("</target>"));
+            g_ptr_array_add(xml, NULL);
 
-            process->target_xml = g_string_free(xml, false);
+            process->target_xml = g_strjoinv(NULL, (void *)xml->pdata);
         }
         return process->target_xml;
     }
Signature · 1 line
2.42.0
#12 lore raw

PATCH v3 9/12 target/arm: Remove references to gdb_has_xml

In reply to Akihiko Odaki

GDB has XML support since 6.7 which was released in 2007. It's time to remove support for old GDB versions without XML support.

---
 target/arm/gdbstub.c | 32 ++------------------------------
 1 file changed, 2 insertions(+), 30 deletions(-)
target/arm/gdbstub.c +2 -30
index 8fc8351df7..b7ace24bfc 100644
@@ -46,21 +46,7 @@ int arm_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
         /* Core integer register.  */
         return gdb_get_reg32(mem_buf, env->regs[n]);
     }
-    if (n < 24) {
-        /* FPA registers.  */
-        if (gdb_has_xml()) {
-            return 0;
-        }
-        return gdb_get_zeroes(mem_buf, 12);
-    }
-    switch (n) {
-    case 24:
-        /* FPA status register.  */
-        if (gdb_has_xml()) {
-            return 0;
-        }
-        return gdb_get_reg32(mem_buf, 0);
-    case 25:
+    if (n == 25) {
         /* CPSR, or XPSR for M-profile */
         if (arm_feature(env, ARM_FEATURE_M)) {
             return gdb_get_reg32(mem_buf, xpsr_read(env));
@@ -100,21 +86,7 @@ int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
         env->regs[n] = tmp;
         return 4;
     }
-    if (n < 24) { /* 16-23 */
-        /* FPA registers (ignored).  */
-        if (gdb_has_xml()) {
-            return 0;
-        }
-        return 12;
-    }
-    switch (n) {
-    case 24:
-        /* FPA status register (ignored).  */
-        if (gdb_has_xml()) {
-            return 0;
-        }
-        return 4;
-    case 25:
+    if (n == 25) {
         /* CPSR, or XPSR for M-profile */
         if (arm_feature(env, ARM_FEATURE_M)) {
             /*
Signature · 1 line
2.42.0
#13 lore raw

PATCH v3 10/12 target/ppc: Remove references to gdb_has_xml

In reply to Akihiko Odaki

GDB has XML support since 6.7 which was released in 2007. It's time to remove support for old GDB versions without XML support.

---
 target/ppc/gdbstub.c | 18 ------------------
 1 file changed, 18 deletions(-)
target/ppc/gdbstub.c +0 -18
index 778ef73bd7..ec5731e5d6 100644
@@ -54,12 +54,6 @@ static int ppc_gdb_register_len(int n)
     case 0 ... 31:
         /* gprs */
         return sizeof(target_ulong);
-    case 32 ... 63:
-        /* fprs */
-        if (gdb_has_xml()) {
-            return 0;
-        }
-        return 8;
     case 66:
         /* cr */
     case 69:
@@ -74,12 +68,6 @@ static int ppc_gdb_register_len(int n)
     case 68:
         /* ctr */
         return sizeof(target_ulong);
-    case 70:
-        /* fpscr */
-        if (gdb_has_xml()) {
-            return 0;
-        }
-        return sizeof(target_ulong);
     default:
         return 0;
     }
@@ -132,9 +120,6 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
     if (n < 32) {
         /* gprs */
         gdb_get_regl(buf, env->gpr[n]);
-    } else if (n < 64) {
-        /* fprs */
-        gdb_get_reg64(buf, *cpu_fpr_ptr(env, n - 32));
     } else {
         switch (n) {
         case 64:
@@ -158,9 +143,6 @@ int ppc_cpu_gdb_read_register(CPUState *cs, GByteArray *buf, int n)
         case 69:
             gdb_get_reg32(buf, cpu_read_xer(env));
             break;
-        case 70:
-            gdb_get_reg32(buf, env->fpscr);
-            break;
         }
     }
     mem_buf = buf->data + buf->len - r;
Signature · 1 line
2.42.0
#14 lore raw

PATCH v3 11/12 gdbstub: Remove gdb_has_xml variable

In reply to Akihiko Odaki

GDB has XML support since 6.7 which was released in 2007. It's time to remove support for old GDB versions without XML support.

---
 gdbstub/internals.h    |  2 --
 include/exec/gdbstub.h |  8 --------
 gdbstub/gdbstub.c      | 15 ---------------
 3 files changed, 25 deletions(-)
gdbstub/internals.h +0 -2
index fee243081f..7128c4aa85 100644
@@ -32,8 +32,6 @@ enum {
 typedef struct GDBProcess {
     uint32_t pid;
     bool attached;
-
-    /* If gdb sends qXfer:features:read:target.xml this will be populated */
     char *target_xml;
 } GDBProcess;
 
include/exec/gdbstub.h +0 -8
index 705be2c5d7..1a01c35f8e 100644
@@ -45,14 +45,6 @@ int gdbserver_start(const char *port_or_device);
 
 void gdb_set_stop_cpu(CPUState *cpu);
 
-/**
- * gdb_has_xml() - report of gdb supports modern target descriptions
- *
- * This will report true if the gdb negotiated qXfer:features:read
- * target descriptions.
- */
-bool gdb_has_xml(void);
-
 /* in gdbstub-xml.c, generated by scripts/feature_to_c.py */
 extern const GDBFeature gdb_static_features[];
 
gdbstub/gdbstub.c +0 -15
index a4f2bf3723..177dce9ba2 100644
@@ -349,11 +349,6 @@ static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
     }
 }
 
-bool gdb_has_xml(void)
-{
-    return !!gdb_get_cpu_process(gdbserver_state.g_cpu)->target_xml;
-}
-
 static const char *get_feature_xml(const char *p, const char **newp,
                                    GDBProcess *process)
 {
@@ -1086,11 +1081,6 @@ static void handle_set_reg(GArray *params, void *user_ctx)
 {
     int reg_size;
 
-    if (!gdb_get_cpu_process(gdbserver_state.g_cpu)->target_xml) {
-        gdb_put_packet("");
-        return;
-    }
-
     if (params->len != 2) {
         gdb_put_packet("E22");
         return;
@@ -1107,11 +1097,6 @@ static void handle_get_reg(GArray *params, void *user_ctx)
 {
     int reg_size;
 
-    if (!gdb_get_cpu_process(gdbserver_state.g_cpu)->target_xml) {
-        gdb_put_packet("");
-        return;
-    }
-
     if (!params->len) {
         gdb_put_packet("E14");
         return;
Signature · 1 line
2.42.0
#15 lore raw

PATCH v3 12/12 gdbstub: Replace gdb_regs with an array

In reply to Akihiko Odaki

An array is a more appropriate data structure than a list for gdb_regs since it is initialized only with append operation and read-only after initialization.

---
 include/hw/core/cpu.h |  2 +-
 gdbstub/gdbstub.c     | 34 ++++++++++++++++++++--------------
 2 files changed, 21 insertions(+), 15 deletions(-)
include/hw/core/cpu.h +1 -1
index 4f5c7eb04e..c84c631242 100644
@@ -375,7 +375,7 @@ struct CPUState {
 
     CPUJumpCache *tb_jmp_cache;
 
-    struct GDBRegisterState *gdb_regs;
+    GArray *gdb_regs;
     int gdb_num_regs;
     int gdb_num_g_regs;
     QTAILQ_ENTRY(CPUState) node;
gdbstub/gdbstub.c +20 -14
index 177dce9ba2..9810d15278 100644
@@ -51,7 +51,6 @@ typedef struct GDBRegisterState {
     gdb_get_reg_cb get_reg;
     gdb_set_reg_cb set_reg;
     const char *xml;
-    struct GDBRegisterState *next;
 } GDBRegisterState;
 
 GDBState gdbserver_state;
@@ -386,7 +385,8 @@ static const char *get_feature_xml(const char *p, const char **newp,
                 xml,
                 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
                                         cc->gdb_core_xml_file));
-            for (r = cpu->gdb_regs; r; r = r->next) {
+            for (guint i = 0; i < cpu->gdb_regs->len; i++) {
+                r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
                 g_ptr_array_add(
                     xml,
                     g_markup_printf_escaped("<xi:include href=\"%s\"/>",
@@ -430,7 +430,8 @@ static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
         return cc->gdb_read_register(cpu, buf, reg);
     }
 
-    for (r = cpu->gdb_regs; r; r = r->next) {
+    for (guint i = 0; i < cpu->gdb_regs->len; i++) {
+        r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
             return r->get_reg(env, buf, reg - r->base_reg);
         }
@@ -448,7 +449,8 @@ static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
         return cc->gdb_write_register(cpu, mem_buf, reg);
     }
 
-    for (r = cpu->gdb_regs; r; r = r->next) {
+    for (guint i = 0; i < cpu->gdb_regs->len; i++) {
+        r =  &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
             return r->set_reg(env, mem_buf, reg - r->base_reg);
         }
@@ -461,17 +463,22 @@ void gdb_register_coprocessor(CPUState *cpu,
                               int num_regs, const char *xml, int g_pos)
 {
     GDBRegisterState *s;
-    GDBRegisterState **p;
-
-    p = &cpu->gdb_regs;
-    while (*p) {
-        /* Check for duplicates.  */
-        if (strcmp((*p)->xml, xml) == 0)
-            return;
-        p = &(*p)->next;
+    guint i;
+
+    if (cpu->gdb_regs) {
+        for (i = 0; i < cpu->gdb_regs->len; i++) {
+            /* Check for duplicates.  */
+            s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
+            if (strcmp(s->xml, xml) == 0)
+                return;
+        }
+    } else {
+        cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState));
+        i = 0;
     }
 
-    s = g_new0(GDBRegisterState, 1);
+    g_array_set_size(cpu->gdb_regs, i + 1);
+    s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
     s->base_reg = cpu->gdb_num_regs;
     s->num_regs = num_regs;
     s->get_reg = get_reg;
@@ -480,7 +487,6 @@ void gdb_register_coprocessor(CPUState *cpu,
 
     /* Add to end of list.  */
     cpu->gdb_num_regs += num_regs;
-    *p = s;
     if (g_pos) {
         if (g_pos != s->base_reg) {
             error_report("Error: Bad gdb register numbering for '%s', "
Signature · 1 line
2.42.0
Applied
#16 lore raw
In reply to Akihiko Odaki
Quoted 7 lines · Akihiko Odaki

This series extracts fixes and refactorings that can be applied independently from "[PATCH RESEND v5 00/26] plugins: Allow to read registers" as suggested by Nicholas Piggin.

Patch "target/ppc: Remove references to gdb_has_xml" is also updated to remove some dead code I missed earlier and thus the Reviewed-by tag is dropped.

Queued to gdbstub/next, thanks.

Quoted 49 lines

V2 -> V3:
Added patch "plugins: Check if vCPU is realized".

V1 -> V2:
Rebased.
Added patch "gdbstub: Fix target_xml initialization". Added patch "gdbstub: Fix target.xml response". Added patch "gdbstub: Replace gdb_regs with an array".

Akihiko Odaki (12):
gdbstub: Fix target_xml initialization
gdbstub: Fix target.xml response
plugins: Check if vCPU is realized contrib/plugins: Use GRWLock in execlog
gdbstub: Introduce GDBFeature structure target/arm: Move the reference to arm-core.xml hw/core/cpu: Return static value with gdb_arch_name()
gdbstub: Use g_markup_printf_escaped() target/arm: Remove references to gdb_has_xml target/ppc: Remove references to gdb_has_xml
gdbstub: Remove gdb_has_xml variable
gdbstub: Replace gdb_regs with an array

 MAINTAINERS               |  2 +-
 meson.build               |  2 +-
 gdbstub/internals.h       |  2 -
 include/exec/gdbstub.h    | 17 +++----
 include/hw/core/cpu.h     |  4 +-
 target/ppc/internal.h     |  2 +-
 contrib/plugins/execlog.c | 16 ++++---
 gdbstub/gdbstub.c         | 94 +++++++++++++++++++--------------------
 gdbstub/softmmu.c         |  2 +-
 plugins/core.c            |  2 +-
 stubs/gdbstub.c           |  6 +--
 target/arm/cpu.c          |  9 ++--
 target/arm/cpu64.c        |  4 +-
 target/arm/gdbstub.c      | 32 +------------
 target/i386/cpu.c         |  6 +--
 target/loongarch/cpu.c    |  8 ++--
 target/ppc/gdbstub.c      | 24 ++--------
 target/riscv/cpu.c        |  6 +--
 target/s390x/cpu.c        |  4 +-
 target/tricore/cpu.c      |  4 +-
 scripts/feature_to_c.py   | 48 ++++++++++++++++++++
 scripts/feature_to_c.sh   | 69 ----------------------------
 22 files changed, 146 insertions(+), 217 deletions(-)
 create mode 100755 scripts/feature_to_c.py
 delete mode 100644 scripts/feature_to_c.sh
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#17 lore raw
Quoted 7 lines · Alex Bennée
Quoted 4 lines · Akihiko Odaki

This series extracts fixes and refactorings that can be applied independently from "[PATCH RESEND v5 00/26] plugins: Allow to read registers" as suggested by Nicholas Piggin.

Quoted 5 lines
Quoted 3 lines

Patch "target/ppc: Remove references to gdb_has_xml" is also updated to remove some dead code I missed earlier and thus the Reviewed-by tag is dropped.

Queued to gdbstub/next, thanks.

I left a comment in patch #3:
https://lore.kernel.org/qemu-devel/[email protected]/

If you agree with the comment I can send a patch to replace so you can keep this series queued.

Regards,

Phil.