blob: 5ca743d67383ef05ba78329006effd751522b2ce [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadList.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00009
10// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include <stdlib.h>
12
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000013// C++ Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include <algorithm>
15
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000016// Other libraries and framework includes
17// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Target/Process.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Target/RegisterContext.h"
20#include "lldb/Target/Thread.h"
21#include "lldb/Target/ThreadList.h"
22#include "lldb/Target/ThreadPlan.h"
Zachary Turner8f186f82015-11-13 21:53:03 +000023#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000024#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000025#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030ThreadList::ThreadList(Process *process)
31 : ThreadCollection(), m_process(process), m_stop_id(0),
32 m_selected_tid(LLDB_INVALID_THREAD_ID) {}
33
34ThreadList::ThreadList(const ThreadList &rhs)
35 : ThreadCollection(), m_process(rhs.m_process), m_stop_id(rhs.m_stop_id),
36 m_selected_tid() {
37 // Use the assignment operator since it uses the mutex
38 *this = rhs;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039}
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041const ThreadList &ThreadList::operator=(const ThreadList &rhs) {
42 if (this != &rhs) {
Adrian Prantl05097242018-04-30 16:49:04 +000043 // Lock both mutexes to make sure neither side changes anyone on us while
44 // the assignment occurs
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Claytonb5cd6e72016-12-08 20:38:19 +000046 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +000047
48 m_process = rhs.m_process;
49 m_stop_id = rhs.m_stop_id;
50 m_threads = rhs.m_threads;
51 m_selected_tid = rhs.m_selected_tid;
52 }
53 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056ThreadList::~ThreadList() {
Adrian Prantl05097242018-04-30 16:49:04 +000057 // Clear the thread list. Clear will take the mutex lock which will ensure
58 // that if anyone is using the list they won't get it removed while using it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 Clear();
60}
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062lldb::ThreadSP ThreadList::GetExpressionExecutionThread() {
63 if (m_expression_tid_stack.empty())
64 return GetSelectedThread();
65 ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
66 if (expr_thread_sp)
67 return expr_thread_sp;
68 else
69 return GetSelectedThread();
70}
71
72void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
73 m_expression_tid_stack.push_back(tid);
74}
75
76void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
77 assert(m_expression_tid_stack.back() == tid);
78 m_expression_tid_stack.pop_back();
79}
80
81uint32_t ThreadList::GetStopID() const { return m_stop_id; }
82
83void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
84
85uint32_t ThreadList::GetSize(bool can_update) {
86 std::lock_guard<std::recursive_mutex> guard(GetMutex());
87
88 if (can_update)
89 m_process->UpdateThreadListIfNeeded();
90 return m_threads.size();
91}
92
93ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) {
94 std::lock_guard<std::recursive_mutex> guard(GetMutex());
95
96 if (can_update)
97 m_process->UpdateThreadListIfNeeded();
98
99 ThreadSP thread_sp;
100 if (idx < m_threads.size())
101 thread_sp = m_threads[idx];
102 return thread_sp;
103}
104
105ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) {
106 std::lock_guard<std::recursive_mutex> guard(GetMutex());
107
108 if (can_update)
109 m_process->UpdateThreadListIfNeeded();
110
111 ThreadSP thread_sp;
112 uint32_t idx = 0;
113 const uint32_t num_threads = m_threads.size();
114 for (idx = 0; idx < num_threads; ++idx) {
115 if (m_threads[idx]->GetID() == tid) {
116 thread_sp = m_threads[idx];
117 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 }
120 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
124 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 if (can_update)
127 m_process->UpdateThreadListIfNeeded();
128
129 ThreadSP thread_sp;
130 uint32_t idx = 0;
131 const uint32_t num_threads = m_threads.size();
132 for (idx = 0; idx < num_threads; ++idx) {
133 if (m_threads[idx]->GetProtocolID() == tid) {
134 thread_sp = m_threads[idx];
135 break;
136 }
137 }
138 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) {
142 std::lock_guard<std::recursive_mutex> guard(GetMutex());
143
144 if (can_update)
145 m_process->UpdateThreadListIfNeeded();
146
147 ThreadSP thread_sp;
148 uint32_t idx = 0;
149 const uint32_t num_threads = m_threads.size();
150 for (idx = 0; idx < num_threads; ++idx) {
151 if (m_threads[idx]->GetID() == tid) {
152 thread_sp = m_threads[idx];
153 m_threads.erase(m_threads.begin() + idx);
154 break;
155 }
156 }
157 return thread_sp;
Jim Ingham8d94ba02016-03-12 02:45:34 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid,
161 bool can_update) {
162 std::lock_guard<std::recursive_mutex> guard(GetMutex());
163
164 if (can_update)
165 m_process->UpdateThreadListIfNeeded();
166
167 ThreadSP thread_sp;
168 uint32_t idx = 0;
169 const uint32_t num_threads = m_threads.size();
170 for (idx = 0; idx < num_threads; ++idx) {
171 if (m_threads[idx]->GetProtocolID() == tid) {
172 thread_sp = m_threads[idx];
173 m_threads.erase(m_threads.begin() + idx);
174 break;
175 }
176 }
177 return thread_sp;
Jim Ingham8d94ba02016-03-12 02:45:34 +0000178}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
181 ThreadSP thread_sp;
182 if (thread_ptr) {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000183 std::lock_guard<std::recursive_mutex> guard(GetMutex());
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 uint32_t idx = 0;
186 const uint32_t num_threads = m_threads.size();
187 for (idx = 0; idx < num_threads; ++idx) {
188 if (m_threads[idx].get() == thread_ptr) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 thread_sp = m_threads[idx];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 break;
191 }
192 }
193 }
194 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
Jonas Devlieghere8db3f7e2018-04-13 11:31:34 +0000197ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
198 std::lock_guard<std::recursive_mutex> guard(GetMutex());
199
200 ThreadSP thread_sp;
201 const uint32_t num_threads = m_threads.size();
202 for (uint32_t idx = 0; idx < num_threads; ++idx) {
203 if (m_threads[idx]->GetBackingThread() == real_thread) {
204 thread_sp = m_threads[idx];
205 break;
206 }
207 }
208 return thread_sp;
209}
210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
212 std::lock_guard<std::recursive_mutex> guard(GetMutex());
213
214 if (can_update)
215 m_process->UpdateThreadListIfNeeded();
216
217 ThreadSP thread_sp;
218 const uint32_t num_threads = m_threads.size();
219 for (uint32_t idx = 0; idx < num_threads; ++idx) {
220 if (m_threads[idx]->GetIndexID() == index_id) {
221 thread_sp = m_threads[idx];
222 break;
223 }
224 }
225 return thread_sp;
226}
227
228bool ThreadList::ShouldStop(Event *event_ptr) {
229 // Running events should never stop, obviously...
230
231 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
232
Adrian Prantl05097242018-04-30 16:49:04 +0000233 // The ShouldStop method of the threads can do a whole lot of work, figuring
234 // out whether the thread plan conditions are met. So we don't want to keep
235 // the ThreadList locked the whole time we are doing this.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 // FIXME: It is possible that running code could cause new threads
Adrian Prantl05097242018-04-30 16:49:04 +0000237 // to be created. If that happens, we will miss asking them whether they
238 // should stop. This is not a big deal since we haven't had a chance to hang
239 // any interesting operations on those threads yet.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240
241 collection threads_copy;
242 {
243 // Scope for locker
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000244 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 m_process->UpdateThreadListIfNeeded();
247 for (lldb::ThreadSP thread_sp : m_threads) {
248 // This is an optimization... If we didn't let a thread run in between
Adrian Prantl05097242018-04-30 16:49:04 +0000249 // the previous stop and this one, we shouldn't have to consult it for
250 // ShouldStop. So just leave it off the list we are going to inspect. On
251 // Linux, if a thread-specific conditional breakpoint was hit, it won't
252 // necessarily be the thread that hit the breakpoint itself that
253 // evaluates the conditional expression, so the thread that hit the
254 // breakpoint could still be asked to stop, even though it hasn't been
255 // allowed to run since the previous stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
257 thread_sp->IsStillAtLastBreakpointHit())
258 threads_copy.push_back(thread_sp);
Jim Inghamb42f3af2012-11-15 22:44:04 +0000259 }
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 // It is possible the threads we were allowing to run all exited and then
Adrian Prantl05097242018-04-30 16:49:04 +0000262 // maybe the user interrupted or something, then fall back on looking at
263 // all threads:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 if (threads_copy.size() == 0)
266 threads_copy = m_threads;
267 }
268
269 collection::iterator pos, end = threads_copy.end();
270
271 if (log) {
272 log->PutCString("");
273 log->Printf("ThreadList::%s: %" PRIu64 " threads, %" PRIu64
274 " unsuspended threads",
275 __FUNCTION__, (uint64_t)m_threads.size(),
276 (uint64_t)threads_copy.size());
277 }
278
279 bool did_anybody_stop_for_a_reason = false;
280
281 // If the event is an Interrupt event, then we're going to stop no matter
282 // what. Otherwise, presume we won't stop.
283 bool should_stop = false;
284 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
Greg Clayton2cad65a2010-09-03 17:10:42 +0000285 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 log->Printf(
287 "ThreadList::%s handling interrupt event, should stop set to true",
288 __FUNCTION__);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 should_stop = true;
291 }
292
293 // Now we run through all the threads and get their stop info's. We want to
Adrian Prantl05097242018-04-30 16:49:04 +0000294 // make sure to do this first before we start running the ShouldStop, because
295 // one thread's ShouldStop could destroy information (like deleting a thread
296 // specific breakpoint another thread had stopped at) which could lead us to
297 // compute the StopInfo incorrectly. We don't need to use it here, we just
298 // want to make sure it gets computed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299
300 for (pos = threads_copy.begin(); pos != end; ++pos) {
301 ThreadSP thread_sp(*pos);
302 thread_sp->GetStopInfo();
303 }
304
305 for (pos = threads_copy.begin(); pos != end; ++pos) {
306 ThreadSP thread_sp(*pos);
307
308 // We should never get a stop for which no thread had a stop reason, but
Adrian Prantl05097242018-04-30 16:49:04 +0000309 // sometimes we do see this - for instance when we first connect to a
310 // remote stub. In that case we should stop, since we can't figure out the
311 // right thing to do and stopping gives the user control over what to do in
312 // this instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 //
314 // Note, this causes a problem when you have a thread specific breakpoint,
Adrian Prantl05097242018-04-30 16:49:04 +0000315 // and a bunch of threads hit the breakpoint, but not the thread which we
316 // are waiting for. All the threads that are not "supposed" to hit the
317 // breakpoint are marked as having no stop reason, which is right, they
318 // should not show a stop reason. But that triggers this code and causes
319 // us to stop seemingly for no reason.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 //
321 // Since the only way we ever saw this error was on first attach, I'm only
Adrian Prantl05097242018-04-30 16:49:04 +0000322 // going to trigger set did_anybody_stop_for_a_reason to true unless this
323 // is the first stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 //
325 // If this becomes a problem, we'll have to have another StopReason like
Adrian Prantl05097242018-04-30 16:49:04 +0000326 // "StopInfoHidden" which will look invalid everywhere but at this check.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327
328 if (thread_sp->GetProcess()->GetStopID() > 1)
329 did_anybody_stop_for_a_reason = true;
330 else
331 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
332
333 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
334 if (thread_should_stop)
335 should_stop |= true;
336 }
337
338 if (!should_stop && !did_anybody_stop_for_a_reason) {
339 should_stop = true;
340 if (log)
341 log->Printf("ThreadList::%s we stopped but no threads had a stop reason, "
342 "overriding should_stop and stopping.",
343 __FUNCTION__);
344 }
345
346 if (log)
347 log->Printf("ThreadList::%s overall should_stop = %i", __FUNCTION__,
348 should_stop);
349
350 if (should_stop) {
351 for (pos = threads_copy.begin(); pos != end; ++pos) {
352 ThreadSP thread_sp(*pos);
353 thread_sp->WillStop();
354 }
355 }
356
357 return should_stop;
358}
359
360Vote ThreadList::ShouldReportStop(Event *event_ptr) {
361 std::lock_guard<std::recursive_mutex> guard(GetMutex());
362
363 Vote result = eVoteNoOpinion;
364 m_process->UpdateThreadListIfNeeded();
365 collection::iterator pos, end = m_threads.end();
366
367 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
368
369 if (log)
370 log->Printf("ThreadList::%s %" PRIu64 " threads", __FUNCTION__,
371 (uint64_t)m_threads.size());
372
Adrian Prantl05097242018-04-30 16:49:04 +0000373 // Run through the threads and ask whether we should report this event. For
374 // stopping, a YES vote wins over everything. A NO vote wins over NO
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 // opinion.
376 for (pos = m_threads.begin(); pos != end; ++pos) {
377 ThreadSP thread_sp(*pos);
378 const Vote vote = thread_sp->ShouldReportStop(event_ptr);
379 switch (vote) {
380 case eVoteNoOpinion:
381 continue;
382
383 case eVoteYes:
384 result = eVoteYes;
385 break;
386
387 case eVoteNo:
388 if (result == eVoteNoOpinion) {
389 result = eVoteNo;
390 } else {
Zachary Turnerdf449882017-02-01 19:45:14 +0000391 LLDB_LOG(log,
392 "Thread {0:x} voted {1}, but lost out because result was {2}",
393 thread_sp->GetID(), vote, result);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 }
395 break;
Jim Ingham35878c42014-04-08 21:33:21 +0000396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 }
Zachary Turnerdf449882017-02-01 19:45:14 +0000398 LLDB_LOG(log, "Returning {0}", result);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 return result;
400}
Jim Inghamb01e7422010-06-19 04:45:32 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402void ThreadList::SetShouldReportStop(Vote vote) {
403 std::lock_guard<std::recursive_mutex> guard(GetMutex());
404
405 m_process->UpdateThreadListIfNeeded();
406 collection::iterator pos, end = m_threads.end();
407 for (pos = m_threads.begin(); pos != end; ++pos) {
408 ThreadSP thread_sp(*pos);
409 thread_sp->SetShouldReportStop(vote);
410 }
411}
412
413Vote ThreadList::ShouldReportRun(Event *event_ptr) {
414
415 std::lock_guard<std::recursive_mutex> guard(GetMutex());
416
417 Vote result = eVoteNoOpinion;
418 m_process->UpdateThreadListIfNeeded();
419 collection::iterator pos, end = m_threads.end();
420
Adrian Prantl05097242018-04-30 16:49:04 +0000421 // Run through the threads and ask whether we should report this event. The
422 // rule is NO vote wins over everything, a YES vote wins over no opinion.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423
424 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
425
426 for (pos = m_threads.begin(); pos != end; ++pos) {
427 if ((*pos)->GetResumeState() != eStateSuspended) {
428 switch ((*pos)->ShouldReportRun(event_ptr)) {
429 case eVoteNoOpinion:
430 continue;
431 case eVoteYes:
432 if (result == eVoteNoOpinion)
433 result = eVoteYes;
434 break;
435 case eVoteNo:
Jim Inghama0079042013-03-21 21:46:56 +0000436 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 log->Printf("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
438 ") says don't report.",
439 (*pos)->GetIndexID(), (*pos)->GetID());
440 result = eVoteNo;
441 break;
442 }
Jim Inghama0079042013-03-21 21:46:56 +0000443 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 }
445 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446}
447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448void ThreadList::Clear() {
449 std::lock_guard<std::recursive_mutex> guard(GetMutex());
450 m_stop_id = 0;
451 m_threads.clear();
452 m_selected_tid = LLDB_INVALID_THREAD_ID;
453}
Greg Clayton2cad65a2010-09-03 17:10:42 +0000454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455void ThreadList::Destroy() {
456 std::lock_guard<std::recursive_mutex> guard(GetMutex());
457 const uint32_t num_threads = m_threads.size();
458 for (uint32_t idx = 0; idx < num_threads; ++idx) {
459 m_threads[idx]->DestroyThread();
460 }
461}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463void ThreadList::RefreshStateAfterStop() {
464 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000465
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 m_process->UpdateThreadListIfNeeded();
Greg Clayton2cad65a2010-09-03 17:10:42 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
469 if (log && log->GetVerbose())
470 log->Printf("Turning off notification of new threads while single stepping "
471 "a thread.");
Jim Ingham92087d82012-01-31 23:09:20 +0000472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 collection::iterator pos, end = m_threads.end();
474 for (pos = m_threads.begin(); pos != end; ++pos)
475 (*pos)->RefreshStateAfterStop();
476}
Jim Ingham92087d82012-01-31 23:09:20 +0000477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478void ThreadList::DiscardThreadPlans() {
Adrian Prantl05097242018-04-30 16:49:04 +0000479 // You don't need to update the thread list here, because only threads that
480 // you currently know about have any thread plans.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 std::lock_guard<std::recursive_mutex> guard(GetMutex());
482
483 collection::iterator pos, end = m_threads.end();
484 for (pos = m_threads.begin(); pos != end; ++pos)
485 (*pos)->DiscardThreadPlans(true);
486}
487
488bool ThreadList::WillResume() {
Adrian Prantl05097242018-04-30 16:49:04 +0000489 // Run through the threads and perform their momentary actions. But we only
490 // do this for threads that are running, user suspended threads stay where
491 // they are.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492
493 std::lock_guard<std::recursive_mutex> guard(GetMutex());
494 m_process->UpdateThreadListIfNeeded();
495
496 collection::iterator pos, end = m_threads.end();
497
498 // See if any thread wants to run stopping others. If it does, then we won't
Adrian Prantl05097242018-04-30 16:49:04 +0000499 // setup the other threads for resume, since they aren't going to get a
500 // chance to run. This is necessary because the SetupForResume might add
501 // "StopOthers" plans which would then get to be part of the who-gets-to-run
502 // negotiation, but they're coming in after the fact, and the threads that
503 // are already set up should take priority.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504
505 bool wants_solo_run = false;
506
507 for (pos = m_threads.begin(); pos != end; ++pos) {
508 lldbassert((*pos)->GetCurrentPlan() &&
509 "thread should not have null thread plan");
510 if ((*pos)->GetResumeState() != eStateSuspended &&
511 (*pos)->GetCurrentPlan()->StopOthers()) {
512 if ((*pos)->IsOperatingSystemPluginThread() &&
513 !(*pos)->GetBackingThread())
514 continue;
515 wants_solo_run = true;
516 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 if (wants_solo_run) {
521 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000522 if (log && log->GetVerbose())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 log->Printf("Turning on notification of new threads while single "
524 "stepping a thread.");
525 m_process->StartNoticingNewThreads();
526 } else {
527 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
528 if (log && log->GetVerbose())
529 log->Printf("Turning off notification of new threads while single "
530 "stepping a thread.");
531 m_process->StopNoticingNewThreads();
532 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 // Give all the threads that are likely to run a last chance to set up their
Adrian Prantl05097242018-04-30 16:49:04 +0000535 // state before we negotiate who is actually going to get a chance to run...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 // Don't set to resume suspended threads, and if any thread wanted to stop
Adrian Prantl05097242018-04-30 16:49:04 +0000537 // others, only call setup on the threads that request StopOthers...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538
539 for (pos = m_threads.begin(); pos != end; ++pos) {
540 if ((*pos)->GetResumeState() != eStateSuspended &&
541 (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) {
542 if ((*pos)->IsOperatingSystemPluginThread() &&
543 !(*pos)->GetBackingThread())
544 continue;
545 (*pos)->SetupForResume();
546 }
547 }
548
549 // Now go through the threads and see if any thread wants to run just itself.
550 // if so then pick one and run it.
551
552 ThreadList run_me_only_list(m_process);
553
554 run_me_only_list.SetStopID(m_process->GetStopID());
555
556 bool run_only_current_thread = false;
557
558 for (pos = m_threads.begin(); pos != end; ++pos) {
559 ThreadSP thread_sp(*pos);
560 if (thread_sp->GetResumeState() != eStateSuspended &&
561 thread_sp->GetCurrentPlan()->StopOthers()) {
562 if ((*pos)->IsOperatingSystemPluginThread() &&
563 !(*pos)->GetBackingThread())
564 continue;
565
566 // You can't say "stop others" and also want yourself to be suspended.
567 assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
568
569 if (thread_sp == GetSelectedThread()) {
570 // If the currently selected thread wants to run on its own, always let
571 // it.
572 run_only_current_thread = true;
573 run_me_only_list.Clear();
574 run_me_only_list.AddThread(thread_sp);
575 break;
576 }
577
578 run_me_only_list.AddThread(thread_sp);
579 }
580 }
581
582 bool need_to_resume = true;
583
584 if (run_me_only_list.GetSize(false) == 0) {
585 // Everybody runs as they wish:
586 for (pos = m_threads.begin(); pos != end; ++pos) {
587 ThreadSP thread_sp(*pos);
588 StateType run_state;
589 if (thread_sp->GetResumeState() != eStateSuspended)
590 run_state = thread_sp->GetCurrentPlan()->RunState();
591 else
592 run_state = eStateSuspended;
593 if (!thread_sp->ShouldResume(run_state))
594 need_to_resume = false;
595 }
596 } else {
597 ThreadSP thread_to_run;
598
599 if (run_only_current_thread) {
600 thread_to_run = GetSelectedThread();
601 } else if (run_me_only_list.GetSize(false) == 1) {
602 thread_to_run = run_me_only_list.GetThreadAtIndex(0);
603 } else {
604 int random_thread =
605 (int)((run_me_only_list.GetSize(false) * (double)rand()) /
606 (RAND_MAX + 1.0));
607 thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread);
608 }
609
610 for (pos = m_threads.begin(); pos != end; ++pos) {
611 ThreadSP thread_sp(*pos);
612 if (thread_sp == thread_to_run) {
613 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
614 need_to_resume = false;
615 } else
616 thread_sp->ShouldResume(eStateSuspended);
617 }
618 }
619
620 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}
622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623void ThreadList::DidResume() {
624 std::lock_guard<std::recursive_mutex> guard(GetMutex());
625 collection::iterator pos, end = m_threads.end();
626 for (pos = m_threads.begin(); pos != end; ++pos) {
627 // Don't clear out threads that aren't going to get a chance to run, rather
628 // leave their state for the next time around.
629 ThreadSP thread_sp(*pos);
630 if (thread_sp->GetResumeState() != eStateSuspended)
631 thread_sp->DidResume();
632 }
633}
634
635void ThreadList::DidStop() {
636 std::lock_guard<std::recursive_mutex> guard(GetMutex());
637 collection::iterator pos, end = m_threads.end();
638 for (pos = m_threads.begin(); pos != end; ++pos) {
Adrian Prantl05097242018-04-30 16:49:04 +0000639 // Notify threads that the process just stopped. Note, this currently
640 // assumes that all threads in the list stop when the process stops. In
641 // the future we will want to support a debugging model where some threads
642 // continue to run while others are stopped. We either need to handle that
643 // somehow here or create a special thread list containing only threads
644 // which will stop in the code that calls this method (currently
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645 // Process::SetPrivateState).
646 ThreadSP thread_sp(*pos);
647 if (StateIsRunningState(thread_sp->GetState()))
648 thread_sp->DidStop();
649 }
650}
651
652ThreadSP ThreadList::GetSelectedThread() {
653 std::lock_guard<std::recursive_mutex> guard(GetMutex());
654 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
655 if (!thread_sp.get()) {
656 if (m_threads.size() == 0)
657 return thread_sp;
658 m_selected_tid = m_threads[0]->GetID();
659 thread_sp = m_threads[0];
660 }
661 return thread_sp;
662}
663
664bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) {
665 std::lock_guard<std::recursive_mutex> guard(GetMutex());
666 ThreadSP selected_thread_sp(FindThreadByID(tid));
667 if (selected_thread_sp) {
668 m_selected_tid = tid;
669 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
670 } else
671 m_selected_tid = LLDB_INVALID_THREAD_ID;
672
673 if (notify)
674 NotifySelectedThreadChanged(m_selected_tid);
675
676 return m_selected_tid != LLDB_INVALID_THREAD_ID;
677}
678
679bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) {
680 std::lock_guard<std::recursive_mutex> guard(GetMutex());
681 ThreadSP selected_thread_sp(FindThreadByIndexID(index_id));
682 if (selected_thread_sp.get()) {
683 m_selected_tid = selected_thread_sp->GetID();
684 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
685 } else
686 m_selected_tid = LLDB_INVALID_THREAD_ID;
687
688 if (notify)
689 NotifySelectedThreadChanged(m_selected_tid);
690
691 return m_selected_tid != LLDB_INVALID_THREAD_ID;
692}
693
694void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) {
695 ThreadSP selected_thread_sp(FindThreadByID(tid));
696 if (selected_thread_sp->EventTypeHasListeners(
697 Thread::eBroadcastBitThreadSelected))
698 selected_thread_sp->BroadcastEvent(
699 Thread::eBroadcastBitThreadSelected,
700 new Thread::ThreadEventData(selected_thread_sp));
701}
702
703void ThreadList::Update(ThreadList &rhs) {
704 if (this != &rhs) {
Adrian Prantl05097242018-04-30 16:49:04 +0000705 // Lock both mutexes to make sure neither side changes anyone on us while
706 // the assignment occurs
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000707 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709 m_process = rhs.m_process;
710 m_stop_id = rhs.m_stop_id;
711 m_threads.swap(rhs.m_threads);
712 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713
Adrian Prantl05097242018-04-30 16:49:04 +0000714 // Now we look for threads that we are done with and make sure to clear
715 // them up as much as possible so anyone with a shared pointer will still
716 // have a reference, but the thread won't be of much use. Using
717 // std::weak_ptr for all backward references (such as a thread to a
718 // process) will eventually solve this issue for us, but for now, we need
719 // to work around the issue
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
721 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) {
722 const lldb::tid_t tid = (*rhs_pos)->GetID();
723 bool thread_is_alive = false;
724 const uint32_t num_threads = m_threads.size();
725 for (uint32_t idx = 0; idx < num_threads; ++idx) {
726 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
727 if (m_threads[idx]->GetID() == tid ||
728 (backing_thread && backing_thread->GetID() == tid)) {
729 thread_is_alive = true;
730 break;
Jim Inghama3241c12010-07-14 02:27:20 +0000731 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 }
733 if (!thread_is_alive)
734 (*rhs_pos)->DestroyThread();
Jim Ingham1c823b42011-01-22 01:33:44 +0000735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737}
738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739void ThreadList::Flush() {
740 std::lock_guard<std::recursive_mutex> guard(GetMutex());
741 collection::iterator pos, end = m_threads.end();
742 for (pos = m_threads.begin(); pos != end; ++pos)
743 (*pos)->Flush();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744}
745
Greg Claytonb5cd6e72016-12-08 20:38:19 +0000746std::recursive_mutex &ThreadList::GetMutex() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 return m_process->m_thread_mutex;
Andrew Kaylor29d65742013-05-10 17:19:04 +0000748}
749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
751 lldb::ThreadSP thread_sp)
752 : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
753 if (thread_sp) {
754 m_tid = thread_sp->GetID();
755 m_thread_list = &thread_sp->GetProcess()->GetThreadList();
756 m_thread_list->PushExpressionExecutionThread(m_tid);
757 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}