src/debugger/memory-debugger.c (view raw)
1#include "memory-debugger.h"
2
3#include "debugger.h"
4
5static void ARMDebuggerShim_store32(struct ARMMemory*, uint32_t address, int32_t value, int* cycleCounter);
6static void ARMDebuggerShim_store16(struct ARMMemory*, uint32_t address, int16_t value, int* cycleCounter);
7static void ARMDebuggerShim_store8(struct ARMMemory*, uint32_t address, int8_t value, int* cycleCounter);
8static void ARMDebuggerShim_setActiveRegion(struct ARMMemory* memory, uint32_t address);
9
10#define CREATE_SHIM(NAME, RETURN, TYPES, ARGS...) \
11 static RETURN ARMDebuggerShim_ ## NAME TYPES { \
12 struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory; \
13 return debugMemory->original->NAME(debugMemory->original, ARGS); \
14 }
15
16CREATE_SHIM(load32, int32_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter)
17CREATE_SHIM(load16, int16_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter)
18CREATE_SHIM(loadU16, uint16_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter)
19CREATE_SHIM(load8, int8_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter)
20CREATE_SHIM(loadU8, uint8_t, (struct ARMMemory* memory, uint32_t address, int* cycleCounter), address, cycleCounter)
21CREATE_SHIM(waitMultiple, int, (struct ARMMemory* memory, uint32_t startAddress, int count), startAddress, count)
22
23static int _checkWatchpoints(struct DebugBreakpoint* watchpoints, uint32_t address, int width) {
24 width -= 1;
25 for (; watchpoints; watchpoints = watchpoints->next) {
26 if (!((watchpoints->address ^ address) & ~width)) {
27 return 1;
28 }
29 }
30 return 0;
31}
32
33void ARMDebuggerInstallMemoryShim(struct ARMDebugger* debugger) {
34 debugger->memoryShim.original = debugger->cpu->memory;
35 memcpy(&debugger->memoryShim.d, debugger->cpu->memory, sizeof(struct ARMMemory));
36 debugger->memoryShim.d.store32 = ARMDebuggerShim_store32;
37 debugger->memoryShim.d.store16 = ARMDebuggerShim_store16;
38 debugger->memoryShim.d.store8 = ARMDebuggerShim_store8;
39 debugger->memoryShim.d.load32 = ARMDebuggerShim_load32;
40 debugger->memoryShim.d.load16 = ARMDebuggerShim_load16;
41 debugger->memoryShim.d.loadU16 = ARMDebuggerShim_loadU16;
42 debugger->memoryShim.d.load8 = ARMDebuggerShim_load8;
43 debugger->memoryShim.d.loadU8 = ARMDebuggerShim_loadU8;
44 debugger->memoryShim.d.setActiveRegion = ARMDebuggerShim_setActiveRegion;
45 debugger->memoryShim.d.waitMultiple = ARMDebuggerShim_waitMultiple;
46 debugger->cpu->memory = &debugger->memoryShim.d;
47}
48
49void ARMDebuggerShim_store32(struct ARMMemory* memory, uint32_t address, int32_t value, int* cycleCounter) {
50 struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory;
51 if (_checkWatchpoints(debugMemory->watchpoints, address, 4)) {
52 ARMDebuggerEnter(debugMemory->p, DEBUGGER_ENTER_WATCHPOINT);
53 }
54 debugMemory->original->store32(debugMemory->original, address, value, cycleCounter);
55}
56
57void ARMDebuggerShim_store16(struct ARMMemory* memory, uint32_t address, int16_t value, int* cycleCounter) {
58 struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory;
59 if (_checkWatchpoints(debugMemory->watchpoints, address, 2)) {
60 ARMDebuggerEnter(debugMemory->p, DEBUGGER_ENTER_WATCHPOINT);
61 }
62 debugMemory->original->store16(debugMemory->original, address, value, cycleCounter);
63}
64
65void ARMDebuggerShim_store8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cycleCounter) {
66 struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory;
67 if (_checkWatchpoints(debugMemory->watchpoints, address, 1)) {
68 ARMDebuggerEnter(debugMemory->p, DEBUGGER_ENTER_WATCHPOINT);
69 }
70 debugMemory->original->store8(debugMemory->original, address, value, cycleCounter);
71}
72
73void ARMDebuggerShim_setActiveRegion(struct ARMMemory* memory, uint32_t address) {
74 struct DebugMemoryShim* debugMemory = (struct DebugMemoryShim*) memory;
75 debugMemory->original->setActiveRegion(debugMemory->original, address);
76 memory->activeRegion = debugMemory->original->activeRegion;
77 memory->activeMask = debugMemory->original->activeMask;
78 memory->activePrefetchCycles32 = debugMemory->original->activePrefetchCycles32;
79 memory->activePrefetchCycles16 = debugMemory->original->activePrefetchCycles16;
80 memory->activeNonseqCycles32 = debugMemory->original->activeNonseqCycles32;
81 memory->activeNonseqCycles16 = debugMemory->original->activeNonseqCycles16;
82}