title: Hardwarebreakpoint WINAPI
body: <p>I'm currently implementing the last task for a little debug framework, namely HW Breakpoints.
  I referred to this article so far: <a href="http://www.codeproject.com/KB/debug/hardwarebreakpoint.aspx">http://www.codeproject.com/KB/debug/hardwarebreakpoint.aspx</a> and a Book about writing a Debugger.</p>
  
  <p>I got following two functions so far for setting a HW breakpoint:</p>
  
  <pre><code>void debuglib::breakpoints::hw_bp() {
      HANDLE helper = 0;
  
      CONTEXT co;
      CURRENTCONTEXT(co);
  
      helper = ::CreateThread(0,0,threadfunc,reinterpret_cast&amp;lt;void*&gt;(co.Eip),0,0);
  
      DWORD status = ::WaitForSingleObject(helper,INFINITE);
  
  
      if (status != WAIT_OBJECT_0) { 
              ::MessageBoxA(0, "Helper thread didn't exit cleanly", "HWBreakpoint", MB_OK);
      }
  
      ::CloseHandle(helper);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>static DWORD WINAPI debuglib::breakpoints::threadfunc(void* param) {
  
      DWORD suspendcnt = ::SuspendThread(debuglib::process::thread());
      if(suspendcnt) {
          return 0;
      }
  
      CONTEXT co;
      ::ZeroMemory(&amp;co,sizeof(co));
      co.ContextFlags = CONTEXT_DEBUG_REGISTERS;
  
      BOOL ok = ::GetThreadContext(debuglib::process::thread(),&amp;co);
  
      if(!ok) {
          return 0;
      }
  
      DWORD freeDr = 0;
      DWORD condition = debuglib::breakpoints::TRIGGER::CODE;
      DWORD length = debuglib::breakpoints::SIZE::SIZE_1;
  
      co.Dr0 = reinterpret_cast&amp;lt;DWORD&gt;(param);
  
      co.Dr7 = co.Dr7 | 1 &amp;lt;&amp;lt; (freeDr*2);
      co.Dr7 = co.Dr7 | condition &amp;lt;&amp;lt; ((freeDr*4)+16);
      co.Dr7 = co.Dr7 | length &amp;lt;&amp;lt; ((freeDr*4)+18);
  
      co.ContextFlags = CONTEXT_DEBUG_REGISTERS;
      ok = ::SetThreadContext(debuglib::process::thread(), &amp;co);
  
      co.ContextFlags = CONTEXT_DEBUG_REGISTERS;
      ::GetThreadContext(debuglib::process::thread(),&amp;co);
  
      suspendcnt = ::ResumeThread(debuglib::process::thread());
      if(suspendcnt == 0xFFFFFFFF) {
          return 0;
      }
  
      return 1;
   }
  </code></pre>
  
  <p>So first I'm creating a helper thread since I´m debugging the current thread. Within the callback function of the helper thread, I´m suspending the main thread. Afterwards I read the current DR values of the main thread (at the moment this is not relevant, since I always use DR0, after this works, I will check which registers are free and use up to 4 BP´s). Afterwards I used the return address of the calling function (EIP) as address to break in DR0 and set the belonging flags in DR7.</p>
  
  <p>At the end I'm resuming the main thread and closing the handle of the helper thread, as it is finished.</p>
  
  <p>With this code I got following problem:</p>
  
  <p>If I execute the program in debug mode the programs halts at the right address, but I cannot do anything anymore, since the INT1 flag is set I guess and the VS debugger cannot single-step further?</p>
  
  <p>If I execute the program without debugging it simple crashes.
  I tried to use __try, __except like in the mentioned project (http://www.codeproject.com/KB/debug/hardwarebreakpoint.aspx) but this does not work either.</p>
  
  <p>I appreciate and help or information what I´m doing wrong and how to solve this problem.</p>