Fix setting of watchpoints on inferior thread creation for Linux.


git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@183139 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/Process/POSIX/ProcessPOSIX.cpp b/source/Plugins/Process/POSIX/ProcessPOSIX.cpp
index 1e0fa05..32dfdd7 100644
--- a/source/Plugins/Process/POSIX/ProcessPOSIX.cpp
+++ b/source/Plugins/Process/POSIX/ProcessPOSIX.cpp
@@ -654,31 +654,44 @@
             return error;
         }
 
-        bool wp_enabled = true;
-        uint32_t thread_count = m_thread_list.GetSize(false);
-        for (uint32_t i = 0; i < thread_count; ++i)
+        // Try to find a vacant watchpoint slot in the inferiors' main thread
+        uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
+        POSIXThread *thread = static_cast<POSIXThread*>(
+                               m_thread_list.GetThreadAtIndex(0, false).get());
+
+        if (thread)
+            wp_hw_index = thread->FindVacantWatchpointIndex();
+
+        if (wp_hw_index == LLDB_INVALID_INDEX32)
         {
-            POSIXThread *thread = static_cast<POSIXThread*>(
-                                  m_thread_list.GetThreadAtIndex(i, false).get());
-            if (thread)
-                wp_enabled &= thread->EnableHardwareWatchpoint(wp);
-            else
-            {
-                wp_enabled = false;
-                break;
-            }
-        }
-        if (wp_enabled)
-        {
-            wp->SetEnabled(true, notify);
-            return error;
+            error.SetErrorString("Setting hardware watchpoint failed.");
         }
         else
         {
-            // Watchpoint enabling failed on at least one
-            // of the threads so roll back all of them
-            DisableWatchpoint(wp, false);
-            error.SetErrorString("Setting hardware watchpoint failed");
+            wp->SetHardwareIndex(wp_hw_index);
+            bool wp_enabled = true;
+            uint32_t thread_count = m_thread_list.GetSize(false);
+            for (uint32_t i = 0; i < thread_count; ++i)
+            {
+                thread = static_cast<POSIXThread*>(
+                         m_thread_list.GetThreadAtIndex(i, false).get());
+                if (thread)
+                    wp_enabled &= thread->EnableHardwareWatchpoint(wp);
+                else
+                    wp_enabled = false;
+            }
+            if (wp_enabled)
+            {
+                wp->SetEnabled(true, notify);
+                return error;
+            }
+            else
+            {
+                // Watchpoint enabling failed on at least one
+                // of the threads so roll back all of them
+                DisableWatchpoint(wp, false);
+                error.SetErrorString("Setting hardware watchpoint failed");
+            }
         }
     }
     else
@@ -724,6 +737,7 @@
             }
             if (wp_disabled)
             {
+                wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
                 wp->SetEnabled(false, notify);
                 return error;
             }