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 if (debugger->state == DEBUGGER_EXITING) {
59 debugger->state = DEBUGGER_RUNNING;
60 }
61 while (debugger->state < DEBUGGER_EXITING) {
62 if (!debugger->breakpoints) {
63 while (debugger->state == DEBUGGER_RUNNING) {
64 ARMRunLoop(debugger->cpu);
65 }
66 } else {
67 while (debugger->state == DEBUGGER_RUNNING) {
68 ARMRun(debugger->cpu);
69 _checkBreakpoints(debugger);
70 }
71 }
72 switch (debugger->state) {
73 case DEBUGGER_RUNNING:
74 break;
75 case DEBUGGER_CUSTOM:
76 while (debugger->state == DEBUGGER_CUSTOM) {
77 ARMRun(debugger->cpu);
78 _checkBreakpoints(debugger);
79 debugger->custom(debugger);
80 }
81 break;
82 case DEBUGGER_PAUSED:
83 if (debugger->paused) {
84 debugger->paused(debugger);
85 } else {
86 debugger->state = DEBUGGER_RUNNING;
87 }
88 break;
89 case DEBUGGER_EXITING:
90 case DEBUGGER_SHUTDOWN:
91 return;
92 }
93 }
94}
95
96void ARMDebuggerEnter(struct ARMDebugger* debugger, enum DebuggerEntryReason reason) {
97 debugger->state = DEBUGGER_PAUSED;
98 if (debugger->entered) {
99 debugger->entered(debugger, reason);
100 }
101}
102
103void ARMDebuggerSetBreakpoint(struct ARMDebugger* debugger, uint32_t address) {
104 struct DebugBreakpoint* breakpoint = malloc(sizeof(struct DebugBreakpoint));
105 breakpoint->address = address;
106 breakpoint->next = debugger->breakpoints;
107 debugger->breakpoints = breakpoint;
108}
109
110void ARMDebuggerClearBreakpoint(struct ARMDebugger* debugger, uint32_t address) {
111 struct DebugBreakpoint** previous = &debugger->breakpoints;
112 struct DebugBreakpoint* breakpoint;
113 for (; (breakpoint = *previous); previous = &breakpoint->next) {
114 if (breakpoint->address == address) {
115 *previous = breakpoint->next;
116 free(breakpoint);
117 }
118 }
119}
120
121void ARMDebuggerSetWatchpoint(struct ARMDebugger* debugger, uint32_t address) {
122 if (!debugger->watchpoints) {
123 ARMDebuggerInstallMemoryShim(debugger);
124 }
125 struct DebugBreakpoint* watchpoint = malloc(sizeof(struct DebugBreakpoint));
126 watchpoint->address = address;
127 watchpoint->next = debugger->watchpoints;
128 debugger->watchpoints = watchpoint;
129}
130
131void ARMDebuggerClearWatchpoint(struct ARMDebugger* debugger, uint32_t address) {
132 struct DebugBreakpoint** previous = &debugger->watchpoints;
133 struct DebugBreakpoint* breakpoint;
134 for (; (breakpoint = *previous); previous = &breakpoint->next) {
135 if (breakpoint->address == address) {
136 *previous = breakpoint->next;
137 free(breakpoint);
138 }
139 }
140 if (!debugger->watchpoints) {
141 ARMDebuggerRemoveMemoryShim(debugger);
142 }
143}