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/Log.h"
17 #include "lldb/Core/State.h"
18 #include "lldb/Target/RegisterContext.h"
19 #include "lldb/Target/Thread.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/ConvertEnum.h"
25 using namespace lldb_private;
27 //----------------------------------------------------------------------
28 // ThreadPlan constructor
29 //----------------------------------------------------------------------
30 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote stop_vote, Vote run_vote)
32 m_stop_vote(stop_vote),
36 m_plan_complete_mutex(),
37 m_cached_plan_explains_stop(eLazyBoolCalculate),
38 m_plan_complete(false),
39 m_plan_private(false),
40 m_okay_to_discard(true),
41 m_is_master_plan(false),
42 m_plan_succeeded(true)
47 //----------------------------------------------------------------------
49 //----------------------------------------------------------------------
50 ThreadPlan::~ThreadPlan() = default;
53 ThreadPlan::PlanExplainsStop (Event *event_ptr)
55 if (m_cached_plan_explains_stop == eLazyBoolCalculate)
57 bool actual_value = DoPlanExplainsStop(event_ptr);
58 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
63 return m_cached_plan_explains_stop == eLazyBoolYes;
68 ThreadPlan::IsPlanComplete ()
70 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
71 return m_plan_complete;
75 ThreadPlan::SetPlanComplete (bool success)
77 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
78 m_plan_complete = true;
79 m_plan_succeeded = success;
83 ThreadPlan::MischiefManaged ()
85 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
86 // Mark the plan is complete, but don't override the success flag.
87 m_plan_complete = true;
92 ThreadPlan::ShouldReportStop (Event *event_ptr)
94 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
96 if (m_stop_vote == eVoteNoOpinion)
98 ThreadPlan *prev_plan = GetPreviousPlan ();
101 Vote prev_vote = prev_plan->ShouldReportStop (event_ptr);
103 log->Printf ("ThreadPlan::ShouldReportStop() returning previous thread plan vote: %s",
104 GetVoteAsCString (prev_vote));
109 log->Printf ("ThreadPlan::ShouldReportStop() returning vote: %s", GetVoteAsCString (m_stop_vote));
114 ThreadPlan::ShouldReportRun (Event *event_ptr)
116 if (m_run_vote == eVoteNoOpinion)
118 ThreadPlan *prev_plan = GetPreviousPlan ();
120 return prev_plan->ShouldReportRun (event_ptr);
126 ThreadPlan::StopOthers ()
128 ThreadPlan *prev_plan;
129 prev_plan = GetPreviousPlan ();
130 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
134 ThreadPlan::SetStopOthers (bool new_value)
136 // SetStopOthers doesn't work up the hierarchy. You have to set the
137 // explicit ThreadPlan you want to affect.
141 ThreadPlan::WillResume (StateType resume_state, bool current_plan)
143 m_cached_plan_explains_stop = eLazyBoolCalculate;
147 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
151 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
153 addr_t pc = reg_ctx->GetPC();
154 addr_t sp = reg_ctx->GetSP();
155 addr_t fp = reg_ctx->GetFP();
156 log->Printf("%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
157 "plan = '%s', state = %s, stop others = %d",
158 __FUNCTION__, m_thread.GetIndexID(),
159 static_cast<void*>(&m_thread), m_thread.GetID(),
160 static_cast<uint64_t>(pc), static_cast<uint64_t>(sp),
161 static_cast<uint64_t>(fp), m_name.c_str(),
162 StateAsCString(resume_state), StopOthers());
165 return DoWillResume (resume_state, current_plan);
169 ThreadPlan::GetNextID()
171 static uint32_t g_nextPlanID = 0;
172 return ++g_nextPlanID;
176 ThreadPlan::DidPush()
181 ThreadPlan::WillPop()
186 ThreadPlan::OkayToDiscard()
188 return IsMasterPlan() ? m_okay_to_discard : true;
192 ThreadPlan::RunState ()
194 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && m_tracer_sp->SingleStepEnabled())
195 return eStateStepping;
197 return GetPlanRunState();
201 ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason)
205 case eStopReasonWatchpoint:
206 case eStopReasonSignal:
207 case eStopReasonException:
208 case eStopReasonExec:
209 case eStopReasonThreadExiting:
210 case eStopReasonInstrumentation:
217 //----------------------------------------------------------------------
219 //----------------------------------------------------------------------
221 ThreadPlanNull::ThreadPlanNull (Thread &thread) :
222 ThreadPlan (ThreadPlan::eKindNull,
230 ThreadPlanNull::~ThreadPlanNull() = default;
233 ThreadPlanNull::GetDescription (Stream *s,
234 lldb::DescriptionLevel level)
236 s->PutCString("Null thread plan - thread has been destroyed.");
240 ThreadPlanNull::ValidatePlan (Stream *error)
242 #ifdef LLDB_CONFIGURATION_DEBUG
243 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
246 m_thread.GetProtocolID());
248 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
250 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
253 m_thread.GetProtocolID());
259 ThreadPlanNull::ShouldStop (Event *event_ptr)
261 #ifdef LLDB_CONFIGURATION_DEBUG
262 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
265 m_thread.GetProtocolID());
267 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
269 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
272 m_thread.GetProtocolID());
278 ThreadPlanNull::WillStop ()
280 #ifdef LLDB_CONFIGURATION_DEBUG
281 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
284 m_thread.GetProtocolID());
286 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
288 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
291 m_thread.GetProtocolID());
297 ThreadPlanNull::DoPlanExplainsStop (Event *event_ptr)
299 #ifdef LLDB_CONFIGURATION_DEBUG
300 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
303 m_thread.GetProtocolID());
305 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
307 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
310 m_thread.GetProtocolID());
315 // The null plan is never done.
317 ThreadPlanNull::MischiefManaged ()
319 // The null plan is never done.
320 #ifdef LLDB_CONFIGURATION_DEBUG
321 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
324 m_thread.GetProtocolID());
326 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
328 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
331 m_thread.GetProtocolID());
337 ThreadPlanNull::GetPlanRunState ()
339 // Not sure what to return here. This is a dead thread.
340 #ifdef LLDB_CONFIGURATION_DEBUG
341 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
344 m_thread.GetProtocolID());
346 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
348 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
351 m_thread.GetProtocolID());
353 return eStateRunning;