diff --git a/breakpoint.cpp b/hardwarebp.cpp similarity index 90% rename from breakpoint.cpp rename to hardwarebp.cpp index 9b0e98e..f6f725c 100644 --- a/breakpoint.cpp +++ b/hardwarebp.cpp @@ -1,86 +1,86 @@ -// Copyright (c) 2000 Mike Morearty -// Original source and docs: http://www.morearty.com/code/breakpoint - -#include -#include -#include "breakpoint.h" - -#ifdef _DEBUG - -void CBreakpoint::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 CBreakpoint::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 +// Copyright (c) 2000 Mike Morearty +// Original source and docs: http://www.morearty.com/code/breakpoint + +#include +#include +#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 diff --git a/breakpoint.h b/hardwarebp.h similarity index 79% rename from breakpoint.h rename to hardwarebp.h index 74819e8..2189bf0 100644 --- a/breakpoint.h +++ b/hardwarebp.h @@ -1,36 +1,36 @@ -// Copyright (c) 2000 Mike Morearty -// Original source and docs: http://www.morearty.com/code/breakpoint - -#ifndef _BREAKPOINT_H_ -#define _BREAKPOINT_H_ - -#ifdef _DEBUG - -class CBreakpoint -{ -public: - CBreakpoint() { m_index = -1; } - ~CBreakpoint() { Clear(); } - - // The enum values correspond to the values used by the Intel Pentium, - // so don't change them! - enum Condition { Write = 1, Read /* or write! */ = 3 }; - - void Set(void* address, int len /* 1, 2, or 4 */, Condition when); - void Clear(); - -protected: - - inline void SetBits(unsigned long& dw, int lowBit, int bits, int newValue) - { - int mask = (1 << bits) - 1; // e.g. 1 becomes 0001, 2 becomes 0011, 3 becomes 0111 - - dw = (dw & ~(mask << lowBit)) | (newValue << lowBit); - } - - int m_index; // -1 means not set; 0-3 means we've set that hardware bp -}; - -#endif // _DEBUG - -#endif // _BREAKPOINT_H_ +// Copyright (c) 2000 Mike Morearty +// Original source and docs: http://www.morearty.com/code/breakpoint + +#ifndef _HARDWAREBP_H_ +#define _HARDWAREBP_H_ + +#ifdef _DEBUG + +class HardwareBreakpoint +{ +public: + HardwareBreakpoint() { m_index = -1; } + ~HardwareBreakpoint() { Clear(); } + + // The enum values correspond to the values used by the Intel Pentium, + // so don't change them! + enum Condition { Write = 1, Read /* or write! */ = 3 }; + + void Set(void* address, int len /* 1, 2, or 4 */, Condition when); + void Clear(); + +protected: + + inline void SetBits(unsigned long& dw, int lowBit, int bits, int newValue) + { + int mask = (1 << bits) - 1; // e.g. 1 becomes 0001, 2 becomes 0011, 3 becomes 0111 + + dw = (dw & ~(mask << lowBit)) | (newValue << lowBit); + } + + int m_index; // -1 means not set; 0-3 means we've set that hardware bp +}; + +#endif // _DEBUG + +#endif // _HARDWAREBP_H_