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
1730fdc8d8SChris Lattner #include "lldb/lldb-private-log.h"
1840d871faSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
1940d871faSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Address.h"
2130fdc8d8SChris Lattner #include "lldb/Core/Log.h"
221f746071SGreg Clayton #include "lldb/Core/Module.h"
2330fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
241f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
25*32abc6edSZachary Turner #include "lldb/Target/ABI.h"
26f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
2730fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2830fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2940d871faSJim Ingham #include "lldb/Target/StopInfo.h"
3030fdc8d8SChris Lattner #include "lldb/Target/Target.h"
3130fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
3230fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
3330fdc8d8SChris Lattner 
3430fdc8d8SChris Lattner using namespace lldb;
3530fdc8d8SChris Lattner using namespace lldb_private;
3630fdc8d8SChris Lattner 
3730fdc8d8SChris Lattner //----------------------------------------------------------------------
3830fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
3930fdc8d8SChris Lattner //----------------------------------------------------------------------
400092c8ebSJim Ingham bool
410092c8ebSJim Ingham ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
420092c8ebSJim Ingham                                           ABI *& abi,
430092c8ebSJim Ingham                                           lldb::addr_t &start_load_addr,
440092c8ebSJim Ingham                                           lldb::addr_t &function_load_addr)
450092c8ebSJim Ingham {
460092c8ebSJim Ingham     SetIsMasterPlan (true);
47923886ceSJim Ingham     SetOkayToDiscard (false);
48327c267aSAndrew Kaylor     SetPrivate (true);
490092c8ebSJim Ingham 
500092c8ebSJim Ingham     ProcessSP process_sp (thread.GetProcess());
510092c8ebSJim Ingham     if (!process_sp)
520092c8ebSJim Ingham         return false;
530092c8ebSJim Ingham 
540092c8ebSJim Ingham     abi = process_sp->GetABI().get();
550092c8ebSJim Ingham 
560092c8ebSJim Ingham     if (!abi)
570092c8ebSJim Ingham         return false;
580092c8ebSJim Ingham 
595160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
600092c8ebSJim Ingham 
610092c8ebSJim Ingham     SetBreakpoints();
620092c8ebSJim Ingham 
630092c8ebSJim Ingham     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
640092c8ebSJim Ingham     // If we can't read memory at the point of the process where we are planning to put our function, we're
650092c8ebSJim Ingham     // not going to get any further...
660092c8ebSJim Ingham     Error error;
670092c8ebSJim Ingham     process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
680092c8ebSJim Ingham     if (!error.Success())
690092c8ebSJim Ingham     {
70ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
710092c8ebSJim Ingham         if (log)
72324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
73324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
74324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
750092c8ebSJim Ingham         return false;
760092c8ebSJim Ingham     }
770092c8ebSJim Ingham 
786fbc48bcSJim Ingham     Module *exe_module = GetTarget().GetExecutableModulePointer();
790092c8ebSJim Ingham 
800092c8ebSJim Ingham     if (exe_module == NULL)
810092c8ebSJim Ingham     {
82ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Can't execute code without an executable module.");
830092c8ebSJim Ingham         if (log)
84324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
85324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
86324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
870092c8ebSJim Ingham         return false;
880092c8ebSJim Ingham     }
890092c8ebSJim Ingham     else
900092c8ebSJim Ingham     {
910092c8ebSJim Ingham         ObjectFile *objectFile = exe_module->GetObjectFile();
920092c8ebSJim Ingham         if (!objectFile)
930092c8ebSJim Ingham         {
94ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
95ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
96ea06f3bfSJim Ingham 
970092c8ebSJim Ingham             if (log)
98324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
99324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
100324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1010092c8ebSJim Ingham             return false;
1020092c8ebSJim Ingham         }
103ea06f3bfSJim Ingham 
1040092c8ebSJim Ingham         m_start_addr = objectFile->GetEntryPointAddress();
1050092c8ebSJim Ingham         if (!m_start_addr.IsValid())
1060092c8ebSJim Ingham         {
107ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
108ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
1090092c8ebSJim Ingham             if (log)
110324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
111324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
112324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1130092c8ebSJim Ingham             return false;
1140092c8ebSJim Ingham         }
1150092c8ebSJim Ingham     }
1160092c8ebSJim Ingham 
1176fbc48bcSJim Ingham     start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
1180092c8ebSJim Ingham 
1190092c8ebSJim Ingham     // Checkpoint the thread state so we can restore it later.
1200092c8ebSJim Ingham     if (log && log->GetVerbose())
1210092c8ebSJim Ingham         ReportRegisterState ("About to checkpoint thread before function call.  Original register state was:");
1220092c8ebSJim Ingham 
1230092c8ebSJim Ingham     if (!thread.CheckpointThreadState (m_stored_thread_state))
1240092c8ebSJim Ingham     {
125ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
1260092c8ebSJim Ingham         if (log)
127324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
128324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
129324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
1300092c8ebSJim Ingham         return false;
1310092c8ebSJim Ingham     }
1326fbc48bcSJim Ingham     function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
1330092c8ebSJim Ingham 
1340092c8ebSJim Ingham     return true;
1350092c8ebSJim Ingham }
13630fdc8d8SChris Lattner 
13730fdc8d8SChris Lattner ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
13800049b8bSMatt Kopec                                                 const Address &function,
139ef651600SJim Ingham                                                 const ClangASTType &return_type,
140a464f3d4SSean Callanan                                                 llvm::ArrayRef<addr_t> args,
141a464f3d4SSean Callanan                                                 const EvaluateExpressionOptions &options) :
142b01e742aSJim Ingham     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
14330fdc8d8SChris Lattner     m_valid (false),
1446fbc48bcSJim Ingham     m_stop_other_threads (options.GetStopOthers()),
1456fbc48bcSJim Ingham     m_unwind_on_error (options.DoesUnwindOnError()),
1466fbc48bcSJim Ingham     m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
1476fbc48bcSJim Ingham     m_debug_execution (options.GetDebug()),
1486fbc48bcSJim Ingham     m_trap_exceptions (options.GetTrapExceptions()),
14916e0c686SJim Ingham     m_function_addr (function),
150b4cb0be3SFilipe Cabecinhas     m_function_sp (0),
151ef651600SJim Ingham     m_return_type (return_type),
152ce553d88SJim Ingham     m_takedown_done (false),
1536fbc48bcSJim Ingham     m_should_clear_objc_exception_bp(false),
1546fbc48bcSJim Ingham     m_should_clear_cxx_exception_bp (false),
1556fbc48bcSJim Ingham     m_stop_address (LLDB_INVALID_ADDRESS)
15630fdc8d8SChris Lattner {
1570092c8ebSJim Ingham     lldb::addr_t start_load_addr;
1580092c8ebSJim Ingham     ABI *abi;
1590092c8ebSJim Ingham     lldb::addr_t function_load_addr;
160923886ceSJim Ingham     if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
1611ac04c30SGreg Clayton         return;
1621ac04c30SGreg Clayton 
16330fdc8d8SChris Lattner     if (!abi->PrepareTrivialCall(thread,
164e359d9b7SSean Callanan                                  m_function_sp,
1650092c8ebSJim Ingham                                  function_load_addr,
1662a48f525SGreg Clayton                                  start_load_addr,
167a464f3d4SSean Callanan                                  args))
16830fdc8d8SChris Lattner         return;
16930fdc8d8SChris Lattner 
1709da3683cSJim Ingham     ReportRegisterState ("Function call was set up.  Register state was:");
1719da3683cSJim Ingham 
1729da3683cSJim Ingham     m_valid = true;
1739da3683cSJim Ingham }
1749da3683cSJim Ingham 
1759da3683cSJim Ingham ThreadPlanCallFunction::~ThreadPlanCallFunction ()
1769da3683cSJim Ingham {
1770161b49cSJim Ingham     DoTakedown(PlanSucceeded());
1789da3683cSJim Ingham }
1799da3683cSJim Ingham 
1809da3683cSJim Ingham void
1819da3683cSJim Ingham ThreadPlanCallFunction::ReportRegisterState (const char *message)
1829da3683cSJim Ingham {
1835160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
184ece96492SSean Callanan     if (log)
185ece96492SSean Callanan     {
186af247d7bSGreg Clayton         StreamString strm;
1875ccbd294SGreg Clayton         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
188ece96492SSean Callanan 
1899da3683cSJim Ingham         log->PutCString(message);
190ece96492SSean Callanan 
191af247d7bSGreg Clayton         RegisterValue reg_value;
192ece96492SSean Callanan 
193af247d7bSGreg Clayton         for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
194af247d7bSGreg Clayton              reg_idx < num_registers;
195af247d7bSGreg Clayton              ++reg_idx)
196af247d7bSGreg Clayton         {
197af247d7bSGreg Clayton             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
198af247d7bSGreg Clayton             if (reg_ctx->ReadRegister(reg_info, reg_value))
199af247d7bSGreg Clayton             {
200af247d7bSGreg Clayton                 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
201af247d7bSGreg Clayton                 strm.EOL();
202ece96492SSean Callanan             }
203ece96492SSean Callanan         }
204af247d7bSGreg Clayton         log->PutCString(strm.GetData());
205af247d7bSGreg Clayton     }
20610af7c43SSean Callanan }
20710af7c43SSean Callanan 
20810af7c43SSean Callanan void
20918de2fdcSJim Ingham ThreadPlanCallFunction::DoTakedown (bool success)
21010af7c43SSean Callanan {
2115160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
21287d0e618SJim Ingham 
21387d0e618SJim Ingham     if (!m_valid)
21487d0e618SJim Ingham     {
21587d0e618SJim Ingham         //Don't call DoTakedown if we were never valid to begin with.
21687d0e618SJim Ingham         if (log)
217324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
218324a1036SSaleem Abdulrasool                          static_cast<void*>(this));
21987d0e618SJim Ingham         return;
22087d0e618SJim Ingham     }
22187d0e618SJim Ingham 
2229da3683cSJim Ingham     if (!m_takedown_done)
22377787033SJim Ingham     {
22418de2fdcSJim Ingham         if (success)
22518de2fdcSJim Ingham         {
2261ac04c30SGreg Clayton             ProcessSP process_sp (m_thread.GetProcess());
2271ac04c30SGreg Clayton             const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
228ef651600SJim Ingham             if (abi && m_return_type.IsValid())
229ef651600SJim Ingham             {
2306153c518SSean Callanan                 const bool persistent = false;
2316153c518SSean Callanan                 m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent);
23270b57657SGreg Clayton             }
23318de2fdcSJim Ingham         }
2349da3683cSJim Ingham         if (log)
235324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
236324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
237324a1036SSaleem Abdulrasool                          IsPlanComplete());
2389da3683cSJim Ingham         m_takedown_done = true;
239ce553d88SJim Ingham         m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
24060c4118cSJim Ingham         m_real_stop_info_sp = GetPrivateStopInfo ();
2417b24e95eSEd Maste         if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
2427b24e95eSEd Maste         {
2437b24e95eSEd Maste             if (log)
244324a1036SSaleem Abdulrasool                 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
245324a1036SSaleem Abdulrasool                             static_cast<void*>(this));
2467b24e95eSEd Maste         }
24718de2fdcSJim Ingham         SetPlanComplete(success);
24810af7c43SSean Callanan         ClearBreakpoints();
2499da3683cSJim Ingham         if (log && log->GetVerbose())
2509da3683cSJim Ingham             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
2512c36439cSJim Ingham 
2529da3683cSJim Ingham     }
2539da3683cSJim Ingham     else
2549da3683cSJim Ingham     {
2559da3683cSJim Ingham         if (log)
256324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
257324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
258324a1036SSaleem Abdulrasool                          IsPlanComplete());
25930fdc8d8SChris Lattner     }
26077787033SJim Ingham }
26130fdc8d8SChris Lattner 
26230fdc8d8SChris Lattner void
263bda4e5ebSJim Ingham ThreadPlanCallFunction::WillPop ()
264bda4e5ebSJim Ingham {
2650161b49cSJim Ingham     DoTakedown(PlanSucceeded());
266bda4e5ebSJim Ingham }
267bda4e5ebSJim Ingham 
268bda4e5ebSJim Ingham void
2692a48f525SGreg Clayton ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
27030fdc8d8SChris Lattner {
2712a48f525SGreg Clayton     if (level == eDescriptionLevelBrief)
27230fdc8d8SChris Lattner     {
27330fdc8d8SChris Lattner         s->Printf("Function call thread plan");
27430fdc8d8SChris Lattner     }
27530fdc8d8SChris Lattner     else
27630fdc8d8SChris Lattner     {
2771ac04c30SGreg Clayton         TargetSP target_sp (m_thread.CalculateTarget());
278d01b2953SDaniel Malea         s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
27930fdc8d8SChris Lattner     }
28030fdc8d8SChris Lattner }
28130fdc8d8SChris Lattner 
28230fdc8d8SChris Lattner bool
28330fdc8d8SChris Lattner ThreadPlanCallFunction::ValidatePlan (Stream *error)
28430fdc8d8SChris Lattner {
28530fdc8d8SChris Lattner     if (!m_valid)
286ea06f3bfSJim Ingham     {
287ea06f3bfSJim Ingham         if (error)
288ea06f3bfSJim Ingham         {
289ea06f3bfSJim Ingham             if (m_constructor_errors.GetSize() > 0)
290ea06f3bfSJim Ingham                 error->PutCString (m_constructor_errors.GetData());
291ea06f3bfSJim Ingham             else
292ea06f3bfSJim Ingham                 error->PutCString ("Unknown error");
293ea06f3bfSJim Ingham         }
29430fdc8d8SChris Lattner         return false;
295ea06f3bfSJim Ingham     }
29630fdc8d8SChris Lattner 
29730fdc8d8SChris Lattner     return true;
29830fdc8d8SChris Lattner }
29930fdc8d8SChris Lattner 
3000161b49cSJim Ingham 
3010161b49cSJim Ingham Vote
3020161b49cSJim Ingham ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
30330fdc8d8SChris Lattner {
3040161b49cSJim Ingham     if (m_takedown_done || IsPlanComplete())
3050161b49cSJim Ingham         return eVoteYes;
3060161b49cSJim Ingham     else
3070161b49cSJim Ingham         return ThreadPlan::ShouldReportStop(event_ptr);
3080161b49cSJim Ingham }
3090161b49cSJim Ingham 
3100161b49cSJim Ingham bool
311221d51cfSJim Ingham ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
3120161b49cSJim Ingham {
3135160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
31460c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo ();
315160f78c5SJim Ingham 
31640d871faSJim Ingham     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
31740d871faSJim Ingham     // we answer yes.
3180161b49cSJim Ingham     if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
319184e9811SJim Ingham     {
320184e9811SJim Ingham         SetPlanComplete();
32140d871faSJim Ingham         return true;
322184e9811SJim Ingham     }
3233e6fedcaSSean Callanan 
324c98aca60SSean Callanan     // Check if the breakpoint is one of ours.
325c98aca60SSean Callanan 
32618de2fdcSJim Ingham     StopReason stop_reason;
32718de2fdcSJim Ingham     if (!m_real_stop_info_sp)
32818de2fdcSJim Ingham         stop_reason = eStopReasonNone;
32918de2fdcSJim Ingham     else
33018de2fdcSJim Ingham         stop_reason = m_real_stop_info_sp->GetStopReason();
3310161b49cSJim Ingham     if (log)
3320161b49cSJim Ingham         log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
33318de2fdcSJim Ingham 
33418de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
335c98aca60SSean Callanan         return true;
336c98aca60SSean Callanan 
33735878c47SJim Ingham     // One more quirk here.  If this event was from Halt interrupting the target, then we should not consider
33835878c47SJim Ingham     // ourselves complete.  Return true to acknowledge the stop.
33935878c47SJim Ingham     if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
34035878c47SJim Ingham     {
34135878c47SJim Ingham         if (log)
34235878c47SJim Ingham             log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: The event is an Interrupt, returning true.");
34335878c47SJim Ingham         return true;
34435878c47SJim Ingham     }
3450161b49cSJim Ingham     // We control breakpoints separately from other "stop reasons."  So first,
3460161b49cSJim Ingham     // check the case where we stopped for an internal breakpoint, in that case, continue on.
3470161b49cSJim Ingham     // If it is not an internal breakpoint, consult m_ignore_breakpoints.
3486db73ca5SSean Callanan 
34918de2fdcSJim Ingham 
35018de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint)
35140d871faSJim Ingham     {
3521ac04c30SGreg Clayton         ProcessSP process_sp (m_thread.CalculateProcess());
353160f78c5SJim Ingham         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3541ac04c30SGreg Clayton         BreakpointSiteSP bp_site_sp;
3551ac04c30SGreg Clayton         if (process_sp)
3561ac04c30SGreg Clayton             bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
35740d871faSJim Ingham         if (bp_site_sp)
35840d871faSJim Ingham         {
35940d871faSJim Ingham             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
36040d871faSJim Ingham             bool is_internal = true;
36140d871faSJim Ingham             for (uint32_t i = 0; i < num_owners; i++)
36240d871faSJim Ingham             {
3636db73ca5SSean Callanan                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3640161b49cSJim Ingham                 if (log)
3650161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
3666db73ca5SSean Callanan 
3676db73ca5SSean Callanan                 if (!bp.IsInternal())
36840d871faSJim Ingham                 {
36940d871faSJim Ingham                     is_internal = false;
37040d871faSJim Ingham                     break;
37140d871faSJim Ingham                 }
37240d871faSJim Ingham             }
37340d871faSJim Ingham             if (is_internal)
3740161b49cSJim Ingham             {
3750161b49cSJim Ingham                 if (log)
3760161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
37740d871faSJim Ingham                 return false;
37840d871faSJim Ingham             }
3790161b49cSJim Ingham         }
38040d871faSJim Ingham 
381184e9811SJim Ingham         if (m_ignore_breakpoints)
382923886ceSJim Ingham         {
3830161b49cSJim Ingham             if (log)
3840161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3850161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(false);
386923886ceSJim Ingham             return true;
387923886ceSJim Ingham         }
388923886ceSJim Ingham         else
3890161b49cSJim Ingham         {
3900161b49cSJim Ingham             if (log)
3910161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3920161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(true);
3930161b49cSJim Ingham             return false;
3940161b49cSJim Ingham         }
3950161b49cSJim Ingham     }
3960161b49cSJim Ingham     else if (!m_unwind_on_error)
3970161b49cSJim Ingham     {
3980161b49cSJim Ingham         // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
399923886ceSJim Ingham         return false;
40040d871faSJim Ingham     }
40140d871faSJim Ingham     else
40240d871faSJim Ingham     {
40340d871faSJim Ingham         // If the subplan is running, any crashes are attributable to us.
4042c36439cSJim Ingham         // If we want to discard the plan, then we say we explain the stop
4052c36439cSJim Ingham         // but if we are going to be discarded, let whoever is above us
4062c36439cSJim Ingham         // explain the stop.
4070161b49cSJim Ingham         // But don't discard the plan if the stop would restart itself (for instance if it is a
4080161b49cSJim Ingham         // signal that is set not to stop.  Check that here first.  We just say we explain the stop
4090161b49cSJim Ingham         // but aren't done and everything will continue on from there.
4100161b49cSJim Ingham 
4110161b49cSJim Ingham         if (m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
4120161b49cSJim Ingham         {
413184e9811SJim Ingham             SetPlanComplete(false);
4149a028519SSean Callanan             if (m_subplan_sp)
41518de2fdcSJim Ingham             {
416184e9811SJim Ingham                 if (m_unwind_on_error)
41718de2fdcSJim Ingham                     return true;
41818de2fdcSJim Ingham                 else
41918de2fdcSJim Ingham                     return false;
42018de2fdcSJim Ingham             }
42118de2fdcSJim Ingham             else
42218de2fdcSJim Ingham                 return false;
42330fdc8d8SChris Lattner         }
4240161b49cSJim Ingham         else
4250161b49cSJim Ingham             return true;
4260161b49cSJim Ingham     }
42740d871faSJim Ingham }
42830fdc8d8SChris Lattner 
42930fdc8d8SChris Lattner bool
43030fdc8d8SChris Lattner ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
43130fdc8d8SChris Lattner {
432221d51cfSJim Ingham     // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
433184e9811SJim Ingham     // We need to do that here to make sure our state is correct.
434221d51cfSJim Ingham     DoPlanExplainsStop(event_ptr);
435184e9811SJim Ingham 
436184e9811SJim Ingham     if (IsPlanComplete())
43730fdc8d8SChris Lattner     {
4389da3683cSJim Ingham         ReportRegisterState ("Function completed.  Register state was:");
43930fdc8d8SChris Lattner         return true;
44030fdc8d8SChris Lattner     }
44130fdc8d8SChris Lattner     else
44230fdc8d8SChris Lattner     {
44330fdc8d8SChris Lattner         return false;
44430fdc8d8SChris Lattner     }
44530fdc8d8SChris Lattner }
44630fdc8d8SChris Lattner 
44730fdc8d8SChris Lattner bool
44830fdc8d8SChris Lattner ThreadPlanCallFunction::StopOthers ()
44930fdc8d8SChris Lattner {
45030fdc8d8SChris Lattner     return m_stop_other_threads;
45130fdc8d8SChris Lattner }
45230fdc8d8SChris Lattner 
45330fdc8d8SChris Lattner StateType
45406e827ccSJim Ingham ThreadPlanCallFunction::GetPlanRunState ()
45530fdc8d8SChris Lattner {
45630fdc8d8SChris Lattner     return eStateRunning;
45730fdc8d8SChris Lattner }
45830fdc8d8SChris Lattner 
45930fdc8d8SChris Lattner void
46030fdc8d8SChris Lattner ThreadPlanCallFunction::DidPush ()
46130fdc8d8SChris Lattner {
462be3a1b14SSean Callanan //#define SINGLE_STEP_EXPRESSIONS
463be3a1b14SSean Callanan 
4648559a355SJim Ingham     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
4658559a355SJim Ingham     // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
4668559a355SJim Ingham 
4678559a355SJim Ingham     GetThread().SetStopInfoToNothing();
4688559a355SJim Ingham 
469be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
47030fdc8d8SChris Lattner     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
47130fdc8d8SChris Lattner 
47230fdc8d8SChris Lattner     m_thread.QueueThreadPlan(m_subplan_sp, false);
47377787033SJim Ingham     m_subplan_sp->SetPrivate (true);
474be3a1b14SSean Callanan #endif
47530fdc8d8SChris Lattner }
47630fdc8d8SChris Lattner 
47730fdc8d8SChris Lattner bool
47830fdc8d8SChris Lattner ThreadPlanCallFunction::WillStop ()
47930fdc8d8SChris Lattner {
48030fdc8d8SChris Lattner     return true;
48130fdc8d8SChris Lattner }
48230fdc8d8SChris Lattner 
48330fdc8d8SChris Lattner bool
48430fdc8d8SChris Lattner ThreadPlanCallFunction::MischiefManaged ()
48530fdc8d8SChris Lattner {
4865160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
48730fdc8d8SChris Lattner 
488184e9811SJim Ingham     if (IsPlanComplete())
489184e9811SJim Ingham     {
49030fdc8d8SChris Lattner         if (log)
491324a1036SSaleem Abdulrasool             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
492324a1036SSaleem Abdulrasool                         static_cast<void*>(this));
49330fdc8d8SChris Lattner 
49430fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
49530fdc8d8SChris Lattner         return true;
49630fdc8d8SChris Lattner     }
49730fdc8d8SChris Lattner     else
49830fdc8d8SChris Lattner     {
49930fdc8d8SChris Lattner         return false;
50030fdc8d8SChris Lattner     }
50130fdc8d8SChris Lattner }
5026db73ca5SSean Callanan 
5036db73ca5SSean Callanan void
5046db73ca5SSean Callanan ThreadPlanCallFunction::SetBreakpoints ()
5056db73ca5SSean Callanan {
5061ac04c30SGreg Clayton     ProcessSP process_sp (m_thread.CalculateProcess());
5076fbc48bcSJim Ingham     if (m_trap_exceptions && process_sp)
5081ac04c30SGreg Clayton     {
5091ac04c30SGreg Clayton         m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
5101ac04c30SGreg Clayton         m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
5116db73ca5SSean Callanan 
512f211510fSSean Callanan         if (m_cxx_language_runtime)
5136fbc48bcSJim Ingham         {
5146fbc48bcSJim Ingham             m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
515f211510fSSean Callanan             m_cxx_language_runtime->SetExceptionBreakpoints();
5166fbc48bcSJim Ingham         }
517f211510fSSean Callanan         if (m_objc_language_runtime)
5186fbc48bcSJim Ingham         {
5196fbc48bcSJim Ingham             m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
520f211510fSSean Callanan             m_objc_language_runtime->SetExceptionBreakpoints();
5216db73ca5SSean Callanan         }
5221ac04c30SGreg Clayton     }
5236fbc48bcSJim Ingham }
5246db73ca5SSean Callanan 
5256db73ca5SSean Callanan void
5266db73ca5SSean Callanan ThreadPlanCallFunction::ClearBreakpoints ()
5276db73ca5SSean Callanan {
5286fbc48bcSJim Ingham     if (m_trap_exceptions)
5296fbc48bcSJim Ingham     {
5306fbc48bcSJim Ingham         if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
531f211510fSSean Callanan             m_cxx_language_runtime->ClearExceptionBreakpoints();
5326fbc48bcSJim Ingham         if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
533f211510fSSean Callanan             m_objc_language_runtime->ClearExceptionBreakpoints();
5346db73ca5SSean Callanan     }
5356fbc48bcSJim Ingham }
536c98aca60SSean Callanan 
537c98aca60SSean Callanan bool
538c98aca60SSean Callanan ThreadPlanCallFunction::BreakpointsExplainStop()
539c98aca60SSean Callanan {
54060c4118cSJim Ingham     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
541c98aca60SSean Callanan 
5426fbc48bcSJim Ingham     if (m_trap_exceptions)
5436fbc48bcSJim Ingham     {
544184e9811SJim Ingham         if ((m_cxx_language_runtime &&
545f211510fSSean Callanan                 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
546184e9811SJim Ingham            ||(m_objc_language_runtime &&
547184e9811SJim Ingham                 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
548184e9811SJim Ingham         {
549641a67ceSJim Ingham             Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
550641a67ceSJim Ingham             if (log)
551641a67ceSJim Ingham                 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
552641a67ceSJim Ingham 
553184e9811SJim Ingham             SetPlanComplete(false);
554641a67ceSJim Ingham 
555641a67ceSJim Ingham             // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
556641a67ceSJim Ingham             // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
557641a67ceSJim Ingham             stop_info_sp->OverrideShouldStop (true);
558c98aca60SSean Callanan             return true;
559184e9811SJim Ingham         }
5606fbc48bcSJim Ingham     }
561f211510fSSean Callanan 
562c98aca60SSean Callanan     return false;
563c98aca60SSean Callanan }
5648559a355SJim Ingham 
5650ff099f1SJim Ingham void
5660ff099f1SJim Ingham ThreadPlanCallFunction::SetStopOthers (bool new_value)
5670ff099f1SJim Ingham {
5680ff099f1SJim Ingham     m_subplan_sp->SetStopOthers(new_value);
5690ff099f1SJim Ingham }
5700ff099f1SJim Ingham 
5710ff099f1SJim Ingham 
5728559a355SJim Ingham bool
5738559a355SJim Ingham ThreadPlanCallFunction::RestoreThreadState()
5748559a355SJim Ingham {
5758559a355SJim Ingham     return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
5768559a355SJim Ingham }
5778559a355SJim Ingham 
578