src/debugger/memory-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 "memory-debugger.h"
7
8#include "debugger.h"
9
10#include "util/math.h"
11
12#include <string.h>
13
14static bool _checkWatchpoints(struct ARMDebugger* debugger, uint32_t address, struct DebuggerEntryInfo* info, enum WatchpointType type, uint32_t newValue, int width);
15
16#define FIND_DEBUGGER(DEBUGGER, CPU) \
17 { \
18 DEBUGGER = 0; \
19 size_t i; \
20 for (i = 0; i < CPU->numComponents; ++i) { \
21 if (CPU->components[i]->id == ARM_DEBUGGER_ID) { \
22 DEBUGGER = (struct ARMDebugger*) cpu->components[i]; \
23 break; \
24 } \
25 } \
26 }
27
28#define CREATE_SHIM(NAME, RETURN, TYPES, ...) \
29 static RETURN ARMDebuggerShim_ ## NAME TYPES { \
30 struct ARMDebugger* debugger; \
31 FIND_DEBUGGER(debugger, cpu); \
32 return debugger->originalMemory.NAME(cpu, __VA_ARGS__); \
33 }
34
35#define CREATE_WATCHPOINT_READ_SHIM(NAME, WIDTH, RETURN, TYPES, ...) \
36 static RETURN ARMDebuggerShim_ ## NAME TYPES { \
37 struct ARMDebugger* debugger; \
38 FIND_DEBUGGER(debugger, cpu); \
39 struct DebuggerEntryInfo info; \
40 if (_checkWatchpoints(debugger, address, &info, WATCHPOINT_READ, 0, WIDTH)) { \
41 ARMDebuggerEnter(debugger, DEBUGGER_ENTER_WATCHPOINT, &info); \
42 } \
43 return debugger->originalMemory.NAME(cpu, __VA_ARGS__); \
44 }
45
46#define CREATE_WATCHPOINT_WRITE_SHIM(NAME, WIDTH, RETURN, TYPES, ...) \
47 static RETURN ARMDebuggerShim_ ## NAME TYPES { \
48 struct ARMDebugger* debugger; \
49 FIND_DEBUGGER(debugger, cpu); \
50 struct DebuggerEntryInfo info; \
51 if (_checkWatchpoints(debugger, address, &info, WATCHPOINT_WRITE, value, WIDTH)) { \
52 ARMDebuggerEnter(debugger, DEBUGGER_ENTER_WATCHPOINT, &info); \
53 } \
54 return debugger->originalMemory.NAME(cpu, __VA_ARGS__); \
55 }
56
57#define CREATE_MULTIPLE_WATCHPOINT_SHIM(NAME, ACCESS_TYPE) \
58 static uint32_t ARMDebuggerShim_ ## NAME (struct ARMCore* cpu, uint32_t address, int mask, enum LSMDirection direction, int* cycleCounter) { \
59 struct ARMDebugger* debugger; \
60 FIND_DEBUGGER(debugger, cpu); \
61 uint32_t popcount = popcount32(mask); \
62 int offset = 4; \
63 int base = address; \
64 if (direction & LSM_D) { \
65 offset = -4; \
66 base -= (popcount << 2) - 4; \
67 } \
68 if (direction & LSM_B) { \
69 base += offset; \
70 } \
71 unsigned i; \
72 for (i = 0; i < popcount; ++i) { \
73 struct DebuggerEntryInfo info; \
74 if (_checkWatchpoints(debugger, base + 4 * i, &info, ACCESS_TYPE, 0, 4)) { \
75 ARMDebuggerEnter(debugger, DEBUGGER_ENTER_WATCHPOINT, &info); \
76 } \
77 } \
78 return debugger->originalMemory.NAME(cpu, address, mask, direction, cycleCounter); \
79 }
80
81CREATE_WATCHPOINT_READ_SHIM(load32, 4, uint32_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter)
82CREATE_WATCHPOINT_READ_SHIM(load16, 2, uint32_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter)
83CREATE_WATCHPOINT_READ_SHIM(load8, 1, uint32_t, (struct ARMCore* cpu, uint32_t address, int* cycleCounter), address, cycleCounter)
84CREATE_WATCHPOINT_WRITE_SHIM(store32, 4, void, (struct ARMCore* cpu, uint32_t address, int32_t value, int* cycleCounter), address, value, cycleCounter)
85CREATE_WATCHPOINT_WRITE_SHIM(store16, 2, void, (struct ARMCore* cpu, uint32_t address, int16_t value, int* cycleCounter), address, value, cycleCounter)
86CREATE_WATCHPOINT_WRITE_SHIM(store8, 1, void, (struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter), address, value, cycleCounter)
87CREATE_MULTIPLE_WATCHPOINT_SHIM(loadMultiple, WATCHPOINT_READ)
88CREATE_MULTIPLE_WATCHPOINT_SHIM(storeMultiple, WATCHPOINT_WRITE)
89CREATE_SHIM(setActiveRegion, void, (struct ARMCore* cpu, uint32_t address), address)
90
91static bool _checkWatchpoints(struct ARMDebugger* debugger, uint32_t address, struct DebuggerEntryInfo* info, enum WatchpointType type, uint32_t newValue, int width) {
92 --width;
93 struct DebugWatchpoint* watchpoint;
94 size_t i;
95 for (i = 0; i < DebugWatchpointListSize(&debugger->watchpoints); ++i) {
96 watchpoint = DebugWatchpointListGetPointer(&debugger->watchpoints, i);
97 if (!((watchpoint->address ^ address) & ~width) && watchpoint->type & type) {
98 switch (width + 1) {
99 case 1:
100 info->oldValue = debugger->originalMemory.load8(debugger->cpu, address, 0);
101 break;
102 case 2:
103 info->oldValue = debugger->originalMemory.load16(debugger->cpu, address, 0);
104 break;
105 case 4:
106 info->oldValue = debugger->originalMemory.load32(debugger->cpu, address, 0);
107 break;
108 }
109 info->newValue = newValue;
110 info->address = address;
111 info->watchType = watchpoint->type;
112 info->accessType = type;
113 return true;
114 }
115 }
116 return false;
117}
118
119void ARMDebuggerInstallMemoryShim(struct ARMDebugger* debugger) {
120 debugger->originalMemory = debugger->cpu->memory;
121 debugger->cpu->memory.store32 = ARMDebuggerShim_store32;
122 debugger->cpu->memory.store16 = ARMDebuggerShim_store16;
123 debugger->cpu->memory.store8 = ARMDebuggerShim_store8;
124 debugger->cpu->memory.load32 = ARMDebuggerShim_load32;
125 debugger->cpu->memory.load16 = ARMDebuggerShim_load16;
126 debugger->cpu->memory.load8 = ARMDebuggerShim_load8;
127 debugger->cpu->memory.storeMultiple = ARMDebuggerShim_storeMultiple;
128 debugger->cpu->memory.loadMultiple = ARMDebuggerShim_loadMultiple;
129 debugger->cpu->memory.setActiveRegion = ARMDebuggerShim_setActiveRegion;
130}
131
132void ARMDebuggerRemoveMemoryShim(struct ARMDebugger* debugger) {
133 debugger->cpu->memory.store32 = debugger->originalMemory.store32;
134 debugger->cpu->memory.store16 = debugger->originalMemory.store16;
135 debugger->cpu->memory.store8 = debugger->originalMemory.store8;
136 debugger->cpu->memory.load32 = debugger->originalMemory.load32;
137 debugger->cpu->memory.load16 = debugger->originalMemory.load16;
138 debugger->cpu->memory.load8 = debugger->originalMemory.load8;
139 debugger->cpu->memory.storeMultiple = debugger->originalMemory.storeMultiple;
140 debugger->cpu->memory.loadMultiple = debugger->originalMemory.loadMultiple;
141 debugger->cpu->memory.setActiveRegion = debugger->originalMemory.setActiveRegion;
142}