all repos — mgba @ 1cc0bdeec140cbe411020b8239f3dd47852a9e2f

mGBA Game Boy Advance Emulator

src/arm/cli-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 "cli-debugger.h"
  7
  8#ifdef USE_CLI_DEBUGGER
  9#include "arm/memory-debugger.h"
 10#include "arm/decoder.h"
 11#include "core/core.h"
 12#include "debugger/cli-debugger.h"
 13
 14static void _printStatus(struct CLIDebuggerSystem*);
 15
 16static void _disassembleArm(struct CLIDebugger*, struct CLIDebugVector*);
 17static void _disassembleThumb(struct CLIDebugger*, struct CLIDebugVector*);
 18static void _setBreakpointARM(struct CLIDebugger*, struct CLIDebugVector*);
 19static void _setBreakpointThumb(struct CLIDebugger*, struct CLIDebugVector*);
 20static void _writeRegister(struct CLIDebugger*, struct CLIDebugVector*);
 21
 22static void _disassembleMode(struct CLIDebugger*, struct CLIDebugVector*, enum ExecutionMode mode);
 23static uint32_t _printLine(struct CLIDebugger* debugger, uint32_t address, enum ExecutionMode mode);
 24
 25static struct CLIDebuggerCommandSummary _armCommands[] = {
 26	{ "b/a", _setBreakpointARM, CLIDVParse, "Set a software breakpoint as ARM" },
 27	{ "b/t", _setBreakpointThumb, CLIDVParse, "Set a software breakpoint as Thumb" },
 28	{ "break/a", _setBreakpointARM, CLIDVParse, "Set a software breakpoint as ARM" },
 29	{ "break/t", _setBreakpointThumb, CLIDVParse, "Set a software breakpoint as Thumb" },
 30	{ "dis/a", _disassembleArm, CLIDVParse, "Disassemble instructions as ARM" },
 31	{ "dis/t", _disassembleThumb, CLIDVParse, "Disassemble instructions as Thumb" },
 32	{ "disasm/a", _disassembleArm, CLIDVParse, "Disassemble instructions as ARM" },
 33	{ "disasm/t", _disassembleThumb, CLIDVParse, "Disassemble instructions as Thumb" },
 34	{ "disassemble/a", _disassembleArm, CLIDVParse, "Disassemble instructions as ARM" },
 35	{ "disassemble/t", _disassembleThumb, CLIDVParse, "Disassemble instructions as Thumb" },
 36	{ "w/r", _writeRegister, CLIDVParse, "Write a register" },
 37	{ 0, 0, 0, 0 }
 38};
 39
 40static inline void _printPSR(union PSR psr) {
 41	printf("%08X [%c%c%c%c%c%c%c]\n", psr.packed,
 42	       psr.n ? 'N' : '-',
 43	       psr.z ? 'Z' : '-',
 44	       psr.c ? 'C' : '-',
 45	       psr.v ? 'V' : '-',
 46	       psr.i ? 'I' : '-',
 47	       psr.f ? 'F' : '-',
 48	       psr.t ? 'T' : '-');
 49}
 50
 51static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv) {
 52	struct ARMCore* cpu = debugger->p->d.core->cpu;
 53	_disassembleMode(debugger->p, dv, cpu->executionMode);
 54}
 55
 56static void _disassembleArm(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 57	_disassembleMode(debugger, dv, MODE_ARM);
 58}
 59
 60static void _disassembleThumb(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 61	_disassembleMode(debugger, dv, MODE_THUMB);
 62}
 63
 64static void _disassembleMode(struct CLIDebugger* debugger, struct CLIDebugVector* dv, enum ExecutionMode mode) {
 65	struct ARMCore* cpu = debugger->d.core->cpu;
 66	uint32_t address;
 67	int size;
 68	int wordSize;
 69
 70	if (mode == MODE_ARM) {
 71		wordSize = WORD_SIZE_ARM;
 72	} else {
 73		wordSize = WORD_SIZE_THUMB;
 74	}
 75
 76	if (!dv || dv->type != CLIDV_INT_TYPE) {
 77		address = cpu->gprs[ARM_PC] - wordSize;
 78	} else {
 79		address = dv->intValue;
 80		dv = dv->next;
 81	}
 82
 83	if (!dv || dv->type != CLIDV_INT_TYPE) {
 84		size = 1;
 85	} else {
 86		size = dv->intValue;
 87		dv = dv->next; // TODO: Check for excess args
 88	}
 89
 90	int i;
 91	for (i = 0; i < size; ++i) {
 92		address += _printLine(debugger, address, mode);
 93	}
 94}
 95
 96static inline uint32_t _printLine(struct CLIDebugger* debugger, uint32_t address, enum ExecutionMode mode) {
 97	char disassembly[48];
 98	struct ARMInstructionInfo info;
 99	printf("%08X:  ", address);
100	if (mode == MODE_ARM) {
101		uint32_t instruction = debugger->d.core->busRead32(debugger->d.core, address);
102		ARMDecodeARM(instruction, &info);
103		ARMDisassemble(&info, address + WORD_SIZE_ARM * 2, disassembly, sizeof(disassembly));
104		printf("%08X\t%s\n", instruction, disassembly);
105		return WORD_SIZE_ARM;
106	} else {
107		struct ARMInstructionInfo info2;
108		struct ARMInstructionInfo combined;
109		uint16_t instruction = debugger->d.core->busRead16(debugger->d.core, address);
110		uint16_t instruction2 = debugger->d.core->busRead16(debugger->d.core, address + WORD_SIZE_THUMB);
111		ARMDecodeThumb(instruction, &info);
112		ARMDecodeThumb(instruction2, &info2);
113		if (ARMDecodeThumbCombine(&info, &info2, &combined)) {
114			ARMDisassemble(&combined, address + WORD_SIZE_THUMB * 2, disassembly, sizeof(disassembly));
115			printf("%04X %04X\t%s\n", instruction, instruction2, disassembly);
116			return WORD_SIZE_THUMB * 2;
117		} else {
118			ARMDisassemble(&info, address + WORD_SIZE_THUMB * 2, disassembly, sizeof(disassembly));
119			printf("%04X     \t%s\n", instruction, disassembly);
120			return WORD_SIZE_THUMB;
121		}
122	}
123}
124
125static void _printStatus(struct CLIDebuggerSystem* debugger) {
126	struct ARMCore* cpu = debugger->p->d.core->cpu;
127	int r;
128	for (r = 0; r < 4; ++r) {
129		printf("%08X %08X %08X %08X\n",
130		    cpu->gprs[r << 2],
131		    cpu->gprs[(r << 2) + 1],
132		    cpu->gprs[(r << 2) + 2],
133		    cpu->gprs[(r << 2) + 3]);
134	}
135	_printPSR(cpu->cpsr);
136	int instructionLength;
137	enum ExecutionMode mode = cpu->cpsr.t;
138	if (mode == MODE_ARM) {
139		instructionLength = WORD_SIZE_ARM;
140	} else {
141		instructionLength = WORD_SIZE_THUMB;
142	}
143	_printLine(debugger->p, cpu->gprs[ARM_PC] - instructionLength, mode);
144}
145
146static void _writeRegister(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
147	struct ARMCore* cpu = debugger->d.core->cpu;
148	if (!dv || dv->type != CLIDV_INT_TYPE) {
149		printf("%s\n", ERROR_MISSING_ARGS);
150		return;
151	}
152	if (!dv->next || dv->next->type != CLIDV_INT_TYPE) {
153		printf("%s\n", ERROR_MISSING_ARGS);
154		return;
155	}
156	uint32_t regid = dv->intValue;
157	uint32_t value = dv->next->intValue;
158	if (regid >= ARM_PC) {
159		return;
160	}
161	cpu->gprs[regid] = value;
162}
163
164static void _setBreakpointARM(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
165	if (!dv || dv->type != CLIDV_INT_TYPE) {
166		printf("%s\n", ERROR_MISSING_ARGS);
167		return;
168	}
169	uint32_t address = dv->intValue;
170	ARMDebuggerSetSoftwareBreakpoint(&debugger->d, address, MODE_ARM);
171}
172
173static void _setBreakpointThumb(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
174	if (!dv || dv->type != CLIDV_INT_TYPE) {
175		printf("%s\n", ERROR_MISSING_ARGS);
176		return;
177	}
178	uint32_t address = dv->intValue;
179	ARMDebuggerSetSoftwareBreakpoint(&debugger->d, address, MODE_THUMB);
180}
181
182static uint32_t _lookupIdentifier(struct mDebugger* debugger, const char* name, struct CLIDebugVector* dv) {
183	struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
184	struct ARMCore* cpu = debugger->core->cpu;
185	if (strcmp(name, "sp") == 0) {
186		return cpu->gprs[ARM_SP];
187	}
188	if (strcmp(name, "lr") == 0) {
189		return cpu->gprs[ARM_LR];
190	}
191	if (strcmp(name, "pc") == 0) {
192		return cpu->gprs[ARM_PC];
193	}
194	if (strcmp(name, "cpsr") == 0) {
195		return cpu->cpsr.packed;
196	}
197	// TODO: test if mode has SPSR
198	if (strcmp(name, "spsr") == 0) {
199		return cpu->spsr.packed;
200	}
201	if (name[0] == 'r' && name[1] >= '0' && name[1] <= '9') {
202		int reg = atoi(&name[1]);
203		if (reg < 16) {
204			return cpu->gprs[reg];
205		}
206	}
207	if (cliDebugger->system) {
208		uint32_t value = cliDebugger->system->lookupIdentifier(cliDebugger->system, name, dv);
209		if (dv->type != CLIDV_ERROR_TYPE) {
210			return value;
211		}
212	} else {
213		dv->type = CLIDV_ERROR_TYPE;
214	}
215	return 0;
216}
217
218void ARMCLIDebuggerCreate(struct CLIDebuggerSystem* debugger) {
219	debugger->printStatus = _printStatus;
220	debugger->disassemble = _disassemble;
221	debugger->platformName = "ARM";
222	debugger->platformCommands = _armCommands;
223}
224
225#endif