all repos — mgba @ 618092a5f6b5ed1501e361b4c7c2f390f4967d66

mGBA Game Boy Advance Emulator

src/debugger/debugger.c (view raw)

  1/* Copyright (c) 2013-2014 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 "debugger.h"
  7
  8#include "arm.h"
  9#include "isa-inlines.h"
 10
 11#include "memory-debugger.h"
 12
 13const uint32_t ARM_DEBUGGER_ID = 0xDEADBEEF;
 14
 15static struct DebugBreakpoint* _lookupBreakpoint(struct DebugBreakpoint* breakpoints, uint32_t address) {
 16	for (; breakpoints; breakpoints = breakpoints->next) {
 17		if (breakpoints->address == address) {
 18			return breakpoints;
 19		}
 20	}
 21	return 0;
 22}
 23
 24static void _checkBreakpoints(struct ARMDebugger* debugger) {
 25	int instructionLength;
 26	enum ExecutionMode mode = debugger->cpu->cpsr.t;
 27	if (mode == MODE_ARM) {
 28		instructionLength = WORD_SIZE_ARM;
 29	} else {
 30		instructionLength = WORD_SIZE_THUMB;
 31	}
 32	struct DebugBreakpoint* breakpoint = _lookupBreakpoint(debugger->breakpoints, debugger->cpu->gprs[ARM_PC] - instructionLength);
 33	if (!breakpoint) {
 34		return;
 35	}
 36	struct DebuggerEntryInfo info = {
 37		.address = breakpoint->address
 38	};
 39	ARMDebuggerEnter(debugger, DEBUGGER_ENTER_BREAKPOINT, &info);
 40}
 41
 42static void ARMDebuggerInit(struct ARMCore*, struct ARMComponent*);
 43static void ARMDebuggerDeinit(struct ARMComponent*);
 44
 45void ARMDebuggerCreate(struct ARMDebugger* debugger) {
 46	debugger->d.id = ARM_DEBUGGER_ID;
 47	debugger->d.init = ARMDebuggerInit;
 48	debugger->d.deinit = ARMDebuggerDeinit;
 49}
 50
 51void ARMDebuggerInit(struct ARMCore* cpu, struct ARMComponent* component) {
 52	struct ARMDebugger* debugger = (struct ARMDebugger*) component;
 53	debugger->cpu = cpu;
 54	debugger->state = DEBUGGER_RUNNING;
 55	debugger->breakpoints = 0;
 56	debugger->originalMemory = cpu->memory;
 57	debugger->watchpoints = 0;
 58	debugger->currentBreakpoint = 0;
 59	if (debugger->init) {
 60		debugger->init(debugger);
 61	}
 62}
 63
 64void ARMDebuggerDeinit(struct ARMComponent* component) {
 65	struct ARMDebugger* debugger = (struct ARMDebugger*) component;
 66	debugger->deinit(debugger);
 67}
 68
 69void ARMDebuggerRun(struct ARMDebugger* debugger) {
 70	switch (debugger->state) {
 71	case DEBUGGER_RUNNING:
 72		if (!debugger->breakpoints && !debugger->watchpoints) {
 73			ARMRunLoop(debugger->cpu);
 74		} else {
 75			ARMRun(debugger->cpu);
 76			_checkBreakpoints(debugger);
 77		}
 78		break;
 79	case DEBUGGER_CUSTOM:
 80		ARMRun(debugger->cpu);
 81		_checkBreakpoints(debugger);
 82		debugger->custom(debugger);
 83		break;
 84	case DEBUGGER_PAUSED:
 85		if (debugger->paused) {
 86			debugger->paused(debugger);
 87		} else {
 88			debugger->state = DEBUGGER_RUNNING;
 89		}
 90		if (debugger->state != DEBUGGER_PAUSED && debugger->currentBreakpoint) {
 91			if (debugger->currentBreakpoint->isSw && debugger->setSoftwareBreakpoint) {
 92				debugger->setSoftwareBreakpoint(debugger, debugger->currentBreakpoint->address, debugger->currentBreakpoint->sw.mode, &debugger->currentBreakpoint->sw.opcode);
 93			}
 94			debugger->currentBreakpoint = 0;
 95		}
 96		break;
 97	case DEBUGGER_SHUTDOWN:
 98		return;
 99	}
100}
101
102void ARMDebuggerEnter(struct ARMDebugger* debugger, enum DebuggerEntryReason reason, struct DebuggerEntryInfo* info) {
103	debugger->state = DEBUGGER_PAUSED;
104	struct ARMCore* cpu = debugger->cpu;
105	cpu->nextEvent = 0;
106	if (reason == DEBUGGER_ENTER_BREAKPOINT) {
107		struct DebugBreakpoint* breakpoint = _lookupBreakpoint(debugger->swBreakpoints, _ARMPCAddress(cpu));
108		debugger->currentBreakpoint = breakpoint;
109		if (breakpoint && breakpoint->isSw) {
110			info->address = breakpoint->address;
111			if (debugger->clearSoftwareBreakpoint) {
112				debugger->clearSoftwareBreakpoint(debugger, breakpoint->address, breakpoint->sw.mode, breakpoint->sw.opcode);
113			}
114
115			ARMRunFake(cpu, breakpoint->sw.opcode);
116		}
117	}
118	if (debugger->entered) {
119		debugger->entered(debugger, reason, info);
120	}
121}
122
123void ARMDebuggerSetBreakpoint(struct ARMDebugger* debugger, uint32_t address) {
124	struct DebugBreakpoint* breakpoint = malloc(sizeof(struct DebugBreakpoint));
125	breakpoint->address = address;
126	breakpoint->next = debugger->breakpoints;
127	breakpoint->isSw = false;
128	debugger->breakpoints = breakpoint;
129}
130
131bool ARMDebuggerSetSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode) {
132	uint32_t opcode;
133	if (!debugger->setSoftwareBreakpoint || !debugger->setSoftwareBreakpoint(debugger, address, mode, &opcode)) {
134		return false;
135	}
136
137	struct DebugBreakpoint* breakpoint = malloc(sizeof(struct DebugBreakpoint));
138	breakpoint->address = address;
139	breakpoint->next = debugger->swBreakpoints;
140	breakpoint->isSw = true;
141	breakpoint->sw.opcode = opcode;
142	breakpoint->sw.mode = mode;
143	debugger->swBreakpoints = breakpoint;
144
145	return true;
146}
147
148void ARMDebuggerClearBreakpoint(struct ARMDebugger* debugger, uint32_t address) {
149	struct DebugBreakpoint** previous = &debugger->breakpoints;
150	struct DebugBreakpoint* breakpoint;
151	for (; (breakpoint = *previous); previous = &breakpoint->next) {
152		if (breakpoint->address == address) {
153			*previous = breakpoint->next;
154			free(breakpoint);
155		}
156	}
157}
158
159void ARMDebuggerSetWatchpoint(struct ARMDebugger* debugger, uint32_t address) {
160	if (!debugger->watchpoints) {
161		ARMDebuggerInstallMemoryShim(debugger);
162	}
163	struct DebugWatchpoint* watchpoint = malloc(sizeof(struct DebugWatchpoint));
164	watchpoint->address = address;
165	watchpoint->next = debugger->watchpoints;
166	debugger->watchpoints = watchpoint;
167}
168
169void ARMDebuggerClearWatchpoint(struct ARMDebugger* debugger, uint32_t address) {
170	struct DebugWatchpoint** previous = &debugger->watchpoints;
171	struct DebugWatchpoint* breakpoint;
172	for (; (breakpoint = *previous); previous = &breakpoint->next) {
173		if (breakpoint->address == address) {
174			*previous = breakpoint->next;
175			free(breakpoint);
176		}
177	}
178	if (!debugger->watchpoints) {
179		ARMDebuggerRemoveMemoryShim(debugger);
180	}
181}