Skip to contentSkip to conversation

target/alpha: Add TCG plugin register tracking support

PATCH Superseded Yodel Eldar 3 participants 7 messages Jun 27, 2025 – Jun 30, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/alpha: Add TCG plugin register tracking support

This patch adds TCG plugin register tracking support for the Alpha target and resolves gitlab issue #2569:

https://gitlab.com/qemu-project/qemu/-/issues/2569

As mentioned in the bug report by Alex Bennée, the register list is built using the target's corresponding GDB XML feature file, but the Alpha target does not have one. The second patch introduces the missing feature file and the necessary plumbing for it.

While testing the second patch, I noticed the following error:

qemu-alpha: GLib: g_strrstr: assertion 'haystack != NULL' failed

when running:

  ./qemu-alpha -d plugin \
  -plugin ./contrib/plugins/libexeclog.so,reg=*,rdisas=on \
  ./tests/tcg/alpha-linux-user/linux-test

and discovered an issue with execlog.c that the first patch resolves:
a missing null check after execlog searches a disassembled instruction for a space separator between the mnemonic and the operands. Execlog assumes that disassembled instructions will contain a space, but some disassemblers use tabs (like Alpha).

Besides adding the null check, the execlog patch also adds tab to the separator search by replacing the g_strstr_len call with a call to strpbrk, so that the plugin would operate as intended for Alpha.

Two pointers in the immediate area of the changed code were converted to const pointers in keeping with the QEMU Coding Style. Also, as a trivial optimization, I took the liberty of adding a break statement to the register search loop that immediately follows the separator search, so that it breaks out of the loop as soon as a relevant register is found in the instruction; please let me know if either of these minor changes should be moved to a separate patch file.

Lastly, this is my first submission to QEMU, and I want to thank every past, present, and future contributor to this project that has kept my system secure as I tinker with weird machines in the ultimate sandbox. QEMU is truly mind-blowing technology, and I have all of you to thank for it: Thanks!

Yodel Eldar (2):
contrib/plugins/execlog: Add tab to the separator search of insn_disas target/alpha: Add GDB XML feature file

 configs/targets/alpha-linux-user.mak |   1 +
 configs/targets/alpha-softmmu.mak    |   1 +
 contrib/plugins/execlog.c            |  15 +--
 gdb-xml/alpha-core.xml               | 136 +++++++++++++++++++++++++++
 target/alpha/cpu.c                   |   1 +
 5 files changed, 148 insertions(+), 6 deletions(-)
 create mode 100644 gdb-xml/alpha-core.xml
Signature · 1 line
2.50.0
#1 lore raw

PATCH 1/2 contrib/plugins/execlog: Add tab to the separator search of insn_disas

Currently, execlog searches for a space separator between the instruction mnemonic and operands, but some disassemblers, e.g. Alpha's, use a tab separator instead; this results in a null pointer being passed as the haystack in g_strstr during a subsequent register search, i.e. undefined behavior, because of a missing null check.

This patch adds tab to the separator search and a null check on the result.

Also, existing, affected pointers are changed to const.

Lastly, a break statement was added to immediately terminate the register search when a user-requested register is found in the current instruction as a trivial optimization, because searching for the remaining requested registers is unnecessary once one is found.

---
 contrib/plugins/execlog.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
contrib/plugins/execlog.c +9 -6
index d67d010761..08fc1f12d4 100644
@@ -232,12 +232,15 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
          */
         if (disas_assist && rmatches) {
             check_regs_next = false;
-            gchar *args = g_strstr_len(insn_disas, -1, " ");
-            for (int n = 0; n < all_reg_names->len; n++) {
-                gchar *reg = g_ptr_array_index(all_reg_names, n);
-                if (g_strrstr(args, reg)) {
-                    check_regs_next = true;
-                    skip = false;
+            const gchar *args = strpbrk(insn_disas, " \t");
+            if (args) {
+                for (int n = 0; n < all_reg_names->len; n++) {
+                    const gchar *reg = g_ptr_array_index(all_reg_names, n);
+                    if (g_strrstr(args, reg)) {
+                        check_regs_next = true;
+                        skip = false;
+                        break;
+                    }
                 }
             }
         }
Signature · 1 line
2.50.0
#2 lore raw

Re: [PATCH 1/2] contrib/plugins/execlog: Add tab to the separator search of insn_disas

Quoted 26 lines · Yodel Eldar

Currently, execlog searches for a space separator between the instruction mnemonic and operands, but some disassemblers, e.g. Alpha's, use a tab separator instead; this results in a null pointer being passed as the haystack in g_strstr during a subsequent register search, i.e. undefined behavior, because of a missing null check.

This patch adds tab to the separator search and a null check on the result.

Also, existing, affected pointers are changed to const.

Lastly, a break statement was added to immediately terminate the register search when a user-requested register is found in the current instruction as a trivial optimization, because searching for the remaining requested registers is unnecessary once one is found.

---
 contrib/plugins/execlog.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
contrib/plugins/execlog.c +0 -0
index d67d010761..08fc1f12d4 100644

contrib/plugins/execlog.c static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) · line 235

Quoted 10 lines
contrib/plugins/execlog.c +1 -6
@@ -232,12 +232,15 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
          */
         if (disas_assist && rmatches) {
             check_regs_next = false;
-            gchar *args = g_strstr_len(insn_disas, -1, " ");
-            for (int n = 0; n < all_reg_names->len; n++) {
-                gchar *reg = g_ptr_array_index(all_reg_names, n);
-                if (g_strrstr(args, reg)) {
-                    check_regs_next = true;
-                    skip = false;
+            const gchar *args = strpbrk(insn_disas, " \t");

We have a general preference for glib here, could we use g_strsplit_set?

Something like:

modified contrib/plugins/execlog.c
@@ -232,12 +232,14 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)

          */
         if (disas_assist && rmatches) {
             check_regs_next = false;

- gchar *args = g_strstr_len(insn_disas, -1, " ");
- for (int n = 0; n < all_reg_names->len; n++) {
- gchar *reg = g_ptr_array_index(all_reg_names, n);
- if (g_strrstr(args, reg)) {
- check_regs_next = true;
- skip = false;
+ g_auto(GStrv) args = g_strsplit_set(insn_disas, " \t", 2);
+ if (args && args[1]) {
+ for (int n = 0; n < all_reg_names->len; n++) {
+ gchar *reg = g_ptr_array_index(all_reg_names, n);
+ if (g_strrstr(args[1], reg)) {
+ check_regs_next = true;
+ skip = false;
+ }

Quoted 11 lines
contrib/plugins/execlog.c +8 -0
+            if (args) {
+                for (int n = 0; n < all_reg_names->len; n++) {
+                    const gchar *reg = g_ptr_array_index(all_reg_names, n);
+                    if (g_strrstr(args, reg)) {
+                        check_regs_next = true;
+                        skip = false;
+                        break;
+                    }
                 }
             }
         }
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw

Re: [PATCH 1/2] contrib/plugins/execlog: Add tab to the separator search of insn_disas

Quoted 52 lines · Alex Bennée
Quoted 26 lines · Yodel Eldar

Currently, execlog searches for a space separator between the instruction mnemonic and operands, but some disassemblers, e.g. Alpha's, use a tab separator instead; this results in a null pointer being passed as the haystack in g_strstr during a subsequent register search, i.e. undefined behavior, because of a missing null check.

This patch adds tab to the separator search and a null check on the result.

Also, existing, affected pointers are changed to const.

Lastly, a break statement was added to immediately terminate the register search when a user-requested register is found in the current instruction as a trivial optimization, because searching for the remaining requested registers is unnecessary once one is found.

---
  contrib/plugins/execlog.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
contrib/plugins/execlog.c +0 -0
index d67d010761..08fc1f12d4 100644

contrib/plugins/execlog.c static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) · line 235

Quoted 10 lines
contrib/plugins/execlog.c +1 -6
@@ -232,12 +232,15 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
           */
          if (disas_assist && rmatches) {
              check_regs_next = false;
-            gchar *args = g_strstr_len(insn_disas, -1, " ");
-            for (int n = 0; n < all_reg_names->len; n++) {
-                gchar *reg = g_ptr_array_index(all_reg_names, n);
-                if (g_strrstr(args, reg)) {
-                    check_regs_next = true;
-                    skip = false;
+            const gchar *args = strpbrk(insn_disas, " \t");

We have a general preference for glib here, could we use g_strsplit_set?

Something like:

modified contrib/plugins/execlog.c

contrib/plugins/execlog.c +0 -5
@@ -232,12 +232,14 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
           */
          if (disas_assist && rmatches) {
              check_regs_next = false;
-            gchar *args = g_strstr_len(insn_disas, -1, " ");
-            for (int n = 0; n < all_reg_names->len; n++) {
-                gchar *reg = g_ptr_array_index(all_reg_names, n);
-                if (g_strrstr(args, reg)) {
-                    check_regs_next = true;

contrib/plugins/execlog.c static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) · line 243

Quoted 10 lines
contrib/plugins/execlog.c +8 -1
-                    skip = false;
+            g_auto(GStrv) args = g_strsplit_set(insn_disas, " \t", 2);
+            if (args && args[1]) {
+                for (int n = 0; n < all_reg_names->len; n++) {
+                    gchar *reg = g_ptr_array_index(all_reg_names, n);
+                    if (g_strrstr(args[1], reg)) {
+                        check_regs_next = true;
+                        skip = false;
+                    }
 

Certainly, and thanks for the suggestion! May I credit you with a "Suggested-by" or "Co-authored-by" tag in v2 of the patch?

Quoted 11 lines
(quoted diff) +0 -0
 > +            if (args) {
 > +                for (int n = 0; n < all_reg_names->len; n++) {
Quoted 9 lines
contrib/plugins/execlog.c +6 -0
+                    const gchar *reg = g_ptr_array_index(all_reg_names, n);
+                    if (g_strrstr(args, reg)) {
+                        check_regs_next = true;
+                        skip = false;
+                        break;
+                    }
                  }
              }
          }
#4 lore raw

Re: [PATCH 1/2] contrib/plugins/execlog: Add tab to the separator search of insn_disas

Quoted 58 lines · Yodel Eldar
Quoted 57 lines · Alex Bennée
Quoted 26 lines · Yodel Eldar

Currently, execlog searches for a space separator between the instruction mnemonic and operands, but some disassemblers, e.g. Alpha's, use a tab separator instead; this results in a null pointer being passed as the haystack in g_strstr during a subsequent register search, i.e. undefined behavior, because of a missing null check.

This patch adds tab to the separator search and a null check on the result.

Also, existing, affected pointers are changed to const.

Lastly, a break statement was added to immediately terminate the register search when a user-requested register is found in the current instruction as a trivial optimization, because searching for the remaining requested registers is unnecessary once one is found.

---
  contrib/plugins/execlog.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
contrib/plugins/execlog.c +0 -0
index d67d010761..08fc1f12d4 100644

contrib/plugins/execlog.c static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) · line 235

Quoted 10 lines
contrib/plugins/execlog.c +1 -6
@@ -232,12 +232,15 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
           */
          if (disas_assist && rmatches) {
              check_regs_next = false;
-            gchar *args = g_strstr_len(insn_disas, -1, " ");
-            for (int n = 0; n < all_reg_names->len; n++) {
-                gchar *reg = g_ptr_array_index(all_reg_names, n);
-                if (g_strrstr(args, reg)) {
-                    check_regs_next = true;
-                    skip = false;
+            const gchar *args = strpbrk(insn_disas, " \t");

We have a general preference for glib here, could we use g_strsplit_set?

Something like:

modified contrib/plugins/execlog.c

contrib/plugins/execlog.c +4 -6
@@ -232,12 +232,14 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
           */
          if (disas_assist && rmatches) {
              check_regs_next = false;
-            gchar *args = g_strstr_len(insn_disas, -1, " ");
-            for (int n = 0; n < all_reg_names->len; n++) {
-                gchar *reg = g_ptr_array_index(all_reg_names, n);
-                if (g_strrstr(args, reg)) {
-                    check_regs_next = true;
-                    skip = false;
+            g_auto(GStrv) args = g_strsplit_set(insn_disas, " \t", 2);
+            if (args && args[1]) {
+                for (int n = 0; n < all_reg_names->len; n++) {
+                    gchar *reg = g_ptr_array_index(all_reg_names, n);
Quoted 8 lines

contrib/plugins/execlog.c static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)

Quoted 5 lines
contrib/plugins/execlog.c +4 -0
+                    if (g_strrstr(args[1], reg)) {
+                        check_regs_next = true;
+                        skip = false;
+                    }
 

Certainly, and thanks for the suggestion! May I credit you with a "Suggested-by" or "Co-authored-by" tag in v2 of the patch?

Suggested-by is fine ;-)

Quoted 13 lines
Quoted 13 lines
(quoted diff) +0 -0
 > +            if (args) {
 > +                for (int n = 0; n < all_reg_names->len; n++) {
Quoted 9 lines
contrib/plugins/execlog.c +6 -0
+                    const gchar *reg = g_ptr_array_index(all_reg_names, n);
+                    if (g_strrstr(args, reg)) {
+                        check_regs_next = true;
+                        skip = false;
+                        break;
+                    }
                  }
              }
          }
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw

PATCH 2/2 target/alpha: Add GDB XML feature file

In reply to Yodel Eldar

This patch adds the GDB XML feature file that describes Alpha's core registers.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2569

---
 configs/targets/alpha-linux-user.mak |   1 +
 configs/targets/alpha-softmmu.mak    |   1 +
 gdb-xml/alpha-core.xml               | 136 +++++++++++++++++++++++++++
 target/alpha/cpu.c                   |   1 +
 4 files changed, 139 insertions(+)
 create mode 100644 gdb-xml/alpha-core.xml
configs/targets/alpha-linux-user.mak +1 -0
index ef8e365b09..aa25766236 100644
@@ -2,3 +2,4 @@ TARGET_ARCH=alpha
 TARGET_SYSTBL_ABI=common
 TARGET_SYSTBL=syscall.tbl
 TARGET_LONG_BITS=64
+TARGET_XML_FILES= gdb-xml/alpha-core.xml
configs/targets/alpha-softmmu.mak +1 -0
index 5275076e50..e31f059a52 100644
@@ -1,2 +1,3 @@
 TARGET_ARCH=alpha
 TARGET_LONG_BITS=64
+TARGET_XML_FILES= gdb-xml/alpha-core.xml
gdb-xml/alpha-core.xml new file +136 -0

136 lines not on this page · raw message

target/alpha/cpu.c +1 -0
index 2082db45ea..bf1787a69d 100644
@@ -286,6 +286,7 @@ static void alpha_cpu_class_init(ObjectClass *oc, const void *data)
     cc->get_pc = alpha_cpu_get_pc;
     cc->gdb_read_register = alpha_cpu_gdb_read_register;
     cc->gdb_write_register = alpha_cpu_gdb_write_register;
+    cc->gdb_core_xml_file = "alpha-core.xml";
 #ifndef CONFIG_USER_ONLY
     dc->vmsd = &vmstate_alpha_cpu;
     cc->sysemu_ops = &alpha_sysemu_ops;
Signature · 1 line
2.50.0
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] target/alpha: Add GDB XML feature file

Quoted 5 lines · Yodel Eldar

This patch adds the GDB XML feature file that describes Alpha's core registers.

Resolves:https://gitlab.com/qemu-project/qemu/-/issues/2569

Quoted 8 lines
---
  configs/targets/alpha-linux-user.mak |   1 +
  configs/targets/alpha-softmmu.mak    |   1 +
  gdb-xml/alpha-core.xml               | 136 +++++++++++++++++++++++++++
  target/alpha/cpu.c                   |   1 +
  4 files changed, 139 insertions(+)

create mode 100644 gdb-xml/alpha-core.xml

r~