include/mgba/debugger/debugger.h (view raw)
1/* Copyright (c) 2013-2019 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#include <mgba-util/vector.h>
16
17mLOG_DECLARE_CATEGORY(DEBUGGER);
18
19extern const uint32_t DEBUGGER_ID;
20
21enum mDebuggerType {
22 DEBUGGER_NONE = 0,
23 DEBUGGER_CUSTOM,
24 DEBUGGER_CLI,
25 DEBUGGER_GDB,
26 DEBUGGER_MAX
27};
28
29enum mDebuggerState {
30 DEBUGGER_PAUSED,
31 DEBUGGER_RUNNING,
32 DEBUGGER_CALLBACK,
33 DEBUGGER_SHUTDOWN
34};
35
36enum mWatchpointType {
37 WATCHPOINT_WRITE = 1,
38 WATCHPOINT_READ = 2,
39 WATCHPOINT_RW = 3,
40 WATCHPOINT_WRITE_CHANGE = 4,
41};
42
43enum mBreakpointType {
44 BREAKPOINT_HARDWARE,
45 BREAKPOINT_SOFTWARE
46};
47
48enum mDebuggerEntryReason {
49 DEBUGGER_ENTER_MANUAL,
50 DEBUGGER_ENTER_ATTACHED,
51 DEBUGGER_ENTER_BREAKPOINT,
52 DEBUGGER_ENTER_WATCHPOINT,
53 DEBUGGER_ENTER_ILLEGAL_OP
54};
55
56struct mDebuggerEntryInfo {
57 uint32_t address;
58 union {
59 struct {
60 uint32_t oldValue;
61 uint32_t newValue;
62 enum mWatchpointType watchType;
63 enum mWatchpointType accessType;
64 } wp;
65
66 struct {
67 uint32_t opcode;
68 enum mBreakpointType breakType;
69 } bp;
70 } type;
71 ssize_t pointId;
72};
73
74struct mBreakpoint {
75 ssize_t id;
76 uint32_t address;
77 int segment;
78 enum mBreakpointType type;
79 struct ParseTree* condition;
80};
81
82struct mWatchpoint {
83 ssize_t id;
84 uint32_t address;
85 int segment;
86 enum mWatchpointType type;
87 struct ParseTree* condition;
88};
89
90DECLARE_VECTOR(mBreakpointList, struct mBreakpoint);
91DECLARE_VECTOR(mWatchpointList, struct mWatchpoint);
92
93struct mDebugger;
94struct ParseTree;
95struct mDebuggerPlatform {
96 struct mDebugger* p;
97
98 void (*init)(void* cpu, struct mDebuggerPlatform*);
99 void (*deinit)(struct mDebuggerPlatform*);
100 void (*entered)(struct mDebuggerPlatform*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
101
102 bool (*hasBreakpoints)(struct mDebuggerPlatform*);
103 void (*checkBreakpoints)(struct mDebuggerPlatform*);
104 bool (*clearBreakpoint)(struct mDebuggerPlatform*, ssize_t id);
105
106 ssize_t (*setBreakpoint)(struct mDebuggerPlatform*, const struct mBreakpoint*);
107 void (*listBreakpoints)(struct mDebuggerPlatform*, struct mBreakpointList*);
108
109 ssize_t (*setWatchpoint)(struct mDebuggerPlatform*, const struct mWatchpoint*);
110 void (*listWatchpoints)(struct mDebuggerPlatform*, struct mWatchpointList*);
111
112 void (*trace)(struct mDebuggerPlatform*, char* out, size_t* length);
113
114 bool (*getRegister)(struct mDebuggerPlatform*, const char* name, int32_t* value);
115 bool (*setRegister)(struct mDebuggerPlatform*, const char* name, int32_t value);
116 bool (*lookupIdentifier)(struct mDebuggerPlatform*, const char* name, int32_t* value, int* segment);
117};
118
119struct mDebugger {
120 struct mCPUComponent d;
121 struct mDebuggerPlatform* platform;
122 enum mDebuggerState state;
123 enum mDebuggerType type;
124 struct mCore* core;
125 struct mScriptBridge* bridge;
126
127 void (*init)(struct mDebugger*);
128 void (*deinit)(struct mDebugger*);
129
130 void (*paused)(struct mDebugger*);
131 void (*entered)(struct mDebugger*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
132 void (*custom)(struct mDebugger*);
133};
134
135struct mDebugger* mDebuggerCreate(enum mDebuggerType type, struct mCore*);
136void mDebuggerAttach(struct mDebugger*, struct mCore*);
137void mDebuggerRun(struct mDebugger*);
138void mDebuggerRunFrame(struct mDebugger*);
139void mDebuggerEnter(struct mDebugger*, enum mDebuggerEntryReason, struct mDebuggerEntryInfo*);
140
141bool mDebuggerLookupIdentifier(struct mDebugger* debugger, const char* name, int32_t* value, int* segment);
142
143CXX_GUARD_END
144
145#endif