blob: 56403ef009d518a0b21f9698545acd498e25ef82 [file] [log] [blame]
Chris Lattner24943d22010-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
10#include "lldb/Target/ThreadPlan.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Jim Ingham745ac7a2010-11-11 19:26:09 +000016#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/State.h"
Greg Clayton5205f0b2010-09-03 17:10:42 +000019#include "lldb/Target/RegisterContext.h"
20#include "lldb/Target/Thread.h"
Jim Ingham745ac7a2010-11-11 19:26:09 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
27//----------------------------------------------------------------------
28// ThreadPlan constructor
29//----------------------------------------------------------------------
Jim Ingham5a47e8b2010-06-19 04:45:32 +000030ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote stop_vote, Vote run_vote) :
Chris Lattner24943d22010-06-08 16:52:24 +000031 m_thread (thread),
Chris Lattner24943d22010-06-08 16:52:24 +000032 m_stop_vote (stop_vote),
33 m_run_vote (run_vote),
Benjamin Kramer36a08102010-07-16 12:32:33 +000034 m_kind (kind),
35 m_name (name),
36 m_plan_complete_mutex (Mutex::eMutexTypeRecursive),
37 m_plan_complete (false),
38 m_plan_private (false),
Chris Lattner24943d22010-06-08 16:52:24 +000039 m_okay_to_discard (false)
40{
41 SetID (GetNextID());
42}
43
44//----------------------------------------------------------------------
45// Destructor
46//----------------------------------------------------------------------
47ThreadPlan::~ThreadPlan()
48{
49}
50
51const char *
52ThreadPlan::GetName () const
53{
54 return m_name.c_str();
55}
56
57Thread &
58ThreadPlan::GetThread()
59{
60 return m_thread;
61}
62
63
64const Thread &
65ThreadPlan::GetThread() const
66{
67 return m_thread;
68}
69
70bool
71ThreadPlan::IsPlanComplete ()
72{
Greg Claytonbef15832010-07-14 00:18:15 +000073 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000074 return m_plan_complete;
75}
76
77void
78ThreadPlan::SetPlanComplete ()
79{
Greg Claytonbef15832010-07-14 00:18:15 +000080 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000081 m_plan_complete = true;
82}
83
84bool
85ThreadPlan::MischiefManaged ()
86{
Greg Claytonbef15832010-07-14 00:18:15 +000087 Mutex::Locker locker(m_plan_complete_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000088 m_plan_complete = true;
89 return true;
90}
91
92Vote
93ThreadPlan::ShouldReportStop (Event *event_ptr)
94{
Greg Claytone005f2c2010-11-06 01:53:30 +000095 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +000096
Chris Lattner24943d22010-06-08 16:52:24 +000097 if (m_stop_vote == eVoteNoOpinion)
98 {
99 ThreadPlan *prev_plan = GetPreviousPlan ();
100 if (prev_plan)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000101 {
102 Vote prev_vote = prev_plan->ShouldReportStop (event_ptr);
103 if (log)
Greg Claytonf04d6612010-09-03 22:45:01 +0000104 log->Printf ("ThreadPlan::ShouldReportStop() returning previous thread plan vote: %s", GetVoteAsCString (prev_vote));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000105 return prev_vote;
106 }
Chris Lattner24943d22010-06-08 16:52:24 +0000107 }
Greg Clayton5205f0b2010-09-03 17:10:42 +0000108 if (log)
Greg Claytonf04d6612010-09-03 22:45:01 +0000109 log->Printf ("ThreadPlan::ShouldReportStop() returning vote: %s", GetVoteAsCString (m_stop_vote));
Chris Lattner24943d22010-06-08 16:52:24 +0000110 return m_stop_vote;
111}
112
113Vote
114ThreadPlan::ShouldReportRun (Event *event_ptr)
115{
116 if (m_run_vote == eVoteNoOpinion)
117 {
118 ThreadPlan *prev_plan = GetPreviousPlan ();
119 if (prev_plan)
120 return prev_plan->ShouldReportRun (event_ptr);
121 }
122 return m_run_vote;
123}
124
125bool
126ThreadPlan::StopOthers ()
127{
128 ThreadPlan *prev_plan;
129 prev_plan = GetPreviousPlan ();
130 if (prev_plan == NULL)
131 return false;
132 else
133 return prev_plan->StopOthers();
134}
135
Jim Ingham360f53f2010-11-30 02:22:11 +0000136void
137ThreadPlan::SetStopOthers (bool new_value)
138{
139 // SetStopOthers doesn't work up the hierarchy. You have to set the
140 // explicit ThreadPlan you want to affect.
141}
142
Chris Lattner24943d22010-06-08 16:52:24 +0000143bool
144ThreadPlan::WillResume (StateType resume_state, bool current_plan)
145{
146 if (current_plan)
147 {
Greg Claytone005f2c2010-11-06 01:53:30 +0000148 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000149
150 if (log)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000151 {
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000152 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Greg Clayton5205f0b2010-09-03 17:10:42 +0000153 addr_t pc = reg_ctx->GetPC();
154 addr_t sp = reg_ctx->GetSP();
155 addr_t fp = reg_ctx->GetFP();
Greg Claytonf04d6612010-09-03 22:45:01 +0000156 log->Printf("%s Thread #%u: tid = 0x%4.4x, pc = 0x%8.8llx, sp = 0x%8.8llx, fp = 0x%8.8llx, plan = '%s', state = %s, stop others = %d",
157 __FUNCTION__,
Greg Clayton5205f0b2010-09-03 17:10:42 +0000158 m_thread.GetIndexID(),
159 m_thread.GetID(),
160 (uint64_t)pc,
161 (uint64_t)sp,
162 (uint64_t)fp,
163 m_name.c_str(),
164 StateAsCString(resume_state),
165 StopOthers());
166 }
Chris Lattner24943d22010-06-08 16:52:24 +0000167 }
168 return true;
169}
170
171lldb::user_id_t
172ThreadPlan::GetNextID()
173{
174 static uint32_t g_nextPlanID = 0;
175 return ++g_nextPlanID;
176}
177
178void
179ThreadPlan::DidPush()
180{
181}
182
183void
184ThreadPlan::WillPop()
185{
186}
187
188void
189ThreadPlan::PushPlan (ThreadPlanSP &thread_plan_sp)
190{
191 m_thread.PushPlan (thread_plan_sp);
192}
193
194ThreadPlan *
195ThreadPlan::GetPreviousPlan ()
196{
197 return m_thread.GetPreviousPlan (this);
198}
199
200void
201ThreadPlan::SetPrivate (bool input)
202{
203 m_plan_private = input;
204}
205
206bool
207ThreadPlan::GetPrivate (void)
208{
209 return m_plan_private;
210}
211
212bool
213ThreadPlan::OkayToDiscard()
214{
215 if (!IsMasterPlan())
216 return true;
217 else
218 return m_okay_to_discard;
219}
220
Jim Ingham745ac7a2010-11-11 19:26:09 +0000221lldb::StateType
222ThreadPlan::RunState ()
223{
224 if (m_tracer_sp && m_tracer_sp->TracingEnabled() && m_tracer_sp->SingleStepEnabled())
225 return eStateStepping;
226 else
227 return GetPlanRunState();
228}