blob: db4407b4b5791408905d6ceb20405f7ce5422bf6 [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) :
Kuba Breckae4d48012014-09-05 19:13:15 +000025 ThreadCollection(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026 m_process (process),
27 m_stop_id (0),
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) :
Kuba Breckae4d48012014-09-05 19:13:15 +000033 ThreadCollection(),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000034 m_process (rhs.m_process),
35 m_stop_id (rhs.m_stop_id),
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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080uint32_t
81ThreadList::GetSize (bool can_update)
82{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000083 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 if (can_update)
85 m_process->UpdateThreadListIfNeeded();
86 return m_threads.size();
87}
88
89ThreadSP
90ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
91{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000092 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 if (can_update)
94 m_process->UpdateThreadListIfNeeded();
95
96 ThreadSP thread_sp;
97 if (idx < m_threads.size())
98 thread_sp = m_threads[idx];
99 return thread_sp;
100}
101
102ThreadSP
103ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
104{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000105 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
107 if (can_update)
108 m_process->UpdateThreadListIfNeeded();
109
110 ThreadSP thread_sp;
111 uint32_t idx = 0;
112 const uint32_t num_threads = m_threads.size();
113 for (idx = 0; idx < num_threads; ++idx)
114 {
115 if (m_threads[idx]->GetID() == tid)
116 {
117 thread_sp = m_threads[idx];
118 break;
119 }
120 }
121 return thread_sp;
122}
123
124ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000125ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
126{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000127 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000128
129 if (can_update)
130 m_process->UpdateThreadListIfNeeded();
131
132 ThreadSP thread_sp;
133 uint32_t idx = 0;
134 const uint32_t num_threads = m_threads.size();
135 for (idx = 0; idx < num_threads; ++idx)
136 {
137 if (m_threads[idx]->GetProtocolID() == tid)
138 {
139 thread_sp = m_threads[idx];
140 break;
141 }
142 }
143 return thread_sp;
144}
145
146
147ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000148ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
149{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000150 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000151
152 if (can_update)
153 m_process->UpdateThreadListIfNeeded();
154
155 ThreadSP thread_sp;
156 uint32_t idx = 0;
157 const uint32_t num_threads = m_threads.size();
158 for (idx = 0; idx < num_threads; ++idx)
159 {
160 if (m_threads[idx]->GetID() == tid)
161 {
162 thread_sp = m_threads[idx];
163 m_threads.erase(m_threads.begin()+idx);
164 break;
165 }
166 }
167 return thread_sp;
168}
169
170ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000171ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
172{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000173 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000174
175 if (can_update)
176 m_process->UpdateThreadListIfNeeded();
177
178 ThreadSP thread_sp;
179 uint32_t idx = 0;
180 const uint32_t num_threads = m_threads.size();
181 for (idx = 0; idx < num_threads; ++idx)
182 {
183 if (m_threads[idx]->GetProtocolID() == tid)
184 {
185 thread_sp = m_threads[idx];
186 m_threads.erase(m_threads.begin()+idx);
187 break;
188 }
189 }
190 return thread_sp;
191}
192
193ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
195{
196 ThreadSP thread_sp;
197 if (thread_ptr)
198 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000199 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
201 uint32_t idx = 0;
202 const uint32_t num_threads = m_threads.size();
203 for (idx = 0; idx < num_threads; ++idx)
204 {
205 if (m_threads[idx].get() == thread_ptr)
206 {
207 thread_sp = m_threads[idx];
208 break;
209 }
210 }
211 }
212 return thread_sp;
213}
214
215
216
217ThreadSP
218ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
219{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000220 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221
222 if (can_update)
223 m_process->UpdateThreadListIfNeeded();
224
225 ThreadSP thread_sp;
226 const uint32_t num_threads = m_threads.size();
227 for (uint32_t idx = 0; idx < num_threads; ++idx)
228 {
229 if (m_threads[idx]->GetIndexID() == index_id)
230 {
231 thread_sp = m_threads[idx];
232 break;
233 }
234 }
235 return thread_sp;
236}
237
238bool
239ThreadList::ShouldStop (Event *event_ptr)
240{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 // Running events should never stop, obviously...
242
Greg Clayton5160ce52013-03-27 23:08:40 +0000243 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244
Jim Inghamb42f3af2012-11-15 22:44:04 +0000245 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000246 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000247 // to keep the ThreadList locked the whole time we are doing this.
248 // FIXME: It is possible that running code could cause new threads
249 // to be created. If that happens we will miss asking them whether
250 // then should stop. This is not a big deal, since we haven't had
251 // a chance to hang any interesting operations on those threads yet.
252
253 collection threads_copy;
254 {
255 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000256 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257
Jim Inghamb42f3af2012-11-15 22:44:04 +0000258 m_process->UpdateThreadListIfNeeded();
259 threads_copy = m_threads;
260 }
261
262 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
Greg Clayton2cad65a2010-09-03 17:10:42 +0000264 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000265 {
266 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000267 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000268 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000269
Jim Inghama0079042013-03-21 21:46:56 +0000270 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000271
272 // 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 +0000273 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000274 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
275 {
276 if (log)
277 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
278
279 should_stop = true;
280 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000281
282 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
283 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
284 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
285 // We don't need to use it here, we just want to make sure it gets computed.
286
287 for (pos = threads_copy.begin(); pos != end; ++pos)
288 {
289 ThreadSP thread_sp(*pos);
290 thread_sp->GetStopInfo();
291 }
Jim Inghama0079042013-03-21 21:46:56 +0000292
Jim Inghamb42f3af2012-11-15 22:44:04 +0000293 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 {
295 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000296
Jim Ingham39fdae72014-01-15 03:32:42 +0000297 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
298 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
299 // the right thing to do and stopping gives the user control over what to do in this instance.
300 //
301 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
302 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
303 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
304 // code and causes us to stop seemingly for no reason.
305 //
306 // 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
307 // to true unless this is the first stop.
308 //
309 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
310 // everywhere but at this check.
311
Ed Maste21afbe02014-01-17 17:31:06 +0000312 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000313 did_anybody_stop_for_a_reason = true;
314 else
315 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000316
Jim Ingham10c4b242011-10-15 00:23:43 +0000317 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000318 if (thread_should_stop)
319 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000321
Jim Inghama0079042013-03-21 21:46:56 +0000322 if (!should_stop && !did_anybody_stop_for_a_reason)
323 {
324 should_stop = true;
325 if (log)
326 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
327 }
328
Greg Clayton2cad65a2010-09-03 17:10:42 +0000329 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000330 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000331
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 if (should_stop)
333 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000334 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 {
336 ThreadSP thread_sp(*pos);
337 thread_sp->WillStop ();
338 }
339 }
340
341 return should_stop;
342}
343
344Vote
345ThreadList::ShouldReportStop (Event *event_ptr)
346{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000347 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000348
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 Vote result = eVoteNoOpinion;
350 m_process->UpdateThreadListIfNeeded();
351 collection::iterator pos, end = m_threads.end();
352
Greg Clayton5160ce52013-03-27 23:08:40 +0000353 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000354
355 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000356 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000357
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 // Run through the threads and ask whether we should report this event.
359 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
360 for (pos = m_threads.begin(); pos != end; ++pos)
361 {
362 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000363 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
364 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 {
Jim Ingham92087d82012-01-31 23:09:20 +0000366 case eVoteNoOpinion:
367 continue;
368
369 case eVoteYes:
370 result = eVoteYes;
371 break;
372
373 case eVoteNo:
374 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 {
Jim Ingham92087d82012-01-31 23:09:20 +0000376 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 }
Jim Ingham92087d82012-01-31 23:09:20 +0000378 else
379 {
380 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000381 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 +0000382 __FUNCTION__,
383 thread_sp->GetID (),
384 GetVoteAsCString (vote),
385 GetVoteAsCString (result));
386 }
387 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 }
389 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000390 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000391 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 return result;
393}
394
Jim Ingham221d51c2013-05-08 00:35:16 +0000395void
396ThreadList::SetShouldReportStop (Vote vote)
397{
398 Mutex::Locker locker(GetMutex());
399 m_process->UpdateThreadListIfNeeded();
400 collection::iterator pos, end = m_threads.end();
401 for (pos = m_threads.begin(); pos != end; ++pos)
402 {
403 ThreadSP thread_sp(*pos);
404 thread_sp->SetShouldReportStop (vote);
405 }
406}
407
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408Vote
409ThreadList::ShouldReportRun (Event *event_ptr)
410{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000411
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000412 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000413
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 Vote result = eVoteNoOpinion;
415 m_process->UpdateThreadListIfNeeded();
416 collection::iterator pos, end = m_threads.end();
417
418 // Run through the threads and ask whether we should report this event.
419 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
420
Greg Clayton5160ce52013-03-27 23:08:40 +0000421 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000422
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 for (pos = m_threads.begin(); pos != end; ++pos)
424 {
Jim Inghamce579832011-01-24 04:11:25 +0000425 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 {
Jim Inghamce579832011-01-24 04:11:25 +0000427 switch ((*pos)->ShouldReportRun (event_ptr))
428 {
429 case eVoteNoOpinion:
430 continue;
431 case eVoteYes:
432 if (result == eVoteNoOpinion)
433 result = eVoteYes;
434 break;
435 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000436 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000437 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000438 (*pos)->GetIndexID(),
439 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000440 result = eVoteNo;
441 break;
442 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443 }
444 }
445 return result;
446}
447
448void
449ThreadList::Clear()
450{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000451 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 m_stop_id = 0;
453 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000454 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455}
456
457void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000458ThreadList::Destroy()
459{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000460 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000461 const uint32_t num_threads = m_threads.size();
462 for (uint32_t idx = 0; idx < num_threads; ++idx)
463 {
464 m_threads[idx]->DestroyThread();
465 }
466}
467
468void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469ThreadList::RefreshStateAfterStop ()
470{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000471 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472
473 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000474
Greg Clayton5160ce52013-03-27 23:08:40 +0000475 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000476 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000477 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478
479 collection::iterator pos, end = m_threads.end();
480 for (pos = m_threads.begin(); pos != end; ++pos)
481 (*pos)->RefreshStateAfterStop ();
482}
483
484void
485ThreadList::DiscardThreadPlans ()
486{
487 // You don't need to update the thread list here, because only threads
488 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000489 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490
491 collection::iterator pos, end = m_threads.end();
492 for (pos = m_threads.begin(); pos != end; ++pos)
493 (*pos)->DiscardThreadPlans (true);
494
495}
496
497bool
498ThreadList::WillResume ()
499{
500 // Run through the threads and perform their momentary actions.
501 // But we only do this for threads that are running, user suspended
502 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000504 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 m_process->UpdateThreadListIfNeeded();
506
507 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508
Jim Inghama3241c12010-07-14 02:27:20 +0000509 // See if any thread wants to run stopping others. If it does, then we won't
510 // setup the other threads for resume, since they aren't going to get a chance
511 // to run. This is necessary because the SetupForResume might add "StopOthers"
512 // plans which would then get to be part of the who-gets-to-run negotiation, but
513 // they're coming in after the fact, and the threads that are already set up should
514 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Jim Inghama3241c12010-07-14 02:27:20 +0000516 bool wants_solo_run = false;
517
518 for (pos = m_threads.begin(); pos != end; ++pos)
519 {
520 if ((*pos)->GetResumeState() != eStateSuspended &&
521 (*pos)->GetCurrentPlan()->StopOthers())
522 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000523 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
524 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000525 wants_solo_run = true;
526 break;
527 }
528 }
529
Jim Ingham1c823b42011-01-22 01:33:44 +0000530 if (wants_solo_run)
531 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000533 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000534 log->Printf ("Turning on notification of new threads while single stepping a thread.");
535 m_process->StartNoticingNewThreads();
536 }
537 else
538 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000539 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000540 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000541 log->Printf ("Turning off notification of new threads while single stepping a thread.");
542 m_process->StopNoticingNewThreads();
543 }
Jim Inghama3241c12010-07-14 02:27:20 +0000544
545 // Give all the threads that are likely to run a last chance to set up their state before we
546 // negotiate who is actually going to get a chance to run...
547 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
548 // call setup on the threads that request StopOthers...
549
550 for (pos = m_threads.begin(); pos != end; ++pos)
551 {
552 if ((*pos)->GetResumeState() != eStateSuspended
553 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
554 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000555 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
556 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000557 (*pos)->SetupForResume ();
558 }
559 }
560
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 // Now go through the threads and see if any thread wants to run just itself.
562 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000563
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 ThreadList run_me_only_list (m_process);
565
566 run_me_only_list.SetStopID(m_process->GetStopID());
567
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 bool run_only_current_thread = false;
569
570 for (pos = m_threads.begin(); pos != end; ++pos)
571 {
572 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000573 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 thread_sp->GetCurrentPlan()->StopOthers())
575 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000576 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
577 continue;
578
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 // You can't say "stop others" and also want yourself to be suspended.
580 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
581
Jim Ingham2976d002010-08-26 21:32:51 +0000582 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 {
584 run_only_current_thread = true;
585 run_me_only_list.Clear();
586 run_me_only_list.AddThread (thread_sp);
587 break;
588 }
589
590 run_me_only_list.AddThread (thread_sp);
591 }
592
593 }
594
Jim Ingham513c6bb2012-09-01 01:02:41 +0000595 bool need_to_resume = true;
596
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000597 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 {
599 // Everybody runs as they wish:
600 for (pos = m_threads.begin(); pos != end; ++pos)
601 {
602 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000603 StateType run_state;
604 if (thread_sp->GetResumeState() != eStateSuspended)
605 run_state = thread_sp->GetCurrentPlan()->RunState();
606 else
607 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000608 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000609 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 }
611 }
612 else
613 {
614 ThreadSP thread_to_run;
615
616 if (run_only_current_thread)
617 {
Jim Ingham2976d002010-08-26 21:32:51 +0000618 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 }
620 else if (run_me_only_list.GetSize (false) == 1)
621 {
622 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
623 }
624 else
625 {
626 int random_thread = (int)
627 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
628 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
629 }
630
631 for (pos = m_threads.begin(); pos != end; ++pos)
632 {
633 ThreadSP thread_sp(*pos);
634 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000635 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000636 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000637 need_to_resume = false;
638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000640 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 }
642 }
643
Jim Ingham513c6bb2012-09-01 01:02:41 +0000644 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645}
646
647void
648ThreadList::DidResume ()
649{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000650 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 collection::iterator pos, end = m_threads.end();
652 for (pos = m_threads.begin(); pos != end; ++pos)
653 {
654 // Don't clear out threads that aren't going to get a chance to run, rather
655 // leave their state for the next time around.
656 ThreadSP thread_sp(*pos);
657 if (thread_sp->GetResumeState() != eStateSuspended)
658 thread_sp->DidResume ();
659 }
660}
661
Andrew Kaylor29d65742013-05-10 17:19:04 +0000662void
663ThreadList::DidStop ()
664{
665 Mutex::Locker locker(GetMutex());
666 collection::iterator pos, end = m_threads.end();
667 for (pos = m_threads.begin(); pos != end; ++pos)
668 {
669 // Notify threads that the process just stopped.
670 // Note, this currently assumes that all threads in the list
671 // stop when the process stops. In the future we will want to support
672 // a debugging model where some threads continue to run while others
673 // are stopped. We either need to handle that somehow here or
674 // create a special thread list containing only threads which will
675 // stop in the code that calls this method (currently
676 // Process::SetPrivateState).
677 ThreadSP thread_sp(*pos);
678 if (StateIsRunningState(thread_sp->GetState()))
679 thread_sp->DidStop ();
680 }
681}
682
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000684ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000686 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000687 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
688 if (!thread_sp.get())
689 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000690 if (m_threads.size() == 0)
691 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000692 m_selected_tid = m_threads[0]->GetID();
693 thread_sp = m_threads[0];
694 }
695 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696}
697
698bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000699ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000701 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000702 ThreadSP selected_thread_sp(FindThreadByID(tid));
703 if (selected_thread_sp)
704 {
Jim Ingham2976d002010-08-26 21:32:51 +0000705 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000706 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
707 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 else
Jim Ingham2976d002010-08-26 21:32:51 +0000709 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710
Jim Inghamc3faa192012-12-11 02:31:48 +0000711 if (notify)
712 NotifySelectedThreadChanged(m_selected_tid);
713
Jim Ingham2976d002010-08-26 21:32:51 +0000714 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715}
716
717bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000718ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, 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 (FindThreadByIndexID(index_id));
722 if (selected_thread_sp.get())
723 {
724 m_selected_tid = selected_thread_sp->GetID();
725 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
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000736void
Jim Inghamc3faa192012-12-11 02:31:48 +0000737ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
738{
739 ThreadSP selected_thread_sp (FindThreadByID(tid));
740 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
741 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
742 new Thread::ThreadEventData(selected_thread_sp));
743}
744
745void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000746ThreadList::Update (ThreadList &rhs)
747{
748 if (this != &rhs)
749 {
750 // Lock both mutexes to make sure neither side changes anyone on us
751 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000752 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000753 m_process = rhs.m_process;
754 m_stop_id = rhs.m_stop_id;
755 m_threads.swap(rhs.m_threads);
756 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000757
758
759 // Now we look for threads that we are done with and
760 // make sure to clear them up as much as possible so
761 // anyone with a shared pointer will still have a reference,
762 // but the thread won't be of much use. Using std::weak_ptr
763 // for all backward references (such as a thread to a process)
764 // will eventually solve this issue for us, but for now, we
765 // need to work around the issue
766 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
767 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
768 {
769 const lldb::tid_t tid = (*rhs_pos)->GetID();
770 bool thread_is_alive = false;
771 const uint32_t num_threads = m_threads.size();
772 for (uint32_t idx = 0; idx < num_threads; ++idx)
773 {
774 if (m_threads[idx]->GetID() == tid)
775 {
776 thread_is_alive = true;
777 break;
778 }
779 }
780 if (!thread_is_alive)
781 (*rhs_pos)->DestroyThread();
782 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000783 }
784}
785
Greg Claytonfa559e52012-05-18 02:38:05 +0000786void
787ThreadList::Flush ()
788{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000789 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000790 collection::iterator pos, end = m_threads.end();
791 for (pos = m_threads.begin(); pos != end; ++pos)
792 (*pos)->Flush ();
793}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000794
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000795Mutex &
796ThreadList::GetMutex ()
797{
798 return m_process->m_thread_mutex;
799}
800