1 //===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Target/ThreadPlan.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/RegisterContext.h"
13 #include "lldb/Target/Target.h"
14 #include "lldb/Target/Thread.h"
15 #include "lldb/Utility/Log.h"
16 #include "lldb/Utility/State.h"
17
18 using namespace lldb;
19 using namespace lldb_private;
20
21 // ThreadPlan constructor
ThreadPlan(ThreadPlanKind kind,const char * name,Thread & thread,Vote stop_vote,Vote run_vote)22 ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
23 Vote stop_vote, Vote run_vote)
24 : m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
25 m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
26 m_kind(kind), m_name(name), m_plan_complete_mutex(),
27 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
28 m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
29 m_plan_succeeded(true) {
30 SetID(GetNextID());
31 }
32
33 // Destructor
34 ThreadPlan::~ThreadPlan() = default;
35
PlanExplainsStop(Event * event_ptr)36 bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
37 if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
38 bool actual_value = DoPlanExplainsStop(event_ptr);
39 m_cached_plan_explains_stop = actual_value ? eLazyBoolYes : eLazyBoolNo;
40 return actual_value;
41 } else {
42 return m_cached_plan_explains_stop == eLazyBoolYes;
43 }
44 }
45
IsPlanComplete()46 bool ThreadPlan::IsPlanComplete() {
47 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
48 return m_plan_complete;
49 }
50
SetPlanComplete(bool success)51 void ThreadPlan::SetPlanComplete(bool success) {
52 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
53 m_plan_complete = true;
54 m_plan_succeeded = success;
55 }
56
MischiefManaged()57 bool ThreadPlan::MischiefManaged() {
58 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
59 // Mark the plan is complete, but don't override the success flag.
60 m_plan_complete = true;
61 return true;
62 }
63
ShouldReportStop(Event * event_ptr)64 Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
65 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
66
67 if (m_stop_vote == eVoteNoOpinion) {
68 ThreadPlan *prev_plan = GetPreviousPlan();
69 if (prev_plan) {
70 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
71 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
72 return prev_vote;
73 }
74 }
75 LLDB_LOG(log, "Returning vote: {0}", m_stop_vote);
76 return m_stop_vote;
77 }
78
ShouldReportRun(Event * event_ptr)79 Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
80 if (m_run_vote == eVoteNoOpinion) {
81 ThreadPlan *prev_plan = GetPreviousPlan();
82 if (prev_plan)
83 return prev_plan->ShouldReportRun(event_ptr);
84 }
85 return m_run_vote;
86 }
87
StopOthers()88 bool ThreadPlan::StopOthers() {
89 ThreadPlan *prev_plan;
90 prev_plan = GetPreviousPlan();
91 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
92 }
93
SetStopOthers(bool new_value)94 void ThreadPlan::SetStopOthers(bool new_value) {
95 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit
96 // ThreadPlan you want to affect.
97 }
98
WillResume(StateType resume_state,bool current_plan)99 bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
100 m_cached_plan_explains_stop = eLazyBoolCalculate;
101
102 if (current_plan) {
103 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
104
105 if (log) {
106 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
107 assert(reg_ctx);
108 addr_t pc = reg_ctx->GetPC();
109 addr_t sp = reg_ctx->GetSP();
110 addr_t fp = reg_ctx->GetFP();
111 LLDB_LOGF(
112 log,
113 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
114 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
115 "plan = '%s', state = %s, stop others = %d",
116 __FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
117 m_thread.GetID(), static_cast<uint64_t>(pc),
118 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
119 StateAsCString(resume_state), StopOthers());
120 }
121 }
122 return DoWillResume(resume_state, current_plan);
123 }
124
GetNextID()125 lldb::user_id_t ThreadPlan::GetNextID() {
126 static uint32_t g_nextPlanID = 0;
127 return ++g_nextPlanID;
128 }
129
DidPush()130 void ThreadPlan::DidPush() {}
131
WillPop()132 void ThreadPlan::WillPop() {}
133
OkayToDiscard()134 bool ThreadPlan::OkayToDiscard() {
135 return IsMasterPlan() ? m_okay_to_discard : true;
136 }
137
RunState()138 lldb::StateType ThreadPlan::RunState() {
139 if (m_tracer_sp && m_tracer_sp->TracingEnabled() &&
140 m_tracer_sp->SingleStepEnabled())
141 return eStateStepping;
142 else
143 return GetPlanRunState();
144 }
145
IsUsuallyUnexplainedStopReason(lldb::StopReason reason)146 bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
147 switch (reason) {
148 case eStopReasonWatchpoint:
149 case eStopReasonSignal:
150 case eStopReasonException:
151 case eStopReasonExec:
152 case eStopReasonThreadExiting:
153 case eStopReasonInstrumentation:
154 return true;
155 default:
156 return false;
157 }
158 }
159
160 // ThreadPlanNull
161
ThreadPlanNull(Thread & thread)162 ThreadPlanNull::ThreadPlanNull(Thread &thread)
163 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
164 eVoteNoOpinion, eVoteNoOpinion) {}
165
166 ThreadPlanNull::~ThreadPlanNull() = default;
167
GetDescription(Stream * s,lldb::DescriptionLevel level)168 void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
169 s->PutCString("Null thread plan - thread has been destroyed.");
170 }
171
ValidatePlan(Stream * error)172 bool ThreadPlanNull::ValidatePlan(Stream *error) {
173 #ifdef LLDB_CONFIGURATION_DEBUG
174 fprintf(stderr,
175 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
176 ", ptid = 0x%" PRIx64 ")",
177 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
178 #else
179 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
180 if (log)
181 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
182 ", ptid = 0x%" PRIx64 ")",
183 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
184 m_thread.GetProtocolID());
185 #endif
186 return true;
187 }
188
ShouldStop(Event * event_ptr)189 bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
190 #ifdef LLDB_CONFIGURATION_DEBUG
191 fprintf(stderr,
192 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
193 ", ptid = 0x%" PRIx64 ")",
194 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
195 #else
196 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
197 if (log)
198 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
199 ", ptid = 0x%" PRIx64 ")",
200 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
201 m_thread.GetProtocolID());
202 #endif
203 return true;
204 }
205
WillStop()206 bool ThreadPlanNull::WillStop() {
207 #ifdef LLDB_CONFIGURATION_DEBUG
208 fprintf(stderr,
209 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
210 ", ptid = 0x%" PRIx64 ")",
211 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
212 #else
213 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
214 if (log)
215 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
216 ", ptid = 0x%" PRIx64 ")",
217 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
218 m_thread.GetProtocolID());
219 #endif
220 return true;
221 }
222
DoPlanExplainsStop(Event * event_ptr)223 bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
224 #ifdef LLDB_CONFIGURATION_DEBUG
225 fprintf(stderr,
226 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
227 ", ptid = 0x%" PRIx64 ")",
228 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
229 #else
230 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
231 if (log)
232 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
233 ", ptid = 0x%" PRIx64 ")",
234 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
235 m_thread.GetProtocolID());
236 #endif
237 return true;
238 }
239
240 // The null plan is never done.
MischiefManaged()241 bool ThreadPlanNull::MischiefManaged() {
242 // The null plan is never done.
243 #ifdef LLDB_CONFIGURATION_DEBUG
244 fprintf(stderr,
245 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
246 ", ptid = 0x%" PRIx64 ")",
247 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
248 #else
249 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
250 if (log)
251 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
252 ", ptid = 0x%" PRIx64 ")",
253 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
254 m_thread.GetProtocolID());
255 #endif
256 return false;
257 }
258
GetPlanRunState()259 lldb::StateType ThreadPlanNull::GetPlanRunState() {
260 // Not sure what to return here. This is a dead thread.
261 #ifdef LLDB_CONFIGURATION_DEBUG
262 fprintf(stderr,
263 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
264 ", ptid = 0x%" PRIx64 ")",
265 LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
266 #else
267 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
268 if (log)
269 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
270 ", ptid = 0x%" PRIx64 ")",
271 LLVM_PRETTY_FUNCTION, m_thread.GetID(),
272 m_thread.GetProtocolID());
273 #endif
274 return eStateRunning;
275 }
276