all repos — mgba @ bf595be5c3fda6c84a9b7468b3bcce812596b355

mGBA Game Boy Advance Emulator

src/debugger/debugger.c (view raw)

  1/* Copyright (c) 2013-2016 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#include <mgba/debugger/debugger.h>
  7
  8#include <mgba/core/core.h>
  9
 10#include <mgba/internal/debugger/cli-debugger.h>
 11#include <mgba/internal/debugger/symbols.h>
 12
 13#ifdef USE_GDB_STUB
 14#include <mgba/internal/debugger/gdb-stub.h>
 15#endif
 16
 17#if ENABLE_SCRIPTING
 18#include <mgba/core/scripting.h>
 19#endif
 20
 21const uint32_t DEBUGGER_ID = 0xDEADBEEF;
 22
 23mLOG_DEFINE_CATEGORY(DEBUGGER, "Debugger", "core.debugger");
 24
 25DEFINE_VECTOR(mBreakpointList, struct mBreakpoint);
 26DEFINE_VECTOR(mWatchpointList, struct mWatchpoint);
 27
 28static void mDebuggerInit(void* cpu, struct mCPUComponent* component);
 29static void mDebuggerDeinit(struct mCPUComponent* component);
 30
 31struct mDebugger* mDebuggerCreate(enum mDebuggerType type, struct mCore* core) {
 32	if (!core->supportsDebuggerType(core, type)) {
 33		return NULL;
 34	}
 35
 36	union DebugUnion {
 37		struct mDebugger d;
 38		struct CLIDebugger cli;
 39#ifdef USE_GDB_STUB
 40		struct GDBStub gdb;
 41#endif
 42	};
 43
 44	union DebugUnion* debugger = malloc(sizeof(union DebugUnion));
 45	memset(debugger, 0, sizeof(*debugger));
 46
 47	switch (type) {
 48	case DEBUGGER_CLI:
 49		CLIDebuggerCreate(&debugger->cli);
 50		struct CLIDebuggerSystem* sys = core->cliDebuggerSystem(core);
 51		CLIDebuggerAttachSystem(&debugger->cli, sys);
 52		break;
 53#ifdef USE_GDB_STUB
 54	case DEBUGGER_GDB:
 55		GDBStubCreate(&debugger->gdb);
 56		GDBStubListen(&debugger->gdb, 2345, 0);
 57		break;
 58#endif
 59	case DEBUGGER_NONE:
 60	case DEBUGGER_MAX:
 61		free(debugger);
 62		return 0;
 63		break;
 64	}
 65
 66	return &debugger->d;
 67}
 68
 69void mDebuggerAttach(struct mDebugger* debugger, struct mCore* core) {
 70	debugger->d.id = DEBUGGER_ID;
 71	debugger->d.init = mDebuggerInit;
 72	debugger->d.deinit = mDebuggerDeinit;
 73	debugger->core = core;
 74	if (!debugger->core->symbolTable) {
 75		debugger->core->loadSymbols(debugger->core, NULL);
 76	}
 77	debugger->platform = core->debuggerPlatform(core);
 78	debugger->platform->p = debugger;
 79	core->attachDebugger(core, debugger);
 80}
 81
 82void mDebuggerRun(struct mDebugger* debugger) {
 83	switch (debugger->state) {
 84	case DEBUGGER_RUNNING:
 85		if (!debugger->platform->hasBreakpoints(debugger->platform)) {
 86			debugger->core->runLoop(debugger->core);
 87		} else {
 88			debugger->core->step(debugger->core);
 89			debugger->platform->checkBreakpoints(debugger->platform);
 90		}
 91		break;
 92	case DEBUGGER_CUSTOM:
 93		debugger->core->step(debugger->core);
 94		debugger->platform->checkBreakpoints(debugger->platform);
 95		debugger->custom(debugger);
 96		break;
 97	case DEBUGGER_PAUSED:
 98		if (debugger->paused) {
 99			debugger->paused(debugger);
100		} else {
101			debugger->state = DEBUGGER_RUNNING;
102		}
103		break;
104	case DEBUGGER_SHUTDOWN:
105		return;
106	}
107}
108
109void mDebuggerRunFrame(struct mDebugger* debugger) {
110	int32_t frame = debugger->core->frameCounter(debugger->core);
111	do {
112		mDebuggerRun(debugger);
113	} while (debugger->core->frameCounter(debugger->core) == frame);
114}
115
116void mDebuggerEnter(struct mDebugger* debugger, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
117	debugger->state = DEBUGGER_PAUSED;
118	if (debugger->platform->entered) {
119		debugger->platform->entered(debugger->platform, reason, info);
120	}
121#ifdef ENABLE_SCRIPTING
122	if (debugger->bridge) {
123		mScriptBridgeDebuggerEntered(debugger->bridge, reason, info);
124	}
125#endif
126}
127
128static void mDebuggerInit(void* cpu, struct mCPUComponent* component) {
129	struct mDebugger* debugger = (struct mDebugger*) component;
130	debugger->state = DEBUGGER_RUNNING;
131	debugger->platform->init(cpu, debugger->platform);
132	if (debugger->init) {
133		debugger->init(debugger);
134	}
135}
136
137static void mDebuggerDeinit(struct mCPUComponent* component) {
138	struct mDebugger* debugger = (struct mDebugger*) component;
139	if (debugger->deinit) {
140		debugger->deinit(debugger);
141	}
142	debugger->platform->deinit(debugger->platform);
143}
144
145bool mDebuggerLookupIdentifier(struct mDebugger* debugger, const char* name, int32_t* value, int* segment) {
146	*segment = -1;
147#ifdef ENABLE_SCRIPTING
148	if (debugger->bridge && mScriptBridgeLookupSymbol(debugger->bridge, name, value)) {
149		return true;
150	}
151#endif
152	if (debugger->core->symbolTable && mDebuggerSymbolLookup(debugger->core->symbolTable, name, value, segment)) {
153		return true;
154	}
155	if (debugger->core->lookupIdentifier(debugger->core, name, value, segment)) {
156		return true;
157	}
158	if (debugger->platform && debugger->platform->getRegister(debugger->platform, name, value)) {
159		return true;
160	}
161	return false;
162}