1ac7ddfbfSEd Maste //===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
109f2f44ceSEd Maste #include "lldb/Target/ThreadPlan.h"
11ac7ddfbfSEd Maste #include "lldb/Core/Debugger.h"
12ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
13435933ddSDimitry Andric #include "lldb/Target/RegisterContext.h"
14ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
15435933ddSDimitry Andric #include "lldb/Target/Thread.h"
16f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
17*b5893f02SDimitry Andric #include "lldb/Utility/State.h"
18ac7ddfbfSEd Maste 
19ac7ddfbfSEd Maste using namespace lldb;
20ac7ddfbfSEd Maste using namespace lldb_private;
21ac7ddfbfSEd Maste 
22ac7ddfbfSEd Maste //----------------------------------------------------------------------
23ac7ddfbfSEd Maste // ThreadPlan constructor
24ac7ddfbfSEd Maste //----------------------------------------------------------------------
ThreadPlan(ThreadPlanKind kind,const char * name,Thread & thread,Vote stop_vote,Vote run_vote)25435933ddSDimitry Andric ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
26435933ddSDimitry Andric                        Vote stop_vote, Vote run_vote)
27435933ddSDimitry Andric     : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
28*b5893f02SDimitry Andric       m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
29435933ddSDimitry Andric       m_kind(kind), m_name(name), m_plan_complete_mutex(),
30435933ddSDimitry Andric       m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
31435933ddSDimitry Andric       m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
32435933ddSDimitry Andric       m_plan_succeeded(true) {
33ac7ddfbfSEd Maste   SetID(GetNextID());
34ac7ddfbfSEd Maste }
35ac7ddfbfSEd Maste 
36ac7ddfbfSEd Maste //----------------------------------------------------------------------
37ac7ddfbfSEd Maste // Destructor
38ac7ddfbfSEd Maste //----------------------------------------------------------------------
399f2f44ceSEd Maste ThreadPlan::~ThreadPlan() = default;
40ac7ddfbfSEd Maste 
PlanExplainsStop(Event * event_ptr)41435933ddSDimitry Andric bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
42435933ddSDimitry Andric   if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
43ac7ddfbfSEd Maste     bool actual_value = DoPlanExplainsStop(event_ptr);
44ac7ddfbfSEd Maste     m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
45ac7ddfbfSEd Maste     return actual_value;
46435933ddSDimitry Andric   } else {
47ac7ddfbfSEd Maste     return m_cached_plan_explains_stop == eLazyBoolYes;
48ac7ddfbfSEd Maste   }
49ac7ddfbfSEd Maste }
50ac7ddfbfSEd Maste 
IsPlanComplete()51435933ddSDimitry Andric bool ThreadPlan::IsPlanComplete() {
524bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
53ac7ddfbfSEd Maste   return m_plan_complete;
54ac7ddfbfSEd Maste }
55ac7ddfbfSEd Maste 
SetPlanComplete(bool success)56435933ddSDimitry Andric void ThreadPlan::SetPlanComplete(bool success) {
574bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
58ac7ddfbfSEd Maste   m_plan_complete = true;
59ac7ddfbfSEd Maste   m_plan_succeeded = success;
60ac7ddfbfSEd Maste }
61ac7ddfbfSEd Maste 
MischiefManaged()62435933ddSDimitry Andric bool ThreadPlan::MischiefManaged() {
634bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
64ac7ddfbfSEd Maste   // Mark the plan is complete, but don't override the success flag.
65ac7ddfbfSEd Maste   m_plan_complete = true;
66ac7ddfbfSEd Maste   return true;
67ac7ddfbfSEd Maste }
68ac7ddfbfSEd Maste 
ShouldReportStop(Event * event_ptr)69435933ddSDimitry Andric Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
70ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
71ac7ddfbfSEd Maste 
72435933ddSDimitry Andric   if (m_stop_vote == eVoteNoOpinion) {
73ac7ddfbfSEd Maste     ThreadPlan *prev_plan = GetPreviousPlan();
74435933ddSDimitry Andric     if (prev_plan) {
75ac7ddfbfSEd Maste       Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
76f678e45dSDimitry Andric       LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
77ac7ddfbfSEd Maste       return prev_vote;
78ac7ddfbfSEd Maste     }
79ac7ddfbfSEd Maste   }
80f678e45dSDimitry Andric   LLDB_LOG(log, "Returning vote: {0}", m_stop_vote);
81ac7ddfbfSEd Maste   return m_stop_vote;
82ac7ddfbfSEd Maste }
83ac7ddfbfSEd Maste 
ShouldReportRun(Event * event_ptr)84435933ddSDimitry Andric Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
85435933ddSDimitry Andric   if (m_run_vote == eVoteNoOpinion) {
86ac7ddfbfSEd Maste     ThreadPlan *prev_plan = GetPreviousPlan();
87ac7ddfbfSEd Maste     if (prev_plan)
88ac7ddfbfSEd Maste       return prev_plan->ShouldReportRun(event_ptr);
89ac7ddfbfSEd Maste   }
90ac7ddfbfSEd Maste   return m_run_vote;
91ac7ddfbfSEd Maste }
92ac7ddfbfSEd Maste 
StopOthers()93435933ddSDimitry Andric bool ThreadPlan::StopOthers() {
94ac7ddfbfSEd Maste   ThreadPlan *prev_plan;
95ac7ddfbfSEd Maste   prev_plan = GetPreviousPlan();
969f2f44ceSEd Maste   return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
97ac7ddfbfSEd Maste }
98ac7ddfbfSEd Maste 
SetStopOthers(bool new_value)99435933ddSDimitry Andric void ThreadPlan::SetStopOthers(bool new_value) {
1004ba319b5SDimitry Andric   // SetStopOthers doesn't work up the hierarchy.  You have to set the explicit
1014ba319b5SDimitry Andric   // ThreadPlan you want to affect.
102ac7ddfbfSEd Maste }
103ac7ddfbfSEd Maste 
WillResume(StateType resume_state,bool current_plan)104435933ddSDimitry Andric bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
105ac7ddfbfSEd Maste   m_cached_plan_explains_stop = eLazyBoolCalculate;
106ac7ddfbfSEd Maste 
107435933ddSDimitry Andric   if (current_plan) {
108ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
109ac7ddfbfSEd Maste 
110435933ddSDimitry Andric     if (log) {
111ac7ddfbfSEd Maste       RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
1121c3bbb01SEd Maste       assert(reg_ctx);
113ac7ddfbfSEd Maste       addr_t pc = reg_ctx->GetPC();
114ac7ddfbfSEd Maste       addr_t sp = reg_ctx->GetSP();
115ac7ddfbfSEd Maste       addr_t fp = reg_ctx->GetFP();
116435933ddSDimitry Andric       log->Printf(
117435933ddSDimitry Andric           "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
118435933ddSDimitry Andric           ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
119ac7ddfbfSEd Maste           "plan = '%s', state = %s, stop others = %d",
120435933ddSDimitry Andric           __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
121435933ddSDimitry Andric           m_thread.GetID(), static_cast<uint64_t>(pc),
122435933ddSDimitry Andric           static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
1230127ef0fSEd Maste           StateAsCString(resume_state), StopOthers());
124ac7ddfbfSEd Maste     }
125ac7ddfbfSEd Maste   }
126ac7ddfbfSEd Maste   return DoWillResume(resume_state, current_plan);
127ac7ddfbfSEd Maste }
128ac7ddfbfSEd Maste 
GetNextID()129435933ddSDimitry Andric lldb::user_id_t ThreadPlan::GetNextID() {
130ac7ddfbfSEd Maste   static uint32_t g_nextPlanID = 0;
131ac7ddfbfSEd Maste   return ++g_nextPlanID;
132ac7ddfbfSEd Maste }
133ac7ddfbfSEd Maste 
DidPush()134435933ddSDimitry Andric void ThreadPlan::DidPush() {}
135ac7ddfbfSEd Maste 
WillPop()136435933ddSDimitry Andric void ThreadPlan::WillPop() {}
137ac7ddfbfSEd Maste 
OkayToDiscard()138435933ddSDimitry Andric bool ThreadPlan::OkayToDiscard() {
1399f2f44ceSEd Maste   return IsMasterPlan() ? m_okay_to_discard : true;
140ac7ddfbfSEd Maste }
141ac7ddfbfSEd Maste 
RunState()142435933ddSDimitry Andric lldb::StateType ThreadPlan::RunState() {
143435933ddSDimitry Andric   if (m_tracer_sp && m_tracer_sp->TracingEnabled() &&
144435933ddSDimitry Andric       m_tracer_sp->SingleStepEnabled())
145ac7ddfbfSEd Maste     return eStateStepping;
146ac7ddfbfSEd Maste   else
147ac7ddfbfSEd Maste     return GetPlanRunState();
148ac7ddfbfSEd Maste }
149ac7ddfbfSEd Maste 
IsUsuallyUnexplainedStopReason(lldb::StopReason reason)150435933ddSDimitry Andric bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
151435933ddSDimitry Andric   switch (reason) {
1529f2f44ceSEd Maste   case eStopReasonWatchpoint:
1539f2f44ceSEd Maste   case eStopReasonSignal:
1549f2f44ceSEd Maste   case eStopReasonException:
1559f2f44ceSEd Maste   case eStopReasonExec:
1569f2f44ceSEd Maste   case eStopReasonThreadExiting:
1579f2f44ceSEd Maste   case eStopReasonInstrumentation:
1589f2f44ceSEd Maste     return true;
1599f2f44ceSEd Maste   default:
1609f2f44ceSEd Maste     return false;
1619f2f44ceSEd Maste   }
1629f2f44ceSEd Maste }
1639f2f44ceSEd Maste 
164ac7ddfbfSEd Maste //----------------------------------------------------------------------
165ac7ddfbfSEd Maste // ThreadPlanNull
166ac7ddfbfSEd Maste //----------------------------------------------------------------------
167ac7ddfbfSEd Maste 
ThreadPlanNull(Thread & thread)168435933ddSDimitry Andric ThreadPlanNull::ThreadPlanNull(Thread &thread)
169435933ddSDimitry Andric     : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
170435933ddSDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion) {}
171ac7ddfbfSEd Maste 
1729f2f44ceSEd Maste ThreadPlanNull::~ThreadPlanNull() = default;
173ac7ddfbfSEd Maste 
GetDescription(Stream * s,lldb::DescriptionLevel level)174435933ddSDimitry Andric void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
175ac7ddfbfSEd Maste   s->PutCString("Null thread plan - thread has been destroyed.");
176ac7ddfbfSEd Maste }
177ac7ddfbfSEd Maste 
ValidatePlan(Stream * error)178435933ddSDimitry Andric bool ThreadPlanNull::ValidatePlan(Stream *error) {
179ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
180435933ddSDimitry Andric   fprintf(stderr,
181435933ddSDimitry Andric           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
182435933ddSDimitry Andric           ", ptid = 0x%" PRIx64 ")",
183435933ddSDimitry Andric           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
184ac7ddfbfSEd Maste #else
185ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
186ac7ddfbfSEd Maste   if (log)
187435933ddSDimitry Andric     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
188435933ddSDimitry Andric                ", ptid = 0x%" PRIx64 ")",
189435933ddSDimitry Andric                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
190ac7ddfbfSEd Maste                m_thread.GetProtocolID());
191ac7ddfbfSEd Maste #endif
192ac7ddfbfSEd Maste   return true;
193ac7ddfbfSEd Maste }
194ac7ddfbfSEd Maste 
ShouldStop(Event * event_ptr)195435933ddSDimitry Andric bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
196ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
197435933ddSDimitry Andric   fprintf(stderr,
198435933ddSDimitry Andric           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
199435933ddSDimitry Andric           ", ptid = 0x%" PRIx64 ")",
200435933ddSDimitry Andric           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
201ac7ddfbfSEd Maste #else
202ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
203ac7ddfbfSEd Maste   if (log)
204435933ddSDimitry Andric     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
205435933ddSDimitry Andric                ", ptid = 0x%" PRIx64 ")",
206435933ddSDimitry Andric                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
207ac7ddfbfSEd Maste                m_thread.GetProtocolID());
208ac7ddfbfSEd Maste #endif
209ac7ddfbfSEd Maste   return true;
210ac7ddfbfSEd Maste }
211ac7ddfbfSEd Maste 
WillStop()212435933ddSDimitry Andric bool ThreadPlanNull::WillStop() {
213ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
214435933ddSDimitry Andric   fprintf(stderr,
215435933ddSDimitry Andric           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
216435933ddSDimitry Andric           ", ptid = 0x%" PRIx64 ")",
217435933ddSDimitry Andric           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
218ac7ddfbfSEd Maste #else
219ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
220ac7ddfbfSEd Maste   if (log)
221435933ddSDimitry Andric     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
222435933ddSDimitry Andric                ", ptid = 0x%" PRIx64 ")",
223435933ddSDimitry Andric                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
224ac7ddfbfSEd Maste                m_thread.GetProtocolID());
225ac7ddfbfSEd Maste #endif
226ac7ddfbfSEd Maste   return true;
227ac7ddfbfSEd Maste }
228ac7ddfbfSEd Maste 
DoPlanExplainsStop(Event * event_ptr)229435933ddSDimitry Andric bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
230ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
231435933ddSDimitry Andric   fprintf(stderr,
232435933ddSDimitry Andric           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
233435933ddSDimitry Andric           ", ptid = 0x%" PRIx64 ")",
234435933ddSDimitry Andric           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
235ac7ddfbfSEd Maste #else
236ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
237ac7ddfbfSEd Maste   if (log)
238435933ddSDimitry Andric     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
239435933ddSDimitry Andric                ", ptid = 0x%" PRIx64 ")",
240435933ddSDimitry Andric                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
241ac7ddfbfSEd Maste                m_thread.GetProtocolID());
242ac7ddfbfSEd Maste #endif
243ac7ddfbfSEd Maste   return true;
244ac7ddfbfSEd Maste }
245ac7ddfbfSEd Maste 
246ac7ddfbfSEd Maste // The null plan is never done.
MischiefManaged()247435933ddSDimitry Andric bool ThreadPlanNull::MischiefManaged() {
248ac7ddfbfSEd Maste // The null plan is never done.
249ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
250435933ddSDimitry Andric   fprintf(stderr,
251435933ddSDimitry Andric           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
252435933ddSDimitry Andric           ", ptid = 0x%" PRIx64 ")",
253435933ddSDimitry Andric           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
254ac7ddfbfSEd Maste #else
255ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
256ac7ddfbfSEd Maste   if (log)
257435933ddSDimitry Andric     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
258435933ddSDimitry Andric                ", ptid = 0x%" PRIx64 ")",
259435933ddSDimitry Andric                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
260ac7ddfbfSEd Maste                m_thread.GetProtocolID());
261ac7ddfbfSEd Maste #endif
262ac7ddfbfSEd Maste   return false;
263ac7ddfbfSEd Maste }
264ac7ddfbfSEd Maste 
GetPlanRunState()265435933ddSDimitry Andric lldb::StateType ThreadPlanNull::GetPlanRunState() {
266ac7ddfbfSEd Maste // Not sure what to return here.  This is a dead thread.
267ac7ddfbfSEd Maste #ifdef LLDB_CONFIGURATION_DEBUG
268435933ddSDimitry Andric   fprintf(stderr,
269435933ddSDimitry Andric           "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
270435933ddSDimitry Andric           ", ptid = 0x%" PRIx64 ")",
271435933ddSDimitry Andric           LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
272ac7ddfbfSEd Maste #else
273ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
274ac7ddfbfSEd Maste   if (log)
275435933ddSDimitry Andric     log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
276435933ddSDimitry Andric                ", ptid = 0x%" PRIx64 ")",
277435933ddSDimitry Andric                LLVM_PRETTY_FUNCTION, m_thread.GetID(),
278ac7ddfbfSEd Maste                m_thread.GetProtocolID());
279ac7ddfbfSEd Maste #endif
280ac7ddfbfSEd Maste   return eStateRunning;
281ac7ddfbfSEd Maste }
282