blob: 8c416943db0f2a6d8bda6efc33ed4c9a43a65203 [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"
Zachary Turner50232572015-03-18 21:31:45 +000020#include "lldb/Utility/ConvertEnum.h"
Zachary Turner8f186f82015-11-13 21:53:03 +000021#include "lldb/Utility/LLDBAssert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26ThreadList::ThreadList (Process *process) :
Kuba Breckae4d48012014-09-05 19:13:15 +000027 ThreadCollection(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028 m_process (process),
29 m_stop_id (0),
Jim Ingham2976d002010-08-26 21:32:51 +000030 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031{
32}
33
34ThreadList::ThreadList (const ThreadList &rhs) :
Kuba Breckae4d48012014-09-05 19:13:15 +000035 ThreadCollection(),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000036 m_process (rhs.m_process),
37 m_stop_id (rhs.m_stop_id),
Jim Ingham2976d002010-08-26 21:32:51 +000038 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039{
40 // Use the assignment operator since it uses the mutex
41 *this = rhs;
42}
43
44const ThreadList&
45ThreadList::operator = (const ThreadList& rhs)
46{
47 if (this != &rhs)
48 {
49 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +000050 // while the assignment occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000051 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082uint32_t
83ThreadList::GetSize (bool can_update)
84{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000085 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 if (can_update)
87 m_process->UpdateThreadListIfNeeded();
88 return m_threads.size();
89}
90
91ThreadSP
92ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
93{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000094 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 if (can_update)
96 m_process->UpdateThreadListIfNeeded();
97
98 ThreadSP thread_sp;
99 if (idx < m_threads.size())
100 thread_sp = m_threads[idx];
101 return thread_sp;
102}
103
104ThreadSP
105ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
106{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000107 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
109 if (can_update)
110 m_process->UpdateThreadListIfNeeded();
111
112 ThreadSP thread_sp;
113 uint32_t idx = 0;
114 const uint32_t num_threads = m_threads.size();
115 for (idx = 0; idx < num_threads; ++idx)
116 {
117 if (m_threads[idx]->GetID() == tid)
118 {
119 thread_sp = m_threads[idx];
120 break;
121 }
122 }
123 return thread_sp;
124}
125
126ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000127ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
128{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000129 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000130
131 if (can_update)
132 m_process->UpdateThreadListIfNeeded();
133
134 ThreadSP thread_sp;
135 uint32_t idx = 0;
136 const uint32_t num_threads = m_threads.size();
137 for (idx = 0; idx < num_threads; ++idx)
138 {
139 if (m_threads[idx]->GetProtocolID() == tid)
140 {
141 thread_sp = m_threads[idx];
142 break;
143 }
144 }
145 return thread_sp;
146}
147
148
149ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000150ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
151{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000152 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000153
154 if (can_update)
155 m_process->UpdateThreadListIfNeeded();
156
157 ThreadSP thread_sp;
158 uint32_t idx = 0;
159 const uint32_t num_threads = m_threads.size();
160 for (idx = 0; idx < num_threads; ++idx)
161 {
162 if (m_threads[idx]->GetID() == tid)
163 {
164 thread_sp = m_threads[idx];
165 m_threads.erase(m_threads.begin()+idx);
166 break;
167 }
168 }
169 return thread_sp;
170}
171
172ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000173ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
174{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000175 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000176
177 if (can_update)
178 m_process->UpdateThreadListIfNeeded();
179
180 ThreadSP thread_sp;
181 uint32_t idx = 0;
182 const uint32_t num_threads = m_threads.size();
183 for (idx = 0; idx < num_threads; ++idx)
184 {
185 if (m_threads[idx]->GetProtocolID() == tid)
186 {
187 thread_sp = m_threads[idx];
188 m_threads.erase(m_threads.begin()+idx);
189 break;
190 }
191 }
192 return thread_sp;
193}
194
195ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
197{
198 ThreadSP thread_sp;
199 if (thread_ptr)
200 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000201 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202
203 uint32_t idx = 0;
204 const uint32_t num_threads = m_threads.size();
205 for (idx = 0; idx < num_threads; ++idx)
206 {
207 if (m_threads[idx].get() == thread_ptr)
208 {
209 thread_sp = m_threads[idx];
210 break;
211 }
212 }
213 }
214 return thread_sp;
215}
216
217
218
219ThreadSP
220ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
221{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000222 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223
224 if (can_update)
225 m_process->UpdateThreadListIfNeeded();
226
227 ThreadSP thread_sp;
228 const uint32_t num_threads = m_threads.size();
229 for (uint32_t idx = 0; idx < num_threads; ++idx)
230 {
231 if (m_threads[idx]->GetIndexID() == index_id)
232 {
233 thread_sp = m_threads[idx];
234 break;
235 }
236 }
237 return thread_sp;
238}
239
240bool
241ThreadList::ShouldStop (Event *event_ptr)
242{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 // Running events should never stop, obviously...
244
Greg Clayton5160ce52013-03-27 23:08:40 +0000245 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246
Jim Inghamb42f3af2012-11-15 22:44:04 +0000247 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000248 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000249 // to keep the ThreadList locked the whole time we are doing this.
250 // FIXME: It is possible that running code could cause new threads
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000251 // to be created. If that happens, we will miss asking them whether
252 // they should stop. This is not a big deal since we haven't had
Jim Inghamb42f3af2012-11-15 22:44:04 +0000253 // a chance to hang any interesting operations on those threads yet.
254
255 collection threads_copy;
256 {
257 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000258 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259
Jim Inghamb42f3af2012-11-15 22:44:04 +0000260 m_process->UpdateThreadListIfNeeded();
Jim Ingham569aaf92015-11-06 22:45:57 +0000261 for (lldb::ThreadSP thread_sp : m_threads)
262 {
263 // This is an optimization... If we didn't let a thread run in between the previous stop and this
264 // one, we shouldn't have to consult it for ShouldStop. So just leave it off the list we are going to
265 // inspect.
Chaoren Lin6e8fbc62015-11-07 02:16:31 +0000266 // On Linux, if a thread-specific conditional breakpoint was hit, it won't necessarily be the thread
267 // that hit the breakpoint itself that evaluates the conditional expression, so the thread that hit
268 // the breakpoint could still be asked to stop, even though it hasn't been allowed to run since the
269 // previous stop.
270 if (thread_sp->GetTemporaryResumeState () != eStateSuspended || thread_sp->IsStillAtLastBreakpointHit())
Jim Ingham569aaf92015-11-06 22:45:57 +0000271 threads_copy.push_back(thread_sp);
272 }
273
274 // It is possible the threads we were allowing to run all exited and then maybe the user interrupted
275 // or something, then fall back on looking at all threads:
276
277 if (threads_copy.size() == 0)
278 threads_copy = m_threads;
Jim Inghamb42f3af2012-11-15 22:44:04 +0000279 }
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("");
Jim Ingham569aaf92015-11-06 22:45:57 +0000286 log->Printf ("ThreadList::%s: %" PRIu64 " threads, %" PRIu64 " unsuspended threads",
287 __FUNCTION__,
288 (uint64_t)m_threads.size(),
289 (uint64_t)threads_copy.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000290 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000291
Jim Inghama0079042013-03-21 21:46:56 +0000292 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000293
294 // 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 +0000295 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000296 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
297 {
298 if (log)
299 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
300
301 should_stop = true;
302 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000303
304 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
305 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
306 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
307 // We don't need to use it here, we just want to make sure it gets computed.
308
309 for (pos = threads_copy.begin(); pos != end; ++pos)
310 {
311 ThreadSP thread_sp(*pos);
312 thread_sp->GetStopInfo();
313 }
Jim Inghama0079042013-03-21 21:46:56 +0000314
Jim Inghamb42f3af2012-11-15 22:44:04 +0000315 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 {
317 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000318
Jim Ingham39fdae72014-01-15 03:32:42 +0000319 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
320 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
321 // the right thing to do and stopping gives the user control over what to do in this instance.
322 //
323 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
324 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
325 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
326 // code and causes us to stop seemingly for no reason.
327 //
328 // 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
329 // to true unless this is the first stop.
330 //
331 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
332 // everywhere but at this check.
333
Ed Maste21afbe02014-01-17 17:31:06 +0000334 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000335 did_anybody_stop_for_a_reason = true;
336 else
337 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000338
Jim Ingham10c4b242011-10-15 00:23:43 +0000339 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000340 if (thread_should_stop)
341 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000343
Jim Inghama0079042013-03-21 21:46:56 +0000344 if (!should_stop && !did_anybody_stop_for_a_reason)
345 {
346 should_stop = true;
347 if (log)
348 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
349 }
350
Greg Clayton2cad65a2010-09-03 17:10:42 +0000351 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000352 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000353
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 if (should_stop)
355 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000356 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 {
358 ThreadSP thread_sp(*pos);
359 thread_sp->WillStop ();
360 }
361 }
362
363 return should_stop;
364}
365
366Vote
367ThreadList::ShouldReportStop (Event *event_ptr)
368{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000369 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 Vote result = eVoteNoOpinion;
372 m_process->UpdateThreadListIfNeeded();
373 collection::iterator pos, end = m_threads.end();
374
Greg Clayton5160ce52013-03-27 23:08:40 +0000375 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000376
377 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000378 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000379
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 // Run through the threads and ask whether we should report this event.
381 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
382 for (pos = m_threads.begin(); pos != end; ++pos)
383 {
384 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000385 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
386 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 {
Jim Ingham92087d82012-01-31 23:09:20 +0000388 case eVoteNoOpinion:
389 continue;
390
391 case eVoteYes:
392 result = eVoteYes;
393 break;
394
395 case eVoteNo:
396 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 {
Jim Ingham92087d82012-01-31 23:09:20 +0000398 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 }
Jim Ingham92087d82012-01-31 23:09:20 +0000400 else
401 {
402 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000403 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 +0000404 __FUNCTION__,
405 thread_sp->GetID (),
406 GetVoteAsCString (vote),
407 GetVoteAsCString (result));
408 }
409 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 }
411 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000412 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000413 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 return result;
415}
416
Jim Ingham221d51c2013-05-08 00:35:16 +0000417void
418ThreadList::SetShouldReportStop (Vote vote)
419{
420 Mutex::Locker locker(GetMutex());
421 m_process->UpdateThreadListIfNeeded();
422 collection::iterator pos, end = m_threads.end();
423 for (pos = m_threads.begin(); pos != end; ++pos)
424 {
425 ThreadSP thread_sp(*pos);
426 thread_sp->SetShouldReportStop (vote);
427 }
428}
429
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430Vote
431ThreadList::ShouldReportRun (Event *event_ptr)
432{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000433
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000434 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000435
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 Vote result = eVoteNoOpinion;
437 m_process->UpdateThreadListIfNeeded();
438 collection::iterator pos, end = m_threads.end();
439
440 // Run through the threads and ask whether we should report this event.
441 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
442
Greg Clayton5160ce52013-03-27 23:08:40 +0000443 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000444
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 for (pos = m_threads.begin(); pos != end; ++pos)
446 {
Jim Inghamce579832011-01-24 04:11:25 +0000447 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 {
Jim Inghamce579832011-01-24 04:11:25 +0000449 switch ((*pos)->ShouldReportRun (event_ptr))
450 {
451 case eVoteNoOpinion:
452 continue;
453 case eVoteYes:
454 if (result == eVoteNoOpinion)
455 result = eVoteYes;
456 break;
457 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000458 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000459 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000460 (*pos)->GetIndexID(),
461 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000462 result = eVoteNo;
463 break;
464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 }
466 }
467 return result;
468}
469
470void
471ThreadList::Clear()
472{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000473 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 m_stop_id = 0;
475 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000476 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
479void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000480ThreadList::Destroy()
481{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000482 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000483 const uint32_t num_threads = m_threads.size();
484 for (uint32_t idx = 0; idx < num_threads; ++idx)
485 {
486 m_threads[idx]->DestroyThread();
487 }
488}
489
490void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491ThreadList::RefreshStateAfterStop ()
492{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000493 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494
495 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000496
Greg Clayton5160ce52013-03-27 23:08:40 +0000497 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000498 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000499 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
501 collection::iterator pos, end = m_threads.end();
502 for (pos = m_threads.begin(); pos != end; ++pos)
503 (*pos)->RefreshStateAfterStop ();
504}
505
506void
507ThreadList::DiscardThreadPlans ()
508{
509 // You don't need to update the thread list here, because only threads
510 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000511 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
513 collection::iterator pos, end = m_threads.end();
514 for (pos = m_threads.begin(); pos != end; ++pos)
515 (*pos)->DiscardThreadPlans (true);
516
517}
518
519bool
520ThreadList::WillResume ()
521{
522 // Run through the threads and perform their momentary actions.
523 // But we only do this for threads that are running, user suspended
524 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000526 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 m_process->UpdateThreadListIfNeeded();
528
529 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530
Jim Inghama3241c12010-07-14 02:27:20 +0000531 // See if any thread wants to run stopping others. If it does, then we won't
532 // setup the other threads for resume, since they aren't going to get a chance
533 // to run. This is necessary because the SetupForResume might add "StopOthers"
534 // plans which would then get to be part of the who-gets-to-run negotiation, but
535 // they're coming in after the fact, and the threads that are already set up should
536 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537
Jim Inghama3241c12010-07-14 02:27:20 +0000538 bool wants_solo_run = false;
539
540 for (pos = m_threads.begin(); pos != end; ++pos)
541 {
Zachary Turner8f186f82015-11-13 21:53:03 +0000542 lldbassert((*pos)->GetCurrentPlan() && "thread should not have null thread plan");
543 if ((*pos)->GetResumeState() != eStateSuspended &&
Jim Inghama3241c12010-07-14 02:27:20 +0000544 (*pos)->GetCurrentPlan()->StopOthers())
545 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000546 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
547 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000548 wants_solo_run = true;
549 break;
550 }
551 }
552
Jim Ingham1c823b42011-01-22 01:33:44 +0000553 if (wants_solo_run)
554 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000555 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000556 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000557 log->Printf ("Turning on notification of new threads while single stepping a thread.");
558 m_process->StartNoticingNewThreads();
559 }
560 else
561 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000563 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000564 log->Printf ("Turning off notification of new threads while single stepping a thread.");
565 m_process->StopNoticingNewThreads();
566 }
Jim Inghama3241c12010-07-14 02:27:20 +0000567
568 // Give all the threads that are likely to run a last chance to set up their state before we
569 // negotiate who is actually going to get a chance to run...
570 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
571 // call setup on the threads that request StopOthers...
572
573 for (pos = m_threads.begin(); pos != end; ++pos)
574 {
575 if ((*pos)->GetResumeState() != eStateSuspended
576 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
577 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000578 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
579 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000580 (*pos)->SetupForResume ();
581 }
582 }
583
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 // Now go through the threads and see if any thread wants to run just itself.
585 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000586
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 ThreadList run_me_only_list (m_process);
588
589 run_me_only_list.SetStopID(m_process->GetStopID());
590
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 bool run_only_current_thread = false;
592
593 for (pos = m_threads.begin(); pos != end; ++pos)
594 {
595 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000596 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 thread_sp->GetCurrentPlan()->StopOthers())
598 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000599 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
600 continue;
601
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 // You can't say "stop others" and also want yourself to be suspended.
603 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
604
Jim Ingham2976d002010-08-26 21:32:51 +0000605 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 {
Jim Inghamacbea8f2015-06-02 20:26:13 +0000607 // If the currently selected thread wants to run on its own, always let it.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 run_only_current_thread = true;
609 run_me_only_list.Clear();
610 run_me_only_list.AddThread (thread_sp);
611 break;
612 }
613
614 run_me_only_list.AddThread (thread_sp);
615 }
616
617 }
618
Jim Ingham513c6bb2012-09-01 01:02:41 +0000619 bool need_to_resume = true;
620
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000621 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 {
623 // Everybody runs as they wish:
624 for (pos = m_threads.begin(); pos != end; ++pos)
625 {
626 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000627 StateType run_state;
628 if (thread_sp->GetResumeState() != eStateSuspended)
629 run_state = thread_sp->GetCurrentPlan()->RunState();
630 else
631 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000632 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000633 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 }
635 }
636 else
637 {
638 ThreadSP thread_to_run;
639
640 if (run_only_current_thread)
641 {
Jim Ingham2976d002010-08-26 21:32:51 +0000642 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 }
644 else if (run_me_only_list.GetSize (false) == 1)
645 {
646 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
647 }
648 else
649 {
650 int random_thread = (int)
651 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
652 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
653 }
654
655 for (pos = m_threads.begin(); pos != end; ++pos)
656 {
657 ThreadSP thread_sp(*pos);
658 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000659 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000660 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000661 need_to_resume = false;
662 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000664 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 }
666 }
667
Jim Ingham513c6bb2012-09-01 01:02:41 +0000668 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669}
670
671void
672ThreadList::DidResume ()
673{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000674 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 collection::iterator pos, end = m_threads.end();
676 for (pos = m_threads.begin(); pos != end; ++pos)
677 {
678 // Don't clear out threads that aren't going to get a chance to run, rather
679 // leave their state for the next time around.
680 ThreadSP thread_sp(*pos);
681 if (thread_sp->GetResumeState() != eStateSuspended)
682 thread_sp->DidResume ();
683 }
684}
685
Andrew Kaylor29d65742013-05-10 17:19:04 +0000686void
687ThreadList::DidStop ()
688{
689 Mutex::Locker locker(GetMutex());
690 collection::iterator pos, end = m_threads.end();
691 for (pos = m_threads.begin(); pos != end; ++pos)
692 {
693 // Notify threads that the process just stopped.
694 // Note, this currently assumes that all threads in the list
695 // stop when the process stops. In the future we will want to support
696 // a debugging model where some threads continue to run while others
697 // are stopped. We either need to handle that somehow here or
698 // create a special thread list containing only threads which will
699 // stop in the code that calls this method (currently
700 // Process::SetPrivateState).
701 ThreadSP thread_sp(*pos);
702 if (StateIsRunningState(thread_sp->GetState()))
703 thread_sp->DidStop ();
704 }
705}
706
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000708ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000710 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000711 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
712 if (!thread_sp.get())
713 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000714 if (m_threads.size() == 0)
715 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000716 m_selected_tid = m_threads[0]->GetID();
717 thread_sp = m_threads[0];
718 }
719 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720}
721
722bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000723ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000725 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000726 ThreadSP selected_thread_sp(FindThreadByID(tid));
727 if (selected_thread_sp)
728 {
Jim Ingham2976d002010-08-26 21:32:51 +0000729 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000730 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
731 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 else
Jim Ingham2976d002010-08-26 21:32:51 +0000733 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734
Jim Inghamc3faa192012-12-11 02:31:48 +0000735 if (notify)
736 NotifySelectedThreadChanged(m_selected_tid);
737
Jim Ingham2976d002010-08-26 21:32:51 +0000738 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739}
740
741bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000742ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000744 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000745 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
746 if (selected_thread_sp.get())
747 {
748 m_selected_tid = selected_thread_sp->GetID();
749 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
750 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 else
Jim Ingham2976d002010-08-26 21:32:51 +0000752 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753
Jim Inghamc3faa192012-12-11 02:31:48 +0000754 if (notify)
755 NotifySelectedThreadChanged(m_selected_tid);
756
Jim Ingham2976d002010-08-26 21:32:51 +0000757 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000760void
Jim Inghamc3faa192012-12-11 02:31:48 +0000761ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
762{
763 ThreadSP selected_thread_sp (FindThreadByID(tid));
764 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
765 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
766 new Thread::ThreadEventData(selected_thread_sp));
767}
768
769void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000770ThreadList::Update (ThreadList &rhs)
771{
772 if (this != &rhs)
773 {
774 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +0000775 // while the assignment occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000776 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000777 m_process = rhs.m_process;
778 m_stop_id = rhs.m_stop_id;
779 m_threads.swap(rhs.m_threads);
780 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000781
782
783 // Now we look for threads that we are done with and
784 // make sure to clear them up as much as possible so
785 // anyone with a shared pointer will still have a reference,
786 // but the thread won't be of much use. Using std::weak_ptr
787 // for all backward references (such as a thread to a process)
788 // will eventually solve this issue for us, but for now, we
789 // need to work around the issue
790 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
791 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
792 {
793 const lldb::tid_t tid = (*rhs_pos)->GetID();
794 bool thread_is_alive = false;
795 const uint32_t num_threads = m_threads.size();
796 for (uint32_t idx = 0; idx < num_threads; ++idx)
797 {
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000798 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
799 if (m_threads[idx]->GetID() == tid || (backing_thread && backing_thread->GetID() == tid))
Greg Claytone1cd1be2012-01-29 20:56:30 +0000800 {
801 thread_is_alive = true;
802 break;
803 }
804 }
805 if (!thread_is_alive)
806 (*rhs_pos)->DestroyThread();
807 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000808 }
809}
810
Greg Claytonfa559e52012-05-18 02:38:05 +0000811void
812ThreadList::Flush ()
813{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000814 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000815 collection::iterator pos, end = m_threads.end();
816 for (pos = m_threads.begin(); pos != end; ++pos)
817 (*pos)->Flush ();
818}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000819
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000820Mutex &
821ThreadList::GetMutex ()
822{
823 return m_process->m_thread_mutex;
824}
825