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