blob: d59d8059b36c86009d1b0f7df74585b237779304 [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(),
27 m_threads_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham2976d002010-08-26 21:32:51 +000028 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029{
30}
31
32ThreadList::ThreadList (const ThreadList &rhs) :
33 m_process (),
34 m_stop_id (),
35 m_threads (),
36 m_threads_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham2976d002010-08-26 21:32:51 +000037 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038{
39 // Use the assignment operator since it uses the mutex
40 *this = rhs;
41}
42
43const ThreadList&
44ThreadList::operator = (const ThreadList& rhs)
45{
46 if (this != &rhs)
47 {
48 // Lock both mutexes to make sure neither side changes anyone on us
49 // while the assignement occurs
Greg Claytonb1320972010-07-14 00:18:15 +000050 Mutex::Locker locker_lhs(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
52 m_process = rhs.m_process;
53 m_stop_id = rhs.m_stop_id;
54 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000055 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056 }
57 return *this;
58}
59
60
61ThreadList::~ThreadList()
62{
Greg Claytonac358da2013-03-28 18:33:53 +000063 // Clear the thread list. Clear will take the mutex lock
64 // which will ensure that if anyone is using the list
65 // they won't get it removed while using it.
66 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067}
68
69
70uint32_t
71ThreadList::GetStopID () const
72{
73 return m_stop_id;
74}
75
76void
77ThreadList::SetStopID (uint32_t stop_id)
78{
79 m_stop_id = stop_id;
80}
81
82
83void
Greg Claytonc3776bf2012-02-09 06:16:32 +000084ThreadList::AddThread (const ThreadSP &thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085{
86 Mutex::Locker locker(m_threads_mutex);
87 m_threads.push_back(thread_sp);
88}
89
90uint32_t
91ThreadList::GetSize (bool can_update)
92{
93 Mutex::Locker locker(m_threads_mutex);
94 if (can_update)
95 m_process->UpdateThreadListIfNeeded();
96 return m_threads.size();
97}
98
99ThreadSP
100ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
101{
102 Mutex::Locker locker(m_threads_mutex);
103 if (can_update)
104 m_process->UpdateThreadListIfNeeded();
105
106 ThreadSP thread_sp;
107 if (idx < m_threads.size())
108 thread_sp = m_threads[idx];
109 return thread_sp;
110}
111
112ThreadSP
113ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
114{
115 Mutex::Locker locker(m_threads_mutex);
116
117 if (can_update)
118 m_process->UpdateThreadListIfNeeded();
119
120 ThreadSP thread_sp;
121 uint32_t idx = 0;
122 const uint32_t num_threads = m_threads.size();
123 for (idx = 0; idx < num_threads; ++idx)
124 {
125 if (m_threads[idx]->GetID() == tid)
126 {
127 thread_sp = m_threads[idx];
128 break;
129 }
130 }
131 return thread_sp;
132}
133
134ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000135ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
136{
137 Mutex::Locker locker(m_threads_mutex);
138
139 if (can_update)
140 m_process->UpdateThreadListIfNeeded();
141
142 ThreadSP thread_sp;
143 uint32_t idx = 0;
144 const uint32_t num_threads = m_threads.size();
145 for (idx = 0; idx < num_threads; ++idx)
146 {
147 if (m_threads[idx]->GetProtocolID() == tid)
148 {
149 thread_sp = m_threads[idx];
150 break;
151 }
152 }
153 return thread_sp;
154}
155
156
157ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000158ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
159{
160 Mutex::Locker locker(m_threads_mutex);
161
162 if (can_update)
163 m_process->UpdateThreadListIfNeeded();
164
165 ThreadSP thread_sp;
166 uint32_t idx = 0;
167 const uint32_t num_threads = m_threads.size();
168 for (idx = 0; idx < num_threads; ++idx)
169 {
170 if (m_threads[idx]->GetID() == tid)
171 {
172 thread_sp = m_threads[idx];
173 m_threads.erase(m_threads.begin()+idx);
174 break;
175 }
176 }
177 return thread_sp;
178}
179
180ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000181ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
182{
183 Mutex::Locker locker(m_threads_mutex);
184
185 if (can_update)
186 m_process->UpdateThreadListIfNeeded();
187
188 ThreadSP thread_sp;
189 uint32_t idx = 0;
190 const uint32_t num_threads = m_threads.size();
191 for (idx = 0; idx < num_threads; ++idx)
192 {
193 if (m_threads[idx]->GetProtocolID() == tid)
194 {
195 thread_sp = m_threads[idx];
196 m_threads.erase(m_threads.begin()+idx);
197 break;
198 }
199 }
200 return thread_sp;
201}
202
203ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
205{
206 ThreadSP thread_sp;
207 if (thread_ptr)
208 {
209 Mutex::Locker locker(m_threads_mutex);
210
211 uint32_t idx = 0;
212 const uint32_t num_threads = m_threads.size();
213 for (idx = 0; idx < num_threads; ++idx)
214 {
215 if (m_threads[idx].get() == thread_ptr)
216 {
217 thread_sp = m_threads[idx];
218 break;
219 }
220 }
221 }
222 return thread_sp;
223}
224
225
226
227ThreadSP
228ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
229{
230 Mutex::Locker locker(m_threads_mutex);
231
232 if (can_update)
233 m_process->UpdateThreadListIfNeeded();
234
235 ThreadSP thread_sp;
236 const uint32_t num_threads = m_threads.size();
237 for (uint32_t idx = 0; idx < num_threads; ++idx)
238 {
239 if (m_threads[idx]->GetIndexID() == index_id)
240 {
241 thread_sp = m_threads[idx];
242 break;
243 }
244 }
245 return thread_sp;
246}
247
248bool
249ThreadList::ShouldStop (Event *event_ptr)
250{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 // Running events should never stop, obviously...
252
Greg Clayton5160ce52013-03-27 23:08:40 +0000253 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254
Jim Inghamb42f3af2012-11-15 22:44:04 +0000255 // The ShouldStop method of the threads can do a whole lot of work,
256 // running breakpoint commands & conditions, etc. So we don't want
257 // to keep the ThreadList locked the whole time we are doing this.
258 // FIXME: It is possible that running code could cause new threads
259 // to be created. If that happens we will miss asking them whether
260 // then should stop. This is not a big deal, since we haven't had
261 // a chance to hang any interesting operations on those threads yet.
262
263 collection threads_copy;
264 {
265 // Scope for locker
266 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
Jim Inghamb42f3af2012-11-15 22:44:04 +0000268 m_process->UpdateThreadListIfNeeded();
269 threads_copy = m_threads;
270 }
271
272 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273
Greg Clayton2cad65a2010-09-03 17:10:42 +0000274 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000275 {
276 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000277 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000278 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000279
Jim Inghama0079042013-03-21 21:46:56 +0000280 bool did_anybody_stop_for_a_reason = false;
Jim Ingham7bc34652013-04-16 22:53:24 +0000281 bool should_stop = false;
282
283 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
284 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
285 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
286 // We don't need to use it here, we just want to make sure it gets computed.
287
288 for (pos = threads_copy.begin(); pos != end; ++pos)
289 {
290 ThreadSP thread_sp(*pos);
291 thread_sp->GetStopInfo();
292 }
Jim Inghama0079042013-03-21 21:46:56 +0000293
Jim Inghamb42f3af2012-11-15 22:44:04 +0000294 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 {
296 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000297
Jim Inghama0079042013-03-21 21:46:56 +0000298 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
299
Jim Ingham10c4b242011-10-15 00:23:43 +0000300 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000301 if (thread_should_stop)
302 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000304
Jim Inghama0079042013-03-21 21:46:56 +0000305 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
306 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
307 // the right thing to do and stopping gives the user control over what to do in this instance.
308
309 if (!should_stop && !did_anybody_stop_for_a_reason)
310 {
311 should_stop = true;
312 if (log)
313 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
314 }
315
Greg Clayton2cad65a2010-09-03 17:10:42 +0000316 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000317 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000318
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 if (should_stop)
320 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000321 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 {
323 ThreadSP thread_sp(*pos);
324 thread_sp->WillStop ();
325 }
326 }
327
328 return should_stop;
329}
330
331Vote
332ThreadList::ShouldReportStop (Event *event_ptr)
333{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000334 Mutex::Locker locker(m_threads_mutex);
335
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 Vote result = eVoteNoOpinion;
337 m_process->UpdateThreadListIfNeeded();
338 collection::iterator pos, end = m_threads.end();
339
Greg Clayton5160ce52013-03-27 23:08:40 +0000340 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000341
342 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000343 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000344
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 // Run through the threads and ask whether we should report this event.
346 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
347 for (pos = m_threads.begin(); pos != end; ++pos)
348 {
349 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000350 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
351 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 {
Jim Ingham92087d82012-01-31 23:09:20 +0000353 case eVoteNoOpinion:
354 continue;
355
356 case eVoteYes:
357 result = eVoteYes;
358 break;
359
360 case eVoteNo:
361 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 {
Jim Ingham92087d82012-01-31 23:09:20 +0000363 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 }
Jim Ingham92087d82012-01-31 23:09:20 +0000365 else
366 {
367 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000368 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 +0000369 __FUNCTION__,
370 thread_sp->GetID (),
371 GetVoteAsCString (vote),
372 GetVoteAsCString (result));
373 }
374 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 }
376 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000377 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000378 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 return result;
380}
381
382Vote
383ThreadList::ShouldReportRun (Event *event_ptr)
384{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000385
386 Mutex::Locker locker(m_threads_mutex);
387
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 Vote result = eVoteNoOpinion;
389 m_process->UpdateThreadListIfNeeded();
390 collection::iterator pos, end = m_threads.end();
391
392 // Run through the threads and ask whether we should report this event.
393 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
394
Greg Clayton5160ce52013-03-27 23:08:40 +0000395 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000396
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 for (pos = m_threads.begin(); pos != end; ++pos)
398 {
Jim Inghamce579832011-01-24 04:11:25 +0000399 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 {
Jim Inghamce579832011-01-24 04:11:25 +0000401 switch ((*pos)->ShouldReportRun (event_ptr))
402 {
403 case eVoteNoOpinion:
404 continue;
405 case eVoteYes:
406 if (result == eVoteNoOpinion)
407 result = eVoteYes;
408 break;
409 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000410 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000411 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000412 (*pos)->GetIndexID(),
413 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000414 result = eVoteNo;
415 break;
416 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 }
418 }
419 return result;
420}
421
422void
423ThreadList::Clear()
424{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000425 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 m_stop_id = 0;
427 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000428 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429}
430
431void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000432ThreadList::Destroy()
433{
434 Mutex::Locker locker(m_threads_mutex);
435 const uint32_t num_threads = m_threads.size();
436 for (uint32_t idx = 0; idx < num_threads; ++idx)
437 {
438 m_threads[idx]->DestroyThread();
439 }
440}
441
442void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443ThreadList::RefreshStateAfterStop ()
444{
445 Mutex::Locker locker(m_threads_mutex);
446
447 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000448
Greg Clayton5160ce52013-03-27 23:08:40 +0000449 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000450 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000451 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
453 collection::iterator pos, end = m_threads.end();
454 for (pos = m_threads.begin(); pos != end; ++pos)
455 (*pos)->RefreshStateAfterStop ();
456}
457
458void
459ThreadList::DiscardThreadPlans ()
460{
461 // You don't need to update the thread list here, because only threads
462 // that you currently know about have any thread plans.
463 Mutex::Locker locker(m_threads_mutex);
464
465 collection::iterator pos, end = m_threads.end();
466 for (pos = m_threads.begin(); pos != end; ++pos)
467 (*pos)->DiscardThreadPlans (true);
468
469}
470
471bool
472ThreadList::WillResume ()
473{
474 // Run through the threads and perform their momentary actions.
475 // But we only do this for threads that are running, user suspended
476 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477
478 Mutex::Locker locker(m_threads_mutex);
479 m_process->UpdateThreadListIfNeeded();
480
481 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482
Jim Inghama3241c12010-07-14 02:27:20 +0000483 // See if any thread wants to run stopping others. If it does, then we won't
484 // setup the other threads for resume, since they aren't going to get a chance
485 // to run. This is necessary because the SetupForResume might add "StopOthers"
486 // plans which would then get to be part of the who-gets-to-run negotiation, but
487 // they're coming in after the fact, and the threads that are already set up should
488 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489
Jim Inghama3241c12010-07-14 02:27:20 +0000490 bool wants_solo_run = false;
491
492 for (pos = m_threads.begin(); pos != end; ++pos)
493 {
494 if ((*pos)->GetResumeState() != eStateSuspended &&
495 (*pos)->GetCurrentPlan()->StopOthers())
496 {
497 wants_solo_run = true;
498 break;
499 }
500 }
501
Jim Ingham1c823b42011-01-22 01:33:44 +0000502 if (wants_solo_run)
503 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000504 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000505 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000506 log->Printf ("Turning on notification of new threads while single stepping a thread.");
507 m_process->StartNoticingNewThreads();
508 }
509 else
510 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000511 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000512 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000513 log->Printf ("Turning off notification of new threads while single stepping a thread.");
514 m_process->StopNoticingNewThreads();
515 }
Jim Inghama3241c12010-07-14 02:27:20 +0000516
517 // Give all the threads that are likely to run a last chance to set up their state before we
518 // negotiate who is actually going to get a chance to run...
519 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
520 // call setup on the threads that request StopOthers...
521
522 for (pos = m_threads.begin(); pos != end; ++pos)
523 {
524 if ((*pos)->GetResumeState() != eStateSuspended
525 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
526 {
527 (*pos)->SetupForResume ();
528 }
529 }
530
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 // Now go through the threads and see if any thread wants to run just itself.
532 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000533
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 ThreadList run_me_only_list (m_process);
535
536 run_me_only_list.SetStopID(m_process->GetStopID());
537
538 ThreadSP immediate_thread_sp;
539 bool run_only_current_thread = false;
540
541 for (pos = m_threads.begin(); pos != end; ++pos)
542 {
543 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000544 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 thread_sp->GetCurrentPlan()->StopOthers())
546 {
547 // You can't say "stop others" and also want yourself to be suspended.
548 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
549
Jim Ingham2976d002010-08-26 21:32:51 +0000550 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551 {
552 run_only_current_thread = true;
553 run_me_only_list.Clear();
554 run_me_only_list.AddThread (thread_sp);
555 break;
556 }
557
558 run_me_only_list.AddThread (thread_sp);
559 }
560
561 }
562
Jim Ingham513c6bb2012-09-01 01:02:41 +0000563 bool need_to_resume = true;
564
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 if (immediate_thread_sp)
566 {
567 for (pos = m_threads.begin(); pos != end; ++pos)
568 {
569 ThreadSP thread_sp(*pos);
570 if (thread_sp.get() == immediate_thread_sp.get())
Greg Clayton160c9d82013-05-01 21:54:04 +0000571 thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000573 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 }
575 }
576 else if (run_me_only_list.GetSize (false) == 0)
577 {
578 // Everybody runs as they wish:
579 for (pos = m_threads.begin(); pos != end; ++pos)
580 {
581 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000582 StateType run_state;
583 if (thread_sp->GetResumeState() != eStateSuspended)
584 run_state = thread_sp->GetCurrentPlan()->RunState();
585 else
586 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000587 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000588 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589 }
590 }
591 else
592 {
593 ThreadSP thread_to_run;
594
595 if (run_only_current_thread)
596 {
Jim Ingham2976d002010-08-26 21:32:51 +0000597 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
599 else if (run_me_only_list.GetSize (false) == 1)
600 {
601 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
602 }
603 else
604 {
605 int random_thread = (int)
606 ((run_me_only_list.GetSize (false) * (double) rand ()) / (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 {
612 ThreadSP thread_sp(*pos);
613 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000614 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000615 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000616 need_to_resume = false;
617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000619 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 }
621 }
622
Jim Ingham513c6bb2012-09-01 01:02:41 +0000623 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624}
625
626void
627ThreadList::DidResume ()
628{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000629 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630 collection::iterator pos, end = m_threads.end();
631 for (pos = m_threads.begin(); pos != end; ++pos)
632 {
633 // Don't clear out threads that aren't going to get a chance to run, rather
634 // leave their state for the next time around.
635 ThreadSP thread_sp(*pos);
636 if (thread_sp->GetResumeState() != eStateSuspended)
637 thread_sp->DidResume ();
638 }
639}
640
641ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000642ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643{
644 Mutex::Locker locker(m_threads_mutex);
Johnny Chen943ddb72011-08-25 21:59:59 +0000645 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
646 if (!thread_sp.get())
647 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000648 if (m_threads.size() == 0)
649 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000650 m_selected_tid = m_threads[0]->GetID();
651 thread_sp = m_threads[0];
652 }
653 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654}
655
656bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000657ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658{
659 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000660 ThreadSP selected_thread_sp(FindThreadByID(tid));
661 if (selected_thread_sp)
662 {
Jim Ingham2976d002010-08-26 21:32:51 +0000663 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000664 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
665 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 else
Jim Ingham2976d002010-08-26 21:32:51 +0000667 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668
Jim Inghamc3faa192012-12-11 02:31:48 +0000669 if (notify)
670 NotifySelectedThreadChanged(m_selected_tid);
671
Jim Ingham2976d002010-08-26 21:32:51 +0000672 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673}
674
675bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000676ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677{
678 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000679 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
680 if (selected_thread_sp.get())
681 {
682 m_selected_tid = selected_thread_sp->GetID();
683 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
684 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 else
Jim Ingham2976d002010-08-26 21:32:51 +0000686 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687
Jim Inghamc3faa192012-12-11 02:31:48 +0000688 if (notify)
689 NotifySelectedThreadChanged(m_selected_tid);
690
Jim Ingham2976d002010-08-26 21:32:51 +0000691 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692}
693
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000694void
Jim Inghamc3faa192012-12-11 02:31:48 +0000695ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
696{
697 ThreadSP selected_thread_sp (FindThreadByID(tid));
698 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
699 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
700 new Thread::ThreadEventData(selected_thread_sp));
701}
702
703void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000704ThreadList::Update (ThreadList &rhs)
705{
706 if (this != &rhs)
707 {
708 // Lock both mutexes to make sure neither side changes anyone on us
709 // while the assignement occurs
710 Mutex::Locker locker_lhs(m_threads_mutex);
711 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
712 m_process = rhs.m_process;
713 m_stop_id = rhs.m_stop_id;
714 m_threads.swap(rhs.m_threads);
715 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000716
717
718 // Now we look for threads that we are done with and
719 // make sure to clear them up as much as possible so
720 // anyone with a shared pointer will still have a reference,
721 // but the thread won't be of much use. Using std::weak_ptr
722 // for all backward references (such as a thread to a process)
723 // will eventually solve this issue for us, but for now, we
724 // need to work around the issue
725 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
726 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
727 {
728 const lldb::tid_t tid = (*rhs_pos)->GetID();
729 bool thread_is_alive = false;
730 const uint32_t num_threads = m_threads.size();
731 for (uint32_t idx = 0; idx < num_threads; ++idx)
732 {
733 if (m_threads[idx]->GetID() == tid)
734 {
735 thread_is_alive = true;
736 break;
737 }
738 }
739 if (!thread_is_alive)
740 (*rhs_pos)->DestroyThread();
741 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000742 }
743}
744
Greg Claytonfa559e52012-05-18 02:38:05 +0000745void
746ThreadList::Flush ()
747{
748 Mutex::Locker locker(m_threads_mutex);
749 collection::iterator pos, end = m_threads.end();
750 for (pos = m_threads.begin(); pos != end; ++pos)
751 (*pos)->Flush ();
752}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000753