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"
181f746071SGreg Clayton #include "lldb/Core/Module.h"
191f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
2032abc6edSZachary Turner #include "lldb/Target/ABI.h"
21f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
2230fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2330fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2440d871faSJim Ingham #include "lldb/Target/StopInfo.h"
2530fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2730fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
286f9e6901SZachary Turner #include "lldb/Utility/Log.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
59*05097246SAdrian Prantl   // to put our function, we're not going to get any further...
6097206d57SZachary Turner   Status error;
610092c8ebSJim Ingham   process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
62b9c1b51eSKate Stone   if (!error.Success()) {
63b9c1b51eSKate Stone     m_constructor_errors.Printf(
64b9c1b51eSKate Stone         "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
65b9c1b51eSKate Stone         m_function_sp);
660092c8ebSJim Ingham     if (log)
67b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
68324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
690092c8ebSJim Ingham     return false;
700092c8ebSJim Ingham   }
710092c8ebSJim Ingham 
726fbc48bcSJim Ingham   Module *exe_module = GetTarget().GetExecutableModulePointer();
730092c8ebSJim Ingham 
74b9c1b51eSKate Stone   if (exe_module == nullptr) {
75b9c1b51eSKate Stone     m_constructor_errors.Printf(
76b9c1b51eSKate Stone         "Can't execute code without an executable module.");
770092c8ebSJim Ingham     if (log)
78b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
79324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
800092c8ebSJim Ingham     return false;
81b9c1b51eSKate Stone   } else {
820092c8ebSJim Ingham     ObjectFile *objectFile = exe_module->GetObjectFile();
83b9c1b51eSKate Stone     if (!objectFile) {
84b9c1b51eSKate Stone       m_constructor_errors.Printf(
85b9c1b51eSKate Stone           "Could not find object file for module \"%s\".",
86ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
87ea06f3bfSJim Ingham 
880092c8ebSJim Ingham       if (log)
89324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
90b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
910092c8ebSJim Ingham       return false;
920092c8ebSJim Ingham     }
93ea06f3bfSJim Ingham 
940092c8ebSJim Ingham     m_start_addr = objectFile->GetEntryPointAddress();
95b9c1b51eSKate Stone     if (!m_start_addr.IsValid()) {
96b9c1b51eSKate Stone       m_constructor_errors.Printf(
97b9c1b51eSKate Stone           "Could not find entry point address for executable module \"%s\".",
98ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
990092c8ebSJim Ingham       if (log)
100324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
101b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
1020092c8ebSJim Ingham       return false;
1030092c8ebSJim Ingham     }
1040092c8ebSJim Ingham   }
1050092c8ebSJim Ingham 
1066fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
1070092c8ebSJim Ingham 
1080092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
1090092c8ebSJim Ingham   if (log && log->GetVerbose())
110b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
111b9c1b51eSKate Stone                         "Original register state was:");
1120092c8ebSJim Ingham 
113b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
114b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
115b9c1b51eSKate Stone                                 "checkpoint thread state.");
1160092c8ebSJim Ingham     if (log)
117b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
118324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
1190092c8ebSJim Ingham     return false;
1200092c8ebSJim Ingham   }
1216fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
1220092c8ebSJim Ingham 
1230092c8ebSJim Ingham   return true;
1240092c8ebSJim Ingham }
12530fdc8d8SChris Lattner 
126b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
127b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
128b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
129b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
130b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
131b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1326fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1336fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1346fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
135b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
136b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1376fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1386fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
139b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
14090ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
14190ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
14290ff7911SEwan Crawford   ABI *abi = nullptr;
14390ff7911SEwan Crawford 
144923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1451ac04c30SGreg Clayton     return;
1461ac04c30SGreg Clayton 
147b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
148b9c1b51eSKate Stone                                start_load_addr, args))
14930fdc8d8SChris Lattner     return;
15030fdc8d8SChris Lattner 
1519da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1529da3683cSJim Ingham 
1539da3683cSJim Ingham   m_valid = true;
1549da3683cSJim Ingham }
1559da3683cSJim Ingham 
156b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
157b9c1b51eSKate Stone     Thread &thread, const Address &function,
158b9c1b51eSKate Stone     const EvaluateExpressionOptions &options)
159b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
160b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
161b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
16290ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
16390ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
16490ff7911SEwan Crawford       m_debug_execution(options.GetDebug()),
165b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
166b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
16790ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
16890ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
169b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
17090ff7911SEwan Crawford 
171b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1720161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1739da3683cSJim Ingham }
1749da3683cSJim Ingham 
175b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
1763b7e1981SPavel Labath   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1773b7e1981SPavel Labath   if (log && log->GetVerbose()) {
178af247d7bSGreg Clayton     StreamString strm;
1795ccbd294SGreg Clayton     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
180ece96492SSean Callanan 
1819da3683cSJim Ingham     log->PutCString(message);
182ece96492SSean Callanan 
183af247d7bSGreg Clayton     RegisterValue reg_value;
184ece96492SSean Callanan 
185af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
186b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
187af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
188b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
189af247d7bSGreg Clayton         reg_value.Dump(&strm, reg_info, true, false, eFormatDefault);
190af247d7bSGreg Clayton         strm.EOL();
191ece96492SSean Callanan       }
192ece96492SSean Callanan     }
193c156427dSZachary Turner     log->PutString(strm.GetString());
194af247d7bSGreg Clayton   }
19510af7c43SSean Callanan }
19610af7c43SSean Callanan 
197b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
1985160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
19987d0e618SJim Ingham 
200b9c1b51eSKate Stone   if (!m_valid) {
20187d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
20287d0e618SJim Ingham     if (log)
203b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): Log called on "
204b9c1b51eSKate Stone                   "ThreadPlanCallFunction that was never valid.",
205324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
20687d0e618SJim Ingham     return;
20787d0e618SJim Ingham   }
20887d0e618SJim Ingham 
209b9c1b51eSKate Stone   if (!m_takedown_done) {
210b9c1b51eSKate Stone     if (success) {
21190ff7911SEwan Crawford       SetReturnValue();
21218de2fdcSJim Ingham     }
2139da3683cSJim Ingham     if (log)
214b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread "
215b9c1b51eSKate Stone                   "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
216324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
217324a1036SSaleem Abdulrasool                   IsPlanComplete());
2189da3683cSJim Ingham     m_takedown_done = true;
219b9c1b51eSKate Stone     m_stop_address =
220b9c1b51eSKate Stone         m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
22160c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
222b9c1b51eSKate Stone     if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
2237b24e95eSEd Maste       if (log)
224b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore "
225b9c1b51eSKate Stone                     "register state",
226324a1036SSaleem Abdulrasool                     static_cast<void *>(this));
2277b24e95eSEd Maste     }
22818de2fdcSJim Ingham     SetPlanComplete(success);
22910af7c43SSean Callanan     ClearBreakpoints();
2309da3683cSJim Ingham     if (log && log->GetVerbose())
231b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
232b9c1b51eSKate Stone                           "Restored register state:");
233b9c1b51eSKate Stone   } else {
2349da3683cSJim Ingham     if (log)
235b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
236b9c1b51eSKate Stone                   "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
237324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
238324a1036SSaleem Abdulrasool                   IsPlanComplete());
23930fdc8d8SChris Lattner   }
24077787033SJim Ingham }
24130fdc8d8SChris Lattner 
242b9c1b51eSKate Stone void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
243bda4e5ebSJim Ingham 
244b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
245b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
24630fdc8d8SChris Lattner     s->Printf("Function call thread plan");
247b9c1b51eSKate Stone   } else {
2481ac04c30SGreg Clayton     TargetSP target_sp(m_thread.CalculateTarget());
249b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
250b9c1b51eSKate Stone               m_function_addr.GetLoadAddress(target_sp.get()));
25130fdc8d8SChris Lattner   }
25230fdc8d8SChris Lattner }
25330fdc8d8SChris Lattner 
254b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
255b9c1b51eSKate Stone   if (!m_valid) {
256b9c1b51eSKate Stone     if (error) {
257ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
258c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
259ea06f3bfSJim Ingham       else
260ea06f3bfSJim Ingham         error->PutCString("Unknown error");
261ea06f3bfSJim Ingham     }
26230fdc8d8SChris Lattner     return false;
263ea06f3bfSJim Ingham   }
26430fdc8d8SChris Lattner 
26530fdc8d8SChris Lattner   return true;
26630fdc8d8SChris Lattner }
26730fdc8d8SChris Lattner 
268b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2690161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2700161b49cSJim Ingham     return eVoteYes;
2710161b49cSJim Ingham   else
2720161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2730161b49cSJim Ingham }
2740161b49cSJim Ingham 
275b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
276b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
277b9c1b51eSKate Stone                                                   LIBLLDB_LOG_PROCESS));
27860c4118cSJim Ingham   m_real_stop_info_sp = GetPrivateStopInfo();
279160f78c5SJim Ingham 
280*05097246SAdrian Prantl   // If our subplan knows why we stopped, even if it's done (which would
281*05097246SAdrian Prantl   // forward the question to us) we answer yes.
282b9c1b51eSKate Stone   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
283184e9811SJim Ingham     SetPlanComplete();
28440d871faSJim Ingham     return true;
285184e9811SJim Ingham   }
2863e6fedcaSSean Callanan 
287c98aca60SSean Callanan   // Check if the breakpoint is one of ours.
288c98aca60SSean Callanan 
28918de2fdcSJim Ingham   StopReason stop_reason;
29018de2fdcSJim Ingham   if (!m_real_stop_info_sp)
29118de2fdcSJim Ingham     stop_reason = eStopReasonNone;
29218de2fdcSJim Ingham   else
29318de2fdcSJim Ingham     stop_reason = m_real_stop_info_sp->GetStopReason();
2940161b49cSJim Ingham   if (log)
295b9c1b51eSKate Stone     log->Printf(
296b9c1b51eSKate Stone         "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.",
297b9c1b51eSKate Stone         Thread::StopReasonAsCString(stop_reason));
29818de2fdcSJim Ingham 
29918de2fdcSJim Ingham   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
300c98aca60SSean Callanan     return true;
301c98aca60SSean Callanan 
302b9c1b51eSKate Stone   // One more quirk here.  If this event was from Halt interrupting the target,
303*05097246SAdrian Prantl   // then we should not consider ourselves complete.  Return true to
304*05097246SAdrian Prantl   // acknowledge the stop.
305b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
30635878c47SJim Ingham     if (log)
307b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an "
308b9c1b51eSKate Stone                   "Interrupt, returning true.");
30935878c47SJim Ingham     return true;
31035878c47SJim Ingham   }
3110161b49cSJim Ingham   // We control breakpoints separately from other "stop reasons."  So first,
312b9c1b51eSKate Stone   // check the case where we stopped for an internal breakpoint, in that case,
313*05097246SAdrian Prantl   // continue on. If it is not an internal breakpoint, consult
314*05097246SAdrian Prantl   // m_ignore_breakpoints.
3156db73ca5SSean Callanan 
316b9c1b51eSKate Stone   if (stop_reason == eStopReasonBreakpoint) {
3171ac04c30SGreg Clayton     ProcessSP process_sp(m_thread.CalculateProcess());
318160f78c5SJim Ingham     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3191ac04c30SGreg Clayton     BreakpointSiteSP bp_site_sp;
3201ac04c30SGreg Clayton     if (process_sp)
3211ac04c30SGreg Clayton       bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
322b9c1b51eSKate Stone     if (bp_site_sp) {
32340d871faSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
32440d871faSJim Ingham       bool is_internal = true;
325b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
3266db73ca5SSean Callanan         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3270161b49cSJim Ingham         if (log)
328b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit "
329b9c1b51eSKate Stone                       "breakpoint %d while calling function",
330b9c1b51eSKate Stone                       bp.GetID());
3316db73ca5SSean Callanan 
332b9c1b51eSKate Stone         if (!bp.IsInternal()) {
33340d871faSJim Ingham           is_internal = false;
33440d871faSJim Ingham           break;
33540d871faSJim Ingham         }
33640d871faSJim Ingham       }
337b9c1b51eSKate Stone       if (is_internal) {
3380161b49cSJim Ingham         if (log)
339b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an "
340b9c1b51eSKate Stone                       "internal breakpoint, not stopping.");
34140d871faSJim Ingham         return false;
34240d871faSJim Ingham       }
3430161b49cSJim Ingham     }
34440d871faSJim Ingham 
345b9c1b51eSKate Stone     if (m_ignore_breakpoints) {
3460161b49cSJim Ingham       if (log)
347b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
348b9c1b51eSKate Stone                     "breakpoints, overriding breakpoint stop info ShouldStop, "
349b9c1b51eSKate Stone                     "returning true");
3500161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(false);
351923886ceSJim Ingham       return true;
352b9c1b51eSKate Stone     } else {
3530161b49cSJim Ingham       if (log)
354b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not "
355b9c1b51eSKate Stone                     "ignoring breakpoints, overriding breakpoint stop info "
356b9c1b51eSKate Stone                     "ShouldStop, returning true");
3570161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(true);
3580161b49cSJim Ingham       return false;
3590161b49cSJim Ingham     }
360b9c1b51eSKate Stone   } else if (!m_unwind_on_error) {
361b9c1b51eSKate Stone     // If we don't want to discard this plan, than any stop we don't understand
362b9c1b51eSKate Stone     // should be propagated up the stack.
363923886ceSJim Ingham     return false;
364b9c1b51eSKate Stone   } else {
365*05097246SAdrian Prantl     // If the subplan is running, any crashes are attributable to us. If we
366*05097246SAdrian Prantl     // want to discard the plan, then we say we explain the stop but if we are
367*05097246SAdrian Prantl     // going to be discarded, let whoever is above us explain the stop. But
368*05097246SAdrian Prantl     // don't discard the plan if the stop would restart itself (for instance if
369*05097246SAdrian Prantl     // it is a signal that is set not to stop.  Check that here first.  We just
370*05097246SAdrian Prantl     // say we explain the stop but aren't done and everything will continue on
371*05097246SAdrian Prantl     // from there.
3720161b49cSJim Ingham 
373b9c1b51eSKate Stone     if (m_real_stop_info_sp &&
374b9c1b51eSKate Stone         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
375184e9811SJim Ingham       SetPlanComplete(false);
376e65b2cf2SEugene Zelenko       return m_subplan_sp ? m_unwind_on_error : false;
377b9c1b51eSKate Stone     } else
3780161b49cSJim Ingham       return true;
3790161b49cSJim Ingham   }
38040d871faSJim Ingham }
38130fdc8d8SChris Lattner 
382b9c1b51eSKate Stone bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
383b9c1b51eSKate Stone   // We do some computation in DoPlanExplainsStop that may or may not set the
384*05097246SAdrian Prantl   // plan as complete. We need to do that here to make sure our state is
385*05097246SAdrian Prantl   // correct.
386221d51cfSJim Ingham   DoPlanExplainsStop(event_ptr);
387184e9811SJim Ingham 
388b9c1b51eSKate Stone   if (IsPlanComplete()) {
3899da3683cSJim Ingham     ReportRegisterState("Function completed.  Register state was:");
39030fdc8d8SChris Lattner     return true;
391b9c1b51eSKate Stone   } else {
39230fdc8d8SChris Lattner     return false;
39330fdc8d8SChris Lattner   }
39430fdc8d8SChris Lattner }
39530fdc8d8SChris Lattner 
396b9c1b51eSKate Stone bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
39730fdc8d8SChris Lattner 
398b9c1b51eSKate Stone StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
39930fdc8d8SChris Lattner 
400b9c1b51eSKate Stone void ThreadPlanCallFunction::DidPush() {
401be3a1b14SSean Callanan   //#define SINGLE_STEP_EXPRESSIONS
402be3a1b14SSean Callanan 
403b9c1b51eSKate Stone   // Now set the thread state to "no reason" so we don't run with whatever
404*05097246SAdrian Prantl   // signal was outstanding... Wait till the plan is pushed so we aren't
405*05097246SAdrian Prantl   // changing the stop info till we're about to run.
4068559a355SJim Ingham 
4078559a355SJim Ingham   GetThread().SetStopInfoToNothing();
4088559a355SJim Ingham 
409be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
410b9c1b51eSKate Stone   m_subplan_sp.reset(
411b9c1b51eSKate Stone       new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
41230fdc8d8SChris Lattner 
41330fdc8d8SChris Lattner   m_thread.QueueThreadPlan(m_subplan_sp, false);
41477787033SJim Ingham   m_subplan_sp->SetPrivate(true);
415be3a1b14SSean Callanan #endif
41630fdc8d8SChris Lattner }
41730fdc8d8SChris Lattner 
418b9c1b51eSKate Stone bool ThreadPlanCallFunction::WillStop() { return true; }
41930fdc8d8SChris Lattner 
420b9c1b51eSKate Stone bool ThreadPlanCallFunction::MischiefManaged() {
4215160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
42230fdc8d8SChris Lattner 
423b9c1b51eSKate Stone   if (IsPlanComplete()) {
42430fdc8d8SChris Lattner     if (log)
425324a1036SSaleem Abdulrasool       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
426324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
42730fdc8d8SChris Lattner 
42830fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
42930fdc8d8SChris Lattner     return true;
430b9c1b51eSKate Stone   } else {
43130fdc8d8SChris Lattner     return false;
43230fdc8d8SChris Lattner   }
43330fdc8d8SChris Lattner }
4346db73ca5SSean Callanan 
435b9c1b51eSKate Stone void ThreadPlanCallFunction::SetBreakpoints() {
4361ac04c30SGreg Clayton   ProcessSP process_sp(m_thread.CalculateProcess());
437b9c1b51eSKate Stone   if (m_trap_exceptions && process_sp) {
438b9c1b51eSKate Stone     m_cxx_language_runtime =
439b9c1b51eSKate Stone         process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
4401ac04c30SGreg Clayton     m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
4416db73ca5SSean Callanan 
442b9c1b51eSKate Stone     if (m_cxx_language_runtime) {
443b9c1b51eSKate Stone       m_should_clear_cxx_exception_bp =
444b9c1b51eSKate Stone           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
445f211510fSSean Callanan       m_cxx_language_runtime->SetExceptionBreakpoints();
4466fbc48bcSJim Ingham     }
447b9c1b51eSKate Stone     if (m_objc_language_runtime) {
448b9c1b51eSKate Stone       m_should_clear_objc_exception_bp =
449b9c1b51eSKate Stone           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
450f211510fSSean Callanan       m_objc_language_runtime->SetExceptionBreakpoints();
4516db73ca5SSean Callanan     }
4521ac04c30SGreg Clayton   }
4536fbc48bcSJim Ingham }
4546db73ca5SSean Callanan 
455b9c1b51eSKate Stone void ThreadPlanCallFunction::ClearBreakpoints() {
456b9c1b51eSKate Stone   if (m_trap_exceptions) {
4576fbc48bcSJim Ingham     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
458f211510fSSean Callanan       m_cxx_language_runtime->ClearExceptionBreakpoints();
4596fbc48bcSJim Ingham     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
460f211510fSSean Callanan       m_objc_language_runtime->ClearExceptionBreakpoints();
4616db73ca5SSean Callanan   }
4626fbc48bcSJim Ingham }
463c98aca60SSean Callanan 
464b9c1b51eSKate Stone bool ThreadPlanCallFunction::BreakpointsExplainStop() {
46560c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
466c98aca60SSean Callanan 
467b9c1b51eSKate Stone   if (m_trap_exceptions) {
468184e9811SJim Ingham     if ((m_cxx_language_runtime &&
469b9c1b51eSKate Stone          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
470b9c1b51eSKate Stone              stop_info_sp)) ||
471b9c1b51eSKate Stone         (m_objc_language_runtime &&
472b9c1b51eSKate Stone          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
473b9c1b51eSKate Stone              stop_info_sp))) {
474641a67ceSJim Ingham       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
475641a67ceSJim Ingham       if (log)
476b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
477b9c1b51eSKate Stone                     "exception breakpoint, setting plan complete.");
478641a67ceSJim Ingham 
479184e9811SJim Ingham       SetPlanComplete(false);
480641a67ceSJim Ingham 
481*05097246SAdrian Prantl       // If the user has set the ObjC language breakpoint, it would normally
482*05097246SAdrian Prantl       // get priority over our internal catcher breakpoint, but in this case we
483*05097246SAdrian Prantl       // can't let that happen, so force the ShouldStop here.
484641a67ceSJim Ingham       stop_info_sp->OverrideShouldStop(true);
485c98aca60SSean Callanan       return true;
486184e9811SJim Ingham     }
4876fbc48bcSJim Ingham   }
488f211510fSSean Callanan 
489c98aca60SSean Callanan   return false;
490c98aca60SSean Callanan }
4918559a355SJim Ingham 
492b9c1b51eSKate Stone void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4930ff099f1SJim Ingham   m_subplan_sp->SetStopOthers(new_value);
4940ff099f1SJim Ingham }
4950ff099f1SJim Ingham 
496b9c1b51eSKate Stone bool ThreadPlanCallFunction::RestoreThreadState() {
4978559a355SJim Ingham   return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
4988559a355SJim Ingham }
4998559a355SJim Ingham 
500b9c1b51eSKate Stone void ThreadPlanCallFunction::SetReturnValue() {
50190ff7911SEwan Crawford   ProcessSP process_sp(m_thread.GetProcess());
502e65b2cf2SEugene Zelenko   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
503b9c1b51eSKate Stone   if (abi && m_return_type.IsValid()) {
50490ff7911SEwan Crawford     const bool persistent = false;
505b9c1b51eSKate Stone     m_return_valobj_sp =
506b9c1b51eSKate Stone         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
50790ff7911SEwan Crawford   }
50890ff7911SEwan Crawford }
509