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
10#include "memory-debugger.h"
11
12const uint32_t ARM_DEBUGGER_ID = 0xDEADBEEF;
13
14static void _checkBreakpoints(struct ARMDebugger* debugger) {
15 struct DebugBreakpoint* breakpoint;
16 int instructionLength;
17 enum ExecutionMode mode = debugger->cpu->cpsr.t;
18 if (mode == MODE_ARM) {
19 instructionLength = WORD_SIZE_ARM;
20 } else {
21 instructionLength = WORD_SIZE_THUMB;
22 }
23 for (breakpoint = debugger->breakpoints; breakpoint; breakpoint = breakpoint->next) {
24 if (breakpoint->address + instructionLength == (uint32_t) debugger->cpu->gprs[ARM_PC]) {
25 ARMDebuggerEnter(debugger, DEBUGGER_ENTER_BREAKPOINT);
26 break;
27 }
28 }
29}
30
31static void ARMDebuggerInit(struct ARMCore*, struct ARMComponent*);
32static void ARMDebuggerDeinit(struct ARMComponent*);
33
34void ARMDebuggerCreate(struct ARMDebugger* debugger) {
35 debugger->d.id = ARM_DEBUGGER_ID;
36 debugger->d.init = ARMDebuggerInit;
37 debugger->d.deinit = ARMDebuggerDeinit;
38}
39
40void ARMDebuggerInit(struct ARMCore* cpu, struct ARMComponent* component) {
41 struct ARMDebugger* debugger = (struct ARMDebugger*) component;
42 debugger->cpu = cpu;
43 debugger->state = DEBUGGER_RUNNING;
44 debugger->breakpoints = 0;
45 debugger->originalMemory = cpu->memory;
46 debugger->watchpoints = 0;
47 if (debugger->init) {
48 debugger->init(debugger);
49 }
50}
51
52void ARMDebuggerDeinit(struct ARMComponent* component) {
53 struct ARMDebugger* debugger = (struct ARMDebugger*) component;
54 debugger->deinit(debugger);
55}
56
57void ARMDebuggerRun(struct ARMDebugger* debugger) {
58 switch (debugger->state) {
59 case DEBUGGER_RUNNING:
60 if (!debugger->breakpoints && !debugger->watchpoints) {
61 ARMRunLoop(debugger->cpu);
62 } else {
63 ARMRun(debugger->cpu);
64 _checkBreakpoints(debugger);
65 }
66 break;
67 case DEBUGGER_CUSTOM:
68 ARMRun(debugger->cpu);
69 _checkBreakpoints(debugger);
70 debugger->custom(debugger);
71 break;
72 case DEBUGGER_PAUSED:
73 if (debugger->paused) {
74 debugger->paused(debugger);
75 } else {
76 debugger->state = DEBUGGER_RUNNING;
77 }
78 break;
79 case DEBUGGER_SHUTDOWN:
80 return;
81 }
82}
83
84void ARMDebuggerEnter(struct ARMDebugger* debugger, enum DebuggerEntryReason reason) {
85 debugger->state = DEBUGGER_PAUSED;
86 if (debugger->entered) {
87 debugger->entered(debugger, reason);
88 }
89}
90
91void ARMDebuggerSetBreakpoint(struct ARMDebugger* debugger, uint32_t address) {
92 struct DebugBreakpoint* breakpoint = malloc(sizeof(struct DebugBreakpoint));
93 breakpoint->address = address;
94 breakpoint->next = debugger->breakpoints;
95 debugger->breakpoints = breakpoint;
96}
97
98void ARMDebuggerClearBreakpoint(struct ARMDebugger* debugger, uint32_t address) {
99 struct DebugBreakpoint** previous = &debugger->breakpoints;
100 struct DebugBreakpoint* breakpoint;
101 for (; (breakpoint = *previous); previous = &breakpoint->next) {
102 if (breakpoint->address == address) {
103 *previous = breakpoint->next;
104 free(breakpoint);
105 }
106 }
107}
108
109void ARMDebuggerSetWatchpoint(struct ARMDebugger* debugger, uint32_t address) {
110 if (!debugger->watchpoints) {
111 ARMDebuggerInstallMemoryShim(debugger);
112 }
113 struct DebugBreakpoint* watchpoint = malloc(sizeof(struct DebugBreakpoint));
114 watchpoint->address = address;
115 watchpoint->next = debugger->watchpoints;
116 debugger->watchpoints = watchpoint;
117}
118
119void ARMDebuggerClearWatchpoint(struct ARMDebugger* debugger, uint32_t address) {
120 struct DebugBreakpoint** previous = &debugger->watchpoints;
121 struct DebugBreakpoint* breakpoint;
122 for (; (breakpoint = *previous); previous = &breakpoint->next) {
123 if (breakpoint->address == address) {
124 *previous = breakpoint->next;
125 free(breakpoint);
126 }
127 }
128 if (!debugger->watchpoints) {
129 ARMDebuggerRemoveMemoryShim(debugger);
130 }
131}