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 // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner // Other libraries and framework includes
1330fdc8d8SChris Lattner // Project includes
14*e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanCallFunction.h"
1540d871faSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
1640d871faSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
1730fdc8d8SChris Lattner #include "lldb/Core/Address.h"
1830fdc8d8SChris Lattner #include "lldb/Core/Log.h"
191f746071SGreg Clayton #include "lldb/Core/Module.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
211f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
2232abc6edSZachary Turner #include "lldb/Target/ABI.h"
23f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
2430fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2530fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2640d871faSJim Ingham #include "lldb/Target/StopInfo.h"
2730fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2830fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2930fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
3030fdc8d8SChris Lattner 
3130fdc8d8SChris Lattner using namespace lldb;
3230fdc8d8SChris Lattner using namespace lldb_private;
3330fdc8d8SChris Lattner 
3430fdc8d8SChris Lattner //----------------------------------------------------------------------
3530fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
370092c8ebSJim Ingham bool
380092c8ebSJim Ingham ThreadPlanCallFunction::ConstructorSetup (Thread &thread,
390092c8ebSJim Ingham                                           ABI *& abi,
400092c8ebSJim Ingham                                           lldb::addr_t &start_load_addr,
410092c8ebSJim Ingham                                           lldb::addr_t &function_load_addr)
420092c8ebSJim Ingham {
430092c8ebSJim Ingham     SetIsMasterPlan (true);
44923886ceSJim Ingham     SetOkayToDiscard (false);
45327c267aSAndrew Kaylor     SetPrivate (true);
460092c8ebSJim Ingham 
470092c8ebSJim Ingham     ProcessSP process_sp (thread.GetProcess());
480092c8ebSJim Ingham     if (!process_sp)
490092c8ebSJim Ingham         return false;
500092c8ebSJim Ingham 
510092c8ebSJim Ingham     abi = process_sp->GetABI().get();
520092c8ebSJim Ingham 
530092c8ebSJim Ingham     if (!abi)
540092c8ebSJim Ingham         return false;
550092c8ebSJim Ingham 
565160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
570092c8ebSJim Ingham 
580092c8ebSJim Ingham     SetBreakpoints();
590092c8ebSJim Ingham 
600092c8ebSJim Ingham     m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
610092c8ebSJim Ingham     // If we can't read memory at the point of the process where we are planning to put our function, we're
620092c8ebSJim Ingham     // not going to get any further...
630092c8ebSJim Ingham     Error error;
640092c8ebSJim Ingham     process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
650092c8ebSJim Ingham     if (!error.Success())
660092c8ebSJim Ingham     {
67ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".", m_function_sp);
680092c8ebSJim Ingham         if (log)
69324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
70324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
71324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
720092c8ebSJim Ingham         return false;
730092c8ebSJim Ingham     }
740092c8ebSJim Ingham 
756fbc48bcSJim Ingham     Module *exe_module = GetTarget().GetExecutableModulePointer();
760092c8ebSJim Ingham 
77*e65b2cf2SEugene Zelenko     if (exe_module == nullptr)
780092c8ebSJim Ingham     {
79ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Can't execute code without an executable module.");
800092c8ebSJim Ingham         if (log)
81324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
82324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
83324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
840092c8ebSJim Ingham         return false;
850092c8ebSJim Ingham     }
860092c8ebSJim Ingham     else
870092c8ebSJim Ingham     {
880092c8ebSJim Ingham         ObjectFile *objectFile = exe_module->GetObjectFile();
890092c8ebSJim Ingham         if (!objectFile)
900092c8ebSJim Ingham         {
91ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find object file for module \"%s\".",
92ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
93ea06f3bfSJim Ingham 
940092c8ebSJim Ingham             if (log)
95324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
96324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
97324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
980092c8ebSJim Ingham             return false;
990092c8ebSJim Ingham         }
100ea06f3bfSJim Ingham 
1010092c8ebSJim Ingham         m_start_addr = objectFile->GetEntryPointAddress();
1020092c8ebSJim Ingham         if (!m_start_addr.IsValid())
1030092c8ebSJim Ingham         {
104ea06f3bfSJim Ingham             m_constructor_errors.Printf ("Could not find entry point address for executable module \"%s\".",
105ea06f3bfSJim Ingham                                          exe_module->GetFileSpec().GetFilename().AsCString());
1060092c8ebSJim Ingham             if (log)
107324a1036SSaleem Abdulrasool                 log->Printf ("ThreadPlanCallFunction(%p): %s.",
108324a1036SSaleem Abdulrasool                              static_cast<void*>(this),
109324a1036SSaleem Abdulrasool                              m_constructor_errors.GetData());
1100092c8ebSJim Ingham             return false;
1110092c8ebSJim Ingham         }
1120092c8ebSJim Ingham     }
1130092c8ebSJim Ingham 
1146fbc48bcSJim Ingham     start_load_addr = m_start_addr.GetLoadAddress (&GetTarget());
1150092c8ebSJim Ingham 
1160092c8ebSJim Ingham     // Checkpoint the thread state so we can restore it later.
1170092c8ebSJim Ingham     if (log && log->GetVerbose())
1180092c8ebSJim Ingham         ReportRegisterState ("About to checkpoint thread before function call.  Original register state was:");
1190092c8ebSJim Ingham 
1200092c8ebSJim Ingham     if (!thread.CheckpointThreadState (m_stored_thread_state))
1210092c8ebSJim Ingham     {
122ea06f3bfSJim Ingham         m_constructor_errors.Printf ("Setting up ThreadPlanCallFunction, failed to checkpoint thread state.");
1230092c8ebSJim Ingham         if (log)
124324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): %s.",
125324a1036SSaleem Abdulrasool                          static_cast<void*>(this),
126324a1036SSaleem Abdulrasool                          m_constructor_errors.GetData());
1270092c8ebSJim Ingham         return false;
1280092c8ebSJim Ingham     }
1296fbc48bcSJim Ingham     function_load_addr = m_function_addr.GetLoadAddress (&GetTarget());
1300092c8ebSJim Ingham 
1310092c8ebSJim Ingham     return true;
1320092c8ebSJim Ingham }
13330fdc8d8SChris Lattner 
13430fdc8d8SChris Lattner ThreadPlanCallFunction::ThreadPlanCallFunction (Thread &thread,
13500049b8bSMatt Kopec                                                 const Address &function,
136a1e5dc86SGreg Clayton                                                 const CompilerType &return_type,
137a464f3d4SSean Callanan                                                 llvm::ArrayRef<addr_t> args,
138a464f3d4SSean Callanan                                                 const EvaluateExpressionOptions &options) :
139b01e742aSJim Ingham     ThreadPlan (ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
14030fdc8d8SChris Lattner     m_valid (false),
1416fbc48bcSJim Ingham     m_stop_other_threads (options.GetStopOthers()),
1426fbc48bcSJim Ingham     m_unwind_on_error (options.DoesUnwindOnError()),
1436fbc48bcSJim Ingham     m_ignore_breakpoints (options.DoesIgnoreBreakpoints()),
1446fbc48bcSJim Ingham     m_debug_execution (options.GetDebug()),
1456fbc48bcSJim Ingham     m_trap_exceptions (options.GetTrapExceptions()),
14616e0c686SJim Ingham     m_function_addr (function),
147b4cb0be3SFilipe Cabecinhas     m_function_sp (0),
148ce553d88SJim Ingham     m_takedown_done (false),
1496fbc48bcSJim Ingham     m_should_clear_objc_exception_bp(false),
1506fbc48bcSJim Ingham     m_should_clear_cxx_exception_bp (false),
15190ff7911SEwan Crawford     m_stop_address (LLDB_INVALID_ADDRESS),
15290ff7911SEwan Crawford     m_return_type (return_type)
15330fdc8d8SChris Lattner {
15490ff7911SEwan Crawford     lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
15590ff7911SEwan Crawford     lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
15690ff7911SEwan Crawford     ABI *abi = nullptr;
15790ff7911SEwan Crawford 
158923886ceSJim Ingham     if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr))
1591ac04c30SGreg Clayton         return;
1601ac04c30SGreg Clayton 
16130fdc8d8SChris Lattner     if (!abi->PrepareTrivialCall(thread,
162e359d9b7SSean Callanan                                  m_function_sp,
1630092c8ebSJim Ingham                                  function_load_addr,
1642a48f525SGreg Clayton                                  start_load_addr,
165a464f3d4SSean Callanan                                  args))
16630fdc8d8SChris Lattner         return;
16730fdc8d8SChris Lattner 
1689da3683cSJim Ingham     ReportRegisterState ("Function call was set up.  Register state was:");
1699da3683cSJim Ingham 
1709da3683cSJim Ingham     m_valid = true;
1719da3683cSJim Ingham }
1729da3683cSJim Ingham 
17390ff7911SEwan Crawford ThreadPlanCallFunction::ThreadPlanCallFunction(Thread &thread,
17490ff7911SEwan Crawford                                                const Address &function,
17590ff7911SEwan Crawford                                                const EvaluateExpressionOptions &options) :
17690ff7911SEwan Crawford     ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion),
17790ff7911SEwan Crawford     m_valid(false),
17890ff7911SEwan Crawford     m_stop_other_threads(options.GetStopOthers()),
17990ff7911SEwan Crawford     m_unwind_on_error(options.DoesUnwindOnError()),
18090ff7911SEwan Crawford     m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
18190ff7911SEwan Crawford     m_debug_execution(options.GetDebug()),
18290ff7911SEwan Crawford     m_trap_exceptions(options.GetTrapExceptions()),
18390ff7911SEwan Crawford     m_function_addr(function),
18490ff7911SEwan Crawford     m_function_sp(0),
18590ff7911SEwan Crawford     m_takedown_done(false),
18690ff7911SEwan Crawford     m_should_clear_objc_exception_bp(false),
18790ff7911SEwan Crawford     m_should_clear_cxx_exception_bp(false),
18890ff7911SEwan Crawford     m_stop_address(LLDB_INVALID_ADDRESS),
189a1e5dc86SGreg Clayton     m_return_type(CompilerType())
19090ff7911SEwan Crawford {
19190ff7911SEwan Crawford }
19290ff7911SEwan Crawford 
1939da3683cSJim Ingham ThreadPlanCallFunction::~ThreadPlanCallFunction ()
1949da3683cSJim Ingham {
1950161b49cSJim Ingham     DoTakedown(PlanSucceeded());
1969da3683cSJim Ingham }
1979da3683cSJim Ingham 
1989da3683cSJim Ingham void
1999da3683cSJim Ingham ThreadPlanCallFunction::ReportRegisterState (const char *message)
2009da3683cSJim Ingham {
2015160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_VERBOSE));
202ece96492SSean Callanan     if (log)
203ece96492SSean Callanan     {
204af247d7bSGreg Clayton         StreamString strm;
2055ccbd294SGreg Clayton         RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
206ece96492SSean Callanan 
2079da3683cSJim Ingham         log->PutCString(message);
208ece96492SSean Callanan 
209af247d7bSGreg Clayton         RegisterValue reg_value;
210ece96492SSean Callanan 
211af247d7bSGreg Clayton         for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
212af247d7bSGreg Clayton              reg_idx < num_registers;
213af247d7bSGreg Clayton              ++reg_idx)
214af247d7bSGreg Clayton         {
215af247d7bSGreg Clayton             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
216af247d7bSGreg Clayton             if (reg_ctx->ReadRegister(reg_info, reg_value))
217af247d7bSGreg Clayton             {
218af247d7bSGreg Clayton                 reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
219af247d7bSGreg Clayton                 strm.EOL();
220ece96492SSean Callanan             }
221ece96492SSean Callanan         }
222af247d7bSGreg Clayton         log->PutCString(strm.GetData());
223af247d7bSGreg Clayton     }
22410af7c43SSean Callanan }
22510af7c43SSean Callanan 
22610af7c43SSean Callanan void
22718de2fdcSJim Ingham ThreadPlanCallFunction::DoTakedown (bool success)
22810af7c43SSean Callanan {
2295160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
23087d0e618SJim Ingham 
23187d0e618SJim Ingham     if (!m_valid)
23287d0e618SJim Ingham     {
23387d0e618SJim Ingham         //Don't call DoTakedown if we were never valid to begin with.
23487d0e618SJim Ingham         if (log)
235324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): Log called on ThreadPlanCallFunction that was never valid.",
236324a1036SSaleem Abdulrasool                          static_cast<void*>(this));
23787d0e618SJim Ingham         return;
23887d0e618SJim Ingham     }
23987d0e618SJim Ingham 
2409da3683cSJim Ingham     if (!m_takedown_done)
24177787033SJim Ingham     {
24218de2fdcSJim Ingham         if (success)
24318de2fdcSJim Ingham         {
24490ff7911SEwan Crawford             SetReturnValue();
24518de2fdcSJim Ingham         }
2469da3683cSJim Ingham         if (log)
247324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
248324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
249324a1036SSaleem Abdulrasool                          IsPlanComplete());
2509da3683cSJim Ingham         m_takedown_done = true;
251ce553d88SJim Ingham         m_stop_address = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
25260c4118cSJim Ingham         m_real_stop_info_sp = GetPrivateStopInfo ();
2537b24e95eSEd Maste         if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state))
2547b24e95eSEd Maste         {
2557b24e95eSEd Maste             if (log)
256324a1036SSaleem Abdulrasool                 log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore register state",
257324a1036SSaleem Abdulrasool                             static_cast<void*>(this));
2587b24e95eSEd Maste         }
25918de2fdcSJim Ingham         SetPlanComplete(success);
26010af7c43SSean Callanan         ClearBreakpoints();
2619da3683cSJim Ingham         if (log && log->GetVerbose())
2629da3683cSJim Ingham             ReportRegisterState ("Restoring thread state after function call.  Restored register state:");
2639da3683cSJim Ingham     }
2649da3683cSJim Ingham     else
2659da3683cSJim Ingham     {
2669da3683cSJim Ingham         if (log)
267324a1036SSaleem Abdulrasool             log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called as no-op for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
268324a1036SSaleem Abdulrasool                          static_cast<void*>(this), m_thread.GetID(), m_valid,
269324a1036SSaleem Abdulrasool                          IsPlanComplete());
27030fdc8d8SChris Lattner     }
27177787033SJim Ingham }
27230fdc8d8SChris Lattner 
27330fdc8d8SChris Lattner void
274bda4e5ebSJim Ingham ThreadPlanCallFunction::WillPop ()
275bda4e5ebSJim Ingham {
2760161b49cSJim Ingham     DoTakedown(PlanSucceeded());
277bda4e5ebSJim Ingham }
278bda4e5ebSJim Ingham 
279bda4e5ebSJim Ingham void
2802a48f525SGreg Clayton ThreadPlanCallFunction::GetDescription (Stream *s, DescriptionLevel level)
28130fdc8d8SChris Lattner {
2822a48f525SGreg Clayton     if (level == eDescriptionLevelBrief)
28330fdc8d8SChris Lattner     {
28430fdc8d8SChris Lattner         s->Printf("Function call thread plan");
28530fdc8d8SChris Lattner     }
28630fdc8d8SChris Lattner     else
28730fdc8d8SChris Lattner     {
2881ac04c30SGreg Clayton         TargetSP target_sp (m_thread.CalculateTarget());
289d01b2953SDaniel Malea         s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get()));
29030fdc8d8SChris Lattner     }
29130fdc8d8SChris Lattner }
29230fdc8d8SChris Lattner 
29330fdc8d8SChris Lattner bool
29430fdc8d8SChris Lattner ThreadPlanCallFunction::ValidatePlan (Stream *error)
29530fdc8d8SChris Lattner {
29630fdc8d8SChris Lattner     if (!m_valid)
297ea06f3bfSJim Ingham     {
298ea06f3bfSJim Ingham         if (error)
299ea06f3bfSJim Ingham         {
300ea06f3bfSJim Ingham             if (m_constructor_errors.GetSize() > 0)
301ea06f3bfSJim Ingham                 error->PutCString (m_constructor_errors.GetData());
302ea06f3bfSJim Ingham             else
303ea06f3bfSJim Ingham                 error->PutCString ("Unknown error");
304ea06f3bfSJim Ingham         }
30530fdc8d8SChris Lattner         return false;
306ea06f3bfSJim Ingham     }
30730fdc8d8SChris Lattner 
30830fdc8d8SChris Lattner     return true;
30930fdc8d8SChris Lattner }
31030fdc8d8SChris Lattner 
3110161b49cSJim Ingham Vote
3120161b49cSJim Ingham ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr)
31330fdc8d8SChris Lattner {
3140161b49cSJim Ingham     if (m_takedown_done || IsPlanComplete())
3150161b49cSJim Ingham         return eVoteYes;
3160161b49cSJim Ingham     else
3170161b49cSJim Ingham         return ThreadPlan::ShouldReportStop(event_ptr);
3180161b49cSJim Ingham }
3190161b49cSJim Ingham 
3200161b49cSJim Ingham bool
321221d51cfSJim Ingham ThreadPlanCallFunction::DoPlanExplainsStop (Event *event_ptr)
3220161b49cSJim Ingham {
3235160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP|LIBLLDB_LOG_PROCESS));
32460c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo ();
325160f78c5SJim Ingham 
32640d871faSJim Ingham     // If our subplan knows why we stopped, even if it's done (which would forward the question to us)
32740d871faSJim Ingham     // we answer yes.
328*e65b2cf2SEugene Zelenko     if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr))
329184e9811SJim Ingham     {
330184e9811SJim Ingham         SetPlanComplete();
33140d871faSJim Ingham         return true;
332184e9811SJim Ingham     }
3333e6fedcaSSean Callanan 
334c98aca60SSean Callanan     // Check if the breakpoint is one of ours.
335c98aca60SSean Callanan 
33618de2fdcSJim Ingham     StopReason stop_reason;
33718de2fdcSJim Ingham     if (!m_real_stop_info_sp)
33818de2fdcSJim Ingham         stop_reason = eStopReasonNone;
33918de2fdcSJim Ingham     else
34018de2fdcSJim Ingham         stop_reason = m_real_stop_info_sp->GetStopReason();
3410161b49cSJim Ingham     if (log)
3420161b49cSJim Ingham         log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.", Thread::StopReasonAsCString(stop_reason));
34318de2fdcSJim Ingham 
34418de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
345c98aca60SSean Callanan         return true;
346c98aca60SSean Callanan 
34735878c47SJim Ingham     // One more quirk here.  If this event was from Halt interrupting the target, then we should not consider
34835878c47SJim Ingham     // ourselves complete.  Return true to acknowledge the stop.
34935878c47SJim Ingham     if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
35035878c47SJim Ingham     {
35135878c47SJim Ingham         if (log)
35235878c47SJim Ingham             log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: The event is an Interrupt, returning true.");
35335878c47SJim Ingham         return true;
35435878c47SJim Ingham     }
3550161b49cSJim Ingham     // We control breakpoints separately from other "stop reasons."  So first,
3560161b49cSJim Ingham     // check the case where we stopped for an internal breakpoint, in that case, continue on.
3570161b49cSJim Ingham     // If it is not an internal breakpoint, consult m_ignore_breakpoints.
3586db73ca5SSean Callanan 
35918de2fdcSJim Ingham     if (stop_reason == eStopReasonBreakpoint)
36040d871faSJim Ingham     {
3611ac04c30SGreg Clayton         ProcessSP process_sp (m_thread.CalculateProcess());
362160f78c5SJim Ingham         uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3631ac04c30SGreg Clayton         BreakpointSiteSP bp_site_sp;
3641ac04c30SGreg Clayton         if (process_sp)
3651ac04c30SGreg Clayton             bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
36640d871faSJim Ingham         if (bp_site_sp)
36740d871faSJim Ingham         {
36840d871faSJim Ingham             uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
36940d871faSJim Ingham             bool is_internal = true;
37040d871faSJim Ingham             for (uint32_t i = 0; i < num_owners; i++)
37140d871faSJim Ingham             {
3726db73ca5SSean Callanan                 Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3730161b49cSJim Ingham                 if (log)
3740161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop: hit breakpoint %d while calling function", bp.GetID());
3756db73ca5SSean Callanan 
3766db73ca5SSean Callanan                 if (!bp.IsInternal())
37740d871faSJim Ingham                 {
37840d871faSJim Ingham                     is_internal = false;
37940d871faSJim Ingham                     break;
38040d871faSJim Ingham                 }
38140d871faSJim Ingham             }
38240d871faSJim Ingham             if (is_internal)
3830161b49cSJim Ingham             {
3840161b49cSJim Ingham                 if (log)
3850161b49cSJim Ingham                     log->Printf ("ThreadPlanCallFunction::PlanExplainsStop hit an internal breakpoint, not stopping.");
38640d871faSJim Ingham                 return false;
38740d871faSJim Ingham             }
3880161b49cSJim Ingham         }
38940d871faSJim Ingham 
390184e9811SJim Ingham         if (m_ignore_breakpoints)
391923886ceSJim Ingham         {
3920161b49cSJim Ingham             if (log)
3930161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
3940161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(false);
395923886ceSJim Ingham             return true;
396923886ceSJim Ingham         }
397923886ceSJim Ingham         else
3980161b49cSJim Ingham         {
3990161b49cSJim Ingham             if (log)
4000161b49cSJim Ingham                 log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not ignoring breakpoints, overriding breakpoint stop info ShouldStop, returning true");
4010161b49cSJim Ingham             m_real_stop_info_sp->OverrideShouldStop(true);
4020161b49cSJim Ingham             return false;
4030161b49cSJim Ingham         }
4040161b49cSJim Ingham     }
4050161b49cSJim Ingham     else if (!m_unwind_on_error)
4060161b49cSJim Ingham     {
4070161b49cSJim Ingham         // If we don't want to discard this plan, than any stop we don't understand should be propagated up the stack.
408923886ceSJim Ingham         return false;
40940d871faSJim Ingham     }
41040d871faSJim Ingham     else
41140d871faSJim Ingham     {
41240d871faSJim Ingham         // If the subplan is running, any crashes are attributable to us.
4132c36439cSJim Ingham         // If we want to discard the plan, then we say we explain the stop
4142c36439cSJim Ingham         // but if we are going to be discarded, let whoever is above us
4152c36439cSJim Ingham         // explain the stop.
4160161b49cSJim Ingham         // But don't discard the plan if the stop would restart itself (for instance if it is a
4170161b49cSJim Ingham         // signal that is set not to stop.  Check that here first.  We just say we explain the stop
4180161b49cSJim Ingham         // but aren't done and everything will continue on from there.
4190161b49cSJim Ingham 
420841ee9b9SJim Ingham         if (m_real_stop_info_sp && m_real_stop_info_sp->ShouldStopSynchronous(event_ptr))
4210161b49cSJim Ingham         {
422184e9811SJim Ingham             SetPlanComplete(false);
423*e65b2cf2SEugene Zelenko             return m_subplan_sp ? m_unwind_on_error : false;
42430fdc8d8SChris Lattner         }
4250161b49cSJim Ingham         else
4260161b49cSJim Ingham             return true;
4270161b49cSJim Ingham     }
42840d871faSJim Ingham }
42930fdc8d8SChris Lattner 
43030fdc8d8SChris Lattner bool
43130fdc8d8SChris Lattner ThreadPlanCallFunction::ShouldStop (Event *event_ptr)
43230fdc8d8SChris Lattner {
433221d51cfSJim Ingham     // We do some computation in DoPlanExplainsStop that may or may not set the plan as complete.
434184e9811SJim Ingham     // We need to do that here to make sure our state is correct.
435221d51cfSJim Ingham     DoPlanExplainsStop(event_ptr);
436184e9811SJim Ingham 
437184e9811SJim Ingham     if (IsPlanComplete())
43830fdc8d8SChris Lattner     {
4399da3683cSJim Ingham         ReportRegisterState ("Function completed.  Register state was:");
44030fdc8d8SChris Lattner         return true;
44130fdc8d8SChris Lattner     }
44230fdc8d8SChris Lattner     else
44330fdc8d8SChris Lattner     {
44430fdc8d8SChris Lattner         return false;
44530fdc8d8SChris Lattner     }
44630fdc8d8SChris Lattner }
44730fdc8d8SChris Lattner 
44830fdc8d8SChris Lattner bool
44930fdc8d8SChris Lattner ThreadPlanCallFunction::StopOthers ()
45030fdc8d8SChris Lattner {
45130fdc8d8SChris Lattner     return m_stop_other_threads;
45230fdc8d8SChris Lattner }
45330fdc8d8SChris Lattner 
45430fdc8d8SChris Lattner StateType
45506e827ccSJim Ingham ThreadPlanCallFunction::GetPlanRunState ()
45630fdc8d8SChris Lattner {
45730fdc8d8SChris Lattner     return eStateRunning;
45830fdc8d8SChris Lattner }
45930fdc8d8SChris Lattner 
46030fdc8d8SChris Lattner void
46130fdc8d8SChris Lattner ThreadPlanCallFunction::DidPush ()
46230fdc8d8SChris Lattner {
463be3a1b14SSean Callanan //#define SINGLE_STEP_EXPRESSIONS
464be3a1b14SSean Callanan 
4658559a355SJim Ingham     // Now set the thread state to "no reason" so we don't run with whatever signal was outstanding...
4668559a355SJim Ingham     // Wait till the plan is pushed so we aren't changing the stop info till we're about to run.
4678559a355SJim Ingham 
4688559a355SJim Ingham     GetThread().SetStopInfoToNothing();
4698559a355SJim Ingham 
470be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
47130fdc8d8SChris Lattner     m_subplan_sp.reset(new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
47230fdc8d8SChris Lattner 
47330fdc8d8SChris Lattner     m_thread.QueueThreadPlan(m_subplan_sp, false);
47477787033SJim Ingham     m_subplan_sp->SetPrivate (true);
475be3a1b14SSean Callanan #endif
47630fdc8d8SChris Lattner }
47730fdc8d8SChris Lattner 
47830fdc8d8SChris Lattner bool
47930fdc8d8SChris Lattner ThreadPlanCallFunction::WillStop ()
48030fdc8d8SChris Lattner {
48130fdc8d8SChris Lattner     return true;
48230fdc8d8SChris Lattner }
48330fdc8d8SChris Lattner 
48430fdc8d8SChris Lattner bool
48530fdc8d8SChris Lattner ThreadPlanCallFunction::MischiefManaged ()
48630fdc8d8SChris Lattner {
4875160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
48830fdc8d8SChris Lattner 
489184e9811SJim Ingham     if (IsPlanComplete())
490184e9811SJim Ingham     {
49130fdc8d8SChris Lattner         if (log)
492324a1036SSaleem Abdulrasool             log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
493324a1036SSaleem Abdulrasool                         static_cast<void*>(this));
49430fdc8d8SChris Lattner 
49530fdc8d8SChris Lattner         ThreadPlan::MischiefManaged ();
49630fdc8d8SChris Lattner         return true;
49730fdc8d8SChris Lattner     }
49830fdc8d8SChris Lattner     else
49930fdc8d8SChris Lattner     {
50030fdc8d8SChris Lattner         return false;
50130fdc8d8SChris Lattner     }
50230fdc8d8SChris Lattner }
5036db73ca5SSean Callanan 
5046db73ca5SSean Callanan void
5056db73ca5SSean Callanan ThreadPlanCallFunction::SetBreakpoints ()
5066db73ca5SSean Callanan {
5071ac04c30SGreg Clayton     ProcessSP process_sp (m_thread.CalculateProcess());
5086fbc48bcSJim Ingham     if (m_trap_exceptions && process_sp)
5091ac04c30SGreg Clayton     {
5101ac04c30SGreg Clayton         m_cxx_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
5111ac04c30SGreg Clayton         m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
5126db73ca5SSean Callanan 
513f211510fSSean Callanan         if (m_cxx_language_runtime)
5146fbc48bcSJim Ingham         {
5156fbc48bcSJim Ingham             m_should_clear_cxx_exception_bp = !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
516f211510fSSean Callanan             m_cxx_language_runtime->SetExceptionBreakpoints();
5176fbc48bcSJim Ingham         }
518f211510fSSean Callanan         if (m_objc_language_runtime)
5196fbc48bcSJim Ingham         {
5206fbc48bcSJim Ingham             m_should_clear_objc_exception_bp = !m_objc_language_runtime->ExceptionBreakpointsAreSet();
521f211510fSSean Callanan             m_objc_language_runtime->SetExceptionBreakpoints();
5226db73ca5SSean Callanan         }
5231ac04c30SGreg Clayton     }
5246fbc48bcSJim Ingham }
5256db73ca5SSean Callanan 
5266db73ca5SSean Callanan void
5276db73ca5SSean Callanan ThreadPlanCallFunction::ClearBreakpoints ()
5286db73ca5SSean Callanan {
5296fbc48bcSJim Ingham     if (m_trap_exceptions)
5306fbc48bcSJim Ingham     {
5316fbc48bcSJim Ingham         if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
532f211510fSSean Callanan             m_cxx_language_runtime->ClearExceptionBreakpoints();
5336fbc48bcSJim Ingham         if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
534f211510fSSean Callanan             m_objc_language_runtime->ClearExceptionBreakpoints();
5356db73ca5SSean Callanan     }
5366fbc48bcSJim Ingham }
537c98aca60SSean Callanan 
538c98aca60SSean Callanan bool
539c98aca60SSean Callanan ThreadPlanCallFunction::BreakpointsExplainStop()
540c98aca60SSean Callanan {
54160c4118cSJim Ingham     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
542c98aca60SSean Callanan 
5436fbc48bcSJim Ingham     if (m_trap_exceptions)
5446fbc48bcSJim Ingham     {
545184e9811SJim Ingham         if ((m_cxx_language_runtime &&
546f211510fSSean Callanan                 m_cxx_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp))
547184e9811SJim Ingham            ||(m_objc_language_runtime &&
548184e9811SJim Ingham                 m_objc_language_runtime->ExceptionBreakpointsExplainStop(stop_info_sp)))
549184e9811SJim Ingham         {
550641a67ceSJim Ingham             Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
551641a67ceSJim Ingham             if (log)
552641a67ceSJim Ingham                 log->Printf ("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an exception breakpoint, setting plan complete.");
553641a67ceSJim Ingham 
554184e9811SJim Ingham             SetPlanComplete(false);
555641a67ceSJim Ingham 
556641a67ceSJim Ingham             // If the user has set the ObjC language breakpoint, it would normally get priority over our internal
557641a67ceSJim Ingham             // catcher breakpoint, but in this case we can't let that happen, so force the ShouldStop here.
558641a67ceSJim Ingham             stop_info_sp->OverrideShouldStop (true);
559c98aca60SSean Callanan             return true;
560184e9811SJim Ingham         }
5616fbc48bcSJim Ingham     }
562f211510fSSean Callanan 
563c98aca60SSean Callanan     return false;
564c98aca60SSean Callanan }
5658559a355SJim Ingham 
5660ff099f1SJim Ingham void
5670ff099f1SJim Ingham ThreadPlanCallFunction::SetStopOthers (bool new_value)
5680ff099f1SJim Ingham {
5690ff099f1SJim Ingham     m_subplan_sp->SetStopOthers(new_value);
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 
57890ff7911SEwan Crawford void
57990ff7911SEwan Crawford ThreadPlanCallFunction::SetReturnValue()
58090ff7911SEwan Crawford {
58190ff7911SEwan Crawford     ProcessSP process_sp(m_thread.GetProcess());
582*e65b2cf2SEugene Zelenko     const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
58390ff7911SEwan Crawford     if (abi && m_return_type.IsValid())
58490ff7911SEwan Crawford     {
58590ff7911SEwan Crawford         const bool persistent = false;
58690ff7911SEwan Crawford         m_return_valobj_sp = abi->GetReturnValueObject(m_thread, m_return_type, persistent);
58790ff7911SEwan Crawford     }
58890ff7911SEwan Crawford }
589