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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadPlan.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/State.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/RegisterContext.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/Thread.h"
21 #include "lldb/Utility/Log.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // ThreadPlan constructor
28 //----------------------------------------------------------------------
29 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
30 Vote stop_vote, Vote run_vote)
31 : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
32 m_kind(kind), m_name(name), m_plan_complete_mutex(),
33 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
34 m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
35 m_plan_succeeded(true) {
39 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
42 ThreadPlan::~ThreadPlan() = default;
44 bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
45 if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
46 bool actual_value = DoPlanExplainsStop(event_ptr);
47 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
50 return m_cached_plan_explains_stop == eLazyBoolYes;
54 bool ThreadPlan::IsPlanComplete() {
55 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
56 return m_plan_complete;
59 void ThreadPlan::SetPlanComplete(bool success) {
60 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
61 m_plan_complete = true;
62 m_plan_succeeded = success;
65 bool ThreadPlan::MischiefManaged() {
66 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
67 // Mark the plan is complete, but don't override the success flag.
68 m_plan_complete = true;
72 Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
73 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
75 if (m_stop_vote == eVoteNoOpinion) {
76 ThreadPlan *prev_plan = GetPreviousPlan();
78 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
79 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
83 LLDB_LOG(log, "Returning vote: {0}", m_stop_vote);
87 Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
88 if (m_run_vote == eVoteNoOpinion) {
89 ThreadPlan *prev_plan = GetPreviousPlan();
91 return prev_plan->ShouldReportRun(event_ptr);
96 bool ThreadPlan::StopOthers() {
97 ThreadPlan *prev_plan;
98 prev_plan = GetPreviousPlan();
99 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
102 void ThreadPlan::SetStopOthers(bool new_value) {
103 // SetStopOthers doesn't work up the hierarchy. You have to set the
104 // explicit ThreadPlan you want to affect.
107 bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
108 m_cached_plan_explains_stop = eLazyBoolCalculate;
111 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
114 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
116 addr_t pc = reg_ctx->GetPC();
117 addr_t sp = reg_ctx->GetSP();
118 addr_t fp = reg_ctx->GetFP();
120 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
121 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
122 "plan = '%s', state = %s, stop others = %d",
123 __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
124 m_thread.GetID(), static_cast<uint64_t>(pc),
125 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
126 StateAsCString(resume_state), StopOthers());
129 return DoWillResume(resume_state, current_plan);
132 lldb::user_id_t ThreadPlan::GetNextID() {
133 static uint32_t g_nextPlanID = 0;
134 return ++g_nextPlanID;
137 void ThreadPlan::DidPush() {}
139 void ThreadPlan::WillPop() {}
141 bool ThreadPlan::OkayToDiscard() {
142 return IsMasterPlan() ? m_okay_to_discard : true;
145 lldb::StateType ThreadPlan::RunState() {
146 if (m_tracer_sp && m_tracer_sp->TracingEnabled() &&
147 m_tracer_sp->SingleStepEnabled())
148 return eStateStepping;
150 return GetPlanRunState();
153 bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
155 case eStopReasonWatchpoint:
156 case eStopReasonSignal:
157 case eStopReasonException:
158 case eStopReasonExec:
159 case eStopReasonThreadExiting:
160 case eStopReasonInstrumentation:
167 //----------------------------------------------------------------------
169 //----------------------------------------------------------------------
171 ThreadPlanNull::ThreadPlanNull(Thread &thread)
172 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
173 eVoteNoOpinion, eVoteNoOpinion) {}
175 ThreadPlanNull::~ThreadPlanNull() = default;
177 void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
178 s->PutCString("Null thread plan - thread has been destroyed.");
181 bool ThreadPlanNull::ValidatePlan(Stream *error) {
182 #ifdef LLDB_CONFIGURATION_DEBUG
184 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
185 ", ptid = 0x%" PRIx64 ")",
186 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
188 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
190 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
191 ", ptid = 0x%" PRIx64 ")",
192 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
193 m_thread.GetProtocolID());
198 bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
199 #ifdef LLDB_CONFIGURATION_DEBUG
201 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
202 ", ptid = 0x%" PRIx64 ")",
203 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
205 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
207 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
208 ", ptid = 0x%" PRIx64 ")",
209 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
210 m_thread.GetProtocolID());
215 bool ThreadPlanNull::WillStop() {
216 #ifdef LLDB_CONFIGURATION_DEBUG
218 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
219 ", ptid = 0x%" PRIx64 ")",
220 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
222 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
224 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
225 ", ptid = 0x%" PRIx64 ")",
226 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
227 m_thread.GetProtocolID());
232 bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
233 #ifdef LLDB_CONFIGURATION_DEBUG
235 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
236 ", ptid = 0x%" PRIx64 ")",
237 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
239 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
241 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
242 ", ptid = 0x%" PRIx64 ")",
243 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
244 m_thread.GetProtocolID());
249 // The null plan is never done.
250 bool ThreadPlanNull::MischiefManaged() {
251 // The null plan is never done.
252 #ifdef LLDB_CONFIGURATION_DEBUG
254 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
255 ", ptid = 0x%" PRIx64 ")",
256 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
258 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
260 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
261 ", ptid = 0x%" PRIx64 ")",
262 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
263 m_thread.GetProtocolID());
268 lldb::StateType ThreadPlanNull::GetPlanRunState() {
269 // Not sure what to return here. This is a dead thread.
270 #ifdef LLDB_CONFIGURATION_DEBUG
272 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
273 ", ptid = 0x%" PRIx64 ")",
274 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
276 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
278 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
279 ", ptid = 0x%" PRIx64 ")",
280 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
281 m_thread.GetProtocolID());
283 return eStateRunning;