Reinstating r181091 and r181106 with fix for Linux regressions.

llvm-svn: 181340
diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp b/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
index 8bb853c..ef88dd3 100644
--- a/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
+++ b/lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
@@ -44,6 +44,8 @@
 using namespace lldb;
 using namespace lldb_private;
 
+static const lldb::tid_t g_kernel_tid = 1;
+
 const char *
 ProcessKDP::GetPluginNameStatic()
 {
@@ -116,7 +118,8 @@
     m_async_thread (LLDB_INVALID_HOST_THREAD),
     m_dyld_plugin_name (),
     m_kernel_load_addr (LLDB_INVALID_ADDRESS),
-    m_command_sp()
+    m_command_sp(),
+    m_kernel_thread_wp()
 {
     m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit");
     m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue");
@@ -377,7 +380,8 @@
     bool resume = false;
     
     // With KDP there is only one thread we can tell what to do
-    ThreadSP kernel_thread_sp (GetKernelThread(m_thread_list, m_thread_list));
+    ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid));
+                            
     if (kernel_thread_sp)
     {
         const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState();
@@ -449,15 +453,17 @@
 }
 
 lldb::ThreadSP
-ProcessKDP::GetKernelThread(ThreadList &old_thread_list, ThreadList &new_thread_list)
+ProcessKDP::GetKernelThread()
 {
     // KDP only tells us about one thread/core. Any other threads will usually
     // be the ones that are read from memory by the OS plug-ins.
-    const lldb::tid_t kernel_tid = 1;
-    ThreadSP thread_sp (old_thread_list.FindThreadByID (kernel_tid, false));
+    
+    ThreadSP thread_sp (m_kernel_thread_wp.lock());
     if (!thread_sp)
-        thread_sp.reset(new ThreadKDP (*this, kernel_tid));
-    new_thread_list.AddThread(thread_sp);
+    {
+        thread_sp.reset(new ThreadKDP (*this, g_kernel_tid));
+        m_kernel_thread_wp = thread_sp;
+    }
     return thread_sp;
 }
 
@@ -474,7 +480,10 @@
     
     // Even though there is a CPU mask, it doesn't mean we can see each CPU
     // indivudually, there is really only one. Lets call this thread 1.
-    GetKernelThread (old_thread_list, new_thread_list);
+    ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
+    if (!thread_sp)
+        thread_sp = GetKernelThread ();
+    new_thread_list.AddThread(thread_sp);
 
     return new_thread_list.GetSize(false) > 0;
 }
@@ -798,7 +807,7 @@
                             is_running = true;
                             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC))
                             {
-                                ThreadSP thread_sp (process->GetKernelThread(process->GetThreadList(), process->GetThreadList()));
+                                ThreadSP thread_sp (process->GetKernelThread());
                                 if (thread_sp)
                                 {
                                     lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());