all repos — mgba @ 6a3002d398033cb74d623bf9bf13ed5c6330c10b

mGBA Game Boy Advance Emulator

Python: Add CLI debugger support
Vicki Pfau vi@endrift.com
Sat, 15 Jul 2017 07:30:32 -0700
commit

6a3002d398033cb74d623bf9bf13ed5c6330c10b

parent

2d49a41a30bca9ad47457ad8825fc13b9e82de81

M include/mgba/debugger/debugger.hinclude/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;
M src/debugger/cli-debugger.csrc/debugger/cli-debugger.c

@@ -832,6 +832,7 @@ debugger->d.deinit = _cliDebuggerDeinit;

debugger->d.custom = _cliDebuggerCustom; debugger->d.paused = _commandLine; debugger->d.entered = _reportEntry; + debugger->d.type = DEBUGGER_CLI; debugger->system = NULL; debugger->backend = NULL;
M src/debugger/gdb-stub.csrc/debugger/gdb-stub.c

@@ -658,6 +658,7 @@ stub->d.deinit = _gdbStubDeinit;

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;
M src/platform/python/_builder.hsrc/platform/python/_builder.h

@@ -58,4 +58,5 @@ #include <mgba/internal/gb/renderers/tile-cache.h>

#endif #ifdef USE_DEBUGGERS #include <mgba/debugger/debugger.h> +#include <mgba/internal/debugger/cli-debugger.h> #endif
M src/platform/python/_builder.pysrc/platform/python/_builder.py

@@ -28,6 +28,7 @@ #include <mgba/core/tile-cache.h>

#include <mgba/core/version.h> #include <mgba/debugger/debugger.h> #include <mgba/internal/arm/arm.h> +#include <mgba/internal/debugger/cli-debugger.h> #include <mgba/internal/gba/gba.h> #include <mgba/internal/gba/input.h> #include <mgba/internal/gba/renderers/tile-cache.h>

@@ -70,17 +71,23 @@ lines.append(line)

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):
M src/platform/python/mgba/debugger.pysrc/platform/python/mgba/debugger.py

@@ -5,6 +5,8 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this

# 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 @@ self._native.platform.clearWatchpoint(self._native.platform, address)

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)