blob: 86248da4993612e9f8f0d3fe2a492ecb7600c874 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadList.cpp ------------------------------------------*- C++ -*-===//
2//
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
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Claytonb5cd6e72016-12-08 20:38:19 +000041 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +000042
43 m_process = rhs.m_process;
44 m_stop_id = rhs.m_stop_id;
45 m_threads = rhs.m_threads;
46 m_selected_tid = rhs.m_selected_tid;
47 }
48 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051ThreadList::~ThreadList() {
Adrian Prantl05097242018-04-30 16:49:04 +000052 // Clear the thread list. Clear will take the mutex lock which will ensure
53 // that if anyone is using the list they won't get it removed while using it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 Clear();
55}
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057lldb::ThreadSP ThreadList::GetExpressionExecutionThread() {
58 if (m_expression_tid_stack.empty())
59 return GetSelectedThread();
60 ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
61 if (expr_thread_sp)
62 return expr_thread_sp;
63 else
64 return GetSelectedThread();
65}
66
67void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
68 m_expression_tid_stack.push_back(tid);
69}
70
71void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
72 assert(m_expression_tid_stack.back() == tid);
73 m_expression_tid_stack.pop_back();
74}
75
76uint32_t ThreadList::GetStopID() const { return m_stop_id; }
77
78void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
79
80uint32_t ThreadList::GetSize(bool can_update) {
81 std::lock_guard<std::recursive_mutex> guard(GetMutex());
82
83 if (can_update)
84 m_process->UpdateThreadListIfNeeded();
85 return m_threads.size();
86}
87
88ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) {
89 std::lock_guard<std::recursive_mutex> guard(GetMutex());
90
91 if (can_update)
92 m_process->UpdateThreadListIfNeeded();
93
94 ThreadSP thread_sp;
95 if (idx < m_threads.size())
96 thread_sp = m_threads[idx];
97 return thread_sp;
98}
99
100ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) {
101 std::lock_guard<std::recursive_mutex> guard(GetMutex());
102
103 if (can_update)
104 m_process->UpdateThreadListIfNeeded();
105
106 ThreadSP thread_sp;
107 uint32_t idx = 0;
108 const uint32_t num_threads = m_threads.size();
109 for (idx = 0; idx < num_threads; ++idx) {
110 if (m_threads[idx]->GetID() == tid) {
111 thread_sp = m_threads[idx];
112 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 }
115 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
119 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 if (can_update)
122 m_process->UpdateThreadListIfNeeded();
123
124 ThreadSP thread_sp;
125 uint32_t idx = 0;
126 const uint32_t num_threads = m_threads.size();
127 for (idx = 0; idx < num_threads; ++idx) {
128 if (m_threads[idx]->GetProtocolID() == tid) {
129 thread_sp = m_threads[idx];
130 break;
131 }
132 }
133 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) {
137 std::lock_guard<std::recursive_mutex> guard(GetMutex());
138
139 if (can_update)
140 m_process->UpdateThreadListIfNeeded();
141
142 ThreadSP thread_sp;
143 uint32_t idx = 0;
144 const uint32_t num_threads = m_threads.size();
145 for (idx = 0; idx < num_threads; ++idx) {
146 if (m_threads[idx]->GetID() == tid) {
147 thread_sp = m_threads[idx];
148 m_threads.erase(m_threads.begin() + idx);
149 break;
150 }
151 }
152 return thread_sp;
Jim Ingham8d94ba02016-03-12 02:45:34 +0000153}
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid,
156 bool can_update) {
157 std::lock_guard<std::recursive_mutex> guard(GetMutex());
158
159 if (can_update)
160 m_process->UpdateThreadListIfNeeded();
161
162 ThreadSP thread_sp;
163 uint32_t idx = 0;
164 const uint32_t num_threads = m_threads.size();
165 for (idx = 0; idx < num_threads; ++idx) {
166 if (m_threads[idx]->GetProtocolID() == tid) {
167 thread_sp = m_threads[idx];
168 m_threads.erase(m_threads.begin() + idx);
169 break;
170 }
171 }
172 return thread_sp;
Jim Ingham8d94ba02016-03-12 02:45:34 +0000173}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
176 ThreadSP thread_sp;
177 if (thread_ptr) {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000178 std::lock_guard<std::recursive_mutex> guard(GetMutex());
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 uint32_t idx = 0;
181 const uint32_t num_threads = m_threads.size();
182 for (idx = 0; idx < num_threads; ++idx) {
183 if (m_threads[idx].get() == thread_ptr) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 thread_sp = m_threads[idx];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 break;
186 }
187 }
188 }
189 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190}
191
Jonas Devlieghere8db3f7e2018-04-13 11:31:34 +0000192ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
193 std::lock_guard<std::recursive_mutex> guard(GetMutex());
194
195 ThreadSP thread_sp;
196 const uint32_t num_threads = m_threads.size();
197 for (uint32_t idx = 0; idx < num_threads; ++idx) {
198 if (m_threads[idx]->GetBackingThread() == real_thread) {
199 thread_sp = m_threads[idx];
200 break;
201 }
202 }
203 return thread_sp;
204}
205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
207 std::lock_guard<std::recursive_mutex> guard(GetMutex());
208
209 if (can_update)
210 m_process->UpdateThreadListIfNeeded();
211
212 ThreadSP thread_sp;
213 const uint32_t num_threads = m_threads.size();
214 for (uint32_t idx = 0; idx < num_threads; ++idx) {
215 if (m_threads[idx]->GetIndexID() == index_id) {
216 thread_sp = m_threads[idx];
217 break;
218 }
219 }
220 return thread_sp;
221}
222
223bool ThreadList::ShouldStop(Event *event_ptr) {
224 // Running events should never stop, obviously...
225
226 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
227
Adrian Prantl05097242018-04-30 16:49:04 +0000228 // The ShouldStop method of the threads can do a whole lot of work, figuring
229 // out whether the thread plan conditions are met. So we don't want to keep
230 // the ThreadList locked the whole time we are doing this.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 // FIXME: It is possible that running code could cause new threads
Adrian Prantl05097242018-04-30 16:49:04 +0000232 // to be created. If that happens, we will miss asking them whether they
233 // should stop. This is not a big deal since we haven't had a chance to hang
234 // any interesting operations on those threads yet.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235
236 collection threads_copy;
237 {
238 // Scope for locker
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000239 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 m_process->UpdateThreadListIfNeeded();
242 for (lldb::ThreadSP thread_sp : m_threads) {
243 // This is an optimization... If we didn't let a thread run in between
Adrian Prantl05097242018-04-30 16:49:04 +0000244 // the previous stop and this one, we shouldn't have to consult it for
245 // ShouldStop. So just leave it off the list we are going to inspect. On
246 // Linux, if a thread-specific conditional breakpoint was hit, it won't
247 // necessarily be the thread that hit the breakpoint itself that
248 // evaluates the conditional expression, so the thread that hit the
249 // breakpoint could still be asked to stop, even though it hasn't been
250 // allowed to run since the previous stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
252 thread_sp->IsStillAtLastBreakpointHit())
253 threads_copy.push_back(thread_sp);
Jim Inghamb42f3af2012-11-15 22:44:04 +0000254 }
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 // It is possible the threads we were allowing to run all exited and then
Adrian Prantl05097242018-04-30 16:49:04 +0000257 // maybe the user interrupted or something, then fall back on looking at
258 // all threads:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (threads_copy.size() == 0)
261 threads_copy = m_threads;
262 }
263
264 collection::iterator pos, end = threads_copy.end();
265
266 if (log) {
267 log->PutCString("");
268 log->Printf("ThreadList::%s: %" PRIu64 " threads, %" PRIu64
269 " unsuspended threads",
270 __FUNCTION__, (uint64_t)m_threads.size(),
271 (uint64_t)threads_copy.size());
272 }
273
274 bool did_anybody_stop_for_a_reason = false;
275
276 // If the event is an Interrupt event, then we're going to stop no matter
277 // what. Otherwise, presume we won't stop.
278 bool should_stop = false;
279 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
Greg Clayton2cad65a2010-09-03 17:10:42 +0000280 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 log->Printf(
282 "ThreadList::%s handling interrupt event, should stop set to true",
283 __FUNCTION__);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 should_stop = true;
286 }
287
288 // Now we run through all the threads and get their stop info's. We want to
Adrian Prantl05097242018-04-30 16:49:04 +0000289 // make sure to do this first before we start running the ShouldStop, because
290 // one thread's ShouldStop could destroy information (like deleting a thread
291 // specific breakpoint another thread had stopped at) which could lead us to
292 // compute the StopInfo incorrectly. We don't need to use it here, we just
293 // want to make sure it gets computed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294
295 for (pos = threads_copy.begin(); pos != end; ++pos) {
296 ThreadSP thread_sp(*pos);
297 thread_sp->GetStopInfo();
298 }
299
300 for (pos = threads_copy.begin(); pos != end; ++pos) {
301 ThreadSP thread_sp(*pos);
302
303 // We should never get a stop for which no thread had a stop reason, but
Adrian Prantl05097242018-04-30 16:49:04 +0000304 // sometimes we do see this - for instance when we first connect to a
305 // remote stub. In that case we should stop, since we can't figure out the
306 // right thing to do and stopping gives the user control over what to do in
307 // this instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 //
309 // Note, this causes a problem when you have a thread specific breakpoint,
Adrian Prantl05097242018-04-30 16:49:04 +0000310 // and a bunch of threads hit the breakpoint, but not the thread which we
311 // are waiting for. All the threads that are not "supposed" to hit the
312 // breakpoint are marked as having no stop reason, which is right, they
313 // should not show a stop reason. But that triggers this code and causes
314 // us to stop seemingly for no reason.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 //
316 // Since the only way we ever saw this error was on first attach, I'm only
Adrian Prantl05097242018-04-30 16:49:04 +0000317 // going to trigger set did_anybody_stop_for_a_reason to true unless this
318 // is the first stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 //
320 // If this becomes a problem, we'll have to have another StopReason like
Adrian Prantl05097242018-04-30 16:49:04 +0000321 // "StopInfoHidden" which will look invalid everywhere but at this check.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322
323 if (thread_sp->GetProcess()->GetStopID() > 1)
324 did_anybody_stop_for_a_reason = true;
325 else
326 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
327
328 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
329 if (thread_should_stop)
330 should_stop |= true;
331 }
332
333 if (!should_stop && !did_anybody_stop_for_a_reason) {
334 should_stop = true;
335 if (log)
336 log->Printf("ThreadList::%s we stopped but no threads had a stop reason, "
337 "overriding should_stop and stopping.",
338 __FUNCTION__);
339 }
340
341 if (log)
342 log->Printf("ThreadList::%s overall should_stop = %i", __FUNCTION__,
343 should_stop);
344
345 if (should_stop) {
346 for (pos = threads_copy.begin(); pos != end; ++pos) {
347 ThreadSP thread_sp(*pos);
348 thread_sp->WillStop();
349 }
350 }
351
352 return should_stop;
353}
354
355Vote ThreadList::ShouldReportStop(Event *event_ptr) {
356 std::lock_guard<std::recursive_mutex> guard(GetMutex());
357
358 Vote result = eVoteNoOpinion;
359 m_process->UpdateThreadListIfNeeded();
360 collection::iterator pos, end = m_threads.end();
361
362 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
363
364 if (log)
365 log->Printf("ThreadList::%s %" PRIu64 " threads", __FUNCTION__,
366 (uint64_t)m_threads.size());
367
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:
Jim Inghama0079042013-03-21 21:46:56 +0000431 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 log->Printf("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
433 ") says don't report.",
434 (*pos)->GetIndexID(), (*pos)->GetID());
435 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())
465 log->Printf("Turning off notification of new threads while single stepping "
466 "a thread.");
Jim Ingham92087d82012-01-31 23:09:20 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 collection::iterator pos, end = m_threads.end();
469 for (pos = m_threads.begin(); pos != end; ++pos)
470 (*pos)->RefreshStateAfterStop();
471}
Jim Ingham92087d82012-01-31 23:09:20 +0000472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473void ThreadList::DiscardThreadPlans() {
Adrian Prantl05097242018-04-30 16:49:04 +0000474 // You don't need to update the thread list here, because only threads that
475 // you currently know about have any thread plans.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 std::lock_guard<std::recursive_mutex> guard(GetMutex());
477
478 collection::iterator pos, end = m_threads.end();
479 for (pos = m_threads.begin(); pos != end; ++pos)
480 (*pos)->DiscardThreadPlans(true);
481}
482
483bool ThreadList::WillResume() {
Adrian Prantl05097242018-04-30 16:49:04 +0000484 // Run through the threads and perform their momentary actions. But we only
485 // do this for threads that are running, user suspended threads stay where
486 // they are.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487
488 std::lock_guard<std::recursive_mutex> guard(GetMutex());
489 m_process->UpdateThreadListIfNeeded();
490
491 collection::iterator pos, end = m_threads.end();
492
493 // See if any thread wants to run stopping others. If it does, then we won't
Adrian Prantl05097242018-04-30 16:49:04 +0000494 // setup the other threads for resume, since they aren't going to get a
495 // chance to run. This is necessary because the SetupForResume might add
496 // "StopOthers" plans which would then get to be part of the who-gets-to-run
497 // negotiation, but they're coming in after the fact, and the threads that
498 // are already set up should take priority.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499
500 bool wants_solo_run = false;
501
502 for (pos = m_threads.begin(); pos != end; ++pos) {
503 lldbassert((*pos)->GetCurrentPlan() &&
504 "thread should not have null thread plan");
505 if ((*pos)->GetResumeState() != eStateSuspended &&
506 (*pos)->GetCurrentPlan()->StopOthers()) {
507 if ((*pos)->IsOperatingSystemPluginThread() &&
508 !(*pos)->GetBackingThread())
509 continue;
510 wants_solo_run = true;
511 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 if (wants_solo_run) {
516 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000517 if (log && log->GetVerbose())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 log->Printf("Turning on notification of new threads while single "
519 "stepping a thread.");
520 m_process->StartNoticingNewThreads();
521 } else {
522 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
523 if (log && log->GetVerbose())
524 log->Printf("Turning off notification of new threads while single "
525 "stepping a thread.");
526 m_process->StopNoticingNewThreads();
527 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 // Give all the threads that are likely to run a last chance to set up their
Adrian Prantl05097242018-04-30 16:49:04 +0000530 // state before we negotiate who is actually going to get a chance to run...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 // Don't set to resume suspended threads, and if any thread wanted to stop
Adrian Prantl05097242018-04-30 16:49:04 +0000532 // others, only call setup on the threads that request StopOthers...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533
534 for (pos = m_threads.begin(); pos != end; ++pos) {
535 if ((*pos)->GetResumeState() != eStateSuspended &&
536 (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) {
537 if ((*pos)->IsOperatingSystemPluginThread() &&
538 !(*pos)->GetBackingThread())
539 continue;
540 (*pos)->SetupForResume();
541 }
542 }
543
544 // Now go through the threads and see if any thread wants to run just itself.
545 // if so then pick one and run it.
546
547 ThreadList run_me_only_list(m_process);
548
549 run_me_only_list.SetStopID(m_process->GetStopID());
550
551 bool run_only_current_thread = false;
552
553 for (pos = m_threads.begin(); pos != end; ++pos) {
554 ThreadSP thread_sp(*pos);
555 if (thread_sp->GetResumeState() != eStateSuspended &&
556 thread_sp->GetCurrentPlan()->StopOthers()) {
557 if ((*pos)->IsOperatingSystemPluginThread() &&
558 !(*pos)->GetBackingThread())
559 continue;
560
561 // You can't say "stop others" and also want yourself to be suspended.
562 assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
563
564 if (thread_sp == GetSelectedThread()) {
565 // If the currently selected thread wants to run on its own, always let
566 // it.
567 run_only_current_thread = true;
568 run_me_only_list.Clear();
569 run_me_only_list.AddThread(thread_sp);
570 break;
571 }
572
573 run_me_only_list.AddThread(thread_sp);
574 }
575 }
576
577 bool need_to_resume = true;
578
579 if (run_me_only_list.GetSize(false) == 0) {
580 // Everybody runs as they wish:
581 for (pos = m_threads.begin(); pos != end; ++pos) {
582 ThreadSP thread_sp(*pos);
583 StateType run_state;
584 if (thread_sp->GetResumeState() != eStateSuspended)
585 run_state = thread_sp->GetCurrentPlan()->RunState();
586 else
587 run_state = eStateSuspended;
588 if (!thread_sp->ShouldResume(run_state))
589 need_to_resume = false;
590 }
591 } else {
592 ThreadSP thread_to_run;
593
594 if (run_only_current_thread) {
595 thread_to_run = GetSelectedThread();
596 } else if (run_me_only_list.GetSize(false) == 1) {
597 thread_to_run = run_me_only_list.GetThreadAtIndex(0);
598 } else {
599 int random_thread =
600 (int)((run_me_only_list.GetSize(false) * (double)rand()) /
601 (RAND_MAX + 1.0));
602 thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread);
603 }
604
605 for (pos = m_threads.begin(); pos != end; ++pos) {
606 ThreadSP thread_sp(*pos);
607 if (thread_sp == thread_to_run) {
608 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
609 need_to_resume = false;
610 } else
611 thread_sp->ShouldResume(eStateSuspended);
612 }
613 }
614
615 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616}
617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618void ThreadList::DidResume() {
619 std::lock_guard<std::recursive_mutex> guard(GetMutex());
620 collection::iterator pos, end = m_threads.end();
621 for (pos = m_threads.begin(); pos != end; ++pos) {
622 // Don't clear out threads that aren't going to get a chance to run, rather
623 // leave their state for the next time around.
624 ThreadSP thread_sp(*pos);
625 if (thread_sp->GetResumeState() != eStateSuspended)
626 thread_sp->DidResume();
627 }
628}
629
630void ThreadList::DidStop() {
631 std::lock_guard<std::recursive_mutex> guard(GetMutex());
632 collection::iterator pos, end = m_threads.end();
633 for (pos = m_threads.begin(); pos != end; ++pos) {
Adrian Prantl05097242018-04-30 16:49:04 +0000634 // Notify threads that the process just stopped. Note, this currently
635 // assumes that all threads in the list stop when the process stops. In
636 // the future we will want to support a debugging model where some threads
637 // continue to run while others are stopped. We either need to handle that
638 // somehow here or create a special thread list containing only threads
639 // which will stop in the code that calls this method (currently
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 // Process::SetPrivateState).
641 ThreadSP thread_sp(*pos);
642 if (StateIsRunningState(thread_sp->GetState()))
643 thread_sp->DidStop();
644 }
645}
646
647ThreadSP ThreadList::GetSelectedThread() {
648 std::lock_guard<std::recursive_mutex> guard(GetMutex());
649 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
650 if (!thread_sp.get()) {
651 if (m_threads.size() == 0)
652 return thread_sp;
653 m_selected_tid = m_threads[0]->GetID();
654 thread_sp = m_threads[0];
655 }
656 return thread_sp;
657}
658
659bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) {
660 std::lock_guard<std::recursive_mutex> guard(GetMutex());
661 ThreadSP selected_thread_sp(FindThreadByID(tid));
662 if (selected_thread_sp) {
663 m_selected_tid = tid;
664 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
665 } else
666 m_selected_tid = LLDB_INVALID_THREAD_ID;
667
668 if (notify)
669 NotifySelectedThreadChanged(m_selected_tid);
670
671 return m_selected_tid != LLDB_INVALID_THREAD_ID;
672}
673
674bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) {
675 std::lock_guard<std::recursive_mutex> guard(GetMutex());
676 ThreadSP selected_thread_sp(FindThreadByIndexID(index_id));
677 if (selected_thread_sp.get()) {
678 m_selected_tid = selected_thread_sp->GetID();
679 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
680 } else
681 m_selected_tid = LLDB_INVALID_THREAD_ID;
682
683 if (notify)
684 NotifySelectedThreadChanged(m_selected_tid);
685
686 return m_selected_tid != LLDB_INVALID_THREAD_ID;
687}
688
689void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) {
690 ThreadSP selected_thread_sp(FindThreadByID(tid));
691 if (selected_thread_sp->EventTypeHasListeners(
692 Thread::eBroadcastBitThreadSelected))
693 selected_thread_sp->BroadcastEvent(
694 Thread::eBroadcastBitThreadSelected,
695 new Thread::ThreadEventData(selected_thread_sp));
696}
697
698void ThreadList::Update(ThreadList &rhs) {
699 if (this != &rhs) {
Adrian Prantl05097242018-04-30 16:49:04 +0000700 // Lock both mutexes to make sure neither side changes anyone on us while
701 // the assignment occurs
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000702 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 m_process = rhs.m_process;
705 m_stop_id = rhs.m_stop_id;
706 m_threads.swap(rhs.m_threads);
707 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708
Adrian Prantl05097242018-04-30 16:49:04 +0000709 // Now we look for threads that we are done with and make sure to clear
710 // them up as much as possible so anyone with a shared pointer will still
711 // have a reference, but the thread won't be of much use. Using
712 // std::weak_ptr for all backward references (such as a thread to a
713 // process) will eventually solve this issue for us, but for now, we need
714 // to work around the issue
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
716 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) {
717 const lldb::tid_t tid = (*rhs_pos)->GetID();
718 bool thread_is_alive = false;
719 const uint32_t num_threads = m_threads.size();
720 for (uint32_t idx = 0; idx < num_threads; ++idx) {
721 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
722 if (m_threads[idx]->GetID() == tid ||
723 (backing_thread && backing_thread->GetID() == tid)) {
724 thread_is_alive = true;
725 break;
Jim Inghama3241c12010-07-14 02:27:20 +0000726 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 }
728 if (!thread_is_alive)
729 (*rhs_pos)->DestroyThread();
Jim Ingham1c823b42011-01-22 01:33:44 +0000730 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732}
733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734void ThreadList::Flush() {
735 std::lock_guard<std::recursive_mutex> guard(GetMutex());
736 collection::iterator pos, end = m_threads.end();
737 for (pos = m_threads.begin(); pos != end; ++pos)
738 (*pos)->Flush();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739}
740
Greg Claytonb5cd6e72016-12-08 20:38:19 +0000741std::recursive_mutex &ThreadList::GetMutex() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 return m_process->m_thread_mutex;
Andrew Kaylor29d65742013-05-10 17:19:04 +0000743}
744
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
746 lldb::ThreadSP thread_sp)
747 : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
748 if (thread_sp) {
749 m_tid = thread_sp->GetID();
750 m_thread_list = &thread_sp->GetProcess()->GetThreadList();
751 m_thread_list->PushExpressionExecutionThread(m_tid);
752 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753}