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
14e65b2cf2SEugene 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"
201f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
2132abc6edSZachary Turner #include "lldb/Target/ABI.h"
22f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
2330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2430fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2540d871faSJim Ingham #include "lldb/Target/StopInfo.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2730fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2830fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
29bf9a7730SZachary Turner #include "lldb/Utility/Stream.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 //----------------------------------------------------------------------
37b9c1b51eSKate Stone bool ThreadPlanCallFunction::ConstructorSetup(
38b9c1b51eSKate Stone     Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
39b9c1b51eSKate Stone     lldb::addr_t &function_load_addr) {
400092c8ebSJim Ingham   SetIsMasterPlan(true);
41923886ceSJim Ingham   SetOkayToDiscard(false);
42327c267aSAndrew Kaylor   SetPrivate(true);
430092c8ebSJim Ingham 
440092c8ebSJim Ingham   ProcessSP process_sp(thread.GetProcess());
450092c8ebSJim Ingham   if (!process_sp)
460092c8ebSJim Ingham     return false;
470092c8ebSJim Ingham 
480092c8ebSJim Ingham   abi = process_sp->GetABI().get();
490092c8ebSJim Ingham 
500092c8ebSJim Ingham   if (!abi)
510092c8ebSJim Ingham     return false;
520092c8ebSJim Ingham 
535160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
540092c8ebSJim Ingham 
550092c8ebSJim Ingham   SetBreakpoints();
560092c8ebSJim Ingham 
570092c8ebSJim Ingham   m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
58b9c1b51eSKate Stone   // If we can't read memory at the point of the process where we are planning
59b9c1b51eSKate Stone   // to put our function, we're
600092c8ebSJim Ingham   // not going to get any further...
610092c8ebSJim Ingham   Error error;
620092c8ebSJim Ingham   process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
63b9c1b51eSKate Stone   if (!error.Success()) {
64b9c1b51eSKate Stone     m_constructor_errors.Printf(
65b9c1b51eSKate Stone         "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
66b9c1b51eSKate Stone         m_function_sp);
670092c8ebSJim Ingham     if (log)
68b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
69324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
700092c8ebSJim Ingham     return false;
710092c8ebSJim Ingham   }
720092c8ebSJim Ingham 
736fbc48bcSJim Ingham   Module *exe_module = GetTarget().GetExecutableModulePointer();
740092c8ebSJim Ingham 
75b9c1b51eSKate Stone   if (exe_module == nullptr) {
76b9c1b51eSKate Stone     m_constructor_errors.Printf(
77b9c1b51eSKate Stone         "Can't execute code without an executable module.");
780092c8ebSJim Ingham     if (log)
79b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
80324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
810092c8ebSJim Ingham     return false;
82b9c1b51eSKate Stone   } else {
830092c8ebSJim Ingham     ObjectFile *objectFile = exe_module->GetObjectFile();
84b9c1b51eSKate Stone     if (!objectFile) {
85b9c1b51eSKate Stone       m_constructor_errors.Printf(
86b9c1b51eSKate Stone           "Could not find object file for module \"%s\".",
87ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
88ea06f3bfSJim Ingham 
890092c8ebSJim Ingham       if (log)
90324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
91b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
920092c8ebSJim Ingham       return false;
930092c8ebSJim Ingham     }
94ea06f3bfSJim Ingham 
950092c8ebSJim Ingham     m_start_addr = objectFile->GetEntryPointAddress();
96b9c1b51eSKate Stone     if (!m_start_addr.IsValid()) {
97b9c1b51eSKate Stone       m_constructor_errors.Printf(
98b9c1b51eSKate Stone           "Could not find entry point address for executable module \"%s\".",
99ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
1000092c8ebSJim Ingham       if (log)
101324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
102b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
1030092c8ebSJim Ingham       return false;
1040092c8ebSJim Ingham     }
1050092c8ebSJim Ingham   }
1060092c8ebSJim Ingham 
1076fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
1080092c8ebSJim Ingham 
1090092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
1100092c8ebSJim Ingham   if (log && log->GetVerbose())
111b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
112b9c1b51eSKate Stone                         "Original register state was:");
1130092c8ebSJim Ingham 
114b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
115b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
116b9c1b51eSKate Stone                                 "checkpoint thread state.");
1170092c8ebSJim Ingham     if (log)
118b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
119324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
1200092c8ebSJim Ingham     return false;
1210092c8ebSJim Ingham   }
1226fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
1230092c8ebSJim Ingham 
1240092c8ebSJim Ingham   return true;
1250092c8ebSJim Ingham }
12630fdc8d8SChris Lattner 
127b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
128b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
129b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
130b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
131b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
132b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1336fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1346fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1356fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
136b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
137b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1386fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1396fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
140b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
14190ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
14290ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
14390ff7911SEwan Crawford   ABI *abi = nullptr;
14490ff7911SEwan Crawford 
145923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1461ac04c30SGreg Clayton     return;
1471ac04c30SGreg Clayton 
148b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
149b9c1b51eSKate Stone                                start_load_addr, args))
15030fdc8d8SChris Lattner     return;
15130fdc8d8SChris Lattner 
1529da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1539da3683cSJim Ingham 
1549da3683cSJim Ingham   m_valid = true;
1559da3683cSJim Ingham }
1569da3683cSJim Ingham 
157b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
158b9c1b51eSKate Stone     Thread &thread, const Address &function,
159b9c1b51eSKate Stone     const EvaluateExpressionOptions &options)
160b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
161b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
162b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
16390ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
16490ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
16590ff7911SEwan Crawford       m_debug_execution(options.GetDebug()),
166b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
167b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
16890ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
16990ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
170b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
17190ff7911SEwan Crawford 
172b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1730161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1749da3683cSJim Ingham }
1759da3683cSJim Ingham 
176b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
177*3b7e1981SPavel Labath   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
178*3b7e1981SPavel Labath   if (log && log->GetVerbose()) {
179af247d7bSGreg Clayton     StreamString strm;
1805ccbd294SGreg Clayton     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
181ece96492SSean Callanan 
1829da3683cSJim Ingham     log->PutCString(message);
183ece96492SSean Callanan 
184af247d7bSGreg Clayton     RegisterValue reg_value;
185ece96492SSean Callanan 
186af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
187b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
188af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
189b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
190af247d7bSGreg Clayton         reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
191af247d7bSGreg Clayton         strm.EOL();
192ece96492SSean Callanan       }
193ece96492SSean Callanan     }
194c156427dSZachary Turner     log->PutString(strm.GetString());
195af247d7bSGreg Clayton   }
19610af7c43SSean Callanan }
19710af7c43SSean Callanan 
198b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
1995160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
20087d0e618SJim Ingham 
201b9c1b51eSKate Stone   if (!m_valid) {
20287d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
20387d0e618SJim Ingham     if (log)
204b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): Log called on "
205b9c1b51eSKate Stone                   "ThreadPlanCallFunction that was never valid.",
206324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
20787d0e618SJim Ingham     return;
20887d0e618SJim Ingham   }
20987d0e618SJim Ingham 
210b9c1b51eSKate Stone   if (!m_takedown_done) {
211b9c1b51eSKate Stone     if (success) {
21290ff7911SEwan Crawford       SetReturnValue();
21318de2fdcSJim Ingham     }
2149da3683cSJim Ingham     if (log)
215b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread "
216b9c1b51eSKate Stone                   "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
217324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
218324a1036SSaleem Abdulrasool                   IsPlanComplete());
2199da3683cSJim Ingham     m_takedown_done = true;
220b9c1b51eSKate Stone     m_stop_address =
221b9c1b51eSKate Stone         m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
22260c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
223b9c1b51eSKate Stone     if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
2247b24e95eSEd Maste       if (log)
225b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore "
226b9c1b51eSKate Stone                     "register state",
227324a1036SSaleem Abdulrasool                     static_cast<void *>(this));
2287b24e95eSEd Maste     }
22918de2fdcSJim Ingham     SetPlanComplete(success);
23010af7c43SSean Callanan     ClearBreakpoints();
2319da3683cSJim Ingham     if (log && log->GetVerbose())
232b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
233b9c1b51eSKate Stone                           "Restored register state:");
234b9c1b51eSKate Stone   } else {
2359da3683cSJim Ingham     if (log)
236b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
237b9c1b51eSKate Stone                   "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
238324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
239324a1036SSaleem Abdulrasool                   IsPlanComplete());
24030fdc8d8SChris Lattner   }
24177787033SJim Ingham }
24230fdc8d8SChris Lattner 
243b9c1b51eSKate Stone void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
244bda4e5ebSJim Ingham 
245b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
246b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
24730fdc8d8SChris Lattner     s->Printf("Function call thread plan");
248b9c1b51eSKate Stone   } else {
2491ac04c30SGreg Clayton     TargetSP target_sp(m_thread.CalculateTarget());
250b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
251b9c1b51eSKate Stone               m_function_addr.GetLoadAddress(target_sp.get()));
25230fdc8d8SChris Lattner   }
25330fdc8d8SChris Lattner }
25430fdc8d8SChris Lattner 
255b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
256b9c1b51eSKate Stone   if (!m_valid) {
257b9c1b51eSKate Stone     if (error) {
258ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
259c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
260ea06f3bfSJim Ingham       else
261ea06f3bfSJim Ingham         error->PutCString("Unknown error");
262ea06f3bfSJim Ingham     }
26330fdc8d8SChris Lattner     return false;
264ea06f3bfSJim Ingham   }
26530fdc8d8SChris Lattner 
26630fdc8d8SChris Lattner   return true;
26730fdc8d8SChris Lattner }
26830fdc8d8SChris Lattner 
269b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2700161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2710161b49cSJim Ingham     return eVoteYes;
2720161b49cSJim Ingham   else
2730161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2740161b49cSJim Ingham }
2750161b49cSJim Ingham 
276b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
277b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
278b9c1b51eSKate Stone                                                   LIBLLDB_LOG_PROCESS));
27960c4118cSJim Ingham   m_real_stop_info_sp = GetPrivateStopInfo();
280160f78c5SJim Ingham 
281b9c1b51eSKate Stone   // If our subplan knows why we stopped, even if it's done (which would forward
282b9c1b51eSKate Stone   // the question to us)
28340d871faSJim Ingham   // we answer yes.
284b9c1b51eSKate Stone   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
285184e9811SJim Ingham     SetPlanComplete();
28640d871faSJim Ingham     return true;
287184e9811SJim Ingham   }
2883e6fedcaSSean Callanan 
289c98aca60SSean Callanan   // Check if the breakpoint is one of ours.
290c98aca60SSean Callanan 
29118de2fdcSJim Ingham   StopReason stop_reason;
29218de2fdcSJim Ingham   if (!m_real_stop_info_sp)
29318de2fdcSJim Ingham     stop_reason = eStopReasonNone;
29418de2fdcSJim Ingham   else
29518de2fdcSJim Ingham     stop_reason = m_real_stop_info_sp->GetStopReason();
2960161b49cSJim Ingham   if (log)
297b9c1b51eSKate Stone     log->Printf(
298b9c1b51eSKate Stone         "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.",
299b9c1b51eSKate Stone         Thread::StopReasonAsCString(stop_reason));
30018de2fdcSJim Ingham 
30118de2fdcSJim Ingham   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
302c98aca60SSean Callanan     return true;
303c98aca60SSean Callanan 
304b9c1b51eSKate Stone   // One more quirk here.  If this event was from Halt interrupting the target,
305b9c1b51eSKate Stone   // then we should not consider
30635878c47SJim Ingham   // ourselves complete.  Return true to acknowledge the stop.
307b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
30835878c47SJim Ingham     if (log)
309b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an "
310b9c1b51eSKate Stone                   "Interrupt, returning true.");
31135878c47SJim Ingham     return true;
31235878c47SJim Ingham   }
3130161b49cSJim Ingham   // We control breakpoints separately from other "stop reasons."  So first,
314b9c1b51eSKate Stone   // check the case where we stopped for an internal breakpoint, in that case,
315b9c1b51eSKate Stone   // continue on.
3160161b49cSJim Ingham   // If it is not an internal breakpoint, consult m_ignore_breakpoints.
3176db73ca5SSean Callanan 
318b9c1b51eSKate Stone   if (stop_reason == eStopReasonBreakpoint) {
3191ac04c30SGreg Clayton     ProcessSP process_sp(m_thread.CalculateProcess());
320160f78c5SJim Ingham     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3211ac04c30SGreg Clayton     BreakpointSiteSP bp_site_sp;
3221ac04c30SGreg Clayton     if (process_sp)
3231ac04c30SGreg Clayton       bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
324b9c1b51eSKate Stone     if (bp_site_sp) {
32540d871faSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
32640d871faSJim Ingham       bool is_internal = true;
327b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
3286db73ca5SSean Callanan         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3290161b49cSJim Ingham         if (log)
330b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit "
331b9c1b51eSKate Stone                       "breakpoint %d while calling function",
332b9c1b51eSKate Stone                       bp.GetID());
3336db73ca5SSean Callanan 
334b9c1b51eSKate Stone         if (!bp.IsInternal()) {
33540d871faSJim Ingham           is_internal = false;
33640d871faSJim Ingham           break;
33740d871faSJim Ingham         }
33840d871faSJim Ingham       }
339b9c1b51eSKate Stone       if (is_internal) {
3400161b49cSJim Ingham         if (log)
341b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an "
342b9c1b51eSKate Stone                       "internal breakpoint, not stopping.");
34340d871faSJim Ingham         return false;
34440d871faSJim Ingham       }
3450161b49cSJim Ingham     }
34640d871faSJim Ingham 
347b9c1b51eSKate Stone     if (m_ignore_breakpoints) {
3480161b49cSJim Ingham       if (log)
349b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
350b9c1b51eSKate Stone                     "breakpoints, overriding breakpoint stop info ShouldStop, "
351b9c1b51eSKate Stone                     "returning true");
3520161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(false);
353923886ceSJim Ingham       return true;
354b9c1b51eSKate Stone     } else {
3550161b49cSJim Ingham       if (log)
356b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not "
357b9c1b51eSKate Stone                     "ignoring breakpoints, overriding breakpoint stop info "
358b9c1b51eSKate Stone                     "ShouldStop, returning true");
3590161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(true);
3600161b49cSJim Ingham       return false;
3610161b49cSJim Ingham     }
362b9c1b51eSKate Stone   } else if (!m_unwind_on_error) {
363b9c1b51eSKate Stone     // If we don't want to discard this plan, than any stop we don't understand
364b9c1b51eSKate Stone     // should be propagated up the stack.
365923886ceSJim Ingham     return false;
366b9c1b51eSKate Stone   } else {
36740d871faSJim Ingham     // If the subplan is running, any crashes are attributable to us.
3682c36439cSJim Ingham     // If we want to discard the plan, then we say we explain the stop
3692c36439cSJim Ingham     // but if we are going to be discarded, let whoever is above us
3702c36439cSJim Ingham     // explain the stop.
371b9c1b51eSKate Stone     // But don't discard the plan if the stop would restart itself (for instance
372b9c1b51eSKate Stone     // if it is a
373b9c1b51eSKate Stone     // signal that is set not to stop.  Check that here first.  We just say we
374b9c1b51eSKate Stone     // explain the stop
3750161b49cSJim Ingham     // but aren't done and everything will continue on from there.
3760161b49cSJim Ingham 
377b9c1b51eSKate Stone     if (m_real_stop_info_sp &&
378b9c1b51eSKate Stone         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
379184e9811SJim Ingham       SetPlanComplete(false);
380e65b2cf2SEugene Zelenko       return m_subplan_sp ? m_unwind_on_error : false;
381b9c1b51eSKate Stone     } else
3820161b49cSJim Ingham       return true;
3830161b49cSJim Ingham   }
38440d871faSJim Ingham }
38530fdc8d8SChris Lattner 
386b9c1b51eSKate Stone bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
387b9c1b51eSKate Stone   // We do some computation in DoPlanExplainsStop that may or may not set the
388b9c1b51eSKate Stone   // plan as complete.
389184e9811SJim Ingham   // We need to do that here to make sure our state is correct.
390221d51cfSJim Ingham   DoPlanExplainsStop(event_ptr);
391184e9811SJim Ingham 
392b9c1b51eSKate Stone   if (IsPlanComplete()) {
3939da3683cSJim Ingham     ReportRegisterState("Function completed.  Register state was:");
39430fdc8d8SChris Lattner     return true;
395b9c1b51eSKate Stone   } else {
39630fdc8d8SChris Lattner     return false;
39730fdc8d8SChris Lattner   }
39830fdc8d8SChris Lattner }
39930fdc8d8SChris Lattner 
400b9c1b51eSKate Stone bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
40130fdc8d8SChris Lattner 
402b9c1b51eSKate Stone StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
40330fdc8d8SChris Lattner 
404b9c1b51eSKate Stone void ThreadPlanCallFunction::DidPush() {
405be3a1b14SSean Callanan   //#define SINGLE_STEP_EXPRESSIONS
406be3a1b14SSean Callanan 
407b9c1b51eSKate Stone   // Now set the thread state to "no reason" so we don't run with whatever
408b9c1b51eSKate Stone   // signal was outstanding...
409b9c1b51eSKate Stone   // Wait till the plan is pushed so we aren't changing the stop info till we're
410b9c1b51eSKate Stone   // about to run.
4118559a355SJim Ingham 
4128559a355SJim Ingham   GetThread().SetStopInfoToNothing();
4138559a355SJim Ingham 
414be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
415b9c1b51eSKate Stone   m_subplan_sp.reset(
416b9c1b51eSKate Stone       new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
41730fdc8d8SChris Lattner 
41830fdc8d8SChris Lattner   m_thread.QueueThreadPlan(m_subplan_sp, false);
41977787033SJim Ingham   m_subplan_sp->SetPrivate(true);
420be3a1b14SSean Callanan #endif
42130fdc8d8SChris Lattner }
42230fdc8d8SChris Lattner 
423b9c1b51eSKate Stone bool ThreadPlanCallFunction::WillStop() { return true; }
42430fdc8d8SChris Lattner 
425b9c1b51eSKate Stone bool ThreadPlanCallFunction::MischiefManaged() {
4265160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
42730fdc8d8SChris Lattner 
428b9c1b51eSKate Stone   if (IsPlanComplete()) {
42930fdc8d8SChris Lattner     if (log)
430324a1036SSaleem Abdulrasool       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
431324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
43230fdc8d8SChris Lattner 
43330fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
43430fdc8d8SChris Lattner     return true;
435b9c1b51eSKate Stone   } else {
43630fdc8d8SChris Lattner     return false;
43730fdc8d8SChris Lattner   }
43830fdc8d8SChris Lattner }
4396db73ca5SSean Callanan 
440b9c1b51eSKate Stone void ThreadPlanCallFunction::SetBreakpoints() {
4411ac04c30SGreg Clayton   ProcessSP process_sp(m_thread.CalculateProcess());
442b9c1b51eSKate Stone   if (m_trap_exceptions && process_sp) {
443b9c1b51eSKate Stone     m_cxx_language_runtime =
444b9c1b51eSKate Stone         process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
4451ac04c30SGreg Clayton     m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
4466db73ca5SSean Callanan 
447b9c1b51eSKate Stone     if (m_cxx_language_runtime) {
448b9c1b51eSKate Stone       m_should_clear_cxx_exception_bp =
449b9c1b51eSKate Stone           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
450f211510fSSean Callanan       m_cxx_language_runtime->SetExceptionBreakpoints();
4516fbc48bcSJim Ingham     }
452b9c1b51eSKate Stone     if (m_objc_language_runtime) {
453b9c1b51eSKate Stone       m_should_clear_objc_exception_bp =
454b9c1b51eSKate Stone           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
455f211510fSSean Callanan       m_objc_language_runtime->SetExceptionBreakpoints();
4566db73ca5SSean Callanan     }
4571ac04c30SGreg Clayton   }
4586fbc48bcSJim Ingham }
4596db73ca5SSean Callanan 
460b9c1b51eSKate Stone void ThreadPlanCallFunction::ClearBreakpoints() {
461b9c1b51eSKate Stone   if (m_trap_exceptions) {
4626fbc48bcSJim Ingham     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
463f211510fSSean Callanan       m_cxx_language_runtime->ClearExceptionBreakpoints();
4646fbc48bcSJim Ingham     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
465f211510fSSean Callanan       m_objc_language_runtime->ClearExceptionBreakpoints();
4666db73ca5SSean Callanan   }
4676fbc48bcSJim Ingham }
468c98aca60SSean Callanan 
469b9c1b51eSKate Stone bool ThreadPlanCallFunction::BreakpointsExplainStop() {
47060c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
471c98aca60SSean Callanan 
472b9c1b51eSKate Stone   if (m_trap_exceptions) {
473184e9811SJim Ingham     if ((m_cxx_language_runtime &&
474b9c1b51eSKate Stone          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
475b9c1b51eSKate Stone              stop_info_sp)) ||
476b9c1b51eSKate Stone         (m_objc_language_runtime &&
477b9c1b51eSKate Stone          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
478b9c1b51eSKate Stone              stop_info_sp))) {
479641a67ceSJim Ingham       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
480641a67ceSJim Ingham       if (log)
481b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
482b9c1b51eSKate Stone                     "exception breakpoint, setting plan complete.");
483641a67ceSJim Ingham 
484184e9811SJim Ingham       SetPlanComplete(false);
485641a67ceSJim Ingham 
486b9c1b51eSKate Stone       // If the user has set the ObjC language breakpoint, it would normally get
487b9c1b51eSKate Stone       // priority over our internal
488b9c1b51eSKate Stone       // catcher breakpoint, but in this case we can't let that happen, so force
489b9c1b51eSKate Stone       // the ShouldStop here.
490641a67ceSJim Ingham       stop_info_sp->OverrideShouldStop(true);
491c98aca60SSean Callanan       return true;
492184e9811SJim Ingham     }
4936fbc48bcSJim Ingham   }
494f211510fSSean Callanan 
495c98aca60SSean Callanan   return false;
496c98aca60SSean Callanan }
4978559a355SJim Ingham 
498b9c1b51eSKate Stone void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4990ff099f1SJim Ingham   m_subplan_sp->SetStopOthers(new_value);
5000ff099f1SJim Ingham }
5010ff099f1SJim Ingham 
502b9c1b51eSKate Stone bool ThreadPlanCallFunction::RestoreThreadState() {
5038559a355SJim Ingham   return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
5048559a355SJim Ingham }
5058559a355SJim Ingham 
506b9c1b51eSKate Stone void ThreadPlanCallFunction::SetReturnValue() {
50790ff7911SEwan Crawford   ProcessSP process_sp(m_thread.GetProcess());
508e65b2cf2SEugene Zelenko   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
509b9c1b51eSKate Stone   if (abi && m_return_type.IsValid()) {
51090ff7911SEwan Crawford     const bool persistent = false;
511b9c1b51eSKate Stone     m_return_valobj_sp =
512b9c1b51eSKate Stone         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
51390ff7911SEwan Crawford   }
51490ff7911SEwan Crawford }
515