gdbstub: Fix client Ctrl-C handling
PATCH 1/1 gdbstub: Fix client Ctrl-C handling
The gdb remote protocol has a special interrupt character (0x03) that is transmitted outside the regular packet processing, and represents a Ctrl-C pressed in the client. Despite not being a regular packet, it does expect a regular stop response if the stub successfully stops the running program.
See: https://sourceware.org/gdb/onlinedocs/gdb/Interrupts.html
Inhibiting the stop reply packet can lead to gdb client hang. So permit a stop response when receiving a character from gdb that stops the vm. Additionally, add a warning if that was not a 0x03 character, because the gdb session is likely to end up getting confused if this happens.
---
Hey Fred, I'm not sure if this is the gdb hang you were seeing, but it is the one I could reproduce. Could be worth checking there are no more corner case hangs after this.
Thanks,
Nick
gdbstub/gdbstub.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
gdbstub/gdbstub.c +11 -2
@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" ); + } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif
Signature · 1 line
2.40.1
Quoted 16 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +4
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 10 lines
gdbstub/gdbstub.c +7
-0+* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" ); + } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif
Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );
Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state. And this does at least take us back to behaviour before the stop filtering patch.
Could go further and only stop the machine if it was a 3, or send a stop packet even if we were stopped, etc. but all that get further from a minimal fix.
Thanks,
Nick
Quoted 6 lines · Nicholas Piggin
The gdb remote protocol has a special interrupt character (
0x03) that is transmitted outside the regular packet processing, and represents a Ctrl-C pressed in the client. Despite not being a regular packet, it does expect a regular stop response if the stub successfully stops the running program.
Quoted 8 lines
See: https://sourceware.org/gdb/onlinedocs/gdb/Interrupts.html
Inhibiting the stop reply packet can lead to gdb client hang. So permit a stop response when receiving a character from gdb that stops the vm. Additionally, add a warning if that was not a
0x03character, because the gdb session is likely to end up getting confused if this happens.
Oh, I should note that this doesn't apply to any stable branches I'm sorry. Will be more careful with the tag...
Thanks,
Nick
Quoted 14 lines · Nicholas Piggin
Quoted 13 lines · Nicholas Piggin
The gdb remote protocol has a special interrupt character (
0x03) that is transmitted outside the regular packet processing, and represents a Ctrl-C pressed in the client. Despite not being a regular packet, it does expect a regular stop response if the stub successfully stops the running program.See: https://sourceware.org/gdb/onlinedocs/gdb/Interrupts.html
Inhibiting the stop reply packet can lead to gdb client hang. So permit a stop response when receiving a character from gdb that stops the vm. Additionally, add a warning if that was not a
0x03character, because the gdb session is likely to end up getting confused if this happens.
Quoted 4 lines
Quoted 1 line
Oh, I should note that this doesn't apply to any stable branches I'm sorry. Will be more careful with the tag...
That's entirely Okay, since the Fixes: tag helps to determine if it fits or not, and 758370052fb is v8.0.0-803-g758370052f. It's worse to miss something important :)
Thank you!
/mjt
Quoted 41 lines · Nicholas Piggin
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state. And this does at least take us back to behaviour before the stop filtering patch.
Quoted 3 lines
Could go further and only stop the machine if it was a 3, or send a stop packet even if we were stopped, etc. but all that get further from a minimal fix.
I was taking a look at -rc1 and it looks like this hasn't made it in. Is it something we want to propose including?
As a user of qemu I'd vote for it to go in.
Cheers,
Joel
Quoted 42 lines · Joel Stanley
Quoted 41 lines · Nicholas Piggin
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state. And this does at least take us back to behaviour before the stop filtering patch.
Quoted 8 lines
Quoted 3 lines
Could go further and only stop the machine if it was a 3, or send a stop packet even if we were stopped, etc. but all that get further from a minimal fix.
I was taking a look at -rc1 and it looks like this hasn't made it in. Is it something we want to propose including?
As a user of qemu I'd vote for it to go in.
I think it should, gdb is hardly usable without it.
Thanks,
Nick
Joel Stanley
Tested-byQuoted 48 lines · Nicholas Piggin
Quoted 46 lines · Joel Stanley
Quoted 44 lines · Nicholas Piggin
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state. And this does at least take us back to behaviour before the stop filtering patch.
Could go further and only stop the machine if it was a 3, or send a stop packet even if we were stopped, etc. but all that get further from a minimal fix.
Quoted 6 lines
Quoted 4 lines
I was taking a look at -rc1 and it looks like this hasn't made it in. Is it something we want to propose including?
As a user of qemu I'd vote for it to go in.
I think it should, gdb is hardly usable without it.
I think I hit this issue when debugging u-boot in the aspeed arm machines. Your patch fixed things:
Alex, Philippe, can we get this queued for 8.1?
Cheers,
Joel
Quoted 55 lines · Joel Stanley
Quoted 53 lines · Nicholas Piggin
Quoted 50 lines · Joel Stanley
Quoted 41 lines · Nicholas Piggin
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state. And this does at least take us back to behaviour before the stop filtering patch.
Quoted 3 lines
Could go further and only stop the machine if it was a 3, or send a stop packet even if we were stopped, etc. but all that get further from a minimal fix.
I was taking a look at -rc1 and it looks like this hasn't made it in. Is it something we want to propose including?
As a user of qemu I'd vote for it to go in.
Quoted 8 lines
Quoted 1 line
I think it should, gdb is hardly usable without it.
I think I hit this issue when debugging u-boot in the aspeed arm machines. Your patch fixed things:
Tested-by Joel Stanley <[email protected]>Alex, Philippe, can we get this queued for 8.1?
I'm doing a pullreq today anyway, so I can grab it.
thanks
-- PMM
Hi, Nick.
Quoted 33 lines
Nicholas Piggin <[email protected]> wrote:
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2067
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Quoted 7 lines
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state.
I just noticed now (as I was integrating the latest upstream patches with our downstream qemu-system-hexagon) that this causes the gdbstub-untimely-packet tcg test to fail.
My first thought was that, if 0x3 is the only valid case where we will read a char when the cpu is running, perhaps not issuing the stop-reply isn't that bad as GDB would ignore it anyways. E.g. from a set debug remote 1 output:
Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;
fork-events+;vfork-events+;exec-events+;vContSupported+;
QThreadEvents+;no-resumed+;
xmlRegisters=i386#6a...Packet instead of Ack, ignoring it
So, perhaps, we could do:
gdbstub/gdbstub.c +2 -1
@@ -2055,8 +2055,9 @@ void gdb_read_byte(uint8_t ch)*/ if (ch !=0x03 ) {warn_report ("gdbstub: client sent packet while target running\n" ); + }else { + gdbserver_state.allow_stop_reply = true; } - gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif
-- >8 --
Alternatively, since GDB ignores the packet anyways, should we just let this be and refactor/remove the test?
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Joel Stanley
Quoted 11 lines · Nicholas Piggin
Quoted 7 lines · Joel Stanley
On Wed, 12 Jul 2023 at 02:12, Nicholas Piggin <[email protected]> wrote:
I was taking a look at -rc1 and it looks like this hasn't made it in. Is it something we want to propose including?
As a user of qemu I'd vote for it to go in.
I think it should, gdb is hardly usable without it.
Quoted 8 lines
Quoted 6 lines
I think I hit this issue when debugging u-boot in the aspeed arm machines. Your patch fixed things:
Tested-by Joel Stanley <[email protected]>Alex, Philippe, can we get this queued for 8.1?
I'm doing a pullreq today anyway, so I can grab it.
Thank you Peter!
Quoted 77 lines · Matheus Tavares Bernardino
Hi, Nick.
Quoted 33 lines
Nicholas Piggin <[email protected]> wrote:
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2063
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Quoted 7 lines
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state.
I just noticed now (as I was integrating the latest upstream patches with our downstream qemu-system-hexagon) that this causes the gdbstub-untimely-packet tcg test to fail.
My first thought was that, if
0x3is the only valid case where we will read a char when the cpu is running, perhaps not issuing the stop-reply isn't that bad as GDB would ignore it anyways. E.g. from aset debug remote 1output:Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+; fork-events+;vfork-events+;exec-events+;vContSupported+; QThreadEvents+;no-resumed+; xmlRegisters=i386#6a... Packet instead of Ack, ignoring itSo, perhaps, we could do:
gdbstub/gdbstub.c +2
-2@@ -2055,8 +2055,9 @@ void gdb_read_byte(uint8_t ch)*/ if (ch !=0x03 ) {warn_report ("gdbstub: client sent packet while target running\n" ); + }else { + gdbserver_state.allow_stop_reply = true; } - gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif -- >8 --
Quoted 2 lines
Alternatively, since GDB ignores the packet anyways, should we just let this be and refactor/remove the test?
Ping, Alex and Nick.
I can confirm that Matheus' patch fixes the problem I am seeing. But I'm also open to removing the test.
r~
Quoted 65 lines · Matheus Tavares Bernardino
Hi, Nick.
Quoted 33 lines
Nicholas Piggin <[email protected]> wrote:
Quoted 21 lines · Matheus Tavares Bernardino
Quoted 21 lines
Nicholas Piggin <[email protected]> wrote:
gdbstub/gdbstub.c +9
-2@@ -2051,8 +2051,17 @@ void gdb_read_byte(uint8_t ch)return ; }if (runstate_is_running ()) { -/* when the CPU is running, we cannot do anything except stop -it when receiving a char */ +/* +* When the CPU is running, we cannot do anything except stop +* it when receiving a char. This is expected on a Ctrl-C in the +* gdb client. Because we are in all-stop mode, gdb sends a +* 0x03 byte which is not a usual packet, so we handle it specially +* here, but it does expect a stop reply. +*/ +if (ch !=0x03 ) { +warn_report ("gdbstub: client sent packet while target running\n" );Quoted 8 lines
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2063
Quoted 5 lines
gdbstub/gdbstub.c +2
-0+ } + gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); } else#endif Makes sense to me, but shouldn't we send the stop-reply packet only for Ctrl+C/0x03?
Quoted 7 lines
Good question.
I think if we get a character here that's not a 3, we're already in trouble, and we eat it so even worse. Since we only send a stop packet back when the vm stops, then if we don't send one now we might never send it. At least if we send one then the client might have some chance to get back to a sane state.
I just noticed now (as I was integrating the latest upstream patches with our downstream qemu-system-hexagon) that this causes the gdbstub-untimely-packet tcg test to fail.
My first thought was that, if
0x3is the only valid case where we will read a char when the cpu is running, perhaps not issuing the stop-reply isn't that bad as GDB would ignore it anyways. E.g. from aset debug remote 1output:Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+; fork-events+;vfork-events+;exec-events+;vContSupported+; QThreadEvents+;no-resumed+; xmlRegisters=i386#6a...Packet instead of Ack, ignoring it
So, perhaps, we could do:
gdbstub/gdbstub.c +0
-0
gdbstub/gdbstub.c void gdb_read_byte(uint8_t ch) · line 2058
Quoted 4 lines
gdbstub/gdbstub.c +0
-0@@ -2055,8 +2055,9 @@ void gdb_read_byte(uint8_t ch)*/ if (ch !=0x03 ) {warn_report ("gdbstub: client sent packet while target running\n");
This warning seems to be triggering either way, investigating now.
Quoted 11 lines
gdbstub/gdbstub.c +2
-1+ }else { + gdbserver_state.allow_stop_reply = true; } - gdbserver_state.allow_stop_reply = true;vm_stop (RUN_STATE_PAUSED ); }else #endif
-- >8 --Alternatively, since GDB ignores the packet anyways, should we just let this be and refactor/remove the test?
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro