blob: 3dc37f1fa0e0a4505113a9d7c5679016918759e8 [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
Jim Ingham8d94ba02016-03-12 02:45:34 +000074lldb::ThreadSP
75ThreadList::GetExpressionExecutionThread()
76{
77 if (m_expression_tid_stack.empty())
78 return GetSelectedThread();
79 ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
80 if (expr_thread_sp)
81 return expr_thread_sp;
82 else
83 return GetSelectedThread();
84}
85
86void
87ThreadList::PushExpressionExecutionThread(lldb::tid_t tid)
88{
89 m_expression_tid_stack.push_back(tid);
90}
91
92void
93ThreadList::PopExpressionExecutionThread(lldb::tid_t tid)
94{
95 assert(m_expression_tid_stack.back() == tid);
96 m_expression_tid_stack.pop_back();
97}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
99uint32_t
100ThreadList::GetStopID () const
101{
102 return m_stop_id;
103}
104
105void
106ThreadList::SetStopID (uint32_t stop_id)
107{
108 m_stop_id = stop_id;
109}
110
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111uint32_t
112ThreadList::GetSize (bool can_update)
113{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000114 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 if (can_update)
116 m_process->UpdateThreadListIfNeeded();
117 return m_threads.size();
118}
119
120ThreadSP
121ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
122{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000123 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 if (can_update)
125 m_process->UpdateThreadListIfNeeded();
126
127 ThreadSP thread_sp;
128 if (idx < m_threads.size())
129 thread_sp = m_threads[idx];
130 return thread_sp;
131}
132
133ThreadSP
134ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
135{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000136 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137
138 if (can_update)
139 m_process->UpdateThreadListIfNeeded();
140
141 ThreadSP thread_sp;
142 uint32_t idx = 0;
143 const uint32_t num_threads = m_threads.size();
144 for (idx = 0; idx < num_threads; ++idx)
145 {
146 if (m_threads[idx]->GetID() == tid)
147 {
148 thread_sp = m_threads[idx];
149 break;
150 }
151 }
152 return thread_sp;
153}
154
155ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000156ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
157{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000158 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000159
160 if (can_update)
161 m_process->UpdateThreadListIfNeeded();
162
163 ThreadSP thread_sp;
164 uint32_t idx = 0;
165 const uint32_t num_threads = m_threads.size();
166 for (idx = 0; idx < num_threads; ++idx)
167 {
168 if (m_threads[idx]->GetProtocolID() == tid)
169 {
170 thread_sp = m_threads[idx];
171 break;
172 }
173 }
174 return thread_sp;
175}
176
177
178ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000179ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
180{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000181 Mutex::Locker locker(GetMutex());
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000182
183 if (can_update)
184 m_process->UpdateThreadListIfNeeded();
185
186 ThreadSP thread_sp;
187 uint32_t idx = 0;
188 const uint32_t num_threads = m_threads.size();
189 for (idx = 0; idx < num_threads; ++idx)
190 {
191 if (m_threads[idx]->GetID() == tid)
192 {
193 thread_sp = m_threads[idx];
194 m_threads.erase(m_threads.begin()+idx);
195 break;
196 }
197 }
198 return thread_sp;
199}
200
201ThreadSP
Greg Clayton160c9d82013-05-01 21:54:04 +0000202ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
203{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000204 Mutex::Locker locker(GetMutex());
Greg Clayton160c9d82013-05-01 21:54:04 +0000205
206 if (can_update)
207 m_process->UpdateThreadListIfNeeded();
208
209 ThreadSP thread_sp;
210 uint32_t idx = 0;
211 const uint32_t num_threads = m_threads.size();
212 for (idx = 0; idx < num_threads; ++idx)
213 {
214 if (m_threads[idx]->GetProtocolID() == tid)
215 {
216 thread_sp = m_threads[idx];
217 m_threads.erase(m_threads.begin()+idx);
218 break;
219 }
220 }
221 return thread_sp;
222}
223
224ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
226{
227 ThreadSP thread_sp;
228 if (thread_ptr)
229 {
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000230 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231
232 uint32_t idx = 0;
233 const uint32_t num_threads = m_threads.size();
234 for (idx = 0; idx < num_threads; ++idx)
235 {
236 if (m_threads[idx].get() == thread_ptr)
237 {
238 thread_sp = m_threads[idx];
239 break;
240 }
241 }
242 }
243 return thread_sp;
244}
245
246
247
248ThreadSP
249ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
250{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000251 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252
253 if (can_update)
254 m_process->UpdateThreadListIfNeeded();
255
256 ThreadSP thread_sp;
257 const uint32_t num_threads = m_threads.size();
258 for (uint32_t idx = 0; idx < num_threads; ++idx)
259 {
260 if (m_threads[idx]->GetIndexID() == index_id)
261 {
262 thread_sp = m_threads[idx];
263 break;
264 }
265 }
266 return thread_sp;
267}
268
269bool
270ThreadList::ShouldStop (Event *event_ptr)
271{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 // Running events should never stop, obviously...
273
Greg Clayton5160ce52013-03-27 23:08:40 +0000274 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
Jim Inghamb42f3af2012-11-15 22:44:04 +0000276 // The ShouldStop method of the threads can do a whole lot of work,
Jim Ingham35878c42014-04-08 21:33:21 +0000277 // figuring out whether the thread plan conditions are met. So we don't want
Jim Inghamb42f3af2012-11-15 22:44:04 +0000278 // to keep the ThreadList locked the whole time we are doing this.
279 // FIXME: It is possible that running code could cause new threads
Adrian McCarthy2f8e4c32015-05-18 23:24:32 +0000280 // to be created. If that happens, we will miss asking them whether
281 // they should stop. This is not a big deal since we haven't had
Jim Inghamb42f3af2012-11-15 22:44:04 +0000282 // a chance to hang any interesting operations on those threads yet.
283
284 collection threads_copy;
285 {
286 // Scope for locker
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000287 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288
Jim Inghamb42f3af2012-11-15 22:44:04 +0000289 m_process->UpdateThreadListIfNeeded();
Jim Ingham569aaf92015-11-06 22:45:57 +0000290 for (lldb::ThreadSP thread_sp : m_threads)
291 {
292 // This is an optimization... If we didn't let a thread run in between the previous stop and this
293 // one, we shouldn't have to consult it for ShouldStop. So just leave it off the list we are going to
294 // inspect.
Chaoren Lin6e8fbc62015-11-07 02:16:31 +0000295 // On Linux, if a thread-specific conditional breakpoint was hit, it won't necessarily be the thread
296 // that hit the breakpoint itself that evaluates the conditional expression, so the thread that hit
297 // the breakpoint could still be asked to stop, even though it hasn't been allowed to run since the
298 // previous stop.
299 if (thread_sp->GetTemporaryResumeState () != eStateSuspended || thread_sp->IsStillAtLastBreakpointHit())
Jim Ingham569aaf92015-11-06 22:45:57 +0000300 threads_copy.push_back(thread_sp);
301 }
302
303 // It is possible the threads we were allowing to run all exited and then maybe the user interrupted
304 // or something, then fall back on looking at all threads:
305
306 if (threads_copy.size() == 0)
307 threads_copy = m_threads;
Jim Inghamb42f3af2012-11-15 22:44:04 +0000308 }
309
310 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311
Greg Clayton2cad65a2010-09-03 17:10:42 +0000312 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000313 {
314 log->PutCString("");
Jim Ingham569aaf92015-11-06 22:45:57 +0000315 log->Printf ("ThreadList::%s: %" PRIu64 " threads, %" PRIu64 " unsuspended threads",
316 __FUNCTION__,
317 (uint64_t)m_threads.size(),
318 (uint64_t)threads_copy.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000319 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000320
Jim Inghama0079042013-03-21 21:46:56 +0000321 bool did_anybody_stop_for_a_reason = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000322
323 // 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 +0000324 bool should_stop = false;
Jim Ingham35878c42014-04-08 21:33:21 +0000325 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
326 {
327 if (log)
328 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
329
330 should_stop = true;
331 }
Jim Ingham7bc34652013-04-16 22:53:24 +0000332
333 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
334 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
335 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
336 // We don't need to use it here, we just want to make sure it gets computed.
337
338 for (pos = threads_copy.begin(); pos != end; ++pos)
339 {
340 ThreadSP thread_sp(*pos);
341 thread_sp->GetStopInfo();
342 }
Jim Inghama0079042013-03-21 21:46:56 +0000343
Jim Inghamb42f3af2012-11-15 22:44:04 +0000344 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 {
346 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000347
Jim Ingham39fdae72014-01-15 03:32:42 +0000348 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
349 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
350 // the right thing to do and stopping gives the user control over what to do in this instance.
351 //
352 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
353 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
354 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
355 // code and causes us to stop seemingly for no reason.
356 //
357 // 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
358 // to true unless this is the first stop.
359 //
360 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
361 // everywhere but at this check.
362
Ed Maste21afbe02014-01-17 17:31:06 +0000363 if (thread_sp->GetProcess()->GetStopID() > 1)
Jim Ingham39fdae72014-01-15 03:32:42 +0000364 did_anybody_stop_for_a_reason = true;
365 else
366 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
Jim Inghama0079042013-03-21 21:46:56 +0000367
Jim Ingham10c4b242011-10-15 00:23:43 +0000368 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000369 if (thread_should_stop)
370 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000372
Jim Inghama0079042013-03-21 21:46:56 +0000373 if (!should_stop && !did_anybody_stop_for_a_reason)
374 {
375 should_stop = true;
376 if (log)
377 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
378 }
379
Greg Clayton2cad65a2010-09-03 17:10:42 +0000380 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000381 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000382
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 if (should_stop)
384 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000385 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 {
387 ThreadSP thread_sp(*pos);
388 thread_sp->WillStop ();
389 }
390 }
391
392 return should_stop;
393}
394
395Vote
396ThreadList::ShouldReportStop (Event *event_ptr)
397{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000398 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000399
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 Vote result = eVoteNoOpinion;
401 m_process->UpdateThreadListIfNeeded();
402 collection::iterator pos, end = m_threads.end();
403
Greg Clayton5160ce52013-03-27 23:08:40 +0000404 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000405
406 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000407 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 // Run through the threads and ask whether we should report this event.
410 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
411 for (pos = m_threads.begin(); pos != end; ++pos)
412 {
413 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000414 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
415 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 {
Jim Ingham92087d82012-01-31 23:09:20 +0000417 case eVoteNoOpinion:
418 continue;
419
420 case eVoteYes:
421 result = eVoteYes;
422 break;
423
424 case eVoteNo:
425 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 {
Jim Ingham92087d82012-01-31 23:09:20 +0000427 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 }
Jim Ingham92087d82012-01-31 23:09:20 +0000429 else
430 {
431 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000432 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 +0000433 __FUNCTION__,
434 thread_sp->GetID (),
435 GetVoteAsCString (vote),
436 GetVoteAsCString (result));
437 }
438 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 }
440 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000441 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000442 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443 return result;
444}
445
Jim Ingham221d51c2013-05-08 00:35:16 +0000446void
447ThreadList::SetShouldReportStop (Vote vote)
448{
449 Mutex::Locker locker(GetMutex());
450 m_process->UpdateThreadListIfNeeded();
451 collection::iterator pos, end = m_threads.end();
452 for (pos = m_threads.begin(); pos != end; ++pos)
453 {
454 ThreadSP thread_sp(*pos);
455 thread_sp->SetShouldReportStop (vote);
456 }
457}
458
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459Vote
460ThreadList::ShouldReportRun (Event *event_ptr)
461{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000462
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000463 Mutex::Locker locker(GetMutex());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000464
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 Vote result = eVoteNoOpinion;
466 m_process->UpdateThreadListIfNeeded();
467 collection::iterator pos, end = m_threads.end();
468
469 // Run through the threads and ask whether we should report this event.
470 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
471
Greg Clayton5160ce52013-03-27 23:08:40 +0000472 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 for (pos = m_threads.begin(); pos != end; ++pos)
475 {
Jim Inghamce579832011-01-24 04:11:25 +0000476 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 {
Jim Inghamce579832011-01-24 04:11:25 +0000478 switch ((*pos)->ShouldReportRun (event_ptr))
479 {
480 case eVoteNoOpinion:
481 continue;
482 case eVoteYes:
483 if (result == eVoteNoOpinion)
484 result = eVoteYes;
485 break;
486 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000487 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000488 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000489 (*pos)->GetIndexID(),
490 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000491 result = eVoteNo;
492 break;
493 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 }
495 }
496 return result;
497}
498
499void
500ThreadList::Clear()
501{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000502 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 m_stop_id = 0;
504 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000505 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506}
507
508void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000509ThreadList::Destroy()
510{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000511 Mutex::Locker locker(GetMutex());
Greg Claytone1cd1be2012-01-29 20:56:30 +0000512 const uint32_t num_threads = m_threads.size();
513 for (uint32_t idx = 0; idx < num_threads; ++idx)
514 {
515 m_threads[idx]->DestroyThread();
516 }
517}
518
519void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520ThreadList::RefreshStateAfterStop ()
521{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000522 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523
524 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000525
Greg Clayton5160ce52013-03-27 23:08:40 +0000526 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000527 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000528 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529
530 collection::iterator pos, end = m_threads.end();
531 for (pos = m_threads.begin(); pos != end; ++pos)
532 (*pos)->RefreshStateAfterStop ();
533}
534
535void
536ThreadList::DiscardThreadPlans ()
537{
538 // You don't need to update the thread list here, because only threads
539 // that you currently know about have any thread plans.
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000540 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541
542 collection::iterator pos, end = m_threads.end();
543 for (pos = m_threads.begin(); pos != end; ++pos)
544 (*pos)->DiscardThreadPlans (true);
545
546}
547
548bool
549ThreadList::WillResume ()
550{
551 // Run through the threads and perform their momentary actions.
552 // But we only do this for threads that are running, user suspended
553 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000555 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 m_process->UpdateThreadListIfNeeded();
557
558 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559
Jim Inghama3241c12010-07-14 02:27:20 +0000560 // See if any thread wants to run stopping others. If it does, then we won't
561 // setup the other threads for resume, since they aren't going to get a chance
562 // to run. This is necessary because the SetupForResume might add "StopOthers"
563 // plans which would then get to be part of the who-gets-to-run negotiation, but
564 // they're coming in after the fact, and the threads that are already set up should
565 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566
Jim Inghama3241c12010-07-14 02:27:20 +0000567 bool wants_solo_run = false;
568
569 for (pos = m_threads.begin(); pos != end; ++pos)
570 {
Zachary Turner8f186f82015-11-13 21:53:03 +0000571 lldbassert((*pos)->GetCurrentPlan() && "thread should not have null thread plan");
572 if ((*pos)->GetResumeState() != eStateSuspended &&
Jim Inghama3241c12010-07-14 02:27:20 +0000573 (*pos)->GetCurrentPlan()->StopOthers())
574 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000575 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
576 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000577 wants_solo_run = true;
578 break;
579 }
580 }
581
Jim Ingham1c823b42011-01-22 01:33:44 +0000582 if (wants_solo_run)
583 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000584 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000585 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000586 log->Printf ("Turning on notification of new threads while single stepping a thread.");
587 m_process->StartNoticingNewThreads();
588 }
589 else
590 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000591 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000592 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000593 log->Printf ("Turning off notification of new threads while single stepping a thread.");
594 m_process->StopNoticingNewThreads();
595 }
Jim Inghama3241c12010-07-14 02:27:20 +0000596
597 // Give all the threads that are likely to run a last chance to set up their state before we
598 // negotiate who is actually going to get a chance to run...
599 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
600 // call setup on the threads that request StopOthers...
601
602 for (pos = m_threads.begin(); pos != end; ++pos)
603 {
604 if ((*pos)->GetResumeState() != eStateSuspended
605 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
606 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000607 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
608 continue;
Jim Inghama3241c12010-07-14 02:27:20 +0000609 (*pos)->SetupForResume ();
610 }
611 }
612
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 // Now go through the threads and see if any thread wants to run just itself.
614 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000615
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 ThreadList run_me_only_list (m_process);
617
618 run_me_only_list.SetStopID(m_process->GetStopID());
619
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 bool run_only_current_thread = false;
621
622 for (pos = m_threads.begin(); pos != end; ++pos)
623 {
624 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000625 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 thread_sp->GetCurrentPlan()->StopOthers())
627 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000628 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
629 continue;
630
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 // You can't say "stop others" and also want yourself to be suspended.
632 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
633
Jim Ingham2976d002010-08-26 21:32:51 +0000634 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 {
Jim Inghamacbea8f2015-06-02 20:26:13 +0000636 // If the currently selected thread wants to run on its own, always let it.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 run_only_current_thread = true;
638 run_me_only_list.Clear();
639 run_me_only_list.AddThread (thread_sp);
640 break;
641 }
642
643 run_me_only_list.AddThread (thread_sp);
644 }
645
646 }
647
Jim Ingham513c6bb2012-09-01 01:02:41 +0000648 bool need_to_resume = true;
649
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000650 if (run_me_only_list.GetSize (false) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 {
652 // Everybody runs as they wish:
653 for (pos = m_threads.begin(); pos != end; ++pos)
654 {
655 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000656 StateType run_state;
657 if (thread_sp->GetResumeState() != eStateSuspended)
658 run_state = thread_sp->GetCurrentPlan()->RunState();
659 else
660 run_state = eStateSuspended;
Greg Clayton160c9d82013-05-01 21:54:04 +0000661 if (!thread_sp->ShouldResume(run_state))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000662 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 }
664 }
665 else
666 {
667 ThreadSP thread_to_run;
668
669 if (run_only_current_thread)
670 {
Jim Ingham2976d002010-08-26 21:32:51 +0000671 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 }
673 else if (run_me_only_list.GetSize (false) == 1)
674 {
675 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
676 }
677 else
678 {
679 int random_thread = (int)
680 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
681 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
682 }
683
684 for (pos = m_threads.begin(); pos != end; ++pos)
685 {
686 ThreadSP thread_sp(*pos);
687 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000688 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000689 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
Jim Ingham513c6bb2012-09-01 01:02:41 +0000690 need_to_resume = false;
691 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000693 thread_sp->ShouldResume (eStateSuspended);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 }
695 }
696
Jim Ingham513c6bb2012-09-01 01:02:41 +0000697 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698}
699
700void
701ThreadList::DidResume ()
702{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000703 Mutex::Locker locker(GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 collection::iterator pos, end = m_threads.end();
705 for (pos = m_threads.begin(); pos != end; ++pos)
706 {
707 // Don't clear out threads that aren't going to get a chance to run, rather
708 // leave their state for the next time around.
709 ThreadSP thread_sp(*pos);
710 if (thread_sp->GetResumeState() != eStateSuspended)
711 thread_sp->DidResume ();
712 }
713}
714
Andrew Kaylor29d65742013-05-10 17:19:04 +0000715void
716ThreadList::DidStop ()
717{
718 Mutex::Locker locker(GetMutex());
719 collection::iterator pos, end = m_threads.end();
720 for (pos = m_threads.begin(); pos != end; ++pos)
721 {
722 // Notify threads that the process just stopped.
723 // Note, this currently assumes that all threads in the list
724 // stop when the process stops. In the future we will want to support
725 // a debugging model where some threads continue to run while others
726 // are stopped. We either need to handle that somehow here or
727 // create a special thread list containing only threads which will
728 // stop in the code that calls this method (currently
729 // Process::SetPrivateState).
730 ThreadSP thread_sp(*pos);
731 if (StateIsRunningState(thread_sp->GetState()))
732 thread_sp->DidStop ();
733 }
734}
735
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000737ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000739 Mutex::Locker locker(GetMutex());
Johnny Chen943ddb72011-08-25 21:59:59 +0000740 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
741 if (!thread_sp.get())
742 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000743 if (m_threads.size() == 0)
744 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000745 m_selected_tid = m_threads[0]->GetID();
746 thread_sp = m_threads[0];
747 }
748 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749}
750
751bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000752ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000754 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000755 ThreadSP selected_thread_sp(FindThreadByID(tid));
756 if (selected_thread_sp)
757 {
Jim Ingham2976d002010-08-26 21:32:51 +0000758 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000759 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
760 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 else
Jim Ingham2976d002010-08-26 21:32:51 +0000762 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763
Jim Inghamc3faa192012-12-11 02:31:48 +0000764 if (notify)
765 NotifySelectedThreadChanged(m_selected_tid);
766
Jim Ingham2976d002010-08-26 21:32:51 +0000767 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768}
769
770bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000771ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000773 Mutex::Locker locker(GetMutex());
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000774 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
775 if (selected_thread_sp.get())
776 {
777 m_selected_tid = selected_thread_sp->GetID();
778 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
779 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 else
Jim Ingham2976d002010-08-26 21:32:51 +0000781 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
Jim Inghamc3faa192012-12-11 02:31:48 +0000783 if (notify)
784 NotifySelectedThreadChanged(m_selected_tid);
785
Jim Ingham2976d002010-08-26 21:32:51 +0000786 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787}
788
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000789void
Jim Inghamc3faa192012-12-11 02:31:48 +0000790ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
791{
792 ThreadSP selected_thread_sp (FindThreadByID(tid));
793 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
794 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
795 new Thread::ThreadEventData(selected_thread_sp));
796}
797
798void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000799ThreadList::Update (ThreadList &rhs)
800{
801 if (this != &rhs)
802 {
803 // Lock both mutexes to make sure neither side changes anyone on us
Bruce Mitchenere171da52015-07-22 00:16:02 +0000804 // while the assignment occurs
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000805 Mutex::Locker locker(GetMutex());
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000806 m_process = rhs.m_process;
807 m_stop_id = rhs.m_stop_id;
808 m_threads.swap(rhs.m_threads);
809 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000810
811
812 // Now we look for threads that we are done with and
813 // make sure to clear them up as much as possible so
814 // anyone with a shared pointer will still have a reference,
815 // but the thread won't be of much use. Using std::weak_ptr
816 // for all backward references (such as a thread to a process)
817 // will eventually solve this issue for us, but for now, we
818 // need to work around the issue
819 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
820 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
821 {
822 const lldb::tid_t tid = (*rhs_pos)->GetID();
823 bool thread_is_alive = false;
824 const uint32_t num_threads = m_threads.size();
825 for (uint32_t idx = 0; idx < num_threads; ++idx)
826 {
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000827 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
828 if (m_threads[idx]->GetID() == tid || (backing_thread && backing_thread->GetID() == tid))
Greg Claytone1cd1be2012-01-29 20:56:30 +0000829 {
830 thread_is_alive = true;
831 break;
832 }
833 }
834 if (!thread_is_alive)
835 (*rhs_pos)->DestroyThread();
836 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000837 }
838}
839
Greg Claytonfa559e52012-05-18 02:38:05 +0000840void
841ThreadList::Flush ()
842{
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000843 Mutex::Locker locker(GetMutex());
Greg Claytonfa559e52012-05-18 02:38:05 +0000844 collection::iterator pos, end = m_threads.end();
845 for (pos = m_threads.begin(); pos != end; ++pos)
846 (*pos)->Flush ();
847}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000848
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000849Mutex &
850ThreadList::GetMutex ()
851{
852 return m_process->m_thread_mutex;
853}
854
Jim Ingham8d94ba02016-03-12 02:45:34 +0000855ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher (lldb::ThreadSP thread_sp) :
856 m_thread_list(nullptr),
857 m_tid(LLDB_INVALID_THREAD_ID)
858{
859 if (thread_sp)
860 {
861 m_tid = thread_sp->GetID();
862 m_thread_list = &thread_sp->GetProcess()->GetThreadList();
863 m_thread_list->PushExpressionExecutionThread(m_tid);
864 }
865}
866
867