Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- ThreadList.cpp ----------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [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 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 8 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include <algorithm> |
| 12 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Target/Process.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 14 | #include "lldb/Target/RegisterContext.h" |
| 15 | #include "lldb/Target/Thread.h" |
| 16 | #include "lldb/Target/ThreadList.h" |
| 17 | #include "lldb/Target/ThreadPlan.h" |
Zachary Turner | 8f186f8 | 2015-11-13 21:53:03 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/State.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | ThreadList::ThreadList(Process *process) |
| 26 | : ThreadCollection(), m_process(process), m_stop_id(0), |
| 27 | m_selected_tid(LLDB_INVALID_THREAD_ID) {} |
| 28 | |
| 29 | ThreadList::ThreadList(const ThreadList &rhs) |
| 30 | : ThreadCollection(), m_process(rhs.m_process), m_stop_id(rhs.m_stop_id), |
| 31 | m_selected_tid() { |
| 32 | // Use the assignment operator since it uses the mutex |
| 33 | *this = rhs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | const ThreadList &ThreadList::operator=(const ThreadList &rhs) { |
| 37 | if (this != &rhs) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 38 | // Lock both mutexes to make sure neither side changes anyone on us while |
| 39 | // the assignment occurs |
Jim Ingham | cdd4892 | 2019-03-29 17:07:30 +0000 | [diff] [blame] | 40 | std::lock(GetMutex(), rhs.GetMutex()); |
| 41 | std::lock_guard<std::recursive_mutex> guard(GetMutex(), std::adopt_lock); |
| 42 | std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex(), |
| 43 | std::adopt_lock); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | |
| 45 | m_process = rhs.m_process; |
| 46 | m_stop_id = rhs.m_stop_id; |
| 47 | m_threads = rhs.m_threads; |
| 48 | m_selected_tid = rhs.m_selected_tid; |
| 49 | } |
| 50 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | ThreadList::~ThreadList() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 54 | // Clear the thread list. Clear will take the mutex lock which will ensure |
| 55 | // that if anyone is using the list they won't get it removed while using it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | Clear(); |
| 57 | } |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | lldb::ThreadSP ThreadList::GetExpressionExecutionThread() { |
| 60 | if (m_expression_tid_stack.empty()) |
| 61 | return GetSelectedThread(); |
| 62 | ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back()); |
| 63 | if (expr_thread_sp) |
| 64 | return expr_thread_sp; |
| 65 | else |
| 66 | return GetSelectedThread(); |
| 67 | } |
| 68 | |
| 69 | void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) { |
| 70 | m_expression_tid_stack.push_back(tid); |
| 71 | } |
| 72 | |
| 73 | void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) { |
| 74 | assert(m_expression_tid_stack.back() == tid); |
| 75 | m_expression_tid_stack.pop_back(); |
| 76 | } |
| 77 | |
| 78 | uint32_t ThreadList::GetStopID() const { return m_stop_id; } |
| 79 | |
| 80 | void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; } |
| 81 | |
| 82 | uint32_t ThreadList::GetSize(bool can_update) { |
| 83 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 84 | |
| 85 | if (can_update) |
| 86 | m_process->UpdateThreadListIfNeeded(); |
| 87 | return m_threads.size(); |
| 88 | } |
| 89 | |
| 90 | ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) { |
| 91 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 92 | |
| 93 | if (can_update) |
| 94 | m_process->UpdateThreadListIfNeeded(); |
| 95 | |
| 96 | ThreadSP thread_sp; |
| 97 | if (idx < m_threads.size()) |
| 98 | thread_sp = m_threads[idx]; |
| 99 | return thread_sp; |
| 100 | } |
| 101 | |
| 102 | ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) { |
| 103 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 104 | |
| 105 | if (can_update) |
| 106 | m_process->UpdateThreadListIfNeeded(); |
| 107 | |
| 108 | ThreadSP thread_sp; |
| 109 | uint32_t idx = 0; |
| 110 | const uint32_t num_threads = m_threads.size(); |
| 111 | for (idx = 0; idx < num_threads; ++idx) { |
| 112 | if (m_threads[idx]->GetID() == tid) { |
| 113 | thread_sp = m_threads[idx]; |
| 114 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | } |
| 117 | return thread_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) { |
| 121 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | if (can_update) |
| 124 | m_process->UpdateThreadListIfNeeded(); |
| 125 | |
| 126 | ThreadSP thread_sp; |
| 127 | uint32_t idx = 0; |
| 128 | const uint32_t num_threads = m_threads.size(); |
| 129 | for (idx = 0; idx < num_threads; ++idx) { |
| 130 | if (m_threads[idx]->GetProtocolID() == tid) { |
| 131 | thread_sp = m_threads[idx]; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | return thread_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) { |
| 139 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 140 | |
| 141 | if (can_update) |
| 142 | m_process->UpdateThreadListIfNeeded(); |
| 143 | |
| 144 | ThreadSP thread_sp; |
| 145 | uint32_t idx = 0; |
| 146 | const uint32_t num_threads = m_threads.size(); |
| 147 | for (idx = 0; idx < num_threads; ++idx) { |
| 148 | if (m_threads[idx]->GetID() == tid) { |
| 149 | thread_sp = m_threads[idx]; |
| 150 | m_threads.erase(m_threads.begin() + idx); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | return thread_sp; |
Jim Ingham | 8d94ba0 | 2016-03-12 02:45:34 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid, |
| 158 | bool can_update) { |
| 159 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 160 | |
| 161 | if (can_update) |
| 162 | m_process->UpdateThreadListIfNeeded(); |
| 163 | |
| 164 | ThreadSP thread_sp; |
| 165 | uint32_t idx = 0; |
| 166 | const uint32_t num_threads = m_threads.size(); |
| 167 | for (idx = 0; idx < num_threads; ++idx) { |
| 168 | if (m_threads[idx]->GetProtocolID() == tid) { |
| 169 | thread_sp = m_threads[idx]; |
| 170 | m_threads.erase(m_threads.begin() + idx); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | return thread_sp; |
Jim Ingham | 8d94ba0 | 2016-03-12 02:45:34 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) { |
| 178 | ThreadSP thread_sp; |
| 179 | if (thread_ptr) { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 180 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | uint32_t idx = 0; |
| 183 | const uint32_t num_threads = m_threads.size(); |
| 184 | for (idx = 0; idx < num_threads; ++idx) { |
| 185 | if (m_threads[idx].get() == thread_ptr) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | thread_sp = m_threads[idx]; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | return thread_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Jonas Devlieghere | 8db3f7e | 2018-04-13 11:31:34 +0000 | [diff] [blame] | 194 | ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) { |
| 195 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 196 | |
| 197 | ThreadSP thread_sp; |
| 198 | const uint32_t num_threads = m_threads.size(); |
| 199 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 200 | if (m_threads[idx]->GetBackingThread() == real_thread) { |
| 201 | thread_sp = m_threads[idx]; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | return thread_sp; |
| 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) { |
| 209 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 210 | |
| 211 | if (can_update) |
| 212 | m_process->UpdateThreadListIfNeeded(); |
| 213 | |
| 214 | ThreadSP thread_sp; |
| 215 | const uint32_t num_threads = m_threads.size(); |
| 216 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 217 | if (m_threads[idx]->GetIndexID() == index_id) { |
| 218 | thread_sp = m_threads[idx]; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | return thread_sp; |
| 223 | } |
| 224 | |
| 225 | bool ThreadList::ShouldStop(Event *event_ptr) { |
| 226 | // Running events should never stop, obviously... |
| 227 | |
| 228 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 229 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 230 | // The ShouldStop method of the threads can do a whole lot of work, figuring |
| 231 | // out whether the thread plan conditions are met. So we don't want to keep |
| 232 | // the ThreadList locked the whole time we are doing this. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | // FIXME: It is possible that running code could cause new threads |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 234 | // to be created. If that happens, we will miss asking them whether they |
| 235 | // should stop. This is not a big deal since we haven't had a chance to hang |
| 236 | // any interesting operations on those threads yet. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | |
| 238 | collection threads_copy; |
| 239 | { |
| 240 | // Scope for locker |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 241 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | m_process->UpdateThreadListIfNeeded(); |
| 244 | for (lldb::ThreadSP thread_sp : m_threads) { |
| 245 | // This is an optimization... If we didn't let a thread run in between |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 246 | // the previous stop and this one, we shouldn't have to consult it for |
| 247 | // ShouldStop. So just leave it off the list we are going to inspect. On |
| 248 | // Linux, if a thread-specific conditional breakpoint was hit, it won't |
| 249 | // necessarily be the thread that hit the breakpoint itself that |
| 250 | // evaluates the conditional expression, so the thread that hit the |
| 251 | // breakpoint could still be asked to stop, even though it hasn't been |
| 252 | // allowed to run since the previous stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | if (thread_sp->GetTemporaryResumeState() != eStateSuspended || |
| 254 | thread_sp->IsStillAtLastBreakpointHit()) |
| 255 | threads_copy.push_back(thread_sp); |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | // It is possible the threads we were allowing to run all exited and then |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 259 | // maybe the user interrupted or something, then fall back on looking at |
| 260 | // all threads: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | if (threads_copy.size() == 0) |
| 263 | threads_copy = m_threads; |
| 264 | } |
| 265 | |
| 266 | collection::iterator pos, end = threads_copy.end(); |
| 267 | |
| 268 | if (log) { |
| 269 | log->PutCString(""); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 270 | LLDB_LOGF(log, |
| 271 | "ThreadList::%s: %" PRIu64 " threads, %" PRIu64 |
| 272 | " unsuspended threads", |
| 273 | __FUNCTION__, (uint64_t)m_threads.size(), |
| 274 | (uint64_t)threads_copy.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool did_anybody_stop_for_a_reason = false; |
| 278 | |
| 279 | // If the event is an Interrupt event, then we're going to stop no matter |
| 280 | // what. Otherwise, presume we won't stop. |
| 281 | bool should_stop = false; |
| 282 | if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 283 | LLDB_LOGF( |
| 284 | log, "ThreadList::%s handling interrupt event, should stop set to true", |
| 285 | __FUNCTION__); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | should_stop = true; |
| 288 | } |
| 289 | |
| 290 | // Now we run through all the threads and get their stop info's. We want to |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 291 | // make sure to do this first before we start running the ShouldStop, because |
| 292 | // one thread's ShouldStop could destroy information (like deleting a thread |
| 293 | // specific breakpoint another thread had stopped at) which could lead us to |
| 294 | // compute the StopInfo incorrectly. We don't need to use it here, we just |
| 295 | // want to make sure it gets computed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | |
| 297 | for (pos = threads_copy.begin(); pos != end; ++pos) { |
| 298 | ThreadSP thread_sp(*pos); |
| 299 | thread_sp->GetStopInfo(); |
| 300 | } |
| 301 | |
| 302 | for (pos = threads_copy.begin(); pos != end; ++pos) { |
| 303 | ThreadSP thread_sp(*pos); |
| 304 | |
| 305 | // We should never get a stop for which no thread had a stop reason, but |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 306 | // sometimes we do see this - for instance when we first connect to a |
| 307 | // remote stub. In that case we should stop, since we can't figure out the |
| 308 | // right thing to do and stopping gives the user control over what to do in |
| 309 | // this instance. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | // |
| 311 | // Note, this causes a problem when you have a thread specific breakpoint, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 312 | // and a bunch of threads hit the breakpoint, but not the thread which we |
| 313 | // are waiting for. All the threads that are not "supposed" to hit the |
| 314 | // breakpoint are marked as having no stop reason, which is right, they |
| 315 | // should not show a stop reason. But that triggers this code and causes |
| 316 | // us to stop seemingly for no reason. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | // |
| 318 | // Since the only way we ever saw this error was on first attach, I'm only |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 319 | // going to trigger set did_anybody_stop_for_a_reason to true unless this |
| 320 | // is the first stop. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 321 | // |
| 322 | // If this becomes a problem, we'll have to have another StopReason like |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 323 | // "StopInfoHidden" which will look invalid everywhere but at this check. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 324 | |
| 325 | if (thread_sp->GetProcess()->GetStopID() > 1) |
| 326 | did_anybody_stop_for_a_reason = true; |
| 327 | else |
| 328 | did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason(); |
| 329 | |
| 330 | const bool thread_should_stop = thread_sp->ShouldStop(event_ptr); |
| 331 | if (thread_should_stop) |
| 332 | should_stop |= true; |
| 333 | } |
| 334 | |
| 335 | if (!should_stop && !did_anybody_stop_for_a_reason) { |
| 336 | should_stop = true; |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 337 | LLDB_LOGF(log, |
| 338 | "ThreadList::%s we stopped but no threads had a stop reason, " |
| 339 | "overriding should_stop and stopping.", |
| 340 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 343 | LLDB_LOGF(log, "ThreadList::%s overall should_stop = %i", __FUNCTION__, |
| 344 | should_stop); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | |
| 346 | if (should_stop) { |
| 347 | for (pos = threads_copy.begin(); pos != end; ++pos) { |
| 348 | ThreadSP thread_sp(*pos); |
| 349 | thread_sp->WillStop(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return should_stop; |
| 354 | } |
| 355 | |
| 356 | Vote ThreadList::ShouldReportStop(Event *event_ptr) { |
| 357 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 358 | |
| 359 | Vote result = eVoteNoOpinion; |
| 360 | m_process->UpdateThreadListIfNeeded(); |
| 361 | collection::iterator pos, end = m_threads.end(); |
| 362 | |
| 363 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 364 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 365 | LLDB_LOGF(log, "ThreadList::%s %" PRIu64 " threads", __FUNCTION__, |
| 366 | (uint64_t)m_threads.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 367 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 368 | // Run through the threads and ask whether we should report this event. For |
| 369 | // stopping, a YES vote wins over everything. A NO vote wins over NO |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | // opinion. |
| 371 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 372 | ThreadSP thread_sp(*pos); |
| 373 | const Vote vote = thread_sp->ShouldReportStop(event_ptr); |
| 374 | switch (vote) { |
| 375 | case eVoteNoOpinion: |
| 376 | continue; |
| 377 | |
| 378 | case eVoteYes: |
| 379 | result = eVoteYes; |
| 380 | break; |
| 381 | |
| 382 | case eVoteNo: |
| 383 | if (result == eVoteNoOpinion) { |
| 384 | result = eVoteNo; |
| 385 | } else { |
Zachary Turner | df44988 | 2017-02-01 19:45:14 +0000 | [diff] [blame] | 386 | LLDB_LOG(log, |
| 387 | "Thread {0:x} voted {1}, but lost out because result was {2}", |
| 388 | thread_sp->GetID(), vote, result); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | } |
| 390 | break; |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 391 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | } |
Zachary Turner | df44988 | 2017-02-01 19:45:14 +0000 | [diff] [blame] | 393 | LLDB_LOG(log, "Returning {0}", result); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | return result; |
| 395 | } |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | void ThreadList::SetShouldReportStop(Vote vote) { |
| 398 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 399 | |
| 400 | m_process->UpdateThreadListIfNeeded(); |
| 401 | collection::iterator pos, end = m_threads.end(); |
| 402 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 403 | ThreadSP thread_sp(*pos); |
| 404 | thread_sp->SetShouldReportStop(vote); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | Vote ThreadList::ShouldReportRun(Event *event_ptr) { |
| 409 | |
| 410 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 411 | |
| 412 | Vote result = eVoteNoOpinion; |
| 413 | m_process->UpdateThreadListIfNeeded(); |
| 414 | collection::iterator pos, end = m_threads.end(); |
| 415 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 416 | // Run through the threads and ask whether we should report this event. The |
| 417 | // rule is NO vote wins over everything, a YES vote wins over no opinion. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | |
| 419 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 420 | |
| 421 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 422 | if ((*pos)->GetResumeState() != eStateSuspended) { |
| 423 | switch ((*pos)->ShouldReportRun(event_ptr)) { |
| 424 | case eVoteNoOpinion: |
| 425 | continue; |
| 426 | case eVoteYes: |
| 427 | if (result == eVoteNoOpinion) |
| 428 | result = eVoteYes; |
| 429 | break; |
| 430 | case eVoteNo: |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 431 | LLDB_LOGF(log, |
| 432 | "ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 |
| 433 | ") says don't report.", |
| 434 | (*pos)->GetIndexID(), (*pos)->GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 435 | result = eVoteNo; |
| 436 | break; |
| 437 | } |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 438 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 439 | } |
| 440 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | void ThreadList::Clear() { |
| 444 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 445 | m_stop_id = 0; |
| 446 | m_threads.clear(); |
| 447 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
| 448 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 449 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | void ThreadList::Destroy() { |
| 451 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 452 | const uint32_t num_threads = m_threads.size(); |
| 453 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 454 | m_threads[idx]->DestroyThread(); |
| 455 | } |
| 456 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 457 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | void ThreadList::RefreshStateAfterStop() { |
| 459 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | m_process->UpdateThreadListIfNeeded(); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 462 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 464 | if (log && log->GetVerbose()) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 465 | LLDB_LOGF(log, |
| 466 | "Turning off notification of new threads while single stepping " |
| 467 | "a thread."); |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 468 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | collection::iterator pos, end = m_threads.end(); |
| 470 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 471 | (*pos)->RefreshStateAfterStop(); |
| 472 | } |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | void ThreadList::DiscardThreadPlans() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 475 | // You don't need to update the thread list here, because only threads that |
| 476 | // you currently know about have any thread plans. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 477 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 478 | |
| 479 | collection::iterator pos, end = m_threads.end(); |
| 480 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 481 | (*pos)->DiscardThreadPlans(true); |
| 482 | } |
| 483 | |
| 484 | bool ThreadList::WillResume() { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 485 | // Run through the threads and perform their momentary actions. But we only |
| 486 | // do this for threads that are running, user suspended threads stay where |
| 487 | // they are. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 488 | |
| 489 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 490 | m_process->UpdateThreadListIfNeeded(); |
| 491 | |
| 492 | collection::iterator pos, end = m_threads.end(); |
| 493 | |
| 494 | // See if any thread wants to run stopping others. If it does, then we won't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 495 | // setup the other threads for resume, since they aren't going to get a |
| 496 | // chance to run. This is necessary because the SetupForResume might add |
| 497 | // "StopOthers" plans which would then get to be part of the who-gets-to-run |
| 498 | // negotiation, but they're coming in after the fact, and the threads that |
| 499 | // are already set up should take priority. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 500 | |
| 501 | bool wants_solo_run = false; |
| 502 | |
| 503 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 504 | lldbassert((*pos)->GetCurrentPlan() && |
| 505 | "thread should not have null thread plan"); |
| 506 | if ((*pos)->GetResumeState() != eStateSuspended && |
| 507 | (*pos)->GetCurrentPlan()->StopOthers()) { |
| 508 | if ((*pos)->IsOperatingSystemPluginThread() && |
| 509 | !(*pos)->GetBackingThread()) |
| 510 | continue; |
| 511 | wants_solo_run = true; |
| 512 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 513 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | if (wants_solo_run) { |
| 517 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 518 | if (log && log->GetVerbose()) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 519 | LLDB_LOGF(log, "Turning on notification of new threads while single " |
| 520 | "stepping a thread."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 521 | m_process->StartNoticingNewThreads(); |
| 522 | } else { |
| 523 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 524 | if (log && log->GetVerbose()) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 525 | LLDB_LOGF(log, "Turning off notification of new threads while single " |
| 526 | "stepping a thread."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | m_process->StopNoticingNewThreads(); |
| 528 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 529 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | // Give all the threads that are likely to run a last chance to set up their |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 531 | // state before we negotiate who is actually going to get a chance to run... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | // Don't set to resume suspended threads, and if any thread wanted to stop |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 533 | // others, only call setup on the threads that request StopOthers... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 534 | |
| 535 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 536 | if ((*pos)->GetResumeState() != eStateSuspended && |
| 537 | (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) { |
| 538 | if ((*pos)->IsOperatingSystemPluginThread() && |
| 539 | !(*pos)->GetBackingThread()) |
| 540 | continue; |
| 541 | (*pos)->SetupForResume(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // Now go through the threads and see if any thread wants to run just itself. |
| 546 | // if so then pick one and run it. |
| 547 | |
| 548 | ThreadList run_me_only_list(m_process); |
| 549 | |
| 550 | run_me_only_list.SetStopID(m_process->GetStopID()); |
| 551 | |
| 552 | bool run_only_current_thread = false; |
| 553 | |
| 554 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 555 | ThreadSP thread_sp(*pos); |
| 556 | if (thread_sp->GetResumeState() != eStateSuspended && |
| 557 | thread_sp->GetCurrentPlan()->StopOthers()) { |
| 558 | if ((*pos)->IsOperatingSystemPluginThread() && |
| 559 | !(*pos)->GetBackingThread()) |
| 560 | continue; |
| 561 | |
| 562 | // You can't say "stop others" and also want yourself to be suspended. |
| 563 | assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended); |
| 564 | |
| 565 | if (thread_sp == GetSelectedThread()) { |
| 566 | // If the currently selected thread wants to run on its own, always let |
| 567 | // it. |
| 568 | run_only_current_thread = true; |
| 569 | run_me_only_list.Clear(); |
| 570 | run_me_only_list.AddThread(thread_sp); |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | run_me_only_list.AddThread(thread_sp); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | bool need_to_resume = true; |
| 579 | |
| 580 | if (run_me_only_list.GetSize(false) == 0) { |
| 581 | // Everybody runs as they wish: |
| 582 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 583 | ThreadSP thread_sp(*pos); |
| 584 | StateType run_state; |
| 585 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 586 | run_state = thread_sp->GetCurrentPlan()->RunState(); |
| 587 | else |
| 588 | run_state = eStateSuspended; |
| 589 | if (!thread_sp->ShouldResume(run_state)) |
| 590 | need_to_resume = false; |
| 591 | } |
| 592 | } else { |
| 593 | ThreadSP thread_to_run; |
| 594 | |
| 595 | if (run_only_current_thread) { |
| 596 | thread_to_run = GetSelectedThread(); |
| 597 | } else if (run_me_only_list.GetSize(false) == 1) { |
| 598 | thread_to_run = run_me_only_list.GetThreadAtIndex(0); |
| 599 | } else { |
| 600 | int random_thread = |
| 601 | (int)((run_me_only_list.GetSize(false) * (double)rand()) / |
| 602 | (RAND_MAX + 1.0)); |
| 603 | thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread); |
| 604 | } |
| 605 | |
| 606 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 607 | ThreadSP thread_sp(*pos); |
| 608 | if (thread_sp == thread_to_run) { |
| 609 | if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState())) |
| 610 | need_to_resume = false; |
| 611 | } else |
| 612 | thread_sp->ShouldResume(eStateSuspended); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return need_to_resume; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | void ThreadList::DidResume() { |
| 620 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 621 | collection::iterator pos, end = m_threads.end(); |
| 622 | for (pos = m_threads.begin(); pos != end; ++pos) { |
| 623 | // Don't clear out threads that aren't going to get a chance to run, rather |
| 624 | // leave their state for the next time around. |
| 625 | ThreadSP thread_sp(*pos); |
| 626 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 627 | thread_sp->DidResume(); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void ThreadList::DidStop() { |
| 632 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 633 | collection::iterator pos, end = m_threads.end(); |
| 634 | for (pos = m_threads.begin(); pos != end; ++pos) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 635 | // Notify threads that the process just stopped. Note, this currently |
| 636 | // assumes that all threads in the list stop when the process stops. In |
| 637 | // the future we will want to support a debugging model where some threads |
| 638 | // continue to run while others are stopped. We either need to handle that |
| 639 | // somehow here or create a special thread list containing only threads |
| 640 | // which will stop in the code that calls this method (currently |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 641 | // Process::SetPrivateState). |
| 642 | ThreadSP thread_sp(*pos); |
| 643 | if (StateIsRunningState(thread_sp->GetState())) |
| 644 | thread_sp->DidStop(); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | ThreadSP ThreadList::GetSelectedThread() { |
| 649 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 650 | ThreadSP thread_sp = FindThreadByID(m_selected_tid); |
| 651 | if (!thread_sp.get()) { |
| 652 | if (m_threads.size() == 0) |
| 653 | return thread_sp; |
| 654 | m_selected_tid = m_threads[0]->GetID(); |
| 655 | thread_sp = m_threads[0]; |
| 656 | } |
| 657 | return thread_sp; |
| 658 | } |
| 659 | |
| 660 | bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) { |
| 661 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 662 | ThreadSP selected_thread_sp(FindThreadByID(tid)); |
| 663 | if (selected_thread_sp) { |
| 664 | m_selected_tid = tid; |
| 665 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 666 | } else |
| 667 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
| 668 | |
| 669 | if (notify) |
| 670 | NotifySelectedThreadChanged(m_selected_tid); |
| 671 | |
| 672 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
| 673 | } |
| 674 | |
| 675 | bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) { |
| 676 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 677 | ThreadSP selected_thread_sp(FindThreadByIndexID(index_id)); |
| 678 | if (selected_thread_sp.get()) { |
| 679 | m_selected_tid = selected_thread_sp->GetID(); |
| 680 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 681 | } else |
| 682 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
| 683 | |
| 684 | if (notify) |
| 685 | NotifySelectedThreadChanged(m_selected_tid); |
| 686 | |
| 687 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
| 688 | } |
| 689 | |
| 690 | void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) { |
| 691 | ThreadSP selected_thread_sp(FindThreadByID(tid)); |
| 692 | if (selected_thread_sp->EventTypeHasListeners( |
| 693 | Thread::eBroadcastBitThreadSelected)) |
| 694 | selected_thread_sp->BroadcastEvent( |
| 695 | Thread::eBroadcastBitThreadSelected, |
| 696 | new Thread::ThreadEventData(selected_thread_sp)); |
| 697 | } |
| 698 | |
| 699 | void ThreadList::Update(ThreadList &rhs) { |
| 700 | if (this != &rhs) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 701 | // Lock both mutexes to make sure neither side changes anyone on us while |
| 702 | // the assignment occurs |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 703 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | m_process = rhs.m_process; |
| 706 | m_stop_id = rhs.m_stop_id; |
| 707 | m_threads.swap(rhs.m_threads); |
| 708 | m_selected_tid = rhs.m_selected_tid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 709 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 710 | // Now we look for threads that we are done with and make sure to clear |
| 711 | // them up as much as possible so anyone with a shared pointer will still |
| 712 | // have a reference, but the thread won't be of much use. Using |
| 713 | // std::weak_ptr for all backward references (such as a thread to a |
| 714 | // process) will eventually solve this issue for us, but for now, we need |
| 715 | // to work around the issue |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 716 | collection::iterator rhs_pos, rhs_end = rhs.m_threads.end(); |
| 717 | for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) { |
Jim Ingham | 1893065 | 2020-03-18 12:05:08 -0700 | [diff] [blame] | 718 | // If this thread has already been destroyed, we don't need to look for |
| 719 | // it to destroy it again. |
| 720 | if (!(*rhs_pos)->IsValid()) |
| 721 | continue; |
| 722 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | const lldb::tid_t tid = (*rhs_pos)->GetID(); |
| 724 | bool thread_is_alive = false; |
| 725 | const uint32_t num_threads = m_threads.size(); |
| 726 | for (uint32_t idx = 0; idx < num_threads; ++idx) { |
| 727 | ThreadSP backing_thread = m_threads[idx]->GetBackingThread(); |
| 728 | if (m_threads[idx]->GetID() == tid || |
| 729 | (backing_thread && backing_thread->GetID() == tid)) { |
| 730 | thread_is_alive = true; |
| 731 | break; |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 732 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 733 | } |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 734 | if (!thread_is_alive) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 735 | (*rhs_pos)->DestroyThread(); |
Jim Ingham | 61e8e68 | 2020-03-10 16:18:11 -0700 | [diff] [blame] | 736 | } |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 737 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 741 | void ThreadList::Flush() { |
| 742 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 743 | collection::iterator pos, end = m_threads.end(); |
| 744 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 745 | (*pos)->Flush(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Greg Clayton | b5cd6e7 | 2016-12-08 20:38:19 +0000 | [diff] [blame] | 748 | std::recursive_mutex &ThreadList::GetMutex() const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | return m_process->m_thread_mutex; |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 752 | ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher( |
| 753 | lldb::ThreadSP thread_sp) |
| 754 | : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) { |
| 755 | if (thread_sp) { |
| 756 | m_tid = thread_sp->GetID(); |
| 757 | m_thread_list = &thread_sp->GetProcess()->GetThreadList(); |
| 758 | m_thread_list->PushExpressionExecutionThread(m_tid); |
| 759 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | } |