130fdc8d8SChris Lattner //===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 9e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlan.h" 1006e827ccSJim Ingham #include "lldb/Core/Debugger.h" 1106e827ccSJim Ingham #include "lldb/Target/Process.h" 12b9c1b51eSKate Stone #include "lldb/Target/RegisterContext.h" 1306e827ccSJim Ingham #include "lldb/Target/Target.h" 14b9c1b51eSKate Stone #include "lldb/Target/Thread.h" 156f9e6901SZachary Turner #include "lldb/Utility/Log.h" 16d821c997SPavel Labath #include "lldb/Utility/State.h" 1730fdc8d8SChris Lattner 1830fdc8d8SChris Lattner using namespace lldb; 1930fdc8d8SChris Lattner using namespace lldb_private; 2030fdc8d8SChris Lattner 2130fdc8d8SChris Lattner // ThreadPlan constructor 22b9c1b51eSKate Stone ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, 23b9c1b51eSKate Stone Vote stop_vote, Vote run_vote) 24b9c1b51eSKate Stone : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote), 25e103ae92SJonas Devlieghere m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false), 26b9c1b51eSKate Stone m_kind(kind), m_name(name), m_plan_complete_mutex(), 27b9c1b51eSKate Stone m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false), 28b9c1b51eSKate Stone m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false), 29b9c1b51eSKate Stone m_plan_succeeded(true) { 3030fdc8d8SChris Lattner SetID(GetNextID()); 3130fdc8d8SChris Lattner } 3230fdc8d8SChris Lattner 3330fdc8d8SChris Lattner // Destructor 34e65b2cf2SEugene Zelenko ThreadPlan::~ThreadPlan() = default; 3530fdc8d8SChris Lattner 36b9c1b51eSKate Stone bool ThreadPlan::PlanExplainsStop(Event *event_ptr) { 37b9c1b51eSKate Stone if (m_cached_plan_explains_stop == eLazyBoolCalculate) { 38221d51cfSJim Ingham bool actual_value = DoPlanExplainsStop(event_ptr); 39221d51cfSJim Ingham m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo; 40221d51cfSJim Ingham return actual_value; 41b9c1b51eSKate Stone } else { 42221d51cfSJim Ingham return m_cached_plan_explains_stop == eLazyBoolYes; 43221d51cfSJim Ingham } 44221d51cfSJim Ingham } 45221d51cfSJim Ingham 46b9c1b51eSKate Stone bool ThreadPlan::IsPlanComplete() { 4716ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex); 4830fdc8d8SChris Lattner return m_plan_complete; 4930fdc8d8SChris Lattner } 5030fdc8d8SChris Lattner 51b9c1b51eSKate Stone void ThreadPlan::SetPlanComplete(bool success) { 5216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex); 5330fdc8d8SChris Lattner m_plan_complete = true; 54fbbfe6ecSJim Ingham m_plan_succeeded = success; 5530fdc8d8SChris Lattner } 5630fdc8d8SChris Lattner 57b9c1b51eSKate Stone bool ThreadPlan::MischiefManaged() { 5816ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex); 5918de2fdcSJim Ingham // Mark the plan is complete, but don't override the success flag. 6030fdc8d8SChris Lattner m_plan_complete = true; 6130fdc8d8SChris Lattner return true; 6230fdc8d8SChris Lattner } 6330fdc8d8SChris Lattner 64b9c1b51eSKate Stone Vote ThreadPlan::ShouldReportStop(Event *event_ptr) { 655160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 662cad65a5SGreg Clayton 67b9c1b51eSKate Stone if (m_stop_vote == eVoteNoOpinion) { 6830fdc8d8SChris Lattner ThreadPlan *prev_plan = GetPreviousPlan(); 69b9c1b51eSKate Stone if (prev_plan) { 702cad65a5SGreg Clayton Vote prev_vote = prev_plan->ShouldReportStop(event_ptr); 7105d382c5SPavel Labath LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote); 722cad65a5SGreg Clayton return prev_vote; 7330fdc8d8SChris Lattner } 742cad65a5SGreg Clayton } 7505d382c5SPavel Labath LLDB_LOG(log, "Returning vote: {0}", m_stop_vote); 7630fdc8d8SChris Lattner return m_stop_vote; 7730fdc8d8SChris Lattner } 7830fdc8d8SChris Lattner 79b9c1b51eSKate Stone Vote ThreadPlan::ShouldReportRun(Event *event_ptr) { 80b9c1b51eSKate Stone if (m_run_vote == eVoteNoOpinion) { 8130fdc8d8SChris Lattner ThreadPlan *prev_plan = GetPreviousPlan(); 8230fdc8d8SChris Lattner if (prev_plan) 8330fdc8d8SChris Lattner return prev_plan->ShouldReportRun(event_ptr); 8430fdc8d8SChris Lattner } 8530fdc8d8SChris Lattner return m_run_vote; 8630fdc8d8SChris Lattner } 8730fdc8d8SChris Lattner 88b9c1b51eSKate Stone bool ThreadPlan::StopOthers() { 8930fdc8d8SChris Lattner ThreadPlan *prev_plan; 9030fdc8d8SChris Lattner prev_plan = GetPreviousPlan(); 91e65b2cf2SEugene Zelenko return (prev_plan == nullptr) ? false : prev_plan->StopOthers(); 9230fdc8d8SChris Lattner } 9330fdc8d8SChris Lattner 94b9c1b51eSKate Stone void ThreadPlan::SetStopOthers(bool new_value) { 9505097246SAdrian Prantl // SetStopOthers doesn't work up the hierarchy. You have to set the explicit 9605097246SAdrian Prantl // ThreadPlan you want to affect. 97f48169bbSJim Ingham } 98f48169bbSJim Ingham 99b9c1b51eSKate Stone bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) { 100221d51cfSJim Ingham m_cached_plan_explains_stop = eLazyBoolCalculate; 101221d51cfSJim Ingham 102b9c1b51eSKate Stone if (current_plan) { 1035160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 10430fdc8d8SChris Lattner 105b9c1b51eSKate Stone if (log) { 1065ccbd294SGreg Clayton RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); 107ff1b5c42SEd Maste assert(reg_ctx); 1082cad65a5SGreg Clayton addr_t pc = reg_ctx->GetPC(); 1092cad65a5SGreg Clayton addr_t sp = reg_ctx->GetSP(); 1102cad65a5SGreg Clayton addr_t fp = reg_ctx->GetFP(); 111*63e5fb76SJonas Devlieghere LLDB_LOGF( 112*63e5fb76SJonas Devlieghere log, 113b9c1b51eSKate Stone "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64 114b9c1b51eSKate Stone ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", " 115b5c0d1ccSJim Ingham "plan = '%s', state = %s, stop others = %d", 116b9c1b51eSKate Stone __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread), 117b9c1b51eSKate Stone m_thread.GetID(), static_cast<uint64_t>(pc), 118b9c1b51eSKate Stone static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(), 119324a1036SSaleem Abdulrasool StateAsCString(resume_state), StopOthers()); 1202cad65a5SGreg Clayton } 12130fdc8d8SChris Lattner } 122221d51cfSJim Ingham return DoWillResume(resume_state, current_plan); 12330fdc8d8SChris Lattner } 12430fdc8d8SChris Lattner 125b9c1b51eSKate Stone lldb::user_id_t ThreadPlan::GetNextID() { 12630fdc8d8SChris Lattner static uint32_t g_nextPlanID = 0; 12730fdc8d8SChris Lattner return ++g_nextPlanID; 12830fdc8d8SChris Lattner } 12930fdc8d8SChris Lattner 130b9c1b51eSKate Stone void ThreadPlan::DidPush() {} 13130fdc8d8SChris Lattner 132b9c1b51eSKate Stone void ThreadPlan::WillPop() {} 13330fdc8d8SChris Lattner 134b9c1b51eSKate Stone bool ThreadPlan::OkayToDiscard() { 135e65b2cf2SEugene Zelenko return IsMasterPlan() ? m_okay_to_discard : true; 13630fdc8d8SChris Lattner } 13730fdc8d8SChris Lattner 138b9c1b51eSKate Stone lldb::StateType ThreadPlan::RunState() { 139b9c1b51eSKate Stone if (m_tracer_sp && m_tracer_sp->TracingEnabled() && 140b9c1b51eSKate Stone m_tracer_sp->SingleStepEnabled()) 14106e827ccSJim Ingham return eStateStepping; 14206e827ccSJim Ingham else 14306e827ccSJim Ingham return GetPlanRunState(); 14406e827ccSJim Ingham } 1456e10f149SGreg Clayton 146b9c1b51eSKate Stone bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) { 147b9c1b51eSKate Stone switch (reason) { 1489b03fa0cSJim Ingham case eStopReasonWatchpoint: 1499b03fa0cSJim Ingham case eStopReasonSignal: 1509b03fa0cSJim Ingham case eStopReasonException: 1519b03fa0cSJim Ingham case eStopReasonExec: 1529b03fa0cSJim Ingham case eStopReasonThreadExiting: 1539b03fa0cSJim Ingham case eStopReasonInstrumentation: 1549b03fa0cSJim Ingham return true; 1559b03fa0cSJim Ingham default: 1569b03fa0cSJim Ingham return false; 1579b03fa0cSJim Ingham } 1589b03fa0cSJim Ingham } 1599b03fa0cSJim Ingham 1606e10f149SGreg Clayton // ThreadPlanNull 1616e10f149SGreg Clayton 162b9c1b51eSKate Stone ThreadPlanNull::ThreadPlanNull(Thread &thread) 163b9c1b51eSKate Stone : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread, 164b9c1b51eSKate Stone eVoteNoOpinion, eVoteNoOpinion) {} 1656e10f149SGreg Clayton 166e65b2cf2SEugene Zelenko ThreadPlanNull::~ThreadPlanNull() = default; 1676e10f149SGreg Clayton 168b9c1b51eSKate Stone void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) { 1696e10f149SGreg Clayton s->PutCString("Null thread plan - thread has been destroyed."); 1706e10f149SGreg Clayton } 1716e10f149SGreg Clayton 172b9c1b51eSKate Stone bool ThreadPlanNull::ValidatePlan(Stream *error) { 1736e10f149SGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG 174b9c1b51eSKate Stone fprintf(stderr, 175b9c1b51eSKate Stone "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 176b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 177b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 1786e10f149SGreg Clayton #else 1796e10f149SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 1806e10f149SGreg Clayton if (log) 181b9c1b51eSKate Stone log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 182b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 183b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), 1846e10f149SGreg Clayton m_thread.GetProtocolID()); 1856e10f149SGreg Clayton #endif 1866e10f149SGreg Clayton return true; 1876e10f149SGreg Clayton } 1886e10f149SGreg Clayton 189b9c1b51eSKate Stone bool ThreadPlanNull::ShouldStop(Event *event_ptr) { 1906e10f149SGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG 191b9c1b51eSKate Stone fprintf(stderr, 192b9c1b51eSKate Stone "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 193b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 194b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 1956e10f149SGreg Clayton #else 1966e10f149SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 1976e10f149SGreg Clayton if (log) 198b9c1b51eSKate Stone log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 199b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 200b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), 2016e10f149SGreg Clayton m_thread.GetProtocolID()); 2026e10f149SGreg Clayton #endif 2036e10f149SGreg Clayton return true; 2046e10f149SGreg Clayton } 2056e10f149SGreg Clayton 206b9c1b51eSKate Stone bool ThreadPlanNull::WillStop() { 2076e10f149SGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG 208b9c1b51eSKate Stone fprintf(stderr, 209b9c1b51eSKate Stone "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 210b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 211b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 2126e10f149SGreg Clayton #else 2136e10f149SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 2146e10f149SGreg Clayton if (log) 215b9c1b51eSKate Stone log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 216b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 217b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), 2186e10f149SGreg Clayton m_thread.GetProtocolID()); 2196e10f149SGreg Clayton #endif 2206e10f149SGreg Clayton return true; 2216e10f149SGreg Clayton } 2226e10f149SGreg Clayton 223b9c1b51eSKate Stone bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) { 2246e10f149SGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG 225b9c1b51eSKate Stone fprintf(stderr, 226b9c1b51eSKate Stone "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 227b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 228b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 2296e10f149SGreg Clayton #else 2306e10f149SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 2316e10f149SGreg Clayton if (log) 232b9c1b51eSKate Stone log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 233b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 234b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), 2356e10f149SGreg Clayton m_thread.GetProtocolID()); 2366e10f149SGreg Clayton #endif 2376e10f149SGreg Clayton return true; 2386e10f149SGreg Clayton } 2396e10f149SGreg Clayton 2406e10f149SGreg Clayton // The null plan is never done. 241b9c1b51eSKate Stone bool ThreadPlanNull::MischiefManaged() { 2426e10f149SGreg Clayton // The null plan is never done. 2436e10f149SGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG 244b9c1b51eSKate Stone fprintf(stderr, 245b9c1b51eSKate Stone "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 246b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 247b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 2486e10f149SGreg Clayton #else 2496e10f149SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 2506e10f149SGreg Clayton if (log) 251b9c1b51eSKate Stone log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 252b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 253b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), 2546e10f149SGreg Clayton m_thread.GetProtocolID()); 2556e10f149SGreg Clayton #endif 2566e10f149SGreg Clayton return false; 2576e10f149SGreg Clayton } 2586e10f149SGreg Clayton 259b9c1b51eSKate Stone lldb::StateType ThreadPlanNull::GetPlanRunState() { 2606e10f149SGreg Clayton // Not sure what to return here. This is a dead thread. 2616e10f149SGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG 262b9c1b51eSKate Stone fprintf(stderr, 263b9c1b51eSKate Stone "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 264b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 265b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 2666e10f149SGreg Clayton #else 2676e10f149SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 2686e10f149SGreg Clayton if (log) 269b9c1b51eSKate Stone log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 270b9c1b51eSKate Stone ", ptid = 0x%" PRIx64 ")", 271b9c1b51eSKate Stone LLVM_PRETTY_FUNCTION, m_thread.GetID(), 2726e10f149SGreg Clayton m_thread.GetProtocolID()); 2736e10f149SGreg Clayton #endif 2746e10f149SGreg Clayton return eStateRunning; 2756e10f149SGreg Clayton } 276