Some fixes for thread stepping on Windows.

This hooks up the changes necessary to set the trap flag on the
CPU and properly manage the process and thread's resume state
and private state so that the ThreadPlan does its thing.

Stepping still doesn't work as of this change, because there are
some issues with stack frames where it doesn't update the thread's
frame list correctly when it breaks inside of a function, but
I will try to fix that separately.

llvm-svn: 226221
diff --git a/lldb/source/Plugins/Process/Windows/TargetThreadWindows.cpp b/lldb/source/Plugins/Process/Windows/TargetThreadWindows.cpp
index 2f5ddc9..b57f33f 100644
--- a/lldb/source/Plugins/Process/Windows/TargetThreadWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/TargetThreadWindows.cpp
@@ -38,12 +38,15 @@
 void
 TargetThreadWindows::RefreshStateAfterStop()
 {
+    DWORD old_suspend_count = ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
+
     GetRegisterContext()->InvalidateIfNeeded(false);
 }
 
 void
 TargetThreadWindows::WillResume(lldb::StateType resume_state)
 {
+    SetResumeState(resume_state);
 }
 
 void
@@ -118,28 +121,22 @@
     if (resume_state == current_state)
         return true;
 
-    bool success = false;
-    DWORD suspend_count = 0;
-    switch (resume_state)
+    if (resume_state == eStateStepping)
     {
-        case eStateRunning:
-            SetState(resume_state);
-            do
-            {
-                suspend_count = ::ResumeThread(m_host_thread.GetNativeThread().GetSystemHandle());
-            } while (suspend_count > 1 && suspend_count != (DWORD)-1);
-            success = (suspend_count != (DWORD)-1);
-            break;
-        case eStateStopped:
-        case eStateSuspended:
-            if (current_state != eStateStopped && current_state != eStateSuspended)
-            {
-                suspend_count = SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
-                success = (suspend_count != (DWORD)-1);
-            }
-            break;
-        default:
-            success = false;
+        uint32_t flags_index = GetRegisterContext()->ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
+        uint64_t flags_value = GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0);
+        flags_value |= 0x100; // Set the trap flag on the CPU
+        GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value);
     }
-    return success;
+
+    if (resume_state == eStateStepping || resume_state == eStateRunning)
+    {
+        DWORD previous_suspend_count = 0;
+        HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
+        do
+        {
+            previous_suspend_count = ::ResumeThread(thread_handle);
+        } while (previous_suspend_count > 0);
+    }
+    return true;
 }