|
| 1 | +from asyncio import create_subprocess_exec |
| 2 | +from asyncio import create_task |
| 3 | +from asyncio import Queue |
| 4 | +from asyncio import to_thread |
| 5 | +from asyncio import Event |
| 6 | +from asyncio import iscoroutinefunction |
| 7 | +from asyncio.subprocess import PIPE |
| 8 | +from contextlib import suppress |
| 9 | +from pathlib import Path |
| 10 | +import os |
| 11 | + |
| 12 | +from . import mion |
| 13 | + |
| 14 | + |
| 15 | +def panic(*args, **kwargs): |
| 16 | + raise AssertionError(*args, **kwargs) |
| 17 | + |
| 18 | + |
| 19 | +class BaseDebugger: |
| 20 | + def __init__(self) -> None: |
| 21 | + do_nothing = lambda *args, **kwargs: None |
| 22 | + self.oob_handler = do_nothing |
| 23 | + self.inferior_handler = do_nothing |
| 24 | + self._inferior_dispatch_done = Event() |
| 25 | + self._inferior_dispatch_done.set() |
| 26 | + self._did_init = False |
| 27 | + |
| 28 | + async def init(self, executable_path: str | Path) -> None: |
| 29 | + self.fd_master, self.fd_slave = os.openpty() |
| 30 | + self.process = await create_subprocess_exec( |
| 31 | + "gdb", |
| 32 | + "--interpreter=mi4", |
| 33 | + "--quiet", |
| 34 | + "-nx", |
| 35 | + "-nh", |
| 36 | + "--tty", |
| 37 | + os.ttyname(self.fd_slave), |
| 38 | + "--args", |
| 39 | + str(executable_path), |
| 40 | + stdin=PIPE, |
| 41 | + stdout=PIPE, |
| 42 | + ) |
| 43 | + |
| 44 | + self.result_queue = Queue[tuple[str, dict]]() |
| 45 | + create_task(self._stdout_dispatch()) |
| 46 | + create_task(self._inferior_dispatch()) |
| 47 | + self._did_init = True |
| 48 | + return self |
| 49 | + |
| 50 | + async def deinit(self) -> None: |
| 51 | + if not self._did_init: |
| 52 | + return |
| 53 | + |
| 54 | + self.process.stdin.write_eof() # or write `-gdb-exit` |
| 55 | + await self.process.stdin.drain() |
| 56 | + await self.process.wait() |
| 57 | + |
| 58 | + self.result_queue.shutdown() |
| 59 | + await self.result_queue.join() |
| 60 | + |
| 61 | + os.close(self.fd_master) |
| 62 | + os.close(self.fd_slave) |
| 63 | + await self._inferior_dispatch_done.wait() |
| 64 | + |
| 65 | + async def run_command(self, command: str): |
| 66 | + self.process.stdin.write(f"{command}\n".encode()) |
| 67 | + await self.process.stdin.drain() |
| 68 | + |
| 69 | + subkind, result = await self.result_queue.get() |
| 70 | + self.result_queue.task_done() |
| 71 | + assert ( |
| 72 | + subkind in mion.RESULT_CLASS |
| 73 | + ), f"Command '{command}' returned unexpected status {subkind}" |
| 74 | + return result |
| 75 | + |
| 76 | + def on_oob[F](self, func: F) -> F: |
| 77 | + """oob = out of band""" |
| 78 | + self.oob_handler = func |
| 79 | + return func |
| 80 | + |
| 81 | + def on_inferior[F](self, func: F) -> F: |
| 82 | + """inferior = inferior output""" |
| 83 | + self.inferior_handler = func |
| 84 | + return func |
| 85 | + |
| 86 | + async def _stdout_dispatch(self) -> None: |
| 87 | + while line := await self.process.stdout.readline(): |
| 88 | + line = line.strip().decode() |
| 89 | + if line == "(gdb)": |
| 90 | + continue |
| 91 | + |
| 92 | + kind, message = line[:1], line[1:] |
| 93 | + match kind: |
| 94 | + case mion.RESULT: |
| 95 | + subkind, message = _split_subkind(message) |
| 96 | + await self.result_queue.put((subkind, mion.loads(message))) |
| 97 | + case _ if kind in mion.ASYNC: |
| 98 | + subkind, message = _split_subkind(message) |
| 99 | + if iscoroutinefunction(self.oob_handler): |
| 100 | + await self.oob_handler((subkind, mion.loads(message))) |
| 101 | + else: |
| 102 | + self.oob_handler((subkind, mion.loads(message))) |
| 103 | + case _ if kind in mion.STREAM: |
| 104 | + if iscoroutinefunction(self.oob_handler): |
| 105 | + await self.oob_handler(message) |
| 106 | + else: |
| 107 | + self.oob_handler(message) |
| 108 | + case _: |
| 109 | + panic(f"Received unknown message from GDB: {kind}") |
| 110 | + |
| 111 | + async def _inferior_dispatch(self) -> None: |
| 112 | + self._inferior_dispatch_done.clear() |
| 113 | + with suppress(OSError): |
| 114 | + while output := await to_thread(os.read, self.fd_master, 512): |
| 115 | + if iscoroutinefunction(self.inferior_handler): |
| 116 | + await self.inferior_handler(output.decode()) |
| 117 | + else: |
| 118 | + self.inferior_handler(output.decode()) |
| 119 | + self._inferior_dispatch_done.set() |
| 120 | + |
| 121 | + |
| 122 | +def _split_subkind(message: str) -> tuple[str, str]: |
| 123 | + """ |
| 124 | + >>> _split_subkind('abc') |
| 125 | + ('abc', '') |
| 126 | + >>> _split_subkind('abc,def') |
| 127 | + ('abc', 'def') |
| 128 | + >>> _split_subkind('abc,def,ghi') |
| 129 | + ('abc', 'def,ghi') |
| 130 | + """ |
| 131 | + |
| 132 | + if "," in message: |
| 133 | + return tuple(message.split(",", 1)) |
| 134 | + return message, "" |
0 commit comments