blob: 032dcc9e5fbd868f0da7d8f3bd1685298a1b9336 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- ThreadList.cpp ----------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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:24 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00008
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009#include <stdlib.h>
10
11#include <algorithm>
12
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Target/Process.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include "lldb/Target/RegisterContext.h"
15#include "lldb/Target/Thread.h"
16#include "lldb/Target/ThreadList.h"
17#include "lldb/Target/ThreadPlan.h"
Zachary Turner8f186f82015-11-13 21:53:03 +000018#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000019#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000020#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
22using namespace lldb;
23using namespace lldb_private;
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025ThreadList::ThreadList(Process *process)
26 : ThreadCollection(), m_process(process), m_stop_id(0),
27 m_selected_tid(LLDB_INVALID_THREAD_ID) {}
28
29ThreadList::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 Lattner30fdc8d2010-06-08 16:52:24 +000034}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036const ThreadList &ThreadList::operator=(const ThreadList &rhs) {
37 if (this != &rhs) {
Adrian Prantl05097242018-04-30 16:49:04 +000038 // Lock both mutexes to make sure neither side changes anyone on us while
39 // the assignment occurs
Jim Inghamcdd48922019-03-29 17:07:30 +000040 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 Stoneb9c1b512016-09-06 20:57:50 +000044
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 Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053ThreadList::~ThreadList() {
Adrian Prantl05097242018-04-30 16:49:04 +000054 // 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 Stoneb9c1b512016-09-06 20:57:50 +000056 Clear();
57}
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059lldb::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
69void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
70 m_expression_tid_stack.push_back(tid);
71}
72
73void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
74 assert(m_expression_tid_stack.back() == tid);
75 m_expression_tid_stack.pop_back();
76}
77
78uint32_t ThreadList::GetStopID() const { return m_stop_id; }
79
80void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
81
82uint32_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
90ThreadSP 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
102ThreadSP 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 Lattner30fdc8d2010-06-08 16:52:24 +0000115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 }
117 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118}
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
121 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 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 Lattner30fdc8d2010-06-08 16:52:24 +0000136}
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138ThreadSP 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 Ingham8d94ba02016-03-12 02:45:34 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157ThreadSP 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 Ingham8d94ba02016-03-12 02:45:34 +0000175}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
178 ThreadSP thread_sp;
179 if (thread_ptr) {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000180 std::lock_guard<std::recursive_mutex> guard(GetMutex());
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 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 Lattner30fdc8d2010-06-08 16:52:24 +0000186 thread_sp = m_threads[idx];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 break;
188 }
189 }
190 }
191 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
Jonas Devlieghere8db3f7e2018-04-13 11:31:34 +0000194ThreadSP 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 Stoneb9c1b512016-09-06 20:57:50 +0000208ThreadSP 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
225bool ThreadList::ShouldStop(Event *event_ptr) {
226 // Running events should never stop, obviously...
227
228 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
229
Adrian Prantl05097242018-04-30 16:49:04 +0000230 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000233 // FIXME: It is possible that running code could cause new threads
Adrian Prantl05097242018-04-30 16:49:04 +0000234 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000237
238 collection threads_copy;
239 {
240 // Scope for locker
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000241 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 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 Prantl05097242018-04-30 16:49:04 +0000246 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000253 if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
254 thread_sp->IsStillAtLastBreakpointHit())
255 threads_copy.push_back(thread_sp);
Jim Inghamb42f3af2012-11-15 22:44:04 +0000256 }
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 // It is possible the threads we were allowing to run all exited and then
Adrian Prantl05097242018-04-30 16:49:04 +0000259 // maybe the user interrupted or something, then fall back on looking at
260 // all threads:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 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 Devlieghere63e5fb72019-07-24 17:56:10 +0000270 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 Stoneb9c1b512016-09-06 20:57:50 +0000275 }
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 Devlieghere63e5fb72019-07-24 17:56:10 +0000283 LLDB_LOGF(
284 log, "ThreadList::%s handling interrupt event, should stop set to true",
285 __FUNCTION__);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 should_stop = true;
288 }
289
290 // Now we run through all the threads and get their stop info's. We want to
Adrian Prantl05097242018-04-30 16:49:04 +0000291 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000296
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 Prantl05097242018-04-30 16:49:04 +0000306 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000310 //
311 // Note, this causes a problem when you have a thread specific breakpoint,
Adrian Prantl05097242018-04-30 16:49:04 +0000312 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000317 //
318 // Since the only way we ever saw this error was on first attach, I'm only
Adrian Prantl05097242018-04-30 16:49:04 +0000319 // going to trigger set did_anybody_stop_for_a_reason to true unless this
320 // is the first stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 //
322 // If this becomes a problem, we'll have to have another StopReason like
Adrian Prantl05097242018-04-30 16:49:04 +0000323 // "StopInfoHidden" which will look invalid everywhere but at this check.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324
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 Devlieghere63e5fb72019-07-24 17:56:10 +0000337 LLDB_LOGF(log,
338 "ThreadList::%s we stopped but no threads had a stop reason, "
339 "overriding should_stop and stopping.",
340 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 }
342
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000343 LLDB_LOGF(log, "ThreadList::%s overall should_stop = %i", __FUNCTION__,
344 should_stop);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345
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
356Vote 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 Devlieghere63e5fb72019-07-24 17:56:10 +0000365 LLDB_LOGF(log, "ThreadList::%s %" PRIu64 " threads", __FUNCTION__,
366 (uint64_t)m_threads.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367
Adrian Prantl05097242018-04-30 16:49:04 +0000368 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000370 // 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 Turnerdf449882017-02-01 19:45:14 +0000386 LLDB_LOG(log,
387 "Thread {0:x} voted {1}, but lost out because result was {2}",
388 thread_sp->GetID(), vote, result);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 }
390 break;
Jim Ingham35878c42014-04-08 21:33:21 +0000391 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 }
Zachary Turnerdf449882017-02-01 19:45:14 +0000393 LLDB_LOG(log, "Returning {0}", result);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 return result;
395}
Jim Inghamb01e7422010-06-19 04:45:32 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397void 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
408Vote 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 Prantl05097242018-04-30 16:49:04 +0000416 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000418
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 Devlieghere63e5fb72019-07-24 17:56:10 +0000431 LLDB_LOGF(log,
432 "ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
433 ") says don't report.",
434 (*pos)->GetIndexID(), (*pos)->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 result = eVoteNo;
436 break;
437 }
Jim Inghama0079042013-03-21 21:46:56 +0000438 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 }
440 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441}
442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443void 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 Clayton2cad65a2010-09-03 17:10:42 +0000449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450void 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 Lattner30fdc8d2010-06-08 16:52:24 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458void ThreadList::RefreshStateAfterStop() {
459 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 m_process->UpdateThreadListIfNeeded();
Greg Clayton2cad65a2010-09-03 17:10:42 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
464 if (log && log->GetVerbose())
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000465 LLDB_LOGF(log,
466 "Turning off notification of new threads while single stepping "
467 "a thread.");
Jim Ingham92087d82012-01-31 23:09:20 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 collection::iterator pos, end = m_threads.end();
470 for (pos = m_threads.begin(); pos != end; ++pos)
471 (*pos)->RefreshStateAfterStop();
472}
Jim Ingham92087d82012-01-31 23:09:20 +0000473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474void ThreadList::DiscardThreadPlans() {
Adrian Prantl05097242018-04-30 16:49:04 +0000475 // You don't need to update the thread list here, because only threads that
476 // you currently know about have any thread plans.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 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
484bool ThreadList::WillResume() {
Adrian Prantl05097242018-04-30 16:49:04 +0000485 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000488
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 Prantl05097242018-04-30 16:49:04 +0000495 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000500
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 Lattner30fdc8d2010-06-08 16:52:24 +0000513 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 if (wants_solo_run) {
517 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000518 if (log && log->GetVerbose())
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000519 LLDB_LOGF(log, "Turning on notification of new threads while single "
520 "stepping a thread.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 m_process->StartNoticingNewThreads();
522 } else {
523 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
524 if (log && log->GetVerbose())
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000525 LLDB_LOGF(log, "Turning off notification of new threads while single "
526 "stepping a thread.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 m_process->StopNoticingNewThreads();
528 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 // Give all the threads that are likely to run a last chance to set up their
Adrian Prantl05097242018-04-30 16:49:04 +0000531 // state before we negotiate who is actually going to get a chance to run...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 // Don't set to resume suspended threads, and if any thread wanted to stop
Adrian Prantl05097242018-04-30 16:49:04 +0000533 // others, only call setup on the threads that request StopOthers...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534
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 Lattner30fdc8d2010-06-08 16:52:24 +0000617}
618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619void 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
631void 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 Prantl05097242018-04-30 16:49:04 +0000635 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000641 // Process::SetPrivateState).
642 ThreadSP thread_sp(*pos);
643 if (StateIsRunningState(thread_sp->GetState()))
644 thread_sp->DidStop();
645 }
646}
647
648ThreadSP 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
660bool 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
675bool 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
690void 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
699void ThreadList::Update(ThreadList &rhs) {
700 if (this != &rhs) {
Adrian Prantl05097242018-04-30 16:49:04 +0000701 // Lock both mutexes to make sure neither side changes anyone on us while
702 // the assignment occurs
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000703 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705 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 Lattner30fdc8d2010-06-08 16:52:24 +0000709
Adrian Prantl05097242018-04-30 16:49:04 +0000710 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000716 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 Ingham18930652020-03-18 12:05:08 -0700718 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000723 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 Inghama3241c12010-07-14 02:27:20 +0000732 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 }
Jim Ingham61e8e682020-03-10 16:18:11 -0700734 if (!thread_is_alive) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 (*rhs_pos)->DestroyThread();
Jim Ingham61e8e682020-03-10 16:18:11 -0700736 }
Jim Ingham1c823b42011-01-22 01:33:44 +0000737 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739}
740
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741void 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 Lattner30fdc8d2010-06-08 16:52:24 +0000746}
747
Greg Claytonb5cd6e72016-12-08 20:38:19 +0000748std::recursive_mutex &ThreadList::GetMutex() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 return m_process->m_thread_mutex;
Andrew Kaylor29d65742013-05-10 17:19:04 +0000750}
751
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752ThreadList::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 Lattner30fdc8d2010-06-08 16:52:24 +0000760}