130fdc8d8SChris Lattner //===-- ThreadPlanCallFunction.cpp ------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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 
2730fdc8d8SChris Lattner using namespace lldb;
2830fdc8d8SChris Lattner using namespace lldb_private;
2930fdc8d8SChris Lattner 
3030fdc8d8SChris Lattner //----------------------------------------------------------------------
3130fdc8d8SChris Lattner // ThreadPlanCallFunction: Plan to call a single function
3230fdc8d8SChris Lattner //----------------------------------------------------------------------
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 
686fbc48bcSJim Ingham   Module *exe_module = GetTarget().GetExecutableModulePointer();
690092c8ebSJim Ingham 
70b9c1b51eSKate Stone   if (exe_module == nullptr) {
71b9c1b51eSKate Stone     m_constructor_errors.Printf(
72b9c1b51eSKate Stone         "Can't execute code without an executable module.");
730092c8ebSJim Ingham     if (log)
74b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
75324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
760092c8ebSJim Ingham     return false;
77b9c1b51eSKate Stone   } else {
780092c8ebSJim Ingham     ObjectFile *objectFile = exe_module->GetObjectFile();
79b9c1b51eSKate Stone     if (!objectFile) {
80b9c1b51eSKate Stone       m_constructor_errors.Printf(
81b9c1b51eSKate Stone           "Could not find object file for module \"%s\".",
82ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
83ea06f3bfSJim Ingham 
840092c8ebSJim Ingham       if (log)
85324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
86b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
870092c8ebSJim Ingham       return false;
880092c8ebSJim Ingham     }
89ea06f3bfSJim Ingham 
900092c8ebSJim Ingham     m_start_addr = objectFile->GetEntryPointAddress();
91b9c1b51eSKate Stone     if (!m_start_addr.IsValid()) {
92b9c1b51eSKate Stone       m_constructor_errors.Printf(
93b9c1b51eSKate Stone           "Could not find entry point address for executable module \"%s\".",
94ea06f3bfSJim Ingham           exe_module->GetFileSpec().GetFilename().AsCString());
950092c8ebSJim Ingham       if (log)
96324a1036SSaleem Abdulrasool         log->Printf("ThreadPlanCallFunction(%p): %s.",
97b9c1b51eSKate Stone                     static_cast<void *>(this), m_constructor_errors.GetData());
980092c8ebSJim Ingham       return false;
990092c8ebSJim Ingham     }
1000092c8ebSJim Ingham   }
1010092c8ebSJim Ingham 
1026fbc48bcSJim Ingham   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
1030092c8ebSJim Ingham 
1040092c8ebSJim Ingham   // Checkpoint the thread state so we can restore it later.
1050092c8ebSJim Ingham   if (log && log->GetVerbose())
106b9c1b51eSKate Stone     ReportRegisterState("About to checkpoint thread before function call.  "
107b9c1b51eSKate Stone                         "Original register state was:");
1080092c8ebSJim Ingham 
109b9c1b51eSKate Stone   if (!thread.CheckpointThreadState(m_stored_thread_state)) {
110b9c1b51eSKate Stone     m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
111b9c1b51eSKate Stone                                 "checkpoint thread state.");
1120092c8ebSJim Ingham     if (log)
113b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
114324a1036SSaleem Abdulrasool                   m_constructor_errors.GetData());
1150092c8ebSJim Ingham     return false;
1160092c8ebSJim Ingham   }
1176fbc48bcSJim Ingham   function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
1180092c8ebSJim Ingham 
1190092c8ebSJim Ingham   return true;
1200092c8ebSJim Ingham }
12130fdc8d8SChris Lattner 
122b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
123b9c1b51eSKate Stone     Thread &thread, const Address &function, const CompilerType &return_type,
124b9c1b51eSKate Stone     llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
125b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
126b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
127b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1286fbc48bcSJim Ingham       m_unwind_on_error(options.DoesUnwindOnError()),
1296fbc48bcSJim Ingham       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1306fbc48bcSJim Ingham       m_debug_execution(options.GetDebug()),
131b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
132b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
1336fbc48bcSJim Ingham       m_should_clear_objc_exception_bp(false),
1346fbc48bcSJim Ingham       m_should_clear_cxx_exception_bp(false),
135b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
13690ff7911SEwan Crawford   lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
13790ff7911SEwan Crawford   lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
13890ff7911SEwan Crawford   ABI *abi = nullptr;
13990ff7911SEwan Crawford 
140923886ceSJim Ingham   if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1411ac04c30SGreg Clayton     return;
1421ac04c30SGreg Clayton 
143b9c1b51eSKate Stone   if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
144b9c1b51eSKate Stone                                start_load_addr, args))
14530fdc8d8SChris Lattner     return;
14630fdc8d8SChris Lattner 
1479da3683cSJim Ingham   ReportRegisterState("Function call was set up.  Register state was:");
1489da3683cSJim Ingham 
1499da3683cSJim Ingham   m_valid = true;
1509da3683cSJim Ingham }
1519da3683cSJim Ingham 
152b9c1b51eSKate Stone ThreadPlanCallFunction::ThreadPlanCallFunction(
153b9c1b51eSKate Stone     Thread &thread, const Address &function,
154b9c1b51eSKate Stone     const EvaluateExpressionOptions &options)
155b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
156b9c1b51eSKate Stone                  eVoteNoOpinion, eVoteNoOpinion),
157b9c1b51eSKate Stone       m_valid(false), m_stop_other_threads(options.GetStopOthers()),
15890ff7911SEwan Crawford       m_unwind_on_error(options.DoesUnwindOnError()),
15990ff7911SEwan Crawford       m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
16090ff7911SEwan Crawford       m_debug_execution(options.GetDebug()),
161b9c1b51eSKate Stone       m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
162b9c1b51eSKate Stone       m_function_sp(0), m_takedown_done(false),
16390ff7911SEwan Crawford       m_should_clear_objc_exception_bp(false),
16490ff7911SEwan Crawford       m_should_clear_cxx_exception_bp(false),
165b9c1b51eSKate Stone       m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
16690ff7911SEwan Crawford 
167b9c1b51eSKate Stone ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1680161b49cSJim Ingham   DoTakedown(PlanSucceeded());
1699da3683cSJim Ingham }
1709da3683cSJim Ingham 
171b9c1b51eSKate Stone void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
1723b7e1981SPavel Labath   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1733b7e1981SPavel Labath   if (log && log->GetVerbose()) {
174af247d7bSGreg Clayton     StreamString strm;
1755ccbd294SGreg Clayton     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
176ece96492SSean Callanan 
1779da3683cSJim Ingham     log->PutCString(message);
178ece96492SSean Callanan 
179af247d7bSGreg Clayton     RegisterValue reg_value;
180ece96492SSean Callanan 
181af247d7bSGreg Clayton     for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
182b9c1b51eSKate Stone          reg_idx < num_registers; ++reg_idx) {
183af247d7bSGreg Clayton       const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
184b9c1b51eSKate Stone       if (reg_ctx->ReadRegister(reg_info, reg_value)) {
185e03334cfSPavel Labath         DumpRegisterValue(reg_value, &strm, reg_info, true, false,
186e03334cfSPavel Labath                           eFormatDefault);
187af247d7bSGreg Clayton         strm.EOL();
188ece96492SSean Callanan       }
189ece96492SSean Callanan     }
190c156427dSZachary Turner     log->PutString(strm.GetString());
191af247d7bSGreg Clayton   }
19210af7c43SSean Callanan }
19310af7c43SSean Callanan 
194b9c1b51eSKate Stone void ThreadPlanCallFunction::DoTakedown(bool success) {
1955160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
19687d0e618SJim Ingham 
197b9c1b51eSKate Stone   if (!m_valid) {
19887d0e618SJim Ingham     // Don't call DoTakedown if we were never valid to begin with.
19987d0e618SJim Ingham     if (log)
200b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): Log called on "
201b9c1b51eSKate Stone                   "ThreadPlanCallFunction that was never valid.",
202324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
20387d0e618SJim Ingham     return;
20487d0e618SJim Ingham   }
20587d0e618SJim Ingham 
206b9c1b51eSKate Stone   if (!m_takedown_done) {
207b9c1b51eSKate Stone     if (success) {
20890ff7911SEwan Crawford       SetReturnValue();
20918de2fdcSJim Ingham     }
2109da3683cSJim Ingham     if (log)
211b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called for thread "
212b9c1b51eSKate Stone                   "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
213324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
214324a1036SSaleem Abdulrasool                   IsPlanComplete());
2159da3683cSJim Ingham     m_takedown_done = true;
216b9c1b51eSKate Stone     m_stop_address =
217b9c1b51eSKate Stone         m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
21860c4118cSJim Ingham     m_real_stop_info_sp = GetPrivateStopInfo();
219b9c1b51eSKate Stone     if (!m_thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
2207b24e95eSEd Maste       if (log)
221b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction(%p): DoTakedown failed to restore "
222b9c1b51eSKate Stone                     "register state",
223324a1036SSaleem Abdulrasool                     static_cast<void *>(this));
2247b24e95eSEd Maste     }
22518de2fdcSJim Ingham     SetPlanComplete(success);
22610af7c43SSean Callanan     ClearBreakpoints();
2279da3683cSJim Ingham     if (log && log->GetVerbose())
228b9c1b51eSKate Stone       ReportRegisterState("Restoring thread state after function call.  "
229b9c1b51eSKate Stone                           "Restored register state:");
230b9c1b51eSKate Stone   } else {
2319da3683cSJim Ingham     if (log)
232b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
233b9c1b51eSKate Stone                   "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
234324a1036SSaleem Abdulrasool                   static_cast<void *>(this), m_thread.GetID(), m_valid,
235324a1036SSaleem Abdulrasool                   IsPlanComplete());
23630fdc8d8SChris Lattner   }
23777787033SJim Ingham }
23830fdc8d8SChris Lattner 
239b9c1b51eSKate Stone void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
240bda4e5ebSJim Ingham 
241b9c1b51eSKate Stone void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
242b9c1b51eSKate Stone   if (level == eDescriptionLevelBrief) {
24330fdc8d8SChris Lattner     s->Printf("Function call thread plan");
244b9c1b51eSKate Stone   } else {
2451ac04c30SGreg Clayton     TargetSP target_sp(m_thread.CalculateTarget());
246b9c1b51eSKate Stone     s->Printf("Thread plan to call 0x%" PRIx64,
247b9c1b51eSKate Stone               m_function_addr.GetLoadAddress(target_sp.get()));
24830fdc8d8SChris Lattner   }
24930fdc8d8SChris Lattner }
25030fdc8d8SChris Lattner 
251b9c1b51eSKate Stone bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
252b9c1b51eSKate Stone   if (!m_valid) {
253b9c1b51eSKate Stone     if (error) {
254ea06f3bfSJim Ingham       if (m_constructor_errors.GetSize() > 0)
255c156427dSZachary Turner         error->PutCString(m_constructor_errors.GetString());
256ea06f3bfSJim Ingham       else
257ea06f3bfSJim Ingham         error->PutCString("Unknown error");
258ea06f3bfSJim Ingham     }
25930fdc8d8SChris Lattner     return false;
260ea06f3bfSJim Ingham   }
26130fdc8d8SChris Lattner 
26230fdc8d8SChris Lattner   return true;
26330fdc8d8SChris Lattner }
26430fdc8d8SChris Lattner 
265b9c1b51eSKate Stone Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2660161b49cSJim Ingham   if (m_takedown_done || IsPlanComplete())
2670161b49cSJim Ingham     return eVoteYes;
2680161b49cSJim Ingham   else
2690161b49cSJim Ingham     return ThreadPlan::ShouldReportStop(event_ptr);
2700161b49cSJim Ingham }
2710161b49cSJim Ingham 
272b9c1b51eSKate Stone bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
273b9c1b51eSKate Stone   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
274b9c1b51eSKate Stone                                                   LIBLLDB_LOG_PROCESS));
27560c4118cSJim Ingham   m_real_stop_info_sp = GetPrivateStopInfo();
276160f78c5SJim Ingham 
27705097246SAdrian Prantl   // If our subplan knows why we stopped, even if it's done (which would
27805097246SAdrian Prantl   // forward the question to us) we answer yes.
279b9c1b51eSKate Stone   if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
280184e9811SJim Ingham     SetPlanComplete();
28140d871faSJim Ingham     return true;
282184e9811SJim Ingham   }
2833e6fedcaSSean Callanan 
284c98aca60SSean Callanan   // Check if the breakpoint is one of ours.
285c98aca60SSean Callanan 
28618de2fdcSJim Ingham   StopReason stop_reason;
28718de2fdcSJim Ingham   if (!m_real_stop_info_sp)
28818de2fdcSJim Ingham     stop_reason = eStopReasonNone;
28918de2fdcSJim Ingham   else
29018de2fdcSJim Ingham     stop_reason = m_real_stop_info_sp->GetStopReason();
2910161b49cSJim Ingham   if (log)
292b9c1b51eSKate Stone     log->Printf(
293b9c1b51eSKate Stone         "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - %s.",
294b9c1b51eSKate Stone         Thread::StopReasonAsCString(stop_reason));
29518de2fdcSJim Ingham 
29618de2fdcSJim Ingham   if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
297c98aca60SSean Callanan     return true;
298c98aca60SSean Callanan 
299b9c1b51eSKate Stone   // One more quirk here.  If this event was from Halt interrupting the target,
30005097246SAdrian Prantl   // then we should not consider ourselves complete.  Return true to
30105097246SAdrian Prantl   // acknowledge the stop.
302b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
30335878c47SJim Ingham     if (log)
304b9c1b51eSKate Stone       log->Printf("ThreadPlanCallFunction::PlanExplainsStop: The event is an "
305b9c1b51eSKate Stone                   "Interrupt, returning true.");
30635878c47SJim Ingham     return true;
30735878c47SJim Ingham   }
3080161b49cSJim Ingham   // We control breakpoints separately from other "stop reasons."  So first,
309b9c1b51eSKate Stone   // check the case where we stopped for an internal breakpoint, in that case,
31005097246SAdrian Prantl   // continue on. If it is not an internal breakpoint, consult
31105097246SAdrian Prantl   // m_ignore_breakpoints.
3126db73ca5SSean Callanan 
313b9c1b51eSKate Stone   if (stop_reason == eStopReasonBreakpoint) {
3141ac04c30SGreg Clayton     ProcessSP process_sp(m_thread.CalculateProcess());
315160f78c5SJim Ingham     uint64_t break_site_id = m_real_stop_info_sp->GetValue();
3161ac04c30SGreg Clayton     BreakpointSiteSP bp_site_sp;
3171ac04c30SGreg Clayton     if (process_sp)
3181ac04c30SGreg Clayton       bp_site_sp = process_sp->GetBreakpointSiteList().FindByID(break_site_id);
319b9c1b51eSKate Stone     if (bp_site_sp) {
32040d871faSJim Ingham       uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
32140d871faSJim Ingham       bool is_internal = true;
322b9c1b51eSKate Stone       for (uint32_t i = 0; i < num_owners; i++) {
3236db73ca5SSean Callanan         Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
3240161b49cSJim Ingham         if (log)
325b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop: hit "
326b9c1b51eSKate Stone                       "breakpoint %d while calling function",
327b9c1b51eSKate Stone                       bp.GetID());
3286db73ca5SSean Callanan 
329b9c1b51eSKate Stone         if (!bp.IsInternal()) {
33040d871faSJim Ingham           is_internal = false;
33140d871faSJim Ingham           break;
33240d871faSJim Ingham         }
33340d871faSJim Ingham       }
334b9c1b51eSKate Stone       if (is_internal) {
3350161b49cSJim Ingham         if (log)
336b9c1b51eSKate Stone           log->Printf("ThreadPlanCallFunction::PlanExplainsStop hit an "
337b9c1b51eSKate Stone                       "internal breakpoint, not stopping.");
33840d871faSJim Ingham         return false;
33940d871faSJim Ingham       }
3400161b49cSJim Ingham     }
34140d871faSJim Ingham 
342b9c1b51eSKate Stone     if (m_ignore_breakpoints) {
3430161b49cSJim Ingham       if (log)
344b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
345b9c1b51eSKate Stone                     "breakpoints, overriding breakpoint stop info ShouldStop, "
346b9c1b51eSKate Stone                     "returning true");
3470161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(false);
348923886ceSJim Ingham       return true;
349b9c1b51eSKate Stone     } else {
3500161b49cSJim Ingham       if (log)
351b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::PlanExplainsStop: we are not "
352b9c1b51eSKate Stone                     "ignoring breakpoints, overriding breakpoint stop info "
353b9c1b51eSKate Stone                     "ShouldStop, returning true");
3540161b49cSJim Ingham       m_real_stop_info_sp->OverrideShouldStop(true);
3550161b49cSJim Ingham       return false;
3560161b49cSJim Ingham     }
357b9c1b51eSKate Stone   } else if (!m_unwind_on_error) {
358b9c1b51eSKate Stone     // If we don't want to discard this plan, than any stop we don't understand
359b9c1b51eSKate Stone     // should be propagated up the stack.
360923886ceSJim Ingham     return false;
361b9c1b51eSKate Stone   } else {
36205097246SAdrian Prantl     // If the subplan is running, any crashes are attributable to us. If we
36305097246SAdrian Prantl     // want to discard the plan, then we say we explain the stop but if we are
36405097246SAdrian Prantl     // going to be discarded, let whoever is above us explain the stop. But
36505097246SAdrian Prantl     // don't discard the plan if the stop would restart itself (for instance if
36605097246SAdrian Prantl     // it is a signal that is set not to stop.  Check that here first.  We just
36705097246SAdrian Prantl     // say we explain the stop but aren't done and everything will continue on
36805097246SAdrian Prantl     // from there.
3690161b49cSJim Ingham 
370b9c1b51eSKate Stone     if (m_real_stop_info_sp &&
371b9c1b51eSKate Stone         m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
372184e9811SJim Ingham       SetPlanComplete(false);
373e65b2cf2SEugene Zelenko       return m_subplan_sp ? m_unwind_on_error : false;
374b9c1b51eSKate Stone     } else
3750161b49cSJim Ingham       return true;
3760161b49cSJim Ingham   }
37740d871faSJim Ingham }
37830fdc8d8SChris Lattner 
379b9c1b51eSKate Stone bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
380b9c1b51eSKate Stone   // We do some computation in DoPlanExplainsStop that may or may not set the
38105097246SAdrian Prantl   // plan as complete. We need to do that here to make sure our state is
38205097246SAdrian Prantl   // correct.
383221d51cfSJim Ingham   DoPlanExplainsStop(event_ptr);
384184e9811SJim Ingham 
385b9c1b51eSKate Stone   if (IsPlanComplete()) {
3869da3683cSJim Ingham     ReportRegisterState("Function completed.  Register state was:");
38730fdc8d8SChris Lattner     return true;
388b9c1b51eSKate Stone   } else {
38930fdc8d8SChris Lattner     return false;
39030fdc8d8SChris Lattner   }
39130fdc8d8SChris Lattner }
39230fdc8d8SChris Lattner 
393b9c1b51eSKate Stone bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
39430fdc8d8SChris Lattner 
395b9c1b51eSKate Stone StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
39630fdc8d8SChris Lattner 
397b9c1b51eSKate Stone void ThreadPlanCallFunction::DidPush() {
398be3a1b14SSean Callanan   //#define SINGLE_STEP_EXPRESSIONS
399be3a1b14SSean Callanan 
400b9c1b51eSKate Stone   // Now set the thread state to "no reason" so we don't run with whatever
40105097246SAdrian Prantl   // signal was outstanding... Wait till the plan is pushed so we aren't
40205097246SAdrian Prantl   // changing the stop info till we're about to run.
4038559a355SJim Ingham 
4048559a355SJim Ingham   GetThread().SetStopInfoToNothing();
4058559a355SJim Ingham 
406be3a1b14SSean Callanan #ifndef SINGLE_STEP_EXPRESSIONS
407b9c1b51eSKate Stone   m_subplan_sp.reset(
408b9c1b51eSKate Stone       new ThreadPlanRunToAddress(m_thread, m_start_addr, m_stop_other_threads));
40930fdc8d8SChris Lattner 
41030fdc8d8SChris Lattner   m_thread.QueueThreadPlan(m_subplan_sp, false);
41177787033SJim Ingham   m_subplan_sp->SetPrivate(true);
412be3a1b14SSean Callanan #endif
41330fdc8d8SChris Lattner }
41430fdc8d8SChris Lattner 
415b9c1b51eSKate Stone bool ThreadPlanCallFunction::WillStop() { return true; }
41630fdc8d8SChris Lattner 
417b9c1b51eSKate Stone bool ThreadPlanCallFunction::MischiefManaged() {
4185160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
41930fdc8d8SChris Lattner 
420b9c1b51eSKate Stone   if (IsPlanComplete()) {
42130fdc8d8SChris Lattner     if (log)
422324a1036SSaleem Abdulrasool       log->Printf("ThreadPlanCallFunction(%p): Completed call function plan.",
423324a1036SSaleem Abdulrasool                   static_cast<void *>(this));
42430fdc8d8SChris Lattner 
42530fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
42630fdc8d8SChris Lattner     return true;
427b9c1b51eSKate Stone   } else {
42830fdc8d8SChris Lattner     return false;
42930fdc8d8SChris Lattner   }
43030fdc8d8SChris Lattner }
4316db73ca5SSean Callanan 
432b9c1b51eSKate Stone void ThreadPlanCallFunction::SetBreakpoints() {
4331ac04c30SGreg Clayton   ProcessSP process_sp(m_thread.CalculateProcess());
434b9c1b51eSKate Stone   if (m_trap_exceptions && process_sp) {
435b9c1b51eSKate Stone     m_cxx_language_runtime =
436b9c1b51eSKate Stone         process_sp->GetLanguageRuntime(eLanguageTypeC_plus_plus);
4371ac04c30SGreg Clayton     m_objc_language_runtime = process_sp->GetLanguageRuntime(eLanguageTypeObjC);
4386db73ca5SSean Callanan 
439b9c1b51eSKate Stone     if (m_cxx_language_runtime) {
440b9c1b51eSKate Stone       m_should_clear_cxx_exception_bp =
441b9c1b51eSKate Stone           !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
442f211510fSSean Callanan       m_cxx_language_runtime->SetExceptionBreakpoints();
4436fbc48bcSJim Ingham     }
444b9c1b51eSKate Stone     if (m_objc_language_runtime) {
445b9c1b51eSKate Stone       m_should_clear_objc_exception_bp =
446b9c1b51eSKate Stone           !m_objc_language_runtime->ExceptionBreakpointsAreSet();
447f211510fSSean Callanan       m_objc_language_runtime->SetExceptionBreakpoints();
4486db73ca5SSean Callanan     }
4491ac04c30SGreg Clayton   }
4506fbc48bcSJim Ingham }
4516db73ca5SSean Callanan 
452b9c1b51eSKate Stone void ThreadPlanCallFunction::ClearBreakpoints() {
453b9c1b51eSKate Stone   if (m_trap_exceptions) {
4546fbc48bcSJim Ingham     if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
455f211510fSSean Callanan       m_cxx_language_runtime->ClearExceptionBreakpoints();
4566fbc48bcSJim Ingham     if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
457f211510fSSean Callanan       m_objc_language_runtime->ClearExceptionBreakpoints();
4586db73ca5SSean Callanan   }
4596fbc48bcSJim Ingham }
460c98aca60SSean Callanan 
461b9c1b51eSKate Stone bool ThreadPlanCallFunction::BreakpointsExplainStop() {
46260c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
463c98aca60SSean Callanan 
464b9c1b51eSKate Stone   if (m_trap_exceptions) {
465184e9811SJim Ingham     if ((m_cxx_language_runtime &&
466b9c1b51eSKate Stone          m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
467b9c1b51eSKate Stone              stop_info_sp)) ||
468b9c1b51eSKate Stone         (m_objc_language_runtime &&
469b9c1b51eSKate Stone          m_objc_language_runtime->ExceptionBreakpointsExplainStop(
470b9c1b51eSKate Stone              stop_info_sp))) {
471641a67ceSJim Ingham       Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
472641a67ceSJim Ingham       if (log)
473b9c1b51eSKate Stone         log->Printf("ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
474b9c1b51eSKate Stone                     "exception breakpoint, setting plan complete.");
475641a67ceSJim Ingham 
476184e9811SJim Ingham       SetPlanComplete(false);
477641a67ceSJim Ingham 
47805097246SAdrian Prantl       // If the user has set the ObjC language breakpoint, it would normally
47905097246SAdrian Prantl       // get priority over our internal catcher breakpoint, but in this case we
48005097246SAdrian Prantl       // can't let that happen, so force the ShouldStop here.
481641a67ceSJim Ingham       stop_info_sp->OverrideShouldStop(true);
482c98aca60SSean Callanan       return true;
483184e9811SJim Ingham     }
4846fbc48bcSJim Ingham   }
485f211510fSSean Callanan 
486c98aca60SSean Callanan   return false;
487c98aca60SSean Callanan }
4888559a355SJim Ingham 
489b9c1b51eSKate Stone void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4900ff099f1SJim Ingham   m_subplan_sp->SetStopOthers(new_value);
4910ff099f1SJim Ingham }
4920ff099f1SJim Ingham 
493b9c1b51eSKate Stone bool ThreadPlanCallFunction::RestoreThreadState() {
4948559a355SJim Ingham   return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
4958559a355SJim Ingham }
4968559a355SJim Ingham 
497b9c1b51eSKate Stone void ThreadPlanCallFunction::SetReturnValue() {
49890ff7911SEwan Crawford   ProcessSP process_sp(m_thread.GetProcess());
499e65b2cf2SEugene Zelenko   const ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
500b9c1b51eSKate Stone   if (abi && m_return_type.IsValid()) {
50190ff7911SEwan Crawford     const bool persistent = false;
502b9c1b51eSKate Stone     m_return_valobj_sp =
503b9c1b51eSKate Stone         abi->GetReturnValueObject(m_thread, m_return_type, persistent);
50490ff7911SEwan Crawford   }
50590ff7911SEwan Crawford }
506