Skip to contentSkip to conversation

gdbstub: fixes cases where wrong threads were reported to GDB on SIGINT

PATCH In review Matheus Branco Borella 2 participants 5 messages Jun 23, 2023 – Aug 10, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gdbstub: fixes cases where wrong threads were reported to GDB on SIGINT

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

This fix is implemented by having the vCont handler set the value of gdbserver_state.c_cpu if any threads are to be resumed. The specific CPU is picked arbitrarily from the ones to be resumed, but it should be okay, as all GDB cares about is that it is a resumed thread.

Keep in mind that because this patch overwrites c_cpu, it breaks cases where $vCont is used together with $Hc, so there might be more work to be done here. It might also be the case that it breaking this, specifically, isn't of consequence, seeing as single stepping with $vCont already overwrites c_cpu anyway, so you could say the implementation already behaves oddly as far as mixing $vCont and $Hc is concerned.

---
 gdbstub/gdbstub.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +30 -1
index be18568d0a..4f7ac5ddfe 100644
@@ -595,6 +595,15 @@ static int gdb_handle_vcont(const char *p)
      *  or incorrect parameters passed.
      */
     res = 0;
+    
+    /* 
+     * target_count and last_target keep track of how many CPUs we are going to
+     * step or resume, and a pointer to the state structure of one of them, 
+     * respectivelly
+     */
+    int target_count = 0;
+    CPUState *last_target = NULL;
+
     while (*p) {
         if (*p++ != ';') {
             res = -ENOTSUP;
@@ -639,8 +648,10 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
-                }
 
+                    target_count++;
+                    last_target = cpu;
+                }
                 cpu = gdb_next_attached_cpu(cpu);
             }
             break;
@@ -657,6 +668,9 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
+                    
+                    target_count++;
+                    last_target = cpu;
                 }
 
                 cpu = gdb_next_cpu_in_process(cpu);
@@ -675,10 +689,25 @@ static int gdb_handle_vcont(const char *p)
             /* only use if no previous match occourred */
             if (newstates[cpu->cpu_index] == 1) {
                 newstates[cpu->cpu_index] = cur_action;
+
+                target_count++;
+                last_target = cpu;
             }
             break;
         }
     }
+
+    /* 
+     * if we're about to resume a specific set of CPUs/threads, make it so that 
+     * in case execution gets interrupted, we can send GDB a stop reply with a
+     * correct value. it doesn't really matter which CPU we tell GDB the signal 
+     * happened in (VM pauses stop all of them anyway), so long as it is one of
+     * the ones we resumed/single stepped here.
+     */
+    if (target_count > 0) {
+        gdbserver_state.c_cpu = last_target;
+    }
+
     gdbserver_state.signal = signal;
     gdb_continue_partial(newstates);
 
Signature · 1 line
2.40.1
#1 lore raw
Quoted 10 lines · Matheus Branco Borella

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

This fix is implemented by having the vCont handler set the value of gdbserver_state.c_cpu if any threads are to be resumed. The specific CPU is picked arbitrarily from the ones to be resumed, but it should be okay, as all GDB cares about is that it is a resumed thread.

Keep in mind that because this patch overwrites c_cpu, it breaks cases where $vCont is used together with $Hc, so there might be more work to be done here.

That doesn't sound good. Is that a possible case or an invalid one because we shouldn't see gdbs using both?

Quoted 4 lines

It might also be the case that it breaking this, specifically, isn't of consequence, seeing as single stepping with $vCont already overwrites c_cpu anyway, so you could say the implementation already behaves oddly as far as mixing $vCont and $Hc is concerned.

It would be nice to have some unit tests for this behaviour to defend it. See the various tests in tests/tcg that call $(GDB_SCRIPT) for examples.

BTW you are missing a Signed-off-by: tag which we will need to take a patch submission. See:

https://qemu.readthedocs.io/en/latest/devel/submitting-a-patch.html

Quoted 61 lines
---
 gdbstub/gdbstub.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +22 -1
index be18568d0a..4f7ac5ddfe 100644
@@ -595,6 +595,15 @@ static int gdb_handle_vcont(const char *p)
      *  or incorrect parameters passed.
      */
     res = 0;
+    
+    /* 
+     * target_count and last_target keep track of how many CPUs we are going to
+     * step or resume, and a pointer to the state structure of one of them, 
+     * respectivelly
+     */
+    int target_count = 0;
+    CPUState *last_target = NULL;
+
     while (*p) {
         if (*p++ != ';') {
             res = -ENOTSUP;
@@ -639,8 +648,10 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
-                }
 
+                    target_count++;
+                    last_target = cpu;
+                }
                 cpu = gdb_next_attached_cpu(cpu);
             }
             break;
@@ -657,6 +668,9 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
+                    
+                    target_count++;
+                    last_target = cpu;
                 }
 
                 cpu = gdb_next_cpu_in_process(cpu);
@@ -675,10 +689,25 @@ static int gdb_handle_vcont(const char *p)
             /* only use if no previous match occourred */
             if (newstates[cpu->cpu_index] == 1) {
                 newstates[cpu->cpu_index] = cur_action;
+
+                target_count++;
+                last_target = cpu;
             }
             break;
         }
     }
+
+    /* 
+     * if we're about to resume a specific set of CPUs/threads, make it so that 
+     * in case execution gets interrupted, we can send GDB a stop reply with a

gdbstub/gdbstub.c static int gdb_handle_vcont(const char *p) · line 712

Quoted 10 lines
gdbstub/gdbstub.c +8 -0
+     * correct value. it doesn't really matter which CPU we tell GDB the signal 
+     * happened in (VM pauses stop all of them anyway), so long as it is one of
+     * the ones we resumed/single stepped here.
+     */
+    if (target_count > 0) {
+        gdbserver_state.c_cpu = last_target;
+    }
+
     gdbserver_state.signal = signal;
     gdb_continue_partial(newstates);

Looks reasonable at first glance but I would like some tests.

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

PATCH 1/1 gdbstub: fixes cases where wrong threads were reported to GDB on SIGINT

Quoted 2 lines · Alex Bennée

Can gdb switch which packet sequence it uses to halt and restart threads?

Yes, but the way it does it does not trigger the behavior I was concerned about. GDB falls back to the old sequence when either (1) the target does not support the vCont command it's trying to send or (2) you step backwards. In both cases, though, whenever it does fall back, it will first send an Hc packet before continuing or stepping, which means we won't ever see a sequence such as ["Hc", "vCont;c:*", "c"]. This means, in short, that, while the shortcoming does exist in the code, GDB never actually triggers it.

Quoted 7 lines

The test I would like see is pretty much your test case

- load a multi-threaded program
- wait until threads running
- pause
- resume thread
- check resumed thread was the right one

What I have here should be pretty much that.

Is there something else you think I'm missing?

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

This fix is implemented by having the vCont handler set the value of gdbserver_state.c_cpu if any threads are to be resumed. The specific CPU picked is arbitrarily from the ones to be resumed, but it should be okay, as all GDB cares about is that it is a resumed thread.

---
 gdbstub/gdbstub.c                             | 29 ++++++
 tests/tcg/multiarch/gdbstub/interrupt.py      | 99 +++++++++++++++++++
 .../multiarch/system/Makefile.softmmu-target  | 12 ++-
 tests/tcg/multiarch/system/interrupt.c        | 25 +++++
 4 files changed, 163 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/interrupt.py
 create mode 100644 tests/tcg/multiarch/system/interrupt.c
gdbstub/gdbstub.c +29 -0
index 6911b73c07..6a5a400348 100644
@@ -597,6 +597,15 @@ static int gdb_handle_vcont(const char *p)
      *  or incorrect parameters passed.
      */
     res = 0;
+
+    /* 
+     * target_count and last_target keep track of how many CPUs we are going to
+     * step or resume, and a pointer to the state structure of one of them, 
+     * respectivelly
+     */
+    int target_count = 0;
+    CPUState *last_target = NULL;
+
     while (*p) {
         if (*p++ != ';') {
             return -ENOTSUP;
@@ -637,6 +646,9 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
+
+                    target_count++;
+                    last_target = cpu;
                 }
 
                 cpu = gdb_next_attached_cpu(cpu);
@@ -654,6 +666,9 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
+
+                    target_count++;
+                    last_target = cpu;
                 }
 
                 cpu = gdb_next_cpu_in_process(cpu);
@@ -671,11 +686,25 @@ static int gdb_handle_vcont(const char *p)
             /* only use if no previous match occourred */
             if (newstates[cpu->cpu_index] == 1) {
                 newstates[cpu->cpu_index] = cur_action;
+
+                target_count++;
+                last_target = cpu;
             }
             break;
         }
     }
 
+    /* 
+     * if we're about to resume a specific set of CPUs/threads, make it so that 
+     * in case execution gets interrupted, we can send GDB a stop reply with a
+     * correct value. it doesn't really matter which CPU we tell GDB the signal 
+     * happened in (VM pauses stop all of them anyway), so long as it is one of
+     * the ones we resumed/single stepped here.
+     */
+    if (target_count > 0) {
+        gdbserver_state.c_cpu = last_target;
+    }
+
     gdbserver_state.signal = signal;
     gdb_continue_partial(newstates);
     return res;
tests/tcg/multiarch/gdbstub/interrupt.py new file +95 -0

95 lines not on this page · raw message

tests/tcg/multiarch/system/Makefile.softmmu-target +10 -2
index fe40195d39..ad3b727ffc 100644
@@ -27,7 +27,15 @@ run-gdbstub-memory: memory
 		"-monitor none -display none -chardev file$(COMMA)path=$<.out$(COMMA)id=output $(QEMU_OPTS)" \
 		--bin $< --test $(MULTIARCH_SRC)/gdbstub/memory.py, \
 	softmmu gdbstub support)
-
+run-gdbstub-interrupt: interrupt
+	$(call run-test, $@, $(GDB_SCRIPT) \
+		--gdb $(HAVE_GDB_BIN) \
+		--qemu $(QEMU) \
+		--output $<.gdb.out \
+		--qargs \
+		"-smp 2 -monitor none -display none -chardev file$(COMMA)path=$<.out$(COMMA)id=output $(QEMU_OPTS)" \
+		--bin $< --test $(MULTIARCH_SRC)/gdbstub/interrupt.py, \
+	softmmu gdbstub support)
 run-gdbstub-untimely-packet: hello
 	$(call run-test, $@, $(GDB_SCRIPT) \
 		--gdb $(HAVE_GDB_BIN) \
@@ -50,4 +58,4 @@ run-gdbstub-%:
 	$(call skip-test, "gdbstub test $*", "need working gdb")
 endif
 
-MULTIARCH_RUNS += run-gdbstub-memory run-gdbstub-untimely-packet
+MULTIARCH_RUNS += run-gdbstub-memory run-gdbstub-interrupt run-gdbstub-untimely-packet
tests/tcg/multiarch/system/interrupt.c new file +25 -0
new file mode 100644
index 0000000000..19fd831a5c
@@ -0,0 +1,25 @@
+/*
+ * External interruption test. This test is structured in such a way that it
+ * passes the cases that require it to exit, but we can make it enter an 
+ * infinite loop from GDB.
+ *
+ * We don't have the benefit of libc, just builtin C primitives and
+ * whatever is in minilib.
+ */
+
+#include <minilib.h>
+
+void loop(void)
+{
+    while(1)
+        /* Loop forever. Just make sure the condition is always a constant 
+         * expression, so that this loop is not UB, as per the C standard. */
+        ;
+}
+
+int main(void)
+{
+    return 0;
+}
+
+
Signature · 1 line
2.40.1
#3 lore raw

PATCH v2 1/1 gdbstub: fixes cases where wrong threads were reported to GDB on SIGINT

In reply to Alex Bennée
Quoted 2 lines · Alex Bennée

Can gdb switch which packet sequence it uses to halt and restart threads?

Yes, but the way it does it does not trigger the behavior I was concerned about. GDB falls back to the old sequence when either (1) the target does not support the vCont command it's trying to send or (2) you step backwards. In both cases, though, whenever it does fall back, it will first send an Hc packet before continuing or stepping, which means we won't ever see a sequence such as ["Hc", "vCont;c:*", "c"]. This means, in short, that, while the shortcoming does exist in the code, GDB never actually triggers it.

Quoted 7 lines

The test I would like see is pretty much your test case

- load a multi-threaded program
- wait until threads running
- pause
- resume thread
- check resumed thread was the right one

What I have here should be pretty much that.

Is there something else you think I'm missing?

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

This fix is implemented by having the vCont handler set the value of gdbserver_state.c_cpu if any threads are to be resumed. The specific CPU picked is arbitrarily from the ones to be resumed, but it should be okay, as all GDB cares about is that it is a resumed thread.

---
 gdbstub/gdbstub.c                             | 29 ++++++
 tests/tcg/multiarch/gdbstub/interrupt.py      | 99 +++++++++++++++++++
 .../multiarch/system/Makefile.softmmu-target  | 12 ++-
 tests/tcg/multiarch/system/interrupt.c        | 25 +++++
 4 files changed, 163 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/interrupt.py
 create mode 100644 tests/tcg/multiarch/system/interrupt.c
gdbstub/gdbstub.c +29 -0
index 6911b73c07..5763399f8b 100644
@@ -597,6 +597,15 @@ static int gdb_handle_vcont(const char *p)
      *  or incorrect parameters passed.
      */
     res = 0;
+
+    /*
+     * target_count and last_target keep track of how many CPUs we are going to
+     * step or resume, and a pointer to the state structure of one of them,
+     * respectivelly
+     */
+    int target_count = 0;
+    CPUState *last_target = NULL;
+
     while (*p) {
         if (*p++ != ';') {
             return -ENOTSUP;
@@ -637,6 +646,9 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
+
+                    target_count++;
+                    last_target = cpu;
                 }
 
                 cpu = gdb_next_attached_cpu(cpu);
@@ -654,6 +666,9 @@ static int gdb_handle_vcont(const char *p)
             while (cpu) {
                 if (newstates[cpu->cpu_index] == 1) {
                     newstates[cpu->cpu_index] = cur_action;
+
+                    target_count++;
+                    last_target = cpu;
                 }
 
                 cpu = gdb_next_cpu_in_process(cpu);
@@ -671,11 +686,25 @@ static int gdb_handle_vcont(const char *p)
             /* only use if no previous match occourred */
             if (newstates[cpu->cpu_index] == 1) {
                 newstates[cpu->cpu_index] = cur_action;
+
+                target_count++;
+                last_target = cpu;
             }
             break;
         }
     }
 
+    /*
+     * if we're about to resume a specific set of CPUs/threads, make it so that
+     * in case execution gets interrupted, we can send GDB a stop reply with a
+     * correct value. it doesn't really matter which CPU we tell GDB the signal
+     * happened in (VM pauses stop all of them anyway), so long as it is one of
+     * the ones we resumed/single stepped here.
+     */
+    if (target_count > 0) {
+        gdbserver_state.c_cpu = last_target;
+    }
+
     gdbserver_state.signal = signal;
     gdb_continue_partial(newstates);
     return res;
tests/tcg/multiarch/gdbstub/interrupt.py new file +99 -0

99 lines not on this page · raw message

tests/tcg/multiarch/system/Makefile.softmmu-target +10 -2
index fe40195d39..ad3b727ffc 100644
@@ -27,7 +27,15 @@ run-gdbstub-memory: memory
 		"-monitor none -display none -chardev file$(COMMA)path=$<.out$(COMMA)id=output $(QEMU_OPTS)" \
 		--bin $< --test $(MULTIARCH_SRC)/gdbstub/memory.py, \
 	softmmu gdbstub support)
-
+run-gdbstub-interrupt: interrupt
+	$(call run-test, $@, $(GDB_SCRIPT) \
+		--gdb $(HAVE_GDB_BIN) \
+		--qemu $(QEMU) \
+		--output $<.gdb.out \
+		--qargs \
+		"-smp 2 -monitor none -display none -chardev file$(COMMA)path=$<.out$(COMMA)id=output $(QEMU_OPTS)" \
+		--bin $< --test $(MULTIARCH_SRC)/gdbstub/interrupt.py, \
+	softmmu gdbstub support)
 run-gdbstub-untimely-packet: hello
 	$(call run-test, $@, $(GDB_SCRIPT) \
 		--gdb $(HAVE_GDB_BIN) \
@@ -50,4 +58,4 @@ run-gdbstub-%:
 	$(call skip-test, "gdbstub test $*", "need working gdb")
 endif
 
-MULTIARCH_RUNS += run-gdbstub-memory run-gdbstub-untimely-packet
+MULTIARCH_RUNS += run-gdbstub-memory run-gdbstub-interrupt run-gdbstub-untimely-packet
tests/tcg/multiarch/system/interrupt.c new file +25 -0
new file mode 100644
index 0000000000..64899caf09
@@ -0,0 +1,25 @@
+/*
+ * External interruption test. This test is structured in such a way that it
+ * passes the cases that require it to exit, but we can make it enter an
+ * infinite loop from GDB.
+ *
+ * We don't have the benefit of libc, just builtin C primitives and
+ * whatever is in minilib.
+ */
+
+#include <minilib.h>
+
+void loop(void)
+{
+    while(1)
+        /* Loop forever. Just make sure the condition is always a constant
+         * expression, so that this loop is not UB, as per the C standard. */
+        ;
+}
+
+int main(void)
+{
+    return 0;
+}
+
+
Signature · 1 line
2.41.0
Applied
#4 lore raw
Quoted 27 lines · Matheus Branco Borella
Quoted 2 lines · Alex Bennée

Can gdb switch which packet sequence it uses to halt and restart threads?

Yes, but the way it does it does not trigger the behavior I was concerned about. GDB falls back to the old sequence when either (1) the target does not support the vCont command it's trying to send or (2) you step backwards. In both cases, though, whenever it does fall back, it will first send an Hc packet before continuing or stepping, which means we won't ever see a sequence such as ["Hc", "vCont;c:*", "c"]. This means, in short, that, while the shortcoming does exist in the code, GDB never actually triggers it.

Quoted 7 lines

The test I would like see is pretty much your test case

- load a multi-threaded program
- wait until threads running
- pause
- resume thread
- check resumed thread was the right one

What I have here should be pretty much that.

Is there something else you think I'm missing?

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

Quoted 6 lines

This fix is implemented by having the vCont handler set the value of gdbserver_state.c_cpu if any threads are to be resumed. The specific CPU picked is arbitrarily from the ones to be resumed, but it should be okay, as all GDB cares about is that it is a resumed thread.

Arg the commit message is in the --- discard section.

Queued to for-8.1/misc-fixes, thanks.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro