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