blob: ba35db19cf311a84e2eb17145e6ba2e395091e1e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/ThreadPlan.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham06e827c2010-11-11 19:26:09 +000018#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Log.h"
20#include "lldb/Core/State.h"
Greg Clayton2cad65a2010-09-03 17:10:42 +000021#include "lldb/Target/RegisterContext.h"
22#include "lldb/Target/Thread.h"
Jim Ingham06e827c2010-11-11 19:26:09 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
29//----------------------------------------------------------------------
30// ThreadPlan constructor
31//----------------------------------------------------------------------
Jim Inghamb01e7422010-06-19 04:45:32 +000032ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote stop_vote, Vote run_vote) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 m_thread (thread),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034 m_stop_vote (stop_vote),
35 m_run_vote (run_vote),
Benjamin Kramer1ee0d4f2010-07-16 12:32:33 +000036 m_kind (kind),
37 m_name (name),
38 m_plan_complete_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham221d51c2013-05-08 00:35:16 +000039 m_cached_plan_explains_stop (eLazyBoolCalculate),
Benjamin Kramer1ee0d4f2010-07-16 12:32:33 +000040 m_plan_complete (false),
41 m_plan_private (false),
Jim Ingham64e7ead2012-05-03 21:19:36 +000042 m_okay_to_discard (true),
Jim Inghamfbbfe6e2012-05-01 18:38:37 +000043 m_is_master_plan (false),
44 m_plan_succeeded(true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045{
46 SetID (GetNextID());
47}
48
49//----------------------------------------------------------------------
50// Destructor
51//----------------------------------------------------------------------
52ThreadPlan::~ThreadPlan()
53{
54}
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056bool
Jim Ingham221d51c2013-05-08 00:35:16 +000057ThreadPlan::PlanExplainsStop (Event *event_ptr)
58{
59 if (m_cached_plan_explains_stop == eLazyBoolCalculate)
60 {
61 bool actual_value = DoPlanExplainsStop(event_ptr);
62 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
63 return actual_value;
64 }
65 else
66 {
67 return m_cached_plan_explains_stop == eLazyBoolYes;
68 }
69}
70
71bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072ThreadPlan::IsPlanComplete ()
73{
Greg Claytonb1320972010-07-14 00:18:15 +000074 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 return m_plan_complete;
76}
77
78void
Jim Inghamfbbfe6e2012-05-01 18:38:37 +000079ThreadPlan::SetPlanComplete (bool success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080{
Greg Claytonb1320972010-07-14 00:18:15 +000081 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_plan_complete = true;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +000083 m_plan_succeeded = success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
86bool
87ThreadPlan::MischiefManaged ()
88{
Greg Claytonb1320972010-07-14 00:18:15 +000089 Mutex::Locker locker(m_plan_complete_mutex);
Jim Ingham18de2fd2012-05-10 01:35:39 +000090 // Mark the plan is complete, but don't override the success flag.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 m_plan_complete = true;
92 return true;
93}
94
95Vote
96ThreadPlan::ShouldReportStop (Event *event_ptr)
97{
Greg Clayton5160ce52013-03-27 23:08:40 +000098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +000099
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 if (m_stop_vote == eVoteNoOpinion)
101 {
102 ThreadPlan *prev_plan = GetPreviousPlan ();
103 if (prev_plan)
Greg Clayton2cad65a2010-09-03 17:10:42 +0000104 {
105 Vote prev_vote = prev_plan->ShouldReportStop (event_ptr);
106 if (log)
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000107 log->Printf ("ThreadPlan::ShouldReportStop() returning previous thread plan vote: %s",
108 GetVoteAsCString (prev_vote));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000109 return prev_vote;
110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000112 if (log)
Greg Clayton1346f7e2010-09-03 22:45:01 +0000113 log->Printf ("ThreadPlan::ShouldReportStop() returning vote: %s", GetVoteAsCString (m_stop_vote));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114 return m_stop_vote;
115}
116
117Vote
118ThreadPlan::ShouldReportRun (Event *event_ptr)
119{
120 if (m_run_vote == eVoteNoOpinion)
121 {
122 ThreadPlan *prev_plan = GetPreviousPlan ();
123 if (prev_plan)
124 return prev_plan->ShouldReportRun (event_ptr);
125 }
126 return m_run_vote;
127}
128
129bool
130ThreadPlan::StopOthers ()
131{
132 ThreadPlan *prev_plan;
133 prev_plan = GetPreviousPlan ();
134 if (prev_plan == NULL)
135 return false;
136 else
137 return prev_plan->StopOthers();
138}
139
Jim Inghamf48169b2010-11-30 02:22:11 +0000140void
141ThreadPlan::SetStopOthers (bool new_value)
142{
143 // SetStopOthers doesn't work up the hierarchy. You have to set the
144 // explicit ThreadPlan you want to affect.
145}
146
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147bool
148ThreadPlan::WillResume (StateType resume_state, bool current_plan)
149{
Jim Ingham221d51c2013-05-08 00:35:16 +0000150 m_cached_plan_explains_stop = eLazyBoolCalculate;
151
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 if (current_plan)
153 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
156 if (log)
Greg Clayton2cad65a2010-09-03 17:10:42 +0000157 {
Greg Clayton5ccbd292011-01-06 22:15:06 +0000158 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Greg Clayton2cad65a2010-09-03 17:10:42 +0000159 addr_t pc = reg_ctx->GetPC();
160 addr_t sp = reg_ctx->GetSP();
161 addr_t fp = reg_ctx->GetFP();
Jim Inghamdee1bc92013-06-22 00:27:45 +0000162 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 ", "
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000163 "plan = '%s', state = %s, stop others = %d",
Greg Clayton1346f7e2010-09-03 22:45:01 +0000164 __FUNCTION__,
Jim Inghamdee1bc92013-06-22 00:27:45 +0000165 m_thread.GetIndexID(),
166 &m_thread,
Greg Clayton2cad65a2010-09-03 17:10:42 +0000167 m_thread.GetID(),
168 (uint64_t)pc,
169 (uint64_t)sp,
170 (uint64_t)fp,
171 m_name.c_str(),
172 StateAsCString(resume_state),
173 StopOthers());
174 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 }
Jim Ingham221d51c2013-05-08 00:35:16 +0000176 return DoWillResume (resume_state, current_plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177}
178
179lldb::user_id_t
180ThreadPlan::GetNextID()
181{
182 static uint32_t g_nextPlanID = 0;
183 return ++g_nextPlanID;
184}
185
186void
187ThreadPlan::DidPush()
188{
189}
190
191void
192ThreadPlan::WillPop()
193{
194}
195
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196bool
197ThreadPlan::OkayToDiscard()
198{
199 if (!IsMasterPlan())
200 return true;
201 else
202 return m_okay_to_discard;
203}
204
Jim Ingham06e827c2010-11-11 19:26:09 +0000205lldb::StateType
206ThreadPlan::RunState ()
207{
208 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && m_tracer_sp->SingleStepEnabled())
209 return eStateStepping;
210 else
211 return GetPlanRunState();
212}