blob: a34cb0fa143adddd388d83a1df6fb234c798a1b9 [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//===----------------------------------------------------------------------===//
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00009
10// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include <stdlib.h>
12
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000013// C++ Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include <algorithm>
15
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000016// Other libraries and framework includes
17// Project includes
Greg Clayton2cad65a2010-09-03 17:10:42 +000018#include "lldb/Core/Log.h"
Andrew Kaylor29d65742013-05-10 17:19:04 +000019#include "lldb/Core/State.h"
Greg Clayton2cad65a2010-09-03 17:10:42 +000020#include "lldb/Target/RegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/ThreadList.h"
22#include "lldb/Target/Thread.h"
23#include "lldb/Target/ThreadPlan.h"
24#include "lldb/Target/Process.h"
Zachary Turner50232572015-03-18 21:31:45 +000025#include "lldb/Utility/ConvertEnum.h"
Zachary Turner8f186f82015-11-13 21:53:03 +000026#include "lldb/Utility/LLDBAssert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31ThreadList::ThreadList (Process *process) :
Kuba Breckae4d48012014-09-05 19:13:15 +000032 ThreadCollection(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 m_process (process),
34 m_stop_id (0),
Jim Ingham2976d002010-08-26 21:32:51 +000035 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036{
37}
38
39ThreadList::ThreadList (const ThreadList &rhs) :
Kuba Breckae4d48012014-09-05 19:13:15 +000040 ThreadCollection(),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000041 m_process (rhs.m_process),
42 m_stop_id (rhs.m_stop_id),
Jim Ingham2976d002010-08-26 21:32:51 +000043 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044{
45 // Use the assignment operator since it uses the mutex
46 *this = rhs;
47}
48
49const ThreadList&
50ThreadList::operator = (const ThreadList& rhs)
51{
52 if (this != &rhs)
53 {
54 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +000055 // while the assignment occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000056 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057 m_process = rhs.m_process;
58 m_stop_id = rhs.m_stop_id;
59 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000060 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 }
62 return *this;
63}
64
65
66ThreadList::~ThreadList()
67{
Greg Claytonac358da2013-03-28 18:33:53 +000068 // Clear the thread list. Clear will take the mutex lock
69 // which will ensure that if anyone is using the list
70 // they won't get it removed while using it.
71 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
74
75uint32_t
76ThreadList::GetStopID () const
77{
78 return m_stop_id;
79}
80
81void
82ThreadList::SetStopID (uint32_t stop_id)
83{
84 m_stop_id = stop_id;
85}
86
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087uint32_t
88ThreadList::GetSize (bool can_update)
89{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000090 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 if (can_update)
92 m_process->UpdateThreadListIfNeeded();
93 return m_threads.size();
94}
95
96ThreadSP
97ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
98{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000099 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 if (can_update)
101 m_process->UpdateThreadListIfNeeded();
102
103 ThreadSP thread_sp;
104 if (idx < m_threads.size())
105 thread_sp = m_threads[idx];
106 return thread_sp;
107}
108
109ThreadSP
110ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
111{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000112 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
114 if (can_update)
115 m_process->UpdateThreadListIfNeeded();
116
117 ThreadSP thread_sp;
118 uint32_t idx = 0;
119 const uint32_t num_threads = m_threads.size();
120 for (idx = 0; idx < num_threads; ++idx)
121 {
122 if (m_threads[idx]->GetID() == tid)
123 {
124 thread_sp = m_threads[idx];
125 break;
126 }
127 }
128 return thread_sp;
129}
130
131ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000132ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
133{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000134 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000135
136 if (can_update)
137 m_process->UpdateThreadListIfNeeded();
138
139 ThreadSP thread_sp;
140 uint32_t idx = 0;
141 const uint32_t num_threads = m_threads.size();
142 for (idx = 0; idx < num_threads; ++idx)
143 {
144 if (m_threads[idx]->GetProtocolID() == tid)
145 {
146 thread_sp = m_threads[idx];
147 break;
148 }
149 }
150 return thread_sp;
151}
152
153
154ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000155ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
156{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000157 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000158
159 if (can_update)
160 m_process->UpdateThreadListIfNeeded();
161
162 ThreadSP thread_sp;
163 uint32_t idx = 0;
164 const uint32_t num_threads = m_threads.size();
165 for (idx = 0; idx < num_threads; ++idx)
166 {
167 if (m_threads[idx]->GetID() == tid)
168 {
169 thread_sp = m_threads[idx];
170 m_threads.erase(m_threads.begin()+idx);
171 break;
172 }
173 }
174 return thread_sp;
175}
176
177ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000178ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
179{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000180 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000181
182 if (can_update)
183 m_process->UpdateThreadListIfNeeded();
184
185 ThreadSP thread_sp;
186 uint32_t idx = 0;
187 const uint32_t num_threads = m_threads.size();
188 for (idx = 0; idx < num_threads; ++idx)
189 {
190 if (m_threads[idx]->GetProtocolID() == tid)
191 {
192 thread_sp = m_threads[idx];
193 m_threads.erase(m_threads.begin()+idx);
194 break;
195 }
196 }
197 return thread_sp;
198}
199
200ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
202{
203 ThreadSP thread_sp;
204 if (thread_ptr)
205 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000206 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207
208 uint32_t idx = 0;
209 const uint32_t num_threads = m_threads.size();
210 for (idx = 0; idx < num_threads; ++idx)
211 {
212 if (m_threads[idx].get() == thread_ptr)
213 {
214 thread_sp = m_threads[idx];
215 break;
216 }
217 }
218 }
219 return thread_sp;
220}
221
222
223
224ThreadSP
225ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
226{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000227 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228
229 if (can_update)
230 m_process->UpdateThreadListIfNeeded();
231
232 ThreadSP thread_sp;
233 const uint32_t num_threads = m_threads.size();
234 for (uint32_t idx = 0; idx < num_threads; ++idx)
235 {
236 if (m_threads[idx]->GetIndexID() == index_id)
237 {
238 thread_sp = m_threads[idx];
239 break;
240 }
241 }
242 return thread_sp;
243}
244
245bool
246ThreadList::ShouldStop (Event *event_ptr)
247{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 // Running events should never stop, obviously...
249
Greg Clayton5160ce52013-03-27 23:08:40 +0000250 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251
Jim Inghamb42f3af2012-11-15 22:44:04 +0000252 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000253 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000254 // to keep the ThreadList locked the whole time we are doing this.
255 // FIXME: It is possible that running code could cause new threads
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000256 // to be created. If that happens, we will miss asking them whether
257 // they should stop. This is not a big deal since we haven't had
Jim Inghamb42f3af2012-11-15 22:44:04 +0000258 // a chance to hang any interesting operations on those threads yet.
259
260 collection threads_copy;
261 {
262 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000263 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
Jim Inghamb42f3af2012-11-15 22:44:04 +0000265 m_process->UpdateThreadListIfNeeded();
Jim Ingham569aaf92015-11-06 22:45:57 +0000266 for (lldb::ThreadSP thread_sp : m_threads)
267 {
268 // This is an optimization... If we didn't let a thread run in between the previous stop and this
269 // one, we shouldn't have to consult it for ShouldStop. So just leave it off the list we are going to
270 // inspect.
Chaoren Lin6e8fbc62015-11-07 02:16:31 +0000271 // On Linux, if a thread-specific conditional breakpoint was hit, it won't necessarily be the thread
272 // that hit the breakpoint itself that evaluates the conditional expression, so the thread that hit
273 // the breakpoint could still be asked to stop, even though it hasn't been allowed to run since the
274 // previous stop.
275 if (thread_sp->GetTemporaryResumeState () != eStateSuspended || thread_sp->IsStillAtLastBreakpointHit())
Jim Ingham569aaf92015-11-06 22:45:57 +0000276 threads_copy.push_back(thread_sp);
277 }
278
279 // It is possible the threads we were allowing to run all exited and then maybe the user interrupted
280 // or something, then fall back on looking at all threads:
281
282 if (threads_copy.size() == 0)
283 threads_copy = m_threads;
Jim Inghamb42f3af2012-11-15 22:44:04 +0000284 }
285
286 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287
Greg Clayton2cad65a2010-09-03 17:10:42 +0000288 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000289 {
290 log->PutCString("");
Jim Ingham569aaf92015-11-06 22:45:57 +0000291 log->Printf ("ThreadList::%s: %" PRIu64 " threads, %" PRIu64 " unsuspended threads",
292 __FUNCTION__,
293 (uint64_t)m_threads.size(),
294 (uint64_t)threads_copy.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000295 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000296
Jim Inghama0079042013-03-21 21:46:56 +0000297 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000298
299 // 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 +0000300 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000301 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
302 {
303 if (log)
304 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
305
306 should_stop = true;
307 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000308
309 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
310 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
311 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
312 // We don't need to use it here, we just want to make sure it gets computed.
313
314 for (pos = threads_copy.begin(); pos != end; ++pos)
315 {
316 ThreadSP thread_sp(*pos);
317 thread_sp->GetStopInfo();
318 }
Jim Inghama0079042013-03-21 21:46:56 +0000319
Jim Inghamb42f3af2012-11-15 22:44:04 +0000320 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 {
322 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000323
Jim Ingham39fdae72014-01-15 03:32:42 +0000324 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
325 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
326 // the right thing to do and stopping gives the user control over what to do in this instance.
327 //
328 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
329 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
330 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
331 // code and causes us to stop seemingly for no reason.
332 //
333 // 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
334 // to true unless this is the first stop.
335 //
336 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
337 // everywhere but at this check.
338
Ed Maste21afbe02014-01-17 17:31:06 +0000339 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000340 did_anybody_stop_for_a_reason = true;
341 else
342 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000343
Jim Ingham10c4b242011-10-15 00:23:43 +0000344 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000345 if (thread_should_stop)
346 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000348
Jim Inghama0079042013-03-21 21:46:56 +0000349 if (!should_stop && !did_anybody_stop_for_a_reason)
350 {
351 should_stop = true;
352 if (log)
353 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
354 }
355
Greg Clayton2cad65a2010-09-03 17:10:42 +0000356 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000357 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000358
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 if (should_stop)
360 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000361 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 {
363 ThreadSP thread_sp(*pos);
364 thread_sp->WillStop ();
365 }
366 }
367
368 return should_stop;
369}
370
371Vote
372ThreadList::ShouldReportStop (Event *event_ptr)
373{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000374 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000375
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 Vote result = eVoteNoOpinion;
377 m_process->UpdateThreadListIfNeeded();
378 collection::iterator pos, end = m_threads.end();
379
Greg Clayton5160ce52013-03-27 23:08:40 +0000380 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000381
382 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000383 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 // Run through the threads and ask whether we should report this event.
386 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
387 for (pos = m_threads.begin(); pos != end; ++pos)
388 {
389 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000390 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
391 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 {
Jim Ingham92087d82012-01-31 23:09:20 +0000393 case eVoteNoOpinion:
394 continue;
395
396 case eVoteYes:
397 result = eVoteYes;
398 break;
399
400 case eVoteNo:
401 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
Jim Ingham92087d82012-01-31 23:09:20 +0000403 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404 }
Jim Ingham92087d82012-01-31 23:09:20 +0000405 else
406 {
407 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000408 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 +0000409 __FUNCTION__,
410 thread_sp->GetID (),
411 GetVoteAsCString (vote),
412 GetVoteAsCString (result));
413 }
414 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 }
416 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000417 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000418 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 return result;
420}
421
Jim Ingham221d51c2013-05-08 00:35:16 +0000422void
423ThreadList::SetShouldReportStop (Vote vote)
424{
425 Mutex::Locker locker(GetMutex());
426 m_process->UpdateThreadListIfNeeded();
427 collection::iterator pos, end = m_threads.end();
428 for (pos = m_threads.begin(); pos != end; ++pos)
429 {
430 ThreadSP thread_sp(*pos);
431 thread_sp->SetShouldReportStop (vote);
432 }
433}
434
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435Vote
436ThreadList::ShouldReportRun (Event *event_ptr)
437{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000438
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000439 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000440
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 Vote result = eVoteNoOpinion;
442 m_process->UpdateThreadListIfNeeded();
443 collection::iterator pos, end = m_threads.end();
444
445 // Run through the threads and ask whether we should report this event.
446 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
447
Greg Clayton5160ce52013-03-27 23:08:40 +0000448 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000449
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 for (pos = m_threads.begin(); pos != end; ++pos)
451 {
Jim Inghamce579832011-01-24 04:11:25 +0000452 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
Jim Inghamce579832011-01-24 04:11:25 +0000454 switch ((*pos)->ShouldReportRun (event_ptr))
455 {
456 case eVoteNoOpinion:
457 continue;
458 case eVoteYes:
459 if (result == eVoteNoOpinion)
460 result = eVoteYes;
461 break;
462 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000463 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000464 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000465 (*pos)->GetIndexID(),
466 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000467 result = eVoteNo;
468 break;
469 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 }
471 }
472 return result;
473}
474
475void
476ThreadList::Clear()
477{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000478 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 m_stop_id = 0;
480 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000481 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482}
483
484void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000485ThreadList::Destroy()
486{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000487 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000488 const uint32_t num_threads = m_threads.size();
489 for (uint32_t idx = 0; idx < num_threads; ++idx)
490 {
491 m_threads[idx]->DestroyThread();
492 }
493}
494
495void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496ThreadList::RefreshStateAfterStop ()
497{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000498 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499
500 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000501
Greg Clayton5160ce52013-03-27 23:08:40 +0000502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000503 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000504 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505
506 collection::iterator pos, end = m_threads.end();
507 for (pos = m_threads.begin(); pos != end; ++pos)
508 (*pos)->RefreshStateAfterStop ();
509}
510
511void
512ThreadList::DiscardThreadPlans ()
513{
514 // You don't need to update the thread list here, because only threads
515 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000516 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517
518 collection::iterator pos, end = m_threads.end();
519 for (pos = m_threads.begin(); pos != end; ++pos)
520 (*pos)->DiscardThreadPlans (true);
521
522}
523
524bool
525ThreadList::WillResume ()
526{
527 // Run through the threads and perform their momentary actions.
528 // But we only do this for threads that are running, user suspended
529 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000531 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 m_process->UpdateThreadListIfNeeded();
533
534 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535
Jim Inghama3241c12010-07-14 02:27:20 +0000536 // See if any thread wants to run stopping others. If it does, then we won't
537 // setup the other threads for resume, since they aren't going to get a chance
538 // to run. This is necessary because the SetupForResume might add "StopOthers"
539 // plans which would then get to be part of the who-gets-to-run negotiation, but
540 // they're coming in after the fact, and the threads that are already set up should
541 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542
Jim Inghama3241c12010-07-14 02:27:20 +0000543 bool wants_solo_run = false;
544
545 for (pos = m_threads.begin(); pos != end; ++pos)
546 {
Zachary Turner8f186f82015-11-13 21:53:03 +0000547 lldbassert((*pos)->GetCurrentPlan() && "thread should not have null thread plan");
548 if ((*pos)->GetResumeState() != eStateSuspended &&
Jim Inghama3241c12010-07-14 02:27:20 +0000549 (*pos)->GetCurrentPlan()->StopOthers())
550 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000551 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
552 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000553 wants_solo_run = true;
554 break;
555 }
556 }
557
Jim Ingham1c823b42011-01-22 01:33:44 +0000558 if (wants_solo_run)
559 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000560 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000561 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000562 log->Printf ("Turning on notification of new threads while single stepping a thread.");
563 m_process->StartNoticingNewThreads();
564 }
565 else
566 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000567 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000568 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000569 log->Printf ("Turning off notification of new threads while single stepping a thread.");
570 m_process->StopNoticingNewThreads();
571 }
Jim Inghama3241c12010-07-14 02:27:20 +0000572
573 // Give all the threads that are likely to run a last chance to set up their state before we
574 // negotiate who is actually going to get a chance to run...
575 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
576 // call setup on the threads that request StopOthers...
577
578 for (pos = m_threads.begin(); pos != end; ++pos)
579 {
580 if ((*pos)->GetResumeState() != eStateSuspended
581 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
582 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000583 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
584 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000585 (*pos)->SetupForResume ();
586 }
587 }
588
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589 // Now go through the threads and see if any thread wants to run just itself.
590 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000591
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 ThreadList run_me_only_list (m_process);
593
594 run_me_only_list.SetStopID(m_process->GetStopID());
595
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596 bool run_only_current_thread = false;
597
598 for (pos = m_threads.begin(); pos != end; ++pos)
599 {
600 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000601 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 thread_sp->GetCurrentPlan()->StopOthers())
603 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000604 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
605 continue;
606
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 // You can't say "stop others" and also want yourself to be suspended.
608 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
609
Jim Ingham2976d002010-08-26 21:32:51 +0000610 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 {
Jim Inghamacbea8f2015-06-02 20:26:13 +0000612 // If the currently selected thread wants to run on its own, always let it.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 run_only_current_thread = true;
614 run_me_only_list.Clear();
615 run_me_only_list.AddThread (thread_sp);
616 break;
617 }
618
619 run_me_only_list.AddThread (thread_sp);
620 }
621
622 }
623
Jim Ingham513c6bb2012-09-01 01:02:41 +0000624 bool need_to_resume = true;
625
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000626 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 {
628 // Everybody runs as they wish:
629 for (pos = m_threads.begin(); pos != end; ++pos)
630 {
631 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000632 StateType run_state;
633 if (thread_sp->GetResumeState() != eStateSuspended)
634 run_state = thread_sp->GetCurrentPlan()->RunState();
635 else
636 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000637 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000638 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 }
640 }
641 else
642 {
643 ThreadSP thread_to_run;
644
645 if (run_only_current_thread)
646 {
Jim Ingham2976d002010-08-26 21:32:51 +0000647 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 }
649 else if (run_me_only_list.GetSize (false) == 1)
650 {
651 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
652 }
653 else
654 {
655 int random_thread = (int)
656 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
657 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
658 }
659
660 for (pos = m_threads.begin(); pos != end; ++pos)
661 {
662 ThreadSP thread_sp(*pos);
663 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000664 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000665 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000666 need_to_resume = false;
667 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000669 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 }
671 }
672
Jim Ingham513c6bb2012-09-01 01:02:41 +0000673 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674}
675
676void
677ThreadList::DidResume ()
678{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000679 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 collection::iterator pos, end = m_threads.end();
681 for (pos = m_threads.begin(); pos != end; ++pos)
682 {
683 // Don't clear out threads that aren't going to get a chance to run, rather
684 // leave their state for the next time around.
685 ThreadSP thread_sp(*pos);
686 if (thread_sp->GetResumeState() != eStateSuspended)
687 thread_sp->DidResume ();
688 }
689}
690
Andrew Kaylor29d65742013-05-10 17:19:04 +0000691void
692ThreadList::DidStop ()
693{
694 Mutex::Locker locker(GetMutex());
695 collection::iterator pos, end = m_threads.end();
696 for (pos = m_threads.begin(); pos != end; ++pos)
697 {
698 // Notify threads that the process just stopped.
699 // Note, this currently assumes that all threads in the list
700 // stop when the process stops. In the future we will want to support
701 // a debugging model where some threads continue to run while others
702 // are stopped. We either need to handle that somehow here or
703 // create a special thread list containing only threads which will
704 // stop in the code that calls this method (currently
705 // Process::SetPrivateState).
706 ThreadSP thread_sp(*pos);
707 if (StateIsRunningState(thread_sp->GetState()))
708 thread_sp->DidStop ();
709 }
710}
711
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000713ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000715 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000716 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
717 if (!thread_sp.get())
718 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000719 if (m_threads.size() == 0)
720 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000721 m_selected_tid = m_threads[0]->GetID();
722 thread_sp = m_threads[0];
723 }
724 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725}
726
727bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000728ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000730 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000731 ThreadSP selected_thread_sp(FindThreadByID(tid));
732 if (selected_thread_sp)
733 {
Jim Ingham2976d002010-08-26 21:32:51 +0000734 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000735 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
736 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 else
Jim Ingham2976d002010-08-26 21:32:51 +0000738 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739
Jim Inghamc3faa192012-12-11 02:31:48 +0000740 if (notify)
741 NotifySelectedThreadChanged(m_selected_tid);
742
Jim Ingham2976d002010-08-26 21:32:51 +0000743 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744}
745
746bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000747ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000749 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000750 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
751 if (selected_thread_sp.get())
752 {
753 m_selected_tid = selected_thread_sp->GetID();
754 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
755 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 else
Jim Ingham2976d002010-08-26 21:32:51 +0000757 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758
Jim Inghamc3faa192012-12-11 02:31:48 +0000759 if (notify)
760 NotifySelectedThreadChanged(m_selected_tid);
761
Jim Ingham2976d002010-08-26 21:32:51 +0000762 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763}
764
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000765void
Jim Inghamc3faa192012-12-11 02:31:48 +0000766ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
767{
768 ThreadSP selected_thread_sp (FindThreadByID(tid));
769 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
770 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
771 new Thread::ThreadEventData(selected_thread_sp));
772}
773
774void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000775ThreadList::Update (ThreadList &rhs)
776{
777 if (this != &rhs)
778 {
779 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +0000780 // while the assignment occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000781 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000782 m_process = rhs.m_process;
783 m_stop_id = rhs.m_stop_id;
784 m_threads.swap(rhs.m_threads);
785 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000786
787
788 // Now we look for threads that we are done with and
789 // make sure to clear them up as much as possible so
790 // anyone with a shared pointer will still have a reference,
791 // but the thread won't be of much use. Using std::weak_ptr
792 // for all backward references (such as a thread to a process)
793 // will eventually solve this issue for us, but for now, we
794 // need to work around the issue
795 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
796 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
797 {
798 const lldb::tid_t tid = (*rhs_pos)->GetID();
799 bool thread_is_alive = false;
800 const uint32_t num_threads = m_threads.size();
801 for (uint32_t idx = 0; idx < num_threads; ++idx)
802 {
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000803 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
804 if (m_threads[idx]->GetID() == tid || (backing_thread && backing_thread->GetID() == tid))
Greg Claytone1cd1be2012-01-29 20:56:30 +0000805 {
806 thread_is_alive = true;
807 break;
808 }
809 }
810 if (!thread_is_alive)
811 (*rhs_pos)->DestroyThread();
812 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000813 }
814}
815
Greg Claytonfa559e52012-05-18 02:38:05 +0000816void
817ThreadList::Flush ()
818{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000819 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000820 collection::iterator pos, end = m_threads.end();
821 for (pos = m_threads.begin(); pos != end; ++pos)
822 (*pos)->Flush ();
823}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000824
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000825Mutex &
826ThreadList::GetMutex ()
827{
828 return m_process->m_thread_mutex;
829}
830