Thread hardening part 3. Now lldb_private::Thread objects have std::weak_ptr
objects for the backlink to the lldb_private::Process. The issues we were
running into before was someone was holding onto a shared pointer to a 
lldb_private::Thread for too long, and the lldb_private::Process parent object
would get destroyed and the lldb_private::Thread had a "Process &m_process"
member which would just treat whatever memory that used to be a Process as a
valid Process. This was mostly happening for lldb_private::StackFrame objects
that had a member like "Thread &m_thread". So this completes the internal
strong/weak changes.

Documented the ExecutionContext and ExecutionContextRef classes so that our
LLDB developers can understand when and where to use ExecutionContext and 
ExecutionContextRef objects.



git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@151009 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp b/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp
index 4938591..55cb222 100644
--- a/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp
+++ b/source/Plugins/Process/gdb-remote/ThreadGDBRemote.cpp
@@ -32,18 +32,25 @@
 // Thread Registers
 //----------------------------------------------------------------------
 
-ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) :
-    Thread(process, tid),
+ThreadGDBRemote::ThreadGDBRemote (const ProcessSP &process_sp, lldb::tid_t tid) :
+    Thread(process_sp, tid),
     m_thread_name (),
     m_dispatch_queue_name (),
     m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
 {
-    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
+    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 
+                               this, 
+                               process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, 
+                               GetID());
 }
 
 ThreadGDBRemote::~ThreadGDBRemote ()
 {
-    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
+    ProcessSP process_sp(GetProcess());
+    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 
+                               this, 
+                               process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, 
+                               GetID());
     DestroyThread();
 }
 
@@ -60,8 +67,16 @@
 ThreadGDBRemote::GetQueueName ()
 {
     // Always re-fetch the dispatch queue name since it can change
+
     if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
-        return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
+    {
+        ProcessSP process_sp (GetProcess());
+        if (process_sp)
+        {
+            ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
+            return gdb_process->GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
+        }
+    }
     return NULL;
 }
 
@@ -79,32 +94,37 @@
     if (log)
         log->Printf ("Resuming thread: %4.4llx with state: %s.", GetID(), StateAsCString(resume_state));
 
-    ProcessGDBRemote &process = GetGDBProcess();
-    switch (resume_state)
+    ProcessSP process_sp (GetProcess());
+    if (process_sp)
     {
-    case eStateSuspended:
-    case eStateStopped:
-        // Don't append anything for threads that should stay stopped.
-        break;
+        ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
+        switch (resume_state)
+        {
+        case eStateSuspended:
+        case eStateStopped:
+            // Don't append anything for threads that should stay stopped.
+            break;
 
-    case eStateRunning:
-        if (m_process.GetUnixSignals().SignalIsValid (signo))
-            process.m_continue_C_tids.push_back(std::make_pair(GetID(), signo));
-        else
-            process.m_continue_c_tids.push_back(GetID());
-        break;
+        case eStateRunning:
+            if (gdb_process->GetUnixSignals().SignalIsValid (signo))
+                gdb_process->m_continue_C_tids.push_back(std::make_pair(GetID(), signo));
+            else
+                gdb_process->m_continue_c_tids.push_back(GetID());
+            break;
 
-    case eStateStepping:
-        if (m_process.GetUnixSignals().SignalIsValid (signo))
-            process.m_continue_S_tids.push_back(std::make_pair(GetID(), signo));
-        else
-            process.m_continue_s_tids.push_back(GetID());
-        break;
+        case eStateStepping:
+            if (gdb_process->GetUnixSignals().SignalIsValid (signo))
+                gdb_process->m_continue_S_tids.push_back(std::make_pair(GetID(), signo));
+            else
+                gdb_process->m_continue_s_tids.push_back(GetID());
+            break;
 
-    default:
-        break;
+        default:
+            break;
+        }
+        return true;
     }
-    return true;
+    return false;
 }
 
 void
@@ -167,8 +187,16 @@
     if (frame)
         concrete_frame_idx = frame->GetConcreteFrameIndex ();
 
+    
     if (concrete_frame_idx == 0)
-        reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, GetGDBProcess().m_register_info, read_all_registers_at_once));
+    {
+        ProcessSP process_sp (GetProcess());
+        if (process_sp)
+        {
+            ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
+            reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
+        }
+    }
     else if (m_unwinder_ap.get())
         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
     return reg_ctx_sp;
@@ -185,25 +213,29 @@
 lldb::StopInfoSP
 ThreadGDBRemote::GetPrivateStopReason ()
 {
-    const uint32_t process_stop_id = GetProcess().GetStopID();
-    if (m_thread_stop_reason_stop_id != process_stop_id ||
-        (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
+    ProcessSP process_sp (GetProcess());
+    if (process_sp)
     {
-        // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
-        // for this thread, then m_actual_stop_info_sp will not ever contain
-        // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
-        // check will never be able to tell us if we have the correct stop info
-        // for this thread and we will continually send qThreadStopInfo packets
-        // down to the remote GDB server, so we need to keep our own notion
-        // of the stop ID that m_actual_stop_info_sp is valid for (even if it
-        // contains nothing). We use m_thread_stop_reason_stop_id for this below.
-        m_thread_stop_reason_stop_id = process_stop_id;
-        m_actual_stop_info_sp.reset();
+        const uint32_t process_stop_id = process_sp->GetStopID();
+        if (m_thread_stop_reason_stop_id != process_stop_id ||
+            (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
+        {
+            // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
+            // for this thread, then m_actual_stop_info_sp will not ever contain
+            // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
+            // check will never be able to tell us if we have the correct stop info
+            // for this thread and we will continually send qThreadStopInfo packets
+            // down to the remote GDB server, so we need to keep our own notion
+            // of the stop ID that m_actual_stop_info_sp is valid for (even if it
+            // contains nothing). We use m_thread_stop_reason_stop_id for this below.
+            m_thread_stop_reason_stop_id = process_stop_id;
+            m_actual_stop_info_sp.reset();
 
-        StringExtractorGDBRemote stop_packet;
-        ProcessGDBRemote &gdb_process = GetGDBProcess();
-        if (gdb_process.GetGDBRemote().GetThreadStopInfo(GetID(), stop_packet))
-            gdb_process.SetThreadStopInfo (stop_packet);
+            StringExtractorGDBRemote stop_packet;
+            ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
+            if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetID(), stop_packet))
+                gdb_process->SetThreadStopInfo (stop_packet);
+        }
     }
     return m_actual_stop_info_sp;
 }