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/lldb-python.h"
12 #include "lldb/Target/ThreadPlan.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/State.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
27 using namespace lldb_private;
29 //----------------------------------------------------------------------
30 // ThreadPlan constructor
31 //----------------------------------------------------------------------
32 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote stop_vote, Vote run_vote) :
34 m_stop_vote (stop_vote),
35 m_run_vote (run_vote),
38 m_plan_complete_mutex (Mutex::eMutexTypeRecursive),
39 m_cached_plan_explains_stop (eLazyBoolCalculate),
40 m_plan_complete (false),
41 m_plan_private (false),
42 m_okay_to_discard (true),
43 m_is_master_plan (false),
44 m_plan_succeeded(true)
49 //----------------------------------------------------------------------
51 //----------------------------------------------------------------------
52 ThreadPlan::~ThreadPlan()
57 ThreadPlan::PlanExplainsStop (Event *event_ptr)
59 if (m_cached_plan_explains_stop == eLazyBoolCalculate)
61 bool actual_value = DoPlanExplainsStop(event_ptr);
62 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
67 return m_cached_plan_explains_stop == eLazyBoolYes;
72 ThreadPlan::IsPlanComplete ()
74 Mutex::Locker locker(m_plan_complete_mutex);
75 return m_plan_complete;
79 ThreadPlan::SetPlanComplete (bool success)
81 Mutex::Locker locker(m_plan_complete_mutex);
82 m_plan_complete = true;
83 m_plan_succeeded = success;
87 ThreadPlan::MischiefManaged ()
89 Mutex::Locker locker(m_plan_complete_mutex);
90 // Mark the plan is complete, but don't override the success flag.
91 m_plan_complete = true;
96 ThreadPlan::ShouldReportStop (Event *event_ptr)
98 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
100 if (m_stop_vote == eVoteNoOpinion)
102 ThreadPlan *prev_plan = GetPreviousPlan ();
105 Vote prev_vote = prev_plan->ShouldReportStop (event_ptr);
107 log->Printf ("ThreadPlan::ShouldReportStop() returning previous thread plan vote: %s",
108 GetVoteAsCString (prev_vote));
113 log->Printf ("ThreadPlan::ShouldReportStop() returning vote: %s", GetVoteAsCString (m_stop_vote));
118 ThreadPlan::ShouldReportRun (Event *event_ptr)
120 if (m_run_vote == eVoteNoOpinion)
122 ThreadPlan *prev_plan = GetPreviousPlan ();
124 return prev_plan->ShouldReportRun (event_ptr);
130 ThreadPlan::StopOthers ()
132 ThreadPlan *prev_plan;
133 prev_plan = GetPreviousPlan ();
134 if (prev_plan == NULL)
137 return prev_plan->StopOthers();
141 ThreadPlan::SetStopOthers (bool new_value)
143 // SetStopOthers doesn't work up the hierarchy. You have to set the
144 // explicit ThreadPlan you want to affect.
148 ThreadPlan::WillResume (StateType resume_state, bool current_plan)
150 m_cached_plan_explains_stop = eLazyBoolCalculate;
154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
158 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",
165 m_thread.GetIndexID(),
172 StateAsCString(resume_state),
176 return DoWillResume (resume_state, current_plan);
180 ThreadPlan::GetNextID()
182 static uint32_t g_nextPlanID = 0;
183 return ++g_nextPlanID;
187 ThreadPlan::DidPush()
192 ThreadPlan::WillPop()
197 ThreadPlan::OkayToDiscard()
202 return m_okay_to_discard;
206 ThreadPlan::RunState ()
208 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && m_tracer_sp->SingleStepEnabled())
209 return eStateStepping;
211 return GetPlanRunState();
214 //----------------------------------------------------------------------
216 //----------------------------------------------------------------------
218 ThreadPlanNull::ThreadPlanNull (Thread &thread) :
219 ThreadPlan (ThreadPlan::eKindNull,
227 ThreadPlanNull::~ThreadPlanNull ()
232 ThreadPlanNull::GetDescription (Stream *s,
233 lldb::DescriptionLevel level)
235 s->PutCString("Null thread plan - thread has been destroyed.");
239 ThreadPlanNull::ValidatePlan (Stream *error)
241 #ifdef LLDB_CONFIGURATION_DEBUG
242 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
245 m_thread.GetProtocolID());
247 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
249 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
252 m_thread.GetProtocolID());
258 ThreadPlanNull::ShouldStop (Event *event_ptr)
260 #ifdef LLDB_CONFIGURATION_DEBUG
261 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
264 m_thread.GetProtocolID());
266 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
268 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
271 m_thread.GetProtocolID());
277 ThreadPlanNull::WillStop ()
279 #ifdef LLDB_CONFIGURATION_DEBUG
280 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
283 m_thread.GetProtocolID());
285 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
287 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
290 m_thread.GetProtocolID());
296 ThreadPlanNull::DoPlanExplainsStop (Event *event_ptr)
298 #ifdef LLDB_CONFIGURATION_DEBUG
299 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
302 m_thread.GetProtocolID());
304 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
306 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
309 m_thread.GetProtocolID());
314 // The null plan is never done.
316 ThreadPlanNull::MischiefManaged ()
318 // The null plan is never done.
319 #ifdef LLDB_CONFIGURATION_DEBUG
320 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
323 m_thread.GetProtocolID());
325 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
327 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
330 m_thread.GetProtocolID());
336 ThreadPlanNull::GetPlanRunState ()
338 // Not sure what to return here. This is a dead thread.
339 #ifdef LLDB_CONFIGURATION_DEBUG
340 fprintf(stderr, "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
343 m_thread.GetProtocolID());
345 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
347 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64 ", ptid = 0x%" PRIx64 ")",
350 m_thread.GetProtocolID());
352 return eStateRunning;