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 
27796ac80bSJonas Devlieghere #include <memory>
28796ac80bSJonas Devlieghere 
2930fdc8d8SChris Lattner using namespace lldb;
3030fdc8d8SChris Lattner using namespace lldb_private;
3130fdc8d8SChris Lattner 
3230fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
33b9c1b51eSKate Stone bool ThreadPlanCallFunction::ConstructorSetup(
34b9c1b51eSKate Stone     Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
35b9c1b51eSKate Stone     lldb::addr_t &function_load_addr) {
360092c8ebSJim Ingham   SetIsMasterPlan(true);
37923886ceSJim Ingham   SetOkayToDiscard(false);
38327c267aSAndrew Kaylor   SetPrivate(true);
390092c8ebSJim Ingham 
400092c8ebSJim Ingham   ProcessSP process_sp(thread.GetProcess());
410092c8ebSJim Ingham   if (!process_sp)
420092c8ebSJim Ingham     return false;
430092c8ebSJim Ingham 
440092c8ebSJim Ingham   abi = process_sp->GetABI().get();
450092c8ebSJim Ingham 
460092c8ebSJim Ingham   if (!abi)
470092c8ebSJim Ingham     return false;
480092c8ebSJim Ingham 
495160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
500092c8ebSJim Ingham 
510092c8ebSJim Ingham   SetBreakpoints();
520092c8ebSJim Ingham 
530092c8ebSJim Ingham   m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
54b9c1b51eSKate Stone   // If we can't read memory at the point of the process where we are planning
5505097246SAdrian Prantl   // to put our function, we're not going to get any further...
5697206d57SZachary Turner   Status error;
570092c8ebSJim Ingham   process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
58b9c1b51eSKate Stone   if (!error.Success()) {
59b9c1b51eSKate Stone     m_constructor_errors.Printf(
60b9c1b51eSKate Stone         "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
61b9c1b51eSKate Stone         m_function_sp);
620092c8ebSJim Ingham     if (log)
63b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
64324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
650092c8ebSJim Ingham     return false;
660092c8ebSJim Ingham   }
670092c8ebSJim Ingham 
68*0288c269SJonas Devlieghere   llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
69*0288c269SJonas Devlieghere   if (!start_address) {
70*0288c269SJonas Devlieghere     m_constructor_errors.Printf(
71*0288c269SJonas Devlieghere         "%s", llvm::toString(start_address.takeError()).c_str());
72*0288c269SJonas Devlieghere     if (log)
73b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
74324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
750092c8ebSJim Ingham     return false;
760092c8ebSJim Ingham   }
77ea06f3bfSJim Ingham 
78*0288c269SJonas Devlieghere   m_start_addr = *start_address;
796fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
800092c8ebSJim Ingham 
810092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
820092c8ebSJim Ingham   if (log && log->GetVerbose())
83b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
84b9c1b51eSKate Stone                         "Original register state was:");
850092c8ebSJim Ingham 
86b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
87b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
88b9c1b51eSKate Stone                                 "checkpoint thread state.");
890092c8ebSJim Ingham     if (log)
90b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
91324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
920092c8ebSJim Ingham     return false;
930092c8ebSJim Ingham   }
946fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
950092c8ebSJim Ingham 
960092c8ebSJim Ingham   return true;
970092c8ebSJim Ingham }
9830fdc8d8SChris Lattner 
99b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
100b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
101b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
102b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
103b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
104b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1056fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1066fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1076fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
108b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
109b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1106fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1116fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
112b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
11390ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
11490ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
11590ff7911SEwan Crawford   ABI *abi = nullptr;
11690ff7911SEwan Crawford 
117923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1181ac04c30SGreg Clayton     return;
1191ac04c30SGreg Clayton 
120b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
121b9c1b51eSKate Stone                                start_load_addr, args))
12230fdc8d8SChris Lattner     return;
12330fdc8d8SChris Lattner 
1249da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1259da3683cSJim Ingham 
1269da3683cSJim Ingham   m_valid = true;
1279da3683cSJim Ingham }
1289da3683cSJim Ingham 
129b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
130b9c1b51eSKate Stone     Thread &thread, const Address &function,
131b9c1b51eSKate Stone     const EvaluateExpressionOptions &options)
132b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
133b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
134b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
13590ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
13690ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
13790ff7911SEwan Crawford       m_debug_execution(options.GetDebug()),
138b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
139b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
14090ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
14190ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
142b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
14390ff7911SEwan Crawford 
144b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1450161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1469da3683cSJim Ingham }
1479da3683cSJim Ingham 
148b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
1493b7e1981SPavel Labath   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1503b7e1981SPavel Labath   if (log && log->GetVerbose()) {
151af247d7bSGreg Clayton     StreamString strm;
1525ccbd294SGreg Clayton     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
153ece96492SSean Callanan 
1549da3683cSJim Ingham     log->PutCString(message);
155ece96492SSean Callanan 
156af247d7bSGreg Clayton     RegisterValue reg_value;
157ece96492SSean Callanan 
158af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
159b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
160af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
161b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
162e03334cfSPavel Labath         DumpRegisterValue(reg_value, &strm, reg_info, true, false,
163e03334cfSPavel Labath                           eFormatDefault);
164af247d7bSGreg Clayton         strm.EOL();
165ece96492SSean Callanan       }
166ece96492SSean Callanan     }
167c156427dSZachary Turner     log->PutString(strm.GetString());
168af247d7bSGreg Clayton   }
16910af7c43SSean Callanan }
17010af7c43SSean Callanan 
171b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
1725160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
17387d0e618SJim Ingham 
174b9c1b51eSKate Stone   if (!m_valid) {
17587d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
17687d0e618SJim Ingham     if (log)
177b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): Log called on "
178b9c1b51eSKate Stone                   "ThreadPlanCallFunction that was never valid.",
179324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
18087d0e618SJim Ingham     return;
18187d0e618SJim Ingham   }
18287d0e618SJim Ingham 
183b9c1b51eSKate Stone   if (!m_takedown_done) {
184b9c1b51eSKate Stone     if (success) {
18590ff7911SEwan Crawford       SetReturnValue();
18618de2fdcSJim Ingham     }
1879da3683cSJim Ingham     if (log)
188b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread "
189b9c1b51eSKate Stone                   "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
190324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
191324a1036SSaleem Abdulrasool                   IsPlanComplete());
1929da3683cSJim Ingham     m_takedown_done = true;
193b9c1b51eSKate Stone     m_stop_address =
194b9c1b51eSKate Stone         m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
19560c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
196b9c1b51eSKate Stone     if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
1977b24e95eSEd Maste       if (log)
198b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore "
199b9c1b51eSKate Stone                     "register state",
200324a1036SSaleem Abdulrasool                     static_cast<void *>(this));
2017b24e95eSEd Maste     }
20218de2fdcSJim Ingham     SetPlanComplete(success);
20310af7c43SSean Callanan     ClearBreakpoints();
2049da3683cSJim Ingham     if (log && log->GetVerbose())
205b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
206b9c1b51eSKate Stone                           "Restored register state:");
207b9c1b51eSKate Stone   } else {
2089da3683cSJim Ingham     if (log)
209b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
210b9c1b51eSKate Stone                   "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
211324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
212324a1036SSaleem Abdulrasool                   IsPlanComplete());
21330fdc8d8SChris Lattner   }
21477787033SJim Ingham }
21530fdc8d8SChris Lattner 
216b9c1b51eSKate Stone void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
217bda4e5ebSJim Ingham 
218b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
219b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
22030fdc8d8SChris Lattner     s->Printf("Function call thread plan");
221b9c1b51eSKate Stone   } else {
2221ac04c30SGreg Clayton     TargetSP target_sp(m_thread.CalculateTarget());
223b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
224b9c1b51eSKate Stone               m_function_addr.GetLoadAddress(target_sp.get()));
22530fdc8d8SChris Lattner   }
22630fdc8d8SChris Lattner }
22730fdc8d8SChris Lattner 
228b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
229b9c1b51eSKate Stone   if (!m_valid) {
230b9c1b51eSKate Stone     if (error) {
231ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
232c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
233ea06f3bfSJim Ingham       else
234ea06f3bfSJim Ingham         error->PutCString("Unknown error");
235ea06f3bfSJim Ingham     }
23630fdc8d8SChris Lattner     return false;
237ea06f3bfSJim Ingham   }
23830fdc8d8SChris Lattner 
23930fdc8d8SChris Lattner   return true;
24030fdc8d8SChris Lattner }
24130fdc8d8SChris Lattner 
242b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2430161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2440161b49cSJim Ingham     return eVoteYes;
2450161b49cSJim Ingham   else
2460161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2470161b49cSJim Ingham }
2480161b49cSJim Ingham 
249b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
250b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
251b9c1b51eSKate Stone                                                   LIBLLDB_LOG_PROCESS));
25260c4118cSJim Ingham   m_real_stop_info_sp = GetPrivateStopInfo();
253160f78c5SJim Ingham 
25405097246SAdrian Prantl   // If our subplan knows why we stopped, even if it's done (which would
25505097246SAdrian Prantl   // forward the question to us) we answer yes.
256b9c1b51eSKate Stone   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
257184e9811SJim Ingham     SetPlanComplete();
25840d871faSJim Ingham     return true;
259184e9811SJim Ingham   }
2603e6fedcaSSean Callanan 
261c98aca60SSean Callanan   // Check if the breakpoint is one of ours.
262c98aca60SSean Callanan 
26318de2fdcSJim Ingham   StopReason stop_reason;
26418de2fdcSJim Ingham   if (!m_real_stop_info_sp)
26518de2fdcSJim Ingham     stop_reason = eStopReasonNone;
26618de2fdcSJim Ingham   else
26718de2fdcSJim Ingham     stop_reason = m_real_stop_info_sp->GetStopReason();
2680161b49cSJim Ingham   if (log)
269b9c1b51eSKate Stone     log->Printf(
270b9c1b51eSKate Stone         "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.",
271b9c1b51eSKate Stone         Thread::StopReasonAsCString(stop_reason));
27218de2fdcSJim Ingham 
27318de2fdcSJim Ingham   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
274c98aca60SSean Callanan     return true;
275c98aca60SSean Callanan 
276b9c1b51eSKate Stone   // One more quirk here.  If this event was from Halt interrupting the target,
27705097246SAdrian Prantl   // then we should not consider ourselves complete.  Return true to
27805097246SAdrian Prantl   // acknowledge the stop.
279b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
28035878c47SJim Ingham     if (log)
281b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an "
282b9c1b51eSKate Stone                   "Interrupt, returning true.");
28335878c47SJim Ingham     return true;
28435878c47SJim Ingham   }
2850161b49cSJim Ingham   // We control breakpoints separately from other "stop reasons."  So first,
286b9c1b51eSKate Stone   // check the case where we stopped for an internal breakpoint, in that case,
28705097246SAdrian Prantl   // continue on. If it is not an internal breakpoint, consult
28805097246SAdrian Prantl   // m_ignore_breakpoints.
2896db73ca5SSean Callanan 
290b9c1b51eSKate Stone   if (stop_reason == eStopReasonBreakpoint) {
2911ac04c30SGreg Clayton     ProcessSP process_sp(m_thread.CalculateProcess());
292160f78c5SJim Ingham     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
2931ac04c30SGreg Clayton     BreakpointSiteSP bp_site_sp;
2941ac04c30SGreg Clayton     if (process_sp)
2951ac04c30SGreg Clayton       bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
296b9c1b51eSKate Stone     if (bp_site_sp) {
29740d871faSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
29840d871faSJim Ingham       bool is_internal = true;
299b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
3006db73ca5SSean Callanan         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3010161b49cSJim Ingham         if (log)
302b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit "
303b9c1b51eSKate Stone                       "breakpoint %d while calling function",
304b9c1b51eSKate Stone                       bp.GetID());
3056db73ca5SSean Callanan 
306b9c1b51eSKate Stone         if (!bp.IsInternal()) {
30740d871faSJim Ingham           is_internal = false;
30840d871faSJim Ingham           break;
30940d871faSJim Ingham         }
31040d871faSJim Ingham       }
311b9c1b51eSKate Stone       if (is_internal) {
3120161b49cSJim Ingham         if (log)
313b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an "
314b9c1b51eSKate Stone                       "internal breakpoint, not stopping.");
31540d871faSJim Ingham         return false;
31640d871faSJim Ingham       }
3170161b49cSJim Ingham     }
31840d871faSJim Ingham 
319b9c1b51eSKate Stone     if (m_ignore_breakpoints) {
3200161b49cSJim Ingham       if (log)
321b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
322b9c1b51eSKate Stone                     "breakpoints, overriding breakpoint stop info ShouldStop, "
323b9c1b51eSKate Stone                     "returning true");
3240161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(false);
325923886ceSJim Ingham       return true;
326b9c1b51eSKate Stone     } else {
3270161b49cSJim Ingham       if (log)
328b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not "
329b9c1b51eSKate Stone                     "ignoring breakpoints, overriding breakpoint stop info "
330b9c1b51eSKate Stone                     "ShouldStop, returning true");
3310161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(true);
3320161b49cSJim Ingham       return false;
3330161b49cSJim Ingham     }
334b9c1b51eSKate Stone   } else if (!m_unwind_on_error) {
335b9c1b51eSKate Stone     // If we don't want to discard this plan, than any stop we don't understand
336b9c1b51eSKate Stone     // should be propagated up the stack.
337923886ceSJim Ingham     return false;
338b9c1b51eSKate Stone   } else {
33905097246SAdrian Prantl     // If the subplan is running, any crashes are attributable to us. If we
34005097246SAdrian Prantl     // want to discard the plan, then we say we explain the stop but if we are
34105097246SAdrian Prantl     // going to be discarded, let whoever is above us explain the stop. But
34205097246SAdrian Prantl     // don't discard the plan if the stop would restart itself (for instance if
34305097246SAdrian Prantl     // it is a signal that is set not to stop.  Check that here first.  We just
34405097246SAdrian Prantl     // say we explain the stop but aren't done and everything will continue on
34505097246SAdrian Prantl     // from there.
3460161b49cSJim Ingham 
347b9c1b51eSKate Stone     if (m_real_stop_info_sp &&
348b9c1b51eSKate Stone         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
349184e9811SJim Ingham       SetPlanComplete(false);
350e65b2cf2SEugene Zelenko       return m_subplan_sp ? m_unwind_on_error : false;
351b9c1b51eSKate Stone     } else
3520161b49cSJim Ingham       return true;
3530161b49cSJim Ingham   }
35440d871faSJim Ingham }
35530fdc8d8SChris Lattner 
356b9c1b51eSKate Stone bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
357b9c1b51eSKate Stone   // We do some computation in DoPlanExplainsStop that may or may not set the
35805097246SAdrian Prantl   // plan as complete. We need to do that here to make sure our state is
35905097246SAdrian Prantl   // correct.
360221d51cfSJim Ingham   DoPlanExplainsStop(event_ptr);
361184e9811SJim Ingham 
362b9c1b51eSKate Stone   if (IsPlanComplete()) {
3639da3683cSJim Ingham     ReportRegisterState("Function completed.  Register state was:");
36430fdc8d8SChris Lattner     return true;
365b9c1b51eSKate Stone   } else {
36630fdc8d8SChris Lattner     return false;
36730fdc8d8SChris Lattner   }
36830fdc8d8SChris Lattner }
36930fdc8d8SChris Lattner 
370b9c1b51eSKate Stone bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
37130fdc8d8SChris Lattner 
372b9c1b51eSKate Stone StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
37330fdc8d8SChris Lattner 
374b9c1b51eSKate Stone void ThreadPlanCallFunction::DidPush() {
375be3a1b14SSean Callanan   //#define SINGLE_STEP_EXPRESSIONS
376be3a1b14SSean Callanan 
377b9c1b51eSKate Stone   // Now set the thread state to "no reason" so we don't run with whatever
37805097246SAdrian Prantl   // signal was outstanding... Wait till the plan is pushed so we aren't
37905097246SAdrian Prantl   // changing the stop info till we're about to run.
3808559a355SJim Ingham 
3818559a355SJim Ingham   GetThread().SetStopInfoToNothing();
3828559a355SJim Ingham 
383be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
384796ac80bSJonas Devlieghere   m_subplan_sp = std::make_shared<ThreadPlanRunToAddress>(
385796ac80bSJonas Devlieghere       m_thread, m_start_addr, m_stop_other_threads);
38630fdc8d8SChris Lattner 
38730fdc8d8SChris Lattner   m_thread.QueueThreadPlan(m_subplan_sp, false);
38877787033SJim Ingham   m_subplan_sp->SetPrivate(true);
389be3a1b14SSean Callanan #endif
39030fdc8d8SChris Lattner }
39130fdc8d8SChris Lattner 
392b9c1b51eSKate Stone bool ThreadPlanCallFunction::WillStop() { return true; }
39330fdc8d8SChris Lattner 
394b9c1b51eSKate Stone bool ThreadPlanCallFunction::MischiefManaged() {
3955160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
39630fdc8d8SChris Lattner 
397b9c1b51eSKate Stone   if (IsPlanComplete()) {
39830fdc8d8SChris Lattner     if (log)
399324a1036SSaleem Abdulrasool       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
400324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
40130fdc8d8SChris Lattner 
40230fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
40330fdc8d8SChris Lattner     return true;
404b9c1b51eSKate Stone   } else {
40530fdc8d8SChris Lattner     return false;
40630fdc8d8SChris Lattner   }
40730fdc8d8SChris Lattner }
4086db73ca5SSean Callanan 
409b9c1b51eSKate Stone void ThreadPlanCallFunction::SetBreakpoints() {
4101ac04c30SGreg Clayton   ProcessSP process_sp(m_thread.CalculateProcess());
411b9c1b51eSKate Stone   if (m_trap_exceptions && process_sp) {
412b9c1b51eSKate Stone     m_cxx_language_runtime =
413b9c1b51eSKate Stone         process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
4141ac04c30SGreg Clayton     m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
4156db73ca5SSean Callanan 
416b9c1b51eSKate Stone     if (m_cxx_language_runtime) {
417b9c1b51eSKate Stone       m_should_clear_cxx_exception_bp =
418b9c1b51eSKate Stone           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
419f211510fSSean Callanan       m_cxx_language_runtime->SetExceptionBreakpoints();
4206fbc48bcSJim Ingham     }
421b9c1b51eSKate Stone     if (m_objc_language_runtime) {
422b9c1b51eSKate Stone       m_should_clear_objc_exception_bp =
423b9c1b51eSKate Stone           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
424f211510fSSean Callanan       m_objc_language_runtime->SetExceptionBreakpoints();
4256db73ca5SSean Callanan     }
4261ac04c30SGreg Clayton   }
4276fbc48bcSJim Ingham }
4286db73ca5SSean Callanan 
429b9c1b51eSKate Stone void ThreadPlanCallFunction::ClearBreakpoints() {
430b9c1b51eSKate Stone   if (m_trap_exceptions) {
4316fbc48bcSJim Ingham     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
432f211510fSSean Callanan       m_cxx_language_runtime->ClearExceptionBreakpoints();
4336fbc48bcSJim Ingham     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
434f211510fSSean Callanan       m_objc_language_runtime->ClearExceptionBreakpoints();
4356db73ca5SSean Callanan   }
4366fbc48bcSJim Ingham }
437c98aca60SSean Callanan 
438b9c1b51eSKate Stone bool ThreadPlanCallFunction::BreakpointsExplainStop() {
43960c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
440c98aca60SSean Callanan 
441b9c1b51eSKate Stone   if (m_trap_exceptions) {
442184e9811SJim Ingham     if ((m_cxx_language_runtime &&
443b9c1b51eSKate Stone          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
444b9c1b51eSKate Stone              stop_info_sp)) ||
445b9c1b51eSKate Stone         (m_objc_language_runtime &&
446b9c1b51eSKate Stone          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
447b9c1b51eSKate Stone              stop_info_sp))) {
448641a67ceSJim Ingham       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
449641a67ceSJim Ingham       if (log)
450b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
451b9c1b51eSKate Stone                     "exception breakpoint, setting plan complete.");
452641a67ceSJim Ingham 
453184e9811SJim Ingham       SetPlanComplete(false);
454641a67ceSJim Ingham 
45505097246SAdrian Prantl       // If the user has set the ObjC language breakpoint, it would normally
45605097246SAdrian Prantl       // get priority over our internal catcher breakpoint, but in this case we
45705097246SAdrian Prantl       // can't let that happen, so force the ShouldStop here.
458641a67ceSJim Ingham       stop_info_sp->OverrideShouldStop(true);
459c98aca60SSean Callanan       return true;
460184e9811SJim Ingham     }
4616fbc48bcSJim Ingham   }
462f211510fSSean Callanan 
463c98aca60SSean Callanan   return false;
464c98aca60SSean Callanan }
4658559a355SJim Ingham 
466b9c1b51eSKate Stone void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4670ff099f1SJim Ingham   m_subplan_sp->SetStopOthers(new_value);
4680ff099f1SJim Ingham }
4690ff099f1SJim Ingham 
470b9c1b51eSKate Stone bool ThreadPlanCallFunction::RestoreThreadState() {
4718559a355SJim Ingham   return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
4728559a355SJim Ingham }
4738559a355SJim Ingham 
474b9c1b51eSKate Stone void ThreadPlanCallFunction::SetReturnValue() {
47590ff7911SEwan Crawford   ProcessSP process_sp(m_thread.GetProcess());
476e65b2cf2SEugene Zelenko   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
477b9c1b51eSKate Stone   if (abi && m_return_type.IsValid()) {
47890ff7911SEwan Crawford     const bool persistent = false;
479b9c1b51eSKate Stone     m_return_valobj_sp =
480b9c1b51eSKate Stone         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
48190ff7911SEwan Crawford   }
48290ff7911SEwan Crawford }
483