-
Notifications
You must be signed in to change notification settings - Fork 13
/
hardwarebp.cpp
86 lines (67 loc) · 2.03 KB
/
hardwarebp.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
// Copyright (c) 2000 Mike Morearty <mike@morearty.com>
// Original source and docs: http://www.morearty.com/code/breakpoint
#include <windows.h>
#include <assert.h>
#include "hardwarebp.h"
#ifdef _DEBUG
void HardwareBreakpoint::Set(void* address, int len, Condition when)
{
// make sure this breakpoint isn't already set
assert(m_index == -1);
CONTEXT cxt;
HANDLE thisThread = GetCurrentThread();
switch (len)
{
case 1: len = 0; break;
case 2: len = 1; break;
case 4: len = 3; break;
default: assert(false); // invalid length
}
// The only registers we care about are the debug registers
cxt.ContextFlags = CONTEXT_DEBUG_REGISTERS;
// Read the register values
if (!GetThreadContext(thisThread, &cxt))
assert(false);
// Find an available hardware register
for (m_index = 0; m_index < 4; ++m_index)
{
if ((cxt.Dr7 & (1 << (m_index*2))) == 0)
break;
}
assert(m_index < 4); // All hardware breakpoint registers are already being used
switch (m_index)
{
case 0: cxt.Dr0 = (DWORD) address; break;
case 1: cxt.Dr1 = (DWORD) address; break;
case 2: cxt.Dr2 = (DWORD) address; break;
case 3: cxt.Dr3 = (DWORD) address; break;
default: assert(false); // m_index has bogus value
}
SetBits(cxt.Dr7, 16 + (m_index*4), 2, when);
SetBits(cxt.Dr7, 18 + (m_index*4), 2, len);
SetBits(cxt.Dr7, m_index*2, 1, 1);
// Write out the new debug registers
if (!SetThreadContext(thisThread, &cxt))
assert(false);
}
void HardwareBreakpoint::Clear()
{
if (m_index != -1)
{
CONTEXT cxt;
HANDLE thisThread = GetCurrentThread();
// The only registers we care about are the debug registers
cxt.ContextFlags = CONTEXT_DEBUG_REGISTERS;
// Read the register values
if (!GetThreadContext(thisThread, &cxt))
assert(false);
// Zero out the debug register settings for this breakpoint
assert(m_index >= 0 && m_index < 4); // m_index has bogus value
SetBits(cxt.Dr7, m_index*2, 1, 0);
// Write out the new debug registers
if (!SetThreadContext(thisThread, &cxt))
assert(false);
m_index = -1;
}
}
#endif // _DEBUG