include/mgba/debugger/debugger.h (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/. */
6#ifndef DEBUGGER_H
7#define DEBUGGER_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/cpu.h>
14#include <mgba/core/log.h>
15
16mLOG_DECLARE_CATEGORY(DEBUGGER);
17
18extern const uint32_t DEBUGGER_ID;
19
20enum mDebuggerType {
21 DEBUGGER_NONE = 0,
22 DEBUGGER_CLI,
23#ifdef USE_GDB_STUB
24 DEBUGGER_GDB,
25#endif
26 DEBUGGER_MAX
27};
28
29enum mDebuggerState {
30 DEBUGGER_PAUSED,
31 DEBUGGER_RUNNING,
32 DEBUGGER_CUSTOM,
33 DEBUGGER_SHUTDOWN
34};
35
36enum mWatchpointType {
37 WATCHPOINT_WRITE = 1,
38 WATCHPOINT_READ = 2,
39 WATCHPOINT_RW = 3
40};
41
42enum mBreakpointType {
43 BREAKPOINT_HARDWARE,
44 BREAKPOINT_SOFTWARE
45};
46
47enum mDebuggerEntryReason {
48 DEBUGGER_ENTER_MANUAL,
49 DEBUGGER_ENTER_ATTACHED,
50 DEBUGGER_ENTER_BREAKPOINT,
51 DEBUGGER_ENTER_WATCHPOINT,
52 DEBUGGER_ENTER_ILLEGAL_OP
53};
54
55struct mDebuggerEntryInfo {
56 uint32_t address;
57 union {
58 struct {
59 uint32_t oldValue;
60 uint32_t newValue;
61 enum mWatchpointType watchType;
62 enum mWatchpointType accessType;
63 } wp;
64
65 struct {
66 uint32_t opcode;
67 enum mBreakpointType breakType;
68 } bp;
69 } type;
70};
71
72struct mDebugger;
73struct ParseTree;
74struct mDebuggerPlatform {
75 struct mDebugger* p;
76
77 void (*init)(void* cpu, struct mDebuggerPlatform*);
78 void (*deinit)(struct mDebuggerPlatform*);
79 void (*entered)(struct mDebuggerPlatform*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
80
81 bool (*hasBreakpoints)(struct mDebuggerPlatform*);
82 void (*setBreakpoint)(struct mDebuggerPlatform*, uint32_t address, int segment);
83 void (*setConditionalBreakpoint)(struct mDebuggerPlatform*, uint32_t address, int segment, struct ParseTree* condition);
84 void (*clearBreakpoint)(struct mDebuggerPlatform*, uint32_t address, int segment);
85 void (*setWatchpoint)(struct mDebuggerPlatform*, uint32_t address, int segment, enum mWatchpointType type);
86 void (*setConditionalWatchpoint)(struct mDebuggerPlatform*, uint32_t address, int segment, enum mWatchpointType type, struct ParseTree* condition);
87 void (*clearWatchpoint)(struct mDebuggerPlatform*, uint32_t address, int segment);
88 void (*checkBreakpoints)(struct mDebuggerPlatform*);
89 void (*trace)(struct mDebuggerPlatform*, char* out, size_t* length);
90
91 bool (*getRegister)(struct mDebuggerPlatform*, const char* name, int32_t* value);
92 bool (*setRegister)(struct mDebuggerPlatform*, const char* name, int32_t value);
93 bool (*lookupIdentifier)(struct mDebuggerPlatform*, const char* name, int32_t* value, int* segment);
94};
95
96struct mDebugger {
97 struct mCPUComponent d;
98 struct mDebuggerPlatform* platform;
99 enum mDebuggerState state;
100 enum mDebuggerType type;
101 struct mCore* core;
102 struct mScriptBridge* bridge;
103
104 void (*init)(struct mDebugger*);
105 void (*deinit)(struct mDebugger*);
106
107 void (*paused)(struct mDebugger*);
108 void (*entered)(struct mDebugger*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
109 void (*custom)(struct mDebugger*);
110};
111
112struct mDebugger* mDebuggerCreate(enum mDebuggerType type, struct mCore*);
113void mDebuggerAttach(struct mDebugger*, struct mCore*);
114void mDebuggerRun(struct mDebugger*);
115void mDebuggerRunFrame(struct mDebugger*);
116void mDebuggerEnter(struct mDebugger*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
117
118bool mDebuggerLookupIdentifier(struct mDebugger* debugger, const char* name, int32_t* value, int* segment);
119
120CXX_GUARD_END
121
122#endif