1 //===-- ThreadPlan.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Target/ThreadPlan.h" 10 #include "lldb/Core/Debugger.h" 11 #include "lldb/Target/Process.h" 12 #include "lldb/Target/RegisterContext.h" 13 #include "lldb/Target/Target.h" 14 #include "lldb/Target/Thread.h" 15 #include "lldb/Utility/Log.h" 16 #include "lldb/Utility/State.h" 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 // ThreadPlan constructor 22 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, 23 Vote stop_vote, Vote run_vote) 24 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()), 25 m_stop_vote(stop_vote), m_run_vote(run_vote), 26 m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false), 27 m_kind(kind), m_thread(&thread), m_name(name), m_plan_complete_mutex(), 28 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false), 29 m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false), 30 m_plan_succeeded(true) { 31 SetID(GetNextID()); 32 } 33 34 // Destructor 35 ThreadPlan::~ThreadPlan() = default; 36 37 Thread &ThreadPlan::GetThread() { 38 if (m_thread) 39 return *m_thread; 40 41 ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid); 42 m_thread = thread_sp.get(); 43 return *m_thread; 44 } 45 46 bool ThreadPlan::PlanExplainsStop(Event *event_ptr) { 47 if (m_cached_plan_explains_stop == eLazyBoolCalculate) { 48 bool actual_value = DoPlanExplainsStop(event_ptr); 49 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo; 50 return actual_value; 51 } else { 52 return m_cached_plan_explains_stop == eLazyBoolYes; 53 } 54 } 55 56 bool ThreadPlan::IsPlanComplete() { 57 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex); 58 return m_plan_complete; 59 } 60 61 void ThreadPlan::SetPlanComplete(bool success) { 62 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex); 63 m_plan_complete = true; 64 m_plan_succeeded = success; 65 } 66 67 bool ThreadPlan::MischiefManaged() { 68 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex); 69 // Mark the plan is complete, but don't override the success flag. 70 m_plan_complete = true; 71 return true; 72 } 73 74 Vote ThreadPlan::ShouldReportStop(Event *event_ptr) { 75 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 76 77 if (m_stop_vote == eVoteNoOpinion) { 78 ThreadPlan *prev_plan = GetPreviousPlan(); 79 if (prev_plan) { 80 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr); 81 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote); 82 return prev_vote; 83 } 84 } 85 LLDB_LOG(log, "Returning vote: {0}", m_stop_vote); 86 return m_stop_vote; 87 } 88 89 Vote ThreadPlan::ShouldReportRun(Event *event_ptr) { 90 if (m_run_vote == eVoteNoOpinion) { 91 ThreadPlan *prev_plan = GetPreviousPlan(); 92 if (prev_plan) 93 return prev_plan->ShouldReportRun(event_ptr); 94 } 95 return m_run_vote; 96 } 97 98 bool ThreadPlan::StopOthers() { 99 ThreadPlan *prev_plan; 100 prev_plan = GetPreviousPlan(); 101 return (prev_plan == nullptr) ? false : prev_plan->StopOthers(); 102 } 103 104 void ThreadPlan::SetStopOthers(bool new_value) { 105 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit 106 // ThreadPlan you want to affect. 107 } 108 109 bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) { 110 m_cached_plan_explains_stop = eLazyBoolCalculate; 111 112 if (current_plan) { 113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 114 115 if (log) { 116 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); 117 assert(reg_ctx); 118 addr_t pc = reg_ctx->GetPC(); 119 addr_t sp = reg_ctx->GetSP(); 120 addr_t fp = reg_ctx->GetFP(); 121 LLDB_LOGF( 122 log, 123 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64 124 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", " 125 "plan = '%s', state = %s, stop others = %d", 126 __FUNCTION__, GetThread().GetIndexID(), 127 static_cast<void *>(&GetThread()), m_tid, static_cast<uint64_t>(pc), 128 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(), 129 StateAsCString(resume_state), StopOthers()); 130 } 131 } 132 return DoWillResume(resume_state, current_plan); 133 } 134 135 lldb::user_id_t ThreadPlan::GetNextID() { 136 static uint32_t g_nextPlanID = 0; 137 return ++g_nextPlanID; 138 } 139 140 void ThreadPlan::DidPush() {} 141 142 void ThreadPlan::WillPop() {} 143 144 bool ThreadPlan::OkayToDiscard() { 145 return IsMasterPlan() ? m_okay_to_discard : true; 146 } 147 148 lldb::StateType ThreadPlan::RunState() { 149 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && 150 m_tracer_sp->SingleStepEnabled()) 151 return eStateStepping; 152 else 153 return GetPlanRunState(); 154 } 155 156 bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) { 157 switch (reason) { 158 case eStopReasonWatchpoint: 159 case eStopReasonSignal: 160 case eStopReasonException: 161 case eStopReasonExec: 162 case eStopReasonThreadExiting: 163 case eStopReasonInstrumentation: 164 return true; 165 default: 166 return false; 167 } 168 } 169 170 // ThreadPlanNull 171 172 ThreadPlanNull::ThreadPlanNull(Thread &thread) 173 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread, 174 eVoteNoOpinion, eVoteNoOpinion) {} 175 176 ThreadPlanNull::~ThreadPlanNull() = default; 177 178 void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) { 179 s->PutCString("Null thread plan - thread has been destroyed."); 180 } 181 182 bool ThreadPlanNull::ValidatePlan(Stream *error) { 183 #ifdef LLDB_CONFIGURATION_DEBUG 184 fprintf(stderr, 185 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 186 ", ptid = 0x%" PRIx64 ")", 187 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 188 #else 189 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 190 if (log) 191 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 192 ", ptid = 0x%" PRIx64 ")", 193 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 194 #endif 195 return true; 196 } 197 198 bool ThreadPlanNull::ShouldStop(Event *event_ptr) { 199 #ifdef LLDB_CONFIGURATION_DEBUG 200 fprintf(stderr, 201 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 202 ", ptid = 0x%" PRIx64 ")", 203 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 204 #else 205 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 206 if (log) 207 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 208 ", ptid = 0x%" PRIx64 ")", 209 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 210 #endif 211 return true; 212 } 213 214 bool ThreadPlanNull::WillStop() { 215 #ifdef LLDB_CONFIGURATION_DEBUG 216 fprintf(stderr, 217 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 218 ", ptid = 0x%" PRIx64 ")", 219 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 220 #else 221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 222 if (log) 223 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 224 ", ptid = 0x%" PRIx64 ")", 225 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 226 #endif 227 return true; 228 } 229 230 bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) { 231 #ifdef LLDB_CONFIGURATION_DEBUG 232 fprintf(stderr, 233 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 234 ", ptid = 0x%" PRIx64 ")", 235 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID()); 236 #else 237 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 238 if (log) 239 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 240 ", ptid = 0x%" PRIx64 ")", 241 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 242 #endif 243 return true; 244 } 245 246 // The null plan is never done. 247 bool ThreadPlanNull::MischiefManaged() { 248 // The null plan is never done. 249 #ifdef LLDB_CONFIGURATION_DEBUG 250 fprintf(stderr, 251 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 252 ", ptid = 0x%" PRIx64 ")", 253 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 254 #else 255 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 256 if (log) 257 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 258 ", ptid = 0x%" PRIx64 ")", 259 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 260 #endif 261 return false; 262 } 263 264 lldb::StateType ThreadPlanNull::GetPlanRunState() { 265 // Not sure what to return here. This is a dead thread. 266 #ifdef LLDB_CONFIGURATION_DEBUG 267 fprintf(stderr, 268 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 269 ", ptid = 0x%" PRIx64 ")", 270 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 271 #else 272 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 273 if (log) 274 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 275 ", ptid = 0x%" PRIx64 ")", 276 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID()); 277 #endif 278 return eStateRunning; 279 } 280