blob: 1f8b351100a6634407caf470dcdd71746219d4c0 [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"
Andrew Kaylor14263f32013-05-08 23:15:46 +000014#include "lldb/Core/State.h"
Greg Clayton2cad65a2010-09-03 17:10:42 +000015#include "lldb/Target/RegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Target/ThreadList.h"
17#include "lldb/Target/Thread.h"
18#include "lldb/Target/ThreadPlan.h"
19#include "lldb/Target/Process.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24ThreadList::ThreadList (Process *process) :
25 m_process (process),
26 m_stop_id (0),
27 m_threads(),
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) :
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000033 m_process (rhs.m_process),
34 m_stop_id (rhs.m_stop_id),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 m_threads (),
Jim Ingham2976d002010-08-26 21:32:51 +000036 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037{
38 // Use the assignment operator since it uses the mutex
39 *this = rhs;
40}
41
42const ThreadList&
43ThreadList::operator = (const ThreadList& rhs)
44{
45 if (this != &rhs)
46 {
47 // Lock both mutexes to make sure neither side changes anyone on us
48 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000049 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050 m_process = rhs.m_process;
51 m_stop_id = rhs.m_stop_id;
52 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000053 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 }
55 return *this;
56}
57
58
59ThreadList::~ThreadList()
60{
Greg Claytonac358da2013-03-28 18:33:53 +000061 // Clear the thread list. Clear will take the mutex lock
62 // which will ensure that if anyone is using the list
63 // they won't get it removed while using it.
64 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
67
68uint32_t
69ThreadList::GetStopID () const
70{
71 return m_stop_id;
72}
73
74void
75ThreadList::SetStopID (uint32_t stop_id)
76{
77 m_stop_id = stop_id;
78}
79
80
81void
Greg Claytonc3776bf2012-02-09 06:16:32 +000082ThreadList::AddThread (const ThreadSP &thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000084 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 m_threads.push_back(thread_sp);
86}
87
88uint32_t
89ThreadList::GetSize (bool can_update)
90{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000091 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 if (can_update)
93 m_process->UpdateThreadListIfNeeded();
94 return m_threads.size();
95}
96
97ThreadSP
98ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
99{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000100 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 if (can_update)
102 m_process->UpdateThreadListIfNeeded();
103
104 ThreadSP thread_sp;
105 if (idx < m_threads.size())
106 thread_sp = m_threads[idx];
107 return thread_sp;
108}
109
110ThreadSP
111ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
112{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000113 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
115 if (can_update)
116 m_process->UpdateThreadListIfNeeded();
117
118 ThreadSP thread_sp;
119 uint32_t idx = 0;
120 const uint32_t num_threads = m_threads.size();
121 for (idx = 0; idx < num_threads; ++idx)
122 {
123 if (m_threads[idx]->GetID() == tid)
124 {
125 thread_sp = m_threads[idx];
126 break;
127 }
128 }
129 return thread_sp;
130}
131
132ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000133ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
134{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000135 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000136
137 if (can_update)
138 m_process->UpdateThreadListIfNeeded();
139
140 ThreadSP thread_sp;
141 uint32_t idx = 0;
142 const uint32_t num_threads = m_threads.size();
143 for (idx = 0; idx < num_threads; ++idx)
144 {
145 if (m_threads[idx]->GetProtocolID() == tid)
146 {
147 thread_sp = m_threads[idx];
148 break;
149 }
150 }
151 return thread_sp;
152}
153
154
155ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000156ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
157{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000158 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000159
160 if (can_update)
161 m_process->UpdateThreadListIfNeeded();
162
163 ThreadSP thread_sp;
164 uint32_t idx = 0;
165 const uint32_t num_threads = m_threads.size();
166 for (idx = 0; idx < num_threads; ++idx)
167 {
168 if (m_threads[idx]->GetID() == tid)
169 {
170 thread_sp = m_threads[idx];
171 m_threads.erase(m_threads.begin()+idx);
172 break;
173 }
174 }
175 return thread_sp;
176}
177
178ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000179ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
180{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000181 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000182
183 if (can_update)
184 m_process->UpdateThreadListIfNeeded();
185
186 ThreadSP thread_sp;
187 uint32_t idx = 0;
188 const uint32_t num_threads = m_threads.size();
189 for (idx = 0; idx < num_threads; ++idx)
190 {
191 if (m_threads[idx]->GetProtocolID() == tid)
192 {
193 thread_sp = m_threads[idx];
194 m_threads.erase(m_threads.begin()+idx);
195 break;
196 }
197 }
198 return thread_sp;
199}
200
201ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
203{
204 ThreadSP thread_sp;
205 if (thread_ptr)
206 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000207 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
209 uint32_t idx = 0;
210 const uint32_t num_threads = m_threads.size();
211 for (idx = 0; idx < num_threads; ++idx)
212 {
213 if (m_threads[idx].get() == thread_ptr)
214 {
215 thread_sp = m_threads[idx];
216 break;
217 }
218 }
219 }
220 return thread_sp;
221}
222
223
224
225ThreadSP
226ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
227{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000228 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229
230 if (can_update)
231 m_process->UpdateThreadListIfNeeded();
232
233 ThreadSP thread_sp;
234 const uint32_t num_threads = m_threads.size();
235 for (uint32_t idx = 0; idx < num_threads; ++idx)
236 {
237 if (m_threads[idx]->GetIndexID() == index_id)
238 {
239 thread_sp = m_threads[idx];
240 break;
241 }
242 }
243 return thread_sp;
244}
245
246bool
247ThreadList::ShouldStop (Event *event_ptr)
248{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 // Running events should never stop, obviously...
250
Greg Clayton5160ce52013-03-27 23:08:40 +0000251 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252
Jim Inghamb42f3af2012-11-15 22:44:04 +0000253 // The ShouldStop method of the threads can do a whole lot of work,
254 // running breakpoint commands & conditions, etc. So we don't want
255 // to keep the ThreadList locked the whole time we are doing this.
256 // FIXME: It is possible that running code could cause new threads
257 // to be created. If that happens we will miss asking them whether
258 // then should stop. This is not a big deal, since we haven't had
259 // a chance to hang any interesting operations on those threads yet.
260
261 collection threads_copy;
262 {
263 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000264 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
Jim Inghamb42f3af2012-11-15 22:44:04 +0000266 m_process->UpdateThreadListIfNeeded();
267 threads_copy = m_threads;
268 }
269
270 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
Greg Clayton2cad65a2010-09-03 17:10:42 +0000272 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000273 {
274 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000275 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000276 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000277
Jim Inghama0079042013-03-21 21:46:56 +0000278 bool did_anybody_stop_for_a_reason = false;
Jim Ingham7bc34652013-04-16 22:53:24 +0000279 bool should_stop = false;
280
281 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
282 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
283 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
284 // We don't need to use it here, we just want to make sure it gets computed.
285
286 for (pos = threads_copy.begin(); pos != end; ++pos)
287 {
288 ThreadSP thread_sp(*pos);
289 thread_sp->GetStopInfo();
290 }
Jim Inghama0079042013-03-21 21:46:56 +0000291
Jim Inghamb42f3af2012-11-15 22:44:04 +0000292 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 {
294 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000295
Jim Inghama0079042013-03-21 21:46:56 +0000296 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
297
Jim Ingham10c4b242011-10-15 00:23:43 +0000298 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000299 if (thread_should_stop)
300 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000302
Jim Inghama0079042013-03-21 21:46:56 +0000303 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
304 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
305 // the right thing to do and stopping gives the user control over what to do in this instance.
306
307 if (!should_stop && !did_anybody_stop_for_a_reason)
308 {
309 should_stop = true;
310 if (log)
311 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
312 }
313
Greg Clayton2cad65a2010-09-03 17:10:42 +0000314 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000315 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000316
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 if (should_stop)
318 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000319 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 {
321 ThreadSP thread_sp(*pos);
322 thread_sp->WillStop ();
323 }
324 }
325
326 return should_stop;
327}
328
329Vote
330ThreadList::ShouldReportStop (Event *event_ptr)
331{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000332 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000333
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 Vote result = eVoteNoOpinion;
335 m_process->UpdateThreadListIfNeeded();
336 collection::iterator pos, end = m_threads.end();
337
Greg Clayton5160ce52013-03-27 23:08:40 +0000338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000339
340 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000341 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 // Run through the threads and ask whether we should report this event.
344 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
345 for (pos = m_threads.begin(); pos != end; ++pos)
346 {
347 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000348 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
349 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 {
Jim Ingham92087d82012-01-31 23:09:20 +0000351 case eVoteNoOpinion:
352 continue;
353
354 case eVoteYes:
355 result = eVoteYes;
356 break;
357
358 case eVoteNo:
359 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 {
Jim Ingham92087d82012-01-31 23:09:20 +0000361 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 }
Jim Ingham92087d82012-01-31 23:09:20 +0000363 else
364 {
365 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000366 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 +0000367 __FUNCTION__,
368 thread_sp->GetID (),
369 GetVoteAsCString (vote),
370 GetVoteAsCString (result));
371 }
372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 }
374 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000375 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000376 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 return result;
378}
379
Jim Ingham221d51c2013-05-08 00:35:16 +0000380void
381ThreadList::SetShouldReportStop (Vote vote)
382{
383 Mutex::Locker locker(GetMutex());
384 m_process->UpdateThreadListIfNeeded();
385 collection::iterator pos, end = m_threads.end();
386 for (pos = m_threads.begin(); pos != end; ++pos)
387 {
388 ThreadSP thread_sp(*pos);
389 thread_sp->SetShouldReportStop (vote);
390 }
391}
392
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393Vote
394ThreadList::ShouldReportRun (Event *event_ptr)
395{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000396
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000397 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000398
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 Vote result = eVoteNoOpinion;
400 m_process->UpdateThreadListIfNeeded();
401 collection::iterator pos, end = m_threads.end();
402
403 // Run through the threads and ask whether we should report this event.
404 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
405
Greg Clayton5160ce52013-03-27 23:08:40 +0000406 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000407
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 for (pos = m_threads.begin(); pos != end; ++pos)
409 {
Jim Inghamce579832011-01-24 04:11:25 +0000410 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 {
Jim Inghamce579832011-01-24 04:11:25 +0000412 switch ((*pos)->ShouldReportRun (event_ptr))
413 {
414 case eVoteNoOpinion:
415 continue;
416 case eVoteYes:
417 if (result == eVoteNoOpinion)
418 result = eVoteYes;
419 break;
420 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000421 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000422 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000423 (*pos)->GetIndexID(),
424 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000425 result = eVoteNo;
426 break;
427 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 }
429 }
430 return result;
431}
432
433void
434ThreadList::Clear()
435{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000436 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 m_stop_id = 0;
438 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000439 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440}
441
442void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000443ThreadList::Destroy()
444{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000445 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000446 const uint32_t num_threads = m_threads.size();
447 for (uint32_t idx = 0; idx < num_threads; ++idx)
448 {
449 m_threads[idx]->DestroyThread();
450 }
451}
452
453void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454ThreadList::RefreshStateAfterStop ()
455{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000456 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457
458 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000459
Greg Clayton5160ce52013-03-27 23:08:40 +0000460 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000461 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000462 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
464 collection::iterator pos, end = m_threads.end();
465 for (pos = m_threads.begin(); pos != end; ++pos)
466 (*pos)->RefreshStateAfterStop ();
467}
468
469void
470ThreadList::DiscardThreadPlans ()
471{
472 // You don't need to update the thread list here, because only threads
473 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000474 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475
476 collection::iterator pos, end = m_threads.end();
477 for (pos = m_threads.begin(); pos != end; ++pos)
478 (*pos)->DiscardThreadPlans (true);
479
480}
481
482bool
483ThreadList::WillResume ()
484{
485 // Run through the threads and perform their momentary actions.
486 // But we only do this for threads that are running, user suspended
487 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000489 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 m_process->UpdateThreadListIfNeeded();
491
492 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
Jim Inghama3241c12010-07-14 02:27:20 +0000494 // See if any thread wants to run stopping others. If it does, then we won't
495 // setup the other threads for resume, since they aren't going to get a chance
496 // to run. This is necessary because the SetupForResume might add "StopOthers"
497 // plans which would then get to be part of the who-gets-to-run negotiation, but
498 // they're coming in after the fact, and the threads that are already set up should
499 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
Jim Inghama3241c12010-07-14 02:27:20 +0000501 bool wants_solo_run = false;
502
503 for (pos = m_threads.begin(); pos != end; ++pos)
504 {
505 if ((*pos)->GetResumeState() != eStateSuspended &&
506 (*pos)->GetCurrentPlan()->StopOthers())
507 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000508 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
509 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000510 wants_solo_run = true;
511 break;
512 }
513 }
514
Jim Ingham1c823b42011-01-22 01:33:44 +0000515 if (wants_solo_run)
516 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000517 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000518 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000519 log->Printf ("Turning on notification of new threads while single stepping a thread.");
520 m_process->StartNoticingNewThreads();
521 }
522 else
523 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000524 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000525 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000526 log->Printf ("Turning off notification of new threads while single stepping a thread.");
527 m_process->StopNoticingNewThreads();
528 }
Jim Inghama3241c12010-07-14 02:27:20 +0000529
530 // Give all the threads that are likely to run a last chance to set up their state before we
531 // negotiate who is actually going to get a chance to run...
532 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
533 // call setup on the threads that request StopOthers...
534
535 for (pos = m_threads.begin(); pos != end; ++pos)
536 {
537 if ((*pos)->GetResumeState() != eStateSuspended
538 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
539 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000540 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
541 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000542 (*pos)->SetupForResume ();
543 }
544 }
545
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 // Now go through the threads and see if any thread wants to run just itself.
547 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000548
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 ThreadList run_me_only_list (m_process);
550
551 run_me_only_list.SetStopID(m_process->GetStopID());
552
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 bool run_only_current_thread = false;
554
555 for (pos = m_threads.begin(); pos != end; ++pos)
556 {
557 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000558 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 thread_sp->GetCurrentPlan()->StopOthers())
560 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000561 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
562 continue;
563
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 // You can't say "stop others" and also want yourself to be suspended.
565 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
566
Jim Ingham2976d002010-08-26 21:32:51 +0000567 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 {
569 run_only_current_thread = true;
570 run_me_only_list.Clear();
571 run_me_only_list.AddThread (thread_sp);
572 break;
573 }
574
575 run_me_only_list.AddThread (thread_sp);
576 }
577
578 }
579
Jim Ingham513c6bb2012-09-01 01:02:41 +0000580 bool need_to_resume = true;
581
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000582 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 {
584 // Everybody runs as they wish:
585 for (pos = m_threads.begin(); pos != end; ++pos)
586 {
587 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000588 StateType run_state;
589 if (thread_sp->GetResumeState() != eStateSuspended)
590 run_state = thread_sp->GetCurrentPlan()->RunState();
591 else
592 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000593 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000594 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 }
596 }
597 else
598 {
599 ThreadSP thread_to_run;
600
601 if (run_only_current_thread)
602 {
Jim Ingham2976d002010-08-26 21:32:51 +0000603 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 }
605 else if (run_me_only_list.GetSize (false) == 1)
606 {
607 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
608 }
609 else
610 {
611 int random_thread = (int)
612 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
613 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
614 }
615
616 for (pos = m_threads.begin(); pos != end; ++pos)
617 {
618 ThreadSP thread_sp(*pos);
619 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000620 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000621 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000622 need_to_resume = false;
623 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000625 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 }
627 }
628
Jim Ingham513c6bb2012-09-01 01:02:41 +0000629 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630}
631
632void
633ThreadList::DidResume ()
634{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000635 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636 collection::iterator pos, end = m_threads.end();
637 for (pos = m_threads.begin(); pos != end; ++pos)
638 {
639 // Don't clear out threads that aren't going to get a chance to run, rather
640 // leave their state for the next time around.
641 ThreadSP thread_sp(*pos);
642 if (thread_sp->GetResumeState() != eStateSuspended)
643 thread_sp->DidResume ();
644 }
645}
646
Andrew Kaylor14263f32013-05-08 23:15:46 +0000647void
648ThreadList::DidStop ()
649{
650 Mutex::Locker locker(GetMutex());
651 collection::iterator pos, end = m_threads.end();
652 for (pos = m_threads.begin(); pos != end; ++pos)
653 {
654 // Notify threads that the process just stopped.
655 // Note, this currently assumes that all threads in the list
656 // stop when the process stops. In the future we will want to support
657 // a debugging model where some threads continue to run while others
658 // are stopped. We either need to handle that somehow here or
659 // create a special thread list containing only threads which will
660 // stop in the code that calls this method (currently
661 // Process::SetPrivateState).
662 ThreadSP thread_sp(*pos);
663 if (StateIsRunningState(thread_sp->GetState()))
664 thread_sp->DidStop ();
665 }
666}
667
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000669ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000671 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000672 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
673 if (!thread_sp.get())
674 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000675 if (m_threads.size() == 0)
676 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000677 m_selected_tid = m_threads[0]->GetID();
678 thread_sp = m_threads[0];
679 }
680 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681}
682
683bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000684ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000686 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000687 ThreadSP selected_thread_sp(FindThreadByID(tid));
688 if (selected_thread_sp)
689 {
Jim Ingham2976d002010-08-26 21:32:51 +0000690 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000691 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
692 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 else
Jim Ingham2976d002010-08-26 21:32:51 +0000694 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695
Jim Inghamc3faa192012-12-11 02:31:48 +0000696 if (notify)
697 NotifySelectedThreadChanged(m_selected_tid);
698
Jim Ingham2976d002010-08-26 21:32:51 +0000699 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700}
701
702bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000703ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000705 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000706 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
707 if (selected_thread_sp.get())
708 {
709 m_selected_tid = selected_thread_sp->GetID();
710 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
711 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 else
Jim Ingham2976d002010-08-26 21:32:51 +0000713 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714
Jim Inghamc3faa192012-12-11 02:31:48 +0000715 if (notify)
716 NotifySelectedThreadChanged(m_selected_tid);
717
Jim Ingham2976d002010-08-26 21:32:51 +0000718 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719}
720
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000721void
Jim Inghamc3faa192012-12-11 02:31:48 +0000722ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
723{
724 ThreadSP selected_thread_sp (FindThreadByID(tid));
725 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
726 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
727 new Thread::ThreadEventData(selected_thread_sp));
728}
729
730void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000731ThreadList::Update (ThreadList &rhs)
732{
733 if (this != &rhs)
734 {
735 // Lock both mutexes to make sure neither side changes anyone on us
736 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000737 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000738 m_process = rhs.m_process;
739 m_stop_id = rhs.m_stop_id;
740 m_threads.swap(rhs.m_threads);
741 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000742
743
744 // Now we look for threads that we are done with and
745 // make sure to clear them up as much as possible so
746 // anyone with a shared pointer will still have a reference,
747 // but the thread won't be of much use. Using std::weak_ptr
748 // for all backward references (such as a thread to a process)
749 // will eventually solve this issue for us, but for now, we
750 // need to work around the issue
751 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
752 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
753 {
754 const lldb::tid_t tid = (*rhs_pos)->GetID();
755 bool thread_is_alive = false;
756 const uint32_t num_threads = m_threads.size();
757 for (uint32_t idx = 0; idx < num_threads; ++idx)
758 {
759 if (m_threads[idx]->GetID() == tid)
760 {
761 thread_is_alive = true;
762 break;
763 }
764 }
765 if (!thread_is_alive)
766 (*rhs_pos)->DestroyThread();
767 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000768 }
769}
770
Greg Claytonfa559e52012-05-18 02:38:05 +0000771void
772ThreadList::Flush ()
773{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000774 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000775 collection::iterator pos, end = m_threads.end();
776 for (pos = m_threads.begin(); pos != end; ++pos)
777 (*pos)->Flush ();
778}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000779
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000780Mutex &
781ThreadList::GetMutex ()
782{
783 return m_process->m_thread_mutex;
784}
785