130fdc8d8SChris Lattner //===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanCallFunction.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1546d005dbSJim Ingham 
1630fdc8d8SChris Lattner // Project includes
1740d871faSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
1840d871faSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Address.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Log.h"
211f746071SGreg Clayton #include "lldb/Core/Module.h"
2230fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
231f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
2432abc6edSZachary Turner #include "lldb/Target/ABI.h"
25f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2730fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2840d871faSJim Ingham #include "lldb/Target/StopInfo.h"
2930fdc8d8SChris Lattner #include "lldb/Target/Target.h"
3030fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
3130fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
3230fdc8d8SChris Lattner 
3330fdc8d8SChris Lattner using namespace lldb;
3430fdc8d8SChris Lattner using namespace lldb_private;
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
3730fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
3830fdc8d8SChris Lattner //----------------------------------------------------------------------
390092c8ebSJim Ingham bool
400092c8ebSJim Ingham ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
410092c8ebSJim Ingham                                           ABI *& abi,
420092c8ebSJim Ingham                                           lldb::addr_t &start_load_addr,
430092c8ebSJim Ingham                                           lldb::addr_t &function_load_addr)
440092c8ebSJim Ingham {
450092c8ebSJim Ingham     SetIsMasterPlan (true);
46923886ceSJim Ingham     SetOkayToDiscard (false);
47327c267aSAndrew Kaylor     SetPrivate (true);
480092c8ebSJim Ingham 
490092c8ebSJim Ingham     ProcessSP process_sp (thread.GetProcess());
500092c8ebSJim Ingham     if (!process_sp)
510092c8ebSJim Ingham         return false;
520092c8ebSJim Ingham 
530092c8ebSJim Ingham     abi = process_sp->GetABI().get();
540092c8ebSJim Ingham 
550092c8ebSJim Ingham     if (!abi)
560092c8ebSJim Ingham         return false;
570092c8ebSJim Ingham 
585160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
590092c8ebSJim Ingham 
600092c8ebSJim Ingham     SetBreakpoints();
610092c8ebSJim Ingham 
620092c8ebSJim Ingham     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
630092c8ebSJim Ingham     // If we can't read memory at the point of the process where we are planning to put our function, we're
640092c8ebSJim Ingham     // not going to get any further...
650092c8ebSJim Ingham     Error error;
660092c8ebSJim Ingham     process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
670092c8ebSJim Ingham     if (!error.Success())
680092c8ebSJim Ingham     {
69ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
700092c8ebSJim Ingham         if (log)
71324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
72324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
73324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
740092c8ebSJim Ingham         return false;
750092c8ebSJim Ingham     }
760092c8ebSJim Ingham 
776fbc48bcSJim Ingham     Module *exe_module = GetTarget().GetExecutableModulePointer();
780092c8ebSJim Ingham 
790092c8ebSJim Ingham     if (exe_module == NULL)
800092c8ebSJim Ingham     {
81ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Can't execute code without an executable module.");
820092c8ebSJim Ingham         if (log)
83324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
84324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
85324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
860092c8ebSJim Ingham         return false;
870092c8ebSJim Ingham     }
880092c8ebSJim Ingham     else
890092c8ebSJim Ingham     {
900092c8ebSJim Ingham         ObjectFile *objectFile = exe_module->GetObjectFile();
910092c8ebSJim Ingham         if (!objectFile)
920092c8ebSJim Ingham         {
93ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
94ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
95ea06f3bfSJim Ingham 
960092c8ebSJim Ingham             if (log)
97324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
98324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
99324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1000092c8ebSJim Ingham             return false;
1010092c8ebSJim Ingham         }
102ea06f3bfSJim Ingham 
1030092c8ebSJim Ingham         m_start_addr = objectFile->GetEntryPointAddress();
1040092c8ebSJim Ingham         if (!m_start_addr.IsValid())
1050092c8ebSJim Ingham         {
106ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
107ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
1080092c8ebSJim Ingham             if (log)
109324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
110324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
111324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1120092c8ebSJim Ingham             return false;
1130092c8ebSJim Ingham         }
1140092c8ebSJim Ingham     }
1150092c8ebSJim Ingham 
1166fbc48bcSJim Ingham     start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
1170092c8ebSJim Ingham 
1180092c8ebSJim Ingham     // Checkpoint the thread state so we can restore it later.
1190092c8ebSJim Ingham     if (log && log->GetVerbose())
1200092c8ebSJim Ingham         ReportRegisterState ("About to checkpoint thread before function call.  Original register state was:");
1210092c8ebSJim Ingham 
1220092c8ebSJim Ingham     if (!thread.CheckpointThreadState (m_stored_thread_state))
1230092c8ebSJim Ingham     {
124ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
1250092c8ebSJim Ingham         if (log)
126324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
127324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
128324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
1290092c8ebSJim Ingham         return false;
1300092c8ebSJim Ingham     }
1316fbc48bcSJim Ingham     function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
1320092c8ebSJim Ingham 
1330092c8ebSJim Ingham     return true;
1340092c8ebSJim Ingham }
13530fdc8d8SChris Lattner 
13630fdc8d8SChris Lattner ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
13700049b8bSMatt Kopec                                                 const Address &function,
138ef651600SJim Ingham                                                 const ClangASTType &return_type,
139a464f3d4SSean Callanan                                                 llvm::ArrayRef<addr_t> args,
140a464f3d4SSean Callanan                                                 const EvaluateExpressionOptions &options) :
141b01e742aSJim Ingham     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
14230fdc8d8SChris Lattner     m_valid (false),
1436fbc48bcSJim Ingham     m_stop_other_threads (options.GetStopOthers()),
1446fbc48bcSJim Ingham     m_unwind_on_error (options.DoesUnwindOnError()),
1456fbc48bcSJim Ingham     m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
1466fbc48bcSJim Ingham     m_debug_execution (options.GetDebug()),
1476fbc48bcSJim Ingham     m_trap_exceptions (options.GetTrapExceptions()),
14816e0c686SJim Ingham     m_function_addr (function),
149b4cb0be3SFilipe Cabecinhas     m_function_sp (0),
150ef651600SJim Ingham     m_return_type (return_type),
151ce553d88SJim Ingham     m_takedown_done (false),
1526fbc48bcSJim Ingham     m_should_clear_objc_exception_bp(false),
1536fbc48bcSJim Ingham     m_should_clear_cxx_exception_bp (false),
1546fbc48bcSJim Ingham     m_stop_address (LLDB_INVALID_ADDRESS)
15530fdc8d8SChris Lattner {
1560092c8ebSJim Ingham     lldb::addr_t start_load_addr;
1570092c8ebSJim Ingham     ABI *abi;
1580092c8ebSJim Ingham     lldb::addr_t function_load_addr;
159923886ceSJim Ingham     if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
1601ac04c30SGreg Clayton         return;
1611ac04c30SGreg Clayton 
16230fdc8d8SChris Lattner     if (!abi->PrepareTrivialCall(thread,
163e359d9b7SSean Callanan                                  m_function_sp,
1640092c8ebSJim Ingham                                  function_load_addr,
1652a48f525SGreg Clayton                                  start_load_addr,
166a464f3d4SSean Callanan                                  args))
16730fdc8d8SChris Lattner         return;
16830fdc8d8SChris Lattner 
1699da3683cSJim Ingham     ReportRegisterState ("Function call was set up.  Register state was:");
1709da3683cSJim Ingham 
1719da3683cSJim Ingham     m_valid = true;
1729da3683cSJim Ingham }
1739da3683cSJim Ingham 
1749da3683cSJim Ingham ThreadPlanCallFunction::~ThreadPlanCallFunction ()
1759da3683cSJim Ingham {
1760161b49cSJim Ingham     DoTakedown(PlanSucceeded());
1779da3683cSJim Ingham }
1789da3683cSJim Ingham 
1799da3683cSJim Ingham void
1809da3683cSJim Ingham ThreadPlanCallFunction::ReportRegisterState (const char *message)
1819da3683cSJim Ingham {
1825160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
183ece96492SSean Callanan     if (log)
184ece96492SSean Callanan     {
185af247d7bSGreg Clayton         StreamString strm;
1865ccbd294SGreg Clayton         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
187ece96492SSean Callanan 
1889da3683cSJim Ingham         log->PutCString(message);
189ece96492SSean Callanan 
190af247d7bSGreg Clayton         RegisterValue reg_value;
191ece96492SSean Callanan 
192af247d7bSGreg Clayton         for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
193af247d7bSGreg Clayton              reg_idx < num_registers;
194af247d7bSGreg Clayton              ++reg_idx)
195af247d7bSGreg Clayton         {
196af247d7bSGreg Clayton             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
197af247d7bSGreg Clayton             if (reg_ctx->ReadRegister(reg_info, reg_value))
198af247d7bSGreg Clayton             {
199af247d7bSGreg Clayton                 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
200af247d7bSGreg Clayton                 strm.EOL();
201ece96492SSean Callanan             }
202ece96492SSean Callanan         }
203af247d7bSGreg Clayton         log->PutCString(strm.GetData());
204af247d7bSGreg Clayton     }
20510af7c43SSean Callanan }
20610af7c43SSean Callanan 
20710af7c43SSean Callanan void
20818de2fdcSJim Ingham ThreadPlanCallFunction::DoTakedown (bool success)
20910af7c43SSean Callanan {
2105160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
21187d0e618SJim Ingham 
21287d0e618SJim Ingham     if (!m_valid)
21387d0e618SJim Ingham     {
21487d0e618SJim Ingham         //Don't call DoTakedown if we were never valid to begin with.
21587d0e618SJim Ingham         if (log)
216324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
217324a1036SSaleem Abdulrasool                          static_cast<void*>(this));
21887d0e618SJim Ingham         return;
21987d0e618SJim Ingham     }
22087d0e618SJim Ingham 
2219da3683cSJim Ingham     if (!m_takedown_done)
22277787033SJim Ingham     {
22318de2fdcSJim Ingham         if (success)
22418de2fdcSJim Ingham         {
2251ac04c30SGreg Clayton             ProcessSP process_sp (m_thread.GetProcess());
2261ac04c30SGreg Clayton             const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
227ef651600SJim Ingham             if (abi && m_return_type.IsValid())
228ef651600SJim Ingham             {
2296153c518SSean Callanan                 const bool persistent = false;
2306153c518SSean Callanan                 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
23170b57657SGreg Clayton             }
23218de2fdcSJim Ingham         }
2339da3683cSJim Ingham         if (log)
234324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
235324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
236324a1036SSaleem Abdulrasool                          IsPlanComplete());
2379da3683cSJim Ingham         m_takedown_done = true;
238ce553d88SJim Ingham         m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
23960c4118cSJim Ingham         m_real_stop_info_sp = GetPrivateStopInfo ();
2407b24e95eSEd Maste         if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
2417b24e95eSEd Maste         {
2427b24e95eSEd Maste             if (log)
243324a1036SSaleem Abdulrasool                 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
244324a1036SSaleem Abdulrasool                             static_cast<void*>(this));
2457b24e95eSEd Maste         }
24618de2fdcSJim Ingham         SetPlanComplete(success);
24710af7c43SSean Callanan         ClearBreakpoints();
2489da3683cSJim Ingham         if (log && log->GetVerbose())
2499da3683cSJim Ingham             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
2502c36439cSJim Ingham 
2519da3683cSJim Ingham     }
2529da3683cSJim Ingham     else
2539da3683cSJim Ingham     {
2549da3683cSJim Ingham         if (log)
255324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
256324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
257324a1036SSaleem Abdulrasool                          IsPlanComplete());
25830fdc8d8SChris Lattner     }
25977787033SJim Ingham }
26030fdc8d8SChris Lattner 
26130fdc8d8SChris Lattner void
262bda4e5ebSJim Ingham ThreadPlanCallFunction::WillPop ()
263bda4e5ebSJim Ingham {
2640161b49cSJim Ingham     DoTakedown(PlanSucceeded());
265bda4e5ebSJim Ingham }
266bda4e5ebSJim Ingham 
267bda4e5ebSJim Ingham void
2682a48f525SGreg Clayton ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
26930fdc8d8SChris Lattner {
2702a48f525SGreg Clayton     if (level == eDescriptionLevelBrief)
27130fdc8d8SChris Lattner     {
27230fdc8d8SChris Lattner         s->Printf("Function call thread plan");
27330fdc8d8SChris Lattner     }
27430fdc8d8SChris Lattner     else
27530fdc8d8SChris Lattner     {
2761ac04c30SGreg Clayton         TargetSP target_sp (m_thread.CalculateTarget());
277d01b2953SDaniel Malea         s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
27830fdc8d8SChris Lattner     }
27930fdc8d8SChris Lattner }
28030fdc8d8SChris Lattner 
28130fdc8d8SChris Lattner bool
28230fdc8d8SChris Lattner ThreadPlanCallFunction::ValidatePlan (Stream *error)
28330fdc8d8SChris Lattner {
28430fdc8d8SChris Lattner     if (!m_valid)
285ea06f3bfSJim Ingham     {
286ea06f3bfSJim Ingham         if (error)
287ea06f3bfSJim Ingham         {
288ea06f3bfSJim Ingham             if (m_constructor_errors.GetSize() > 0)
289ea06f3bfSJim Ingham                 error->PutCString (m_constructor_errors.GetData());
290ea06f3bfSJim Ingham             else
291ea06f3bfSJim Ingham                 error->PutCString ("Unknown error");
292ea06f3bfSJim Ingham         }
29330fdc8d8SChris Lattner         return false;
294ea06f3bfSJim Ingham     }
29530fdc8d8SChris Lattner 
29630fdc8d8SChris Lattner     return true;
29730fdc8d8SChris Lattner }
29830fdc8d8SChris Lattner 
2990161b49cSJim Ingham 
3000161b49cSJim Ingham Vote
3010161b49cSJim Ingham ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
30230fdc8d8SChris Lattner {
3030161b49cSJim Ingham     if (m_takedown_done || IsPlanComplete())
3040161b49cSJim Ingham         return eVoteYes;
3050161b49cSJim Ingham     else
3060161b49cSJim Ingham         return ThreadPlan::ShouldReportStop(event_ptr);
3070161b49cSJim Ingham }
3080161b49cSJim Ingham 
3090161b49cSJim Ingham bool
310221d51cfSJim Ingham ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
3110161b49cSJim Ingham {
3125160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
31360c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo ();
314160f78c5SJim Ingham 
31540d871faSJim Ingham     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
31640d871faSJim Ingham     // we answer yes.
3170161b49cSJim Ingham     if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
318184e9811SJim Ingham     {
319184e9811SJim Ingham         SetPlanComplete();
32040d871faSJim Ingham         return true;
321184e9811SJim Ingham     }
3223e6fedcaSSean Callanan 
323c98aca60SSean Callanan     // Check if the breakpoint is one of ours.
324c98aca60SSean Callanan 
32518de2fdcSJim Ingham     StopReason stop_reason;
32618de2fdcSJim Ingham     if (!m_real_stop_info_sp)
32718de2fdcSJim Ingham         stop_reason = eStopReasonNone;
32818de2fdcSJim Ingham     else
32918de2fdcSJim Ingham         stop_reason = m_real_stop_info_sp->GetStopReason();
3300161b49cSJim Ingham     if (log)
3310161b49cSJim Ingham         log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
33218de2fdcSJim Ingham 
33318de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
334c98aca60SSean Callanan         return true;
335c98aca60SSean Callanan 
33635878c47SJim Ingham     // One more quirk here.  If this event was from Halt interrupting the target, then we should not consider
33735878c47SJim Ingham     // ourselves complete.  Return true to acknowledge the stop.
33835878c47SJim Ingham     if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
33935878c47SJim Ingham     {
34035878c47SJim Ingham         if (log)
34135878c47SJim Ingham             log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: The event is an Interrupt, returning true.");
34235878c47SJim Ingham         return true;
34335878c47SJim Ingham     }
3440161b49cSJim Ingham     // We control breakpoints separately from other "stop reasons."  So first,
3450161b49cSJim Ingham     // check the case where we stopped for an internal breakpoint, in that case, continue on.
3460161b49cSJim Ingham     // If it is not an internal breakpoint, consult m_ignore_breakpoints.
3476db73ca5SSean Callanan 
34818de2fdcSJim Ingham 
34918de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint)
35040d871faSJim Ingham     {
3511ac04c30SGreg Clayton         ProcessSP process_sp (m_thread.CalculateProcess());
352160f78c5SJim Ingham         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3531ac04c30SGreg Clayton         BreakpointSiteSP bp_site_sp;
3541ac04c30SGreg Clayton         if (process_sp)
3551ac04c30SGreg Clayton             bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
35640d871faSJim Ingham         if (bp_site_sp)
35740d871faSJim Ingham         {
35840d871faSJim Ingham             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
35940d871faSJim Ingham             bool is_internal = true;
36040d871faSJim Ingham             for (uint32_t i = 0; i < num_owners; i++)
36140d871faSJim Ingham             {
3626db73ca5SSean Callanan                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3630161b49cSJim Ingham                 if (log)
3640161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
3656db73ca5SSean Callanan 
3666db73ca5SSean Callanan                 if (!bp.IsInternal())
36740d871faSJim Ingham                 {
36840d871faSJim Ingham                     is_internal = false;
36940d871faSJim Ingham                     break;
37040d871faSJim Ingham                 }
37140d871faSJim Ingham             }
37240d871faSJim Ingham             if (is_internal)
3730161b49cSJim Ingham             {
3740161b49cSJim Ingham                 if (log)
3750161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
37640d871faSJim Ingham                 return false;
37740d871faSJim Ingham             }
3780161b49cSJim Ingham         }
37940d871faSJim Ingham 
380184e9811SJim Ingham         if (m_ignore_breakpoints)
381923886ceSJim Ingham         {
3820161b49cSJim Ingham             if (log)
3830161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3840161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(false);
385923886ceSJim Ingham             return true;
386923886ceSJim Ingham         }
387923886ceSJim Ingham         else
3880161b49cSJim Ingham         {
3890161b49cSJim Ingham             if (log)
3900161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3910161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(true);
3920161b49cSJim Ingham             return false;
3930161b49cSJim Ingham         }
3940161b49cSJim Ingham     }
3950161b49cSJim Ingham     else if (!m_unwind_on_error)
3960161b49cSJim Ingham     {
3970161b49cSJim Ingham         // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
398923886ceSJim Ingham         return false;
39940d871faSJim Ingham     }
40040d871faSJim Ingham     else
40140d871faSJim Ingham     {
40240d871faSJim Ingham         // If the subplan is running, any crashes are attributable to us.
4032c36439cSJim Ingham         // If we want to discard the plan, then we say we explain the stop
4042c36439cSJim Ingham         // but if we are going to be discarded, let whoever is above us
4052c36439cSJim Ingham         // explain the stop.
4060161b49cSJim Ingham         // But don't discard the plan if the stop would restart itself (for instance if it is a
4070161b49cSJim Ingham         // signal that is set not to stop.  Check that here first.  We just say we explain the stop
4080161b49cSJim Ingham         // but aren't done and everything will continue on from there.
4090161b49cSJim Ingham 
410*841ee9b9SJim Ingham         if (m_real_stop_info_sp && m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
4110161b49cSJim Ingham         {
412184e9811SJim Ingham             SetPlanComplete(false);
4139a028519SSean Callanan             if (m_subplan_sp)
41418de2fdcSJim Ingham             {
415184e9811SJim Ingham                 if (m_unwind_on_error)
41618de2fdcSJim Ingham                     return true;
41718de2fdcSJim Ingham                 else
41818de2fdcSJim Ingham                     return false;
41918de2fdcSJim Ingham             }
42018de2fdcSJim Ingham             else
42118de2fdcSJim Ingham                 return false;
42230fdc8d8SChris Lattner         }
4230161b49cSJim Ingham         else
4240161b49cSJim Ingham             return true;
4250161b49cSJim Ingham     }
42640d871faSJim Ingham }
42730fdc8d8SChris Lattner 
42830fdc8d8SChris Lattner bool
42930fdc8d8SChris Lattner ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
43030fdc8d8SChris Lattner {
431221d51cfSJim Ingham     // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
432184e9811SJim Ingham     // We need to do that here to make sure our state is correct.
433221d51cfSJim Ingham     DoPlanExplainsStop(event_ptr);
434184e9811SJim Ingham 
435184e9811SJim Ingham     if (IsPlanComplete())
43630fdc8d8SChris Lattner     {
4379da3683cSJim Ingham         ReportRegisterState ("Function completed.  Register state was:");
43830fdc8d8SChris Lattner         return true;
43930fdc8d8SChris Lattner     }
44030fdc8d8SChris Lattner     else
44130fdc8d8SChris Lattner     {
44230fdc8d8SChris Lattner         return false;
44330fdc8d8SChris Lattner     }
44430fdc8d8SChris Lattner }
44530fdc8d8SChris Lattner 
44630fdc8d8SChris Lattner bool
44730fdc8d8SChris Lattner ThreadPlanCallFunction::StopOthers ()
44830fdc8d8SChris Lattner {
44930fdc8d8SChris Lattner     return m_stop_other_threads;
45030fdc8d8SChris Lattner }
45130fdc8d8SChris Lattner 
45230fdc8d8SChris Lattner StateType
45306e827ccSJim Ingham ThreadPlanCallFunction::GetPlanRunState ()
45430fdc8d8SChris Lattner {
45530fdc8d8SChris Lattner     return eStateRunning;
45630fdc8d8SChris Lattner }
45730fdc8d8SChris Lattner 
45830fdc8d8SChris Lattner void
45930fdc8d8SChris Lattner ThreadPlanCallFunction::DidPush ()
46030fdc8d8SChris Lattner {
461be3a1b14SSean Callanan //#define SINGLE_STEP_EXPRESSIONS
462be3a1b14SSean Callanan 
4638559a355SJim Ingham     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
4648559a355SJim Ingham     // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
4658559a355SJim Ingham 
4668559a355SJim Ingham     GetThread().SetStopInfoToNothing();
4678559a355SJim Ingham 
468be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
46930fdc8d8SChris Lattner     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
47030fdc8d8SChris Lattner 
47130fdc8d8SChris Lattner     m_thread.QueueThreadPlan(m_subplan_sp, false);
47277787033SJim Ingham     m_subplan_sp->SetPrivate (true);
473be3a1b14SSean Callanan #endif
47430fdc8d8SChris Lattner }
47530fdc8d8SChris Lattner 
47630fdc8d8SChris Lattner bool
47730fdc8d8SChris Lattner ThreadPlanCallFunction::WillStop ()
47830fdc8d8SChris Lattner {
47930fdc8d8SChris Lattner     return true;
48030fdc8d8SChris Lattner }
48130fdc8d8SChris Lattner 
48230fdc8d8SChris Lattner bool
48330fdc8d8SChris Lattner ThreadPlanCallFunction::MischiefManaged ()
48430fdc8d8SChris Lattner {
4855160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
48630fdc8d8SChris Lattner 
487184e9811SJim Ingham     if (IsPlanComplete())
488184e9811SJim Ingham     {
48930fdc8d8SChris Lattner         if (log)
490324a1036SSaleem Abdulrasool             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
491324a1036SSaleem Abdulrasool                         static_cast<void*>(this));
49230fdc8d8SChris Lattner 
49330fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
49430fdc8d8SChris Lattner         return true;
49530fdc8d8SChris Lattner     }
49630fdc8d8SChris Lattner     else
49730fdc8d8SChris Lattner     {
49830fdc8d8SChris Lattner         return false;
49930fdc8d8SChris Lattner     }
50030fdc8d8SChris Lattner }
5016db73ca5SSean Callanan 
5026db73ca5SSean Callanan void
5036db73ca5SSean Callanan ThreadPlanCallFunction::SetBreakpoints ()
5046db73ca5SSean Callanan {
5051ac04c30SGreg Clayton     ProcessSP process_sp (m_thread.CalculateProcess());
5066fbc48bcSJim Ingham     if (m_trap_exceptions && process_sp)
5071ac04c30SGreg Clayton     {
5081ac04c30SGreg Clayton         m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
5091ac04c30SGreg Clayton         m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
5106db73ca5SSean Callanan 
511f211510fSSean Callanan         if (m_cxx_language_runtime)
5126fbc48bcSJim Ingham         {
5136fbc48bcSJim Ingham             m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
514f211510fSSean Callanan             m_cxx_language_runtime->SetExceptionBreakpoints();
5156fbc48bcSJim Ingham         }
516f211510fSSean Callanan         if (m_objc_language_runtime)
5176fbc48bcSJim Ingham         {
5186fbc48bcSJim Ingham             m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
519f211510fSSean Callanan             m_objc_language_runtime->SetExceptionBreakpoints();
5206db73ca5SSean Callanan         }
5211ac04c30SGreg Clayton     }
5226fbc48bcSJim Ingham }
5236db73ca5SSean Callanan 
5246db73ca5SSean Callanan void
5256db73ca5SSean Callanan ThreadPlanCallFunction::ClearBreakpoints ()
5266db73ca5SSean Callanan {
5276fbc48bcSJim Ingham     if (m_trap_exceptions)
5286fbc48bcSJim Ingham     {
5296fbc48bcSJim Ingham         if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
530f211510fSSean Callanan             m_cxx_language_runtime->ClearExceptionBreakpoints();
5316fbc48bcSJim Ingham         if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
532f211510fSSean Callanan             m_objc_language_runtime->ClearExceptionBreakpoints();
5336db73ca5SSean Callanan     }
5346fbc48bcSJim Ingham }
535c98aca60SSean Callanan 
536c98aca60SSean Callanan bool
537c98aca60SSean Callanan ThreadPlanCallFunction::BreakpointsExplainStop()
538c98aca60SSean Callanan {
53960c4118cSJim Ingham     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
540c98aca60SSean Callanan 
5416fbc48bcSJim Ingham     if (m_trap_exceptions)
5426fbc48bcSJim Ingham     {
543184e9811SJim Ingham         if ((m_cxx_language_runtime &&
544f211510fSSean Callanan                 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
545184e9811SJim Ingham            ||(m_objc_language_runtime &&
546184e9811SJim Ingham                 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
547184e9811SJim Ingham         {
548641a67ceSJim Ingham             Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
549641a67ceSJim Ingham             if (log)
550641a67ceSJim Ingham                 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
551641a67ceSJim Ingham 
552184e9811SJim Ingham             SetPlanComplete(false);
553641a67ceSJim Ingham 
554641a67ceSJim Ingham             // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
555641a67ceSJim Ingham             // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
556641a67ceSJim Ingham             stop_info_sp->OverrideShouldStop (true);
557c98aca60SSean Callanan             return true;
558184e9811SJim Ingham         }
5596fbc48bcSJim Ingham     }
560f211510fSSean Callanan 
561c98aca60SSean Callanan     return false;
562c98aca60SSean Callanan }
5638559a355SJim Ingham 
5640ff099f1SJim Ingham void
5650ff099f1SJim Ingham ThreadPlanCallFunction::SetStopOthers (bool new_value)
5660ff099f1SJim Ingham {
5670ff099f1SJim Ingham     m_subplan_sp->SetStopOthers(new_value);
5680ff099f1SJim Ingham }
5690ff099f1SJim Ingham 
5700ff099f1SJim Ingham 
5718559a355SJim Ingham bool
5728559a355SJim Ingham ThreadPlanCallFunction::RestoreThreadState()
5738559a355SJim Ingham {
5748559a355SJim Ingham     return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
5758559a355SJim Ingham }
5768559a355SJim Ingham 
577