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