all repos — mgba @ 19b77189c8f2a5d2f9172845836fae1e05263763

mGBA Game Boy Advance Emulator

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