130fdc8d8SChris Lattner //===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanCallFunction.h"
1040d871faSJim Ingham #include "lldb/Breakpoint/Breakpoint.h"
1140d871faSJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
1230fdc8d8SChris Lattner #include "lldb/Core/Address.h"
13e03334cfSPavel Labath #include "lldb/Core/DumpRegisterValue.h"
141f746071SGreg Clayton #include "lldb/Core/Module.h"
151f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
1632abc6edSZachary Turner #include "lldb/Target/ABI.h"
17f211510fSSean Callanan #include "lldb/Target/LanguageRuntime.h"
1830fdc8d8SChris Lattner #include "lldb/Target/Process.h"
1930fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
2040d871faSJim Ingham #include "lldb/Target/StopInfo.h"
2130fdc8d8SChris Lattner #include "lldb/Target/Target.h"
2230fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2330fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanRunToAddress.h"
246f9e6901SZachary Turner #include "lldb/Utility/Log.h"
25bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2630fdc8d8SChris Lattner 
27*796ac80bSJonas Devlieghere #include <memory>
28*796ac80bSJonas Devlieghere 
2930fdc8d8SChris Lattner using namespace lldb;
3030fdc8d8SChris Lattner using namespace lldb_private;
3130fdc8d8SChris Lattner 
3230fdc8d8SChris Lattner //----------------------------------------------------------------------
3330fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
3430fdc8d8SChris Lattner //----------------------------------------------------------------------
35b9c1b51eSKate Stone bool ThreadPlanCallFunction::ConstructorSetup(
36b9c1b51eSKate Stone     Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
37b9c1b51eSKate Stone     lldb::addr_t &function_load_addr) {
380092c8ebSJim Ingham   SetIsMasterPlan(true);
39923886ceSJim Ingham   SetOkayToDiscard(false);
40327c267aSAndrew Kaylor   SetPrivate(true);
410092c8ebSJim Ingham 
420092c8ebSJim Ingham   ProcessSP process_sp(thread.GetProcess());
430092c8ebSJim Ingham   if (!process_sp)
440092c8ebSJim Ingham     return false;
450092c8ebSJim Ingham 
460092c8ebSJim Ingham   abi = process_sp->GetABI().get();
470092c8ebSJim Ingham 
480092c8ebSJim Ingham   if (!abi)
490092c8ebSJim Ingham     return false;
500092c8ebSJim Ingham 
515160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
520092c8ebSJim Ingham 
530092c8ebSJim Ingham   SetBreakpoints();
540092c8ebSJim Ingham 
550092c8ebSJim Ingham   m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
56b9c1b51eSKate Stone   // If we can't read memory at the point of the process where we are planning
5705097246SAdrian Prantl   // to put our function, we're not going to get any further...
5897206d57SZachary Turner   Status error;
590092c8ebSJim Ingham   process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
60b9c1b51eSKate Stone   if (!error.Success()) {
61b9c1b51eSKate Stone     m_constructor_errors.Printf(
62b9c1b51eSKate Stone         "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
63b9c1b51eSKate Stone         m_function_sp);
640092c8ebSJim Ingham     if (log)
65b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
66324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
670092c8ebSJim Ingham     return false;
680092c8ebSJim Ingham   }
690092c8ebSJim Ingham 
706fbc48bcSJim Ingham   Module *exe_module = GetTarget().GetExecutableModulePointer();
710092c8ebSJim Ingham 
72b9c1b51eSKate Stone   if (exe_module == nullptr) {
73b9c1b51eSKate Stone     m_constructor_errors.Printf(
74b9c1b51eSKate Stone         "Can't execute code without an executable module.");
750092c8ebSJim Ingham     if (log)
76b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
77324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
780092c8ebSJim Ingham     return false;
79b9c1b51eSKate Stone   } else {
800092c8ebSJim Ingham     ObjectFile *objectFile = exe_module->GetObjectFile();
81b9c1b51eSKate Stone     if (!objectFile) {
82b9c1b51eSKate Stone       m_constructor_errors.Printf(
83b9c1b51eSKate Stone           "Could not find object file for module \"%s\".",
84ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
85ea06f3bfSJim Ingham 
860092c8ebSJim Ingham       if (log)
87324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
88b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
890092c8ebSJim Ingham       return false;
900092c8ebSJim Ingham     }
91ea06f3bfSJim Ingham 
920092c8ebSJim Ingham     m_start_addr = objectFile->GetEntryPointAddress();
93b9c1b51eSKate Stone     if (!m_start_addr.IsValid()) {
94b9c1b51eSKate Stone       m_constructor_errors.Printf(
95b9c1b51eSKate Stone           "Could not find entry point address for executable module \"%s\".",
96ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
970092c8ebSJim Ingham       if (log)
98324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
99b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
1000092c8ebSJim Ingham       return false;
1010092c8ebSJim Ingham     }
1020092c8ebSJim Ingham   }
1030092c8ebSJim Ingham 
1046fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
1050092c8ebSJim Ingham 
1060092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
1070092c8ebSJim Ingham   if (log && log->GetVerbose())
108b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
109b9c1b51eSKate Stone                         "Original register state was:");
1100092c8ebSJim Ingham 
111b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
112b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
113b9c1b51eSKate Stone                                 "checkpoint thread state.");
1140092c8ebSJim Ingham     if (log)
115b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
116324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
1170092c8ebSJim Ingham     return false;
1180092c8ebSJim Ingham   }
1196fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
1200092c8ebSJim Ingham 
1210092c8ebSJim Ingham   return true;
1220092c8ebSJim Ingham }
12330fdc8d8SChris Lattner 
124b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
125b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
126b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
127b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
128b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
129b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1306fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1316fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1326fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
133b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
134b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1356fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1366fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
137b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
13890ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
13990ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
14090ff7911SEwan Crawford   ABI *abi = nullptr;
14190ff7911SEwan Crawford 
142923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1431ac04c30SGreg Clayton     return;
1441ac04c30SGreg Clayton 
145b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
146b9c1b51eSKate Stone                                start_load_addr, args))
14730fdc8d8SChris Lattner     return;
14830fdc8d8SChris Lattner 
1499da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1509da3683cSJim Ingham 
1519da3683cSJim Ingham   m_valid = true;
1529da3683cSJim Ingham }
1539da3683cSJim Ingham 
154b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
155b9c1b51eSKate Stone     Thread &thread, const Address &function,
156b9c1b51eSKate Stone     const EvaluateExpressionOptions &options)
157b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
158b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
159b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
16090ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
16190ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
16290ff7911SEwan Crawford       m_debug_execution(options.GetDebug()),
163b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
164b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
16590ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
16690ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
167b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
16890ff7911SEwan Crawford 
169b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1700161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1719da3683cSJim Ingham }
1729da3683cSJim Ingham 
173b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
1743b7e1981SPavel Labath   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1753b7e1981SPavel Labath   if (log && log->GetVerbose()) {
176af247d7bSGreg Clayton     StreamString strm;
1775ccbd294SGreg Clayton     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
178ece96492SSean Callanan 
1799da3683cSJim Ingham     log->PutCString(message);
180ece96492SSean Callanan 
181af247d7bSGreg Clayton     RegisterValue reg_value;
182ece96492SSean Callanan 
183af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
184b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
185af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
186b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
187e03334cfSPavel Labath         DumpRegisterValue(reg_value, &strm, reg_info, true, false,
188e03334cfSPavel Labath                           eFormatDefault);
189af247d7bSGreg Clayton         strm.EOL();
190ece96492SSean Callanan       }
191ece96492SSean Callanan     }
192c156427dSZachary Turner     log->PutString(strm.GetString());
193af247d7bSGreg Clayton   }
19410af7c43SSean Callanan }
19510af7c43SSean Callanan 
196b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
1975160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
19887d0e618SJim Ingham 
199b9c1b51eSKate Stone   if (!m_valid) {
20087d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
20187d0e618SJim Ingham     if (log)
202b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): Log called on "
203b9c1b51eSKate Stone                   "ThreadPlanCallFunction that was never valid.",
204324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
20587d0e618SJim Ingham     return;
20687d0e618SJim Ingham   }
20787d0e618SJim Ingham 
208b9c1b51eSKate Stone   if (!m_takedown_done) {
209b9c1b51eSKate Stone     if (success) {
21090ff7911SEwan Crawford       SetReturnValue();
21118de2fdcSJim Ingham     }
2129da3683cSJim Ingham     if (log)
213b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread "
214b9c1b51eSKate Stone                   "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
215324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
216324a1036SSaleem Abdulrasool                   IsPlanComplete());
2179da3683cSJim Ingham     m_takedown_done = true;
218b9c1b51eSKate Stone     m_stop_address =
219b9c1b51eSKate Stone         m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
22060c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
221b9c1b51eSKate Stone     if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
2227b24e95eSEd Maste       if (log)
223b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore "
224b9c1b51eSKate Stone                     "register state",
225324a1036SSaleem Abdulrasool                     static_cast<void *>(this));
2267b24e95eSEd Maste     }
22718de2fdcSJim Ingham     SetPlanComplete(success);
22810af7c43SSean Callanan     ClearBreakpoints();
2299da3683cSJim Ingham     if (log && log->GetVerbose())
230b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
231b9c1b51eSKate Stone                           "Restored register state:");
232b9c1b51eSKate Stone   } else {
2339da3683cSJim Ingham     if (log)
234b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
235b9c1b51eSKate Stone                   "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
236324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
237324a1036SSaleem Abdulrasool                   IsPlanComplete());
23830fdc8d8SChris Lattner   }
23977787033SJim Ingham }
24030fdc8d8SChris Lattner 
241b9c1b51eSKate Stone void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
242bda4e5ebSJim Ingham 
243b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
244b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
24530fdc8d8SChris Lattner     s->Printf("Function call thread plan");
246b9c1b51eSKate Stone   } else {
2471ac04c30SGreg Clayton     TargetSP target_sp(m_thread.CalculateTarget());
248b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
249b9c1b51eSKate Stone               m_function_addr.GetLoadAddress(target_sp.get()));
25030fdc8d8SChris Lattner   }
25130fdc8d8SChris Lattner }
25230fdc8d8SChris Lattner 
253b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
254b9c1b51eSKate Stone   if (!m_valid) {
255b9c1b51eSKate Stone     if (error) {
256ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
257c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
258ea06f3bfSJim Ingham       else
259ea06f3bfSJim Ingham         error->PutCString("Unknown error");
260ea06f3bfSJim Ingham     }
26130fdc8d8SChris Lattner     return false;
262ea06f3bfSJim Ingham   }
26330fdc8d8SChris Lattner 
26430fdc8d8SChris Lattner   return true;
26530fdc8d8SChris Lattner }
26630fdc8d8SChris Lattner 
267b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2680161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2690161b49cSJim Ingham     return eVoteYes;
2700161b49cSJim Ingham   else
2710161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2720161b49cSJim Ingham }
2730161b49cSJim Ingham 
274b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
275b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
276b9c1b51eSKate Stone                                                   LIBLLDB_LOG_PROCESS));
27760c4118cSJim Ingham   m_real_stop_info_sp = GetPrivateStopInfo();
278160f78c5SJim Ingham 
27905097246SAdrian Prantl   // If our subplan knows why we stopped, even if it's done (which would
28005097246SAdrian Prantl   // forward the question to us) we answer yes.
281b9c1b51eSKate Stone   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
282184e9811SJim Ingham     SetPlanComplete();
28340d871faSJim Ingham     return true;
284184e9811SJim Ingham   }
2853e6fedcaSSean Callanan 
286c98aca60SSean Callanan   // Check if the breakpoint is one of ours.
287c98aca60SSean Callanan 
28818de2fdcSJim Ingham   StopReason stop_reason;
28918de2fdcSJim Ingham   if (!m_real_stop_info_sp)
29018de2fdcSJim Ingham     stop_reason = eStopReasonNone;
29118de2fdcSJim Ingham   else
29218de2fdcSJim Ingham     stop_reason = m_real_stop_info_sp->GetStopReason();
2930161b49cSJim Ingham   if (log)
294b9c1b51eSKate Stone     log->Printf(
295b9c1b51eSKate Stone         "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.",
296b9c1b51eSKate Stone         Thread::StopReasonAsCString(stop_reason));
29718de2fdcSJim Ingham 
29818de2fdcSJim Ingham   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
299c98aca60SSean Callanan     return true;
300c98aca60SSean Callanan 
301b9c1b51eSKate Stone   // One more quirk here.  If this event was from Halt interrupting the target,
30205097246SAdrian Prantl   // then we should not consider ourselves complete.  Return true to
30305097246SAdrian Prantl   // acknowledge the stop.
304b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
30535878c47SJim Ingham     if (log)
306b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an "
307b9c1b51eSKate Stone                   "Interrupt, returning true.");
30835878c47SJim Ingham     return true;
30935878c47SJim Ingham   }
3100161b49cSJim Ingham   // We control breakpoints separately from other "stop reasons."  So first,
311b9c1b51eSKate Stone   // check the case where we stopped for an internal breakpoint, in that case,
31205097246SAdrian Prantl   // continue on. If it is not an internal breakpoint, consult
31305097246SAdrian Prantl   // m_ignore_breakpoints.
3146db73ca5SSean Callanan 
315b9c1b51eSKate Stone   if (stop_reason == eStopReasonBreakpoint) {
3161ac04c30SGreg Clayton     ProcessSP process_sp(m_thread.CalculateProcess());
317160f78c5SJim Ingham     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3181ac04c30SGreg Clayton     BreakpointSiteSP bp_site_sp;
3191ac04c30SGreg Clayton     if (process_sp)
3201ac04c30SGreg Clayton       bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
321b9c1b51eSKate Stone     if (bp_site_sp) {
32240d871faSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
32340d871faSJim Ingham       bool is_internal = true;
324b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
3256db73ca5SSean Callanan         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3260161b49cSJim Ingham         if (log)
327b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit "
328b9c1b51eSKate Stone                       "breakpoint %d while calling function",
329b9c1b51eSKate Stone                       bp.GetID());
3306db73ca5SSean Callanan 
331b9c1b51eSKate Stone         if (!bp.IsInternal()) {
33240d871faSJim Ingham           is_internal = false;
33340d871faSJim Ingham           break;
33440d871faSJim Ingham         }
33540d871faSJim Ingham       }
336b9c1b51eSKate Stone       if (is_internal) {
3370161b49cSJim Ingham         if (log)
338b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an "
339b9c1b51eSKate Stone                       "internal breakpoint, not stopping.");
34040d871faSJim Ingham         return false;
34140d871faSJim Ingham       }
3420161b49cSJim Ingham     }
34340d871faSJim Ingham 
344b9c1b51eSKate Stone     if (m_ignore_breakpoints) {
3450161b49cSJim Ingham       if (log)
346b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
347b9c1b51eSKate Stone                     "breakpoints, overriding breakpoint stop info ShouldStop, "
348b9c1b51eSKate Stone                     "returning true");
3490161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(false);
350923886ceSJim Ingham       return true;
351b9c1b51eSKate Stone     } else {
3520161b49cSJim Ingham       if (log)
353b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not "
354b9c1b51eSKate Stone                     "ignoring breakpoints, overriding breakpoint stop info "
355b9c1b51eSKate Stone                     "ShouldStop, returning true");
3560161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(true);
3570161b49cSJim Ingham       return false;
3580161b49cSJim Ingham     }
359b9c1b51eSKate Stone   } else if (!m_unwind_on_error) {
360b9c1b51eSKate Stone     // If we don't want to discard this plan, than any stop we don't understand
361b9c1b51eSKate Stone     // should be propagated up the stack.
362923886ceSJim Ingham     return false;
363b9c1b51eSKate Stone   } else {
36405097246SAdrian Prantl     // If the subplan is running, any crashes are attributable to us. If we
36505097246SAdrian Prantl     // want to discard the plan, then we say we explain the stop but if we are
36605097246SAdrian Prantl     // going to be discarded, let whoever is above us explain the stop. But
36705097246SAdrian Prantl     // don't discard the plan if the stop would restart itself (for instance if
36805097246SAdrian Prantl     // it is a signal that is set not to stop.  Check that here first.  We just
36905097246SAdrian Prantl     // say we explain the stop but aren't done and everything will continue on
37005097246SAdrian Prantl     // from there.
3710161b49cSJim Ingham 
372b9c1b51eSKate Stone     if (m_real_stop_info_sp &&
373b9c1b51eSKate Stone         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
374184e9811SJim Ingham       SetPlanComplete(false);
375e65b2cf2SEugene Zelenko       return m_subplan_sp ? m_unwind_on_error : false;
376b9c1b51eSKate Stone     } else
3770161b49cSJim Ingham       return true;
3780161b49cSJim Ingham   }
37940d871faSJim Ingham }
38030fdc8d8SChris Lattner 
381b9c1b51eSKate Stone bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
382b9c1b51eSKate Stone   // We do some computation in DoPlanExplainsStop that may or may not set the
38305097246SAdrian Prantl   // plan as complete. We need to do that here to make sure our state is
38405097246SAdrian Prantl   // correct.
385221d51cfSJim Ingham   DoPlanExplainsStop(event_ptr);
386184e9811SJim Ingham 
387b9c1b51eSKate Stone   if (IsPlanComplete()) {
3889da3683cSJim Ingham     ReportRegisterState("Function completed.  Register state was:");
38930fdc8d8SChris Lattner     return true;
390b9c1b51eSKate Stone   } else {
39130fdc8d8SChris Lattner     return false;
39230fdc8d8SChris Lattner   }
39330fdc8d8SChris Lattner }
39430fdc8d8SChris Lattner 
395b9c1b51eSKate Stone bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
39630fdc8d8SChris Lattner 
397b9c1b51eSKate Stone StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
39830fdc8d8SChris Lattner 
399b9c1b51eSKate Stone void ThreadPlanCallFunction::DidPush() {
400be3a1b14SSean Callanan   //#define SINGLE_STEP_EXPRESSIONS
401be3a1b14SSean Callanan 
402b9c1b51eSKate Stone   // Now set the thread state to "no reason" so we don't run with whatever
40305097246SAdrian Prantl   // signal was outstanding... Wait till the plan is pushed so we aren't
40405097246SAdrian Prantl   // changing the stop info till we're about to run.
4058559a355SJim Ingham 
4068559a355SJim Ingham   GetThread().SetStopInfoToNothing();
4078559a355SJim Ingham 
408be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
409*796ac80bSJonas Devlieghere   m_subplan_sp = std::make_shared<ThreadPlanRunToAddress>(
410*796ac80bSJonas Devlieghere       m_thread, m_start_addr, m_stop_other_threads);
41130fdc8d8SChris Lattner 
41230fdc8d8SChris Lattner   m_thread.QueueThreadPlan(m_subplan_sp, false);
41377787033SJim Ingham   m_subplan_sp->SetPrivate(true);
414be3a1b14SSean Callanan #endif
41530fdc8d8SChris Lattner }
41630fdc8d8SChris Lattner 
417b9c1b51eSKate Stone bool ThreadPlanCallFunction::WillStop() { return true; }
41830fdc8d8SChris Lattner 
419b9c1b51eSKate Stone bool ThreadPlanCallFunction::MischiefManaged() {
4205160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
42130fdc8d8SChris Lattner 
422b9c1b51eSKate Stone   if (IsPlanComplete()) {
42330fdc8d8SChris Lattner     if (log)
424324a1036SSaleem Abdulrasool       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
425324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
42630fdc8d8SChris Lattner 
42730fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
42830fdc8d8SChris Lattner     return true;
429b9c1b51eSKate Stone   } else {
43030fdc8d8SChris Lattner     return false;
43130fdc8d8SChris Lattner   }
43230fdc8d8SChris Lattner }
4336db73ca5SSean Callanan 
434b9c1b51eSKate Stone void ThreadPlanCallFunction::SetBreakpoints() {
4351ac04c30SGreg Clayton   ProcessSP process_sp(m_thread.CalculateProcess());
436b9c1b51eSKate Stone   if (m_trap_exceptions && process_sp) {
437b9c1b51eSKate Stone     m_cxx_language_runtime =
438b9c1b51eSKate Stone         process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
4391ac04c30SGreg Clayton     m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
4406db73ca5SSean Callanan 
441b9c1b51eSKate Stone     if (m_cxx_language_runtime) {
442b9c1b51eSKate Stone       m_should_clear_cxx_exception_bp =
443b9c1b51eSKate Stone           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
444f211510fSSean Callanan       m_cxx_language_runtime->SetExceptionBreakpoints();
4456fbc48bcSJim Ingham     }
446b9c1b51eSKate Stone     if (m_objc_language_runtime) {
447b9c1b51eSKate Stone       m_should_clear_objc_exception_bp =
448b9c1b51eSKate Stone           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
449f211510fSSean Callanan       m_objc_language_runtime->SetExceptionBreakpoints();
4506db73ca5SSean Callanan     }
4511ac04c30SGreg Clayton   }
4526fbc48bcSJim Ingham }
4536db73ca5SSean Callanan 
454b9c1b51eSKate Stone void ThreadPlanCallFunction::ClearBreakpoints() {
455b9c1b51eSKate Stone   if (m_trap_exceptions) {
4566fbc48bcSJim Ingham     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
457f211510fSSean Callanan       m_cxx_language_runtime->ClearExceptionBreakpoints();
4586fbc48bcSJim Ingham     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
459f211510fSSean Callanan       m_objc_language_runtime->ClearExceptionBreakpoints();
4606db73ca5SSean Callanan   }
4616fbc48bcSJim Ingham }
462c98aca60SSean Callanan 
463b9c1b51eSKate Stone bool ThreadPlanCallFunction::BreakpointsExplainStop() {
46460c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
465c98aca60SSean Callanan 
466b9c1b51eSKate Stone   if (m_trap_exceptions) {
467184e9811SJim Ingham     if ((m_cxx_language_runtime &&
468b9c1b51eSKate Stone          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
469b9c1b51eSKate Stone              stop_info_sp)) ||
470b9c1b51eSKate Stone         (m_objc_language_runtime &&
471b9c1b51eSKate Stone          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
472b9c1b51eSKate Stone              stop_info_sp))) {
473641a67ceSJim Ingham       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
474641a67ceSJim Ingham       if (log)
475b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
476b9c1b51eSKate Stone                     "exception breakpoint, setting plan complete.");
477641a67ceSJim Ingham 
478184e9811SJim Ingham       SetPlanComplete(false);
479641a67ceSJim Ingham 
48005097246SAdrian Prantl       // If the user has set the ObjC language breakpoint, it would normally
48105097246SAdrian Prantl       // get priority over our internal catcher breakpoint, but in this case we
48205097246SAdrian Prantl       // can't let that happen, so force the ShouldStop here.
483641a67ceSJim Ingham       stop_info_sp->OverrideShouldStop(true);
484c98aca60SSean Callanan       return true;
485184e9811SJim Ingham     }
4866fbc48bcSJim Ingham   }
487f211510fSSean Callanan 
488c98aca60SSean Callanan   return false;
489c98aca60SSean Callanan }
4908559a355SJim Ingham 
491b9c1b51eSKate Stone void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4920ff099f1SJim Ingham   m_subplan_sp->SetStopOthers(new_value);
4930ff099f1SJim Ingham }
4940ff099f1SJim Ingham 
495b9c1b51eSKate Stone bool ThreadPlanCallFunction::RestoreThreadState() {
4968559a355SJim Ingham   return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
4978559a355SJim Ingham }
4988559a355SJim Ingham 
499b9c1b51eSKate Stone void ThreadPlanCallFunction::SetReturnValue() {
50090ff7911SEwan Crawford   ProcessSP process_sp(m_thread.GetProcess());
501e65b2cf2SEugene Zelenko   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
502b9c1b51eSKate Stone   if (abi && m_return_type.IsValid()) {
50390ff7911SEwan Crawford     const bool persistent = false;
504b9c1b51eSKate Stone     m_return_valobj_sp =
505b9c1b51eSKate Stone         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
50690ff7911SEwan Crawford   }
50790ff7911SEwan Crawford }
508