Skip to contentSkip to conversation

gdbstub: Re-factor gdb command extensions

RFC PATCH In review Alex Bennée 4 participants 10 messages Jul 16, 2024 – Jul 17, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 gdbstub: Re-factor gdb command extensions

Coverity reported a memory leak (CID 1549757) in this code and its admittedly rather clumsy handling of extending the command table. Instead of handing over a full array of the commands lets use the lighter weight GPtrArray and simply test for the presence of each entry as we go. This avoids complications of transferring ownership of arrays and keeps the final command entries as static entries in the target code.

Cc Akihiko Odaki <[email protected]>
Cc Gustavo Bueno Romero <[email protected]>
Cc Peter Maydell <[email protected]>
---
 include/gdbstub/commands.h |  19 ++++--
 target/arm/internals.h     |   4 +-
 gdbstub/gdbstub.c          | 127 +++++++++++++++++++++----------------
 target/arm/gdbstub.c       |  16 ++---
 target/arm/gdbstub64.c     |  11 ++--
 5 files changed, 95 insertions(+), 82 deletions(-)
include/gdbstub/commands.h +12 -7
index f3058f9dda..40f0514fe9 100644
@@ -74,23 +74,28 @@ int gdb_put_packet(const char *buf);
 
 /**
  * gdb_extend_query_table() - Extend query table.
- * @table: The table with the additional query packet handlers.
- * @size: The number of handlers to be added.
+ * @table: GPtrArray of GdbCmdParseEntry entries.
+ *
+ * The caller should free @table afterwards
  */
-void gdb_extend_query_table(GdbCmdParseEntry *table, int size);
+void gdb_extend_query_table(GPtrArray *table);
 
 /**
  * gdb_extend_set_table() - Extend set table.
- * @table: The table with the additional set packet handlers.
- * @size: The number of handlers to be added.
+ * @table: GPtrArray of GdbCmdParseEntry entries.
+ *
+ * The caller should free @table afterwards
  */
-void gdb_extend_set_table(GdbCmdParseEntry *table, int size);
+void gdb_extend_set_table(GPtrArray *table);
 
 /**
  * gdb_extend_qsupported_features() - Extend the qSupported features string.
  * @qsupported_features: The additional qSupported feature(s) string. The string
  * should start with a semicolon and, if there are more than one feature, the
- * features should be separate by a semiocolon.
+ * features should be separate by a semicolon.
+ *
+ * The caller should free @qsupported_features afterwards if
+ * dynamically allocated.
  */
 void gdb_extend_qsupported_features(char *qsupported_features);
 
target/arm/internals.h +2 -2
index da22d04121..757b1fae92 100644
@@ -359,8 +359,8 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);
 void arm_translate_init(void);
 
 void arm_cpu_register_gdb_commands(ARMCPU *cpu);
-void aarch64_cpu_register_gdb_commands(ARMCPU *cpu, GString *, GArray *,
-                                       GArray *);
+void aarch64_cpu_register_gdb_commands(ARMCPU *cpu, GString *,
+                                       GPtrArray *, GPtrArray *);
 
 void arm_restore_state_to_opc(CPUState *cs,
                               const TranslationBlock *tb,
gdbstub/gdbstub.c +71 -56

199 lines not on this page · raw message

target/arm/gdbstub.c +5 -11
index c3a9b5eb1e..554b8736bb 100644
@@ -477,11 +477,9 @@ static GDBFeature *arm_gen_dynamic_m_secextreg_feature(CPUState *cs,
 
 void arm_cpu_register_gdb_commands(ARMCPU *cpu)
 {
-    GArray *query_table =
-        g_array_new(FALSE, FALSE, sizeof(GdbCmdParseEntry));
-    GArray *set_table =
-        g_array_new(FALSE, FALSE, sizeof(GdbCmdParseEntry));
-    GString *qsupported_features = g_string_new(NULL);
+    g_autoptr(GPtrArray) query_table = g_ptr_array_new();
+    g_autoptr(GPtrArray) set_table = g_ptr_array_new();
+    g_autoptr(GString) qsupported_features = g_string_new(NULL);
 
     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
     #ifdef TARGET_AARCH64
@@ -492,16 +490,12 @@ void arm_cpu_register_gdb_commands(ARMCPU *cpu)
 
     /* Set arch-specific handlers for 'q' commands. */
     if (query_table->len) {
-        gdb_extend_query_table(&g_array_index(query_table,
-                                              GdbCmdParseEntry, 0),
-                                              query_table->len);
+        gdb_extend_query_table(query_table);
     }
 
     /* Set arch-specific handlers for 'Q' commands. */
     if (set_table->len) {
-        gdb_extend_set_table(&g_array_index(set_table,
-                             GdbCmdParseEntry, 0),
-                             set_table->len);
+        gdb_extend_set_table(set_table);
     }
 
     /* Set arch-specific qSupported feature. */
target/arm/gdbstub64.c +5 -6
index 2e2bc2700b..c8cef8cbc0 100644
@@ -564,7 +564,7 @@ enum Command {
     NUM_CMDS
 };
 
-static GdbCmdParseEntry cmd_handler_table[NUM_CMDS] = {
+static const GdbCmdParseEntry cmd_handler_table[NUM_CMDS] = {
     [qMemTags] = {
         .handler = handle_q_memtag,
         .cmd_startswith = true,
@@ -590,17 +590,16 @@ static GdbCmdParseEntry cmd_handler_table[NUM_CMDS] = {
 #endif /* CONFIG_USER_ONLY */
 
 void aarch64_cpu_register_gdb_commands(ARMCPU *cpu, GString *qsupported,
-                                       GArray *qtable, GArray *stable)
+                                       GPtrArray *qtable, GPtrArray *stable)
 {
 #ifdef CONFIG_USER_ONLY
     /* MTE */
     if (cpu_isar_feature(aa64_mte, cpu)) {
         g_string_append(qsupported, ";memory-tagging+");
 
-        g_array_append_val(qtable, cmd_handler_table[qMemTags]);
-        g_array_append_val(qtable, cmd_handler_table[qIsAddressTagged]);
-
-        g_array_append_val(stable, cmd_handler_table[QMemTags]);
+        g_ptr_array_add(qtable, (gpointer) &cmd_handler_table[qMemTags]);
+        g_ptr_array_add(qtable, (gpointer) &cmd_handler_table[qIsAddressTagged]);
+        g_ptr_array_add(stable, (gpointer) &cmd_handler_table[QMemTags]);
     }
 #endif
 }
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

Hi Alex,

Quoted 7 lines · Alex Bennée

Coverity reported a memory leak (CID 1549757) in this code and its admittedly rather clumsy handling of extending the command table. Instead of handing over a full array of the commands lets use the lighter weight GPtrArray and simply test for the presence of each entry as we go. This avoids complications of transferring ownership of arrays and keeps the final command entries as static entries in the target code.

How did you run Coverity to find the leak? I'm wondering what's the quickest way to check it next time.

Quoted 4 lines
Cc Akihiko Odaki <[email protected]>
Cc Gustavo Bueno Romero <[email protected]>
Cc Peter Maydell <[email protected]>

Cheers,
Gustavo

#2 lore raw
Quoted 10 lines · Gustavo Romero

Hi Alex,

Quoted 7 lines · Alex Bennée

Coverity reported a memory leak (CID 1549757) in this code and its admittedly rather clumsy handling of extending the command table. Instead of handing over a full array of the commands lets use the lighter weight GPtrArray and simply test for the presence of each entry as we go. This avoids complications of transferring ownership of arrays and keeps the final command entries as static entries in the target code.

Quoted 2 lines

How did you run Coverity to find the leak? I'm wondering what's the quickest way to check it next time.

Coverity is only run in the cloud on the released build. There is a container somewhere but I don't know how its used.

I did test on a build with --enable-sanitizers though.

Quoted 12 lines
Quoted 6 lines
Cc Akihiko Odaki <[email protected]>
Cc Gustavo Bueno Romero <[email protected]>
Cc Peter Maydell <[email protected]>

Cheers,
Gustavo

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw
Quoted 13 lines · Alex Bennée
Quoted 10 lines · Gustavo Romero

Hi Alex,

Quoted 7 lines · Alex Bennée

Coverity reported a memory leak (CID 1549757) in this code and its admittedly rather clumsy handling of extending the command table. Instead of handing over a full array of the commands lets use the lighter weight GPtrArray and simply test for the presence of each entry as we go. This avoids complications of transferring ownership of arrays and keeps the final command entries as static entries in the target code.

Quoted 5 lines
Quoted 2 lines

How did you run Coverity to find the leak? I'm wondering what's the quickest way to check it next time.

Coverity is only run in the cloud on the released build. There is a container somewhere but I don't know how its used.

The Coverity cloud stuff comes in two parts:
(1) you build locally with the Coverity tools, which creates
a big opaque build-artefact
(2) you upload that artefact to the cloud server, and the
actual analysis happens on the cloud

The container stuff and the integration with the Gitlab CI is there for the sole purpose of automating the "local build and upload" steps. You can't do an analysis-run locally. (Well, you probably can if you have the paid-for commercial version of the tooling, but we haven't got any kind of setup for doing that.)

We only do analysis runs on head-of-git because the Coverity Scan resource limits for open source projects give us about one complete scan a day. So this is all "after the fact" stuff. Developers who want to look at the scan results can create an account via https://scan.coverity.com/projects/qemu . Triaging new coverity reports is a bit tedious because there are a ton of false positives...

thanks
-- PMM

#4 lore raw

Hi Peter,

Quoted 33 lines · Peter Maydell
Quoted 13 lines · Alex Bennée
Quoted 10 lines · Gustavo Romero

Hi Alex,

Quoted 7 lines · Alex Bennée

Coverity reported a memory leak (CID 1549757) in this code and its admittedly rather clumsy handling of extending the command table. Instead of handing over a full array of the commands lets use the lighter weight GPtrArray and simply test for the presence of each entry as we go. This avoids complications of transferring ownership of arrays and keeps the final command entries as static entries in the target code.

Quoted 5 lines
Quoted 2 lines

How did you run Coverity to find the leak? I'm wondering what's the quickest way to check it next time.

Coverity is only run in the cloud on the released build. There is a container somewhere but I don't know how its used.

The Coverity cloud stuff comes in two parts:
(1) you build locally with the Coverity tools, which creates
a big opaque build-artefact
(2) you upload that artefact to the cloud server, and the
actual analysis happens on the cloud

The container stuff and the integration with the Gitlab CI is there for the sole purpose of automating the "local build and upload" steps. You can't do an analysis-run locally. (Well, you probably can if you have the paid-for commercial version of the tooling, but we haven't got any kind of setup for doing that.)

Quoted 7 lines

We only do analysis runs on head-of-git because the Coverity Scan resource limits for open source projects give us about one complete scan a day. So this is all "after the fact" stuff. Developers who want to look at the scan results can create an account via https://scan.coverity.com/projects/qemu . Triaging new coverity reports is a bit tedious because there are a ton of false positives...

Thanks for the explanation!

Cheers,
Gustavo

#5 lore raw
In reply to Alex Bennée
Quoted 4 lines · Alex Bennée
  void gdb_extend_qsupported_features(char *qsupported_features)
  {
gdbstub/gdbstub.c +0 -2
-    /*
-     * We don't support different sets of CPU gdb features on different CPUs yet

gdbstub/gdbstub.c static void handle_query_thread_extra(GArray *params, void *user_ctx) · line 1623

Quoted 10 lines
gdbstub/gdbstub.c +3 -7
-     * so assert the feature strings are the same on all CPUs, or is set only
-     * once (1 CPU).
-     */
-    g_assert(extended_qsupported_features == NULL ||
-             g_strcmp0(extended_qsupported_features, qsupported_features) == 0);
-
-    extended_qsupported_features = qsupported_features;
+    if (!extended_qsupported_features) {
+        extended_qsupported_features = g_strdup(qsupported_features);
+    } else if (!g_strrstr(extended_qsupported_features, qsupported_features)) {

Did you really need the last instance of the substring?

I'll note that g_strrstr is quite simplistic, whereas strstr has a much more scalable algorithm.

gdbstub/gdbstub.c static void handle_query_thread_extra(GArray *params, void *user_ctx) · line 1625

Quoted 2 lines
gdbstub/gdbstub.c +2 -0
+        char *old = extended_qsupported_features;
+        extended_qsupported_features = g_strdup_printf("%s%s", old, qsupported_features);

Right tool for the right job, please: g_strconcat().

That said, did you *really* want to concatenate now, and have to search through the middle, as opposed to storing N strings separately? You could defer the concat until the actual negotiation with gdb. That would reduce strstr above to a loop over strcmp.

gdbstub/gdbstub.c static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { · line 1776

Quoted 4 lines
gdbstub/gdbstub.c +4 -0
+    for (int i = 0; i < extensions->len; i++) {
+        gpointer entry = g_ptr_array_index(extensions, i);
+        if (!g_ptr_array_find(table, entry, NULL)) {
+            g_ptr_array_add(table, entry);

Are you expecting the same GdbCmdParseEntry object to be registered multiple times? Can we fix that at a higher level?

r~

#6 lore raw
Quoted 16 lines · Richard Henderson
Quoted 4 lines · Alex Bennée
  void gdb_extend_qsupported_features(char *qsupported_features)
  {
gdbstub/gdbstub.c +0 -2
-    /*
-     * We don't support different sets of CPU gdb features on different CPUs yet

gdbstub/gdbstub.c static void handle_query_thread_extra(GArray *params, void *user_ctx) · line 1623

Quoted 10 lines
gdbstub/gdbstub.c +3 -7
-     * so assert the feature strings are the same on all CPUs, or is set only
-     * once (1 CPU).
-     */
-    g_assert(extended_qsupported_features == NULL ||
-             g_strcmp0(extended_qsupported_features, qsupported_features) == 0);
-
-    extended_qsupported_features = qsupported_features;
+    if (!extended_qsupported_features) {
+        extended_qsupported_features = g_strdup(qsupported_features);
+    } else if (!g_strrstr(extended_qsupported_features, qsupported_features)) {
Quoted 1 line

Did you really need the last instance of the substring?

Not really - I just want to check the string hasn't been added before.

Quoted 15 lines

I'll note that g_strrstr is quite simplistic, whereas strstr has a much more scalable algorithm.

gdbstub/gdbstub.c static void handle_query_thread_extra(GArray *params, void *user_ctx) · line 1625

Quoted 2 lines
gdbstub/gdbstub.c +2 -0
+        char *old = extended_qsupported_features;
+        extended_qsupported_features = g_strdup_printf("%s%s", old, qsupported_features);

Right tool for the right job, please: g_strconcat().

That said, did you *really* want to concatenate now, and have to search through the middle, as opposed to storing N strings separately? You could defer the concat until the actual negotiation with gdb. That would reduce strstr above to a loop over strcmp.

Quoted 7 lines

gdbstub/gdbstub.c static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { · line 1776

Quoted 4 lines
gdbstub/gdbstub.c +4 -0
+    for (int i = 0; i < extensions->len; i++) {
+        gpointer entry = g_ptr_array_index(extensions, i);
+        if (!g_ptr_array_find(table, entry, NULL)) {
+            g_ptr_array_add(table, entry);

Are you expecting the same GdbCmdParseEntry object to be registered multiple times? Can we fix that at a higher level?

Its basically a hack to deal with the fact everything is tied to the CPUObject so we register everything multiple times. We could do a if (!registerd) register() dance but I guess I'm thinking forward to a hydrogenous future but I guess we'd need to do more work then anyway.

Quoted 3 lines

r~

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#7 lore raw
Quoted 7 lines · Alex Bennée
Quoted 2 lines

Are you expecting the same GdbCmdParseEntry object to be registered multiple times? Can we fix that at a higher level?

Its basically a hack to deal with the fact everything is tied to the CPUObject so we register everything multiple times. We could do a if (!registerd) register() dance but I guess I'm thinking forward to a hydrogenous future but I guess we'd need to do more work then anyway.

Any chance we could move it all to the CPUClass?

r~

#8 lore raw
Quoted 9 lines · Richard Henderson
Quoted 6 lines · Alex Bennée
Quoted 2 lines

Are you expecting the same GdbCmdParseEntry object to be registered multiple times? Can we fix that at a higher level?

Its basically a hack to deal with the fact everything is tied to the CPUObject so we register everything multiple times. We could do a if (!registerd) register() dance but I guess I'm thinking forward to a hydrogenous future but I guess we'd need to do more work then anyway.

Any chance we could move it all to the CPUClass?

We would have to move quite a bunch of stuff, including the system register creation code. I don't think that's a re-factor I want to attempt quite so close to soft-freeze.

Quoted 3 lines

r~

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#9 lore raw
In reply to Richard Henderson
Quoted 11 lines · Richard Henderson
Quoted 7 lines · Alex Bennée
Quoted 2 lines

Are you expecting the same GdbCmdParseEntry object to be registered multiple times? Can we fix that at a higher level?

Its basically a hack to deal with the fact everything is tied to the CPUObject so we register everything multiple times. We could do a if (!registerd) register() dance but I guess I'm thinking forward to a hydrogenous future but I guess we'd need to do more work then anyway.

Any chance we could move it all to the CPUClass?

No, because different instances of the same CPUClass might have different feature sets. In this case, one CPU might have MTE and another not, or one be AArch64 and another not.

The underlying problem here is that there's quite a lot here that potentially varies across different CPUs in the system, but the gdbstub layer has an assumption of heterogeneity. (cf also the stuff about system registers.)

thanks
-- PMM