180814287SRaphael Isemann //===-- ThreadPlanCallFunction.cpp ----------------------------------------===//
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"
24*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
256f9e6901SZachary Turner #include "lldb/Utility/Log.h"
26bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2730fdc8d8SChris Lattner 
28796ac80bSJonas Devlieghere #include <memory>
29796ac80bSJonas Devlieghere 
3030fdc8d8SChris Lattner using namespace lldb;
3130fdc8d8SChris Lattner using namespace lldb_private;
3230fdc8d8SChris Lattner 
3330fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
34b9c1b51eSKate Stone bool ThreadPlanCallFunction::ConstructorSetup(
35b9c1b51eSKate Stone     Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
36b9c1b51eSKate Stone     lldb::addr_t &function_load_addr) {
3704cbfa95SQuinn Pham   SetIsControllingPlan(true);
38923886ceSJim Ingham   SetOkayToDiscard(false);
39327c267aSAndrew Kaylor   SetPrivate(true);
400092c8ebSJim Ingham 
410092c8ebSJim Ingham   ProcessSP process_sp(thread.GetProcess());
420092c8ebSJim Ingham   if (!process_sp)
430092c8ebSJim Ingham     return false;
440092c8ebSJim Ingham 
450092c8ebSJim Ingham   abi = process_sp->GetABI().get();
460092c8ebSJim Ingham 
470092c8ebSJim Ingham   if (!abi)
480092c8ebSJim Ingham     return false;
490092c8ebSJim Ingham 
50a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
510092c8ebSJim Ingham 
520092c8ebSJim Ingham   SetBreakpoints();
530092c8ebSJim Ingham 
540092c8ebSJim Ingham   m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
55b9c1b51eSKate Stone   // If we can't read memory at the point of the process where we are planning
5605097246SAdrian Prantl   // to put our function, we're not going to get any further...
5797206d57SZachary Turner   Status error;
580092c8ebSJim Ingham   process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
59b9c1b51eSKate Stone   if (!error.Success()) {
60b9c1b51eSKate Stone     m_constructor_errors.Printf(
61b9c1b51eSKate Stone         "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
62b9c1b51eSKate Stone         m_function_sp);
6363e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
64324a1036SSaleem Abdulrasool               m_constructor_errors.GetData());
650092c8ebSJim Ingham     return false;
660092c8ebSJim Ingham   }
670092c8ebSJim Ingham 
680288c269SJonas Devlieghere   llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
690288c269SJonas Devlieghere   if (!start_address) {
700288c269SJonas Devlieghere     m_constructor_errors.Printf(
710288c269SJonas Devlieghere         "%s", llvm::toString(start_address.takeError()).c_str());
7263e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
73324a1036SSaleem Abdulrasool               m_constructor_errors.GetData());
740092c8ebSJim Ingham     return false;
750092c8ebSJim Ingham   }
76ea06f3bfSJim Ingham 
770288c269SJonas Devlieghere   m_start_addr = *start_address;
786fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
790092c8ebSJim Ingham 
800092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
810092c8ebSJim Ingham   if (log && log->GetVerbose())
82b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
83b9c1b51eSKate Stone                         "Original register state was:");
840092c8ebSJim Ingham 
85b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
86b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
87b9c1b51eSKate Stone                                 "checkpoint thread state.");
8863e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
89324a1036SSaleem Abdulrasool               m_constructor_errors.GetData());
900092c8ebSJim Ingham     return false;
910092c8ebSJim Ingham   }
926fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
930092c8ebSJim Ingham 
940092c8ebSJim Ingham   return true;
950092c8ebSJim Ingham }
9630fdc8d8SChris Lattner 
97b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
98b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
99b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
100b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
101b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
102b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1036fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1046fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1056fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
106b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
107b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1086fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1096fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
110b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
11190ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
11290ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
11390ff7911SEwan Crawford   ABI *abi = nullptr;
11490ff7911SEwan Crawford 
115923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1161ac04c30SGreg Clayton     return;
1171ac04c30SGreg Clayton 
118b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
119b9c1b51eSKate Stone                                start_load_addr, args))
12030fdc8d8SChris Lattner     return;
12130fdc8d8SChris Lattner 
1229da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1239da3683cSJim Ingham 
1249da3683cSJim Ingham   m_valid = true;
1259da3683cSJim Ingham }
1269da3683cSJim Ingham 
127b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
128b9c1b51eSKate Stone     Thread &thread, const Address &function,
129b9c1b51eSKate Stone     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()),
13390ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
13490ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
13590ff7911SEwan Crawford       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),
13890ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
13990ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
140b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
14190ff7911SEwan Crawford 
142b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1430161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1449da3683cSJim Ingham }
1459da3683cSJim Ingham 
146b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
147a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
1483b7e1981SPavel Labath   if (log && log->GetVerbose()) {
149af247d7bSGreg Clayton     StreamString strm;
150e4598dc0SJim Ingham     RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
151ece96492SSean Callanan 
1529da3683cSJim Ingham     log->PutCString(message);
153ece96492SSean Callanan 
154af247d7bSGreg Clayton     RegisterValue reg_value;
155ece96492SSean Callanan 
156af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
157b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
158af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
159b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
160e03334cfSPavel Labath         DumpRegisterValue(reg_value, &strm, reg_info, true, false,
161e03334cfSPavel Labath                           eFormatDefault);
162af247d7bSGreg Clayton         strm.EOL();
163ece96492SSean Callanan       }
164ece96492SSean Callanan     }
165c156427dSZachary Turner     log->PutString(strm.GetString());
166af247d7bSGreg Clayton   }
16710af7c43SSean Callanan }
16810af7c43SSean Callanan 
169b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
170a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
17187d0e618SJim Ingham 
172b9c1b51eSKate Stone   if (!m_valid) {
17387d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
17463e5fb76SJonas Devlieghere     LLDB_LOGF(log,
17563e5fb76SJonas Devlieghere               "ThreadPlanCallFunction(%p): Log called on "
176b9c1b51eSKate Stone               "ThreadPlanCallFunction that was never valid.",
177324a1036SSaleem Abdulrasool               static_cast<void *>(this));
17887d0e618SJim Ingham     return;
17987d0e618SJim Ingham   }
18087d0e618SJim Ingham 
181b9c1b51eSKate Stone   if (!m_takedown_done) {
182e4598dc0SJim Ingham     Thread &thread = GetThread();
183b9c1b51eSKate Stone     if (success) {
18490ff7911SEwan Crawford       SetReturnValue();
18518de2fdcSJim Ingham     }
18663e5fb76SJonas Devlieghere     LLDB_LOGF(log,
18763e5fb76SJonas Devlieghere               "ThreadPlanCallFunction(%p): DoTakedown called for thread "
188b9c1b51eSKate Stone               "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
189e4598dc0SJim Ingham               static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
1909da3683cSJim Ingham     m_takedown_done = true;
191b9c1b51eSKate Stone     m_stop_address =
192e4598dc0SJim Ingham         thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
19360c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
194e4598dc0SJim Ingham     if (!thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
19563e5fb76SJonas Devlieghere       LLDB_LOGF(log,
19663e5fb76SJonas Devlieghere                 "ThreadPlanCallFunction(%p): DoTakedown failed to restore "
197b9c1b51eSKate Stone                 "register state",
198324a1036SSaleem Abdulrasool                 static_cast<void *>(this));
1997b24e95eSEd Maste     }
20018de2fdcSJim Ingham     SetPlanComplete(success);
20110af7c43SSean Callanan     ClearBreakpoints();
2029da3683cSJim Ingham     if (log && log->GetVerbose())
203b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
204b9c1b51eSKate Stone                           "Restored register state:");
205b9c1b51eSKate Stone   } else {
20663e5fb76SJonas Devlieghere     LLDB_LOGF(log,
20763e5fb76SJonas Devlieghere               "ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
208b9c1b51eSKate Stone               "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
209e4598dc0SJim Ingham               static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
21030fdc8d8SChris Lattner   }
21177787033SJim Ingham }
21230fdc8d8SChris Lattner 
21341d0b20cSDave Lee void ThreadPlanCallFunction::DidPop() { DoTakedown(PlanSucceeded()); }
214bda4e5ebSJim Ingham 
215b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
216b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
21730fdc8d8SChris Lattner     s->Printf("Function call thread plan");
218b9c1b51eSKate Stone   } else {
219b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
220e4598dc0SJim Ingham               m_function_addr.GetLoadAddress(&GetTarget()));
22130fdc8d8SChris Lattner   }
22230fdc8d8SChris Lattner }
22330fdc8d8SChris Lattner 
224b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
225b9c1b51eSKate Stone   if (!m_valid) {
226b9c1b51eSKate Stone     if (error) {
227ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
228c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
229ea06f3bfSJim Ingham       else
230ea06f3bfSJim Ingham         error->PutCString("Unknown error");
231ea06f3bfSJim Ingham     }
23230fdc8d8SChris Lattner     return false;
233ea06f3bfSJim Ingham   }
23430fdc8d8SChris Lattner 
23530fdc8d8SChris Lattner   return true;
23630fdc8d8SChris Lattner }
23730fdc8d8SChris Lattner 
238b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2390161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2400161b49cSJim Ingham     return eVoteYes;
2410161b49cSJim Ingham   else
2420161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2430161b49cSJim Ingham }
2440161b49cSJim Ingham 
245b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
246a007a6d8SPavel Labath   Log *log(GetLog(LLDBLog::Step | LLDBLog::Process));
24760c4118cSJim Ingham   m_real_stop_info_sp = GetPrivateStopInfo();
248160f78c5SJim Ingham 
24905097246SAdrian Prantl   // If our subplan knows why we stopped, even if it's done (which would
25005097246SAdrian Prantl   // forward the question to us) we answer yes.
251b9c1b51eSKate Stone   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
252184e9811SJim Ingham     SetPlanComplete();
25340d871faSJim Ingham     return true;
254184e9811SJim Ingham   }
2553e6fedcaSSean Callanan 
256c98aca60SSean Callanan   // Check if the breakpoint is one of ours.
257c98aca60SSean Callanan 
25818de2fdcSJim Ingham   StopReason stop_reason;
25918de2fdcSJim Ingham   if (!m_real_stop_info_sp)
26018de2fdcSJim Ingham     stop_reason = eStopReasonNone;
26118de2fdcSJim Ingham   else
26218de2fdcSJim Ingham     stop_reason = m_real_stop_info_sp->GetStopReason();
263a4a08442SRaphael Isemann   LLDB_LOG(log,
264a4a08442SRaphael Isemann            "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - {0}.",
265a4a08442SRaphael Isemann            Thread::StopReasonAsString(stop_reason));
26618de2fdcSJim Ingham 
26718de2fdcSJim Ingham   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
268c98aca60SSean Callanan     return true;
269c98aca60SSean Callanan 
270b9c1b51eSKate Stone   // One more quirk here.  If this event was from Halt interrupting the target,
27105097246SAdrian Prantl   // then we should not consider ourselves complete.  Return true to
27205097246SAdrian Prantl   // acknowledge the stop.
273b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
27463e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop: The event is an "
275b9c1b51eSKate Stone                    "Interrupt, returning true.");
27635878c47SJim Ingham     return true;
27735878c47SJim Ingham   }
2780161b49cSJim Ingham   // We control breakpoints separately from other "stop reasons."  So first,
279b9c1b51eSKate Stone   // check the case where we stopped for an internal breakpoint, in that case,
28005097246SAdrian Prantl   // continue on. If it is not an internal breakpoint, consult
28105097246SAdrian Prantl   // m_ignore_breakpoints.
2826db73ca5SSean Callanan 
283b9c1b51eSKate Stone   if (stop_reason == eStopReasonBreakpoint) {
284160f78c5SJim Ingham     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
2851ac04c30SGreg Clayton     BreakpointSiteSP bp_site_sp;
286e4598dc0SJim Ingham     bp_site_sp = m_process.GetBreakpointSiteList().FindByID(break_site_id);
287b9c1b51eSKate Stone     if (bp_site_sp) {
28840d871faSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
28940d871faSJim Ingham       bool is_internal = true;
290b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
2916db73ca5SSean Callanan         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
29263e5fb76SJonas Devlieghere         LLDB_LOGF(log,
29363e5fb76SJonas Devlieghere                   "ThreadPlanCallFunction::PlanExplainsStop: hit "
294b9c1b51eSKate Stone                   "breakpoint %d while calling function",
295b9c1b51eSKate Stone                   bp.GetID());
2966db73ca5SSean Callanan 
297b9c1b51eSKate Stone         if (!bp.IsInternal()) {
29840d871faSJim Ingham           is_internal = false;
29940d871faSJim Ingham           break;
30040d871faSJim Ingham         }
30140d871faSJim Ingham       }
302b9c1b51eSKate Stone       if (is_internal) {
30363e5fb76SJonas Devlieghere         LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop hit an "
304b9c1b51eSKate Stone                        "internal breakpoint, not stopping.");
30540d871faSJim Ingham         return false;
30640d871faSJim Ingham       }
3070161b49cSJim Ingham     }
30840d871faSJim Ingham 
309b9c1b51eSKate Stone     if (m_ignore_breakpoints) {
31063e5fb76SJonas Devlieghere       LLDB_LOGF(log,
31163e5fb76SJonas Devlieghere                 "ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
312b9c1b51eSKate Stone                 "breakpoints, overriding breakpoint stop info ShouldStop, "
313b9c1b51eSKate Stone                 "returning true");
3140161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(false);
315923886ceSJim Ingham       return true;
316b9c1b51eSKate Stone     } else {
31763e5fb76SJonas Devlieghere       LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop: we are not "
318b9c1b51eSKate Stone                      "ignoring breakpoints, overriding breakpoint stop info "
319b9c1b51eSKate Stone                      "ShouldStop, returning true");
3200161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(true);
3210161b49cSJim Ingham       return false;
3220161b49cSJim Ingham     }
323b9c1b51eSKate Stone   } else if (!m_unwind_on_error) {
324b9c1b51eSKate Stone     // If we don't want to discard this plan, than any stop we don't understand
325b9c1b51eSKate Stone     // should be propagated up the stack.
326923886ceSJim Ingham     return false;
327b9c1b51eSKate Stone   } else {
32805097246SAdrian Prantl     // If the subplan is running, any crashes are attributable to us. If we
32905097246SAdrian Prantl     // want to discard the plan, then we say we explain the stop but if we are
33005097246SAdrian Prantl     // going to be discarded, let whoever is above us explain the stop. But
33105097246SAdrian Prantl     // don't discard the plan if the stop would restart itself (for instance if
33205097246SAdrian Prantl     // it is a signal that is set not to stop.  Check that here first.  We just
33305097246SAdrian Prantl     // say we explain the stop but aren't done and everything will continue on
33405097246SAdrian Prantl     // from there.
3350161b49cSJim Ingham 
336b9c1b51eSKate Stone     if (m_real_stop_info_sp &&
337b9c1b51eSKate Stone         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
338184e9811SJim Ingham       SetPlanComplete(false);
339e65b2cf2SEugene Zelenko       return m_subplan_sp ? m_unwind_on_error : false;
340b9c1b51eSKate Stone     } else
3410161b49cSJim Ingham       return true;
3420161b49cSJim Ingham   }
34340d871faSJim Ingham }
34430fdc8d8SChris Lattner 
345b9c1b51eSKate Stone bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
346b9c1b51eSKate Stone   // We do some computation in DoPlanExplainsStop that may or may not set the
34705097246SAdrian Prantl   // plan as complete. We need to do that here to make sure our state is
34805097246SAdrian Prantl   // correct.
349221d51cfSJim Ingham   DoPlanExplainsStop(event_ptr);
350184e9811SJim Ingham 
351b9c1b51eSKate Stone   if (IsPlanComplete()) {
3529da3683cSJim Ingham     ReportRegisterState("Function completed.  Register state was:");
35330fdc8d8SChris Lattner     return true;
354b9c1b51eSKate Stone   } else {
35530fdc8d8SChris Lattner     return false;
35630fdc8d8SChris Lattner   }
35730fdc8d8SChris Lattner }
35830fdc8d8SChris Lattner 
359b9c1b51eSKate Stone bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
36030fdc8d8SChris Lattner 
361b9c1b51eSKate Stone StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
36230fdc8d8SChris Lattner 
363b9c1b51eSKate Stone void ThreadPlanCallFunction::DidPush() {
364be3a1b14SSean Callanan   //#define SINGLE_STEP_EXPRESSIONS
365be3a1b14SSean Callanan 
366b9c1b51eSKate Stone   // Now set the thread state to "no reason" so we don't run with whatever
36705097246SAdrian Prantl   // signal was outstanding... Wait till the plan is pushed so we aren't
36805097246SAdrian Prantl   // changing the stop info till we're about to run.
3698559a355SJim Ingham 
3708559a355SJim Ingham   GetThread().SetStopInfoToNothing();
3718559a355SJim Ingham 
372be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
373e4598dc0SJim Ingham   Thread &thread = GetThread();
374e4598dc0SJim Ingham   m_subplan_sp = std::make_shared<ThreadPlanRunToAddress>(thread, m_start_addr,
375e4598dc0SJim Ingham                                                           m_stop_other_threads);
37630fdc8d8SChris Lattner 
377e4598dc0SJim Ingham   thread.QueueThreadPlan(m_subplan_sp, false);
37877787033SJim Ingham   m_subplan_sp->SetPrivate(true);
379be3a1b14SSean Callanan #endif
38030fdc8d8SChris Lattner }
38130fdc8d8SChris Lattner 
382b9c1b51eSKate Stone bool ThreadPlanCallFunction::WillStop() { return true; }
38330fdc8d8SChris Lattner 
384b9c1b51eSKate Stone bool ThreadPlanCallFunction::MischiefManaged() {
385a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
38630fdc8d8SChris Lattner 
387b9c1b51eSKate Stone   if (IsPlanComplete()) {
38863e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): Completed call function plan.",
389324a1036SSaleem Abdulrasool               static_cast<void *>(this));
39030fdc8d8SChris Lattner 
39130fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
39230fdc8d8SChris Lattner     return true;
393b9c1b51eSKate Stone   } else {
39430fdc8d8SChris Lattner     return false;
39530fdc8d8SChris Lattner   }
39630fdc8d8SChris Lattner }
3976db73ca5SSean Callanan 
398b9c1b51eSKate Stone void ThreadPlanCallFunction::SetBreakpoints() {
399e4598dc0SJim Ingham   if (m_trap_exceptions) {
400b9c1b51eSKate Stone     m_cxx_language_runtime =
401e4598dc0SJim Ingham         m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
402e4598dc0SJim Ingham     m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
4036db73ca5SSean Callanan 
404b9c1b51eSKate Stone     if (m_cxx_language_runtime) {
405b9c1b51eSKate Stone       m_should_clear_cxx_exception_bp =
406b9c1b51eSKate Stone           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
407f211510fSSean Callanan       m_cxx_language_runtime->SetExceptionBreakpoints();
4086fbc48bcSJim Ingham     }
409b9c1b51eSKate Stone     if (m_objc_language_runtime) {
410b9c1b51eSKate Stone       m_should_clear_objc_exception_bp =
411b9c1b51eSKate Stone           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
412f211510fSSean Callanan       m_objc_language_runtime->SetExceptionBreakpoints();
4136db73ca5SSean Callanan     }
4141ac04c30SGreg Clayton   }
4156fbc48bcSJim Ingham }
4166db73ca5SSean Callanan 
417b9c1b51eSKate Stone void ThreadPlanCallFunction::ClearBreakpoints() {
418b9c1b51eSKate Stone   if (m_trap_exceptions) {
4196fbc48bcSJim Ingham     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
420f211510fSSean Callanan       m_cxx_language_runtime->ClearExceptionBreakpoints();
4216fbc48bcSJim Ingham     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
422f211510fSSean Callanan       m_objc_language_runtime->ClearExceptionBreakpoints();
4236db73ca5SSean Callanan   }
4246fbc48bcSJim Ingham }
425c98aca60SSean Callanan 
426b9c1b51eSKate Stone bool ThreadPlanCallFunction::BreakpointsExplainStop() {
42760c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
428c98aca60SSean Callanan 
429b9c1b51eSKate Stone   if (m_trap_exceptions) {
430184e9811SJim Ingham     if ((m_cxx_language_runtime &&
431b9c1b51eSKate Stone          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
432b9c1b51eSKate Stone              stop_info_sp)) ||
433b9c1b51eSKate Stone         (m_objc_language_runtime &&
434b9c1b51eSKate Stone          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
435b9c1b51eSKate Stone              stop_info_sp))) {
436a007a6d8SPavel Labath       Log *log = GetLog(LLDBLog::Step);
43763e5fb76SJonas Devlieghere       LLDB_LOGF(log, "ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
438b9c1b51eSKate Stone                      "exception breakpoint, setting plan complete.");
439641a67ceSJim Ingham 
440184e9811SJim Ingham       SetPlanComplete(false);
441641a67ceSJim Ingham 
44205097246SAdrian Prantl       // If the user has set the ObjC language breakpoint, it would normally
44305097246SAdrian Prantl       // get priority over our internal catcher breakpoint, but in this case we
44405097246SAdrian Prantl       // can't let that happen, so force the ShouldStop here.
445641a67ceSJim Ingham       stop_info_sp->OverrideShouldStop(true);
446c98aca60SSean Callanan       return true;
447184e9811SJim Ingham     }
4486fbc48bcSJim Ingham   }
449f211510fSSean Callanan 
450c98aca60SSean Callanan   return false;
451c98aca60SSean Callanan }
4528559a355SJim Ingham 
453b9c1b51eSKate Stone void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4540ff099f1SJim Ingham   m_subplan_sp->SetStopOthers(new_value);
4550ff099f1SJim Ingham }
4560ff099f1SJim Ingham 
45765d91b40SDave Lee void ThreadPlanCallFunction::RestoreThreadState() {
45865d91b40SDave Lee   GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
4598559a355SJim Ingham }
4608559a355SJim Ingham 
461b9c1b51eSKate Stone void ThreadPlanCallFunction::SetReturnValue() {
462e4598dc0SJim Ingham   const ABI *abi = m_process.GetABI().get();
463b9c1b51eSKate Stone   if (abi && m_return_type.IsValid()) {
46490ff7911SEwan Crawford     const bool persistent = false;
465b9c1b51eSKate Stone     m_return_valobj_sp =
466e4598dc0SJim Ingham         abi->GetReturnValueObject(GetThread(), m_return_type, persistent);
46790ff7911SEwan Crawford   }
46890ff7911SEwan Crawford }
469