Don't try to use "OkayToDiscard" to mean BOTH this plan is a user plan or not AND unwind on error.

rdar://problem/11419156


git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@156627 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/ClangFunction.cpp b/source/Expression/ClangFunction.cpp
index a6344f9..d75f5eb 100644
--- a/source/Expression/ClangFunction.cpp
+++ b/source/Expression/ClangFunction.cpp
@@ -410,6 +410,8 @@
                                                        discard_on_error,
                                                        this_arg,
                                                        cmd_arg);
+    new_plan->SetIsMasterPlan(true);
+    new_plan->SetOkayToDiscard (false);
     return new_plan;
 }
 
diff --git a/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp b/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp
index e561a9f..7ffac10 100644
--- a/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp
+++ b/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp
@@ -65,12 +65,8 @@
 void
 AppleThreadPlanStepThroughObjCTrampoline::DidPush ()
 {
-//    StreamString errors;
-//    ExecutionContext exc_ctx;
-//    m_thread.CalculateExecutionContext(exc_ctx);
-//    m_func_sp.reset(m_impl_function->GetThreadPlanToCallFunction (exc_ctx, m_args_addr, errors, m_stop_others));
-//    m_func_sp->SetPrivate(true);
-//    m_thread.QueueThreadPlan (m_func_sp, false);
+    // Setting up the memory space for the called function text might require allocations,
+    // i.e. a nested function call.  This needs to be done as a PreResumeAction.
     m_thread.GetProcess()->AddPreResumeAction (PreResumeInitializeClangFunction, (void *) this);
 }
 
@@ -91,6 +87,7 @@
         m_thread.CalculateExecutionContext(exc_ctx);
         m_func_sp.reset(m_impl_function->GetThreadPlanToCallFunction (exc_ctx, m_args_addr, errors, m_stop_others));
         m_func_sp->SetPrivate(true);
+        m_func_sp->SetOkayToDiscard(true);
         m_thread.QueueThreadPlan (m_func_sp, false);
     }
     return true;
diff --git a/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp b/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
index 376c41b..a68f7d3 100644
--- a/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
+++ b/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
@@ -90,6 +90,10 @@
                 if (call_plan_sp)
                 {
                     StreamFile error_strm;
+                    // This plan is a utility plan, so set it to discard itself when done.
+                    call_plan_sp->SetIsMasterPlan (true);
+                    call_plan_sp->SetOkayToDiscard(true);
+                    
                     StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
                     if (frame)
                     {
@@ -164,6 +168,10 @@
                if (call_plan_sp)
                {
                    StreamFile error_strm;
+                   // This plan is a utility plan, so set it to discard itself when done.
+                   call_plan_sp->SetIsMasterPlan (true);
+                   call_plan_sp->SetOkayToDiscard(true);
+                   
                    StackFrame *frame = thread->GetStackFrameAtIndex (0).get();
                    if (frame)
                    {
diff --git a/source/Target/Thread.cpp b/source/Target/Thread.cpp
index 0351213..822015a 100644
--- a/source/Target/Thread.cpp
+++ b/source/Target/Thread.cpp
@@ -447,7 +447,6 @@
                             break;
                         }
                     }
-
                 }
                 else
                 {
diff --git a/source/Target/ThreadPlanCallFunction.cpp b/source/Target/ThreadPlanCallFunction.cpp
index 7ebe879..8e91e8b 100644
--- a/source/Target/ThreadPlanCallFunction.cpp
+++ b/source/Target/ThreadPlanCallFunction.cpp
@@ -36,15 +36,12 @@
 //----------------------------------------------------------------------
 bool
 ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
-                                          bool discard_on_error,
                                           ABI *& abi,
                                           lldb::addr_t &start_load_addr,
                                           lldb::addr_t &function_load_addr)
 {
-    // Call function thread plans need to be master plans so that they can potentially stay on the stack when
-    // a breakpoint is hit during the function call.
     SetIsMasterPlan (true);
-    SetOkayToDiscard (discard_on_error);
+    SetOkayToDiscard (false);
 
     ProcessSP process_sp (thread.GetProcess());
     if (!process_sp)
@@ -136,12 +133,13 @@
     m_function_sp (NULL),
     m_return_type (return_type),
     m_takedown_done (false),
-    m_stop_address (LLDB_INVALID_ADDRESS)
+    m_stop_address (LLDB_INVALID_ADDRESS),
+    m_discard_on_error (discard_on_error)
 {
     lldb::addr_t start_load_addr;
     ABI *abi;
     lldb::addr_t function_load_addr;
-    if (!ConstructorSetup (thread, discard_on_error, abi, start_load_addr, function_load_addr))
+    if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
         return;
         
     if (this_arg && cmd_arg)
@@ -204,7 +202,7 @@
     lldb::addr_t start_load_addr;
     ABI *abi;
     lldb::addr_t function_load_addr;
-    if (!ConstructorSetup (thread, discard_on_error, abi, start_load_addr, function_load_addr))
+    if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
         return;
     
     if (!abi->PrepareTrivialCall (thread, 
@@ -354,7 +352,7 @@
         return true;
     
     // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
-    if (!OkayToDiscard())
+    if (!m_discard_on_error)
         return false;
             
     // Otherwise, check the case where we stopped for an internal breakpoint, in that case, continue on.
@@ -386,7 +384,13 @@
                 return false;
         }
         
-        return OkayToDiscard();
+        if (m_discard_on_error)
+        {
+            DoTakedown(false);
+            return true;
+        }
+        else
+            return false;
     }
     else
     {
@@ -396,7 +400,7 @@
         // explain the stop.
         if (m_subplan_sp != NULL)
         {
-            if (OkayToDiscard())
+            if (m_discard_on_error)
             {
                 DoTakedown(false);
                 return true;
diff --git a/source/Target/ThreadPlanCallUserExpression.cpp b/source/Target/ThreadPlanCallUserExpression.cpp
index b46d6b4..f739d2a 100644
--- a/source/Target/ThreadPlanCallUserExpression.cpp
+++ b/source/Target/ThreadPlanCallUserExpression.cpp
@@ -47,6 +47,9 @@
     ThreadPlanCallFunction (thread, function, ClangASTType(), arg, stop_other_threads, discard_on_error, this_arg, cmd_arg),
     m_user_expression_sp (user_expression_sp)
 {
+    // User expressions are generally "User generated" so we should set them up to stop when done.
+    SetIsMasterPlan (true);
+    SetOkayToDiscard(false);
 }
 
 ThreadPlanCallUserExpression::~ThreadPlanCallUserExpression ()