blob: b175859f2f26f7e5d0adeb31417992dbacf16073 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25ThreadList::ThreadList (Process *process) :
Kuba Breckae4d48012014-09-05 19:13:15 +000026 ThreadCollection(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027 m_process (process),
28 m_stop_id (0),
Jim Ingham2976d002010-08-26 21:32:51 +000029 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030{
31}
32
33ThreadList::ThreadList (const ThreadList &rhs) :
Kuba Breckae4d48012014-09-05 19:13:15 +000034 ThreadCollection(),
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000035 m_process (rhs.m_process),
36 m_stop_id (rhs.m_stop_id),
Jim Ingham2976d002010-08-26 21:32:51 +000037 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038{
39 // Use the assignment operator since it uses the mutex
40 *this = rhs;
41}
42
43const ThreadList&
44ThreadList::operator = (const ThreadList& rhs)
45{
46 if (this != &rhs)
47 {
48 // Lock both mutexes to make sure neither side changes anyone on us
49 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000050 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 m_process = rhs.m_process;
52 m_stop_id = rhs.m_stop_id;
53 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000054 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 }
56 return *this;
57}
58
59
60ThreadList::~ThreadList()
61{
Greg Claytonac358da2013-03-28 18:33:53 +000062 // Clear the thread list. Clear will take the mutex lock
63 // which will ensure that if anyone is using the list
64 // they won't get it removed while using it.
65 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066}
67
68
69uint32_t
70ThreadList::GetStopID () const
71{
72 return m_stop_id;
73}
74
75void
76ThreadList::SetStopID (uint32_t stop_id)
77{
78 m_stop_id = stop_id;
79}
80
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081uint32_t
82ThreadList::GetSize (bool can_update)
83{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000084 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 if (can_update)
86 m_process->UpdateThreadListIfNeeded();
87 return m_threads.size();
88}
89
90ThreadSP
91ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
92{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +000093 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 if (can_update)
95 m_process->UpdateThreadListIfNeeded();
96
97 ThreadSP thread_sp;
98 if (idx < m_threads.size())
99 thread_sp = m_threads[idx];
100 return thread_sp;
101}
102
103ThreadSP
104ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
105{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000106 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107
108 if (can_update)
109 m_process->UpdateThreadListIfNeeded();
110
111 ThreadSP thread_sp;
112 uint32_t idx = 0;
113 const uint32_t num_threads = m_threads.size();
114 for (idx = 0; idx < num_threads; ++idx)
115 {
116 if (m_threads[idx]->GetID() == tid)
117 {
118 thread_sp = m_threads[idx];
119 break;
120 }
121 }
122 return thread_sp;
123}
124
125ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000126ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
127{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000128 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000129
130 if (can_update)
131 m_process->UpdateThreadListIfNeeded();
132
133 ThreadSP thread_sp;
134 uint32_t idx = 0;
135 const uint32_t num_threads = m_threads.size();
136 for (idx = 0; idx < num_threads; ++idx)
137 {
138 if (m_threads[idx]->GetProtocolID() == tid)
139 {
140 thread_sp = m_threads[idx];
141 break;
142 }
143 }
144 return thread_sp;
145}
146
147
148ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000149ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
150{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000151 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000152
153 if (can_update)
154 m_process->UpdateThreadListIfNeeded();
155
156 ThreadSP thread_sp;
157 uint32_t idx = 0;
158 const uint32_t num_threads = m_threads.size();
159 for (idx = 0; idx < num_threads; ++idx)
160 {
161 if (m_threads[idx]->GetID() == tid)
162 {
163 thread_sp = m_threads[idx];
164 m_threads.erase(m_threads.begin()+idx);
165 break;
166 }
167 }
168 return thread_sp;
169}
170
171ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000172ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
173{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000174 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000175
176 if (can_update)
177 m_process->UpdateThreadListIfNeeded();
178
179 ThreadSP thread_sp;
180 uint32_t idx = 0;
181 const uint32_t num_threads = m_threads.size();
182 for (idx = 0; idx < num_threads; ++idx)
183 {
184 if (m_threads[idx]->GetProtocolID() == tid)
185 {
186 thread_sp = m_threads[idx];
187 m_threads.erase(m_threads.begin()+idx);
188 break;
189 }
190 }
191 return thread_sp;
192}
193
194ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
196{
197 ThreadSP thread_sp;
198 if (thread_ptr)
199 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000200 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
202 uint32_t idx = 0;
203 const uint32_t num_threads = m_threads.size();
204 for (idx = 0; idx < num_threads; ++idx)
205 {
206 if (m_threads[idx].get() == thread_ptr)
207 {
208 thread_sp = m_threads[idx];
209 break;
210 }
211 }
212 }
213 return thread_sp;
214}
215
216
217
218ThreadSP
219ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
220{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000221 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222
223 if (can_update)
224 m_process->UpdateThreadListIfNeeded();
225
226 ThreadSP thread_sp;
227 const uint32_t num_threads = m_threads.size();
228 for (uint32_t idx = 0; idx < num_threads; ++idx)
229 {
230 if (m_threads[idx]->GetIndexID() == index_id)
231 {
232 thread_sp = m_threads[idx];
233 break;
234 }
235 }
236 return thread_sp;
237}
238
239bool
240ThreadList::ShouldStop (Event *event_ptr)
241{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 // Running events should never stop, obviously...
243
Greg Clayton5160ce52013-03-27 23:08:40 +0000244 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245
Jim Inghamb42f3af2012-11-15 22:44:04 +0000246 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000247 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000248 // to keep the ThreadList locked the whole time we are doing this.
249 // FIXME: It is possible that running code could cause new threads
250 // to be created. If that happens we will miss asking them whether
251 // then should stop. This is not a big deal, since we haven't had
252 // a chance to hang any interesting operations on those threads yet.
253
254 collection threads_copy;
255 {
256 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000257 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258
Jim Inghamb42f3af2012-11-15 22:44:04 +0000259 m_process->UpdateThreadListIfNeeded();
260 threads_copy = m_threads;
261 }
262
263 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
Greg Clayton2cad65a2010-09-03 17:10:42 +0000265 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000266 {
267 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000268 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000269 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000270
Jim Inghama0079042013-03-21 21:46:56 +0000271 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000272
273 // 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 +0000274 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000275 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
276 {
277 if (log)
278 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
279
280 should_stop = true;
281 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000282
283 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
284 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
285 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
286 // We don't need to use it here, we just want to make sure it gets computed.
287
288 for (pos = threads_copy.begin(); pos != end; ++pos)
289 {
290 ThreadSP thread_sp(*pos);
291 thread_sp->GetStopInfo();
292 }
Jim Inghama0079042013-03-21 21:46:56 +0000293
Jim Inghamb42f3af2012-11-15 22:44:04 +0000294 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 {
296 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000297
Jim Ingham39fdae72014-01-15 03:32:42 +0000298 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
299 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
300 // the right thing to do and stopping gives the user control over what to do in this instance.
301 //
302 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
303 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
304 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
305 // code and causes us to stop seemingly for no reason.
306 //
307 // 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
308 // to true unless this is the first stop.
309 //
310 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
311 // everywhere but at this check.
312
Ed Maste21afbe02014-01-17 17:31:06 +0000313 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000314 did_anybody_stop_for_a_reason = true;
315 else
316 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000317
Jim Ingham10c4b242011-10-15 00:23:43 +0000318 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000319 if (thread_should_stop)
320 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000322
Jim Inghama0079042013-03-21 21:46:56 +0000323 if (!should_stop && !did_anybody_stop_for_a_reason)
324 {
325 should_stop = true;
326 if (log)
327 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
328 }
329
Greg Clayton2cad65a2010-09-03 17:10:42 +0000330 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000331 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000332
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 if (should_stop)
334 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000335 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 {
337 ThreadSP thread_sp(*pos);
338 thread_sp->WillStop ();
339 }
340 }
341
342 return should_stop;
343}
344
345Vote
346ThreadList::ShouldReportStop (Event *event_ptr)
347{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000348 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000349
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 Vote result = eVoteNoOpinion;
351 m_process->UpdateThreadListIfNeeded();
352 collection::iterator pos, end = m_threads.end();
353
Greg Clayton5160ce52013-03-27 23:08:40 +0000354 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000355
356 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000357 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000358
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 // Run through the threads and ask whether we should report this event.
360 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
361 for (pos = m_threads.begin(); pos != end; ++pos)
362 {
363 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000364 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
365 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366 {
Jim Ingham92087d82012-01-31 23:09:20 +0000367 case eVoteNoOpinion:
368 continue;
369
370 case eVoteYes:
371 result = eVoteYes;
372 break;
373
374 case eVoteNo:
375 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 {
Jim Ingham92087d82012-01-31 23:09:20 +0000377 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 }
Jim Ingham92087d82012-01-31 23:09:20 +0000379 else
380 {
381 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000382 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 +0000383 __FUNCTION__,
384 thread_sp->GetID (),
385 GetVoteAsCString (vote),
386 GetVoteAsCString (result));
387 }
388 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
390 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000391 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000392 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 return result;
394}
395
Jim Ingham221d51c2013-05-08 00:35:16 +0000396void
397ThreadList::SetShouldReportStop (Vote vote)
398{
399 Mutex::Locker locker(GetMutex());
400 m_process->UpdateThreadListIfNeeded();
401 collection::iterator pos, end = m_threads.end();
402 for (pos = m_threads.begin(); pos != end; ++pos)
403 {
404 ThreadSP thread_sp(*pos);
405 thread_sp->SetShouldReportStop (vote);
406 }
407}
408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409Vote
410ThreadList::ShouldReportRun (Event *event_ptr)
411{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000412
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000413 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000414
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 Vote result = eVoteNoOpinion;
416 m_process->UpdateThreadListIfNeeded();
417 collection::iterator pos, end = m_threads.end();
418
419 // Run through the threads and ask whether we should report this event.
420 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
421
Greg Clayton5160ce52013-03-27 23:08:40 +0000422 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000423
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 for (pos = m_threads.begin(); pos != end; ++pos)
425 {
Jim Inghamce579832011-01-24 04:11:25 +0000426 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 {
Jim Inghamce579832011-01-24 04:11:25 +0000428 switch ((*pos)->ShouldReportRun (event_ptr))
429 {
430 case eVoteNoOpinion:
431 continue;
432 case eVoteYes:
433 if (result == eVoteNoOpinion)
434 result = eVoteYes;
435 break;
436 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000437 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000438 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000439 (*pos)->GetIndexID(),
440 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000441 result = eVoteNo;
442 break;
443 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 }
445 }
446 return result;
447}
448
449void
450ThreadList::Clear()
451{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000452 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 m_stop_id = 0;
454 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000455 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456}
457
458void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000459ThreadList::Destroy()
460{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000461 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000462 const uint32_t num_threads = m_threads.size();
463 for (uint32_t idx = 0; idx < num_threads; ++idx)
464 {
465 m_threads[idx]->DestroyThread();
466 }
467}
468
469void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470ThreadList::RefreshStateAfterStop ()
471{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000472 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473
474 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000475
Greg Clayton5160ce52013-03-27 23:08:40 +0000476 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000477 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000478 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479
480 collection::iterator pos, end = m_threads.end();
481 for (pos = m_threads.begin(); pos != end; ++pos)
482 (*pos)->RefreshStateAfterStop ();
483}
484
485void
486ThreadList::DiscardThreadPlans ()
487{
488 // You don't need to update the thread list here, because only threads
489 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000490 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
492 collection::iterator pos, end = m_threads.end();
493 for (pos = m_threads.begin(); pos != end; ++pos)
494 (*pos)->DiscardThreadPlans (true);
495
496}
497
498bool
499ThreadList::WillResume ()
500{
501 // Run through the threads and perform their momentary actions.
502 // But we only do this for threads that are running, user suspended
503 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000505 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 m_process->UpdateThreadListIfNeeded();
507
508 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509
Jim Inghama3241c12010-07-14 02:27:20 +0000510 // See if any thread wants to run stopping others. If it does, then we won't
511 // setup the other threads for resume, since they aren't going to get a chance
512 // to run. This is necessary because the SetupForResume might add "StopOthers"
513 // plans which would then get to be part of the who-gets-to-run negotiation, but
514 // they're coming in after the fact, and the threads that are already set up should
515 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516
Jim Inghama3241c12010-07-14 02:27:20 +0000517 bool wants_solo_run = false;
518
519 for (pos = m_threads.begin(); pos != end; ++pos)
520 {
521 if ((*pos)->GetResumeState() != eStateSuspended &&
522 (*pos)->GetCurrentPlan()->StopOthers())
523 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000524 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
525 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000526 wants_solo_run = true;
527 break;
528 }
529 }
530
Jim Ingham1c823b42011-01-22 01:33:44 +0000531 if (wants_solo_run)
532 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000533 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000534 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000535 log->Printf ("Turning on notification of new threads while single stepping a thread.");
536 m_process->StartNoticingNewThreads();
537 }
538 else
539 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000541 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000542 log->Printf ("Turning off notification of new threads while single stepping a thread.");
543 m_process->StopNoticingNewThreads();
544 }
Jim Inghama3241c12010-07-14 02:27:20 +0000545
546 // Give all the threads that are likely to run a last chance to set up their state before we
547 // negotiate who is actually going to get a chance to run...
548 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
549 // call setup on the threads that request StopOthers...
550
551 for (pos = m_threads.begin(); pos != end; ++pos)
552 {
553 if ((*pos)->GetResumeState() != eStateSuspended
554 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
555 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000556 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
557 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000558 (*pos)->SetupForResume ();
559 }
560 }
561
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 // Now go through the threads and see if any thread wants to run just itself.
563 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000564
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 ThreadList run_me_only_list (m_process);
566
567 run_me_only_list.SetStopID(m_process->GetStopID());
568
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 bool run_only_current_thread = false;
570
571 for (pos = m_threads.begin(); pos != end; ++pos)
572 {
573 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000574 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575 thread_sp->GetCurrentPlan()->StopOthers())
576 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000577 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
578 continue;
579
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 // You can't say "stop others" and also want yourself to be suspended.
581 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
582
Jim Ingham2976d002010-08-26 21:32:51 +0000583 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 {
585 run_only_current_thread = true;
586 run_me_only_list.Clear();
587 run_me_only_list.AddThread (thread_sp);
588 break;
589 }
590
591 run_me_only_list.AddThread (thread_sp);
592 }
593
594 }
595
Jim Ingham513c6bb2012-09-01 01:02:41 +0000596 bool need_to_resume = true;
597
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000598 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 {
600 // Everybody runs as they wish:
601 for (pos = m_threads.begin(); pos != end; ++pos)
602 {
603 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000604 StateType run_state;
605 if (thread_sp->GetResumeState() != eStateSuspended)
606 run_state = thread_sp->GetCurrentPlan()->RunState();
607 else
608 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000609 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000610 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 }
612 }
613 else
614 {
615 ThreadSP thread_to_run;
616
617 if (run_only_current_thread)
618 {
Jim Ingham2976d002010-08-26 21:32:51 +0000619 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 }
621 else if (run_me_only_list.GetSize (false) == 1)
622 {
623 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
624 }
625 else
626 {
627 int random_thread = (int)
628 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
629 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
630 }
631
632 for (pos = m_threads.begin(); pos != end; ++pos)
633 {
634 ThreadSP thread_sp(*pos);
635 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000636 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000637 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000638 need_to_resume = false;
639 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000641 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 }
643 }
644
Jim Ingham513c6bb2012-09-01 01:02:41 +0000645 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646}
647
648void
649ThreadList::DidResume ()
650{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000651 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 collection::iterator pos, end = m_threads.end();
653 for (pos = m_threads.begin(); pos != end; ++pos)
654 {
655 // Don't clear out threads that aren't going to get a chance to run, rather
656 // leave their state for the next time around.
657 ThreadSP thread_sp(*pos);
658 if (thread_sp->GetResumeState() != eStateSuspended)
659 thread_sp->DidResume ();
660 }
661}
662
Andrew Kaylor29d65742013-05-10 17:19:04 +0000663void
664ThreadList::DidStop ()
665{
666 Mutex::Locker locker(GetMutex());
667 collection::iterator pos, end = m_threads.end();
668 for (pos = m_threads.begin(); pos != end; ++pos)
669 {
670 // Notify threads that the process just stopped.
671 // Note, this currently assumes that all threads in the list
672 // stop when the process stops. In the future we will want to support
673 // a debugging model where some threads continue to run while others
674 // are stopped. We either need to handle that somehow here or
675 // create a special thread list containing only threads which will
676 // stop in the code that calls this method (currently
677 // Process::SetPrivateState).
678 ThreadSP thread_sp(*pos);
679 if (StateIsRunningState(thread_sp->GetState()))
680 thread_sp->DidStop ();
681 }
682}
683
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000685ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000687 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000688 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
689 if (!thread_sp.get())
690 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000691 if (m_threads.size() == 0)
692 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000693 m_selected_tid = m_threads[0]->GetID();
694 thread_sp = m_threads[0];
695 }
696 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
699bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000700ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000702 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000703 ThreadSP selected_thread_sp(FindThreadByID(tid));
704 if (selected_thread_sp)
705 {
Jim Ingham2976d002010-08-26 21:32:51 +0000706 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000707 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
708 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 else
Jim Ingham2976d002010-08-26 21:32:51 +0000710 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711
Jim Inghamc3faa192012-12-11 02:31:48 +0000712 if (notify)
713 NotifySelectedThreadChanged(m_selected_tid);
714
Jim Ingham2976d002010-08-26 21:32:51 +0000715 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716}
717
718bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000719ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000721 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000722 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
723 if (selected_thread_sp.get())
724 {
725 m_selected_tid = selected_thread_sp->GetID();
726 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
727 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 else
Jim Ingham2976d002010-08-26 21:32:51 +0000729 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730
Jim Inghamc3faa192012-12-11 02:31:48 +0000731 if (notify)
732 NotifySelectedThreadChanged(m_selected_tid);
733
Jim Ingham2976d002010-08-26 21:32:51 +0000734 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735}
736
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000737void
Jim Inghamc3faa192012-12-11 02:31:48 +0000738ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
739{
740 ThreadSP selected_thread_sp (FindThreadByID(tid));
741 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
742 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
743 new Thread::ThreadEventData(selected_thread_sp));
744}
745
746void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000747ThreadList::Update (ThreadList &rhs)
748{
749 if (this != &rhs)
750 {
751 // Lock both mutexes to make sure neither side changes anyone on us
752 // while the assignement occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000753 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000754 m_process = rhs.m_process;
755 m_stop_id = rhs.m_stop_id;
756 m_threads.swap(rhs.m_threads);
757 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000758
759
760 // Now we look for threads that we are done with and
761 // make sure to clear them up as much as possible so
762 // anyone with a shared pointer will still have a reference,
763 // but the thread won't be of much use. Using std::weak_ptr
764 // for all backward references (such as a thread to a process)
765 // will eventually solve this issue for us, but for now, we
766 // need to work around the issue
767 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
768 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
769 {
770 const lldb::tid_t tid = (*rhs_pos)->GetID();
771 bool thread_is_alive = false;
772 const uint32_t num_threads = m_threads.size();
773 for (uint32_t idx = 0; idx < num_threads; ++idx)
774 {
775 if (m_threads[idx]->GetID() == tid)
776 {
777 thread_is_alive = true;
778 break;
779 }
780 }
781 if (!thread_is_alive)
782 (*rhs_pos)->DestroyThread();
783 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000784 }
785}
786
Greg Claytonfa559e52012-05-18 02:38:05 +0000787void
788ThreadList::Flush ()
789{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000790 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000791 collection::iterator pos, end = m_threads.end();
792 for (pos = m_threads.begin(); pos != end; ++pos)
793 (*pos)->Flush ();
794}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000795
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000796Mutex &
797ThreadList::GetMutex ()
798{
799 return m_process->m_thread_mutex;
800}
801