blob: 6b55f3912d11b9f35a984b52ec74b0112bcb176c [file] [log] [blame]
Raphael Isemann80814282020-01-24 07:23:271//===-- ThreadPlan.cpp ----------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:242//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Chris Lattner30fdc8d2010-06-08 16:52:246//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenkoe65b2cf2015-12-15 01:33:199#include "lldb/Target/ThreadPlan.h"
Jim Ingham06e827c2010-11-11 19:26:0910#include "lldb/Core/Debugger.h"
Jim Ingham06e827c2010-11-11 19:26:0911#include "lldb/Target/Process.h"
Kate Stoneb9c1b512016-09-06 20:57:5012#include "lldb/Target/RegisterContext.h"
Jim Ingham06e827c2010-11-11 19:26:0913#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:5014#include "lldb/Target/Thread.h"
Zachary Turner6f9e6902017-03-03 20:56:2815#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:2116#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:2417
18using namespace lldb;
19using namespace lldb_private;
20
Chris Lattner30fdc8d2010-06-08 16:52:2421// ThreadPlan constructor
Kate Stoneb9c1b512016-09-06 20:57:5022ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
Dave Lee9d3b9e52021-02-17 23:09:5023 Vote report_stop_vote, Vote report_run_vote)
Jim Ingham18930652020-03-18 19:05:0824 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()),
Dave Lee9d3b9e52021-02-17 23:09:5025 m_report_stop_vote(report_stop_vote), m_report_run_vote(report_run_vote),
Jonas Devliegheree103ae92018-11-15 01:18:1526 m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
Eric Christopher3ccd4542020-04-04 00:58:5927 m_thread(&thread), m_kind(kind), m_name(name), m_plan_complete_mutex(),
Kate Stoneb9c1b512016-09-06 20:57:5028 m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
29 m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
30 m_plan_succeeded(true) {
31 SetID(GetNextID());
Chris Lattner30fdc8d2010-06-08 16:52:2432}
33
Chris Lattner30fdc8d2010-06-08 16:52:2434// Destructor
Eugene Zelenkoe65b2cf2015-12-15 01:33:1935ThreadPlan::~ThreadPlan() = default;
Chris Lattner30fdc8d2010-06-08 16:52:2436
Jim Ingham61e8e682020-03-10 23:18:1137Target &ThreadPlan::GetTarget() { return m_process.GetTarget(); }
38
39const Target &ThreadPlan::GetTarget() const { return m_process.GetTarget(); }
40
Jim Inghame4598dc2020-03-10 21:03:5341Thread &ThreadPlan::GetThread() {
42 if (m_thread)
43 return *m_thread;
Jim Ingham18930652020-03-18 19:05:0844
Jim Inghame4598dc2020-03-10 21:03:5345 ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid);
46 m_thread = thread_sp.get();
47 return *m_thread;
48}
49
Kate Stoneb9c1b512016-09-06 20:57:5050bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
51 if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
52 bool actual_value = DoPlanExplainsStop(event_ptr);
Dave Lee606c3be2021-02-08 04:16:4853 CachePlanExplainsStop(actual_value);
Kate Stoneb9c1b512016-09-06 20:57:5054 return actual_value;
55 } else {
56 return m_cached_plan_explains_stop == eLazyBoolYes;
57 }
58}
59
60bool ThreadPlan::IsPlanComplete() {
61 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
62 return m_plan_complete;
63}
64
65void ThreadPlan::SetPlanComplete(bool success) {
66 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
67 m_plan_complete = true;
68 m_plan_succeeded = success;
69}
70
71bool ThreadPlan::MischiefManaged() {
72 std::lock_guard<std::recursive_mutex> guard(m_plan_complete_mutex);
73 // Mark the plan is complete, but don't override the success flag.
74 m_plan_complete = true;
75 return true;
76}
77
78Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
79 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
80
Dave Lee9d3b9e52021-02-17 23:09:5081 if (m_report_stop_vote == eVoteNoOpinion) {
Kate Stoneb9c1b512016-09-06 20:57:5082 ThreadPlan *prev_plan = GetPreviousPlan();
83 if (prev_plan) {
84 Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
Pavel Labath05d382c2017-02-03 18:50:4585 LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
Kate Stoneb9c1b512016-09-06 20:57:5086 return prev_vote;
Jim Ingham221d51c2013-05-08 00:35:1687 }
Kate Stoneb9c1b512016-09-06 20:57:5088 }
Dave Lee9d3b9e52021-02-17 23:09:5089 LLDB_LOG(log, "Returning vote: {0}", m_report_stop_vote);
90 return m_report_stop_vote;
Kate Stoneb9c1b512016-09-06 20:57:5091}
92
93Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
Dave Lee9d3b9e52021-02-17 23:09:5094 if (m_report_run_vote == eVoteNoOpinion) {
Kate Stoneb9c1b512016-09-06 20:57:5095 ThreadPlan *prev_plan = GetPreviousPlan();
96 if (prev_plan)
97 return prev_plan->ShouldReportRun(event_ptr);
98 }
Dave Lee9d3b9e52021-02-17 23:09:5099 return m_report_run_vote;
Kate Stoneb9c1b512016-09-06 20:57:50100}
101
Walter Erquinigo4bb62442021-01-22 18:22:26102void ThreadPlan::ClearThreadCache() { m_thread = nullptr; }
103
Kate Stoneb9c1b512016-09-06 20:57:50104bool ThreadPlan::StopOthers() {
105 ThreadPlan *prev_plan;
106 prev_plan = GetPreviousPlan();
107 return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
108}
109
110void ThreadPlan::SetStopOthers(bool new_value) {
Adrian Prantl05097242018-04-30 16:49:04111 // SetStopOthers doesn't work up the hierarchy. You have to set the explicit
112 // ThreadPlan you want to affect.
Kate Stoneb9c1b512016-09-06 20:57:50113}
114
115bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
116 m_cached_plan_explains_stop = eLazyBoolCalculate;
117
118 if (current_plan) {
119 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
120
121 if (log) {
Jim Inghame4598dc2020-03-10 21:03:53122 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
Kate Stoneb9c1b512016-09-06 20:57:50123 assert(reg_ctx);
124 addr_t pc = reg_ctx->GetPC();
125 addr_t sp = reg_ctx->GetSP();
126 addr_t fp = reg_ctx->GetFP();
Jonas Devlieghere63e5fb72019-07-24 17:56:10127 LLDB_LOGF(
128 log,
Kate Stoneb9c1b512016-09-06 20:57:50129 "%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
130 ", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
131 "plan = '%s', state = %s, stop others = %d",
Jim Ingham18930652020-03-18 19:05:08132 __FUNCTION__, GetThread().GetIndexID(),
Jim Inghame4598dc2020-03-10 21:03:53133 static_cast<void *>(&GetThread()), m_tid, static_cast<uint64_t>(pc),
Kate Stoneb9c1b512016-09-06 20:57:50134 static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
135 StateAsCString(resume_state), StopOthers());
Jim Ingham221d51c2013-05-08 00:35:16136 }
Kate Stoneb9c1b512016-09-06 20:57:50137 }
Jim Ingham18930652020-03-18 19:05:08138 bool success = DoWillResume(resume_state, current_plan);
Walter Erquinigo4bb62442021-01-22 18:22:26139 ClearThreadCache(); // We don't cache the thread pointer over resumes. This
Jim Ingham18930652020-03-18 19:05:08140 // Thread might go away, and another Thread represent
141 // the same underlying object on a later stop.
142 return success;
Jim Ingham221d51c2013-05-08 00:35:16143}
144
Kate Stoneb9c1b512016-09-06 20:57:50145lldb::user_id_t ThreadPlan::GetNextID() {
146 static uint32_t g_nextPlanID = 0;
147 return ++g_nextPlanID;
Chris Lattner30fdc8d2010-06-08 16:52:24148}
149
Kate Stoneb9c1b512016-09-06 20:57:50150void ThreadPlan::DidPush() {}
151
152void ThreadPlan::WillPop() {}
153
154bool ThreadPlan::OkayToDiscard() {
155 return IsMasterPlan() ? m_okay_to_discard : true;
Chris Lattner30fdc8d2010-06-08 16:52:24156}
157
Kate Stoneb9c1b512016-09-06 20:57:50158lldb::StateType ThreadPlan::RunState() {
Dave Lee22f0aa02021-02-13 23:48:05159 if (m_tracer_sp && m_tracer_sp->TracingEnabled())
Kate Stoneb9c1b512016-09-06 20:57:50160 return eStateStepping;
161 else
162 return GetPlanRunState();
163}
164
165bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) {
166 switch (reason) {
167 case eStopReasonWatchpoint:
168 case eStopReasonSignal:
169 case eStopReasonException:
170 case eStopReasonExec:
171 case eStopReasonThreadExiting:
172 case eStopReasonInstrumentation:
Chris Lattner30fdc8d2010-06-08 16:52:24173 return true;
Kate Stoneb9c1b512016-09-06 20:57:50174 default:
175 return false;
176 }
Jim Ingham9b03fa02015-07-23 19:55:02177}
178
Greg Clayton6e10f142013-07-30 00:23:06179// ThreadPlanNull
Greg Clayton6e10f142013-07-30 00:23:06180
Kate Stoneb9c1b512016-09-06 20:57:50181ThreadPlanNull::ThreadPlanNull(Thread &thread)
182 : ThreadPlan(ThreadPlan::eKindNull, "Null Thread Plan", thread,
183 eVoteNoOpinion, eVoteNoOpinion) {}
Greg Clayton6e10f142013-07-30 00:23:06184
Eugene Zelenkoe65b2cf2015-12-15 01:33:19185ThreadPlanNull::~ThreadPlanNull() = default;
Greg Clayton6e10f142013-07-30 00:23:06186
Kate Stoneb9c1b512016-09-06 20:57:50187void ThreadPlanNull::GetDescription(Stream *s, lldb::DescriptionLevel level) {
188 s->PutCString("Null thread plan - thread has been destroyed.");
Greg Clayton6e10f142013-07-30 00:23:06189}
190
Kate Stoneb9c1b512016-09-06 20:57:50191bool ThreadPlanNull::ValidatePlan(Stream *error) {
Greg Clayton6e10f142013-07-30 00:23:06192#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50193 fprintf(stderr,
194 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
195 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53196 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06197#else
Kate Stoneb9c1b512016-09-06 20:57:50198 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
199 if (log)
200 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
201 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53202 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06203#endif
Kate Stoneb9c1b512016-09-06 20:57:50204 return true;
Greg Clayton6e10f142013-07-30 00:23:06205}
206
Kate Stoneb9c1b512016-09-06 20:57:50207bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
Greg Clayton6e10f142013-07-30 00:23:06208#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50209 fprintf(stderr,
210 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
211 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53212 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06213#else
Kate Stoneb9c1b512016-09-06 20:57:50214 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
215 if (log)
216 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
217 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53218 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06219#endif
Kate Stoneb9c1b512016-09-06 20:57:50220 return true;
Greg Clayton6e10f142013-07-30 00:23:06221}
222
Kate Stoneb9c1b512016-09-06 20:57:50223bool ThreadPlanNull::WillStop() {
Greg Clayton6e10f142013-07-30 00:23:06224#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50225 fprintf(stderr,
226 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
227 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53228 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06229#else
Kate Stoneb9c1b512016-09-06 20:57:50230 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 ")",
Jim Inghame4598dc2020-03-10 21:03:53234 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06235#endif
Kate Stoneb9c1b512016-09-06 20:57:50236 return true;
Greg Clayton6e10f142013-07-30 00:23:06237}
238
Kate Stoneb9c1b512016-09-06 20:57:50239bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
Greg Clayton6e10f142013-07-30 00:23:06240#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50241 fprintf(stderr,
242 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
243 ", ptid = 0x%" PRIx64 ")",
Walter Erquinigo96612252020-04-04 02:49:07244 LLVM_PRETTY_FUNCTION, GetThread().GetID(), GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06245#else
Kate Stoneb9c1b512016-09-06 20:57:50246 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
247 if (log)
248 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
249 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53250 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06251#endif
Kate Stoneb9c1b512016-09-06 20:57:50252 return true;
Greg Clayton6e10f142013-07-30 00:23:06253}
254
255// The null plan is never done.
Kate Stoneb9c1b512016-09-06 20:57:50256bool ThreadPlanNull::MischiefManaged() {
257// The null plan is never done.
Greg Clayton6e10f142013-07-30 00:23:06258#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50259 fprintf(stderr,
260 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
261 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53262 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06263#else
Kate Stoneb9c1b512016-09-06 20:57:50264 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
265 if (log)
266 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
267 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53268 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06269#endif
Kate Stoneb9c1b512016-09-06 20:57:50270 return false;
Greg Clayton6e10f142013-07-30 00:23:06271}
272
Kate Stoneb9c1b512016-09-06 20:57:50273lldb::StateType ThreadPlanNull::GetPlanRunState() {
274// Not sure what to return here. This is a dead thread.
Greg Clayton6e10f142013-07-30 00:23:06275#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50276 fprintf(stderr,
277 "error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
278 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53279 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06280#else
Kate Stoneb9c1b512016-09-06 20:57:50281 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
282 if (log)
283 log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
284 ", ptid = 0x%" PRIx64 ")",
Jim Inghame4598dc2020-03-10 21:03:53285 LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
Greg Clayton6e10f142013-07-30 00:23:06286#endif
Kate Stoneb9c1b512016-09-06 20:57:50287 return eStateRunning;
Greg Clayton6e10f142013-07-30 00:23:06288}