blob: 7fb16fdac5c03fa0ec64b50eb9126c4c8b465d5c [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 Kaylor29d65742013-05-10 17:19:04 +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
Greg Claytone98008c2014-02-13 23:34:38 +000088void
89ThreadList::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx)
90{
91 Mutex::Locker locker(GetMutex());
92 if (idx < m_threads.size())
93 m_threads.insert(m_threads.begin() + idx, thread_sp);
94 else
95 m_threads.push_back (thread_sp);
96}
97
98
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099uint32_t
100ThreadList::GetSize (bool can_update)
101{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000102 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 if (can_update)
104 m_process->UpdateThreadListIfNeeded();
105 return m_threads.size();
106}
107
108ThreadSP
109ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
110{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000111 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 if (can_update)
113 m_process->UpdateThreadListIfNeeded();
114
115 ThreadSP thread_sp;
116 if (idx < m_threads.size())
117 thread_sp = m_threads[idx];
118 return thread_sp;
119}
120
121ThreadSP
122ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
123{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000124 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
126 if (can_update)
127 m_process->UpdateThreadListIfNeeded();
128
129 ThreadSP thread_sp;
130 uint32_t idx = 0;
131 const uint32_t num_threads = m_threads.size();
132 for (idx = 0; idx < num_threads; ++idx)
133 {
134 if (m_threads[idx]->GetID() == tid)
135 {
136 thread_sp = m_threads[idx];
137 break;
138 }
139 }
140 return thread_sp;
141}
142
143ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000144ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
145{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000146 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000147
148 if (can_update)
149 m_process->UpdateThreadListIfNeeded();
150
151 ThreadSP thread_sp;
152 uint32_t idx = 0;
153 const uint32_t num_threads = m_threads.size();
154 for (idx = 0; idx < num_threads; ++idx)
155 {
156 if (m_threads[idx]->GetProtocolID() == tid)
157 {
158 thread_sp = m_threads[idx];
159 break;
160 }
161 }
162 return thread_sp;
163}
164
165
166ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000167ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
168{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000169 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000170
171 if (can_update)
172 m_process->UpdateThreadListIfNeeded();
173
174 ThreadSP thread_sp;
175 uint32_t idx = 0;
176 const uint32_t num_threads = m_threads.size();
177 for (idx = 0; idx < num_threads; ++idx)
178 {
179 if (m_threads[idx]->GetID() == tid)
180 {
181 thread_sp = m_threads[idx];
182 m_threads.erase(m_threads.begin()+idx);
183 break;
184 }
185 }
186 return thread_sp;
187}
188
189ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000190ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
191{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000192 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000193
194 if (can_update)
195 m_process->UpdateThreadListIfNeeded();
196
197 ThreadSP thread_sp;
198 uint32_t idx = 0;
199 const uint32_t num_threads = m_threads.size();
200 for (idx = 0; idx < num_threads; ++idx)
201 {
202 if (m_threads[idx]->GetProtocolID() == tid)
203 {
204 thread_sp = m_threads[idx];
205 m_threads.erase(m_threads.begin()+idx);
206 break;
207 }
208 }
209 return thread_sp;
210}
211
212ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
214{
215 ThreadSP thread_sp;
216 if (thread_ptr)
217 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000218 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219
220 uint32_t idx = 0;
221 const uint32_t num_threads = m_threads.size();
222 for (idx = 0; idx < num_threads; ++idx)
223 {
224 if (m_threads[idx].get() == thread_ptr)
225 {
226 thread_sp = m_threads[idx];
227 break;
228 }
229 }
230 }
231 return thread_sp;
232}
233
234
235
236ThreadSP
237ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
238{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000239 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240
241 if (can_update)
242 m_process->UpdateThreadListIfNeeded();
243
244 ThreadSP thread_sp;
245 const uint32_t num_threads = m_threads.size();
246 for (uint32_t idx = 0; idx < num_threads; ++idx)
247 {
248 if (m_threads[idx]->GetIndexID() == index_id)
249 {
250 thread_sp = m_threads[idx];
251 break;
252 }
253 }
254 return thread_sp;
255}
256
257bool
258ThreadList::ShouldStop (Event *event_ptr)
259{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260 // Running events should never stop, obviously...
261
Greg Clayton5160ce52013-03-27 23:08:40 +0000262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
Jim Inghamb42f3af2012-11-15 22:44:04 +0000264 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000265 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000266 // to keep the ThreadList locked the whole time we are doing this.
267 // FIXME: It is possible that running code could cause new threads
268 // to be created. If that happens we will miss asking them whether
269 // then should stop. This is not a big deal, since we haven't had
270 // a chance to hang any interesting operations on those threads yet.
271
272 collection threads_copy;
273 {
274 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000275 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276
Jim Inghamb42f3af2012-11-15 22:44:04 +0000277 m_process->UpdateThreadListIfNeeded();
278 threads_copy = m_threads;
279 }
280
281 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282
Greg Clayton2cad65a2010-09-03 17:10:42 +0000283 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000284 {
285 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000286 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000287 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000288
Jim Inghama0079042013-03-21 21:46:56 +0000289 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000290
291 // 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 +0000292 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000293 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
294 {
295 if (log)
296 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
297
298 should_stop = true;
299 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000300
301 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
302 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
303 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
304 // We don't need to use it here, we just want to make sure it gets computed.
305
306 for (pos = threads_copy.begin(); pos != end; ++pos)
307 {
308 ThreadSP thread_sp(*pos);
309 thread_sp->GetStopInfo();
310 }
Jim Inghama0079042013-03-21 21:46:56 +0000311
Jim Inghamb42f3af2012-11-15 22:44:04 +0000312 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 {
314 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000315
Jim Ingham39fdae72014-01-15 03:32:42 +0000316 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
317 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
318 // the right thing to do and stopping gives the user control over what to do in this instance.
319 //
320 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
321 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
322 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
323 // code and causes us to stop seemingly for no reason.
324 //
325 // 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
326 // to true unless this is the first stop.
327 //
328 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
329 // everywhere but at this check.
330
Ed Maste21afbe02014-01-17 17:31:06 +0000331 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000332 did_anybody_stop_for_a_reason = true;
333 else
334 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000335
Jim Ingham10c4b242011-10-15 00:23:43 +0000336 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000337 if (thread_should_stop)
338 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000340
Jim Inghama0079042013-03-21 21:46:56 +0000341 if (!should_stop && !did_anybody_stop_for_a_reason)
342 {
343 should_stop = true;
344 if (log)
345 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
346 }
347
Greg Clayton2cad65a2010-09-03 17:10:42 +0000348 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000349 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000350
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 if (should_stop)
352 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000353 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 {
355 ThreadSP thread_sp(*pos);
356 thread_sp->WillStop ();
357 }
358 }
359
360 return should_stop;
361}
362
363Vote
364ThreadList::ShouldReportStop (Event *event_ptr)
365{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000366 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000367
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368 Vote result = eVoteNoOpinion;
369 m_process->UpdateThreadListIfNeeded();
370 collection::iterator pos, end = m_threads.end();
371
Greg Clayton5160ce52013-03-27 23:08:40 +0000372 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000373
374 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000375 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000376
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 // Run through the threads and ask whether we should report this event.
378 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
379 for (pos = m_threads.begin(); pos != end; ++pos)
380 {
381 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000382 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
383 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 {
Jim Ingham92087d82012-01-31 23:09:20 +0000385 case eVoteNoOpinion:
386 continue;
387
388 case eVoteYes:
389 result = eVoteYes;
390 break;
391
392 case eVoteNo:
393 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 {
Jim Ingham92087d82012-01-31 23:09:20 +0000395 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 }
Jim Ingham92087d82012-01-31 23:09:20 +0000397 else
398 {
399 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000400 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 +0000401 __FUNCTION__,
402 thread_sp->GetID (),
403 GetVoteAsCString (vote),
404 GetVoteAsCString (result));
405 }
406 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 }
408 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000409 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000410 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 return result;
412}
413
Jim Ingham221d51c2013-05-08 00:35:16 +0000414void
415ThreadList::SetShouldReportStop (Vote vote)
416{
417 Mutex::Locker locker(GetMutex());
418 m_process->UpdateThreadListIfNeeded();
419 collection::iterator pos, end = m_threads.end();
420 for (pos = m_threads.begin(); pos != end; ++pos)
421 {
422 ThreadSP thread_sp(*pos);
423 thread_sp->SetShouldReportStop (vote);
424 }
425}
426
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427Vote
428ThreadList::ShouldReportRun (Event *event_ptr)
429{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000430
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000431 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000432
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 Vote result = eVoteNoOpinion;
434 m_process->UpdateThreadListIfNeeded();
435 collection::iterator pos, end = m_threads.end();
436
437 // Run through the threads and ask whether we should report this event.
438 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
439
Greg Clayton5160ce52013-03-27 23:08:40 +0000440 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000441
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 for (pos = m_threads.begin(); pos != end; ++pos)
443 {
Jim Inghamce579832011-01-24 04:11:25 +0000444 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 {
Jim Inghamce579832011-01-24 04:11:25 +0000446 switch ((*pos)->ShouldReportRun (event_ptr))
447 {
448 case eVoteNoOpinion:
449 continue;
450 case eVoteYes:
451 if (result == eVoteNoOpinion)
452 result = eVoteYes;
453 break;
454 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000455 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000456 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000457 (*pos)->GetIndexID(),
458 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000459 result = eVoteNo;
460 break;
461 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 }
463 }
464 return result;
465}
466
467void
468ThreadList::Clear()
469{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000470 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 m_stop_id = 0;
472 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000473 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
476void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000477ThreadList::Destroy()
478{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000479 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000480 const uint32_t num_threads = m_threads.size();
481 for (uint32_t idx = 0; idx < num_threads; ++idx)
482 {
483 m_threads[idx]->DestroyThread();
484 }
485}
486
487void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488ThreadList::RefreshStateAfterStop ()
489{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000490 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
492 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000493
Greg Clayton5160ce52013-03-27 23:08:40 +0000494 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000495 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000496 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497
498 collection::iterator pos, end = m_threads.end();
499 for (pos = m_threads.begin(); pos != end; ++pos)
500 (*pos)->RefreshStateAfterStop ();
501}
502
503void
504ThreadList::DiscardThreadPlans ()
505{
506 // You don't need to update the thread list here, because only threads
507 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000508 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509
510 collection::iterator pos, end = m_threads.end();
511 for (pos = m_threads.begin(); pos != end; ++pos)
512 (*pos)->DiscardThreadPlans (true);
513
514}
515
516bool
517ThreadList::WillResume ()
518{
519 // Run through the threads and perform their momentary actions.
520 // But we only do this for threads that are running, user suspended
521 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000523 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524 m_process->UpdateThreadListIfNeeded();
525
526 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527
Jim Inghama3241c12010-07-14 02:27:20 +0000528 // See if any thread wants to run stopping others. If it does, then we won't
529 // setup the other threads for resume, since they aren't going to get a chance
530 // to run. This is necessary because the SetupForResume might add "StopOthers"
531 // plans which would then get to be part of the who-gets-to-run negotiation, but
532 // they're coming in after the fact, and the threads that are already set up should
533 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534
Jim Inghama3241c12010-07-14 02:27:20 +0000535 bool wants_solo_run = false;
536
537 for (pos = m_threads.begin(); pos != end; ++pos)
538 {
539 if ((*pos)->GetResumeState() != eStateSuspended &&
540 (*pos)->GetCurrentPlan()->StopOthers())
541 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000542 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
543 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000544 wants_solo_run = true;
545 break;
546 }
547 }
548
Jim Ingham1c823b42011-01-22 01:33:44 +0000549 if (wants_solo_run)
550 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000551 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000552 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000553 log->Printf ("Turning on notification of new threads while single stepping a thread.");
554 m_process->StartNoticingNewThreads();
555 }
556 else
557 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000558 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000559 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000560 log->Printf ("Turning off notification of new threads while single stepping a thread.");
561 m_process->StopNoticingNewThreads();
562 }
Jim Inghama3241c12010-07-14 02:27:20 +0000563
564 // Give all the threads that are likely to run a last chance to set up their state before we
565 // negotiate who is actually going to get a chance to run...
566 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
567 // call setup on the threads that request StopOthers...
568
569 for (pos = m_threads.begin(); pos != end; ++pos)
570 {
571 if ((*pos)->GetResumeState() != eStateSuspended
572 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
573 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000574 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
575 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000576 (*pos)->SetupForResume ();
577 }
578 }
579
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 // Now go through the threads and see if any thread wants to run just itself.
581 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000582
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 ThreadList run_me_only_list (m_process);
584
585 run_me_only_list.SetStopID(m_process->GetStopID());
586
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 bool run_only_current_thread = false;
588
589 for (pos = m_threads.begin(); pos != end; ++pos)
590 {
591 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000592 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593 thread_sp->GetCurrentPlan()->StopOthers())
594 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000595 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
596 continue;
597
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 // You can't say "stop others" and also want yourself to be suspended.
599 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
600
Jim Ingham2976d002010-08-26 21:32:51 +0000601 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 {
603 run_only_current_thread = true;
604 run_me_only_list.Clear();
605 run_me_only_list.AddThread (thread_sp);
606 break;
607 }
608
609 run_me_only_list.AddThread (thread_sp);
610 }
611
612 }
613
Jim Ingham513c6bb2012-09-01 01:02:41 +0000614 bool need_to_resume = true;
615
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000616 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 {
618 // Everybody runs as they wish:
619 for (pos = m_threads.begin(); pos != end; ++pos)
620 {
621 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000622 StateType run_state;
623 if (thread_sp->GetResumeState() != eStateSuspended)
624 run_state = thread_sp->GetCurrentPlan()->RunState();
625 else
626 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000627 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000628 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
630 }
631 else
632 {
633 ThreadSP thread_to_run;
634
635 if (run_only_current_thread)
636 {
Jim Ingham2976d002010-08-26 21:32:51 +0000637 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 }
639 else if (run_me_only_list.GetSize (false) == 1)
640 {
641 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
642 }
643 else
644 {
645 int random_thread = (int)
646 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
647 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
648 }
649
650 for (pos = m_threads.begin(); pos != end; ++pos)
651 {
652 ThreadSP thread_sp(*pos);
653 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000654 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000655 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000656 need_to_resume = false;
657 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000659 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 }
661 }
662
Jim Ingham513c6bb2012-09-01 01:02:41 +0000663 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664}
665
666void
667ThreadList::DidResume ()
668{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000669 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 collection::iterator pos, end = m_threads.end();
671 for (pos = m_threads.begin(); pos != end; ++pos)
672 {
673 // Don't clear out threads that aren't going to get a chance to run, rather
674 // leave their state for the next time around.
675 ThreadSP thread_sp(*pos);
676 if (thread_sp->GetResumeState() != eStateSuspended)
677 thread_sp->DidResume ();
678 }
679}
680
Andrew Kaylor29d65742013-05-10 17:19:04 +0000681void
682ThreadList::DidStop ()
683{
684 Mutex::Locker locker(GetMutex());
685 collection::iterator pos, end = m_threads.end();
686 for (pos = m_threads.begin(); pos != end; ++pos)
687 {
688 // Notify threads that the process just stopped.
689 // Note, this currently assumes that all threads in the list
690 // stop when the process stops. In the future we will want to support
691 // a debugging model where some threads continue to run while others
692 // are stopped. We either need to handle that somehow here or
693 // create a special thread list containing only threads which will
694 // stop in the code that calls this method (currently
695 // Process::SetPrivateState).
696 ThreadSP thread_sp(*pos);
697 if (StateIsRunningState(thread_sp->GetState()))
698 thread_sp->DidStop ();
699 }
700}
701
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000703ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000705 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000706 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
707 if (!thread_sp.get())
708 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000709 if (m_threads.size() == 0)
710 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000711 m_selected_tid = m_threads[0]->GetID();
712 thread_sp = m_threads[0];
713 }
714 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715}
716
717bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000718ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000720 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000721 ThreadSP selected_thread_sp(FindThreadByID(tid));
722 if (selected_thread_sp)
723 {
Jim Ingham2976d002010-08-26 21:32:51 +0000724 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000725 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
726 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 else
Jim Ingham2976d002010-08-26 21:32:51 +0000728 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729
Jim Inghamc3faa192012-12-11 02:31:48 +0000730 if (notify)
731 NotifySelectedThreadChanged(m_selected_tid);
732
Jim Ingham2976d002010-08-26 21:32:51 +0000733 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734}
735
736bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000737ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000739 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000740 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
741 if (selected_thread_sp.get())
742 {
743 m_selected_tid = selected_thread_sp->GetID();
744 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
745 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 else
Jim Ingham2976d002010-08-26 21:32:51 +0000747 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748
Jim Inghamc3faa192012-12-11 02:31:48 +0000749 if (notify)
750 NotifySelectedThreadChanged(m_selected_tid);
751
Jim Ingham2976d002010-08-26 21:32:51 +0000752 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753}
754
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000755void
Jim Inghamc3faa192012-12-11 02:31:48 +0000756ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
757{
758 ThreadSP selected_thread_sp (FindThreadByID(tid));
759 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
760 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
761 new Thread::ThreadEventData(selected_thread_sp));
762}
763
764void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000765ThreadList::Update (ThreadList &rhs)
766{
767 if (this != &rhs)
768 {
769 // Lock both mutexes to make sure neither side changes anyone on us
770 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000771 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000772 m_process = rhs.m_process;
773 m_stop_id = rhs.m_stop_id;
774 m_threads.swap(rhs.m_threads);
775 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000776
777
778 // Now we look for threads that we are done with and
779 // make sure to clear them up as much as possible so
780 // anyone with a shared pointer will still have a reference,
781 // but the thread won't be of much use. Using std::weak_ptr
782 // for all backward references (such as a thread to a process)
783 // will eventually solve this issue for us, but for now, we
784 // need to work around the issue
785 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
786 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
787 {
788 const lldb::tid_t tid = (*rhs_pos)->GetID();
789 bool thread_is_alive = false;
790 const uint32_t num_threads = m_threads.size();
791 for (uint32_t idx = 0; idx < num_threads; ++idx)
792 {
793 if (m_threads[idx]->GetID() == tid)
794 {
795 thread_is_alive = true;
796 break;
797 }
798 }
799 if (!thread_is_alive)
800 (*rhs_pos)->DestroyThread();
801 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000802 }
803}
804
Greg Claytonfa559e52012-05-18 02:38:05 +0000805void
806ThreadList::Flush ()
807{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000808 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000809 collection::iterator pos, end = m_threads.end();
810 for (pos = m_threads.begin(); pos != end; ++pos)
811 (*pos)->Flush ();
812}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000813
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000814Mutex &
815ThreadList::GetMutex ()
816{
817 return m_process->m_thread_mutex;
818}
819