Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Log.h" |
| 14 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Target/ThreadList.h" |
| 16 | #include "lldb/Target/Thread.h" |
| 17 | #include "lldb/Target/ThreadPlan.h" |
| 18 | #include "lldb/Target/Process.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| 23 | ThreadList::ThreadList (Process *process) : |
| 24 | m_process (process), |
| 25 | m_stop_id (0), |
| 26 | m_threads(), |
| 27 | m_threads_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 28 | m_selected_tid (LLDB_INVALID_THREAD_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
| 32 | ThreadList::ThreadList (const ThreadList &rhs) : |
| 33 | m_process (), |
| 34 | m_stop_id (), |
| 35 | m_threads (), |
| 36 | m_threads_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 37 | m_selected_tid () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
| 39 | // Use the assignment operator since it uses the mutex |
| 40 | *this = rhs; |
| 41 | } |
| 42 | |
| 43 | const ThreadList& |
| 44 | ThreadList::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 Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 50 | Mutex::Locker locker_lhs(m_threads_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | 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 Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 55 | m_selected_tid = rhs.m_selected_tid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | return *this; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | ThreadList::~ThreadList() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | |
| 66 | uint32_t |
| 67 | ThreadList::GetStopID () const |
| 68 | { |
| 69 | return m_stop_id; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | ThreadList::SetStopID (uint32_t stop_id) |
| 74 | { |
| 75 | m_stop_id = stop_id; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 80 | ThreadList::AddThread (const ThreadSP &thread_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | { |
| 82 | Mutex::Locker locker(m_threads_mutex); |
| 83 | m_threads.push_back(thread_sp); |
| 84 | } |
| 85 | |
| 86 | uint32_t |
| 87 | ThreadList::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 | |
| 95 | ThreadSP |
| 96 | ThreadList::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 | |
| 108 | ThreadSP |
| 109 | ThreadList::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 | |
| 130 | ThreadSP |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame^] | 131 | ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update) |
| 132 | { |
| 133 | Mutex::Locker locker(m_threads_mutex); |
| 134 | |
| 135 | if (can_update) |
| 136 | m_process->UpdateThreadListIfNeeded(); |
| 137 | |
| 138 | ThreadSP thread_sp; |
| 139 | uint32_t idx = 0; |
| 140 | const uint32_t num_threads = m_threads.size(); |
| 141 | for (idx = 0; idx < num_threads; ++idx) |
| 142 | { |
| 143 | if (m_threads[idx]->GetID() == tid) |
| 144 | { |
| 145 | thread_sp = m_threads[idx]; |
| 146 | m_threads.erase(m_threads.begin()+idx); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | return thread_sp; |
| 151 | } |
| 152 | |
| 153 | ThreadSP |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr) |
| 155 | { |
| 156 | ThreadSP thread_sp; |
| 157 | if (thread_ptr) |
| 158 | { |
| 159 | Mutex::Locker locker(m_threads_mutex); |
| 160 | |
| 161 | uint32_t idx = 0; |
| 162 | const uint32_t num_threads = m_threads.size(); |
| 163 | for (idx = 0; idx < num_threads; ++idx) |
| 164 | { |
| 165 | if (m_threads[idx].get() == thread_ptr) |
| 166 | { |
| 167 | thread_sp = m_threads[idx]; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return thread_sp; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | ThreadSP |
| 178 | ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update) |
| 179 | { |
| 180 | Mutex::Locker locker(m_threads_mutex); |
| 181 | |
| 182 | if (can_update) |
| 183 | m_process->UpdateThreadListIfNeeded(); |
| 184 | |
| 185 | ThreadSP thread_sp; |
| 186 | const uint32_t num_threads = m_threads.size(); |
| 187 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 188 | { |
| 189 | if (m_threads[idx]->GetIndexID() == index_id) |
| 190 | { |
| 191 | thread_sp = m_threads[idx]; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | return thread_sp; |
| 196 | } |
| 197 | |
| 198 | bool |
| 199 | ThreadList::ShouldStop (Event *event_ptr) |
| 200 | { |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 201 | bool should_stop = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | // Running events should never stop, obviously... |
| 203 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 204 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 206 | // The ShouldStop method of the threads can do a whole lot of work, |
| 207 | // running breakpoint commands & conditions, etc. So we don't want |
| 208 | // to keep the ThreadList locked the whole time we are doing this. |
| 209 | // FIXME: It is possible that running code could cause new threads |
| 210 | // to be created. If that happens we will miss asking them whether |
| 211 | // then should stop. This is not a big deal, since we haven't had |
| 212 | // a chance to hang any interesting operations on those threads yet. |
| 213 | |
| 214 | collection threads_copy; |
| 215 | { |
| 216 | // Scope for locker |
| 217 | Mutex::Locker locker(m_threads_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 219 | m_process->UpdateThreadListIfNeeded(); |
| 220 | threads_copy = m_threads; |
| 221 | } |
| 222 | |
| 223 | collection::iterator pos, end = threads_copy.end(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 225 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 226 | { |
| 227 | log->PutCString(""); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 228 | log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size()); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 229 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 230 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 231 | for (pos = threads_copy.begin(); pos != end; ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | { |
| 233 | ThreadSP thread_sp(*pos); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 234 | |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 235 | const bool thread_should_stop = thread_sp->ShouldStop(event_ptr); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 236 | if (thread_should_stop) |
| 237 | should_stop |= true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | } |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 239 | |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 240 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 241 | log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 243 | if (should_stop) |
| 244 | { |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 245 | for (pos = threads_copy.begin(); pos != end; ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | { |
| 247 | ThreadSP thread_sp(*pos); |
| 248 | thread_sp->WillStop (); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return should_stop; |
| 253 | } |
| 254 | |
| 255 | Vote |
| 256 | ThreadList::ShouldReportStop (Event *event_ptr) |
| 257 | { |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 258 | Mutex::Locker locker(m_threads_mutex); |
| 259 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | Vote result = eVoteNoOpinion; |
| 261 | m_process->UpdateThreadListIfNeeded(); |
| 262 | collection::iterator pos, end = m_threads.end(); |
| 263 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 264 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 265 | |
| 266 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 267 | log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | // Run through the threads and ask whether we should report this event. |
| 270 | // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion. |
| 271 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 272 | { |
| 273 | ThreadSP thread_sp(*pos); |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 274 | const Vote vote = thread_sp->ShouldReportStop (event_ptr); |
| 275 | switch (vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | { |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 277 | case eVoteNoOpinion: |
| 278 | continue; |
| 279 | |
| 280 | case eVoteYes: |
| 281 | result = eVoteYes; |
| 282 | break; |
| 283 | |
| 284 | case eVoteNo: |
| 285 | if (result == eVoteNoOpinion) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 286 | { |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 287 | result = eVoteNo; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | } |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 289 | else |
| 290 | { |
| 291 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 292 | log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s", |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 293 | __FUNCTION__, |
| 294 | thread_sp->GetID (), |
| 295 | GetVoteAsCString (vote), |
| 296 | GetVoteAsCString (result)); |
| 297 | } |
| 298 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 301 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 302 | log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | |
| 306 | Vote |
| 307 | ThreadList::ShouldReportRun (Event *event_ptr) |
| 308 | { |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 309 | |
| 310 | Mutex::Locker locker(m_threads_mutex); |
| 311 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 312 | Vote result = eVoteNoOpinion; |
| 313 | m_process->UpdateThreadListIfNeeded(); |
| 314 | collection::iterator pos, end = m_threads.end(); |
| 315 | |
| 316 | // Run through the threads and ask whether we should report this event. |
| 317 | // The rule is NO vote wins over everything, a YES vote wins over no opinion. |
| 318 | |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 319 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 320 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 322 | { |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 323 | if ((*pos)->GetResumeState () != eStateSuspended) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | { |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 325 | switch ((*pos)->ShouldReportRun (event_ptr)) |
| 326 | { |
| 327 | case eVoteNoOpinion: |
| 328 | continue; |
| 329 | case eVoteYes: |
| 330 | if (result == eVoteNoOpinion) |
| 331 | result = eVoteYes; |
| 332 | break; |
| 333 | case eVoteNo: |
Greg Clayton | abcbc8a | 2011-01-24 05:36:47 +0000 | [diff] [blame] | 334 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 335 | log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.", |
Greg Clayton | abcbc8a | 2011-01-24 05:36:47 +0000 | [diff] [blame] | 336 | (*pos)->GetIndexID(), |
| 337 | (*pos)->GetID()); |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 338 | result = eVoteNo; |
| 339 | break; |
| 340 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | return result; |
| 344 | } |
| 345 | |
| 346 | void |
| 347 | ThreadList::Clear() |
| 348 | { |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 349 | Mutex::Locker locker(m_threads_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | m_stop_id = 0; |
| 351 | m_threads.clear(); |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 352 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 356 | ThreadList::Destroy() |
| 357 | { |
| 358 | Mutex::Locker locker(m_threads_mutex); |
| 359 | const uint32_t num_threads = m_threads.size(); |
| 360 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 361 | { |
| 362 | m_threads[idx]->DestroyThread(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | ThreadList::RefreshStateAfterStop () |
| 368 | { |
| 369 | Mutex::Locker locker(m_threads_mutex); |
| 370 | |
| 371 | m_process->UpdateThreadListIfNeeded(); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 372 | |
| 373 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 374 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 375 | log->Printf ("Turning off notification of new threads while single stepping a thread."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | |
| 377 | collection::iterator pos, end = m_threads.end(); |
| 378 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 379 | (*pos)->RefreshStateAfterStop (); |
| 380 | } |
| 381 | |
| 382 | void |
| 383 | ThreadList::DiscardThreadPlans () |
| 384 | { |
| 385 | // You don't need to update the thread list here, because only threads |
| 386 | // that you currently know about have any thread plans. |
| 387 | Mutex::Locker locker(m_threads_mutex); |
| 388 | |
| 389 | collection::iterator pos, end = m_threads.end(); |
| 390 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 391 | (*pos)->DiscardThreadPlans (true); |
| 392 | |
| 393 | } |
| 394 | |
| 395 | bool |
| 396 | ThreadList::WillResume () |
| 397 | { |
| 398 | // Run through the threads and perform their momentary actions. |
| 399 | // But we only do this for threads that are running, user suspended |
| 400 | // threads stay where they are. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | |
| 402 | Mutex::Locker locker(m_threads_mutex); |
| 403 | m_process->UpdateThreadListIfNeeded(); |
| 404 | |
| 405 | collection::iterator pos, end = m_threads.end(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 406 | |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 407 | // See if any thread wants to run stopping others. If it does, then we won't |
| 408 | // setup the other threads for resume, since they aren't going to get a chance |
| 409 | // to run. This is necessary because the SetupForResume might add "StopOthers" |
| 410 | // plans which would then get to be part of the who-gets-to-run negotiation, but |
| 411 | // they're coming in after the fact, and the threads that are already set up should |
| 412 | // take priority. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 413 | |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 414 | bool wants_solo_run = false; |
| 415 | |
| 416 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 417 | { |
| 418 | if ((*pos)->GetResumeState() != eStateSuspended && |
| 419 | (*pos)->GetCurrentPlan()->StopOthers()) |
| 420 | { |
| 421 | wants_solo_run = true; |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 426 | if (wants_solo_run) |
| 427 | { |
| 428 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 429 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 430 | log->Printf ("Turning on notification of new threads while single stepping a thread."); |
| 431 | m_process->StartNoticingNewThreads(); |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 436 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 437 | log->Printf ("Turning off notification of new threads while single stepping a thread."); |
| 438 | m_process->StopNoticingNewThreads(); |
| 439 | } |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 440 | |
| 441 | // Give all the threads that are likely to run a last chance to set up their state before we |
| 442 | // negotiate who is actually going to get a chance to run... |
| 443 | // Don't set to resume suspended threads, and if any thread wanted to stop others, only |
| 444 | // call setup on the threads that request StopOthers... |
| 445 | |
| 446 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 447 | { |
| 448 | if ((*pos)->GetResumeState() != eStateSuspended |
| 449 | && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) |
| 450 | { |
| 451 | (*pos)->SetupForResume (); |
| 452 | } |
| 453 | } |
| 454 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | // Now go through the threads and see if any thread wants to run just itself. |
| 456 | // if so then pick one and run it. |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 458 | ThreadList run_me_only_list (m_process); |
| 459 | |
| 460 | run_me_only_list.SetStopID(m_process->GetStopID()); |
| 461 | |
| 462 | ThreadSP immediate_thread_sp; |
| 463 | bool run_only_current_thread = false; |
| 464 | |
| 465 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 466 | { |
| 467 | ThreadSP thread_sp(*pos); |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 468 | if (thread_sp->GetResumeState() != eStateSuspended && |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 469 | thread_sp->GetCurrentPlan()->StopOthers()) |
| 470 | { |
| 471 | // You can't say "stop others" and also want yourself to be suspended. |
| 472 | assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended); |
| 473 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 474 | if (thread_sp == GetSelectedThread()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | { |
| 476 | run_only_current_thread = true; |
| 477 | run_me_only_list.Clear(); |
| 478 | run_me_only_list.AddThread (thread_sp); |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | run_me_only_list.AddThread (thread_sp); |
| 483 | } |
| 484 | |
| 485 | } |
| 486 | |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 487 | bool need_to_resume = true; |
| 488 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 489 | if (immediate_thread_sp) |
| 490 | { |
| 491 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 492 | { |
| 493 | ThreadSP thread_sp(*pos); |
| 494 | if (thread_sp.get() == immediate_thread_sp.get()) |
| 495 | thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState()); |
| 496 | else |
| 497 | thread_sp->WillResume (eStateSuspended); |
| 498 | } |
| 499 | } |
| 500 | else if (run_me_only_list.GetSize (false) == 0) |
| 501 | { |
| 502 | // Everybody runs as they wish: |
| 503 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 504 | { |
| 505 | ThreadSP thread_sp(*pos); |
Jim Ingham | cb5d5a5 | 2012-05-31 20:47:56 +0000 | [diff] [blame] | 506 | StateType run_state; |
| 507 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 508 | run_state = thread_sp->GetCurrentPlan()->RunState(); |
| 509 | else |
| 510 | run_state = eStateSuspended; |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 511 | if (!thread_sp->WillResume(run_state)) |
| 512 | need_to_resume = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | ThreadSP thread_to_run; |
| 518 | |
| 519 | if (run_only_current_thread) |
| 520 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 521 | thread_to_run = GetSelectedThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 522 | } |
| 523 | else if (run_me_only_list.GetSize (false) == 1) |
| 524 | { |
| 525 | thread_to_run = run_me_only_list.GetThreadAtIndex (0); |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | int random_thread = (int) |
| 530 | ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0)); |
| 531 | thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread); |
| 532 | } |
| 533 | |
| 534 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 535 | { |
| 536 | ThreadSP thread_sp(*pos); |
| 537 | if (thread_sp == thread_to_run) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 538 | { |
| 539 | if (!thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState())) |
| 540 | need_to_resume = false; |
| 541 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 542 | else |
| 543 | thread_sp->WillResume (eStateSuspended); |
| 544 | } |
| 545 | } |
| 546 | |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 547 | return need_to_resume; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void |
| 551 | ThreadList::DidResume () |
| 552 | { |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 553 | Mutex::Locker locker(m_threads_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 554 | collection::iterator pos, end = m_threads.end(); |
| 555 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 556 | { |
| 557 | // Don't clear out threads that aren't going to get a chance to run, rather |
| 558 | // leave their state for the next time around. |
| 559 | ThreadSP thread_sp(*pos); |
| 560 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 561 | thread_sp->DidResume (); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | ThreadSP |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 566 | ThreadList::GetSelectedThread () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | { |
| 568 | Mutex::Locker locker(m_threads_mutex); |
Johnny Chen | 943ddb7 | 2011-08-25 21:59:59 +0000 | [diff] [blame] | 569 | ThreadSP thread_sp = FindThreadByID(m_selected_tid); |
| 570 | if (!thread_sp.get()) |
| 571 | { |
Jason Molenda | 354b9a6 | 2011-09-13 01:13:16 +0000 | [diff] [blame] | 572 | if (m_threads.size() == 0) |
| 573 | return thread_sp; |
Johnny Chen | 943ddb7 | 2011-08-25 21:59:59 +0000 | [diff] [blame] | 574 | m_selected_tid = m_threads[0]->GetID(); |
| 575 | thread_sp = m_threads[0]; |
| 576 | } |
| 577 | return thread_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | bool |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 581 | ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | { |
| 583 | Mutex::Locker locker(m_threads_mutex); |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 584 | ThreadSP selected_thread_sp(FindThreadByID(tid)); |
| 585 | if (selected_thread_sp) |
| 586 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 587 | m_selected_tid = tid; |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 588 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 589 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | else |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 591 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 592 | |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 593 | if (notify) |
| 594 | NotifySelectedThreadChanged(m_selected_tid); |
| 595 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 596 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | bool |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 600 | ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 601 | { |
| 602 | Mutex::Locker locker(m_threads_mutex); |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 603 | ThreadSP selected_thread_sp (FindThreadByIndexID(index_id)); |
| 604 | if (selected_thread_sp.get()) |
| 605 | { |
| 606 | m_selected_tid = selected_thread_sp->GetID(); |
| 607 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 608 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 609 | else |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 610 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 611 | |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 612 | if (notify) |
| 613 | NotifySelectedThreadChanged(m_selected_tid); |
| 614 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 615 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 618 | void |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 619 | ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid) |
| 620 | { |
| 621 | ThreadSP selected_thread_sp (FindThreadByID(tid)); |
| 622 | if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected)) |
| 623 | selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected, |
| 624 | new Thread::ThreadEventData(selected_thread_sp)); |
| 625 | } |
| 626 | |
| 627 | void |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 628 | ThreadList::Update (ThreadList &rhs) |
| 629 | { |
| 630 | if (this != &rhs) |
| 631 | { |
| 632 | // Lock both mutexes to make sure neither side changes anyone on us |
| 633 | // while the assignement occurs |
| 634 | Mutex::Locker locker_lhs(m_threads_mutex); |
| 635 | Mutex::Locker locker_rhs(rhs.m_threads_mutex); |
| 636 | m_process = rhs.m_process; |
| 637 | m_stop_id = rhs.m_stop_id; |
| 638 | m_threads.swap(rhs.m_threads); |
| 639 | m_selected_tid = rhs.m_selected_tid; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 640 | |
| 641 | |
| 642 | // Now we look for threads that we are done with and |
| 643 | // make sure to clear them up as much as possible so |
| 644 | // anyone with a shared pointer will still have a reference, |
| 645 | // but the thread won't be of much use. Using std::weak_ptr |
| 646 | // for all backward references (such as a thread to a process) |
| 647 | // will eventually solve this issue for us, but for now, we |
| 648 | // need to work around the issue |
| 649 | collection::iterator rhs_pos, rhs_end = rhs.m_threads.end(); |
| 650 | for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) |
| 651 | { |
| 652 | const lldb::tid_t tid = (*rhs_pos)->GetID(); |
| 653 | bool thread_is_alive = false; |
| 654 | const uint32_t num_threads = m_threads.size(); |
| 655 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 656 | { |
| 657 | if (m_threads[idx]->GetID() == tid) |
| 658 | { |
| 659 | thread_is_alive = true; |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | if (!thread_is_alive) |
| 664 | (*rhs_pos)->DestroyThread(); |
| 665 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 669 | void |
| 670 | ThreadList::Flush () |
| 671 | { |
| 672 | Mutex::Locker locker(m_threads_mutex); |
| 673 | collection::iterator pos, end = m_threads.end(); |
| 674 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 675 | (*pos)->Flush (); |
| 676 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 677 | |