blob: 80df5957c3fce23d9c0ae894856a8a7578ab0dbd [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//===----------------------------------------------------------------------===//
9#include <stdlib.h>
10
11#include <algorithm>
12
Greg Clayton2cad65a2010-09-03 17:10:42 +000013#include "lldb/Core/Log.h"
14#include "lldb/Target/RegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Target/ThreadList.h"
16#include "lldb/Target/Thread.h"
17#include "lldb/Target/ThreadPlan.h"
18#include "lldb/Target/Process.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
23ThreadList::ThreadList (Process *process) :
24 m_process (process),
25 m_stop_id (0),
26 m_threads(),
Jim Ingham2976d002010-08-26 21:32:51 +000027 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028{
29}
30
31ThreadList::ThreadList (const ThreadList &rhs) :
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000032 m_process (rhs.m_process),
33 m_stop_id (rhs.m_stop_id),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034 m_threads (),
Jim Ingham2976d002010-08-26 21:32:51 +000035 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036{
37 // Use the assignment operator since it uses the mutex
38 *this = rhs;
39}
40
41const ThreadList&
42ThreadList::operator = (const ThreadList& rhs)
43{
44 if (this != &rhs)
45 {
46 // Lock both mutexes to make sure neither side changes anyone on us
47 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000048 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 m_process = rhs.m_process;
50 m_stop_id = rhs.m_stop_id;
51 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000052 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 }
54 return *this;
55}
56
57
58ThreadList::~ThreadList()
59{
Greg Claytonac358da2013-03-28 18:33:53 +000060 // Clear the thread list. Clear will take the mutex lock
61 // which will ensure that if anyone is using the list
62 // they won't get it removed while using it.
63 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
66
67uint32_t
68ThreadList::GetStopID () const
69{
70 return m_stop_id;
71}
72
73void
74ThreadList::SetStopID (uint32_t stop_id)
75{
76 m_stop_id = stop_id;
77}
78
79
80void
Greg Claytonc3776bf2012-02-09 06:16:32 +000081ThreadList::AddThread (const ThreadSP &thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000083 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 m_threads.push_back(thread_sp);
85}
86
87uint32_t
88ThreadList::GetSize (bool can_update)
89{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000090 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 if (can_update)
92 m_process->UpdateThreadListIfNeeded();
93 return m_threads.size();
94}
95
96ThreadSP
97ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
98{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000099 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 if (can_update)
101 m_process->UpdateThreadListIfNeeded();
102
103 ThreadSP thread_sp;
104 if (idx < m_threads.size())
105 thread_sp = m_threads[idx];
106 return thread_sp;
107}
108
109ThreadSP
110ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
111{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000112 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
114 if (can_update)
115 m_process->UpdateThreadListIfNeeded();
116
117 ThreadSP thread_sp;
118 uint32_t idx = 0;
119 const uint32_t num_threads = m_threads.size();
120 for (idx = 0; idx < num_threads; ++idx)
121 {
122 if (m_threads[idx]->GetID() == tid)
123 {
124 thread_sp = m_threads[idx];
125 break;
126 }
127 }
128 return thread_sp;
129}
130
131ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000132ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
133{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000134 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000135
136 if (can_update)
137 m_process->UpdateThreadListIfNeeded();
138
139 ThreadSP thread_sp;
140 uint32_t idx = 0;
141 const uint32_t num_threads = m_threads.size();
142 for (idx = 0; idx < num_threads; ++idx)
143 {
144 if (m_threads[idx]->GetProtocolID() == tid)
145 {
146 thread_sp = m_threads[idx];
147 break;
148 }
149 }
150 return thread_sp;
151}
152
153
154ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000155ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
156{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000157 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000158
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 {
167 if (m_threads[idx]->GetID() == tid)
168 {
169 thread_sp = m_threads[idx];
170 m_threads.erase(m_threads.begin()+idx);
171 break;
172 }
173 }
174 return thread_sp;
175}
176
177ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000178ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
179{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000180 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000181
182 if (can_update)
183 m_process->UpdateThreadListIfNeeded();
184
185 ThreadSP thread_sp;
186 uint32_t idx = 0;
187 const uint32_t num_threads = m_threads.size();
188 for (idx = 0; idx < num_threads; ++idx)
189 {
190 if (m_threads[idx]->GetProtocolID() == tid)
191 {
192 thread_sp = m_threads[idx];
193 m_threads.erase(m_threads.begin()+idx);
194 break;
195 }
196 }
197 return thread_sp;
198}
199
200ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
202{
203 ThreadSP thread_sp;
204 if (thread_ptr)
205 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000206 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207
208 uint32_t idx = 0;
209 const uint32_t num_threads = m_threads.size();
210 for (idx = 0; idx < num_threads; ++idx)
211 {
212 if (m_threads[idx].get() == thread_ptr)
213 {
214 thread_sp = m_threads[idx];
215 break;
216 }
217 }
218 }
219 return thread_sp;
220}
221
222
223
224ThreadSP
225ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
226{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000227 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228
229 if (can_update)
230 m_process->UpdateThreadListIfNeeded();
231
232 ThreadSP thread_sp;
233 const uint32_t num_threads = m_threads.size();
234 for (uint32_t idx = 0; idx < num_threads; ++idx)
235 {
236 if (m_threads[idx]->GetIndexID() == index_id)
237 {
238 thread_sp = m_threads[idx];
239 break;
240 }
241 }
242 return thread_sp;
243}
244
245bool
246ThreadList::ShouldStop (Event *event_ptr)
247{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 // Running events should never stop, obviously...
249
Greg Clayton5160ce52013-03-27 23:08:40 +0000250 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251
Jim Inghamb42f3af2012-11-15 22:44:04 +0000252 // The ShouldStop method of the threads can do a whole lot of work,
253 // running breakpoint commands & conditions, etc. So we don't want
254 // to keep the ThreadList locked the whole time we are doing this.
255 // FIXME: It is possible that running code could cause new threads
256 // to be created. If that happens we will miss asking them whether
257 // then should stop. This is not a big deal, since we haven't had
258 // a chance to hang any interesting operations on those threads yet.
259
260 collection threads_copy;
261 {
262 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000263 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
Jim Inghamb42f3af2012-11-15 22:44:04 +0000265 m_process->UpdateThreadListIfNeeded();
266 threads_copy = m_threads;
267 }
268
269 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270
Greg Clayton2cad65a2010-09-03 17:10:42 +0000271 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000272 {
273 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000274 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000275 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000276
Jim Inghama0079042013-03-21 21:46:56 +0000277 bool did_anybody_stop_for_a_reason = false;
Jim Ingham7bc34652013-04-16 22:53:24 +0000278 bool should_stop = false;
279
280 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
281 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
282 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
283 // We don't need to use it here, we just want to make sure it gets computed.
284
285 for (pos = threads_copy.begin(); pos != end; ++pos)
286 {
287 ThreadSP thread_sp(*pos);
288 thread_sp->GetStopInfo();
289 }
Jim Inghama0079042013-03-21 21:46:56 +0000290
Jim Inghamb42f3af2012-11-15 22:44:04 +0000291 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 {
293 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000294
Jim Inghama0079042013-03-21 21:46:56 +0000295 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
296
Jim Ingham10c4b242011-10-15 00:23:43 +0000297 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000298 if (thread_should_stop)
299 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000301
Jim Inghama0079042013-03-21 21:46:56 +0000302 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
303 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
304 // the right thing to do and stopping gives the user control over what to do in this instance.
305
306 if (!should_stop && !did_anybody_stop_for_a_reason)
307 {
308 should_stop = true;
309 if (log)
310 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
311 }
312
Greg Clayton2cad65a2010-09-03 17:10:42 +0000313 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000314 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000315
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 if (should_stop)
317 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000318 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 {
320 ThreadSP thread_sp(*pos);
321 thread_sp->WillStop ();
322 }
323 }
324
325 return should_stop;
326}
327
328Vote
329ThreadList::ShouldReportStop (Event *event_ptr)
330{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000331 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000332
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 Vote result = eVoteNoOpinion;
334 m_process->UpdateThreadListIfNeeded();
335 collection::iterator pos, end = m_threads.end();
336
Greg Clayton5160ce52013-03-27 23:08:40 +0000337 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000338
339 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000340 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000341
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 // Run through the threads and ask whether we should report this event.
343 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
344 for (pos = m_threads.begin(); pos != end; ++pos)
345 {
346 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000347 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
348 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 {
Jim Ingham92087d82012-01-31 23:09:20 +0000350 case eVoteNoOpinion:
351 continue;
352
353 case eVoteYes:
354 result = eVoteYes;
355 break;
356
357 case eVoteNo:
358 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 {
Jim Ingham92087d82012-01-31 23:09:20 +0000360 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361 }
Jim Ingham92087d82012-01-31 23:09:20 +0000362 else
363 {
364 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000365 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 +0000366 __FUNCTION__,
367 thread_sp->GetID (),
368 GetVoteAsCString (vote),
369 GetVoteAsCString (result));
370 }
371 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 }
373 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000374 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000375 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 return result;
377}
378
379Vote
380ThreadList::ShouldReportRun (Event *event_ptr)
381{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000382
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000383 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 Vote result = eVoteNoOpinion;
386 m_process->UpdateThreadListIfNeeded();
387 collection::iterator pos, end = m_threads.end();
388
389 // Run through the threads and ask whether we should report this event.
390 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
391
Greg Clayton5160ce52013-03-27 23:08:40 +0000392 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000393
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 for (pos = m_threads.begin(); pos != end; ++pos)
395 {
Jim Inghamce579832011-01-24 04:11:25 +0000396 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 {
Jim Inghamce579832011-01-24 04:11:25 +0000398 switch ((*pos)->ShouldReportRun (event_ptr))
399 {
400 case eVoteNoOpinion:
401 continue;
402 case eVoteYes:
403 if (result == eVoteNoOpinion)
404 result = eVoteYes;
405 break;
406 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000407 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000408 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000409 (*pos)->GetIndexID(),
410 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000411 result = eVoteNo;
412 break;
413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 }
415 }
416 return result;
417}
418
419void
420ThreadList::Clear()
421{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000422 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 m_stop_id = 0;
424 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000425 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
428void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000429ThreadList::Destroy()
430{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000431 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000432 const uint32_t num_threads = m_threads.size();
433 for (uint32_t idx = 0; idx < num_threads; ++idx)
434 {
435 m_threads[idx]->DestroyThread();
436 }
437}
438
439void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440ThreadList::RefreshStateAfterStop ()
441{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000442 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
444 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000445
Greg Clayton5160ce52013-03-27 23:08:40 +0000446 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000447 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000448 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449
450 collection::iterator pos, end = m_threads.end();
451 for (pos = m_threads.begin(); pos != end; ++pos)
452 (*pos)->RefreshStateAfterStop ();
453}
454
455void
456ThreadList::DiscardThreadPlans ()
457{
458 // You don't need to update the thread list here, because only threads
459 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000460 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461
462 collection::iterator pos, end = m_threads.end();
463 for (pos = m_threads.begin(); pos != end; ++pos)
464 (*pos)->DiscardThreadPlans (true);
465
466}
467
468bool
469ThreadList::WillResume ()
470{
471 // Run through the threads and perform their momentary actions.
472 // But we only do this for threads that are running, user suspended
473 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000475 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 m_process->UpdateThreadListIfNeeded();
477
478 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479
Jim Inghama3241c12010-07-14 02:27:20 +0000480 // See if any thread wants to run stopping others. If it does, then we won't
481 // setup the other threads for resume, since they aren't going to get a chance
482 // to run. This is necessary because the SetupForResume might add "StopOthers"
483 // plans which would then get to be part of the who-gets-to-run negotiation, but
484 // they're coming in after the fact, and the threads that are already set up should
485 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486
Jim Inghama3241c12010-07-14 02:27:20 +0000487 bool wants_solo_run = false;
488
489 for (pos = m_threads.begin(); pos != end; ++pos)
490 {
491 if ((*pos)->GetResumeState() != eStateSuspended &&
492 (*pos)->GetCurrentPlan()->StopOthers())
493 {
494 wants_solo_run = true;
495 break;
496 }
497 }
498
Jim Ingham1c823b42011-01-22 01:33:44 +0000499 if (wants_solo_run)
500 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000501 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000502 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000503 log->Printf ("Turning on notification of new threads while single stepping a thread.");
504 m_process->StartNoticingNewThreads();
505 }
506 else
507 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000508 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000509 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000510 log->Printf ("Turning off notification of new threads while single stepping a thread.");
511 m_process->StopNoticingNewThreads();
512 }
Jim Inghama3241c12010-07-14 02:27:20 +0000513
514 // Give all the threads that are likely to run a last chance to set up their state before we
515 // negotiate who is actually going to get a chance to run...
516 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
517 // call setup on the threads that request StopOthers...
518
519 for (pos = m_threads.begin(); pos != end; ++pos)
520 {
521 if ((*pos)->GetResumeState() != eStateSuspended
522 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
523 {
524 (*pos)->SetupForResume ();
525 }
526 }
527
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 // Now go through the threads and see if any thread wants to run just itself.
529 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000530
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 ThreadList run_me_only_list (m_process);
532
533 run_me_only_list.SetStopID(m_process->GetStopID());
534
535 ThreadSP immediate_thread_sp;
536 bool run_only_current_thread = false;
537
538 for (pos = m_threads.begin(); pos != end; ++pos)
539 {
540 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000541 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 thread_sp->GetCurrentPlan()->StopOthers())
543 {
544 // You can't say "stop others" and also want yourself to be suspended.
545 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
546
Jim Ingham2976d002010-08-26 21:32:51 +0000547 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 {
549 run_only_current_thread = true;
550 run_me_only_list.Clear();
551 run_me_only_list.AddThread (thread_sp);
552 break;
553 }
554
555 run_me_only_list.AddThread (thread_sp);
556 }
557
558 }
559
Jim Ingham513c6bb2012-09-01 01:02:41 +0000560 bool need_to_resume = true;
561
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 if (immediate_thread_sp)
563 {
564 for (pos = m_threads.begin(); pos != end; ++pos)
565 {
566 ThreadSP thread_sp(*pos);
567 if (thread_sp.get() == immediate_thread_sp.get())
Greg Clayton160c9d82013-05-01 21:54:04 +0000568 thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000570 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571 }
572 }
573 else if (run_me_only_list.GetSize (false) == 0)
574 {
575 // Everybody runs as they wish:
576 for (pos = m_threads.begin(); pos != end; ++pos)
577 {
578 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000579 StateType run_state;
580 if (thread_sp->GetResumeState() != eStateSuspended)
581 run_state = thread_sp->GetCurrentPlan()->RunState();
582 else
583 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000584 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000585 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 }
587 }
588 else
589 {
590 ThreadSP thread_to_run;
591
592 if (run_only_current_thread)
593 {
Jim Ingham2976d002010-08-26 21:32:51 +0000594 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 }
596 else if (run_me_only_list.GetSize (false) == 1)
597 {
598 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
599 }
600 else
601 {
602 int random_thread = (int)
603 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
604 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
605 }
606
607 for (pos = m_threads.begin(); pos != end; ++pos)
608 {
609 ThreadSP thread_sp(*pos);
610 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000611 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000612 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000613 need_to_resume = false;
614 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000616 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 }
618 }
619
Jim Ingham513c6bb2012-09-01 01:02:41 +0000620 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}
622
623void
624ThreadList::DidResume ()
625{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000626 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 collection::iterator pos, end = m_threads.end();
628 for (pos = m_threads.begin(); pos != end; ++pos)
629 {
630 // Don't clear out threads that aren't going to get a chance to run, rather
631 // leave their state for the next time around.
632 ThreadSP thread_sp(*pos);
633 if (thread_sp->GetResumeState() != eStateSuspended)
634 thread_sp->DidResume ();
635 }
636}
637
638ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000639ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000641 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000642 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
643 if (!thread_sp.get())
644 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000645 if (m_threads.size() == 0)
646 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000647 m_selected_tid = m_threads[0]->GetID();
648 thread_sp = m_threads[0];
649 }
650 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651}
652
653bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000654ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000656 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000657 ThreadSP selected_thread_sp(FindThreadByID(tid));
658 if (selected_thread_sp)
659 {
Jim Ingham2976d002010-08-26 21:32:51 +0000660 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000661 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
662 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 else
Jim Ingham2976d002010-08-26 21:32:51 +0000664 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665
Jim Inghamc3faa192012-12-11 02:31:48 +0000666 if (notify)
667 NotifySelectedThreadChanged(m_selected_tid);
668
Jim Ingham2976d002010-08-26 21:32:51 +0000669 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670}
671
672bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000673ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000675 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000676 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
677 if (selected_thread_sp.get())
678 {
679 m_selected_tid = selected_thread_sp->GetID();
680 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
681 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 else
Jim Ingham2976d002010-08-26 21:32:51 +0000683 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684
Jim Inghamc3faa192012-12-11 02:31:48 +0000685 if (notify)
686 NotifySelectedThreadChanged(m_selected_tid);
687
Jim Ingham2976d002010-08-26 21:32:51 +0000688 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689}
690
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000691void
Jim Inghamc3faa192012-12-11 02:31:48 +0000692ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
693{
694 ThreadSP selected_thread_sp (FindThreadByID(tid));
695 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
696 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
697 new Thread::ThreadEventData(selected_thread_sp));
698}
699
700void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000701ThreadList::Update (ThreadList &rhs)
702{
703 if (this != &rhs)
704 {
705 // Lock both mutexes to make sure neither side changes anyone on us
706 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000707 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000708 m_process = rhs.m_process;
709 m_stop_id = rhs.m_stop_id;
710 m_threads.swap(rhs.m_threads);
711 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000712
713
714 // Now we look for threads that we are done with and
715 // make sure to clear them up as much as possible so
716 // anyone with a shared pointer will still have a reference,
717 // but the thread won't be of much use. Using std::weak_ptr
718 // for all backward references (such as a thread to a process)
719 // will eventually solve this issue for us, but for now, we
720 // need to work around the issue
721 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
722 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
723 {
724 const lldb::tid_t tid = (*rhs_pos)->GetID();
725 bool thread_is_alive = false;
726 const uint32_t num_threads = m_threads.size();
727 for (uint32_t idx = 0; idx < num_threads; ++idx)
728 {
729 if (m_threads[idx]->GetID() == tid)
730 {
731 thread_is_alive = true;
732 break;
733 }
734 }
735 if (!thread_is_alive)
736 (*rhs_pos)->DestroyThread();
737 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000738 }
739}
740
Greg Claytonfa559e52012-05-18 02:38:05 +0000741void
742ThreadList::Flush ()
743{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000744 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000745 collection::iterator pos, end = m_threads.end();
746 for (pos = m_threads.begin(); pos != end; ++pos)
747 (*pos)->Flush ();
748}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000749
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000750Mutex &
751ThreadList::GetMutex ()
752{
753 return m_process->m_thread_mutex;
754}
755