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/Target/ABI.cpp b/source/Target/ABI.cpp
index 0ac91da..069a132 100644
--- a/source/Target/ABI.cpp
+++ b/source/Target/ABI.cpp
@@ -105,8 +105,8 @@
 
 ValueObjectSP
 ABI::GetReturnValueObject (Thread &thread,
-                          ClangASTType &ast_type,
-                          bool persistent) const
+                           ClangASTType &ast_type,
+                           bool persistent) const
 {
     if (!ast_type.IsValid())
         return ValueObjectSP();
@@ -123,7 +123,7 @@
     
     if (persistent)
     {
-        ClangPersistentVariables& persistent_variables = thread.GetProcess().GetTarget().GetPersistentVariables();
+        ClangPersistentVariables& persistent_variables = thread.CalculateTarget()->GetPersistentVariables();
         ConstString persistent_variable_name (persistent_variables.GetNextPersistentVariableName());
 
         lldb::ValueObjectSP const_valobj_sp;
diff --git a/source/Target/ExecutionContext.cpp b/source/Target/ExecutionContext.cpp
index e284438..3e0731d 100644
--- a/source/Target/ExecutionContext.cpp
+++ b/source/Target/ExecutionContext.cpp
@@ -72,6 +72,50 @@
         SetContext (frame_sp);
 }
 
+ExecutionContext::ExecutionContext (const lldb::TargetWP &target_wp, bool get_process) :
+    m_target_sp (),
+    m_process_sp (),
+    m_thread_sp (),
+    m_frame_sp ()
+{
+    lldb::TargetSP target_sp(target_wp.lock());
+    if (target_sp)
+        SetContext (target_sp, get_process);
+}
+
+ExecutionContext::ExecutionContext (const lldb::ProcessWP &process_wp) :
+    m_target_sp (),
+    m_process_sp (),
+    m_thread_sp (),
+    m_frame_sp ()
+{
+    lldb::ProcessSP process_sp(process_wp.lock());
+    if (process_sp)
+        SetContext (process_sp);
+}
+
+ExecutionContext::ExecutionContext (const lldb::ThreadWP &thread_wp) :
+    m_target_sp (),
+    m_process_sp (),
+    m_thread_sp (),
+    m_frame_sp ()
+{
+    lldb::ThreadSP thread_sp(thread_wp.lock());
+    if (thread_sp)
+        SetContext (thread_sp);
+}
+
+ExecutionContext::ExecutionContext (const lldb::StackFrameWP &frame_wp) :
+    m_target_sp (),
+    m_process_sp (),
+    m_thread_sp (),
+    m_frame_sp ()
+{
+    lldb::StackFrameSP frame_sp(frame_wp.lock());
+    if (frame_sp)
+        SetContext (frame_sp);
+}
+
 ExecutionContext::ExecutionContext (Target* t, bool fill_current_process_thread_frame) :
     m_target_sp (t->shared_from_this()),
     m_process_sp (),
@@ -208,28 +252,36 @@
 Target &
 ExecutionContext::GetTargetRef () const
 {
+#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
     assert (m_target_sp.get());
+#endif
     return *m_target_sp;
 }
 
 Process &
 ExecutionContext::GetProcessRef () const
 {
+#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
     assert (m_process_sp.get());
+#endif
     return *m_process_sp;
 }
 
 Thread &
 ExecutionContext::GetThreadRef () const
 {
+#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
     assert (m_thread_sp.get());
+#endif
     return *m_thread_sp;
 }
 
 StackFrame &
 ExecutionContext::GetFrameRef () const
 {
+#if defined (LLDB_CONFIGURATION_DEBUG) || defined (LLDB_CONFIGURATION_RELEASE)
     assert (m_frame_sp.get());
+#endif
     return *m_frame_sp;
 }
 
@@ -324,7 +376,7 @@
     m_thread_sp = thread_sp;
     if (thread_sp)
     {
-        m_process_sp = thread_sp->GetProcess().shared_from_this();
+        m_process_sp = thread_sp->GetProcess();
         if (m_process_sp)
             m_target_sp = m_process_sp->GetTarget().shared_from_this();
         else
@@ -346,7 +398,7 @@
         m_thread_sp = frame_sp->CalculateThread();
         if (m_thread_sp)
         {
-            m_process_sp = m_thread_sp->GetProcess().shared_from_this();
+            m_process_sp = m_thread_sp->GetProcess();
             if (m_process_sp)
                 m_target_sp = m_process_sp->GetTarget().shared_from_this();
             else
diff --git a/source/Target/RegisterContext.cpp b/source/Target/RegisterContext.cpp
index ba1f641..19e754d 100644
--- a/source/Target/RegisterContext.cpp
+++ b/source/Target/RegisterContext.cpp
@@ -27,7 +27,7 @@
 RegisterContext::RegisterContext (Thread &thread, uint32_t concrete_frame_idx) :
     m_thread (thread),
     m_concrete_frame_idx (concrete_frame_idx),
-    m_stop_id (thread.GetProcess().GetStopID())
+    m_stop_id (thread.GetProcess()->GetStopID())
 {
 }
 
@@ -41,9 +41,19 @@
 void
 RegisterContext::InvalidateIfNeeded (bool force)
 {
-    const uint32_t this_stop_id = GetStopID();
-    const uint32_t process_stop_id = m_thread.GetProcess().GetStopID();    
-    if (force || process_stop_id != this_stop_id)
+    ProcessSP process_sp (m_thread.GetProcess());
+    bool invalidate = force;
+    uint32_t process_stop_id = UINT32_MAX;
+
+    if (process_sp)
+        process_stop_id = process_sp->GetStopID();
+    else
+        invalidate = true;
+    
+    if (!invalidate)
+        invalidate = process_stop_id != GetStopID();
+
+    if (invalidate)
     {
         InvalidateAllRegisters ();
         SetStopID (process_stop_id);
@@ -279,33 +289,39 @@
         return error;
     }
     
-    Process &process = m_thread.GetProcess();
-    uint8_t src[RegisterValue::kMaxRegisterByteSize];
-   
-    // Read the memory
-    const uint32_t bytes_read = process.ReadMemory (src_addr, src, src_len, error);
-
-    // Make sure the memory read succeeded...
-    if (bytes_read != src_len)
+    ProcessSP process_sp (m_thread.GetProcess());
+    if (process_sp)
     {
-        if (error.Success())
+        uint8_t src[RegisterValue::kMaxRegisterByteSize];
+       
+        // Read the memory
+        const uint32_t bytes_read = process_sp->ReadMemory (src_addr, src, src_len, error);
+
+        // Make sure the memory read succeeded...
+        if (bytes_read != src_len)
         {
-            // This might happen if we read _some_ bytes but not all
-            error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read, src_len);
+            if (error.Success())
+            {
+                // This might happen if we read _some_ bytes but not all
+                error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read, src_len);
+            }
+            return error;
         }
-        return error;
+        
+        // We now have a memory buffer that contains the part or all of the register
+        // value. Set the register value using this memory data.
+        // TODO: we might need to add a parameter to this function in case the byte
+        // order of the memory data doesn't match the process. For now we are assuming
+        // they are the same.
+        reg_value.SetFromMemoryData (reg_info, 
+                                     src, 
+                                     src_len, 
+                                     process_sp->GetByteOrder(), 
+                                     error);
     }
-    
-    // We now have a memory buffer that contains the part or all of the register
-    // value. Set the register value using this memory data.
-    // TODO: we might need to add a parameter to this function in case the byte
-    // order of the memory data doesn't match the process. For now we are assuming
-    // they are the same.
-    reg_value.SetFromMemoryData (reg_info, 
-                                 src, 
-                                 src_len, 
-                                 process.GetByteOrder(), 
-                                 error);
+    else
+        error.SetErrorString("invalid process");
+
     return error;
 }
 
@@ -320,37 +336,42 @@
 
     Error error;
 
-    Process &process = m_thread.GetProcess();
-
-    // TODO: we might need to add a parameter to this function in case the byte
-    // order of the memory data doesn't match the process. For now we are assuming
-    // they are the same.
-
-    const uint32_t bytes_copied = reg_value.GetAsMemoryData (reg_info, 
-                                                             dst, 
-                                                             dst_len, 
-                                                             process.GetByteOrder(), 
-                                                             error);
-
-    if (error.Success())
+    ProcessSP process_sp (m_thread.GetProcess());
+    if (process_sp)
     {
-        if (bytes_copied == 0)
+
+        // TODO: we might need to add a parameter to this function in case the byte
+        // order of the memory data doesn't match the process. For now we are assuming
+        // they are the same.
+
+        const uint32_t bytes_copied = reg_value.GetAsMemoryData (reg_info, 
+                                                                 dst, 
+                                                                 dst_len, 
+                                                                 process_sp->GetByteOrder(), 
+                                                                 error);
+
+        if (error.Success())
         {
-            error.SetErrorString("byte copy failed.");
-        }
-        else
-        {
-            const uint32_t bytes_written = process.WriteMemory (dst_addr, dst, bytes_copied, error);
-            if (bytes_written != bytes_copied)
+            if (bytes_copied == 0)
             {
-                if (error.Success())
+                error.SetErrorString("byte copy failed.");
+            }
+            else
+            {
+                const uint32_t bytes_written = process_sp->WriteMemory (dst_addr, dst, bytes_copied, error);
+                if (bytes_written != bytes_copied)
                 {
-                    // This might happen if we read _some_ bytes but not all
-                    error.SetErrorStringWithFormat("only wrote %u of %u bytes", bytes_written, bytes_copied);
+                    if (error.Success())
+                    {
+                        // This might happen if we read _some_ bytes but not all
+                        error.SetErrorStringWithFormat("only wrote %u of %u bytes", bytes_written, bytes_copied);
+                    }
                 }
             }
         }
     }
+    else
+        error.SetErrorString("invalid process");
 
     return error;
 
diff --git a/source/Target/StackFrame.cpp b/source/Target/StackFrame.cpp
index c1802b4..5324a69 100644
--- a/source/Target/StackFrame.cpp
+++ b/source/Target/StackFrame.cpp
@@ -112,7 +112,7 @@
     m_frame_index (frame_idx),
     m_concrete_frame_index (unwind_frame_index),    
     m_reg_context_sp (reg_context_sp),
-    m_id (pc_addr.GetLoadAddress (&thread_sp->GetProcess().GetTarget()), cfa, NULL),
+    m_id (pc_addr.GetLoadAddress (thread_sp->CalculateTarget().get()), cfa, NULL),
     m_frame_code_addr (pc_addr),
     m_sc (),
     m_flags (),
diff --git a/source/Target/StackFrameList.cpp b/source/Target/StackFrameList.cpp
index 27383d8..8d8d131 100644
--- a/source/Target/StackFrameList.cpp
+++ b/source/Target/StackFrameList.cpp
@@ -401,14 +401,14 @@
 void
 StackFrameList::SetDefaultFileAndLineToSelectedFrame()
 {
-    if (m_thread.GetID() == m_thread.GetProcess().GetThreadList().GetSelectedThread()->GetID())
+    if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
     {
         StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
         if (frame_sp)
         {
             SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
             if (sc.line_entry.file)
-                m_thread.GetProcess().GetTarget().GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file, 
+                m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file, 
                                                                                             sc.line_entry.line);
         }
     }
diff --git a/source/Target/StopInfo.cpp b/source/Target/StopInfo.cpp
index ccfd5be..d5ef53b 100644
--- a/source/Target/StopInfo.cpp
+++ b/source/Target/StopInfo.cpp
@@ -34,8 +34,8 @@
 
 StopInfo::StopInfo (Thread &thread, uint64_t value) :
     m_thread (thread),
-    m_stop_id (thread.GetProcess().GetStopID()),
-    m_resume_id (thread.GetProcess().GetResumeID()),
+    m_stop_id (thread.GetProcess()->GetStopID()),
+    m_resume_id (thread.GetProcess()->GetResumeID()),
     m_value (value)
 {
 }
@@ -43,20 +43,20 @@
 bool
 StopInfo::IsValid () const
 {
-    return m_thread.GetProcess().GetStopID() == m_stop_id;
+    return m_thread.GetProcess()->GetStopID() == m_stop_id;
 }
 
 void
 StopInfo::MakeStopInfoValid ()
 {
-    m_stop_id = m_thread.GetProcess().GetStopID();
-    m_resume_id = m_thread.GetProcess().GetResumeID();
+    m_stop_id = m_thread.GetProcess()->GetStopID();
+    m_resume_id = m_thread.GetProcess()->GetResumeID();
 }
 
 bool
 StopInfo::HasTargetRunSinceMe ()
 {
-    lldb::StateType ret_type = m_thread.GetProcess().GetPrivateState();
+    lldb::StateType ret_type = m_thread.GetProcess()->GetPrivateState();
     if (ret_type == eStateRunning)
     {
         return true;
@@ -69,8 +69,8 @@
         // and resumes caused by expressions, and check if there are any resumes NOT caused
         // by expressions.
         
-        uint32_t curr_resume_id = m_thread.GetProcess().GetResumeID();
-        uint32_t last_user_expression_id = m_thread.GetProcess().GetLastUserExpressionResumeID ();
+        uint32_t curr_resume_id = m_thread.GetProcess()->GetResumeID();
+        uint32_t last_user_expression_id = m_thread.GetProcess()->GetLastUserExpressionResumeID ();
         if (curr_resume_id == m_resume_id)
         {
             return false;
@@ -101,7 +101,7 @@
         m_should_perform_action (true),
         m_address (LLDB_INVALID_ADDRESS)
     {
-        BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
+        BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
         if (bp_site_sp)
         {
           m_address = bp_site_sp->GetLoadAddress();
@@ -116,7 +116,7 @@
         m_should_perform_action (true),
         m_address (LLDB_INVALID_ADDRESS)
     {
-        BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
+        BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
         if (bp_site_sp)
         {
           m_address = bp_site_sp->GetLoadAddress();
@@ -139,15 +139,11 @@
         if (!m_should_stop_is_valid)
         {
             // Only check once if we should stop at a breakpoint
-            BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
+            BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
             if (bp_site_sp)
             {
-                StoppointCallbackContext context (event_ptr, 
-                                                  &m_thread.GetProcess(), 
-                                                  &m_thread, 
-                                                  m_thread.GetStackFrameAtIndex(0).get(),
-                                                  true);
-                
+                ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
+                StoppointCallbackContext context (event_ptr, exe_ctx, true);
                 m_should_stop = bp_site_sp->ShouldStop (&context);
             }
             else
@@ -173,7 +169,7 @@
         
         LogSP log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
         
-        BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
+        BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
         
         if (bp_site_sp)
         {
@@ -197,11 +193,8 @@
                 
                 m_should_stop = false;
 
-                StoppointCallbackContext context (event_ptr, 
-                                                  &m_thread.GetProcess(), 
-                                                  &m_thread, 
-                                                  m_thread.GetStackFrameAtIndex(0).get(),
-                                                  false);
+                ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
+                StoppointCallbackContext context (event_ptr, exe_ctx, false);
 
                 for (size_t j = 0; j < num_owners; j++)
                 {
@@ -222,7 +215,7 @@
                         ValueObjectSP result_value_sp;
                         const bool discard_on_error = true;
                         Error error;
-                        result_code = ClangUserExpression::EvaluateWithError (context.exe_ctx,
+                        result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
                                                                               eExecutionPolicyAlways,
                                                                               lldb::eLanguageTypeUnknown,
                                                                               ClangUserExpression::eResultTypeAny,
@@ -256,7 +249,7 @@
                         }
                         else
                         {
-                            Debugger &debugger = context.exe_ctx.GetTargetRef().GetDebugger();
+                            Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
                             StreamSP error_sp = debugger.GetAsyncErrorStream ();
                             error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
                             bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
@@ -286,7 +279,7 @@
                     // to get out of there.  So set it here.
                     // When we figure out how to nest breakpoint hits then this will change.
                     
-                    Debugger &debugger = m_thread.GetProcess().GetTarget().GetDebugger();
+                    Debugger &debugger = m_thread.CalculateTarget()->GetDebugger();
                     bool old_async = debugger.GetAsyncExecution();
                     debugger.SetAsyncExecution (true);
                     
@@ -326,7 +319,7 @@
     virtual bool
     ShouldNotify (Event *event_ptr)
     {
-        BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
+        BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
         if (bp_site_sp)
         {
             bool all_internal = true;
@@ -349,7 +342,7 @@
     {
         if (m_description.empty())
         {
-            BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (m_value));
+            BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (m_value));
             if (bp_site_sp)
             {
                 StreamString strm;
@@ -422,16 +415,12 @@
             return m_should_stop;
 
         WatchpointSP wp_sp =
-            m_thread.GetProcess().GetTarget().GetWatchpointList().FindByID(GetValue());
+            m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
         if (wp_sp)
         {
             // Check if we should stop at a watchpoint.
-            StoppointCallbackContext context (event_ptr, 
-                                              &m_thread.GetProcess(), 
-                                              &m_thread, 
-                                              m_thread.GetStackFrameAtIndex(0).get(),
-                                              true);
-                
+            ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
+            StoppointCallbackContext context (event_ptr, exe_ctx, true);
             m_should_stop = wp_sp->ShouldStop (&context);
         }
         else
@@ -459,14 +448,11 @@
         m_should_stop = true;
         
         WatchpointSP wp_sp =
-            m_thread.GetProcess().GetTarget().GetWatchpointList().FindByID(GetValue());
+            m_thread.CalculateTarget()->GetWatchpointList().FindByID(GetValue());
         if (wp_sp)
         {
-            StoppointCallbackContext context (event_ptr, 
-                                              &m_thread.GetProcess(), 
-                                              &m_thread, 
-                                              m_thread.GetStackFrameAtIndex(0).get(),
-                                              false);
+            ExecutionContext exe_ctx (m_thread.GetStackFrameAtIndex(0));
+            StoppointCallbackContext context (event_ptr, exe_ctx, false);
             bool stop_requested = wp_sp->InvokeCallback (&context);
             // Also make sure that the callback hasn't continued the target.  
             // If it did, when we'll set m_should_start to false and get out of here.
@@ -483,16 +469,11 @@
             {
                 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
                 // constructor errors up to the debugger's Async I/O.
-                StoppointCallbackContext context (event_ptr, 
-                                                  &m_thread.GetProcess(), 
-                                                  &m_thread, 
-                                                  m_thread.GetStackFrameAtIndex(0).get(),
-                                                  false);
                 ExecutionResults result_code;
                 ValueObjectSP result_value_sp;
                 const bool discard_on_error = true;
                 Error error;
-                result_code = ClangUserExpression::EvaluateWithError (context.exe_ctx,
+                result_code = ClangUserExpression::EvaluateWithError (exe_ctx,
                                                                       eExecutionPolicyAlways,
                                                                       lldb::eLanguageTypeUnknown,
                                                                       ClangUserExpression::eResultTypeAny,
@@ -532,7 +513,7 @@
                 }
                 else
                 {
-                    Debugger &debugger = context.exe_ctx.GetTargetRef().GetDebugger();
+                    Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
                     StreamSP error_sp = debugger.GetAsyncErrorStream ();
                     error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
                     wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
@@ -609,7 +590,7 @@
     virtual bool
     ShouldStop (Event *event_ptr)
     {
-        return m_thread.GetProcess().GetUnixSignals().GetShouldStop (m_value);
+        return m_thread.GetProcess()->GetUnixSignals().GetShouldStop (m_value);
     }
     
     
@@ -617,14 +598,14 @@
     virtual bool
     ShouldNotify (Event *event_ptr)
     {
-        return m_thread.GetProcess().GetUnixSignals().GetShouldNotify (m_value);
+        return m_thread.GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
     }
 
     
     virtual void
     WillResume (lldb::StateType resume_state)
     {
-        if (m_thread.GetProcess().GetUnixSignals().GetShouldSuppress(m_value) == false)
+        if (m_thread.GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
             m_thread.SetResumeSignal(m_value);
     }
 
@@ -634,7 +615,7 @@
         if (m_description.empty())
         {
             StreamString strm;
-            const char *signal_name = m_thread.GetProcess().GetUnixSignals().GetSignalAsCString (m_value);
+            const char *signal_name = m_thread.GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
             if (signal_name)
                 strm.Printf("signal %s", signal_name);
             else
diff --git a/source/Target/Thread.cpp b/source/Target/Thread.cpp
index 7423249..0db727e 100644
--- a/source/Target/Thread.cpp
+++ b/source/Target/Thread.cpp
@@ -43,12 +43,12 @@
 using namespace lldb;
 using namespace lldb_private;
 
-Thread::Thread (Process &process, lldb::tid_t tid) :
+Thread::Thread (const ProcessSP &process_sp, lldb::tid_t tid) :
     UserID (tid),
     ThreadInstanceSettings (GetSettingsController()),
-    m_process (process),
+    m_process_wp (process_sp),
     m_actual_stop_info_sp (),
-    m_index_id (process.GetNextThreadIndexID ()),
+    m_index_id (process_sp->GetNextThreadIndexID ()),
     m_reg_context_sp (),
     m_state (eStateUnloaded),
     m_state_mutex (Mutex::eMutexTypeRecursive),
@@ -99,9 +99,11 @@
         return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
     else
     {
-        if (m_actual_stop_info_sp 
+        ProcessSP process_sp (GetProcess());
+        if (process_sp 
+            && m_actual_stop_info_sp 
             && m_actual_stop_info_sp->IsValid()
-            && m_thread_stop_reason_stop_id == m_process.GetStopID())
+            && m_thread_stop_reason_stop_id == process_sp->GetStopID())
             return m_actual_stop_info_sp;
         else
             return GetPrivateStopReason ();
@@ -114,7 +116,11 @@
     m_actual_stop_info_sp = stop_info_sp;
     if (m_actual_stop_info_sp)
         m_actual_stop_info_sp->MakeStopInfoValid();
-    m_thread_stop_reason_stop_id = GetProcess().GetStopID();
+    ProcessSP process_sp (GetProcess());
+    if (process_sp)
+        m_thread_stop_reason_stop_id = process_sp->GetStopID();
+    else
+        m_thread_stop_reason_stop_id = UINT32_MAX;
 }
 
 void
@@ -138,8 +144,9 @@
         return false;
 
     saved_state.stop_info_sp = GetStopInfo();
-    saved_state.orig_stop_id = GetProcess().GetStopID();
-
+    ProcessSP process_sp (GetProcess());
+    if (process_sp)
+        saved_state.orig_stop_id = process_sp->GetStopID();
     return true;
 }
 
@@ -193,7 +200,7 @@
         // plan is.
 
         lldb::addr_t pc = GetRegisterContext()->GetPC();
-        BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
+        BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
         if (bp_site_sp && bp_site_sp->IsEnabled())
         {
             // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
@@ -234,7 +241,7 @@
     // the target, 'cause that slows down single stepping.  So assume that if we got to the point where
     // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
     // about the fact that we are resuming...
-        const uint32_t process_stop_id = GetProcess().GetStopID();
+        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()))
     {
@@ -1018,13 +1025,18 @@
 TargetSP
 Thread::CalculateTarget ()
 {
-    return m_process.CalculateTarget();
+    TargetSP target_sp;
+    ProcessSP process_sp(GetProcess());
+    if (process_sp)
+        target_sp = process_sp->CalculateTarget();
+    return target_sp;
+    
 }
 
 ProcessSP
 Thread::CalculateProcess ()
 {
-    return m_process.shared_from_this();
+    return GetProcess();
 }
 
 ThreadSP
@@ -1042,9 +1054,7 @@
 void
 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
 {
-    m_process.CalculateExecutionContext (exe_ctx);
-    exe_ctx.SetThreadPtr (this);
-    exe_ctx.SetFramePtr (NULL);
+    exe_ctx.SetContext (shared_from_this());
 }
 
 
@@ -1073,11 +1083,13 @@
 void
 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
 {
-    ExecutionContext exe_ctx;
+    ExecutionContext exe_ctx (shared_from_this());
+    Process *process = exe_ctx.GetProcessPtr();
+    if (process == NULL)
+        return;
+
     StackFrameSP frame_sp;
     SymbolContext frame_sc;
-    CalculateExecutionContext (exe_ctx);
-
     if (frame_idx != LLDB_INVALID_INDEX32)
     {
         frame_sp = GetStackFrameAtIndex (frame_idx);
@@ -1088,7 +1100,7 @@
         }
     }
 
-    const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
+    const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
     assert (thread_format);
     const char *end = NULL;
     Debugger::FormatPrompt (thread_format, 
@@ -1203,10 +1215,19 @@
 size_t
 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
 {
+    ExecutionContext exe_ctx (shared_from_this());
+    Target *target = exe_ctx.GetTargetPtr();
+    Process *process = exe_ctx.GetProcessPtr();
     size_t num_frames_shown = 0;
     strm.Indent();
-    strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' ');
-    if (GetProcess().GetTarget().GetDebugger().GetUseExternalEditor())
+    bool is_selected = false;
+    if (process)
+    {
+        if (process->GetThreadList().GetSelectedThread().get() == this)
+            is_selected = true;
+    }
+    strm.Printf("%c ", is_selected ? '*' : ' ');
+    if (target && target->GetDebugger().GetUseExternalEditor())
     {
         StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
         if (frame_sp)
@@ -1294,7 +1315,7 @@
 {
     if (m_unwinder_ap.get() == NULL)
     {
-        const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
+        const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
         const llvm::Triple::ArchType machine = target_arch.GetMachine();
         switch (machine)
         {
diff --git a/source/Target/ThreadPlanCallFunction.cpp b/source/Target/ThreadPlanCallFunction.cpp
index a518061..09bdd3d 100644
--- a/source/Target/ThreadPlanCallFunction.cpp
+++ b/source/Target/ThreadPlanCallFunction.cpp
@@ -54,20 +54,24 @@
 {
     SetOkayToDiscard (discard_on_error);
 
-    Process& process = thread.GetProcess();
-    Target& target = process.GetTarget();
-    const ABI *abi = process.GetABI().get();
+    ProcessSP process_sp (thread.GetProcess());
+    if (!process_sp)
+        return;
+    
+    const ABI *abi = process_sp->GetABI().get();
     
     if (!abi)
         return;
     
+    TargetSP target_sp (thread.CalculateTarget());
+
     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
     
     SetBreakpoints();
     
     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
     
-    Module *exe_module = target.GetExecutableModulePointer();
+    Module *exe_module = target_sp->GetExecutableModulePointer();
 
     if (exe_module == NULL)
     {
@@ -95,7 +99,7 @@
         }
     }
     
-    addr_t start_load_addr = m_start_addr.GetLoadAddress(&target);
+    addr_t start_load_addr = m_start_addr.GetLoadAddress (target_sp.get());
     
     // Checkpoint the thread state so we can restore it later.
     if (log && log->GetVerbose())
@@ -110,7 +114,7 @@
     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
     thread.SetStopInfoToNothing();
     
-    addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
+    addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress (target_sp.get());
         
     if (this_arg && cmd_arg)
     {
@@ -170,20 +174,24 @@
 {
     SetOkayToDiscard (discard_on_error);
     
-    Process& process = thread.GetProcess();
-    Target& target = process.GetTarget();
-    const ABI *abi = process.GetABI().get();
+    ProcessSP process_sp (thread.GetProcess());
+    if (!process_sp)
+        return;
+    
+    const ABI *abi = process_sp->GetABI().get();
     
     if (!abi)
         return;
-    
+
+    TargetSP target_sp (thread.CalculateTarget());
+
     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
     
     SetBreakpoints();
     
     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
     
-    Module *exe_module = target.GetExecutableModulePointer();
+    Module *exe_module = target_sp->GetExecutableModulePointer();
     
     if (exe_module == NULL)
     {
@@ -211,7 +219,7 @@
         }
     }
     
-    addr_t start_load_addr = m_start_addr.GetLoadAddress(&target);
+    addr_t start_load_addr = m_start_addr.GetLoadAddress(target_sp.get());
     
     // Checkpoint the thread state so we can restore it later.
     if (log && log->GetVerbose())
@@ -226,7 +234,7 @@
     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
     thread.SetStopInfoToNothing();
     
-    addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(&target);
+    addr_t FunctionLoadAddr = m_function_addr.GetLoadAddress(target_sp.get());
     
     if (!abi->PrepareTrivialCall (thread, 
                                   m_function_sp, 
@@ -285,7 +293,8 @@
     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
     if (!m_takedown_done)
     {
-        const ABI *abi = m_thread.GetProcess().GetABI().get();
+        ProcessSP process_sp (m_thread.GetProcess());
+        const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
         if (abi && m_return_type.IsValid())
         {
             m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type);
@@ -325,7 +334,8 @@
     }
     else
     {
-        s->Printf("Thread plan to call 0x%llx", m_function_addr.GetLoadAddress(&m_thread.GetProcess().GetTarget()));
+        TargetSP target_sp (m_thread.CalculateTarget());
+        s->Printf("Thread plan to call 0x%llx", m_function_addr.GetLoadAddress(target_sp.get()));
     }
 }
 
@@ -362,8 +372,11 @@
     
     if (m_real_stop_info_sp && m_real_stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
     {
+        ProcessSP process_sp (m_thread.CalculateProcess());
         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
-        BreakpointSiteSP bp_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(break_site_id);
+        BreakpointSiteSP bp_site_sp;
+        if (process_sp)
+            bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
         if (bp_site_sp)
         {
             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
@@ -475,13 +488,17 @@
 void
 ThreadPlanCallFunction::SetBreakpoints ()
 {
-    m_cxx_language_runtime = m_thread.GetProcess().GetLanguageRuntime(eLanguageTypeC_plus_plus);
-    m_objc_language_runtime = m_thread.GetProcess().GetLanguageRuntime(eLanguageTypeObjC);
+    ProcessSP process_sp (m_thread.CalculateProcess());
+    if (process_sp)
+    {
+        m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
+        m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
     
-    if (m_cxx_language_runtime)
-        m_cxx_language_runtime->SetExceptionBreakpoints();
-    if (m_objc_language_runtime)
-        m_objc_language_runtime->SetExceptionBreakpoints();
+        if (m_cxx_language_runtime)
+            m_cxx_language_runtime->SetExceptionBreakpoints();
+        if (m_objc_language_runtime)
+            m_objc_language_runtime->SetExceptionBreakpoints();
+    }
 }
 
 void
diff --git a/source/Target/ThreadPlanCallUserExpression.cpp b/source/Target/ThreadPlanCallUserExpression.cpp
index 6352edb..b46d6b4 100644
--- a/source/Target/ThreadPlanCallUserExpression.cpp
+++ b/source/Target/ThreadPlanCallUserExpression.cpp
@@ -64,7 +64,7 @@
 {
     StopInfoSP stop_info_sp = ThreadPlanCallFunction::GetRealStopInfo();
     lldb::addr_t addr = GetStopAddress();
-    DynamicCheckerFunctions *checkers = m_thread.GetProcess().GetDynamicCheckers();
+    DynamicCheckerFunctions *checkers = m_thread.GetProcess()->GetDynamicCheckers();
     StreamString s;
     
     if (checkers && checkers->DoCheckersExplainStop(addr, s))
diff --git a/source/Target/ThreadPlanRunToAddress.cpp b/source/Target/ThreadPlanRunToAddress.cpp
index 0779a1f..f406c37 100644
--- a/source/Target/ThreadPlanRunToAddress.cpp
+++ b/source/Target/ThreadPlanRunToAddress.cpp
@@ -39,7 +39,7 @@
     m_addresses (),
     m_break_ids ()
 {
-    m_addresses.push_back (address.GetOpcodeLoadAddress (&m_thread.GetProcess().GetTarget()));
+    m_addresses.push_back (address.GetOpcodeLoadAddress (m_thread.CalculateTarget().get()));
     SetInitialBreakpoints();
 }
 
@@ -54,7 +54,7 @@
     m_addresses (),
     m_break_ids ()
 {
-    m_addresses.push_back(m_thread.GetProcess().GetTarget().GetOpcodeLoadAddress(address));
+    m_addresses.push_back(m_thread.CalculateTarget()->GetOpcodeLoadAddress(address));
     SetInitialBreakpoints();
 }
 
@@ -71,7 +71,7 @@
 {
     // Convert all addressses into opcode addresses to make sure we set 
     // breakpoints at the correct address.
-    Target &target = thread.GetProcess().GetTarget();
+    Target &target = thread.GetProcess()->GetTarget();
     std::vector<lldb::addr_t>::iterator pos, end = m_addresses.end();
     for (pos = m_addresses.begin(); pos != end; ++pos)
         *pos = target.GetOpcodeLoadAddress (*pos);
@@ -88,7 +88,7 @@
     for (size_t i = 0; i < num_addresses; i++)
     {
         Breakpoint *breakpoint;
-        breakpoint = m_thread.GetProcess().GetTarget().CreateBreakpoint (m_addresses[i], true).get();
+        breakpoint = m_thread.CalculateTarget()->CreateBreakpoint (m_addresses[i], true).get();
         if (breakpoint != NULL)
         {
             m_break_ids[i] = breakpoint->GetID();
@@ -102,7 +102,7 @@
     size_t num_break_ids = m_break_ids.size();
     for (size_t i = 0; i <  num_break_ids; i++)
     {
-        m_thread.GetProcess().GetTarget().RemoveBreakpointByID (m_break_ids[i]);
+        m_thread.CalculateTarget()->RemoveBreakpointByID (m_break_ids[i]);
     }
 }
 
@@ -153,7 +153,7 @@
             
             s->Address(m_addresses[i], sizeof (addr_t));
             s->Printf (" using breakpoint: %d - ", m_break_ids[i]);
-            Breakpoint *breakpoint = m_thread.GetProcess().GetTarget().GetBreakpointByID (m_break_ids[i]).get();
+            Breakpoint *breakpoint = m_thread.CalculateTarget()->GetBreakpointByID (m_break_ids[i]).get();
             if (breakpoint)
                 breakpoint->Dump (s);
             else
@@ -236,7 +236,7 @@
         {
             if (m_break_ids[i] != LLDB_INVALID_BREAK_ID)
             {
-                m_thread.GetProcess().GetTarget().RemoveBreakpointByID (m_break_ids[i]);
+                m_thread.CalculateTarget()->RemoveBreakpointByID (m_break_ids[i]);
                 m_break_ids[i] = LLDB_INVALID_BREAK_ID;
             }
         }
diff --git a/source/Target/ThreadPlanStepInRange.cpp b/source/Target/ThreadPlanStepInRange.cpp
index 3455e90..094a7dc 100644
--- a/source/Target/ThreadPlanStepInRange.cpp
+++ b/source/Target/ThreadPlanStepInRange.cpp
@@ -108,7 +108,7 @@
     {
         StreamString s;
         s.Address (m_thread.GetRegisterContext()->GetPC(), 
-                   m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize());
+                   m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
         log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
     }
 
diff --git a/source/Target/ThreadPlanStepInstruction.cpp b/source/Target/ThreadPlanStepInstruction.cpp
index de23c22..ac337cb 100644
--- a/source/Target/ThreadPlanStepInstruction.cpp
+++ b/source/Target/ThreadPlanStepInstruction.cpp
@@ -123,10 +123,10 @@
                     StreamString s;
                     s.PutCString ("Stepped in to: ");
                     addr_t stop_addr = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
-                    s.Address (stop_addr, m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize());
+                    s.Address (stop_addr, m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
                     s.PutCString (" stepping out to: ");
                     addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
-                    s.Address (return_addr, m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize());
+                    s.Address (return_addr, m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
                     log->Printf("%s.", s.GetData());
                 }
                 m_thread.QueueThreadPlanForStepOut(false, NULL, true, m_stop_other_threads, eVoteNo, eVoteNoOpinion, 0);
diff --git a/source/Target/ThreadPlanStepOut.cpp b/source/Target/ThreadPlanStepOut.cpp
index 485db25..e8aac1f 100644
--- a/source/Target/ThreadPlanStepOut.cpp
+++ b/source/Target/ThreadPlanStepOut.cpp
@@ -84,8 +84,8 @@
         // Find the return address and set a breakpoint there:
         // FIXME - can we do this more securely if we know first_insn?
 
-        m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess().GetTarget());
-        Breakpoint *return_bp = m_thread.GetProcess().GetTarget().CreateBreakpoint (m_return_addr, true).get();
+        m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
+        Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true).get();
         if (return_bp != NULL)
         {
             return_bp->SetThreadID(m_thread.GetID());
@@ -116,7 +116,7 @@
 ThreadPlanStepOut::~ThreadPlanStepOut ()
 {
     if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
-        m_thread.GetProcess().GetTarget().RemoveBreakpointByID(m_return_bp_id);
+        m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
 }
 
 void
@@ -191,7 +191,7 @@
         case eStopReasonBreakpoint:
         {
             // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
-            BreakpointSiteSP site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
+            BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
             if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
             {
                 const uint32_t num_frames = m_thread.GetStackFrameCount();
@@ -301,7 +301,7 @@
 
     if (current_plan)
     {
-        Breakpoint *return_bp = m_thread.GetProcess().GetTarget().GetBreakpointByID(m_return_bp_id).get();
+        Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
         if (return_bp != NULL)
             return_bp->SetEnabled (true);
     }
@@ -313,7 +313,7 @@
 {
     if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
     {
-        Breakpoint *return_bp = m_thread.GetProcess().GetTarget().GetBreakpointByID(m_return_bp_id).get();
+        Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
         if (return_bp != NULL)
             return_bp->SetEnabled (false);
     }
@@ -337,7 +337,7 @@
             log->Printf("Completed step out plan.");
         if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
         {
-            m_thread.GetProcess().GetTarget().RemoveBreakpointByID (m_return_bp_id);
+            m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
             m_return_bp_id = LLDB_INVALID_BREAK_ID;
         }
         
@@ -424,7 +424,7 @@
         {
             ClangASTType ast_type (return_type->GetClangAST(), return_clang_type);
             
-            lldb::ABISP abi_sp = m_thread.GetProcess().GetABI();
+            lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
             if (abi_sp)
             {
                 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, ast_type);
diff --git a/source/Target/ThreadPlanStepOverBreakpoint.cpp b/source/Target/ThreadPlanStepOverBreakpoint.cpp
index c30219c..f5d9e18 100644
--- a/source/Target/ThreadPlanStepOverBreakpoint.cpp
+++ b/source/Target/ThreadPlanStepOverBreakpoint.cpp
@@ -38,7 +38,7 @@
 
 {
     m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
-    m_breakpoint_site_id =  m_thread.GetProcess().GetBreakpointSiteList().FindIDByAddress (m_breakpoint_addr);
+    m_breakpoint_site_id =  m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress (m_breakpoint_addr);
 }
 
 ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint ()
@@ -88,9 +88,9 @@
 
     if (current_plan)
     {
-        BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
+        BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
         if (bp_site_sp  && bp_site_sp->IsEnabled())
-            m_thread.GetProcess().DisableBreakpoint (bp_site_sp.get());
+            m_thread.GetProcess()->DisableBreakpoint (bp_site_sp.get());
     }
     return true;
 }
@@ -98,9 +98,9 @@
 bool
 ThreadPlanStepOverBreakpoint::WillStop ()
 {
-    BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
+    BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
     if (bp_site_sp)
-        m_thread.GetProcess().EnableBreakpoint (bp_site_sp.get());
+        m_thread.GetProcess()->EnableBreakpoint (bp_site_sp.get());
     return true;
 }
 
@@ -121,9 +121,9 @@
         if (log)
             log->Printf("Completed step over breakpoint plan.");
         // Otherwise, re-enable the breakpoint we were stepping over, and we're done.
-        BreakpointSiteSP bp_site_sp (m_thread.GetProcess().GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
+        BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
         if (bp_site_sp)
-            m_thread.GetProcess().EnableBreakpoint (bp_site_sp.get());
+            m_thread.GetProcess()->EnableBreakpoint (bp_site_sp.get());
         ThreadPlan::MischiefManaged ();
         return true;
     }
diff --git a/source/Target/ThreadPlanStepOverRange.cpp b/source/Target/ThreadPlanStepOverRange.cpp
index 0df0156..9687c65 100644
--- a/source/Target/ThreadPlanStepOverRange.cpp
+++ b/source/Target/ThreadPlanStepOverRange.cpp
@@ -96,7 +96,7 @@
     {
         StreamString s;
         s.Address (m_thread.GetRegisterContext()->GetPC(), 
-                   m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize());
+                   m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
         log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
     }
     
diff --git a/source/Target/ThreadPlanStepRange.cpp b/source/Target/ThreadPlanStepRange.cpp
index 5bde935..01803a1 100644
--- a/source/Target/ThreadPlanStepRange.cpp
+++ b/source/Target/ThreadPlanStepRange.cpp
@@ -89,14 +89,14 @@
     size_t num_ranges = m_address_ranges.size();
     if (num_ranges == 1)
     {
-        m_address_ranges[0].Dump (s, &m_thread.GetProcess().GetTarget(), Address::DumpStyleLoadAddress);
+        m_address_ranges[0].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
     }
     else
     {
         for (size_t i = 0; i < num_ranges; i++)
         {
             s->PutCString("%d: ");
-            m_address_ranges[i].Dump (s, &m_thread.GetProcess().GetTarget(), Address::DumpStyleLoadAddress);
+            m_address_ranges[i].Dump (s, m_thread.CalculateTarget().get(), Address::DumpStyleLoadAddress);
         }
     }
 }
@@ -112,7 +112,7 @@
     size_t num_ranges = m_address_ranges.size();
     for (size_t i = 0; i < num_ranges; i++)
     {
-        ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, &m_thread.GetProcess().GetTarget());
+        ret_value = m_address_ranges[i].ContainsLoadAddress(pc_load_addr, m_thread.CalculateTarget().get());
         if (ret_value)
             break;
     }
@@ -136,13 +136,13 @@
                     {
                         StreamString s;
                         m_addr_context.line_entry.range.Dump (&s, 
-                                                              &m_thread.GetProcess().GetTarget(), 
+                                                              m_thread.CalculateTarget().get(), 
                                                               Address::DumpStyleLoadAddress);
 
                         log->Printf ("Step range plan stepped to another range of same line: %s", s.GetData());
                     }
                 }
-                else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(&m_thread.GetProcess().GetTarget())
+                else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(m_thread.CalculateTarget().get())
                          != pc_load_addr)
                 {
                     // Another thing that sometimes happens here is that we step out of one line into the MIDDLE of another
@@ -157,7 +157,7 @@
                     {
                         StreamString s;
                         m_addr_context.line_entry.range.Dump (&s, 
-                                                              &m_thread.GetProcess().GetTarget(), 
+                                                              m_thread.CalculateTarget().get(), 
                                                               Address::DumpStyleLoadAddress);
 
                         log->Printf ("Step range plan stepped to the middle of new line(%d): %s, continuing to clear this line.", 
@@ -184,11 +184,11 @@
     lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
     if (m_addr_context.function != NULL)
     {
-        return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, &m_thread.GetProcess().GetTarget());
+        return m_addr_context.function->GetAddressRange().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
     }
     else if (m_addr_context.symbol != NULL)
     {
-        return m_addr_context.symbol->GetAddressRangeRef().ContainsLoadAddress (cur_pc, &m_thread.GetProcess().GetTarget());
+        return m_addr_context.symbol->GetAddressRangeRef().ContainsLoadAddress (cur_pc, m_thread.CalculateTarget().get());
     }
     return false;
 }
diff --git a/source/Target/ThreadPlanStepThrough.cpp b/source/Target/ThreadPlanStepThrough.cpp
index 606cfd1..cb7bf99 100644
--- a/source/Target/ThreadPlanStepThrough.cpp
+++ b/source/Target/ThreadPlanStepThrough.cpp
@@ -56,8 +56,8 @@
         
         if (return_frame_sp)
         {
-            m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess().GetTarget());
-            Breakpoint *return_bp = m_thread.GetProcess().GetTarget().CreateBreakpoint (m_backstop_addr, true).get();
+            m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(m_thread.CalculateTarget().get());
+            Breakpoint *return_bp = m_thread.GetProcess()->GetTarget().CreateBreakpoint (m_backstop_addr, true).get();
             if (return_bp != NULL)
             {
                 return_bp->SetThreadID(m_thread.GetID());
@@ -76,7 +76,7 @@
 {
     if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
     {
-        m_thread.GetProcess().GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id);
+        m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id);
         m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
     }
 }
@@ -91,11 +91,11 @@
 void
 ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC()
 {
-    m_sub_plan_sp = m_thread.GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
+    m_sub_plan_sp = m_thread.GetProcess()->GetDynamicLoader()->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
     // If that didn't come up with anything, try the ObjC runtime plugin:
     if (!m_sub_plan_sp.get())
     {
-        ObjCLanguageRuntime *objc_runtime = m_thread.GetProcess().GetObjCLanguageRuntime();
+        ObjCLanguageRuntime *objc_runtime = m_thread.GetProcess()->GetObjCLanguageRuntime();
         if (objc_runtime)
             m_sub_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
     }
@@ -244,7 +244,7 @@
         ThreadPlan::MischiefManaged ();
         if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
         {
-            m_thread.GetProcess().GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id);
+            m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id);
             m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
         }
         return true;
@@ -258,7 +258,7 @@
     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
     {
         break_id_t stop_value = (break_id_t) stop_info_sp->GetValue();
-        BreakpointSiteSP cur_site_sp = m_thread.GetProcess().GetBreakpointSiteList().FindByID(stop_value);
+        BreakpointSiteSP cur_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value);
         if (cur_site_sp && cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id))
         {
             size_t current_stack_depth = m_thread.GetStackFrameCount();
diff --git a/source/Target/ThreadPlanStepUntil.cpp b/source/Target/ThreadPlanStepUntil.cpp
index f54bf46..1d5f433 100644
--- a/source/Target/ThreadPlanStepUntil.cpp
+++ b/source/Target/ThreadPlanStepUntil.cpp
@@ -55,7 +55,7 @@
 
     SetOkayToDiscard(true);
     // Stash away our "until" addresses:
-    Target &target = m_thread.GetProcess().GetTarget();
+    TargetSP target_sp (m_thread.CalculateTarget());
 
     StackFrameSP frame_sp (m_thread.GetStackFrameAtIndex (frame_idx));
     if (frame_sp)
@@ -71,7 +71,7 @@
         {
             // TODO: add inline functionality
             m_return_addr = return_frame_sp->GetStackID().GetPC();
-            Breakpoint *return_bp = target.CreateBreakpoint (m_return_addr, true).get();
+            Breakpoint *return_bp = target_sp->CreateBreakpoint (m_return_addr, true).get();
             if (return_bp != NULL)
             {
                 return_bp->SetThreadID(thread_id);
@@ -84,7 +84,7 @@
         // Now set breakpoints on all our return addresses:
         for (int i = 0; i < num_addresses; i++)
         {
-            Breakpoint *until_bp = target.CreateBreakpoint (address_list[i], true).get();
+            Breakpoint *until_bp = target_sp->CreateBreakpoint (address_list[i], true).get();
             if (until_bp != NULL)
             {
                 until_bp->SetThreadID(thread_id);
@@ -106,17 +106,20 @@
 void
 ThreadPlanStepUntil::Clear()
 {
-    Target &target = m_thread.GetProcess().GetTarget();
-    if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
+    TargetSP target_sp (m_thread.CalculateTarget());
+    if (target_sp)
     {
-        target.RemoveBreakpointByID(m_return_bp_id);
-        m_return_bp_id = LLDB_INVALID_BREAK_ID;
-    }
+        if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
+        {
+            target_sp->RemoveBreakpointByID(m_return_bp_id);
+            m_return_bp_id = LLDB_INVALID_BREAK_ID;
+        }
 
-    until_collection::iterator pos, end = m_until_points.end();
-    for (pos = m_until_points.begin(); pos != end; pos++)
-    {
-        target.RemoveBreakpointByID((*pos).second);
+        until_collection::iterator pos, end = m_until_points.end();
+        for (pos = m_until_points.begin(); pos != end; pos++)
+        {
+            target_sp->RemoveBreakpointByID((*pos).second);
+        }
     }
     m_until_points.clear();
 }
@@ -187,7 +190,7 @@
             case eStopReasonBreakpoint:
             {
                 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
-                BreakpointSiteSP this_site = m_thread.GetProcess().GetBreakpointSiteList().FindByID (stop_info_sp->GetValue());
+                BreakpointSiteSP this_site = m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue());
                 if (!this_site)
                 {
                     m_explains_stop = false;
@@ -305,17 +308,20 @@
     ThreadPlan::WillResume (resume_state, current_plan);
     if (current_plan)
     {
-        Target &target = m_thread.GetProcess().GetTarget();
-        Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
-        if (return_bp != NULL)
-            return_bp->SetEnabled (true);
-
-        until_collection::iterator pos, end = m_until_points.end();
-        for (pos = m_until_points.begin(); pos != end; pos++)
+        TargetSP target_sp (m_thread.CalculateTarget());
+        if (target_sp)
         {
-            Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
-            if (until_bp != NULL)
-                until_bp->SetEnabled (true);
+            Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
+            if (return_bp != NULL)
+                return_bp->SetEnabled (true);
+
+            until_collection::iterator pos, end = m_until_points.end();
+            for (pos = m_until_points.begin(); pos != end; pos++)
+            {
+                Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
+                if (until_bp != NULL)
+                    until_bp->SetEnabled (true);
+            }
         }
     }
     
@@ -328,17 +334,20 @@
 bool
 ThreadPlanStepUntil::WillStop ()
 {
-    Target &target = m_thread.GetProcess().GetTarget();
-    Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
-    if (return_bp != NULL)
-        return_bp->SetEnabled (false);
-
-    until_collection::iterator pos, end = m_until_points.end();
-    for (pos = m_until_points.begin(); pos != end; pos++)
+    TargetSP target_sp (m_thread.CalculateTarget());
+    if (target_sp)
     {
-        Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
-        if (until_bp != NULL)
-            until_bp->SetEnabled (false);
+        Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
+        if (return_bp != NULL)
+            return_bp->SetEnabled (false);
+
+        until_collection::iterator pos, end = m_until_points.end();
+        for (pos = m_until_points.begin(); pos != end; pos++)
+        {
+            Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
+            if (until_bp != NULL)
+                until_bp->SetEnabled (false);
+        }
     }
     return true;
 }
diff --git a/source/Target/ThreadPlanTestCondition.cpp b/source/Target/ThreadPlanTestCondition.cpp
index b9f8a27..589d97b 100644
--- a/source/Target/ThreadPlanTestCondition.cpp
+++ b/source/Target/ThreadPlanTestCondition.cpp
@@ -119,7 +119,7 @@
     else
     {
         // Now we have to change the event to a breakpoint event and mark it up appropriately:
-        Process::ProcessEventData *new_data = new Process::ProcessEventData (m_thread.GetProcess().shared_from_this(), eStateStopped);
+        Process::ProcessEventData *new_data = new Process::ProcessEventData (m_thread.GetProcess(), eStateStopped);
         event_ptr->SetData(new_data);
         event_ptr->SetType(Process::eBroadcastBitStateChanged);
         SetStopInfo(StopInfo::CreateStopReasonWithBreakpointSiteID (m_thread, 
diff --git a/source/Target/ThreadPlanTracer.cpp b/source/Target/ThreadPlanTracer.cpp
index 1a3f0d4..ee5b801 100644
--- a/source/Target/ThreadPlanTracer.cpp
+++ b/source/Target/ThreadPlanTracer.cpp
@@ -55,7 +55,12 @@
     if (m_stream_sp.get())
         return m_stream_sp.get();
     else
-        return &m_thread.GetProcess().GetTarget().GetDebugger().GetOutputStream();
+    {
+        TargetSP target_sp (m_thread.CalculateTarget());
+        if (target_sp)
+            return &target_sp->GetDebugger().GetOutputStream();
+    }
+    return NULL;
 }
 
 void 
@@ -109,7 +114,7 @@
 ThreadPlanAssemblyTracer::GetDisassembler ()
 {
     if (m_disassembler_ap.get() == NULL)
-        m_disassembler_ap.reset(Disassembler::FindPlugin(m_thread.GetProcess().GetTarget().GetArchitecture(), NULL));
+        m_disassembler_ap.reset(Disassembler::FindPlugin(m_thread.GetProcess()->GetTarget().GetArchitecture(), NULL));
     return m_disassembler_ap.get();
 }
 
@@ -118,13 +123,16 @@
 {
     if (!m_intptr_type.IsValid ())
     {
-        Target &target = m_thread.GetProcess().GetTarget();
-        Module *exe_module = target.GetExecutableModulePointer();
-        
-        if (exe_module)
+        TargetSP target_sp (m_thread.CalculateTarget());
+        if (target_sp)
         {
-            m_intptr_type = TypeFromUser(exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, m_thread.GetProcess().GetAddressByteSize() * 8),
-                                         exe_module->GetClangASTContext().getASTContext());
+            Module *exe_module = target_sp->GetExecutableModulePointer();
+        
+            if (exe_module)
+            {
+                m_intptr_type = TypeFromUser(exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, target_sp->GetArchitecture().GetAddressByteSize() * 8),
+                                             exe_module->GetClangASTContext().getASTContext());
+            }
         }        
     }
     return m_intptr_type;
@@ -173,11 +181,11 @@
     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
     
     lldb::addr_t pc = reg_ctx->GetPC();
-    Process &process = m_thread.GetProcess();
+    ProcessSP process_sp (m_thread.GetProcess());
     Address pc_addr;
     bool addr_valid = false;
     uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
-    addr_valid = process.GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
+    addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
     
     pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
     stream->PutCString (" ");
@@ -186,13 +194,13 @@
     if (disassembler)
     {        
         Error err;
-        process.ReadMemory(pc, buffer, sizeof(buffer), err);
+        process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
         
         if (err.Success())
         {
             DataExtractor extractor(buffer, sizeof(buffer), 
-                                    process.GetByteOrder(), 
-                                    process.GetAddressByteSize());
+                                    process_sp->GetByteOrder(), 
+                                    process_sp->GetAddressByteSize());
             
             if (addr_valid)
                 disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false);
@@ -217,7 +225,7 @@
         }
     }
     
-    const ABI *abi = process.GetABI().get();
+    const ABI *abi = process_sp->GetABI().get();
     TypeFromUser intptr_type = GetIntPointerType();
     
     if (abi && intptr_type.IsValid())