Python: Add CLI debugger support

This commit is contained in:
Vicki Pfau 2017-07-15 07:30:32 -07:00
parent 2d49a41a30
commit 6a3002d398
6 changed files with 35 additions and 3 deletions

View File

@ -91,6 +91,7 @@ struct mDebugger {
struct mCPUComponent d;
struct mDebuggerPlatform* platform;
enum mDebuggerState state;
enum mDebuggerType type;
struct mCore* core;
struct mScriptBridge* bridge;

View File

@ -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;

View File

@ -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;

View File

@ -58,4 +58,5 @@ void free(void*);
#endif
#ifdef USE_DEBUGGERS
#include <mgba/debugger/debugger.h>
#include <mgba/internal/debugger/cli-debugger.h>
#endif

View File

@ -28,6 +28,7 @@ ffi.set_source("mgba._pylib", """
#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 @@ 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):

View File

@ -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)