blob: 1c1cf3e393a62b5474c6856cc6e4c8219f673ee4 [file] [log] [blame]
Chris Lattner24943d22010-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 Clayton5205f0b2010-09-03 17:10:42 +000013#include "lldb/Core/Log.h"
14#include "lldb/Target/RegisterContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Target/ThreadList.h"
16#include "lldb/Target/Thread.h"
17#include "lldb/Target/ThreadPlan.h"
18#include "lldb/Target/Process.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
23ThreadList::ThreadList (Process *process) :
24 m_process (process),
25 m_stop_id (0),
26 m_threads(),
27 m_threads_mutex (Mutex::eMutexTypeRecursive),
Jim Inghamc8332952010-08-26 21:32:51 +000028 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner24943d22010-06-08 16:52:24 +000029{
30}
31
32ThreadList::ThreadList (const ThreadList &rhs) :
33 m_process (),
34 m_stop_id (),
35 m_threads (),
36 m_threads_mutex (Mutex::eMutexTypeRecursive),
Jim Inghamc8332952010-08-26 21:32:51 +000037 m_selected_tid ()
Chris Lattner24943d22010-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
Greg Claytonbef15832010-07-14 00:18:15 +000050 Mutex::Locker locker_lhs(m_threads_mutex);
Chris Lattner24943d22010-06-08 16:52:24 +000051 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
52 m_process = rhs.m_process;
53 m_stop_id = rhs.m_stop_id;
54 m_threads = rhs.m_threads;
Jim Inghamc8332952010-08-26 21:32:51 +000055 m_selected_tid = rhs.m_selected_tid;
Chris Lattner24943d22010-06-08 16:52:24 +000056 }
57 return *this;
58}
59
60
61ThreadList::~ThreadList()
62{
63}
64
65
66uint32_t
67ThreadList::GetStopID () const
68{
69 return m_stop_id;
70}
71
72void
73ThreadList::SetStopID (uint32_t stop_id)
74{
75 m_stop_id = stop_id;
76}
77
78
79void
80ThreadList::AddThread (ThreadSP &thread_sp)
81{
82 Mutex::Locker locker(m_threads_mutex);
83 m_threads.push_back(thread_sp);
84}
85
86uint32_t
87ThreadList::GetSize (bool can_update)
88{
89 Mutex::Locker locker(m_threads_mutex);
90 if (can_update)
91 m_process->UpdateThreadListIfNeeded();
92 return m_threads.size();
93}
94
95ThreadSP
96ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
97{
98 Mutex::Locker locker(m_threads_mutex);
99 if (can_update)
100 m_process->UpdateThreadListIfNeeded();
101
102 ThreadSP thread_sp;
103 if (idx < m_threads.size())
104 thread_sp = m_threads[idx];
105 return thread_sp;
106}
107
108ThreadSP
109ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
110{
111 Mutex::Locker locker(m_threads_mutex);
112
113 if (can_update)
114 m_process->UpdateThreadListIfNeeded();
115
116 ThreadSP thread_sp;
117 uint32_t idx = 0;
118 const uint32_t num_threads = m_threads.size();
119 for (idx = 0; idx < num_threads; ++idx)
120 {
121 if (m_threads[idx]->GetID() == tid)
122 {
123 thread_sp = m_threads[idx];
124 break;
125 }
126 }
127 return thread_sp;
128}
129
130ThreadSP
131ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
132{
133 ThreadSP thread_sp;
134 if (thread_ptr)
135 {
136 Mutex::Locker locker(m_threads_mutex);
137
138 uint32_t idx = 0;
139 const uint32_t num_threads = m_threads.size();
140 for (idx = 0; idx < num_threads; ++idx)
141 {
142 if (m_threads[idx].get() == thread_ptr)
143 {
144 thread_sp = m_threads[idx];
145 break;
146 }
147 }
148 }
149 return thread_sp;
150}
151
152
153
154ThreadSP
155ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
156{
157 Mutex::Locker locker(m_threads_mutex);
158
159 if (can_update)
160 m_process->UpdateThreadListIfNeeded();
161
162 ThreadSP thread_sp;
163 const uint32_t num_threads = m_threads.size();
164 for (uint32_t idx = 0; idx < num_threads; ++idx)
165 {
166 if (m_threads[idx]->GetIndexID() == index_id)
167 {
168 thread_sp = m_threads[idx];
169 break;
170 }
171 }
172 return thread_sp;
173}
174
175bool
176ThreadList::ShouldStop (Event *event_ptr)
177{
178 Mutex::Locker locker(m_threads_mutex);
179
180 // Running events should never stop, obviously...
181
Greg Clayton5205f0b2010-09-03 17:10:42 +0000182 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
Chris Lattner24943d22010-06-08 16:52:24 +0000183
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000184 bool should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000185 m_process->UpdateThreadListIfNeeded();
186
187 collection::iterator pos, end = m_threads.end();
188
Greg Clayton5205f0b2010-09-03 17:10:42 +0000189 if (log)
190 log->Printf ("%s %zu threads\n", __FUNCTION__, m_threads.size());
191
Chris Lattner24943d22010-06-08 16:52:24 +0000192 // Run through the threads and ask whether we should stop. Don't ask
193 // suspended threads, however, it makes more sense for them to preserve their
194 // state across the times the process runs but they don't get a chance to.
195 for (pos = m_threads.begin(); pos != end; ++pos)
196 {
197 ThreadSP thread_sp(*pos);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000198
199 if (log)
200 log->Printf ("%s thread 0x%4.4x: pc = 0x%16.16llx ", __FUNCTION__, thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC());
201
202 if (thread_sp->GetResumeState () == eStateSuspended)
Chris Lattner24943d22010-06-08 16:52:24 +0000203 {
Greg Clayton5205f0b2010-09-03 17:10:42 +0000204 if (log)
205 log->Printf("ignore: thread was suspended\n", thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC());
206 continue;
Chris Lattner24943d22010-06-08 16:52:24 +0000207 }
Greg Clayton5205f0b2010-09-03 17:10:42 +0000208
209 if (thread_sp->ThreadStoppedForAReason() == false)
210 {
211 if (log)
212 log->Printf("ignore: no stop reason\n", thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC());
213 continue;
214
215 }
216
217 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
218 if (log)
219 log->Printf("should_stop = %i\n", thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC(), thread_should_stop);
220 if (thread_should_stop)
221 should_stop |= true;
Chris Lattner24943d22010-06-08 16:52:24 +0000222 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000223
Greg Clayton5205f0b2010-09-03 17:10:42 +0000224 if (log)
225 log->Printf ("%s overall should_stop = %i\n", __FUNCTION__, should_stop);
226
Chris Lattner24943d22010-06-08 16:52:24 +0000227 if (should_stop)
228 {
229 for (pos = m_threads.begin(); pos != end; ++pos)
230 {
231 ThreadSP thread_sp(*pos);
232 thread_sp->WillStop ();
233 }
234 }
235
236 return should_stop;
237}
238
239Vote
240ThreadList::ShouldReportStop (Event *event_ptr)
241{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000242 Mutex::Locker locker(m_threads_mutex);
243
Chris Lattner24943d22010-06-08 16:52:24 +0000244 Vote result = eVoteNoOpinion;
245 m_process->UpdateThreadListIfNeeded();
246 collection::iterator pos, end = m_threads.end();
247
Greg Clayton5205f0b2010-09-03 17:10:42 +0000248 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
249
250 if (log)
251 log->Printf ("%s %zu threads\n", __FUNCTION__, m_threads.size());
252
Chris Lattner24943d22010-06-08 16:52:24 +0000253 // Run through the threads and ask whether we should report this event.
254 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
255 for (pos = m_threads.begin(); pos != end; ++pos)
256 {
257 ThreadSP thread_sp(*pos);
258 if (thread_sp->ThreadStoppedForAReason() && (thread_sp->GetResumeState () != eStateSuspended))
259 {
Greg Clayton5205f0b2010-09-03 17:10:42 +0000260 const lldb::Vote vote = thread_sp->ShouldReportStop (event_ptr);
261 if (log)
262 log->Printf ("%s thread 0x%4.4x: pc = 0x%16.16llx vote: %s\n",
263 __FUNCTION__,
264 thread_sp->GetID (),
265 thread_sp->GetRegisterContext()->GetPC(),
266 GetVoteAsCString (vote));
267 switch (vote)
Chris Lattner24943d22010-06-08 16:52:24 +0000268 {
Greg Clayton5205f0b2010-09-03 17:10:42 +0000269 case eVoteNoOpinion:
270 continue;
271
272 case eVoteYes:
273 result = eVoteYes;
274 break;
275
276 case eVoteNo:
277 if (result == eVoteNoOpinion)
278 {
279 result = eVoteNo;
280 }
281 else
282 {
283 if (log)
284 log->Printf ("%s thread 0x%4.4x: pc = 0x%16.16llx voted %s, but lost out because result was %s\n",
285 __FUNCTION__,
286 thread_sp->GetID (),
287 thread_sp->GetRegisterContext()->GetPC(),
288 GetVoteAsCString (vote),
289 GetVoteAsCString (result));
290 }
291 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000292 }
293 }
294 }
Greg Clayton5205f0b2010-09-03 17:10:42 +0000295 if (log)
296 log->Printf ("%s returning %s\n", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner24943d22010-06-08 16:52:24 +0000297 return result;
298}
299
300Vote
301ThreadList::ShouldReportRun (Event *event_ptr)
302{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000303
304 Mutex::Locker locker(m_threads_mutex);
305
Chris Lattner24943d22010-06-08 16:52:24 +0000306 Vote result = eVoteNoOpinion;
307 m_process->UpdateThreadListIfNeeded();
308 collection::iterator pos, end = m_threads.end();
309
310 // Run through the threads and ask whether we should report this event.
311 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
312
313 for (pos = m_threads.begin(); pos != end; ++pos)
314 {
315 ThreadSP thread_sp(*pos);
316 if (thread_sp->GetResumeState () != eStateSuspended)
317
318 switch (thread_sp->ShouldReportRun (event_ptr))
319 {
320 case eVoteNoOpinion:
321 continue;
322 case eVoteYes:
323 if (result == eVoteNoOpinion)
324 result = eVoteYes;
325 break;
326 case eVoteNo:
327 result = eVoteNo;
328 break;
329 }
330 }
331 return result;
332}
333
334void
335ThreadList::Clear()
336{
337 m_stop_id = 0;
338 m_threads.clear();
Jim Inghamc8332952010-08-26 21:32:51 +0000339 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000340}
341
342void
343ThreadList::RefreshStateAfterStop ()
344{
345 Mutex::Locker locker(m_threads_mutex);
346
347 m_process->UpdateThreadListIfNeeded();
348
349 collection::iterator pos, end = m_threads.end();
350 for (pos = m_threads.begin(); pos != end; ++pos)
351 (*pos)->RefreshStateAfterStop ();
352}
353
354void
355ThreadList::DiscardThreadPlans ()
356{
357 // You don't need to update the thread list here, because only threads
358 // that you currently know about have any thread plans.
359 Mutex::Locker locker(m_threads_mutex);
360
361 collection::iterator pos, end = m_threads.end();
362 for (pos = m_threads.begin(); pos != end; ++pos)
363 (*pos)->DiscardThreadPlans (true);
364
365}
366
367bool
368ThreadList::WillResume ()
369{
370 // Run through the threads and perform their momentary actions.
371 // But we only do this for threads that are running, user suspended
372 // threads stay where they are.
373 bool success = true;
374
375 Mutex::Locker locker(m_threads_mutex);
376 m_process->UpdateThreadListIfNeeded();
377
378 collection::iterator pos, end = m_threads.end();
Chris Lattner24943d22010-06-08 16:52:24 +0000379
Jim Inghama99afce2010-07-14 02:27:20 +0000380 // See if any thread wants to run stopping others. If it does, then we won't
381 // setup the other threads for resume, since they aren't going to get a chance
382 // to run. This is necessary because the SetupForResume might add "StopOthers"
383 // plans which would then get to be part of the who-gets-to-run negotiation, but
384 // they're coming in after the fact, and the threads that are already set up should
385 // take priority.
Chris Lattner24943d22010-06-08 16:52:24 +0000386
Jim Inghama99afce2010-07-14 02:27:20 +0000387 bool wants_solo_run = false;
388
389 for (pos = m_threads.begin(); pos != end; ++pos)
390 {
391 if ((*pos)->GetResumeState() != eStateSuspended &&
392 (*pos)->GetCurrentPlan()->StopOthers())
393 {
394 wants_solo_run = true;
395 break;
396 }
397 }
398
399
400 // Give all the threads that are likely to run a last chance to set up their state before we
401 // negotiate who is actually going to get a chance to run...
402 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
403 // call setup on the threads that request StopOthers...
404
405 for (pos = m_threads.begin(); pos != end; ++pos)
406 {
407 if ((*pos)->GetResumeState() != eStateSuspended
408 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
409 {
410 (*pos)->SetupForResume ();
411 }
412 }
413
Chris Lattner24943d22010-06-08 16:52:24 +0000414 // Now go through the threads and see if any thread wants to run just itself.
415 // if so then pick one and run it.
Jim Inghama99afce2010-07-14 02:27:20 +0000416
Chris Lattner24943d22010-06-08 16:52:24 +0000417 ThreadList run_me_only_list (m_process);
418
419 run_me_only_list.SetStopID(m_process->GetStopID());
420
421 ThreadSP immediate_thread_sp;
422 bool run_only_current_thread = false;
423
424 for (pos = m_threads.begin(); pos != end; ++pos)
425 {
426 ThreadSP thread_sp(*pos);
427 if (thread_sp->GetCurrentPlan()->IsImmediate())
428 {
429 // We first do all the immediate plans, so if we find one, set
430 // immediate_thread_sp and break out, and we'll pick it up first thing
431 // when we're negotiating which threads get to run.
432 immediate_thread_sp = thread_sp;
433 break;
434 }
435 else if (thread_sp->GetResumeState() != eStateSuspended &&
436 thread_sp->GetCurrentPlan()->StopOthers())
437 {
438 // You can't say "stop others" and also want yourself to be suspended.
439 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
440
Jim Inghamc8332952010-08-26 21:32:51 +0000441 if (thread_sp == GetSelectedThread())
Chris Lattner24943d22010-06-08 16:52:24 +0000442 {
443 run_only_current_thread = true;
444 run_me_only_list.Clear();
445 run_me_only_list.AddThread (thread_sp);
446 break;
447 }
448
449 run_me_only_list.AddThread (thread_sp);
450 }
451
452 }
453
454 if (immediate_thread_sp)
455 {
456 for (pos = m_threads.begin(); pos != end; ++pos)
457 {
458 ThreadSP thread_sp(*pos);
459 if (thread_sp.get() == immediate_thread_sp.get())
460 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
461 else
462 thread_sp->WillResume (eStateSuspended);
463 }
464 }
465 else if (run_me_only_list.GetSize (false) == 0)
466 {
467 // Everybody runs as they wish:
468 for (pos = m_threads.begin(); pos != end; ++pos)
469 {
470 ThreadSP thread_sp(*pos);
471 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
472 }
473 }
474 else
475 {
476 ThreadSP thread_to_run;
477
478 if (run_only_current_thread)
479 {
Jim Inghamc8332952010-08-26 21:32:51 +0000480 thread_to_run = GetSelectedThread();
Chris Lattner24943d22010-06-08 16:52:24 +0000481 }
482 else if (run_me_only_list.GetSize (false) == 1)
483 {
484 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
485 }
486 else
487 {
488 int random_thread = (int)
489 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
490 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
491 }
492
493 for (pos = m_threads.begin(); pos != end; ++pos)
494 {
495 ThreadSP thread_sp(*pos);
496 if (thread_sp == thread_to_run)
497 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
498 else
499 thread_sp->WillResume (eStateSuspended);
500 }
501 }
502
503 return success;
504}
505
506void
507ThreadList::DidResume ()
508{
509 collection::iterator pos, end = m_threads.end();
510 for (pos = m_threads.begin(); pos != end; ++pos)
511 {
512 // Don't clear out threads that aren't going to get a chance to run, rather
513 // leave their state for the next time around.
514 ThreadSP thread_sp(*pos);
515 if (thread_sp->GetResumeState() != eStateSuspended)
516 thread_sp->DidResume ();
517 }
518}
519
520ThreadSP
Jim Inghamc8332952010-08-26 21:32:51 +0000521ThreadList::GetSelectedThread ()
Chris Lattner24943d22010-06-08 16:52:24 +0000522{
523 Mutex::Locker locker(m_threads_mutex);
Jim Inghamc8332952010-08-26 21:32:51 +0000524 return FindThreadByID(m_selected_tid);
Chris Lattner24943d22010-06-08 16:52:24 +0000525}
526
527bool
Jim Inghamc8332952010-08-26 21:32:51 +0000528ThreadList::SetSelectedThreadByID (lldb::tid_t tid)
Chris Lattner24943d22010-06-08 16:52:24 +0000529{
530 Mutex::Locker locker(m_threads_mutex);
531 if (FindThreadByID(tid).get())
Jim Inghamc8332952010-08-26 21:32:51 +0000532 m_selected_tid = tid;
Chris Lattner24943d22010-06-08 16:52:24 +0000533 else
Jim Inghamc8332952010-08-26 21:32:51 +0000534 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000535
Jim Inghamc8332952010-08-26 21:32:51 +0000536 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000537}
538
539bool
Jim Inghamc8332952010-08-26 21:32:51 +0000540ThreadList::SetSelectedThreadByIndexID (uint32_t index_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000541{
542 Mutex::Locker locker(m_threads_mutex);
543 ThreadSP thread_sp (FindThreadByIndexID(index_id));
544 if (thread_sp.get())
Jim Inghamc8332952010-08-26 21:32:51 +0000545 m_selected_tid = thread_sp->GetID();
Chris Lattner24943d22010-06-08 16:52:24 +0000546 else
Jim Inghamc8332952010-08-26 21:32:51 +0000547 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000548
Jim Inghamc8332952010-08-26 21:32:51 +0000549 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner24943d22010-06-08 16:52:24 +0000550}
551