Remove shared_pointer from NativeThreadProtocol

Summary:
The NativeThread class is useless without the containing process (and in
some places it is already assuming the process is always around). This
makes it clear that the NativeProcessProtocol is the object owning the
threads, and makes the destruction order deterministic (first threads,
then process). The NativeProcess is the only thing holding a thread
unique_ptr, and methods that used to hand out thread shared pointers now
return raw pointers or references.

Reviewers: krytarowski, eugene

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D35618

llvm-svn: 316007
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
index a5dac2a..150de3c 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
@@ -305,10 +305,9 @@
   assert(m_sigchld_handle && status.Success());
 
   for (const auto &tid : tids) {
-    NativeThreadLinuxSP thread_sp = AddThread(tid);
-    assert(thread_sp && "AddThread() returned a nullptr thread");
-    thread_sp->SetStoppedBySignal(SIGSTOP);
-    ThreadWasCreated(*thread_sp);
+    NativeThreadLinux &thread = AddThread(tid);
+    thread.SetStoppedBySignal(SIGSTOP);
+    ThreadWasCreated(thread);
   }
 
   // Let our process instance know the thread has stopped.
@@ -478,11 +477,11 @@
     LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code,
              info.si_pid);
 
-    auto thread_sp = AddThread(pid);
+    NativeThreadLinux &thread = AddThread(pid);
 
     // Resume the newly created thread.
-    ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER);
-    ThreadWasCreated(*thread_sp);
+    ResumeThread(thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER);
+    ThreadWasCreated(thread);
     return;
   }
 
@@ -549,12 +548,9 @@
 void NativeProcessLinux::WaitForNewThread(::pid_t tid) {
   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
 
-  NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid);
-
-  if (new_thread_sp) {
+  if (GetThreadByID(tid)) {
     // We are already tracking the thread - we got the event on the new thread
-    // (see
-    // MonitorSignal) before this one. We are done.
+    // (see MonitorSignal) before this one. We are done.
     return;
   }
 
@@ -587,10 +583,10 @@
   }
 
   LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid);
-  new_thread_sp = AddThread(tid);
+  NativeThreadLinux &new_thread = AddThread(tid);
 
-  ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER);
-  ThreadWasCreated(*new_thread_sp);
+  ResumeThread(new_thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER);
+  ThreadWasCreated(new_thread);
 }
 
 void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info,
@@ -630,7 +626,6 @@
   }
 
   case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): {
-    NativeThreadLinuxSP main_thread_sp;
     LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP);
 
     // Exec clears any pending notifications.
@@ -640,44 +635,26 @@
     // which only copies the main thread.
     LLDB_LOG(log, "exec received, stop tracking all but main thread");
 
-    for (auto thread_sp : m_threads) {
-      const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID();
-      if (is_main_thread) {
-        main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp);
-        LLDB_LOG(log, "found main thread with tid {0}, keeping",
-                 main_thread_sp->GetID());
-      } else {
-        LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec",
-                 thread_sp->GetID());
-      }
+    for (auto i = m_threads.begin(); i != m_threads.end();) {
+      if ((*i)->GetID() == GetID())
+        i = m_threads.erase(i);
+      else
+        ++i;
     }
+    assert(m_threads.size() == 1);
+    auto *main_thread = static_cast<NativeThreadLinux *>(m_threads[0].get());
 
-    m_threads.clear();
-
-    if (main_thread_sp) {
-      m_threads.push_back(main_thread_sp);
-      SetCurrentThreadID(main_thread_sp->GetID());
-      main_thread_sp->SetStoppedByExec();
-    } else {
-      SetCurrentThreadID(LLDB_INVALID_THREAD_ID);
-      LLDB_LOG(log,
-               "pid {0} no main thread found, discarded all threads, "
-               "we're in a no-thread state!",
-               GetID());
-    }
+    SetCurrentThreadID(main_thread->GetID());
+    main_thread->SetStoppedByExec();
 
     // Tell coordinator about about the "new" (since exec) stopped main thread.
-    ThreadWasCreated(*main_thread_sp);
+    ThreadWasCreated(*main_thread);
 
     // Let our delegate know we have just exec'd.
     NotifyDidExec();
 
-    // If we have a main thread, indicate we are stopped.
-    assert(main_thread_sp && "exec called during ptraced process but no main "
-                             "thread metadata tracked");
-
     // Let the process know we're stopped.
-    StopRunningThreads(main_thread_sp->GetID());
+    StopRunningThreads(main_thread->GetID());
 
     break;
   }
@@ -1115,44 +1092,44 @@
   bool software_single_step = !SupportHardwareSingleStepping();
 
   if (software_single_step) {
-    for (auto thread_sp : m_threads) {
-      assert(thread_sp && "thread list should not contain NULL threads");
+    for (const auto &thread : m_threads) {
+      assert(thread && "thread list should not contain NULL threads");
 
       const ResumeAction *const action =
-          resume_actions.GetActionForThread(thread_sp->GetID(), true);
+          resume_actions.GetActionForThread(thread->GetID(), true);
       if (action == nullptr)
         continue;
 
       if (action->state == eStateStepping) {
         Status error = SetupSoftwareSingleStepping(
-            static_cast<NativeThreadLinux &>(*thread_sp));
+            static_cast<NativeThreadLinux &>(*thread));
         if (error.Fail())
           return error;
       }
     }
   }
 
-  for (auto thread_sp : m_threads) {
-    assert(thread_sp && "thread list should not contain NULL threads");
+  for (const auto &thread : m_threads) {
+    assert(thread && "thread list should not contain NULL threads");
 
     const ResumeAction *const action =
-        resume_actions.GetActionForThread(thread_sp->GetID(), true);
+        resume_actions.GetActionForThread(thread->GetID(), true);
 
     if (action == nullptr) {
       LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
-               thread_sp->GetID());
+               thread->GetID());
       continue;
     }
 
     LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}",
-             action->state, GetID(), thread_sp->GetID());
+             action->state, GetID(), thread->GetID());
 
     switch (action->state) {
     case eStateRunning:
     case eStateStepping: {
       // Run the thread, possibly feeding it the signal.
       const int signo = action->signal;
-      ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state,
+      ResumeThread(static_cast<NativeThreadLinux &>(*thread), action->state,
                    signo);
       break;
     }
@@ -1165,7 +1142,7 @@
       return Status("NativeProcessLinux::%s (): unexpected state %s specified "
                     "for pid %" PRIu64 ", tid %" PRIu64,
                     __FUNCTION__, StateAsCString(action->state), GetID(),
-                    thread_sp->GetID());
+                    thread->GetID());
     }
   }
 
@@ -1191,8 +1168,8 @@
   if (GetID() == LLDB_INVALID_PROCESS_ID)
     return error;
 
-  for (auto thread_sp : m_threads) {
-    Status e = Detach(thread_sp->GetID());
+  for (const auto &thread : m_threads) {
+    Status e = Detach(thread->GetID());
     if (e.Fail())
       error =
           e; // Save the error, but still attempt to detach from other threads.
@@ -1222,29 +1199,25 @@
   // the chosen thread that will be the stop-reason thread.
   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
 
-  NativeThreadProtocolSP running_thread_sp;
-  NativeThreadProtocolSP stopped_thread_sp;
+  NativeThreadProtocol *running_thread = nullptr;
+  NativeThreadProtocol *stopped_thread = nullptr;
 
   LLDB_LOG(log, "selecting running thread for interrupt target");
-  for (auto thread_sp : m_threads) {
-    // The thread shouldn't be null but lets just cover that here.
-    if (!thread_sp)
-      continue;
-
+  for (const auto &thread : m_threads) {
     // If we have a running or stepping thread, we'll call that the
     // target of the interrupt.
-    const auto thread_state = thread_sp->GetState();
+    const auto thread_state = thread->GetState();
     if (thread_state == eStateRunning || thread_state == eStateStepping) {
-      running_thread_sp = thread_sp;
+      running_thread = thread.get();
       break;
-    } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) {
+    } else if (!stopped_thread && StateIsStoppedState(thread_state, true)) {
       // Remember the first non-dead stopped thread.  We'll use that as a backup
       // if there are no running threads.
-      stopped_thread_sp = thread_sp;
+      stopped_thread = thread.get();
     }
   }
 
-  if (!running_thread_sp && !stopped_thread_sp) {
+  if (!running_thread && !stopped_thread) {
     Status error("found no running/stepping or live stopped threads as target "
                  "for interrupt");
     LLDB_LOG(log, "skipping due to error: {0}", error);
@@ -1252,14 +1225,14 @@
     return error;
   }
 
-  NativeThreadProtocolSP deferred_signal_thread_sp =
-      running_thread_sp ? running_thread_sp : stopped_thread_sp;
+  NativeThreadProtocol *deferred_signal_thread =
+      running_thread ? running_thread : stopped_thread;
 
   LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(),
-           running_thread_sp ? "running" : "stopped",
-           deferred_signal_thread_sp->GetID());
+           running_thread ? "running" : "stopped",
+           deferred_signal_thread->GetID());
 
-  StopRunningThreads(deferred_signal_thread_sp->GetID());
+  StopRunningThreads(deferred_signal_thread->GetID());
 
   return Status();
 }
@@ -1975,9 +1948,9 @@
 }
 
 bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) {
-  for (auto thread_sp : m_threads) {
-    assert(thread_sp && "thread list should not contain NULL threads");
-    if (thread_sp->GetID() == thread_id) {
+  for (const auto &thread : m_threads) {
+    assert(thread && "thread list should not contain NULL threads");
+    if (thread->GetID() == thread_id) {
       // We have this thread.
       return true;
     }
@@ -2006,7 +1979,7 @@
   return found;
 }
 
-NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) {
+NativeThreadLinux &NativeProcessLinux::AddThread(lldb::tid_t thread_id) {
   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
   LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
 
@@ -2017,8 +1990,7 @@
   if (m_threads.empty())
     SetCurrentThreadID(thread_id);
 
-  auto thread_sp = std::make_shared<NativeThreadLinux>(*this, thread_id);
-  m_threads.push_back(thread_sp);
+  m_threads.push_back(llvm::make_unique<NativeThreadLinux>(*this, thread_id));
 
   if (m_pt_proces_trace_id != LLDB_INVALID_UID) {
     auto traceMonitor = ProcessorTraceMonitor::Create(
@@ -2034,7 +2006,7 @@
     }
   }
 
-  return thread_sp;
+  return static_cast<NativeThreadLinux &>(*m_threads.back());
 }
 
 Status
@@ -2156,8 +2128,8 @@
   return Status("No load address found for specified file.");
 }
 
-NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) {
-  return std::static_pointer_cast<NativeThreadLinux>(
+NativeThreadLinux *NativeProcessLinux::GetThreadByID(lldb::tid_t tid) {
+  return static_cast<NativeThreadLinux *>(
       NativeProcessProtocol::GetThreadByID(tid));
 }
 
@@ -2212,9 +2184,9 @@
 
   // Request a stop for all the thread stops that need to be stopped
   // and are not already known to be stopped.
-  for (const auto &thread_sp : m_threads) {
-    if (StateIsRunningState(thread_sp->GetState()))
-      static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop();
+  for (const auto &thread : m_threads) {
+    if (StateIsRunningState(thread->GetState()))
+      static_cast<NativeThreadLinux *>(thread.get())->RequestStop();
   }
 
   SignalIfAllThreadsStopped();
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
index c9ec002..3d86120 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
@@ -102,7 +102,7 @@
   Status GetFileLoadAddress(const llvm::StringRef &file_name,
                             lldb::addr_t &load_addr) override;
 
-  NativeThreadLinuxSP GetThreadByID(lldb::tid_t id);
+  NativeThreadLinux *GetThreadByID(lldb::tid_t id);
 
   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
   GetAuxvData() const override {
@@ -203,7 +203,7 @@
 
   bool StopTrackingThread(lldb::tid_t thread_id);
 
-  NativeThreadLinuxSP AddThread(lldb::tid_t thread_id);
+  NativeThreadLinux &AddThread(lldb::tid_t thread_id);
 
   Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
 
diff --git a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
index 6ae87fe..01b54d9 100644
--- a/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
+++ b/lldb/source/Plugins/Process/Linux/NativeThreadLinux.h
@@ -110,8 +110,6 @@
   WatchpointIndexMap m_hw_break_index_map;
   std::unique_ptr<SingleStepWorkaround> m_step_workaround;
 };
-
-typedef std::shared_ptr<NativeThreadLinux> NativeThreadLinuxSP;
 } // namespace process_linux
 } // namespace lldb_private
 
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
index 388989a..4f9a696 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
@@ -109,10 +109,8 @@
   if (status.Fail())
     return status.ToError();
 
-  for (const auto &thread_sp : process_up->m_threads) {
-    static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal(
-        SIGSTOP);
-  }
+  for (const auto &thread : process_up->m_threads)
+    static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
   process_up->SetState(StateType::eStateStopped);
 
   return std::move(process_up);
@@ -198,9 +196,9 @@
     // Handle SIGSTOP from LLGS (LLDB GDB Server)
     if (info.psi_siginfo.si_code == SI_USER &&
         info.psi_siginfo.si_pid == ::getpid()) {
-      /* Stop Tracking All Threads attached to Process */
-      for (const auto &thread_sp : m_threads) {
-        static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal(
+      /* Stop Tracking all Threads attached to Process */
+      for (const auto &thread : m_threads) {
+        static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
             SIGSTOP, &info.psi_siginfo);
       }
     }
@@ -221,18 +219,15 @@
 
   switch (info.psi_siginfo.si_code) {
   case TRAP_BRKPT:
-    for (const auto &thread_sp : m_threads) {
-      static_pointer_cast<NativeThreadNetBSD>(thread_sp)
-          ->SetStoppedByBreakpoint();
-      FixupBreakpointPCAsNeeded(
-          *static_pointer_cast<NativeThreadNetBSD>(thread_sp));
+    for (const auto &thread : m_threads) {
+      static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
+      FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread));
     }
     SetState(StateType::eStateStopped, true);
     break;
   case TRAP_TRACE:
-    for (const auto &thread_sp : m_threads) {
-      static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedByTrace();
-    }
+    for (const auto &thread : m_threads)
+      static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace();
     SetState(StateType::eStateStopped, true);
     break;
   case TRAP_EXEC: {
@@ -245,37 +240,34 @@
     // Let our delegate know we have just exec'd.
     NotifyDidExec();
 
-    for (const auto &thread_sp : m_threads) {
-      static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedByExec();
-    }
+    for (const auto &thread : m_threads)
+      static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
     SetState(StateType::eStateStopped, true);
   } break;
   case TRAP_DBREG: {
     // If a watchpoint was hit, report it
     uint32_t wp_index;
-    Status error =
-        static_pointer_cast<NativeThreadNetBSD>(m_threads[info.psi_lwpid])
-            ->GetRegisterContext()
-            ->GetWatchpointHitIndex(wp_index,
-                                    (uintptr_t)info.psi_siginfo.si_addr);
+    Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
+                       .GetRegisterContext()
+                       ->GetWatchpointHitIndex(
+                           wp_index, (uintptr_t)info.psi_siginfo.si_addr);
     if (error.Fail())
       LLDB_LOG(log,
                "received error while checking for watchpoint hits, pid = "
                "{0}, LWP = {1}, error = {2}",
                GetID(), info.psi_lwpid, error);
     if (wp_index != LLDB_INVALID_INDEX32) {
-      for (const auto &thread_sp : m_threads) {
-        static_pointer_cast<NativeThreadNetBSD>(thread_sp)
-            ->SetStoppedByWatchpoint(wp_index);
-      }
+      for (const auto &thread : m_threads)
+        static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint(
+            wp_index);
       SetState(StateType::eStateStopped, true);
       break;
     }
 
     // If a breakpoint was hit, report it
     uint32_t bp_index;
-    error = static_pointer_cast<NativeThreadNetBSD>(m_threads[info.psi_lwpid])
-                ->GetRegisterContext()
+    error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid])
+                .GetRegisterContext()
                 ->GetHardwareBreakHitIndex(bp_index,
                                            (uintptr_t)info.psi_siginfo.si_addr);
     if (error.Fail())
@@ -284,10 +276,8 @@
                "breakpoint hits, pid = {0}, LWP = {1}, error = {2}",
                GetID(), info.psi_lwpid, error);
     if (bp_index != LLDB_INVALID_INDEX32) {
-      for (const auto &thread_sp : m_threads) {
-        static_pointer_cast<NativeThreadNetBSD>(thread_sp)
-            ->SetStoppedByBreakpoint();
-      }
+      for (const auto &thread : m_threads)
+        static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint();
       SetState(StateType::eStateStopped, true);
       break;
     }
@@ -300,8 +290,8 @@
   const auto siginfo_err =
       PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
 
-  for (const auto &thread_sp : m_threads) {
-    static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal(
+  for (const auto &thread : m_threads) {
+    static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
         info.psi_siginfo.si_signo, &info.psi_siginfo);
   }
   SetState(StateType::eStateStopped, true);
@@ -433,13 +423,13 @@
   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
   LLDB_LOG(log, "pid {0}", GetID());
 
-  const auto &thread_sp = m_threads[0];
+  const auto &thread = m_threads[0];
   const ResumeAction *const action =
-      resume_actions.GetActionForThread(thread_sp->GetID(), true);
+      resume_actions.GetActionForThread(thread->GetID(), true);
 
   if (action == nullptr) {
     LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
-             thread_sp->GetID());
+             thread->GetID());
     return Status();
   }
 
@@ -452,9 +442,8 @@
                                                action->signal);
     if (!error.Success())
       return error;
-    for (const auto &thread_sp : m_threads) {
-      static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetRunning();
-    }
+    for (const auto &thread : m_threads)
+      static_cast<NativeThreadNetBSD &>(*thread).SetRunning();
     SetState(eStateRunning, true);
     break;
   }
@@ -464,9 +453,8 @@
                                                action->signal);
     if (!error.Success())
       return error;
-    for (const auto &thread_sp : m_threads) {
-      static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStepping();
-    }
+    for (const auto &thread : m_threads)
+      static_cast<NativeThreadNetBSD &>(*thread).SetStepping();
     SetState(eStateStepping, true);
     break;
 
@@ -478,7 +466,7 @@
     return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
                   "for pid %" PRIu64 ", tid %" PRIu64,
                   __FUNCTION__, StateAsCString(action->state), GetID(),
-                  thread_sp->GetID());
+                  thread->GetID());
   }
 
   return Status();
@@ -764,9 +752,9 @@
 }
 
 bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
-  for (auto thread_sp : m_threads) {
-    assert(thread_sp && "thread list should not contain NULL threads");
-    if (thread_sp->GetID() == thread_id) {
+  for (const auto &thread : m_threads) {
+    assert(thread && "thread list should not contain NULL threads");
+    if (thread->GetID() == thread_id) {
       // We have this thread.
       return true;
     }
@@ -776,7 +764,7 @@
   return false;
 }
 
-NativeThreadNetBSDSP NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
+NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) {
 
   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
   LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
@@ -788,9 +776,8 @@
   if (m_threads.empty())
     SetCurrentThreadID(thread_id);
 
-  auto thread_sp = std::make_shared<NativeThreadNetBSD>(*this, thread_id);
-  m_threads.push_back(thread_sp);
-  return thread_sp;
+  m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id));
+  return static_cast<NativeThreadNetBSD &>(*m_threads.back());
 }
 
 Status NativeProcessNetBSD::Attach() {
@@ -811,10 +798,8 @@
   if (status.Fail())
     return status;
 
-  for (const auto &thread_sp : m_threads) {
-    static_pointer_cast<NativeThreadNetBSD>(thread_sp)->SetStoppedBySignal(
-        SIGSTOP);
-  }
+  for (const auto &thread : m_threads)
+    static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
 
   // Let our process instance know the thread has stopped.
   SetState(StateType::eStateStopped);
@@ -928,7 +913,7 @@
   }
   // Reinitialize from scratch threads and register them in process
   while (info.pl_lwpid != 0) {
-    NativeThreadNetBSDSP thread_sp = AddThread(info.pl_lwpid);
+    AddThread(info.pl_lwpid);
     error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info));
     if (error.Fail()) {
       return error;
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
index 2cbd5e3..5ecf462 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
+++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
@@ -121,7 +121,7 @@
 
   bool HasThreadNoLock(lldb::tid_t thread_id);
 
-  NativeThreadNetBSDSP AddThread(lldb::tid_t thread_id);
+  NativeThreadNetBSD &AddThread(lldb::tid_t thread_id);
 
   void MonitorCallback(lldb::pid_t pid, int signal);
   void MonitorExited(lldb::pid_t pid, WaitStatus status);
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
index 9294359..d5b031b 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
@@ -523,16 +523,16 @@
 
   // Ensure we can get info on the given thread.
   uint32_t thread_idx = 0;
-  for (NativeThreadProtocolSP thread_sp;
-       (thread_sp = process.GetThreadAtIndex(thread_idx)) != nullptr;
+  for (NativeThreadProtocol *thread;
+       (thread = process.GetThreadAtIndex(thread_idx)) != nullptr;
        ++thread_idx) {
 
-    lldb::tid_t tid = thread_sp->GetID();
+    lldb::tid_t tid = thread->GetID();
 
     // Grab the reason this thread stopped.
     struct ThreadStopInfo tid_stop_info;
     std::string description;
-    if (!thread_sp->GetStopReason(tid_stop_info, description))
+    if (!thread->GetStopReason(tid_stop_info, description))
       return nullptr;
 
     const int signum = tid_stop_info.details.signal.signo;
@@ -548,7 +548,7 @@
     threads_array_sp->AppendObject(thread_obj_sp);
 
     if (!abridged) {
-      if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread_sp))
+      if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread))
         thread_obj_sp->SetObject("registers", registers_sp);
     }
 
@@ -556,7 +556,7 @@
     if (signum != 0)
       thread_obj_sp->SetObject("signal", std::make_shared<JSONNumber>(signum));
 
-    const std::string thread_name = thread_sp->GetName();
+    const std::string thread_name = thread->GetName();
     if (!thread_name.empty())
       thread_obj_sp->SetObject("name",
                                std::make_shared<JSONString>(thread_name));
@@ -604,14 +604,14 @@
            m_debugged_process_up->GetID(), tid);
 
   // Ensure we can get info on the given thread.
-  NativeThreadProtocolSP thread_sp(m_debugged_process_up->GetThreadByID(tid));
-  if (!thread_sp)
+  NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
+  if (!thread)
     return SendErrorResponse(51);
 
   // Grab the reason this thread stopped.
   struct ThreadStopInfo tid_stop_info;
   std::string description;
-  if (!thread_sp->GetStopReason(tid_stop_info, description))
+  if (!thread->GetStopReason(tid_stop_info, description))
     return SendErrorResponse(52);
 
   // FIXME implement register handling for exec'd inferiors.
@@ -638,7 +638,7 @@
   response.Printf("thread:%" PRIx64 ";", tid);
 
   // Include the thread name if there is one.
-  const std::string thread_name = thread_sp->GetName();
+  const std::string thread_name = thread->GetName();
   if (!thread_name.empty()) {
     size_t thread_name_len = thread_name.length();
 
@@ -665,15 +665,13 @@
     response.PutCString("threads:");
 
     uint32_t thread_index = 0;
-    NativeThreadProtocolSP listed_thread_sp;
-    for (listed_thread_sp =
-             m_debugged_process_up->GetThreadAtIndex(thread_index);
-         listed_thread_sp; ++thread_index,
-        listed_thread_sp = m_debugged_process_up->GetThreadAtIndex(
-            thread_index)) {
+    NativeThreadProtocol *listed_thread;
+    for (listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
+         listed_thread; ++thread_index,
+        listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
       if (thread_index > 0)
         response.PutChar(',');
-      response.Printf("%" PRIx64, listed_thread_sp->GetID());
+      response.Printf("%" PRIx64, listed_thread->GetID());
     }
     response.PutChar(';');
 
@@ -701,10 +699,10 @@
     uint32_t i = 0;
     response.PutCString("thread-pcs");
     char delimiter = ':';
-    for (NativeThreadProtocolSP thread_sp;
-         (thread_sp = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr;
+    for (NativeThreadProtocol *thread;
+         (thread = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr;
          ++i) {
-      NativeRegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext();
+      NativeRegisterContextSP reg_ctx_sp = thread->GetRegisterContext();
       if (!reg_ctx_sp)
         continue;
 
@@ -739,7 +737,7 @@
   //
 
   // Grab the register context.
-  NativeRegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext();
+  NativeRegisterContextSP reg_ctx_sp = thread->GetRegisterContext();
   if (reg_ctx_sp) {
     // Expedite all registers in the first register set (i.e. should be GPRs)
     // that are not contained in other registers.
@@ -1316,12 +1314,12 @@
   lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID();
   SetCurrentThreadID(tid);
 
-  NativeThreadProtocolSP thread_sp = m_debugged_process_up->GetCurrentThread();
-  if (!thread_sp)
+  NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread();
+  if (!thread)
     return SendErrorResponse(69);
 
   StreamString response;
-  response.Printf("QC%" PRIx64, thread_sp->GetID());
+  response.Printf("QC%" PRIx64, thread->GetID());
 
   return SendPacketNoLock(response.GetString());
 }
@@ -1692,12 +1690,12 @@
     return SendErrorResponse(68);
 
   // Ensure we have a thread.
-  NativeThreadProtocolSP thread_sp(m_debugged_process_up->GetThreadAtIndex(0));
-  if (!thread_sp)
+  NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0);
+  if (!thread)
     return SendErrorResponse(69);
 
   // Get the register context for the first thread.
-  NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext());
+  NativeRegisterContextSP reg_context_sp(thread->GetRegisterContext());
   if (!reg_context_sp)
     return SendErrorResponse(69);
 
@@ -1908,18 +1906,17 @@
   response.PutChar('m');
 
   LLDB_LOG(log, "starting thread iteration");
-  NativeThreadProtocolSP thread_sp;
+  NativeThreadProtocol *thread;
   uint32_t thread_index;
   for (thread_index = 0,
-      thread_sp = m_debugged_process_up->GetThreadAtIndex(thread_index);
-       thread_sp; ++thread_index,
-      thread_sp = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
-    LLDB_LOG(log, "iterated thread {0}({1}, tid={2})", thread_index,
-             thread_sp ? "is not null" : "null",
-             thread_sp ? thread_sp->GetID() : LLDB_INVALID_THREAD_ID);
+      thread = m_debugged_process_up->GetThreadAtIndex(thread_index);
+       thread; ++thread_index,
+      thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) {
+    LLDB_LOG(log, "iterated thread {0}(tid={2})", thread_index,
+             thread->GetID());
     if (thread_index > 0)
       response.PutChar(',');
-    response.Printf("%" PRIx64, thread_sp->GetID());
+    response.Printf("%" PRIx64, thread->GetID());
   }
 
   LLDB_LOG(log, "finished thread iteration");
@@ -1951,22 +1948,19 @@
   }
 
   // Get the thread to use.
-  NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet);
-  if (!thread_sp) {
-    if (log)
-      log->Printf(
-          "GDBRemoteCommunicationServerLLGS::%s failed, no thread available",
-          __FUNCTION__);
+  NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
+  if (!thread) {
+    LLDB_LOG(log, "failed, no thread available");
     return SendErrorResponse(0x15);
   }
 
   // Get the thread's register context.
-  NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext());
+  NativeRegisterContextSP reg_context_sp(thread->GetRegisterContext());
   if (!reg_context_sp) {
     LLDB_LOG(
         log,
         "pid {0} tid {1} failed, no register context available for the thread",
-        m_debugged_process_up->GetID(), thread_sp->GetID());
+        m_debugged_process_up->GetID(), thread->GetID());
     return SendErrorResponse(0x15);
   }
 
@@ -2063,8 +2057,8 @@
   size_t reg_size = packet.GetHexBytesAvail(reg_bytes);
 
   // Get the thread to use.
-  NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet);
-  if (!thread_sp) {
+  NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
+  if (!thread) {
     if (log)
       log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, no thread "
                   "available (thread index 0)",
@@ -2073,13 +2067,13 @@
   }
 
   // Get the thread's register context.
-  NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext());
+  NativeRegisterContextSP reg_context_sp(thread->GetRegisterContext());
   if (!reg_context_sp) {
     if (log)
       log->Printf(
           "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64
           " failed, no register context available for the thread",
-          __FUNCTION__, m_debugged_process_up->GetID(), thread_sp->GetID());
+          __FUNCTION__, m_debugged_process_up->GetID(), thread->GetID());
     return SendErrorResponse(0x15);
   }
 
@@ -2177,8 +2171,8 @@
   // Ensure we have the given thread when not specifying -1 (all threads) or 0
   // (any thread).
   if (tid != LLDB_INVALID_THREAD_ID && tid != 0) {
-    NativeThreadProtocolSP thread_sp(m_debugged_process_up->GetThreadByID(tid));
-    if (!thread_sp) {
+    NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
+    if (!thread) {
       if (log)
         log->Printf("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64
                     " not found",
@@ -2739,8 +2733,8 @@
 
   // Double check that we have such a thread.
   // TODO investigate: on MacOSX we might need to do an UpdateThreads () here.
-  NativeThreadProtocolSP thread_sp = m_debugged_process_up->GetThreadByID(tid);
-  if (!thread_sp || thread_sp->GetID() != tid)
+  NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid);
+  if (!thread)
     return SendErrorResponse(0x33);
 
   // Create the step action for the given thread.
@@ -2865,8 +2859,8 @@
   packet.SetFilePos(strlen("QSaveRegisterState"));
 
   // Get the thread to use.
-  NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet);
-  if (!thread_sp) {
+  NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
+  if (!thread) {
     if (m_thread_suffix_supported)
       return SendIllFormedResponse(
           packet, "No thread specified in QSaveRegisterState packet");
@@ -2876,12 +2870,12 @@
   }
 
   // Grab the register context for the thread.
-  NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext());
+  NativeRegisterContextSP reg_context_sp(thread->GetRegisterContext());
   if (!reg_context_sp) {
     LLDB_LOG(
         log,
         "pid {0} tid {1} failed, no register context available for the thread",
-        m_debugged_process_up->GetID(), thread_sp->GetID());
+        m_debugged_process_up->GetID(), thread->GetID());
     return SendErrorResponse(0x15);
   }
 
@@ -2930,8 +2924,8 @@
   }
 
   // Get the thread to use.
-  NativeThreadProtocolSP thread_sp = GetThreadFromSuffix(packet);
-  if (!thread_sp) {
+  NativeThreadProtocol *thread = GetThreadFromSuffix(packet);
+  if (!thread) {
     if (m_thread_suffix_supported)
       return SendIllFormedResponse(
           packet, "No thread specified in QRestoreRegisterState packet");
@@ -2941,12 +2935,12 @@
   }
 
   // Grab the register context for the thread.
-  NativeRegisterContextSP reg_context_sp(thread_sp->GetRegisterContext());
+  NativeRegisterContextSP reg_context_sp(thread->GetRegisterContext());
   if (!reg_context_sp) {
     LLDB_LOG(
         log,
         "pid {0} tid {1} failed, no register context available for the thread",
-        m_debugged_process_up->GetID(), thread_sp->GetID());
+        m_debugged_process_up->GetID(), thread->GetID());
     return SendErrorResponse(0x15);
   }
 
@@ -3219,14 +3213,12 @@
   }
 }
 
-NativeThreadProtocolSP GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix(
+NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix(
     StringExtractorGDBRemote &packet) {
-  NativeThreadProtocolSP thread_sp;
-
   // We have no thread if we don't have a process.
   if (!m_debugged_process_up ||
       m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)
-    return thread_sp;
+    return nullptr;
 
   // If the client hasn't asked for thread suffix support, there will not be a
   // thread suffix.
@@ -3234,7 +3226,7 @@
   if (!m_thread_suffix_supported) {
     const lldb::tid_t current_tid = GetCurrentThreadID();
     if (current_tid == LLDB_INVALID_THREAD_ID)
-      return thread_sp;
+      return nullptr;
     else if (current_tid == 0) {
       // Pick a thread.
       return m_debugged_process_up->GetThreadAtIndex(0);
@@ -3251,11 +3243,11 @@
                   "error: expected ';' prior to start of thread suffix: packet "
                   "contents = '%s'",
                   __FUNCTION__, packet.GetStringRef().c_str());
-    return thread_sp;
+    return nullptr;
   }
 
   if (!packet.GetBytesLeft())
-    return thread_sp;
+    return nullptr;
 
   // Parse out thread: portion.
   if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) {
@@ -3264,14 +3256,14 @@
                   "error: expected 'thread:' but not found, packet contents = "
                   "'%s'",
                   __FUNCTION__, packet.GetStringRef().c_str());
-    return thread_sp;
+    return nullptr;
   }
   packet.SetFilePos(packet.GetFilePos() + strlen("thread:"));
   const lldb::tid_t tid = packet.GetHexMaxU64(false, 0);
   if (tid != 0)
     return m_debugged_process_up->GetThreadByID(tid);
 
-  return thread_sp;
+  return nullptr;
 }
 
 lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const {
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h
index 7119947..7deee4b 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h
@@ -234,7 +234,7 @@
 
   void HandleInferiorState_Stopped(NativeProcessProtocol *process);
 
-  NativeThreadProtocolSP GetThreadFromSuffix(StringExtractorGDBRemote &packet);
+  NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
 
   uint32_t GetNextSavedRegistersID();