Add ability to run code indefinitely (or at least until we crash)

This commit is contained in:
Jeffrey Pfau 2013-04-13 13:50:41 -07:00
parent dbe9796b34
commit e5379c99e0
5 changed files with 49 additions and 9 deletions

View File

@ -28,6 +28,7 @@ static const char* ERROR_MISSING_ARGS = "Arguments missing";
typedef void (DebuggerComamnd)(struct ARMDebugger*, struct DebugVector*);
static void _breakInto(struct ARMDebugger*, struct DebugVector*);
static void _continue(struct ARMDebugger*, struct DebugVector*);
static void _print(struct ARMDebugger*, struct DebugVector*);
static void _printHex(struct ARMDebugger*, struct DebugVector*);
static void _printStatus(struct ARMDebugger*, struct DebugVector*);
@ -40,6 +41,8 @@ struct {
const char* name;
DebuggerComamnd* command;
} debuggerCommands[] = {
{ "c", _continue },
{ "continue", _continue },
{ "i", _printStatus },
{ "info", _printStatus },
{ "p", _print },
@ -80,6 +83,11 @@ static void _breakInto(struct ARMDebugger* debugger, struct DebugVector* dv) {
signal(SIGTRAP, oldSignal);
}
static void _continue(struct ARMDebugger* debugger, struct DebugVector* dv) {
(void)(dv);
debugger->state = DEBUGGER_RUNNING;
}
static void _print(struct ARMDebugger* debugger, struct DebugVector* dv) {
(void)(debugger);
for ( ; dv; dv = dv->next) {
@ -441,19 +449,37 @@ static void _parse(struct ARMDebugger* debugger, const char* line) {
void ARMDebuggerInit(struct ARMDebugger* debugger, struct ARMCore* cpu) {
debugger->cpu = cpu;
debugger->state = DEBUGGER_PAUSED;
}
void ARMDebuggerRun(struct ARMDebugger* debugger) {
while (debugger->state != DEBUGGER_EXITING) {
while (debugger->state == DEBUGGER_RUNNING) {
ARMRun(debugger->cpu);
}
switch (debugger->state) {
case DEBUGGER_PAUSED:
ARMDebuggerEnter(debugger);
break;
case DEBUGGER_EXITING:
return;
default:
// Should never be reached
break;
}
}
}
void ARMDebuggerEnter(struct ARMDebugger* debugger) {
char* line;
_printStatus(debugger, 0);
while ((line = linenoise("> "))) {
while (debugger->state == DEBUGGER_PAUSED) {
line = linenoise("> ");
if (!line) {
debugger->state = DEBUGGER_EXITING;
return;
}
_parse(debugger, line);
free(line);
switch (debugger->state) {
case DEBUGGER_EXITING:
return;
default:
break;
}
}
}

View File

@ -13,6 +13,7 @@ struct ARMDebugger {
};
void ARMDebuggerInit(struct ARMDebugger*, struct ARMCore*);
void ARMDebuggerRun(struct ARMDebugger*);
void ARMDebuggerEnter(struct ARMDebugger*);
#endif

View File

@ -1,7 +1,10 @@
#include "gba.h"
#include "debugger.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
@ -76,6 +79,11 @@ void GBABoardReset(struct ARMBoard* board) {
cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
}
void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
ARMDebuggerInit(debugger, &gba->cpu);
gba->debugger = debugger;
}
void GBALoadROM(struct GBA* gba, int fd) {
gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
// TODO: error check
@ -382,4 +390,5 @@ void GBALog(int level, const char* format, ...) {
void GBAHitStub(struct ARMBoard* board, uint32_t opcode) {
GBALog(GBA_LOG_STUB, "Stub opcode: %08x", opcode);
abort();
}

View File

@ -92,6 +92,8 @@ struct GBA {
struct GBABoard board;
struct GBAMemory memory;
struct ARMDebugger* debugger;
enum GBAError errno;
const char* errstr;
};
@ -105,6 +107,8 @@ void GBAMemoryDeinit(struct GBAMemory* memory);
void GBABoardInit(struct GBABoard* board);
void GBABoardReset(struct ARMBoard* board);
void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
void GBALoadROM(struct GBA* gba, int fd);
int32_t GBALoad32(struct ARMMemory* memory, uint32_t address);

View File

@ -14,8 +14,8 @@ int main(int argc, char** argv) {
GBALoadROM(&gba, fd);
gba.cpu.gprs[ARM_PC] = 0x08000004;
gba.memory.d.setActiveRegion(&gba.memory.d, gba.cpu.gprs[ARM_PC]);
ARMDebuggerInit(&debugger, &gba.cpu);
ARMDebuggerEnter(&debugger);
GBAAttachDebugger(&gba, &debugger);
ARMDebuggerRun(&debugger);
GBADeinit(&gba);
close(fd);