15ffd83dbSDimitry Andric //===-- ThreadPlanCallFunction.cpp ----------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanCallFunction.h"
100b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
110b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
120b57cec5SDimitry Andric #include "lldb/Core/Address.h"
130b57cec5SDimitry Andric #include "lldb/Core/DumpRegisterValue.h"
140b57cec5SDimitry Andric #include "lldb/Core/Module.h"
150b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
160b57cec5SDimitry Andric #include "lldb/Target/ABI.h"
170b57cec5SDimitry Andric #include "lldb/Target/LanguageRuntime.h"
180b57cec5SDimitry Andric #include "lldb/Target/Process.h"
190b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
200b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
210b57cec5SDimitry Andric #include "lldb/Target/Target.h"
220b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
230b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanRunToAddress.h"
240b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
250b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric #include <memory>
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric using namespace lldb;
300b57cec5SDimitry Andric using namespace lldb_private;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric // ThreadPlanCallFunction: Plan to call a single function
ConstructorSetup(Thread & thread,ABI * & abi,lldb::addr_t & start_load_addr,lldb::addr_t & function_load_addr)330b57cec5SDimitry Andric bool ThreadPlanCallFunction::ConstructorSetup(
340b57cec5SDimitry Andric Thread &thread, ABI *&abi, lldb::addr_t &start_load_addr,
350b57cec5SDimitry Andric lldb::addr_t &function_load_addr) {
360b57cec5SDimitry Andric SetIsMasterPlan(true);
370b57cec5SDimitry Andric SetOkayToDiscard(false);
380b57cec5SDimitry Andric SetPrivate(true);
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric ProcessSP process_sp(thread.GetProcess());
410b57cec5SDimitry Andric if (!process_sp)
420b57cec5SDimitry Andric return false;
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric abi = process_sp->GetABI().get();
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric if (!abi)
470b57cec5SDimitry Andric return false;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric SetBreakpoints();
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric m_function_sp = thread.GetRegisterContext()->GetSP() - abi->GetRedZoneSize();
540b57cec5SDimitry Andric // If we can't read memory at the point of the process where we are planning
550b57cec5SDimitry Andric // to put our function, we're not going to get any further...
560b57cec5SDimitry Andric Status error;
570b57cec5SDimitry Andric process_sp->ReadUnsignedIntegerFromMemory(m_function_sp, 4, 0, error);
580b57cec5SDimitry Andric if (!error.Success()) {
590b57cec5SDimitry Andric m_constructor_errors.Printf(
600b57cec5SDimitry Andric "Trying to put the stack in unreadable memory at: 0x%" PRIx64 ".",
610b57cec5SDimitry Andric m_function_sp);
629dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
630b57cec5SDimitry Andric m_constructor_errors.GetData());
640b57cec5SDimitry Andric return false;
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric
679dba64beSDimitry Andric llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
689dba64beSDimitry Andric if (!start_address) {
690b57cec5SDimitry Andric m_constructor_errors.Printf(
709dba64beSDimitry Andric "%s", llvm::toString(start_address.takeError()).c_str());
719dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
720b57cec5SDimitry Andric m_constructor_errors.GetData());
730b57cec5SDimitry Andric return false;
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric
769dba64beSDimitry Andric m_start_addr = *start_address;
770b57cec5SDimitry Andric start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric // Checkpoint the thread state so we can restore it later.
800b57cec5SDimitry Andric if (log && log->GetVerbose())
810b57cec5SDimitry Andric ReportRegisterState("About to checkpoint thread before function call. "
820b57cec5SDimitry Andric "Original register state was:");
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric if (!thread.CheckpointThreadState(m_stored_thread_state)) {
850b57cec5SDimitry Andric m_constructor_errors.Printf("Setting up ThreadPlanCallFunction, failed to "
860b57cec5SDimitry Andric "checkpoint thread state.");
879dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
880b57cec5SDimitry Andric m_constructor_errors.GetData());
890b57cec5SDimitry Andric return false;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric function_load_addr = m_function_addr.GetLoadAddress(&GetTarget());
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric return true;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
ThreadPlanCallFunction(Thread & thread,const Address & function,const CompilerType & return_type,llvm::ArrayRef<addr_t> args,const EvaluateExpressionOptions & options)960b57cec5SDimitry Andric ThreadPlanCallFunction::ThreadPlanCallFunction(
970b57cec5SDimitry Andric Thread &thread, const Address &function, const CompilerType &return_type,
980b57cec5SDimitry Andric llvm::ArrayRef<addr_t> args, const EvaluateExpressionOptions &options)
990b57cec5SDimitry Andric : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
1000b57cec5SDimitry Andric eVoteNoOpinion, eVoteNoOpinion),
1010b57cec5SDimitry Andric m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1020b57cec5SDimitry Andric m_unwind_on_error(options.DoesUnwindOnError()),
1030b57cec5SDimitry Andric m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1040b57cec5SDimitry Andric m_debug_execution(options.GetDebug()),
1050b57cec5SDimitry Andric m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
1060b57cec5SDimitry Andric m_function_sp(0), m_takedown_done(false),
1070b57cec5SDimitry Andric m_should_clear_objc_exception_bp(false),
1080b57cec5SDimitry Andric m_should_clear_cxx_exception_bp(false),
1090b57cec5SDimitry Andric m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
1100b57cec5SDimitry Andric lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
1110b57cec5SDimitry Andric lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
1120b57cec5SDimitry Andric ABI *abi = nullptr;
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr))
1150b57cec5SDimitry Andric return;
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric if (!abi->PrepareTrivialCall(thread, m_function_sp, function_load_addr,
1180b57cec5SDimitry Andric start_load_addr, args))
1190b57cec5SDimitry Andric return;
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric ReportRegisterState("Function call was set up. Register state was:");
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric m_valid = true;
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric
ThreadPlanCallFunction(Thread & thread,const Address & function,const EvaluateExpressionOptions & options)1260b57cec5SDimitry Andric ThreadPlanCallFunction::ThreadPlanCallFunction(
1270b57cec5SDimitry Andric Thread &thread, const Address &function,
1280b57cec5SDimitry Andric const EvaluateExpressionOptions &options)
1290b57cec5SDimitry Andric : ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread,
1300b57cec5SDimitry Andric eVoteNoOpinion, eVoteNoOpinion),
1310b57cec5SDimitry Andric m_valid(false), m_stop_other_threads(options.GetStopOthers()),
1320b57cec5SDimitry Andric m_unwind_on_error(options.DoesUnwindOnError()),
1330b57cec5SDimitry Andric m_ignore_breakpoints(options.DoesIgnoreBreakpoints()),
1340b57cec5SDimitry Andric m_debug_execution(options.GetDebug()),
1350b57cec5SDimitry Andric m_trap_exceptions(options.GetTrapExceptions()), m_function_addr(function),
1360b57cec5SDimitry Andric m_function_sp(0), m_takedown_done(false),
1370b57cec5SDimitry Andric m_should_clear_objc_exception_bp(false),
1380b57cec5SDimitry Andric m_should_clear_cxx_exception_bp(false),
1390b57cec5SDimitry Andric m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(CompilerType()) {}
1400b57cec5SDimitry Andric
~ThreadPlanCallFunction()1410b57cec5SDimitry Andric ThreadPlanCallFunction::~ThreadPlanCallFunction() {
1420b57cec5SDimitry Andric DoTakedown(PlanSucceeded());
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric
ReportRegisterState(const char * message)1450b57cec5SDimitry Andric void ThreadPlanCallFunction::ReportRegisterState(const char *message) {
1460b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1470b57cec5SDimitry Andric if (log && log->GetVerbose()) {
1480b57cec5SDimitry Andric StreamString strm;
1495ffd83dbSDimitry Andric RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric log->PutCString(message);
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric RegisterValue reg_value;
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric for (uint32_t reg_idx = 0, num_registers = reg_ctx->GetRegisterCount();
1560b57cec5SDimitry Andric reg_idx < num_registers; ++reg_idx) {
1570b57cec5SDimitry Andric const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
1580b57cec5SDimitry Andric if (reg_ctx->ReadRegister(reg_info, reg_value)) {
1590b57cec5SDimitry Andric DumpRegisterValue(reg_value, &strm, reg_info, true, false,
1600b57cec5SDimitry Andric eFormatDefault);
1610b57cec5SDimitry Andric strm.EOL();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric log->PutString(strm.GetString());
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
DoTakedown(bool success)1680b57cec5SDimitry Andric void ThreadPlanCallFunction::DoTakedown(bool success) {
1690b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric if (!m_valid) {
1720b57cec5SDimitry Andric // Don't call DoTakedown if we were never valid to begin with.
1739dba64beSDimitry Andric LLDB_LOGF(log,
1749dba64beSDimitry Andric "ThreadPlanCallFunction(%p): Log called on "
1750b57cec5SDimitry Andric "ThreadPlanCallFunction that was never valid.",
1760b57cec5SDimitry Andric static_cast<void *>(this));
1770b57cec5SDimitry Andric return;
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric if (!m_takedown_done) {
1815ffd83dbSDimitry Andric Thread &thread = GetThread();
1820b57cec5SDimitry Andric if (success) {
1830b57cec5SDimitry Andric SetReturnValue();
1840b57cec5SDimitry Andric }
1859dba64beSDimitry Andric LLDB_LOGF(log,
1869dba64beSDimitry Andric "ThreadPlanCallFunction(%p): DoTakedown called for thread "
1870b57cec5SDimitry Andric "0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
1885ffd83dbSDimitry Andric static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
1890b57cec5SDimitry Andric m_takedown_done = true;
1900b57cec5SDimitry Andric m_stop_address =
1915ffd83dbSDimitry Andric thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
1920b57cec5SDimitry Andric m_real_stop_info_sp = GetPrivateStopInfo();
1935ffd83dbSDimitry Andric if (!thread.RestoreRegisterStateFromCheckpoint(m_stored_thread_state)) {
1949dba64beSDimitry Andric LLDB_LOGF(log,
1959dba64beSDimitry Andric "ThreadPlanCallFunction(%p): DoTakedown failed to restore "
1960b57cec5SDimitry Andric "register state",
1970b57cec5SDimitry Andric static_cast<void *>(this));
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric SetPlanComplete(success);
2000b57cec5SDimitry Andric ClearBreakpoints();
2010b57cec5SDimitry Andric if (log && log->GetVerbose())
2020b57cec5SDimitry Andric ReportRegisterState("Restoring thread state after function call. "
2030b57cec5SDimitry Andric "Restored register state:");
2040b57cec5SDimitry Andric } else {
2059dba64beSDimitry Andric LLDB_LOGF(log,
2069dba64beSDimitry Andric "ThreadPlanCallFunction(%p): DoTakedown called as no-op for "
2070b57cec5SDimitry Andric "thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n",
2085ffd83dbSDimitry Andric static_cast<void *>(this), m_tid, m_valid, IsPlanComplete());
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric
WillPop()2120b57cec5SDimitry Andric void ThreadPlanCallFunction::WillPop() { DoTakedown(PlanSucceeded()); }
2130b57cec5SDimitry Andric
GetDescription(Stream * s,DescriptionLevel level)2140b57cec5SDimitry Andric void ThreadPlanCallFunction::GetDescription(Stream *s, DescriptionLevel level) {
2150b57cec5SDimitry Andric if (level == eDescriptionLevelBrief) {
2160b57cec5SDimitry Andric s->Printf("Function call thread plan");
2170b57cec5SDimitry Andric } else {
2180b57cec5SDimitry Andric s->Printf("Thread plan to call 0x%" PRIx64,
2195ffd83dbSDimitry Andric m_function_addr.GetLoadAddress(&GetTarget()));
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric
ValidatePlan(Stream * error)2230b57cec5SDimitry Andric bool ThreadPlanCallFunction::ValidatePlan(Stream *error) {
2240b57cec5SDimitry Andric if (!m_valid) {
2250b57cec5SDimitry Andric if (error) {
2260b57cec5SDimitry Andric if (m_constructor_errors.GetSize() > 0)
2270b57cec5SDimitry Andric error->PutCString(m_constructor_errors.GetString());
2280b57cec5SDimitry Andric else
2290b57cec5SDimitry Andric error->PutCString("Unknown error");
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric return false;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric return true;
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
ShouldReportStop(Event * event_ptr)2370b57cec5SDimitry Andric Vote ThreadPlanCallFunction::ShouldReportStop(Event *event_ptr) {
2380b57cec5SDimitry Andric if (m_takedown_done || IsPlanComplete())
2390b57cec5SDimitry Andric return eVoteYes;
2400b57cec5SDimitry Andric else
2410b57cec5SDimitry Andric return ThreadPlan::ShouldReportStop(event_ptr);
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric
DoPlanExplainsStop(Event * event_ptr)2440b57cec5SDimitry Andric bool ThreadPlanCallFunction::DoPlanExplainsStop(Event *event_ptr) {
2450b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP |
2460b57cec5SDimitry Andric LIBLLDB_LOG_PROCESS));
2470b57cec5SDimitry Andric m_real_stop_info_sp = GetPrivateStopInfo();
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric // If our subplan knows why we stopped, even if it's done (which would
2500b57cec5SDimitry Andric // forward the question to us) we answer yes.
2510b57cec5SDimitry Andric if (m_subplan_sp && m_subplan_sp->PlanExplainsStop(event_ptr)) {
2520b57cec5SDimitry Andric SetPlanComplete();
2530b57cec5SDimitry Andric return true;
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric // Check if the breakpoint is one of ours.
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric StopReason stop_reason;
2590b57cec5SDimitry Andric if (!m_real_stop_info_sp)
2600b57cec5SDimitry Andric stop_reason = eStopReasonNone;
2610b57cec5SDimitry Andric else
2620b57cec5SDimitry Andric stop_reason = m_real_stop_info_sp->GetStopReason();
263af732203SDimitry Andric LLDB_LOG(log,
264af732203SDimitry Andric "ThreadPlanCallFunction::PlanExplainsStop: Got stop reason - {0}.",
265af732203SDimitry Andric Thread::StopReasonAsString(stop_reason));
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric if (stop_reason == eStopReasonBreakpoint && BreakpointsExplainStop())
2680b57cec5SDimitry Andric return true;
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric // One more quirk here. If this event was from Halt interrupting the target,
2710b57cec5SDimitry Andric // then we should not consider ourselves complete. Return true to
2720b57cec5SDimitry Andric // acknowledge the stop.
2730b57cec5SDimitry Andric if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
2749dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop: The event is an "
2750b57cec5SDimitry Andric "Interrupt, returning true.");
2760b57cec5SDimitry Andric return true;
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric // We control breakpoints separately from other "stop reasons." So first,
2790b57cec5SDimitry Andric // check the case where we stopped for an internal breakpoint, in that case,
2800b57cec5SDimitry Andric // continue on. If it is not an internal breakpoint, consult
2810b57cec5SDimitry Andric // m_ignore_breakpoints.
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric if (stop_reason == eStopReasonBreakpoint) {
2840b57cec5SDimitry Andric uint64_t break_site_id = m_real_stop_info_sp->GetValue();
2850b57cec5SDimitry Andric BreakpointSiteSP bp_site_sp;
2865ffd83dbSDimitry Andric bp_site_sp = m_process.GetBreakpointSiteList().FindByID(break_site_id);
2870b57cec5SDimitry Andric if (bp_site_sp) {
2880b57cec5SDimitry Andric uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
2890b57cec5SDimitry Andric bool is_internal = true;
2900b57cec5SDimitry Andric for (uint32_t i = 0; i < num_owners; i++) {
2910b57cec5SDimitry Andric Breakpoint &bp = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint();
2929dba64beSDimitry Andric LLDB_LOGF(log,
2939dba64beSDimitry Andric "ThreadPlanCallFunction::PlanExplainsStop: hit "
2940b57cec5SDimitry Andric "breakpoint %d while calling function",
2950b57cec5SDimitry Andric bp.GetID());
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andric if (!bp.IsInternal()) {
2980b57cec5SDimitry Andric is_internal = false;
2990b57cec5SDimitry Andric break;
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric if (is_internal) {
3039dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop hit an "
3040b57cec5SDimitry Andric "internal breakpoint, not stopping.");
3050b57cec5SDimitry Andric return false;
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andric if (m_ignore_breakpoints) {
3109dba64beSDimitry Andric LLDB_LOGF(log,
3119dba64beSDimitry Andric "ThreadPlanCallFunction::PlanExplainsStop: we are ignoring "
3120b57cec5SDimitry Andric "breakpoints, overriding breakpoint stop info ShouldStop, "
3130b57cec5SDimitry Andric "returning true");
3140b57cec5SDimitry Andric m_real_stop_info_sp->OverrideShouldStop(false);
3150b57cec5SDimitry Andric return true;
3160b57cec5SDimitry Andric } else {
3179dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction::PlanExplainsStop: we are not "
3180b57cec5SDimitry Andric "ignoring breakpoints, overriding breakpoint stop info "
3190b57cec5SDimitry Andric "ShouldStop, returning true");
3200b57cec5SDimitry Andric m_real_stop_info_sp->OverrideShouldStop(true);
3210b57cec5SDimitry Andric return false;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric } else if (!m_unwind_on_error) {
3240b57cec5SDimitry Andric // If we don't want to discard this plan, than any stop we don't understand
3250b57cec5SDimitry Andric // should be propagated up the stack.
3260b57cec5SDimitry Andric return false;
3270b57cec5SDimitry Andric } else {
3280b57cec5SDimitry Andric // If the subplan is running, any crashes are attributable to us. If we
3290b57cec5SDimitry Andric // want to discard the plan, then we say we explain the stop but if we are
3300b57cec5SDimitry Andric // going to be discarded, let whoever is above us explain the stop. But
3310b57cec5SDimitry Andric // don't discard the plan if the stop would restart itself (for instance if
3320b57cec5SDimitry Andric // it is a signal that is set not to stop. Check that here first. We just
3330b57cec5SDimitry Andric // say we explain the stop but aren't done and everything will continue on
3340b57cec5SDimitry Andric // from there.
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andric if (m_real_stop_info_sp &&
3370b57cec5SDimitry Andric m_real_stop_info_sp->ShouldStopSynchronous(event_ptr)) {
3380b57cec5SDimitry Andric SetPlanComplete(false);
3390b57cec5SDimitry Andric return m_subplan_sp ? m_unwind_on_error : false;
3400b57cec5SDimitry Andric } else
3410b57cec5SDimitry Andric return true;
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric
ShouldStop(Event * event_ptr)3450b57cec5SDimitry Andric bool ThreadPlanCallFunction::ShouldStop(Event *event_ptr) {
3460b57cec5SDimitry Andric // We do some computation in DoPlanExplainsStop that may or may not set the
3470b57cec5SDimitry Andric // plan as complete. We need to do that here to make sure our state is
3480b57cec5SDimitry Andric // correct.
3490b57cec5SDimitry Andric DoPlanExplainsStop(event_ptr);
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric if (IsPlanComplete()) {
3520b57cec5SDimitry Andric ReportRegisterState("Function completed. Register state was:");
3530b57cec5SDimitry Andric return true;
3540b57cec5SDimitry Andric } else {
3550b57cec5SDimitry Andric return false;
3560b57cec5SDimitry Andric }
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric
StopOthers()3590b57cec5SDimitry Andric bool ThreadPlanCallFunction::StopOthers() { return m_stop_other_threads; }
3600b57cec5SDimitry Andric
GetPlanRunState()3610b57cec5SDimitry Andric StateType ThreadPlanCallFunction::GetPlanRunState() { return eStateRunning; }
3620b57cec5SDimitry Andric
DidPush()3630b57cec5SDimitry Andric void ThreadPlanCallFunction::DidPush() {
3640b57cec5SDimitry Andric //#define SINGLE_STEP_EXPRESSIONS
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric // Now set the thread state to "no reason" so we don't run with whatever
3670b57cec5SDimitry Andric // signal was outstanding... Wait till the plan is pushed so we aren't
3680b57cec5SDimitry Andric // changing the stop info till we're about to run.
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andric GetThread().SetStopInfoToNothing();
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric #ifndef SINGLE_STEP_EXPRESSIONS
3735ffd83dbSDimitry Andric Thread &thread = GetThread();
3745ffd83dbSDimitry Andric m_subplan_sp = std::make_shared<ThreadPlanRunToAddress>(thread, m_start_addr,
3755ffd83dbSDimitry Andric m_stop_other_threads);
3760b57cec5SDimitry Andric
3775ffd83dbSDimitry Andric thread.QueueThreadPlan(m_subplan_sp, false);
3780b57cec5SDimitry Andric m_subplan_sp->SetPrivate(true);
3790b57cec5SDimitry Andric #endif
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric
WillStop()3820b57cec5SDimitry Andric bool ThreadPlanCallFunction::WillStop() { return true; }
3830b57cec5SDimitry Andric
MischiefManaged()3840b57cec5SDimitry Andric bool ThreadPlanCallFunction::MischiefManaged() {
3850b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andric if (IsPlanComplete()) {
3889dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction(%p): Completed call function plan.",
3890b57cec5SDimitry Andric static_cast<void *>(this));
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric ThreadPlan::MischiefManaged();
3920b57cec5SDimitry Andric return true;
3930b57cec5SDimitry Andric } else {
3940b57cec5SDimitry Andric return false;
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric
SetBreakpoints()3980b57cec5SDimitry Andric void ThreadPlanCallFunction::SetBreakpoints() {
3995ffd83dbSDimitry Andric if (m_trap_exceptions) {
4000b57cec5SDimitry Andric m_cxx_language_runtime =
4015ffd83dbSDimitry Andric m_process.GetLanguageRuntime(eLanguageTypeC_plus_plus);
4025ffd83dbSDimitry Andric m_objc_language_runtime = m_process.GetLanguageRuntime(eLanguageTypeObjC);
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric if (m_cxx_language_runtime) {
4050b57cec5SDimitry Andric m_should_clear_cxx_exception_bp =
4060b57cec5SDimitry Andric !m_cxx_language_runtime->ExceptionBreakpointsAreSet();
4070b57cec5SDimitry Andric m_cxx_language_runtime->SetExceptionBreakpoints();
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric if (m_objc_language_runtime) {
4100b57cec5SDimitry Andric m_should_clear_objc_exception_bp =
4110b57cec5SDimitry Andric !m_objc_language_runtime->ExceptionBreakpointsAreSet();
4120b57cec5SDimitry Andric m_objc_language_runtime->SetExceptionBreakpoints();
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
ClearBreakpoints()4170b57cec5SDimitry Andric void ThreadPlanCallFunction::ClearBreakpoints() {
4180b57cec5SDimitry Andric if (m_trap_exceptions) {
4190b57cec5SDimitry Andric if (m_cxx_language_runtime && m_should_clear_cxx_exception_bp)
4200b57cec5SDimitry Andric m_cxx_language_runtime->ClearExceptionBreakpoints();
4210b57cec5SDimitry Andric if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
4220b57cec5SDimitry Andric m_objc_language_runtime->ClearExceptionBreakpoints();
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric
BreakpointsExplainStop()4260b57cec5SDimitry Andric bool ThreadPlanCallFunction::BreakpointsExplainStop() {
4270b57cec5SDimitry Andric StopInfoSP stop_info_sp = GetPrivateStopInfo();
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andric if (m_trap_exceptions) {
4300b57cec5SDimitry Andric if ((m_cxx_language_runtime &&
4310b57cec5SDimitry Andric m_cxx_language_runtime->ExceptionBreakpointsExplainStop(
4320b57cec5SDimitry Andric stop_info_sp)) ||
4330b57cec5SDimitry Andric (m_objc_language_runtime &&
4340b57cec5SDimitry Andric m_objc_language_runtime->ExceptionBreakpointsExplainStop(
4350b57cec5SDimitry Andric stop_info_sp))) {
4360b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_STEP));
4379dba64beSDimitry Andric LLDB_LOGF(log, "ThreadPlanCallFunction::BreakpointsExplainStop - Hit an "
4380b57cec5SDimitry Andric "exception breakpoint, setting plan complete.");
4390b57cec5SDimitry Andric
4400b57cec5SDimitry Andric SetPlanComplete(false);
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric // If the user has set the ObjC language breakpoint, it would normally
4430b57cec5SDimitry Andric // get priority over our internal catcher breakpoint, but in this case we
4440b57cec5SDimitry Andric // can't let that happen, so force the ShouldStop here.
4450b57cec5SDimitry Andric stop_info_sp->OverrideShouldStop(true);
4460b57cec5SDimitry Andric return true;
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric }
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andric return false;
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric
SetStopOthers(bool new_value)4530b57cec5SDimitry Andric void ThreadPlanCallFunction::SetStopOthers(bool new_value) {
4540b57cec5SDimitry Andric m_subplan_sp->SetStopOthers(new_value);
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric
RestoreThreadState()457*5f7ddb14SDimitry Andric void ThreadPlanCallFunction::RestoreThreadState() {
458*5f7ddb14SDimitry Andric GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state);
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric
SetReturnValue()4610b57cec5SDimitry Andric void ThreadPlanCallFunction::SetReturnValue() {
4625ffd83dbSDimitry Andric const ABI *abi = m_process.GetABI().get();
4630b57cec5SDimitry Andric if (abi && m_return_type.IsValid()) {
4640b57cec5SDimitry Andric const bool persistent = false;
4650b57cec5SDimitry Andric m_return_valobj_sp =
4665ffd83dbSDimitry Andric abi->GetReturnValueObject(GetThread(), m_return_type, persistent);
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric }
469