1 //===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlan.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Target/Process.h"
13 #include "lldb/Target/RegisterContext.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/State.h"
20 using namespace lldb_private;
22 //----------------------------------------------------------------------
23 // ThreadPlan constructor
24 //----------------------------------------------------------------------
25 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
26 Vote stop_vote, Vote run_vote)
27 : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
28 m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
29 m_kind(kind), m_name(name), m_plan_complete_mutex(),
30 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
31 m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
32 m_plan_succeeded(true) {
36 //----------------------------------------------------------------------
38 //----------------------------------------------------------------------
39 ThreadPlan::~ThreadPlan() = default;
41 bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
42 if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
43 bool actual_value = DoPlanExplainsStop(event_ptr);
44 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
47 return m_cached_plan_explains_stop == eLazyBoolYes;
51 bool ThreadPlan::IsPlanComplete() {
52 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
53 return m_plan_complete;
56 void ThreadPlan::SetPlanComplete(bool success) {
57 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
58 m_plan_complete = true;
59 m_plan_succeeded = success;
62 bool ThreadPlan::MischiefManaged() {
63 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
64 // Mark the plan is complete, but don't override the success flag.
65 m_plan_complete = true;
69 Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
70 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
72 if (m_stop_vote == eVoteNoOpinion) {
73 ThreadPlan *prev_plan = GetPreviousPlan();
75 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
76 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
80 LLDB_LOG(log, "Returning vote: {0}", m_stop_vote);
84 Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
85 if (m_run_vote == eVoteNoOpinion) {
86 ThreadPlan *prev_plan = GetPreviousPlan();
88 return prev_plan->ShouldReportRun(event_ptr);
93 bool ThreadPlan::StopOthers() {
94 ThreadPlan *prev_plan;
95 prev_plan = GetPreviousPlan();
96 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
99 void ThreadPlan::SetStopOthers(bool new_value) {
100 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit
101 // ThreadPlan you want to affect.
104 bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
105 m_cached_plan_explains_stop = eLazyBoolCalculate;
108 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
111 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
113 addr_t pc = reg_ctx->GetPC();
114 addr_t sp = reg_ctx->GetSP();
115 addr_t fp = reg_ctx->GetFP();
117 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
118 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
119 "plan = '%s', state = %s, stop others = %d",
120 __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
121 m_thread.GetID(), static_cast<uint64_t>(pc),
122 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
123 StateAsCString(resume_state), StopOthers());
126 return DoWillResume(resume_state, current_plan);
129 lldb::user_id_t ThreadPlan::GetNextID() {
130 static uint32_t g_nextPlanID = 0;
131 return ++g_nextPlanID;
134 void ThreadPlan::DidPush() {}
136 void ThreadPlan::WillPop() {}
138 bool ThreadPlan::OkayToDiscard() {
139 return IsMasterPlan() ? m_okay_to_discard : true;
142 lldb::StateType ThreadPlan::RunState() {
143 if (m_tracer_sp && m_tracer_sp->TracingEnabled() &&
144 m_tracer_sp->SingleStepEnabled())
145 return eStateStepping;
147 return GetPlanRunState();
150 bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
152 case eStopReasonWatchpoint:
153 case eStopReasonSignal:
154 case eStopReasonException:
155 case eStopReasonExec:
156 case eStopReasonThreadExiting:
157 case eStopReasonInstrumentation:
164 //----------------------------------------------------------------------
166 //----------------------------------------------------------------------
168 ThreadPlanNull::ThreadPlanNull(Thread &thread)
169 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
170 eVoteNoOpinion, eVoteNoOpinion) {}
172 ThreadPlanNull::~ThreadPlanNull() = default;
174 void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
175 s->PutCString("Null thread plan - thread has been destroyed.");
178 bool ThreadPlanNull::ValidatePlan(Stream *error) {
179 #ifdef LLDB_CONFIGURATION_DEBUG
181 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
182 ", ptid = 0x%" PRIx64 ")",
183 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
185 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
187 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
188 ", ptid = 0x%" PRIx64 ")",
189 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
190 m_thread.GetProtocolID());
195 bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
196 #ifdef LLDB_CONFIGURATION_DEBUG
198 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
199 ", ptid = 0x%" PRIx64 ")",
200 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
202 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
204 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
205 ", ptid = 0x%" PRIx64 ")",
206 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
207 m_thread.GetProtocolID());
212 bool ThreadPlanNull::WillStop() {
213 #ifdef LLDB_CONFIGURATION_DEBUG
215 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
216 ", ptid = 0x%" PRIx64 ")",
217 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
219 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
221 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
222 ", ptid = 0x%" PRIx64 ")",
223 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
224 m_thread.GetProtocolID());
229 bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
230 #ifdef LLDB_CONFIGURATION_DEBUG
232 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
233 ", ptid = 0x%" PRIx64 ")",
234 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
236 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
238 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
239 ", ptid = 0x%" PRIx64 ")",
240 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
241 m_thread.GetProtocolID());
246 // The null plan is never done.
247 bool ThreadPlanNull::MischiefManaged() {
248 // The null plan is never done.
249 #ifdef LLDB_CONFIGURATION_DEBUG
251 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
252 ", ptid = 0x%" PRIx64 ")",
253 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
255 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
257 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
258 ", ptid = 0x%" PRIx64 ")",
259 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
260 m_thread.GetProtocolID());
265 lldb::StateType ThreadPlanNull::GetPlanRunState() {
266 // Not sure what to return here. This is a dead thread.
267 #ifdef LLDB_CONFIGURATION_DEBUG
269 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
270 ", ptid = 0x%" PRIx64 ")",
271 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
273 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
275 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
276 ", ptid = 0x%" PRIx64 ")",
277 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
278 m_thread.GetProtocolID());
280 return eStateRunning;