blob: 23e9f78073217d6aeb100a7686507a4bc06d8457 [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
Greg Clayton2cad65a2010-09-03 17:10:42 +000018#include "lldb/Core/Log.h"
Andrew Kaylor29d65742013-05-10 17:19:04 +000019#include "lldb/Core/State.h"
Greg Clayton2cad65a2010-09-03 17:10:42 +000020#include "lldb/Target/RegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/ThreadList.h"
22#include "lldb/Target/Thread.h"
23#include "lldb/Target/ThreadPlan.h"
24#include "lldb/Target/Process.h"
Zachary Turner50232572015-03-18 21:31:45 +000025#include "lldb/Utility/ConvertEnum.h"
Zachary Turner8f186f82015-11-13 21:53:03 +000026#include "lldb/Utility/LLDBAssert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31ThreadList::ThreadList (Process *process) :
Kuba Breckae4d48012014-09-05 19:13:15 +000032 ThreadCollection(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 m_process (process),
34 m_stop_id (0),
Jim Ingham2976d002010-08-26 21:32:51 +000035 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036{
37}
38
39ThreadList::ThreadList (const ThreadList &rhs) :
Kuba Breckae4d48012014-09-05 19:13:15 +000040 ThreadCollection(),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000041 m_process (rhs.m_process),
42 m_stop_id (rhs.m_stop_id),
Jim Ingham2976d002010-08-26 21:32:51 +000043 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044{
45 // Use the assignment operator since it uses the mutex
46 *this = rhs;
47}
48
49const ThreadList&
50ThreadList::operator = (const ThreadList& rhs)
51{
52 if (this != &rhs)
53 {
54 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +000055 // while the assignment occurs
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000056 std::lock_guard<std::recursive_mutex> guard(GetMutex());
57
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058 m_process = rhs.m_process;
59 m_stop_id = rhs.m_stop_id;
60 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000061 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 }
63 return *this;
64}
65
66
67ThreadList::~ThreadList()
68{
Greg Claytonac358da2013-03-28 18:33:53 +000069 // Clear the thread list. Clear will take the mutex lock
70 // which will ensure that if anyone is using the list
71 // they won't get it removed while using it.
72 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Jim Ingham8d94ba02016-03-12 02:45:34 +000075lldb::ThreadSP
76ThreadList::GetExpressionExecutionThread()
77{
78 if (m_expression_tid_stack.empty())
79 return GetSelectedThread();
80 ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
81 if (expr_thread_sp)
82 return expr_thread_sp;
83 else
84 return GetSelectedThread();
85}
86
87void
88ThreadList::PushExpressionExecutionThread(lldb::tid_t tid)
89{
90 m_expression_tid_stack.push_back(tid);
91}
92
93void
94ThreadList::PopExpressionExecutionThread(lldb::tid_t tid)
95{
96 assert(m_expression_tid_stack.back() == tid);
97 m_expression_tid_stack.pop_back();
98}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
100uint32_t
101ThreadList::GetStopID () const
102{
103 return m_stop_id;
104}
105
106void
107ThreadList::SetStopID (uint32_t stop_id)
108{
109 m_stop_id = stop_id;
110}
111
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112uint32_t
113ThreadList::GetSize (bool can_update)
114{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000115 std::lock_guard<std::recursive_mutex> guard(GetMutex());
116
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117 if (can_update)
118 m_process->UpdateThreadListIfNeeded();
119 return m_threads.size();
120}
121
122ThreadSP
123ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
124{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000125 std::lock_guard<std::recursive_mutex> guard(GetMutex());
126
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 if (can_update)
128 m_process->UpdateThreadListIfNeeded();
129
130 ThreadSP thread_sp;
131 if (idx < m_threads.size())
132 thread_sp = m_threads[idx];
133 return thread_sp;
134}
135
136ThreadSP
137ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
138{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000139 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
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 {
149 if (m_threads[idx]->GetID() == tid)
150 {
151 thread_sp = m_threads[idx];
152 break;
153 }
154 }
155 return thread_sp;
156}
157
158ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000159ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
160{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000161 std::lock_guard<std::recursive_mutex> guard(GetMutex());
162
Greg Clayton160c9d82013-05-01 21:54:04 +0000163 if (can_update)
164 m_process->UpdateThreadListIfNeeded();
165
166 ThreadSP thread_sp;
167 uint32_t idx = 0;
168 const uint32_t num_threads = m_threads.size();
169 for (idx = 0; idx < num_threads; ++idx)
170 {
171 if (m_threads[idx]->GetProtocolID() == tid)
172 {
173 thread_sp = m_threads[idx];
174 break;
175 }
176 }
177 return thread_sp;
178}
179
180
181ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000182ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
183{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000184 std::lock_guard<std::recursive_mutex> guard(GetMutex());
185
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000186 if (can_update)
187 m_process->UpdateThreadListIfNeeded();
188
189 ThreadSP thread_sp;
190 uint32_t idx = 0;
191 const uint32_t num_threads = m_threads.size();
192 for (idx = 0; idx < num_threads; ++idx)
193 {
194 if (m_threads[idx]->GetID() == tid)
195 {
196 thread_sp = m_threads[idx];
197 m_threads.erase(m_threads.begin()+idx);
198 break;
199 }
200 }
201 return thread_sp;
202}
203
204ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000205ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
206{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000207 std::lock_guard<std::recursive_mutex> guard(GetMutex());
208
Greg Clayton160c9d82013-05-01 21:54:04 +0000209 if (can_update)
210 m_process->UpdateThreadListIfNeeded();
211
212 ThreadSP thread_sp;
213 uint32_t idx = 0;
214 const uint32_t num_threads = m_threads.size();
215 for (idx = 0; idx < num_threads; ++idx)
216 {
217 if (m_threads[idx]->GetProtocolID() == tid)
218 {
219 thread_sp = m_threads[idx];
220 m_threads.erase(m_threads.begin()+idx);
221 break;
222 }
223 }
224 return thread_sp;
225}
226
227ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
229{
230 ThreadSP thread_sp;
231 if (thread_ptr)
232 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000233 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234
235 uint32_t idx = 0;
236 const uint32_t num_threads = m_threads.size();
237 for (idx = 0; idx < num_threads; ++idx)
238 {
239 if (m_threads[idx].get() == thread_ptr)
240 {
241 thread_sp = m_threads[idx];
242 break;
243 }
244 }
245 }
246 return thread_sp;
247}
248
249
250
251ThreadSP
252ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
253{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000254 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
256 if (can_update)
257 m_process->UpdateThreadListIfNeeded();
258
259 ThreadSP thread_sp;
260 const uint32_t num_threads = m_threads.size();
261 for (uint32_t idx = 0; idx < num_threads; ++idx)
262 {
263 if (m_threads[idx]->GetIndexID() == index_id)
264 {
265 thread_sp = m_threads[idx];
266 break;
267 }
268 }
269 return thread_sp;
270}
271
272bool
273ThreadList::ShouldStop (Event *event_ptr)
274{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 // Running events should never stop, obviously...
276
Greg Clayton5160ce52013-03-27 23:08:40 +0000277 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
Jim Inghamb42f3af2012-11-15 22:44:04 +0000279 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000280 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000281 // to keep the ThreadList locked the whole time we are doing this.
282 // FIXME: It is possible that running code could cause new threads
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000283 // to be created. If that happens, we will miss asking them whether
284 // they should stop. This is not a big deal since we haven't had
Jim Inghamb42f3af2012-11-15 22:44:04 +0000285 // a chance to hang any interesting operations on those threads yet.
286
287 collection threads_copy;
288 {
289 // Scope for locker
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000290 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291
Jim Inghamb42f3af2012-11-15 22:44:04 +0000292 m_process->UpdateThreadListIfNeeded();
Jim Ingham569aaf92015-11-06 22:45:57 +0000293 for (lldb::ThreadSP thread_sp : m_threads)
294 {
295 // This is an optimization... If we didn't let a thread run in between the previous stop and this
296 // one, we shouldn't have to consult it for ShouldStop. So just leave it off the list we are going to
297 // inspect.
Chaoren Lin6e8fbc62015-11-07 02:16:31 +0000298 // On Linux, if a thread-specific conditional breakpoint was hit, it won't necessarily be the thread
299 // that hit the breakpoint itself that evaluates the conditional expression, so the thread that hit
300 // the breakpoint could still be asked to stop, even though it hasn't been allowed to run since the
301 // previous stop.
302 if (thread_sp->GetTemporaryResumeState () != eStateSuspended || thread_sp->IsStillAtLastBreakpointHit())
Jim Ingham569aaf92015-11-06 22:45:57 +0000303 threads_copy.push_back(thread_sp);
304 }
305
306 // It is possible the threads we were allowing to run all exited and then maybe the user interrupted
307 // or something, then fall back on looking at all threads:
308
309 if (threads_copy.size() == 0)
310 threads_copy = m_threads;
Jim Inghamb42f3af2012-11-15 22:44:04 +0000311 }
312
313 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314
Greg Clayton2cad65a2010-09-03 17:10:42 +0000315 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000316 {
317 log->PutCString("");
Jim Ingham569aaf92015-11-06 22:45:57 +0000318 log->Printf ("ThreadList::%s: %" PRIu64 " threads, %" PRIu64 " unsuspended threads",
319 __FUNCTION__,
320 (uint64_t)m_threads.size(),
321 (uint64_t)threads_copy.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000322 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000323
Jim Inghama0079042013-03-21 21:46:56 +0000324 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000325
326 // If the event is an Interrupt event, then we're going to stop no matter what. Otherwise, presume we won't stop.
Jim Ingham7bc34652013-04-16 22:53:24 +0000327 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000328 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
329 {
330 if (log)
331 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
332
333 should_stop = true;
334 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000335
336 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
337 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
338 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
339 // We don't need to use it here, we just want to make sure it gets computed.
340
341 for (pos = threads_copy.begin(); pos != end; ++pos)
342 {
343 ThreadSP thread_sp(*pos);
344 thread_sp->GetStopInfo();
345 }
Jim Inghama0079042013-03-21 21:46:56 +0000346
Jim Inghamb42f3af2012-11-15 22:44:04 +0000347 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 {
349 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000350
Jim Ingham39fdae72014-01-15 03:32:42 +0000351 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
352 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
353 // the right thing to do and stopping gives the user control over what to do in this instance.
354 //
355 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
356 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
357 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
358 // code and causes us to stop seemingly for no reason.
359 //
360 // Since the only way we ever saw this error was on first attach, I'm only going to trigger set did_anybody_stop_for_a_reason
361 // to true unless this is the first stop.
362 //
363 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
364 // everywhere but at this check.
365
Ed Maste21afbe02014-01-17 17:31:06 +0000366 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000367 did_anybody_stop_for_a_reason = true;
368 else
369 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000370
Jim Ingham10c4b242011-10-15 00:23:43 +0000371 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000372 if (thread_should_stop)
373 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000375
Jim Inghama0079042013-03-21 21:46:56 +0000376 if (!should_stop && !did_anybody_stop_for_a_reason)
377 {
378 should_stop = true;
379 if (log)
380 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
381 }
382
Greg Clayton2cad65a2010-09-03 17:10:42 +0000383 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000384 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000385
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 if (should_stop)
387 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000388 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 {
390 ThreadSP thread_sp(*pos);
391 thread_sp->WillStop ();
392 }
393 }
394
395 return should_stop;
396}
397
398Vote
399ThreadList::ShouldReportStop (Event *event_ptr)
400{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000401 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000402
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 Vote result = eVoteNoOpinion;
404 m_process->UpdateThreadListIfNeeded();
405 collection::iterator pos, end = m_threads.end();
406
Greg Clayton5160ce52013-03-27 23:08:40 +0000407 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000408
409 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000410 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000411
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 // Run through the threads and ask whether we should report this event.
413 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
414 for (pos = m_threads.begin(); pos != end; ++pos)
415 {
416 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000417 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
418 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 {
Jim Ingham92087d82012-01-31 23:09:20 +0000420 case eVoteNoOpinion:
421 continue;
422
423 case eVoteYes:
424 result = eVoteYes;
425 break;
426
427 case eVoteNo:
428 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 {
Jim Ingham92087d82012-01-31 23:09:20 +0000430 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 }
Jim Ingham92087d82012-01-31 23:09:20 +0000432 else
433 {
434 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000435 log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s",
Jim Ingham92087d82012-01-31 23:09:20 +0000436 __FUNCTION__,
437 thread_sp->GetID (),
438 GetVoteAsCString (vote),
439 GetVoteAsCString (result));
440 }
441 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 }
443 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000444 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000445 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 return result;
447}
448
Jim Ingham221d51c2013-05-08 00:35:16 +0000449void
450ThreadList::SetShouldReportStop (Vote vote)
451{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000452 std::lock_guard<std::recursive_mutex> guard(GetMutex());
453
Jim Ingham221d51c2013-05-08 00:35:16 +0000454 m_process->UpdateThreadListIfNeeded();
455 collection::iterator pos, end = m_threads.end();
456 for (pos = m_threads.begin(); pos != end; ++pos)
457 {
458 ThreadSP thread_sp(*pos);
459 thread_sp->SetShouldReportStop (vote);
460 }
461}
462
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463Vote
464ThreadList::ShouldReportRun (Event *event_ptr)
465{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000466
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000467 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000468
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 Vote result = eVoteNoOpinion;
470 m_process->UpdateThreadListIfNeeded();
471 collection::iterator pos, end = m_threads.end();
472
473 // Run through the threads and ask whether we should report this event.
474 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
475
Greg Clayton5160ce52013-03-27 23:08:40 +0000476 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000477
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 for (pos = m_threads.begin(); pos != end; ++pos)
479 {
Jim Inghamce579832011-01-24 04:11:25 +0000480 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 {
Jim Inghamce579832011-01-24 04:11:25 +0000482 switch ((*pos)->ShouldReportRun (event_ptr))
483 {
484 case eVoteNoOpinion:
485 continue;
486 case eVoteYes:
487 if (result == eVoteNoOpinion)
488 result = eVoteYes;
489 break;
490 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000491 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000492 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000493 (*pos)->GetIndexID(),
494 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000495 result = eVoteNo;
496 break;
497 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 }
499 }
500 return result;
501}
502
503void
504ThreadList::Clear()
505{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000506 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507 m_stop_id = 0;
508 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000509 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510}
511
512void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000513ThreadList::Destroy()
514{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000515 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000516 const uint32_t num_threads = m_threads.size();
517 for (uint32_t idx = 0; idx < num_threads; ++idx)
518 {
519 m_threads[idx]->DestroyThread();
520 }
521}
522
523void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524ThreadList::RefreshStateAfterStop ()
525{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000526 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527
528 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000529
Greg Clayton5160ce52013-03-27 23:08:40 +0000530 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000531 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000532 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
534 collection::iterator pos, end = m_threads.end();
535 for (pos = m_threads.begin(); pos != end; ++pos)
536 (*pos)->RefreshStateAfterStop ();
537}
538
539void
540ThreadList::DiscardThreadPlans ()
541{
542 // You don't need to update the thread list here, because only threads
543 // that you currently know about have any thread plans.
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000544 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545
546 collection::iterator pos, end = m_threads.end();
547 for (pos = m_threads.begin(); pos != end; ++pos)
548 (*pos)->DiscardThreadPlans (true);
549
550}
551
552bool
553ThreadList::WillResume ()
554{
555 // Run through the threads and perform their momentary actions.
556 // But we only do this for threads that are running, user suspended
557 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000559 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 m_process->UpdateThreadListIfNeeded();
561
562 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563
Jim Inghama3241c12010-07-14 02:27:20 +0000564 // See if any thread wants to run stopping others. If it does, then we won't
565 // setup the other threads for resume, since they aren't going to get a chance
566 // to run. This is necessary because the SetupForResume might add "StopOthers"
567 // plans which would then get to be part of the who-gets-to-run negotiation, but
568 // they're coming in after the fact, and the threads that are already set up should
569 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570
Jim Inghama3241c12010-07-14 02:27:20 +0000571 bool wants_solo_run = false;
572
573 for (pos = m_threads.begin(); pos != end; ++pos)
574 {
Zachary Turner8f186f82015-11-13 21:53:03 +0000575 lldbassert((*pos)->GetCurrentPlan() && "thread should not have null thread plan");
576 if ((*pos)->GetResumeState() != eStateSuspended &&
Jim Inghama3241c12010-07-14 02:27:20 +0000577 (*pos)->GetCurrentPlan()->StopOthers())
578 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000579 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
580 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000581 wants_solo_run = true;
582 break;
583 }
584 }
585
Jim Ingham1c823b42011-01-22 01:33:44 +0000586 if (wants_solo_run)
587 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000588 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000589 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000590 log->Printf ("Turning on notification of new threads while single stepping a thread.");
591 m_process->StartNoticingNewThreads();
592 }
593 else
594 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000595 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000596 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000597 log->Printf ("Turning off notification of new threads while single stepping a thread.");
598 m_process->StopNoticingNewThreads();
599 }
Jim Inghama3241c12010-07-14 02:27:20 +0000600
601 // Give all the threads that are likely to run a last chance to set up their state before we
602 // negotiate who is actually going to get a chance to run...
603 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
604 // call setup on the threads that request StopOthers...
605
606 for (pos = m_threads.begin(); pos != end; ++pos)
607 {
608 if ((*pos)->GetResumeState() != eStateSuspended
609 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
610 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000611 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
612 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000613 (*pos)->SetupForResume ();
614 }
615 }
616
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 // Now go through the threads and see if any thread wants to run just itself.
618 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000619
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 ThreadList run_me_only_list (m_process);
621
622 run_me_only_list.SetStopID(m_process->GetStopID());
623
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 bool run_only_current_thread = false;
625
626 for (pos = m_threads.begin(); pos != end; ++pos)
627 {
628 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000629 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630 thread_sp->GetCurrentPlan()->StopOthers())
631 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000632 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
633 continue;
634
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 // You can't say "stop others" and also want yourself to be suspended.
636 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
637
Jim Ingham2976d002010-08-26 21:32:51 +0000638 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 {
Jim Inghamacbea8f2015-06-02 20:26:13 +0000640 // If the currently selected thread wants to run on its own, always let it.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 run_only_current_thread = true;
642 run_me_only_list.Clear();
643 run_me_only_list.AddThread (thread_sp);
644 break;
645 }
646
647 run_me_only_list.AddThread (thread_sp);
648 }
649
650 }
651
Jim Ingham513c6bb2012-09-01 01:02:41 +0000652 bool need_to_resume = true;
653
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000654 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655 {
656 // Everybody runs as they wish:
657 for (pos = m_threads.begin(); pos != end; ++pos)
658 {
659 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000660 StateType run_state;
661 if (thread_sp->GetResumeState() != eStateSuspended)
662 run_state = thread_sp->GetCurrentPlan()->RunState();
663 else
664 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000665 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000666 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 }
668 }
669 else
670 {
671 ThreadSP thread_to_run;
672
673 if (run_only_current_thread)
674 {
Jim Ingham2976d002010-08-26 21:32:51 +0000675 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 }
677 else if (run_me_only_list.GetSize (false) == 1)
678 {
679 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
680 }
681 else
682 {
683 int random_thread = (int)
684 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
685 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
686 }
687
688 for (pos = m_threads.begin(); pos != end; ++pos)
689 {
690 ThreadSP thread_sp(*pos);
691 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000692 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000693 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000694 need_to_resume = false;
695 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000697 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 }
699 }
700
Jim Ingham513c6bb2012-09-01 01:02:41 +0000701 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702}
703
704void
705ThreadList::DidResume ()
706{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000707 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 collection::iterator pos, end = m_threads.end();
709 for (pos = m_threads.begin(); pos != end; ++pos)
710 {
711 // Don't clear out threads that aren't going to get a chance to run, rather
712 // leave their state for the next time around.
713 ThreadSP thread_sp(*pos);
714 if (thread_sp->GetResumeState() != eStateSuspended)
715 thread_sp->DidResume ();
716 }
717}
718
Andrew Kaylor29d65742013-05-10 17:19:04 +0000719void
720ThreadList::DidStop ()
721{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000722 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Andrew Kaylor29d65742013-05-10 17:19:04 +0000723 collection::iterator pos, end = m_threads.end();
724 for (pos = m_threads.begin(); pos != end; ++pos)
725 {
726 // Notify threads that the process just stopped.
727 // Note, this currently assumes that all threads in the list
728 // stop when the process stops. In the future we will want to support
729 // a debugging model where some threads continue to run while others
730 // are stopped. We either need to handle that somehow here or
731 // create a special thread list containing only threads which will
732 // stop in the code that calls this method (currently
733 // Process::SetPrivateState).
734 ThreadSP thread_sp(*pos);
735 if (StateIsRunningState(thread_sp->GetState()))
736 thread_sp->DidStop ();
737 }
738}
739
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000741ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000743 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000744 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
745 if (!thread_sp.get())
746 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000747 if (m_threads.size() == 0)
748 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000749 m_selected_tid = m_threads[0]->GetID();
750 thread_sp = m_threads[0];
751 }
752 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753}
754
755bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000756ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000758 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000759 ThreadSP selected_thread_sp(FindThreadByID(tid));
760 if (selected_thread_sp)
761 {
Jim Ingham2976d002010-08-26 21:32:51 +0000762 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000763 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
764 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 else
Jim Ingham2976d002010-08-26 21:32:51 +0000766 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767
Jim Inghamc3faa192012-12-11 02:31:48 +0000768 if (notify)
769 NotifySelectedThreadChanged(m_selected_tid);
770
Jim Ingham2976d002010-08-26 21:32:51 +0000771 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772}
773
774bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000775ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000777 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000778 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
779 if (selected_thread_sp.get())
780 {
781 m_selected_tid = selected_thread_sp->GetID();
782 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
783 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 else
Jim Ingham2976d002010-08-26 21:32:51 +0000785 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786
Jim Inghamc3faa192012-12-11 02:31:48 +0000787 if (notify)
788 NotifySelectedThreadChanged(m_selected_tid);
789
Jim Ingham2976d002010-08-26 21:32:51 +0000790 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791}
792
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000793void
Jim Inghamc3faa192012-12-11 02:31:48 +0000794ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
795{
796 ThreadSP selected_thread_sp (FindThreadByID(tid));
797 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
798 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
799 new Thread::ThreadEventData(selected_thread_sp));
800}
801
802void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000803ThreadList::Update (ThreadList &rhs)
804{
805 if (this != &rhs)
806 {
807 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +0000808 // while the assignment occurs
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000809 std::lock_guard<std::recursive_mutex> guard(GetMutex());
810
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000811 m_process = rhs.m_process;
812 m_stop_id = rhs.m_stop_id;
813 m_threads.swap(rhs.m_threads);
814 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000815
816
817 // Now we look for threads that we are done with and
818 // make sure to clear them up as much as possible so
819 // anyone with a shared pointer will still have a reference,
820 // but the thread won't be of much use. Using std::weak_ptr
821 // for all backward references (such as a thread to a process)
822 // will eventually solve this issue for us, but for now, we
823 // need to work around the issue
824 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
825 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
826 {
827 const lldb::tid_t tid = (*rhs_pos)->GetID();
828 bool thread_is_alive = false;
829 const uint32_t num_threads = m_threads.size();
830 for (uint32_t idx = 0; idx < num_threads; ++idx)
831 {
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000832 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
833 if (m_threads[idx]->GetID() == tid || (backing_thread && backing_thread->GetID() == tid))
Greg Claytone1cd1be2012-01-29 20:56:30 +0000834 {
835 thread_is_alive = true;
836 break;
837 }
838 }
839 if (!thread_is_alive)
840 (*rhs_pos)->DestroyThread();
841 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000842 }
843}
844
Greg Claytonfa559e52012-05-18 02:38:05 +0000845void
846ThreadList::Flush ()
847{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000848 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000849 collection::iterator pos, end = m_threads.end();
850 for (pos = m_threads.begin(); pos != end; ++pos)
851 (*pos)->Flush ();
852}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000853
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000854std::recursive_mutex &
855ThreadList::GetMutex()
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000856{
857 return m_process->m_thread_mutex;
858}
859
Jim Ingham8d94ba02016-03-12 02:45:34 +0000860ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher (lldb::ThreadSP thread_sp) :
861 m_thread_list(nullptr),
862 m_tid(LLDB_INVALID_THREAD_ID)
863{
864 if (thread_sp)
865 {
866 m_tid = thread_sp->GetID();
867 m_thread_list = &thread_sp->GetProcess()->GetThreadList();
868 m_thread_list->PushExpressionExecutionThread(m_tid);
869 }
870}
871
872