From 6a3002d398033cb74d623bf9bf13ed5c6330c10b Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sat, 15 Jul 2017 07:30:32 -0700 Subject: [PATCH] Python: Add CLI debugger support --- include/mgba/debugger/debugger.h | 1 + src/debugger/cli-debugger.c | 1 + src/debugger/gdb-stub.c | 1 + src/platform/python/_builder.h | 1 + src/platform/python/_builder.py | 13 ++++++++++--- src/platform/python/mgba/debugger.py | 21 +++++++++++++++++++++ 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/mgba/debugger/debugger.h b/include/mgba/debugger/debugger.h index 34535b9d4..213236425 100644 --- a/include/mgba/debugger/debugger.h +++ b/include/mgba/debugger/debugger.h @@ -91,6 +91,7 @@ struct mDebugger { struct mCPUComponent d; struct mDebuggerPlatform* platform; enum mDebuggerState state; + enum mDebuggerType type; struct mCore* core; struct mScriptBridge* bridge; diff --git a/src/debugger/cli-debugger.c b/src/debugger/cli-debugger.c index c991f08ee..9eb9801bd 100644 --- a/src/debugger/cli-debugger.c +++ b/src/debugger/cli-debugger.c @@ -832,6 +832,7 @@ void CLIDebuggerCreate(struct CLIDebugger* debugger) { debugger->d.custom = _cliDebuggerCustom; debugger->d.paused = _commandLine; debugger->d.entered = _reportEntry; + debugger->d.type = DEBUGGER_CLI; debugger->system = NULL; debugger->backend = NULL; diff --git a/src/debugger/gdb-stub.c b/src/debugger/gdb-stub.c index 3c149c9fc..6e2d287fa 100644 --- a/src/debugger/gdb-stub.c +++ b/src/debugger/gdb-stub.c @@ -658,6 +658,7 @@ void GDBStubCreate(struct GDBStub* stub) { stub->d.paused = _gdbStubWait; stub->d.entered = _gdbStubEntered; stub->d.custom = _gdbStubPoll; + stub->d.type = DEBUGGER_GDB; stub->untilPoll = GDB_STUB_INTERVAL; stub->lineAck = GDB_ACK_PENDING; stub->shouldBlock = false; diff --git a/src/platform/python/_builder.h b/src/platform/python/_builder.h index 0af3b4124..19842c402 100644 --- a/src/platform/python/_builder.h +++ b/src/platform/python/_builder.h @@ -58,4 +58,5 @@ void free(void*); #endif #ifdef USE_DEBUGGERS #include +#include #endif diff --git a/src/platform/python/_builder.py b/src/platform/python/_builder.py index 05cbddb39..79947021e 100644 --- a/src/platform/python/_builder.py +++ b/src/platform/python/_builder.py @@ -28,6 +28,7 @@ ffi.set_source("mgba._pylib", """ #include #include #include +#include #include #include #include @@ -70,17 +71,23 @@ for line in preprocessed.splitlines(): ffi.embedding_api('\n'.join(lines)) ffi.embedding_init_code(""" - from mgba._pylib import ffi + from mgba._pylib import ffi, lib debugger = None pendingCode = [] @ffi.def_extern() def mPythonSetDebugger(_debugger): - from mgba.debugger import NativeDebugger + from mgba.debugger import NativeDebugger, CLIDebugger global debugger if debugger and debugger._native == _debugger: return - debugger = _debugger and NativeDebugger(_debugger) + if not _debugger: + debugger = None + return + if _debugger.type == lib.DEBUGGER_CLI: + debugger = CLIDebugger(_debugger) + else: + debugger = NativeDebugger(_debugger) @ffi.def_extern() def mPythonLoadScript(name, vf): diff --git a/src/platform/python/mgba/debugger.py b/src/platform/python/mgba/debugger.py index d6bf30e2e..2d597491c 100644 --- a/src/platform/python/mgba/debugger.py +++ b/src/platform/python/mgba/debugger.py @@ -5,6 +5,8 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. from ._pylib import ffi, lib from .core import IRunner, ICoreOwner, Core +import io +import sys class DebuggerCoreOwner(ICoreOwner): def __init__(self, debugger): @@ -78,3 +80,22 @@ class NativeDebugger(IRunner): def addCallback(self, cb): self._cbs.append(cb) + +class CLIBackend(object): + def __init__(self, backend): + self.backend = backend + + def write(self, string): + self.backend.printf(string) + +class CLIDebugger(NativeDebugger): + def __init__(self, native): + super(CLIDebugger, self).__init__(native) + self._cli = ffi.cast("struct CLIDebugger*", native) + + def printf(self, message, *args, **kwargs): + message = message.format(*args, **kwargs) + self._cli.backend.printf(ffi.new("char []", b"%s"), ffi.new("char []", message.encode('utf-8'))) + + def installPrint(self): + sys.stdout = CLIBackend(self)