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"
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) {
36*04cbfa95SQuinn Pham   SetIsControllingPlan(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);
6263e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
63324a1036SSaleem Abdulrasool               m_constructor_errors.GetData());
640092c8ebSJim Ingham     return false;
650092c8ebSJim Ingham   }
660092c8ebSJim Ingham 
670288c269SJonas Devlieghere   llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
680288c269SJonas Devlieghere   if (!start_address) {
690288c269SJonas Devlieghere     m_constructor_errors.Printf(
700288c269SJonas Devlieghere         "%s", llvm::toString(start_address.takeError()).c_str());
7163e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
72324a1036SSaleem Abdulrasool               m_constructor_errors.GetData());
730092c8ebSJim Ingham     return false;
740092c8ebSJim Ingham   }
75ea06f3bfSJim Ingham 
760288c269SJonas Devlieghere   m_start_addr = *start_address;
776fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
780092c8ebSJim Ingham 
790092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
800092c8ebSJim Ingham   if (log && log->GetVerbose())
81b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
82b9c1b51eSKate Stone                         "Original register state was:");
830092c8ebSJim Ingham 
84b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
85b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
86b9c1b51eSKate Stone                                 "checkpoint thread state.");
8763e5fb76SJonas Devlieghere     LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
88324a1036SSaleem Abdulrasool               m_constructor_errors.GetData());
890092c8ebSJim Ingham     return false;
900092c8ebSJim Ingham   }
916fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
920092c8ebSJim Ingham 
930092c8ebSJim Ingham   return true;
940092c8ebSJim Ingham }
9530fdc8d8SChris Lattner 
96b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
97b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
98b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
99b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
100b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
101b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1026fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1036fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1046fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
105b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
106b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1076fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1086fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
109b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
11090ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
11190ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
11290ff7911SEwan Crawford   ABI *abi = nullptr;
11390ff7911SEwan Crawford 
114923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1151ac04c30SGreg Clayton     return;
1161ac04c30SGreg Clayton 
117b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
118b9c1b51eSKate Stone                                start_load_addr, args))
11930fdc8d8SChris Lattner     return;
12030fdc8d8SChris Lattner 
1219da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1229da3683cSJim Ingham 
1239da3683cSJim Ingham   m_valid = true;
1249da3683cSJim Ingham }
1259da3683cSJim Ingham 
126b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
127b9c1b51eSKate Stone     Thread &thread, const Address &function,
128b9c1b51eSKate Stone     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()),
13290ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
13390ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
13490ff7911SEwan Crawford       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),
13790ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
13890ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
139b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
14090ff7911SEwan Crawford 
141b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1420161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1439da3683cSJim Ingham }
1449da3683cSJim Ingham 
145b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
1463b7e1981SPavel Labath   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1473b7e1981SPavel Labath   if (log && log->GetVerbose()) {
148af247d7bSGreg Clayton     StreamString strm;
149e4598dc0SJim Ingham     RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
150ece96492SSean Callanan 
1519da3683cSJim Ingham     log->PutCString(message);
152ece96492SSean Callanan 
153af247d7bSGreg Clayton     RegisterValue reg_value;
154ece96492SSean Callanan 
155af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
156b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
157af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
158b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
159e03334cfSPavel Labath         DumpRegisterValue(reg_value, &strm, reg_info, true, false,
160e03334cfSPavel Labath                           eFormatDefault);
161af247d7bSGreg Clayton         strm.EOL();
162ece96492SSean Callanan       }
163ece96492SSean Callanan     }
164c156427dSZachary Turner     log->PutString(strm.GetString());
165af247d7bSGreg Clayton   }
16610af7c43SSean Callanan }
16710af7c43SSean Callanan 
168b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
1695160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
17087d0e618SJim Ingham 
171b9c1b51eSKate Stone   if (!m_valid) {
17287d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
17363e5fb76SJonas Devlieghere     LLDB_LOGF(log,
17463e5fb76SJonas Devlieghere               "ThreadPlanCallFunction(%p): Log called on "
175b9c1b51eSKate Stone               "ThreadPlanCallFunction that was never valid.",
176324a1036SSaleem Abdulrasool               static_cast<void *>(this));
17787d0e618SJim Ingham     return;
17887d0e618SJim Ingham   }
17987d0e618SJim Ingham 
180b9c1b51eSKate Stone   if (!m_takedown_done) {
181e4598dc0SJim Ingham     Thread &thread = GetThread();
182b9c1b51eSKate Stone     if (success) {
18390ff7911SEwan Crawford       SetReturnValue();
18418de2fdcSJim Ingham     }
18563e5fb76SJonas Devlieghere     LLDB_LOGF(log,
18663e5fb76SJonas Devlieghere               "ThreadPlanCallFunction(%p): DoTakedown called for thread "
187b9c1b51eSKate Stone               "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
188e4598dc0SJim Ingham               static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
1899da3683cSJim Ingham     m_takedown_done = true;
190b9c1b51eSKate Stone     m_stop_address =
191e4598dc0SJim Ingham         thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
19260c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
193e4598dc0SJim Ingham     if (!thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
19463e5fb76SJonas Devlieghere       LLDB_LOGF(log,
19563e5fb76SJonas Devlieghere                 "ThreadPlanCallFunction(%p): DoTakedown failed to restore "
196b9c1b51eSKate Stone                 "register state",
197324a1036SSaleem Abdulrasool                 static_cast<void *>(this));
1987b24e95eSEd Maste     }
19918de2fdcSJim Ingham     SetPlanComplete(success);
20010af7c43SSean Callanan     ClearBreakpoints();
2019da3683cSJim Ingham     if (log && log->GetVerbose())
202b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
203b9c1b51eSKate Stone                           "Restored register state:");
204b9c1b51eSKate Stone   } else {
20563e5fb76SJonas Devlieghere     LLDB_LOGF(log,
20663e5fb76SJonas Devlieghere               "ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
207b9c1b51eSKate Stone               "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
208e4598dc0SJim Ingham               static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
20930fdc8d8SChris Lattner   }
21077787033SJim Ingham }
21130fdc8d8SChris Lattner 
21241d0b20cSDave Lee void ThreadPlanCallFunction::DidPop() { DoTakedown(PlanSucceeded()); }
213bda4e5ebSJim Ingham 
214b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
215b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
21630fdc8d8SChris Lattner     s->Printf("Function call thread plan");
217b9c1b51eSKate Stone   } else {
218b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
219e4598dc0SJim Ingham               m_function_addr.GetLoadAddress(&GetTarget()));
22030fdc8d8SChris Lattner   }
22130fdc8d8SChris Lattner }
22230fdc8d8SChris Lattner 
223b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
224b9c1b51eSKate Stone   if (!m_valid) {
225b9c1b51eSKate Stone     if (error) {
226ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
227c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
228ea06f3bfSJim Ingham       else
229ea06f3bfSJim Ingham         error->PutCString("Unknown error");
230ea06f3bfSJim Ingham     }
23130fdc8d8SChris Lattner     return false;
232ea06f3bfSJim Ingham   }
23330fdc8d8SChris Lattner 
23430fdc8d8SChris Lattner   return true;
23530fdc8d8SChris Lattner }
23630fdc8d8SChris Lattner 
237b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2380161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2390161b49cSJim Ingham     return eVoteYes;
2400161b49cSJim Ingham   else
2410161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2420161b49cSJim Ingham }
2430161b49cSJim Ingham 
244b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
245b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
246b9c1b51eSKate Stone                                                   LIBLLDB_LOG_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() {
3855160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_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))) {
436641a67ceSJim Ingham       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_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