-
Notifications
You must be signed in to change notification settings - Fork 11
/
Breakpoint.cpp
143 lines (115 loc) · 2.53 KB
/
Breakpoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Breakpoint.h"
// ----------------------------------------------------------------------------
Breakpoint::Breakpoint(DWORD tid, void* addr, UCHAR type)
{
m_hThread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT, FALSE, tid);
m_nIndex = -1;
this->set(addr, type);
this->enable();
}
// ----------------------------------------------------------------------------
Breakpoint::~Breakpoint()
{
this->disable();
CloseHandle(m_hThread);
}
// ----------------------------------------------------------------------------
bool Breakpoint::set(void* addr, UCHAR type)
{
m_pAddr = addr;
m_nType = type;
if (this->enabled())
{
// update existing breakpoint
return this->enable();
}
return true;
}
// ----------------------------------------------------------------------------
bool Breakpoint::enable()
{
BOOL ok = false;
SuspendThread(m_hThread);
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
if (GetThreadContext(m_hThread, &ctx))
{
ok = true;
auto index = -1;
if (this->enabled()) index = m_nIndex;
else if (!(ctx.Dr7 & 0x01)) index = 0;
else if (!(ctx.Dr7 & 0x04)) index = 1;
else if (!(ctx.Dr7 & 0x10)) index = 2;
else if (!(ctx.Dr7 & 0x40)) index = 3;
switch (index)
{
default:
ok = false;
break;
case 0:
ctx.Dr0 = (DWORD64)m_pAddr;
ctx.Dr7 &= 0xFFF0FFFC;
ctx.Dr7 |= (m_nType << 16) | 0x01;
break;
case 1:
ctx.Dr1 = (DWORD64)m_pAddr;
ctx.Dr7 &= 0xFF0FFFF3;
ctx.Dr7 |= (m_nType << 20) | 0x04;
break;
case 2:
ctx.Dr2 = (DWORD64)m_pAddr;
ctx.Dr7 &= 0xF0FFFFCF;
ctx.Dr7 |= (m_nType << 24) | 0x10;
break;
case 3:
ctx.Dr3 = (DWORD64)m_pAddr;
ctx.Dr7 &= 0x0FFFFF3F;
ctx.Dr7 |= (m_nType << 28) | 0x40;
break;
}
ctx.Dr6 = 0;
if (ok &= SetThreadContext(m_hThread, &ctx))
{
m_nIndex = index;
}
}
ResumeThread(m_hThread);
return ok;
}
// ----------------------------------------------------------------------------
bool Breakpoint::disable()
{
BOOL ok = false;
SuspendThread(m_hThread);
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
if (GetThreadContext(m_hThread, &ctx))
{
ok = true;
switch (m_nIndex)
{
default:
ok = false;
break;
case 0:
ctx.Dr7 &= 0xFFF0FFFC;
break;
case 1:
ctx.Dr7 &= 0xFF0FFFF3;
break;
case 2:
ctx.Dr7 &= 0xF0FFFFCF;
break;
case 3:
ctx.Dr7 &= 0x0FFFFF3F;
break;
}
ctx.Dr6 = 0;
if (ok &= SetThreadContext(m_hThread, &ctx))
{
m_nIndex = -1;
}
}
ResumeThread(m_hThread);
return ok;
}