all repos — mgba @ 25b4faef1262e37154e955d21d0fe1befa07e23b

mGBA Game Boy Advance Emulator

src/platform/python/mgba/debugger.py (view raw)

 1# Copyright (c) 2013-2017 Jeffrey Pfau
 2#
 3# This Source Code Form is subject to the terms of the Mozilla Public
 4# License, v. 2.0. If a copy of the MPL was not distributed with this
 5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6from ._pylib import ffi, lib
 7from .core import IRunner, ICoreOwner, Core
 8
 9class DebuggerCoreOwner(ICoreOwner):
10    def __init__(self, debugger):
11        self.debugger = debugger
12        self.wasPaused = False
13
14    def claim(self):
15        if self.debugger.isRunning():
16            self.wasPaused = True
17            self.debugger.pause()
18        return self.debugger._core
19
20    def release(self):
21        if self.wasPaused:
22            self.debugger.unpause()
23
24class NativeDebugger(IRunner):
25    WATCHPOINT_WRITE = lib.WATCHPOINT_WRITE
26    WATCHPOINT_READ = lib.WATCHPOINT_READ
27    WATCHPOINT_RW = lib.WATCHPOINT_RW
28
29    def __init__(self, native):
30        self._native = native
31        self._core = Core._detect(native.core)
32        self._core._wasReset = True
33
34    def pause(self):
35        lib.mDebuggerEnter(self._native, lib.DEBUGGER_ENTER_MANUAL, ffi.NULL)
36
37    def unpause(self):
38        self._native.state = lib.DEBUGGER_RUNNING
39
40    def isRunning(self):
41        return self._native.state == lib.DEBUGGER_RUNNING
42
43    def isPaused(self):
44        return self._native.state in (lib.DEBUGGER_PAUSED, lib.DEBUGGER_CUSTOM)
45
46    def useCore(self):
47        return DebuggerCoreOwner(self)
48
49    def setBreakpoint(self, address):
50        if not self._native.platform.setBreakpoint:
51            raise RuntimeError("Platform does not support breakpoints")
52        self._native.platform.setBreakpoint(self._native.platform, address)
53
54    def clearBreakpoint(self, address):
55        if not self._native.platform.setBreakpoint:
56            raise RuntimeError("Platform does not support breakpoints")
57        self._native.platform.clearBreakpoint(self._native.platform, address)
58
59    def setWatchpoint(self, address):
60        if not self._native.platform.setWatchpoint:
61            raise RuntimeError("Platform does not support watchpoints")
62        self._native.platform.setWatchpoint(self._native.platform, address)
63
64    def clearWatchpoint(self, address):
65        if not self._native.platform.clearWatchpoint:
66            raise RuntimeError("Platform does not support watchpoints")
67        self._native.platform.clearWatchpoint(self._native.platform, address)