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"
14 // Other libraries and framework includes
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/State.h"
19 #include "lldb/Target/RegisterContext.h"
20 #include "lldb/Target/Thread.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/ConvertEnum.h"
26 using namespace lldb_private;
28 //----------------------------------------------------------------------
29 // ThreadPlan constructor
30 //----------------------------------------------------------------------
31 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote stop_vote, Vote run_vote) :
33 m_stop_vote (stop_vote),
34 m_run_vote (run_vote),
37 m_plan_complete_mutex (Mutex::eMutexTypeRecursive),
38 m_cached_plan_explains_stop (eLazyBoolCalculate),
39 m_plan_complete (false),
40 m_plan_private (false),
41 m_okay_to_discard (true),
42 m_is_master_plan (false),
43 m_plan_succeeded(true)
48 //----------------------------------------------------------------------
50 //----------------------------------------------------------------------
51 ThreadPlan::~ThreadPlan()
56 ThreadPlan::PlanExplainsStop (Event *event_ptr)
58 if (m_cached_plan_explains_stop == eLazyBoolCalculate)
60 bool actual_value = DoPlanExplainsStop(event_ptr);
61 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
66 return m_cached_plan_explains_stop == eLazyBoolYes;
71 ThreadPlan::IsPlanComplete ()
73 Mutex::Locker locker(m_plan_complete_mutex);
74 return m_plan_complete;
78 ThreadPlan::SetPlanComplete (bool success)
80 Mutex::Locker locker(m_plan_complete_mutex);
81 m_plan_complete = true;
82 m_plan_succeeded = success;
86 ThreadPlan::MischiefManaged ()
88 Mutex::Locker locker(m_plan_complete_mutex);
89 // Mark the plan is complete, but don't override the success flag.
90 m_plan_complete = true;
95 ThreadPlan::ShouldReportStop (Event *event_ptr)
97 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
99 if (m_stop_vote == eVoteNoOpinion)
101 ThreadPlan *prev_plan = GetPreviousPlan ();
104 Vote prev_vote = prev_plan->ShouldReportStop (event_ptr);
106 log->Printf ("ThreadPlan::ShouldReportStop() returning previous thread plan vote: %s",
107 GetVoteAsCString (prev_vote));
112 log->Printf ("ThreadPlan::ShouldReportStop() returning vote: %s", GetVoteAsCString (m_stop_vote));
117 ThreadPlan::ShouldReportRun (Event *event_ptr)
119 if (m_run_vote == eVoteNoOpinion)
121 ThreadPlan *prev_plan = GetPreviousPlan ();
123 return prev_plan->ShouldReportRun (event_ptr);
129 ThreadPlan::StopOthers ()
131 ThreadPlan *prev_plan;
132 prev_plan = GetPreviousPlan ();
133 if (prev_plan == NULL)
136 return prev_plan->StopOthers();
140 ThreadPlan::SetStopOthers (bool new_value)
142 // SetStopOthers doesn't work up the hierarchy. You have to set the
143 // explicit ThreadPlan you want to affect.
147 ThreadPlan::WillResume (StateType resume_state, bool current_plan)
149 m_cached_plan_explains_stop = eLazyBoolCalculate;
153 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
157 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
159 addr_t pc = reg_ctx->GetPC();
160 addr_t sp = reg_ctx->GetSP();
161 addr_t fp = reg_ctx->GetFP();
162 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 ", "
163 "plan = '%s', state = %s, stop others = %d",
164 __FUNCTION__, m_thread.GetIndexID(),
165 static_cast<void*>(&m_thread), m_thread.GetID(),
166 static_cast<uint64_t>(pc), static_cast<uint64_t>(sp),
167 static_cast<uint64_t>(fp), m_name.c_str(),
168 StateAsCString(resume_state), StopOthers());
171 return DoWillResume (resume_state, current_plan);
175 ThreadPlan::GetNextID()
177 static uint32_t g_nextPlanID = 0;
178 return ++g_nextPlanID;
182 ThreadPlan::DidPush()
187 ThreadPlan::WillPop()
192 ThreadPlan::OkayToDiscard()
197 return m_okay_to_discard;
201 ThreadPlan::RunState ()
203 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && m_tracer_sp->SingleStepEnabled())
204 return eStateStepping;
206 return GetPlanRunState();
209 //----------------------------------------------------------------------
211 //----------------------------------------------------------------------
213 ThreadPlanNull::ThreadPlanNull (Thread &thread) :
214 ThreadPlan (ThreadPlan::eKindNull,
222 ThreadPlanNull::~ThreadPlanNull ()
227 ThreadPlanNull::GetDescription (Stream *s,
228 lldb::DescriptionLevel level)
230 s->PutCString("Null thread plan - thread has been destroyed.");
234 ThreadPlanNull::ValidatePlan (Stream *error)
236 #ifdef LLDB_CONFIGURATION_DEBUG
237 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
240 m_thread.GetProtocolID());
242 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
244 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
247 m_thread.GetProtocolID());
253 ThreadPlanNull::ShouldStop (Event *event_ptr)
255 #ifdef LLDB_CONFIGURATION_DEBUG
256 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
259 m_thread.GetProtocolID());
261 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
263 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
266 m_thread.GetProtocolID());
272 ThreadPlanNull::WillStop ()
274 #ifdef LLDB_CONFIGURATION_DEBUG
275 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
278 m_thread.GetProtocolID());
280 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
282 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
285 m_thread.GetProtocolID());
291 ThreadPlanNull::DoPlanExplainsStop (Event *event_ptr)
293 #ifdef LLDB_CONFIGURATION_DEBUG
294 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
297 m_thread.GetProtocolID());
299 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
301 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
304 m_thread.GetProtocolID());
309 // The null plan is never done.
311 ThreadPlanNull::MischiefManaged ()
313 // The null plan is never done.
314 #ifdef LLDB_CONFIGURATION_DEBUG
315 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
318 m_thread.GetProtocolID());
320 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
322 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
325 m_thread.GetProtocolID());
331 ThreadPlanNull::GetPlanRunState ()
333 // Not sure what to return here. This is a dead thread.
334 #ifdef LLDB_CONFIGURATION_DEBUG
335 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
338 m_thread.GetProtocolID());
340 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
342 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
345 m_thread.GetProtocolID());
347 return eStateRunning;