blob: 01d75af49e1cbb92519293d767f2a00b42f481b8 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000014#include "lldb/Target/ThreadPlan.h"
Jim Ingham06e827c2010-11-11 19:26:09 +000015#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
17#include "lldb/Core/State.h"
Jim Ingham06e827c2010-11-11 19:26:09 +000018#include "lldb/Target/Process.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Target/RegisterContext.h"
Jim Ingham06e827c2010-11-11 19:26:09 +000020#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------
27// ThreadPlan constructor
28//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000029ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
30 Vote stop_vote, Vote run_vote)
31 : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
32 m_kind(kind), m_name(name), m_plan_complete_mutex(),
33 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
34 m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
35 m_plan_succeeded(true) {
36 SetID(GetNextID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
39//----------------------------------------------------------------------
40// Destructor
41//----------------------------------------------------------------------
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000042ThreadPlan::~ThreadPlan() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
45 if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
46 bool actual_value = DoPlanExplainsStop(event_ptr);
47 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
48 return actual_value;
49 } else {
50 return m_cached_plan_explains_stop == eLazyBoolYes;
51 }
52}
53
54bool ThreadPlan::IsPlanComplete() {
55 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
56 return m_plan_complete;
57}
58
59void ThreadPlan::SetPlanComplete(bool success) {
60 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
61 m_plan_complete = true;
62 m_plan_succeeded = success;
63}
64
65bool ThreadPlan::MischiefManaged() {
66 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
67 // Mark the plan is complete, but don't override the success flag.
68 m_plan_complete = true;
69 return true;
70}
71
72Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
73 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
74
75 if (m_stop_vote == eVoteNoOpinion) {
76 ThreadPlan *prev_plan = GetPreviousPlan();
77 if (prev_plan) {
78 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
Pavel Labath05d382c2017-02-03 18:50:45 +000079 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 return prev_vote;
Jim Ingham221d51c2013-05-08 00:35:16 +000081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
Pavel Labath05d382c2017-02-03 18:50:45 +000083 LLDB_LOG(log, "Returning vote: {0}", m_stop_vote);
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 return m_stop_vote;
85}
86
87Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
88 if (m_run_vote == eVoteNoOpinion) {
89 ThreadPlan *prev_plan = GetPreviousPlan();
90 if (prev_plan)
91 return prev_plan->ShouldReportRun(event_ptr);
92 }
93 return m_run_vote;
94}
95
96bool ThreadPlan::StopOthers() {
97 ThreadPlan *prev_plan;
98 prev_plan = GetPreviousPlan();
99 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
100}
101
102void ThreadPlan::SetStopOthers(bool new_value) {
103 // SetStopOthers doesn't work up the hierarchy. You have to set the
104 // explicit ThreadPlan you want to affect.
105}
106
107bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
108 m_cached_plan_explains_stop = eLazyBoolCalculate;
109
110 if (current_plan) {
111 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
112
113 if (log) {
114 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
115 assert(reg_ctx);
116 addr_t pc = reg_ctx->GetPC();
117 addr_t sp = reg_ctx->GetSP();
118 addr_t fp = reg_ctx->GetFP();
119 log->Printf(
120 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
121 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
122 "plan = '%s', state = %s, stop others = %d",
123 __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
124 m_thread.GetID(), static_cast<uint64_t>(pc),
125 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
126 StateAsCString(resume_state), StopOthers());
Jim Ingham221d51c2013-05-08 00:35:16 +0000127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 }
129 return DoWillResume(resume_state, current_plan);
Jim Ingham221d51c2013-05-08 00:35:16 +0000130}
131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132lldb::user_id_t ThreadPlan::GetNextID() {
133 static uint32_t g_nextPlanID = 0;
134 return ++g_nextPlanID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137void ThreadPlan::DidPush() {}
138
139void ThreadPlan::WillPop() {}
140
141bool ThreadPlan::OkayToDiscard() {
142 return IsMasterPlan() ? m_okay_to_discard : true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145lldb::StateType ThreadPlan::RunState() {
146 if (m_tracer_sp && m_tracer_sp->TracingEnabled() &&
147 m_tracer_sp->SingleStepEnabled())
148 return eStateStepping;
149 else
150 return GetPlanRunState();
151}
152
153bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
154 switch (reason) {
155 case eStopReasonWatchpoint:
156 case eStopReasonSignal:
157 case eStopReasonException:
158 case eStopReasonExec:
159 case eStopReasonThreadExiting:
160 case eStopReasonInstrumentation:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 default:
163 return false;
164 }
Jim Ingham9b03fa02015-07-23 19:55:02 +0000165}
166
Greg Clayton6e10f142013-07-30 00:23:06 +0000167//----------------------------------------------------------------------
168// ThreadPlanNull
169//----------------------------------------------------------------------
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171ThreadPlanNull::ThreadPlanNull(Thread &thread)
172 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
173 eVoteNoOpinion, eVoteNoOpinion) {}
Greg Clayton6e10f142013-07-30 00:23:06 +0000174
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000175ThreadPlanNull::~ThreadPlanNull() = default;
Greg Clayton6e10f142013-07-30 00:23:06 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
178 s->PutCString("Null thread plan - thread has been destroyed.");
Greg Clayton6e10f142013-07-30 00:23:06 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181bool ThreadPlanNull::ValidatePlan(Stream *error) {
Greg Clayton6e10f142013-07-30 00:23:06 +0000182#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 fprintf(stderr,
184 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
185 ", ptid = 0x%" PRIx64 ")",
186 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000187#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
189 if (log)
190 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
191 ", ptid = 0x%" PRIx64 ")",
192 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
193 m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000194#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 return true;
Greg Clayton6e10f142013-07-30 00:23:06 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
Greg Clayton6e10f142013-07-30 00:23:06 +0000199#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 fprintf(stderr,
201 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
202 ", ptid = 0x%" PRIx64 ")",
203 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000204#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
206 if (log)
207 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
208 ", ptid = 0x%" PRIx64 ")",
209 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
210 m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000211#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 return true;
Greg Clayton6e10f142013-07-30 00:23:06 +0000213}
214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215bool ThreadPlanNull::WillStop() {
Greg Clayton6e10f142013-07-30 00:23:06 +0000216#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 fprintf(stderr,
218 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
219 ", ptid = 0x%" PRIx64 ")",
220 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000221#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
223 if (log)
224 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
225 ", ptid = 0x%" PRIx64 ")",
226 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
227 m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000228#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 return true;
Greg Clayton6e10f142013-07-30 00:23:06 +0000230}
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
Greg Clayton6e10f142013-07-30 00:23:06 +0000233#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 fprintf(stderr,
235 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
236 ", ptid = 0x%" PRIx64 ")",
237 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000238#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
240 if (log)
241 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
242 ", ptid = 0x%" PRIx64 ")",
243 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
244 m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000245#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 return true;
Greg Clayton6e10f142013-07-30 00:23:06 +0000247}
248
249// The null plan is never done.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250bool ThreadPlanNull::MischiefManaged() {
251// The null plan is never done.
Greg Clayton6e10f142013-07-30 00:23:06 +0000252#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 fprintf(stderr,
254 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
255 ", ptid = 0x%" PRIx64 ")",
256 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000257#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
259 if (log)
260 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
261 ", ptid = 0x%" PRIx64 ")",
262 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
263 m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000264#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 return false;
Greg Clayton6e10f142013-07-30 00:23:06 +0000266}
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268lldb::StateType ThreadPlanNull::GetPlanRunState() {
269// Not sure what to return here. This is a dead thread.
Greg Clayton6e10f142013-07-30 00:23:06 +0000270#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 fprintf(stderr,
272 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
273 ", ptid = 0x%" PRIx64 ")",
274 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000275#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
277 if (log)
278 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
279 ", ptid = 0x%" PRIx64 ")",
280 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
281 m_thread.GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06 +0000282#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 return eStateRunning;
Greg Clayton6e10f142013-07-30 00:23:06 +0000284}