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