Skip to content

Commit b390b7c

Browse files
retrocpugeekclaude
andcommitted
gdb: support async interrupt (ctrl-c) to break into a running guest
The stub drove continue by calling emu_start synchronously and only read the socket again once the target stopped on its own, so the bare \x03 break byte a client sends to pause a running target was never seen. A guest that free-runs (e.g. an idle/event loop) could not be interrupted at all -- gdb/Ghidra reported 'Cannot execute this command while the target is running'. Poll the client socket for the break byte from the per-instruction run hook (dbg_hook), which does run on the emulation thread during emu_start: - GdbSerialConn.poll_interrupt(): non-blocking select+recv, True on \x03. - QlGdbUtils.dbg_hook: throttled (every INTR_POLL_INTERVAL insns) check of an installed check_interrupt callback; on a break, stop emulation and record it. - handle_c: reply SIGINT when the stop was an interrupt rather than a breakpoint or normal exit. Verified against a free-running MIPS64 BE guest: \x03 -> S02 in <1ms; the existing gdb-stub regression test still passes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 3f02ebe commit b390b7c

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎qiling/debugger/gdb/gdb.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import os
1717
import socket
18+
import select
1819
import re
1920
import tempfile
2021
from functools import partial
@@ -124,6 +125,10 @@ def run(self):
124125
server = GdbSerialConn(self.ip, self.port, self.ql.log)
125126
killed = False
126127

128+
# let the run hook break into a free-running guest when the client sends
129+
# an async interrupt (ctrl-c / \x03); see QlGdbUtils.dbg_hook.
130+
self.gdb.check_interrupt = server.poll_interrupt
131+
127132
def __hexstr(value: int, nibbles: int = 0) -> str:
128133
"""Encode a value into a hex string.
129134
"""
@@ -254,7 +259,10 @@ def handle_c(subcmd: str) -> Reply:
254259
reply = f'S{SIGINT:02x}'
255260

256261
else:
257-
if getattr(self.ql.arch, 'effective_pc', self.ql.arch.regs.arch_pc) == self.gdb.last_bp:
262+
if self.gdb.interrupted:
263+
# emulation was stopped by an async interrupt from the client
264+
reply = f'S{SIGINT:02x}'
265+
elif getattr(self.ql.arch, 'effective_pc', self.ql.arch.regs.arch_pc) == self.gdb.last_bp:
258266
# emulation stopped because it hit a breakpoint
259267
reply = f'S{SIGTRAP:02x}'
260268
else:
@@ -839,6 +847,27 @@ def close(self):
839847
self.client.close()
840848
self.sock.close()
841849

850+
def poll_interrupt(self) -> bool:
851+
"""Non-blocking check for an async interrupt from the client.
852+
853+
While the target is running the only thing gdb sends is a bare ``\\x03``
854+
break byte (it waits for a stop reply before sending anything else), so
855+
any readable bytes here are that interrupt or a stray protocol ack.
856+
Returns True if a break was seen. Called from the run hook, so it must
857+
never block.
858+
"""
859+
860+
readable, _, _ = select.select([self.client], [], [], 0)
861+
if not readable:
862+
return False
863+
864+
try:
865+
incoming = self.client.recv(self.BUFSIZE)
866+
except (ConnectionError, OSError):
867+
return False
868+
869+
return b'\x03' in incoming
870+
842871
def readpackets(self) -> Iterator[bytes]:
843872
"""Iterate through incoming packets in an active connection until
844873
it is terminated.

‎qiling/debugger/gdb/utils.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@
1414

1515

1616
class QlGdbUtils:
17+
# how often (in guest instructions) the run hook polls the client socket for
18+
# an async interrupt. small enough to feel instant, large enough that the
19+
# extra non-blocking socket check does not dominate the per-instruction hook.
20+
INTR_POLL_INTERVAL = 200
21+
1722
def __init__(self, ql: Qiling, entry_point: int, exit_point: int):
1823
self.ql = ql
1924

2025
self.exit_point = exit_point
2126
self.swbp = set()
2227
self.last_bp = None
2328

29+
# async-interrupt support: `check_interrupt` is a callable installed by the
30+
# gdb stub that returns True when the client sent a break (ctrl-c / \x03)
31+
# while the target was running. `interrupted` records that the last resume
32+
# stopped for that reason (rather than a breakpoint or normal exit).
33+
self.check_interrupt = None
34+
self.interrupted = False
35+
self._poll_counter = 0
36+
2437
def __entry_point_hook(ql: Qiling):
2538
ql.hook_del(ep_hret)
2639
ql.hook_code(self.dbg_hook)
@@ -36,6 +49,24 @@ def dbg_hook(self, ql: Qiling, address: int, size: int):
3649
if getattr(ql.arch, 'is_thumb', False):
3750
address |= 1
3851

52+
# poll for an async interrupt from the client (gdb sends a bare \x03 while
53+
# the target is running). throttled so the socket check stays off the hot
54+
# path. this is the only way to break into a free-running guest, since the
55+
# stub's packet loop is blocked inside emu_start until the target stops.
56+
if self.check_interrupt is not None:
57+
self._poll_counter += 1
58+
59+
if self._poll_counter >= self.INTR_POLL_INTERVAL:
60+
self._poll_counter = 0
61+
62+
if self.check_interrupt():
63+
self.interrupted = True
64+
self.last_bp = None
65+
66+
ql.log.info(f'{PROMPT} interrupted by client, stopped at {address:#x}')
67+
ql.stop()
68+
return
69+
3970
# resuming emulation after hitting a breakpoint will re-enter this hook.
4071
# avoid an endless hooking loop by detecting and skipping this case
4172
if address == self.last_bp:
@@ -83,4 +114,8 @@ def resume_emu(self, address: Optional[int] = None, steps: int = 0):
83114
op = f'stepping {steps} instructions' if steps else 'resuming'
84115
self.ql.log.info(f'{PROMPT} {op} from {address:#x}')
85116

117+
# clear any pending interrupt state from a previous resume
118+
self.interrupted = False
119+
self._poll_counter = 0
120+
86121
self.ql.emu_start(address, self.exit_point, count=steps)

0 commit comments

Comments
 (0)