|
1 |
| -// Copyright (c) 2000 Mike Morearty <mike@morearty.com> |
2 |
| -// Original source and docs: http://www.morearty.com/code/breakpoint |
3 |
| - |
4 |
| -#include <windows.h> |
5 |
| -#include <assert.h> |
6 |
| -#include "breakpoint.h" |
7 |
| - |
8 |
| -#ifdef _DEBUG |
9 |
| - |
10 |
| -void CBreakpoint::Set(void* address, int len, Condition when) |
11 |
| -{ |
12 |
| - // make sure this breakpoint isn't already set |
13 |
| - assert(m_index == -1); |
14 |
| - |
15 |
| - CONTEXT cxt; |
16 |
| - HANDLE thisThread = GetCurrentThread(); |
17 |
| - |
18 |
| - switch (len) |
19 |
| - { |
20 |
| - case 1: len = 0; break; |
21 |
| - case 2: len = 1; break; |
22 |
| - case 4: len = 3; break; |
23 |
| - default: assert(false); // invalid length |
24 |
| - } |
25 |
| - |
26 |
| - // The only registers we care about are the debug registers |
27 |
| - cxt.ContextFlags = CONTEXT_DEBUG_REGISTERS; |
28 |
| - |
29 |
| - // Read the register values |
30 |
| - if (!GetThreadContext(thisThread, &cxt)) |
31 |
| - assert(false); |
32 |
| - |
33 |
| - // Find an available hardware register |
34 |
| - for (m_index = 0; m_index < 4; ++m_index) |
35 |
| - { |
36 |
| - if ((cxt.Dr7 & (1 << (m_index*2))) == 0) |
37 |
| - break; |
38 |
| - } |
39 |
| - assert(m_index < 4); // All hardware breakpoint registers are already being used |
40 |
| - |
41 |
| - switch (m_index) |
42 |
| - { |
43 |
| - case 0: cxt.Dr0 = (DWORD) address; break; |
44 |
| - case 1: cxt.Dr1 = (DWORD) address; break; |
45 |
| - case 2: cxt.Dr2 = (DWORD) address; break; |
46 |
| - case 3: cxt.Dr3 = (DWORD) address; break; |
47 |
| - default: assert(false); // m_index has bogus value |
48 |
| - } |
49 |
| - |
50 |
| - SetBits(cxt.Dr7, 16 + (m_index*4), 2, when); |
51 |
| - SetBits(cxt.Dr7, 18 + (m_index*4), 2, len); |
52 |
| - SetBits(cxt.Dr7, m_index*2, 1, 1); |
53 |
| - |
54 |
| - // Write out the new debug registers |
55 |
| - if (!SetThreadContext(thisThread, &cxt)) |
56 |
| - assert(false); |
57 |
| -} |
58 |
| - |
59 |
| - |
60 |
| -void CBreakpoint::Clear() |
61 |
| -{ |
62 |
| - if (m_index != -1) |
63 |
| - { |
64 |
| - CONTEXT cxt; |
65 |
| - HANDLE thisThread = GetCurrentThread(); |
66 |
| - |
67 |
| - // The only registers we care about are the debug registers |
68 |
| - cxt.ContextFlags = CONTEXT_DEBUG_REGISTERS; |
69 |
| - |
70 |
| - // Read the register values |
71 |
| - if (!GetThreadContext(thisThread, &cxt)) |
72 |
| - assert(false); |
73 |
| - |
74 |
| - // Zero out the debug register settings for this breakpoint |
75 |
| - assert(m_index >= 0 && m_index < 4); // m_index has bogus value |
76 |
| - SetBits(cxt.Dr7, m_index*2, 1, 0); |
77 |
| - |
78 |
| - // Write out the new debug registers |
79 |
| - if (!SetThreadContext(thisThread, &cxt)) |
80 |
| - assert(false); |
81 |
| - |
82 |
| - m_index = -1; |
83 |
| - } |
84 |
| -} |
85 |
| - |
86 |
| -#endif // _DEBUG |
| 1 | +// Copyright (c) 2000 Mike Morearty <mike@morearty.com> |
| 2 | +// Original source and docs: http://www.morearty.com/code/breakpoint |
| 3 | + |
| 4 | +#include <windows.h> |
| 5 | +#include <assert.h> |
| 6 | +#include "hardwarebp.h" |
| 7 | + |
| 8 | +#ifdef _DEBUG |
| 9 | + |
| 10 | +void HardwareBreakpoint::Set(void* address, int len, Condition when) |
| 11 | +{ |
| 12 | + // make sure this breakpoint isn't already set |
| 13 | + assert(m_index == -1); |
| 14 | + |
| 15 | + CONTEXT cxt; |
| 16 | + HANDLE thisThread = GetCurrentThread(); |
| 17 | + |
| 18 | + switch (len) |
| 19 | + { |
| 20 | + case 1: len = 0; break; |
| 21 | + case 2: len = 1; break; |
| 22 | + case 4: len = 3; break; |
| 23 | + default: assert(false); // invalid length |
| 24 | + } |
| 25 | + |
| 26 | + // The only registers we care about are the debug registers |
| 27 | + cxt.ContextFlags = CONTEXT_DEBUG_REGISTERS; |
| 28 | + |
| 29 | + // Read the register values |
| 30 | + if (!GetThreadContext(thisThread, &cxt)) |
| 31 | + assert(false); |
| 32 | + |
| 33 | + // Find an available hardware register |
| 34 | + for (m_index = 0; m_index < 4; ++m_index) |
| 35 | + { |
| 36 | + if ((cxt.Dr7 & (1 << (m_index*2))) == 0) |
| 37 | + break; |
| 38 | + } |
| 39 | + assert(m_index < 4); // All hardware breakpoint registers are already being used |
| 40 | + |
| 41 | + switch (m_index) |
| 42 | + { |
| 43 | + case 0: cxt.Dr0 = (DWORD) address; break; |
| 44 | + case 1: cxt.Dr1 = (DWORD) address; break; |
| 45 | + case 2: cxt.Dr2 = (DWORD) address; break; |
| 46 | + case 3: cxt.Dr3 = (DWORD) address; break; |
| 47 | + default: assert(false); // m_index has bogus value |
| 48 | + } |
| 49 | + |
| 50 | + SetBits(cxt.Dr7, 16 + (m_index*4), 2, when); |
| 51 | + SetBits(cxt.Dr7, 18 + (m_index*4), 2, len); |
| 52 | + SetBits(cxt.Dr7, m_index*2, 1, 1); |
| 53 | + |
| 54 | + // Write out the new debug registers |
| 55 | + if (!SetThreadContext(thisThread, &cxt)) |
| 56 | + assert(false); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +void HardwareBreakpoint::Clear() |
| 61 | +{ |
| 62 | + if (m_index != -1) |
| 63 | + { |
| 64 | + CONTEXT cxt; |
| 65 | + HANDLE thisThread = GetCurrentThread(); |
| 66 | + |
| 67 | + // The only registers we care about are the debug registers |
| 68 | + cxt.ContextFlags = CONTEXT_DEBUG_REGISTERS; |
| 69 | + |
| 70 | + // Read the register values |
| 71 | + if (!GetThreadContext(thisThread, &cxt)) |
| 72 | + assert(false); |
| 73 | + |
| 74 | + // Zero out the debug register settings for this breakpoint |
| 75 | + assert(m_index >= 0 && m_index < 4); // m_index has bogus value |
| 76 | + SetBits(cxt.Dr7, m_index*2, 1, 0); |
| 77 | + |
| 78 | + // Write out the new debug registers |
| 79 | + if (!SetThreadContext(thisThread, &cxt)) |
| 80 | + assert(false); |
| 81 | + |
| 82 | + m_index = -1; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#endif // _DEBUG |
0 commit comments