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,
138*a1e5dc86SGreg Clayton                                                 const CompilerType &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),
150ce553d88SJim Ingham     m_takedown_done (false),
1516fbc48bcSJim Ingham     m_should_clear_objc_exception_bp(false),
1526fbc48bcSJim Ingham     m_should_clear_cxx_exception_bp (false),
15390ff7911SEwan Crawford     m_stop_address (LLDB_INVALID_ADDRESS),
15490ff7911SEwan Crawford     m_return_type (return_type)
15530fdc8d8SChris Lattner {
15690ff7911SEwan Crawford     lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
15790ff7911SEwan Crawford     lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
15890ff7911SEwan Crawford     ABI *abi = nullptr;
15990ff7911SEwan Crawford 
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 
17590ff7911SEwan Crawford ThreadPlanCallFunction::ThreadPlanCallFunction(Thread &thread,
17690ff7911SEwan Crawford                                                const Address &function,
17790ff7911SEwan Crawford                                                const EvaluateExpressionOptions &options) :
17890ff7911SEwan Crawford     ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
17990ff7911SEwan Crawford     m_valid(false),
18090ff7911SEwan Crawford     m_stop_other_threads(options.GetStopOthers()),
18190ff7911SEwan Crawford     m_unwind_on_error(options.DoesUnwindOnError()),
18290ff7911SEwan Crawford     m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
18390ff7911SEwan Crawford     m_debug_execution(options.GetDebug()),
18490ff7911SEwan Crawford     m_trap_exceptions(options.GetTrapExceptions()),
18590ff7911SEwan Crawford     m_function_addr(function),
18690ff7911SEwan Crawford     m_function_sp(0),
18790ff7911SEwan Crawford     m_takedown_done(false),
18890ff7911SEwan Crawford     m_should_clear_objc_exception_bp(false),
18990ff7911SEwan Crawford     m_should_clear_cxx_exception_bp(false),
19090ff7911SEwan Crawford     m_stop_address(LLDB_INVALID_ADDRESS),
191*a1e5dc86SGreg Clayton     m_return_type(CompilerType())
19290ff7911SEwan Crawford {
19390ff7911SEwan Crawford 
19490ff7911SEwan Crawford }
19590ff7911SEwan Crawford 
1969da3683cSJim Ingham ThreadPlanCallFunction::~ThreadPlanCallFunction ()
1979da3683cSJim Ingham {
1980161b49cSJim Ingham     DoTakedown(PlanSucceeded());
1999da3683cSJim Ingham }
2009da3683cSJim Ingham 
2019da3683cSJim Ingham void
2029da3683cSJim Ingham ThreadPlanCallFunction::ReportRegisterState (const char *message)
2039da3683cSJim Ingham {
2045160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
205ece96492SSean Callanan     if (log)
206ece96492SSean Callanan     {
207af247d7bSGreg Clayton         StreamString strm;
2085ccbd294SGreg Clayton         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
209ece96492SSean Callanan 
2109da3683cSJim Ingham         log->PutCString(message);
211ece96492SSean Callanan 
212af247d7bSGreg Clayton         RegisterValue reg_value;
213ece96492SSean Callanan 
214af247d7bSGreg Clayton         for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
215af247d7bSGreg Clayton              reg_idx < num_registers;
216af247d7bSGreg Clayton              ++reg_idx)
217af247d7bSGreg Clayton         {
218af247d7bSGreg Clayton             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
219af247d7bSGreg Clayton             if (reg_ctx->ReadRegister(reg_info, reg_value))
220af247d7bSGreg Clayton             {
221af247d7bSGreg Clayton                 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
222af247d7bSGreg Clayton                 strm.EOL();
223ece96492SSean Callanan             }
224ece96492SSean Callanan         }
225af247d7bSGreg Clayton         log->PutCString(strm.GetData());
226af247d7bSGreg Clayton     }
22710af7c43SSean Callanan }
22810af7c43SSean Callanan 
22910af7c43SSean Callanan void
23018de2fdcSJim Ingham ThreadPlanCallFunction::DoTakedown (bool success)
23110af7c43SSean Callanan {
2325160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
23387d0e618SJim Ingham 
23487d0e618SJim Ingham     if (!m_valid)
23587d0e618SJim Ingham     {
23687d0e618SJim Ingham         //Don't call DoTakedown if we were never valid to begin with.
23787d0e618SJim Ingham         if (log)
238324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
239324a1036SSaleem Abdulrasool                          static_cast<void*>(this));
24087d0e618SJim Ingham         return;
24187d0e618SJim Ingham     }
24287d0e618SJim Ingham 
2439da3683cSJim Ingham     if (!m_takedown_done)
24477787033SJim Ingham     {
24518de2fdcSJim Ingham         if (success)
24618de2fdcSJim Ingham         {
24790ff7911SEwan Crawford             SetReturnValue();
24818de2fdcSJim Ingham         }
2499da3683cSJim Ingham         if (log)
250324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
251324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
252324a1036SSaleem Abdulrasool                          IsPlanComplete());
2539da3683cSJim Ingham         m_takedown_done = true;
254ce553d88SJim Ingham         m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
25560c4118cSJim Ingham         m_real_stop_info_sp = GetPrivateStopInfo ();
2567b24e95eSEd Maste         if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
2577b24e95eSEd Maste         {
2587b24e95eSEd Maste             if (log)
259324a1036SSaleem Abdulrasool                 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
260324a1036SSaleem Abdulrasool                             static_cast<void*>(this));
2617b24e95eSEd Maste         }
26218de2fdcSJim Ingham         SetPlanComplete(success);
26310af7c43SSean Callanan         ClearBreakpoints();
2649da3683cSJim Ingham         if (log && log->GetVerbose())
2659da3683cSJim Ingham             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
2662c36439cSJim Ingham 
2679da3683cSJim Ingham     }
2689da3683cSJim Ingham     else
2699da3683cSJim Ingham     {
2709da3683cSJim Ingham         if (log)
271324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
272324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
273324a1036SSaleem Abdulrasool                          IsPlanComplete());
27430fdc8d8SChris Lattner     }
27577787033SJim Ingham }
27630fdc8d8SChris Lattner 
27730fdc8d8SChris Lattner void
278bda4e5ebSJim Ingham ThreadPlanCallFunction::WillPop ()
279bda4e5ebSJim Ingham {
2800161b49cSJim Ingham     DoTakedown(PlanSucceeded());
281bda4e5ebSJim Ingham }
282bda4e5ebSJim Ingham 
283bda4e5ebSJim Ingham void
2842a48f525SGreg Clayton ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
28530fdc8d8SChris Lattner {
2862a48f525SGreg Clayton     if (level == eDescriptionLevelBrief)
28730fdc8d8SChris Lattner     {
28830fdc8d8SChris Lattner         s->Printf("Function call thread plan");
28930fdc8d8SChris Lattner     }
29030fdc8d8SChris Lattner     else
29130fdc8d8SChris Lattner     {
2921ac04c30SGreg Clayton         TargetSP target_sp (m_thread.CalculateTarget());
293d01b2953SDaniel Malea         s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
29430fdc8d8SChris Lattner     }
29530fdc8d8SChris Lattner }
29630fdc8d8SChris Lattner 
29730fdc8d8SChris Lattner bool
29830fdc8d8SChris Lattner ThreadPlanCallFunction::ValidatePlan (Stream *error)
29930fdc8d8SChris Lattner {
30030fdc8d8SChris Lattner     if (!m_valid)
301ea06f3bfSJim Ingham     {
302ea06f3bfSJim Ingham         if (error)
303ea06f3bfSJim Ingham         {
304ea06f3bfSJim Ingham             if (m_constructor_errors.GetSize() > 0)
305ea06f3bfSJim Ingham                 error->PutCString (m_constructor_errors.GetData());
306ea06f3bfSJim Ingham             else
307ea06f3bfSJim Ingham                 error->PutCString ("Unknown error");
308ea06f3bfSJim Ingham         }
30930fdc8d8SChris Lattner         return false;
310ea06f3bfSJim Ingham     }
31130fdc8d8SChris Lattner 
31230fdc8d8SChris Lattner     return true;
31330fdc8d8SChris Lattner }
31430fdc8d8SChris Lattner 
3150161b49cSJim Ingham 
3160161b49cSJim Ingham Vote
3170161b49cSJim Ingham ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
31830fdc8d8SChris Lattner {
3190161b49cSJim Ingham     if (m_takedown_done || IsPlanComplete())
3200161b49cSJim Ingham         return eVoteYes;
3210161b49cSJim Ingham     else
3220161b49cSJim Ingham         return ThreadPlan::ShouldReportStop(event_ptr);
3230161b49cSJim Ingham }
3240161b49cSJim Ingham 
3250161b49cSJim Ingham bool
326221d51cfSJim Ingham ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
3270161b49cSJim Ingham {
3285160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
32960c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo ();
330160f78c5SJim Ingham 
33140d871faSJim Ingham     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
33240d871faSJim Ingham     // we answer yes.
3330161b49cSJim Ingham     if (m_subplan_sp.get() != NULL && m_subplan_sp->PlanExplainsStop(event_ptr))
334184e9811SJim Ingham     {
335184e9811SJim Ingham         SetPlanComplete();
33640d871faSJim Ingham         return true;
337184e9811SJim Ingham     }
3383e6fedcaSSean Callanan 
339c98aca60SSean Callanan     // Check if the breakpoint is one of ours.
340c98aca60SSean Callanan 
34118de2fdcSJim Ingham     StopReason stop_reason;
34218de2fdcSJim Ingham     if (!m_real_stop_info_sp)
34318de2fdcSJim Ingham         stop_reason = eStopReasonNone;
34418de2fdcSJim Ingham     else
34518de2fdcSJim Ingham         stop_reason = m_real_stop_info_sp->GetStopReason();
3460161b49cSJim Ingham     if (log)
3470161b49cSJim Ingham         log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
34818de2fdcSJim Ingham 
34918de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
350c98aca60SSean Callanan         return true;
351c98aca60SSean Callanan 
35235878c47SJim Ingham     // One more quirk here.  If this event was from Halt interrupting the target, then we should not consider
35335878c47SJim Ingham     // ourselves complete.  Return true to acknowledge the stop.
35435878c47SJim Ingham     if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
35535878c47SJim Ingham     {
35635878c47SJim Ingham         if (log)
35735878c47SJim Ingham             log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: The event is an Interrupt, returning true.");
35835878c47SJim Ingham         return true;
35935878c47SJim Ingham     }
3600161b49cSJim Ingham     // We control breakpoints separately from other "stop reasons."  So first,
3610161b49cSJim Ingham     // check the case where we stopped for an internal breakpoint, in that case, continue on.
3620161b49cSJim Ingham     // If it is not an internal breakpoint, consult m_ignore_breakpoints.
3636db73ca5SSean Callanan 
36418de2fdcSJim Ingham 
36518de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint)
36640d871faSJim Ingham     {
3671ac04c30SGreg Clayton         ProcessSP process_sp (m_thread.CalculateProcess());
368160f78c5SJim Ingham         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3691ac04c30SGreg Clayton         BreakpointSiteSP bp_site_sp;
3701ac04c30SGreg Clayton         if (process_sp)
3711ac04c30SGreg Clayton             bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
37240d871faSJim Ingham         if (bp_site_sp)
37340d871faSJim Ingham         {
37440d871faSJim Ingham             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
37540d871faSJim Ingham             bool is_internal = true;
37640d871faSJim Ingham             for (uint32_t i = 0; i < num_owners; i++)
37740d871faSJim Ingham             {
3786db73ca5SSean Callanan                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3790161b49cSJim Ingham                 if (log)
3800161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
3816db73ca5SSean Callanan 
3826db73ca5SSean Callanan                 if (!bp.IsInternal())
38340d871faSJim Ingham                 {
38440d871faSJim Ingham                     is_internal = false;
38540d871faSJim Ingham                     break;
38640d871faSJim Ingham                 }
38740d871faSJim Ingham             }
38840d871faSJim Ingham             if (is_internal)
3890161b49cSJim Ingham             {
3900161b49cSJim Ingham                 if (log)
3910161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
39240d871faSJim Ingham                 return false;
39340d871faSJim Ingham             }
3940161b49cSJim Ingham         }
39540d871faSJim Ingham 
396184e9811SJim Ingham         if (m_ignore_breakpoints)
397923886ceSJim Ingham         {
3980161b49cSJim Ingham             if (log)
3990161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
4000161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(false);
401923886ceSJim Ingham             return true;
402923886ceSJim Ingham         }
403923886ceSJim Ingham         else
4040161b49cSJim Ingham         {
4050161b49cSJim Ingham             if (log)
4060161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
4070161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(true);
4080161b49cSJim Ingham             return false;
4090161b49cSJim Ingham         }
4100161b49cSJim Ingham     }
4110161b49cSJim Ingham     else if (!m_unwind_on_error)
4120161b49cSJim Ingham     {
4130161b49cSJim Ingham         // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
414923886ceSJim Ingham         return false;
41540d871faSJim Ingham     }
41640d871faSJim Ingham     else
41740d871faSJim Ingham     {
41840d871faSJim Ingham         // If the subplan is running, any crashes are attributable to us.
4192c36439cSJim Ingham         // If we want to discard the plan, then we say we explain the stop
4202c36439cSJim Ingham         // but if we are going to be discarded, let whoever is above us
4212c36439cSJim Ingham         // explain the stop.
4220161b49cSJim Ingham         // But don't discard the plan if the stop would restart itself (for instance if it is a
4230161b49cSJim Ingham         // signal that is set not to stop.  Check that here first.  We just say we explain the stop
4240161b49cSJim Ingham         // but aren't done and everything will continue on from there.
4250161b49cSJim Ingham 
426841ee9b9SJim Ingham         if (m_real_stop_info_sp && m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
4270161b49cSJim Ingham         {
428184e9811SJim Ingham             SetPlanComplete(false);
4299a028519SSean Callanan             if (m_subplan_sp)
43018de2fdcSJim Ingham             {
431184e9811SJim Ingham                 if (m_unwind_on_error)
43218de2fdcSJim Ingham                     return true;
43318de2fdcSJim Ingham                 else
43418de2fdcSJim Ingham                     return false;
43518de2fdcSJim Ingham             }
43618de2fdcSJim Ingham             else
43718de2fdcSJim Ingham                 return false;
43830fdc8d8SChris Lattner         }
4390161b49cSJim Ingham         else
4400161b49cSJim Ingham             return true;
4410161b49cSJim Ingham     }
44240d871faSJim Ingham }
44330fdc8d8SChris Lattner 
44430fdc8d8SChris Lattner bool
44530fdc8d8SChris Lattner ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
44630fdc8d8SChris Lattner {
447221d51cfSJim Ingham     // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
448184e9811SJim Ingham     // We need to do that here to make sure our state is correct.
449221d51cfSJim Ingham     DoPlanExplainsStop(event_ptr);
450184e9811SJim Ingham 
451184e9811SJim Ingham     if (IsPlanComplete())
45230fdc8d8SChris Lattner     {
4539da3683cSJim Ingham         ReportRegisterState ("Function completed.  Register state was:");
45430fdc8d8SChris Lattner         return true;
45530fdc8d8SChris Lattner     }
45630fdc8d8SChris Lattner     else
45730fdc8d8SChris Lattner     {
45830fdc8d8SChris Lattner         return false;
45930fdc8d8SChris Lattner     }
46030fdc8d8SChris Lattner }
46130fdc8d8SChris Lattner 
46230fdc8d8SChris Lattner bool
46330fdc8d8SChris Lattner ThreadPlanCallFunction::StopOthers ()
46430fdc8d8SChris Lattner {
46530fdc8d8SChris Lattner     return m_stop_other_threads;
46630fdc8d8SChris Lattner }
46730fdc8d8SChris Lattner 
46830fdc8d8SChris Lattner StateType
46906e827ccSJim Ingham ThreadPlanCallFunction::GetPlanRunState ()
47030fdc8d8SChris Lattner {
47130fdc8d8SChris Lattner     return eStateRunning;
47230fdc8d8SChris Lattner }
47330fdc8d8SChris Lattner 
47430fdc8d8SChris Lattner void
47530fdc8d8SChris Lattner ThreadPlanCallFunction::DidPush ()
47630fdc8d8SChris Lattner {
477be3a1b14SSean Callanan //#define SINGLE_STEP_EXPRESSIONS
478be3a1b14SSean Callanan 
4798559a355SJim Ingham     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
4808559a355SJim Ingham     // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
4818559a355SJim Ingham 
4828559a355SJim Ingham     GetThread().SetStopInfoToNothing();
4838559a355SJim Ingham 
484be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
48530fdc8d8SChris Lattner     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
48630fdc8d8SChris Lattner 
48730fdc8d8SChris Lattner     m_thread.QueueThreadPlan(m_subplan_sp, false);
48877787033SJim Ingham     m_subplan_sp->SetPrivate (true);
489be3a1b14SSean Callanan #endif
49030fdc8d8SChris Lattner }
49130fdc8d8SChris Lattner 
49230fdc8d8SChris Lattner bool
49330fdc8d8SChris Lattner ThreadPlanCallFunction::WillStop ()
49430fdc8d8SChris Lattner {
49530fdc8d8SChris Lattner     return true;
49630fdc8d8SChris Lattner }
49730fdc8d8SChris Lattner 
49830fdc8d8SChris Lattner bool
49930fdc8d8SChris Lattner ThreadPlanCallFunction::MischiefManaged ()
50030fdc8d8SChris Lattner {
5015160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
50230fdc8d8SChris Lattner 
503184e9811SJim Ingham     if (IsPlanComplete())
504184e9811SJim Ingham     {
50530fdc8d8SChris Lattner         if (log)
506324a1036SSaleem Abdulrasool             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
507324a1036SSaleem Abdulrasool                         static_cast<void*>(this));
50830fdc8d8SChris Lattner 
50930fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
51030fdc8d8SChris Lattner         return true;
51130fdc8d8SChris Lattner     }
51230fdc8d8SChris Lattner     else
51330fdc8d8SChris Lattner     {
51430fdc8d8SChris Lattner         return false;
51530fdc8d8SChris Lattner     }
51630fdc8d8SChris Lattner }
5176db73ca5SSean Callanan 
5186db73ca5SSean Callanan void
5196db73ca5SSean Callanan ThreadPlanCallFunction::SetBreakpoints ()
5206db73ca5SSean Callanan {
5211ac04c30SGreg Clayton     ProcessSP process_sp (m_thread.CalculateProcess());
5226fbc48bcSJim Ingham     if (m_trap_exceptions && process_sp)
5231ac04c30SGreg Clayton     {
5241ac04c30SGreg Clayton         m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
5251ac04c30SGreg Clayton         m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
5266db73ca5SSean Callanan 
527f211510fSSean Callanan         if (m_cxx_language_runtime)
5286fbc48bcSJim Ingham         {
5296fbc48bcSJim Ingham             m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
530f211510fSSean Callanan             m_cxx_language_runtime->SetExceptionBreakpoints();
5316fbc48bcSJim Ingham         }
532f211510fSSean Callanan         if (m_objc_language_runtime)
5336fbc48bcSJim Ingham         {
5346fbc48bcSJim Ingham             m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
535f211510fSSean Callanan             m_objc_language_runtime->SetExceptionBreakpoints();
5366db73ca5SSean Callanan         }
5371ac04c30SGreg Clayton     }
5386fbc48bcSJim Ingham }
5396db73ca5SSean Callanan 
5406db73ca5SSean Callanan void
5416db73ca5SSean Callanan ThreadPlanCallFunction::ClearBreakpoints ()
5426db73ca5SSean Callanan {
5436fbc48bcSJim Ingham     if (m_trap_exceptions)
5446fbc48bcSJim Ingham     {
5456fbc48bcSJim Ingham         if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
546f211510fSSean Callanan             m_cxx_language_runtime->ClearExceptionBreakpoints();
5476fbc48bcSJim Ingham         if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
548f211510fSSean Callanan             m_objc_language_runtime->ClearExceptionBreakpoints();
5496db73ca5SSean Callanan     }
5506fbc48bcSJim Ingham }
551c98aca60SSean Callanan 
552c98aca60SSean Callanan bool
553c98aca60SSean Callanan ThreadPlanCallFunction::BreakpointsExplainStop()
554c98aca60SSean Callanan {
55560c4118cSJim Ingham     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
556c98aca60SSean Callanan 
5576fbc48bcSJim Ingham     if (m_trap_exceptions)
5586fbc48bcSJim Ingham     {
559184e9811SJim Ingham         if ((m_cxx_language_runtime &&
560f211510fSSean Callanan                 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
561184e9811SJim Ingham            ||(m_objc_language_runtime &&
562184e9811SJim Ingham                 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
563184e9811SJim Ingham         {
564641a67ceSJim Ingham             Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
565641a67ceSJim Ingham             if (log)
566641a67ceSJim Ingham                 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
567641a67ceSJim Ingham 
568184e9811SJim Ingham             SetPlanComplete(false);
569641a67ceSJim Ingham 
570641a67ceSJim Ingham             // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
571641a67ceSJim Ingham             // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
572641a67ceSJim Ingham             stop_info_sp->OverrideShouldStop (true);
573c98aca60SSean Callanan             return true;
574184e9811SJim Ingham         }
5756fbc48bcSJim Ingham     }
576f211510fSSean Callanan 
577c98aca60SSean Callanan     return false;
578c98aca60SSean Callanan }
5798559a355SJim Ingham 
5800ff099f1SJim Ingham void
5810ff099f1SJim Ingham ThreadPlanCallFunction::SetStopOthers (bool new_value)
5820ff099f1SJim Ingham {
5830ff099f1SJim Ingham     m_subplan_sp->SetStopOthers(new_value);
5840ff099f1SJim Ingham }
5850ff099f1SJim Ingham 
5860ff099f1SJim Ingham 
5878559a355SJim Ingham bool
5888559a355SJim Ingham ThreadPlanCallFunction::RestoreThreadState()
5898559a355SJim Ingham {
5908559a355SJim Ingham     return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
5918559a355SJim Ingham }
5928559a355SJim Ingham 
59390ff7911SEwan Crawford 
59490ff7911SEwan Crawford void
59590ff7911SEwan Crawford ThreadPlanCallFunction::SetReturnValue()
59690ff7911SEwan Crawford {
59790ff7911SEwan Crawford     ProcessSP process_sp(m_thread.GetProcess());
59890ff7911SEwan Crawford     const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
59990ff7911SEwan Crawford     if (abi && m_return_type.IsValid())
60090ff7911SEwan Crawford     {
60190ff7911SEwan Crawford         const bool persistent = false;
60290ff7911SEwan Crawford         m_return_valobj_sp = abi->GetReturnValueObject(m_thread, m_return_type, persistent);
60390ff7911SEwan Crawford     }
60490ff7911SEwan Crawford }
605