Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Thread.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 | |
| 10 | #include "lldb/lldb-private-log.h" |
Jim Ingham | 3c7b5b9 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 11 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Log.h" |
| 13 | #include "lldb/Core/Stream.h" |
| 14 | #include "lldb/Core/StreamString.h" |
| 15 | #include "lldb/Host/Host.h" |
| 16 | #include "lldb/Target/DynamicLoader.h" |
| 17 | #include "lldb/Target/ExecutionContext.h" |
| 18 | #include "lldb/Target/Process.h" |
| 19 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 20 | #include "lldb/Target/StopInfo.h" |
Greg Clayton | 7661a98 | 2010-07-23 16:45:51 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Thread.h" |
| 23 | #include "lldb/Target/ThreadPlan.h" |
| 24 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Target/ThreadPlanBase.h" |
| 26 | #include "lldb/Target/ThreadPlanStepInstruction.h" |
| 27 | #include "lldb/Target/ThreadPlanStepOut.h" |
| 28 | #include "lldb/Target/ThreadPlanStepOverBreakpoint.h" |
| 29 | #include "lldb/Target/ThreadPlanStepThrough.h" |
| 30 | #include "lldb/Target/ThreadPlanStepInRange.h" |
| 31 | #include "lldb/Target/ThreadPlanStepOverRange.h" |
| 32 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
| 33 | #include "lldb/Target/ThreadPlanStepUntil.h" |
Jim Ingham | 3c7b5b9 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 34 | #include "lldb/Target/ThreadSpec.h" |
Jim Ingham | 7121908 | 2010-08-12 02:14:28 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Unwind.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
| 40 | Thread::Thread (Process &process, lldb::tid_t tid) : |
| 41 | UserID (tid), |
Benjamin Kramer | 36a0810 | 2010-07-16 12:32:33 +0000 | [diff] [blame] | 42 | m_process (process), |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 43 | m_public_stop_info_sp (), |
| 44 | m_actual_stop_info_sp (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | m_index_id (process.GetNextThreadIndexID ()), |
| 46 | m_reg_context_sp (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | m_state (eStateUnloaded), |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 48 | m_state_mutex (Mutex::eMutexTypeRecursive), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | m_plan_stack (), |
| 50 | m_immediate_plan_stack(), |
| 51 | m_completed_plan_stack(), |
Greg Clayton | f40e308 | 2010-08-26 02:28:22 +0000 | [diff] [blame] | 52 | m_curr_frames_ap (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER), |
Jim Ingham | 7121908 | 2010-08-12 02:14:28 +0000 | [diff] [blame] | 54 | m_resume_state (eStateRunning), |
| 55 | m_unwinder_ap () |
| 56 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | { |
| 58 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); |
| 59 | if (log) |
| 60 | log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID()); |
| 61 | |
| 62 | QueueFundamentalPlan(true); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | Thread::~Thread() |
| 67 | { |
| 68 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); |
| 69 | if (log) |
| 70 | log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID()); |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | Thread::GetResumeSignal () const |
| 75 | { |
| 76 | return m_resume_signal; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Thread::SetResumeSignal (int signal) |
| 81 | { |
| 82 | m_resume_signal = signal; |
| 83 | } |
| 84 | |
| 85 | StateType |
| 86 | Thread::GetResumeState () const |
| 87 | { |
| 88 | return m_resume_state; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Thread::SetResumeState (StateType state) |
| 93 | { |
| 94 | m_resume_state = state; |
| 95 | } |
| 96 | |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 97 | StopInfo * |
| 98 | Thread::GetStopInfo () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 100 | if (m_public_stop_info_sp.get() == NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 102 | ThreadPlanSP plan_sp (GetCompletedPlan()); |
| 103 | if (plan_sp) |
| 104 | m_public_stop_info_sp = StopInfo::CreateStopReasonWithPlan (plan_sp); |
| 105 | else |
| 106 | m_public_stop_info_sp = GetPrivateStopReason (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 108 | return m_public_stop_info_sp.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool |
| 112 | Thread::ThreadStoppedForAReason (void) |
| 113 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 114 | return GetPrivateStopReason () != NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | StateType |
| 118 | Thread::GetState() const |
| 119 | { |
| 120 | // If any other threads access this we will need a mutex for it |
| 121 | Mutex::Locker locker(m_state_mutex); |
| 122 | return m_state; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | Thread::SetState(StateType state) |
| 127 | { |
| 128 | Mutex::Locker locker(m_state_mutex); |
| 129 | m_state = state; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | Thread::WillStop() |
| 134 | { |
| 135 | ThreadPlan *current_plan = GetCurrentPlan(); |
| 136 | |
| 137 | // FIXME: I may decide to disallow threads with no plans. In which |
| 138 | // case this should go to an assert. |
| 139 | |
| 140 | if (!current_plan) |
| 141 | return; |
| 142 | |
| 143 | current_plan->WillStop(); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | Thread::SetupForResume () |
| 148 | { |
| 149 | if (GetResumeState() != eStateSuspended) |
| 150 | { |
| 151 | |
| 152 | // If we're at a breakpoint push the step-over breakpoint plan. Do this before |
| 153 | // telling the current plan it will resume, since we might change what the current |
| 154 | // plan is. |
| 155 | |
| 156 | lldb::addr_t pc = GetRegisterContext()->GetPC(); |
| 157 | BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc); |
| 158 | if (bp_site_sp && bp_site_sp->IsEnabled()) |
| 159 | { |
| 160 | // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything |
| 161 | // special to step over a breakpoint. |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 162 | |
| 163 | ThreadPlan *cur_plan = GetCurrentPlan(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 165 | if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint) |
| 166 | { |
| 167 | ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this); |
| 168 | if (step_bp_plan) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | { |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 170 | ThreadPlanSP step_bp_plan_sp; |
| 171 | step_bp_plan->SetPrivate (true); |
| 172 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | if (GetCurrentPlan()->RunState() != eStateStepping) |
| 174 | { |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 175 | step_bp_plan->SetAutoContinue(true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | } |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 177 | step_bp_plan_sp.reset (step_bp_plan); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | QueueThreadPlan (step_bp_plan_sp, false); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | bool |
| 186 | Thread::WillResume (StateType resume_state) |
| 187 | { |
| 188 | // At this point clear the completed plan stack. |
| 189 | m_completed_plan_stack.clear(); |
| 190 | m_discarded_plan_stack.clear(); |
| 191 | |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 192 | StopInfo *stop_info = GetPrivateStopReason().get(); |
| 193 | if (stop_info) |
| 194 | stop_info->WillResume (resume_state); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | |
| 196 | // Tell all the plans that we are about to resume in case they need to clear any state. |
| 197 | // We distinguish between the plan on the top of the stack and the lower |
| 198 | // plans in case a plan needs to do any special business before it runs. |
| 199 | |
| 200 | ThreadPlan *plan_ptr = GetCurrentPlan(); |
| 201 | plan_ptr->WillResume(resume_state, true); |
| 202 | |
| 203 | while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) |
| 204 | { |
| 205 | plan_ptr->WillResume (resume_state, false); |
| 206 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 207 | |
| 208 | m_public_stop_info_sp.reset(); |
| 209 | m_actual_stop_info_sp.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | Thread::DidResume () |
| 215 | { |
| 216 | SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER); |
| 217 | } |
| 218 | |
| 219 | bool |
| 220 | Thread::ShouldStop (Event* event_ptr) |
| 221 | { |
| 222 | ThreadPlan *current_plan = GetCurrentPlan(); |
| 223 | bool should_stop = true; |
| 224 | |
| 225 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 226 | if (log) |
| 227 | { |
| 228 | StreamString s; |
| 229 | DumpThreadPlans(&s); |
| 230 | log->PutCString (s.GetData()); |
| 231 | } |
| 232 | |
| 233 | if (current_plan->PlanExplainsStop()) |
| 234 | { |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 235 | bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | while (1) |
| 237 | { |
| 238 | should_stop = current_plan->ShouldStop(event_ptr); |
| 239 | if (current_plan->MischiefManaged()) |
| 240 | { |
| 241 | if (should_stop) |
| 242 | current_plan->WillStop(); |
| 243 | |
| 244 | // If a Master Plan wants to stop, and wants to stick on the stack, we let it. |
| 245 | // Otherwise, see if the plan's parent wants to stop. |
| 246 | |
| 247 | if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard()) |
| 248 | { |
| 249 | PopPlan(); |
| 250 | break; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | |
| 255 | PopPlan(); |
| 256 | |
| 257 | current_plan = GetCurrentPlan(); |
| 258 | if (current_plan == NULL) |
| 259 | { |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | break; |
| 268 | } |
| 269 | } |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 270 | if (over_ride_stop) |
| 271 | should_stop = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | } |
| 273 | else |
| 274 | { |
| 275 | // If the current plan doesn't explain the stop, then, find one that |
| 276 | // does and let it handle the situation. |
| 277 | ThreadPlan *plan_ptr = current_plan; |
| 278 | while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) |
| 279 | { |
| 280 | if (plan_ptr->PlanExplainsStop()) |
| 281 | { |
| 282 | should_stop = plan_ptr->ShouldStop (event_ptr); |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return should_stop; |
| 290 | } |
| 291 | |
| 292 | Vote |
| 293 | Thread::ShouldReportStop (Event* event_ptr) |
| 294 | { |
| 295 | StateType thread_state = GetResumeState (); |
| 296 | if (thread_state == eStateSuspended |
| 297 | || thread_state == eStateInvalid) |
| 298 | return eVoteNoOpinion; |
| 299 | |
| 300 | if (m_completed_plan_stack.size() > 0) |
| 301 | { |
| 302 | // Don't use GetCompletedPlan here, since that suppresses private plans. |
| 303 | return m_completed_plan_stack.back()->ShouldReportStop (event_ptr); |
| 304 | } |
| 305 | else |
| 306 | return GetCurrentPlan()->ShouldReportStop (event_ptr); |
| 307 | } |
| 308 | |
| 309 | Vote |
| 310 | Thread::ShouldReportRun (Event* event_ptr) |
| 311 | { |
| 312 | StateType thread_state = GetResumeState (); |
| 313 | if (thread_state == eStateSuspended |
| 314 | || thread_state == eStateInvalid) |
| 315 | return eVoteNoOpinion; |
| 316 | |
| 317 | if (m_completed_plan_stack.size() > 0) |
| 318 | { |
| 319 | // Don't use GetCompletedPlan here, since that suppresses private plans. |
| 320 | return m_completed_plan_stack.back()->ShouldReportRun (event_ptr); |
| 321 | } |
| 322 | else |
| 323 | return GetCurrentPlan()->ShouldReportRun (event_ptr); |
| 324 | } |
| 325 | |
Jim Ingham | 3c7b5b9 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 326 | bool |
| 327 | Thread::MatchesSpec (const ThreadSpec *spec) |
| 328 | { |
| 329 | if (spec == NULL) |
| 330 | return true; |
Jim Ingham | 3c7b5b9 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 331 | |
Jim Ingham | 649492b | 2010-06-18 01:00:58 +0000 | [diff] [blame] | 332 | return spec->ThreadPassesBasicTests(this); |
Jim Ingham | 3c7b5b9 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 335 | void |
| 336 | Thread::PushPlan (ThreadPlanSP &thread_plan_sp) |
| 337 | { |
| 338 | if (thread_plan_sp) |
| 339 | { |
| 340 | if (thread_plan_sp->IsImmediate()) |
| 341 | m_immediate_plan_stack.push_back (thread_plan_sp); |
| 342 | else |
| 343 | m_plan_stack.push_back (thread_plan_sp); |
| 344 | |
| 345 | thread_plan_sp->DidPush(); |
| 346 | |
| 347 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 348 | if (log) |
| 349 | { |
| 350 | StreamString s; |
| 351 | thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull); |
| 352 | log->Printf("Pushing plan: \"%s\" for thread: %d immediate: %s.", |
| 353 | s.GetData(), |
| 354 | thread_plan_sp->GetThread().GetID(), |
| 355 | thread_plan_sp->IsImmediate() ? "true" : "false"); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void |
| 361 | Thread::PopPlan () |
| 362 | { |
| 363 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 364 | |
| 365 | if (!m_immediate_plan_stack.empty()) |
| 366 | { |
| 367 | ThreadPlanSP &plan = m_immediate_plan_stack.back(); |
| 368 | if (log) |
| 369 | { |
| 370 | log->Printf("Popping plan: \"%s\" for thread: %d immediate: true.", plan->GetName(), plan->GetThread().GetID()); |
| 371 | } |
| 372 | plan->WillPop(); |
| 373 | m_immediate_plan_stack.pop_back(); |
| 374 | } |
| 375 | else if (m_plan_stack.empty()) |
| 376 | return; |
| 377 | else |
| 378 | { |
| 379 | ThreadPlanSP &plan = m_plan_stack.back(); |
| 380 | if (log) |
| 381 | { |
| 382 | log->Printf("Popping plan: \"%s\" for thread: 0x%x immediate: false.", plan->GetName(), plan->GetThread().GetID()); |
| 383 | } |
| 384 | m_completed_plan_stack.push_back (plan); |
| 385 | plan->WillPop(); |
| 386 | m_plan_stack.pop_back(); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | Thread::DiscardPlan () |
| 392 | { |
| 393 | if (m_plan_stack.size() > 1) |
| 394 | { |
| 395 | ThreadPlanSP &plan = m_plan_stack.back(); |
| 396 | m_discarded_plan_stack.push_back (plan); |
| 397 | plan->WillPop(); |
| 398 | m_plan_stack.pop_back(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | ThreadPlan * |
| 403 | Thread::GetCurrentPlan () |
| 404 | { |
| 405 | if (!m_immediate_plan_stack.empty()) |
| 406 | return m_immediate_plan_stack.back().get(); |
| 407 | else if (m_plan_stack.empty()) |
| 408 | return NULL; |
| 409 | else |
| 410 | return m_plan_stack.back().get(); |
| 411 | } |
| 412 | |
| 413 | ThreadPlanSP |
| 414 | Thread::GetCompletedPlan () |
| 415 | { |
| 416 | ThreadPlanSP empty_plan_sp; |
| 417 | if (!m_completed_plan_stack.empty()) |
| 418 | { |
| 419 | for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) |
| 420 | { |
| 421 | ThreadPlanSP completed_plan_sp; |
| 422 | completed_plan_sp = m_completed_plan_stack[i]; |
| 423 | if (!completed_plan_sp->GetPrivate ()) |
| 424 | return completed_plan_sp; |
| 425 | } |
| 426 | } |
| 427 | return empty_plan_sp; |
| 428 | } |
| 429 | |
| 430 | bool |
| 431 | Thread::IsThreadPlanDone (ThreadPlan *plan) |
| 432 | { |
| 433 | ThreadPlanSP empty_plan_sp; |
| 434 | if (!m_completed_plan_stack.empty()) |
| 435 | { |
| 436 | for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) |
| 437 | { |
| 438 | if (m_completed_plan_stack[i].get() == plan) |
| 439 | return true; |
| 440 | } |
| 441 | } |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | bool |
| 446 | Thread::WasThreadPlanDiscarded (ThreadPlan *plan) |
| 447 | { |
| 448 | ThreadPlanSP empty_plan_sp; |
| 449 | if (!m_discarded_plan_stack.empty()) |
| 450 | { |
| 451 | for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) |
| 452 | { |
| 453 | if (m_discarded_plan_stack[i].get() == plan) |
| 454 | return true; |
| 455 | } |
| 456 | } |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | ThreadPlan * |
| 461 | Thread::GetPreviousPlan (ThreadPlan *current_plan) |
| 462 | { |
| 463 | if (current_plan == NULL) |
| 464 | return NULL; |
| 465 | |
| 466 | int stack_size = m_completed_plan_stack.size(); |
| 467 | for (int i = stack_size - 1; i > 0; i--) |
| 468 | { |
| 469 | if (current_plan == m_completed_plan_stack[i].get()) |
| 470 | return m_completed_plan_stack[i-1].get(); |
| 471 | } |
| 472 | |
| 473 | if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) |
| 474 | { |
| 475 | if (m_immediate_plan_stack.size() > 0) |
| 476 | return m_immediate_plan_stack.back().get(); |
| 477 | else if (m_plan_stack.size() > 0) |
| 478 | return m_plan_stack.back().get(); |
| 479 | else |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | stack_size = m_immediate_plan_stack.size(); |
| 484 | for (int i = stack_size - 1; i > 0; i--) |
| 485 | { |
| 486 | if (current_plan == m_immediate_plan_stack[i].get()) |
| 487 | return m_immediate_plan_stack[i-1].get(); |
| 488 | } |
| 489 | if (stack_size > 0 && m_immediate_plan_stack[0].get() == current_plan) |
| 490 | { |
| 491 | if (m_plan_stack.size() > 0) |
| 492 | return m_plan_stack.back().get(); |
| 493 | else |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | stack_size = m_plan_stack.size(); |
| 498 | for (int i = stack_size - 1; i > 0; i--) |
| 499 | { |
| 500 | if (current_plan == m_plan_stack[i].get()) |
| 501 | return m_plan_stack[i-1].get(); |
| 502 | } |
| 503 | return NULL; |
| 504 | } |
| 505 | |
| 506 | void |
| 507 | Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans) |
| 508 | { |
| 509 | if (abort_other_plans) |
| 510 | DiscardThreadPlans(true); |
| 511 | |
| 512 | PushPlan (thread_plan_sp); |
| 513 | } |
| 514 | |
| 515 | void |
| 516 | Thread::DiscardThreadPlans(bool force) |
| 517 | { |
| 518 | // FIXME: It is not always safe to just discard plans. Some, like the step over |
| 519 | // breakpoint trap can't be discarded in general (though you can if you plan to |
| 520 | // force a return from a function, for instance. |
| 521 | // For now I'm just not clearing immediate plans, but I need a way for plans to |
| 522 | // say they really need to be kept on, and then a way to override that. Humm... |
| 523 | |
| 524 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 525 | if (log) |
| 526 | { |
| 527 | log->Printf("Discarding thread plans for thread: 0x%x: force %d.", GetID(), force); |
| 528 | } |
| 529 | |
| 530 | if (force) |
| 531 | { |
| 532 | int stack_size = m_plan_stack.size(); |
| 533 | for (int i = stack_size - 1; i > 0; i--) |
| 534 | { |
| 535 | DiscardPlan(); |
| 536 | } |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | while (1) |
| 541 | { |
| 542 | |
| 543 | int master_plan_idx; |
| 544 | bool discard; |
| 545 | |
| 546 | // Find the first master plan, see if it wants discarding, and if yes discard up to it. |
| 547 | for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--) |
| 548 | { |
| 549 | if (m_plan_stack[master_plan_idx]->IsMasterPlan()) |
| 550 | { |
| 551 | discard = m_plan_stack[master_plan_idx]->OkayToDiscard(); |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (discard) |
| 557 | { |
| 558 | // First pop all the dependent plans: |
| 559 | for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) |
| 560 | { |
| 561 | |
| 562 | // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop" |
| 563 | // for the plan leaves it in a state that it is safe to pop the plan |
| 564 | // with no more notice? |
| 565 | DiscardPlan(); |
| 566 | } |
| 567 | |
| 568 | // Now discard the master plan itself. |
| 569 | // The bottom-most plan never gets discarded. "OkayToDiscard" for it means |
| 570 | // discard it's dependent plans, but not it... |
| 571 | if (master_plan_idx > 0) |
| 572 | { |
| 573 | DiscardPlan(); |
| 574 | } |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | // If the master plan doesn't want to get discarded, then we're done. |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | } |
| 583 | // FIXME: What should we do about the immediate plans? |
| 584 | } |
| 585 | |
| 586 | ThreadPlan * |
| 587 | Thread::QueueFundamentalPlan (bool abort_other_plans) |
| 588 | { |
| 589 | ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this)); |
| 590 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 591 | return thread_plan_sp.get(); |
| 592 | } |
| 593 | |
| 594 | ThreadPlan * |
| 595 | Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads) |
| 596 | { |
| 597 | ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion)); |
| 598 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 599 | return thread_plan_sp.get(); |
| 600 | } |
| 601 | |
| 602 | ThreadPlan * |
Greg Clayton | 8f5fd6b | 2010-06-12 18:59:55 +0000 | [diff] [blame] | 603 | Thread::QueueThreadPlanForStepRange |
| 604 | ( |
| 605 | bool abort_other_plans, |
| 606 | StepType type, |
| 607 | const AddressRange &range, |
| 608 | const SymbolContext &addr_context, |
| 609 | lldb::RunMode stop_other_threads, |
| 610 | bool avoid_code_without_debug_info |
| 611 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 612 | { |
| 613 | ThreadPlanSP thread_plan_sp; |
| 614 | if (type == eStepTypeInto) |
Greg Clayton | 8f5fd6b | 2010-06-12 18:59:55 +0000 | [diff] [blame] | 615 | { |
| 616 | ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads); |
| 617 | if (avoid_code_without_debug_info) |
| 618 | plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug); |
| 619 | else |
| 620 | plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug); |
| 621 | thread_plan_sp.reset (plan); |
| 622 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | else |
| 624 | thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads)); |
| 625 | |
| 626 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 627 | return thread_plan_sp.get(); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | ThreadPlan * |
| 632 | Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans) |
| 633 | { |
| 634 | ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this)); |
| 635 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 636 | return thread_plan_sp.get(); |
| 637 | } |
| 638 | |
| 639 | ThreadPlan * |
| 640 | Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn, |
| 641 | bool stop_other_threads, Vote stop_vote, Vote run_vote) |
| 642 | { |
| 643 | ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote)); |
| 644 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 645 | return thread_plan_sp.get(); |
| 646 | } |
| 647 | |
| 648 | ThreadPlan * |
| 649 | Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads) |
| 650 | { |
| 651 | ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads)); |
| 652 | if (thread_plan_sp.get() == NULL) |
| 653 | { |
| 654 | thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads)); |
| 655 | if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL)) |
Greg Clayton | f8e98a6 | 2010-07-23 15:37:46 +0000 | [diff] [blame] | 656 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 657 | } |
| 658 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 659 | return thread_plan_sp.get(); |
| 660 | } |
| 661 | |
| 662 | ThreadPlan * |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 663 | Thread::QueueThreadPlanForCallFunction (bool abort_other_plans, |
| 664 | Address& function, |
| 665 | lldb::addr_t arg, |
| 666 | bool stop_other_threads, |
| 667 | bool discard_on_error) |
| 668 | { |
| 669 | ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error)); |
| 670 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 671 | return thread_plan_sp.get(); |
| 672 | } |
| 673 | |
| 674 | ThreadPlan * |
| 675 | Thread::QueueThreadPlanForCallFunction (bool abort_other_plans, |
| 676 | Address& function, |
| 677 | ValueList &args, |
| 678 | bool stop_other_threads, |
| 679 | bool discard_on_error) |
| 680 | { |
| 681 | ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error)); |
| 682 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 683 | return thread_plan_sp.get(); |
| 684 | } |
| 685 | |
| 686 | ThreadPlan * |
| 687 | Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans, |
| 688 | Address &target_addr, |
| 689 | bool stop_other_threads) |
| 690 | { |
| 691 | ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads)); |
| 692 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 693 | return thread_plan_sp.get(); |
| 694 | } |
| 695 | |
| 696 | ThreadPlan * |
| 697 | Thread::QueueThreadPlanForStepUntil (bool abort_other_plans, |
| 698 | lldb::addr_t *address_list, |
| 699 | size_t num_addresses, |
| 700 | bool stop_other_threads) |
| 701 | { |
| 702 | ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads)); |
| 703 | QueueThreadPlan (thread_plan_sp, abort_other_plans); |
| 704 | return thread_plan_sp.get(); |
| 705 | |
| 706 | } |
| 707 | |
| 708 | uint32_t |
| 709 | Thread::GetIndexID () const |
| 710 | { |
| 711 | return m_index_id; |
| 712 | } |
| 713 | |
| 714 | void |
| 715 | Thread::DumpThreadPlans (lldb_private::Stream *s) const |
| 716 | { |
| 717 | uint32_t stack_size = m_plan_stack.size(); |
Jim Ingham | 3c7b5b9 | 2010-06-16 02:00:15 +0000 | [diff] [blame] | 718 | s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x - %d elements.\n", GetIndexID(), GetID(), stack_size); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 719 | for (int i = stack_size - 1; i > 0; i--) |
| 720 | { |
| 721 | s->Printf ("Element %d: ", i); |
| 722 | s->IndentMore(); |
| 723 | m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); |
| 724 | s->IndentLess(); |
| 725 | s->EOL(); |
| 726 | } |
| 727 | |
| 728 | stack_size = m_immediate_plan_stack.size(); |
| 729 | s->Printf ("Immediate Plan Stack: %d elements.\n", stack_size); |
| 730 | for (int i = stack_size - 1; i > 0; i--) |
| 731 | { |
| 732 | s->Printf ("Element %d: ", i); |
| 733 | s->IndentMore(); |
| 734 | m_immediate_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); |
| 735 | s->IndentLess(); |
| 736 | s->EOL(); |
| 737 | } |
| 738 | |
| 739 | stack_size = m_completed_plan_stack.size(); |
| 740 | s->Printf ("Completed Plan Stack: %d elements.\n", stack_size); |
| 741 | for (int i = stack_size - 1; i > 0; i--) |
| 742 | { |
| 743 | s->Printf ("Element %d: ", i); |
| 744 | s->IndentMore(); |
| 745 | m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); |
| 746 | s->IndentLess(); |
| 747 | s->EOL(); |
| 748 | } |
| 749 | |
| 750 | stack_size = m_discarded_plan_stack.size(); |
| 751 | s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size); |
| 752 | for (int i = stack_size - 1; i > 0; i--) |
| 753 | { |
| 754 | s->Printf ("Element %d: ", i); |
| 755 | s->IndentMore(); |
| 756 | m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); |
| 757 | s->IndentLess(); |
| 758 | s->EOL(); |
| 759 | } |
| 760 | |
| 761 | } |
| 762 | |
| 763 | Target * |
| 764 | Thread::CalculateTarget () |
| 765 | { |
| 766 | return m_process.CalculateTarget(); |
| 767 | } |
| 768 | |
| 769 | Process * |
| 770 | Thread::CalculateProcess () |
| 771 | { |
| 772 | return &m_process; |
| 773 | } |
| 774 | |
| 775 | Thread * |
| 776 | Thread::CalculateThread () |
| 777 | { |
| 778 | return this; |
| 779 | } |
| 780 | |
| 781 | StackFrame * |
| 782 | Thread::CalculateStackFrame () |
| 783 | { |
| 784 | return NULL; |
| 785 | } |
| 786 | |
| 787 | void |
| 788 | Thread::Calculate (ExecutionContext &exe_ctx) |
| 789 | { |
| 790 | m_process.Calculate (exe_ctx); |
| 791 | exe_ctx.thread = this; |
| 792 | exe_ctx.frame = NULL; |
| 793 | } |
| 794 | |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 795 | |
| 796 | StackFrameList & |
| 797 | Thread::GetStackFrameList () |
| 798 | { |
Greg Clayton | f40e308 | 2010-08-26 02:28:22 +0000 | [diff] [blame] | 799 | if (m_curr_frames_ap.get() == NULL) |
| 800 | m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_ap.release(), true)); |
| 801 | return *m_curr_frames_ap; |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | |
| 805 | |
Jim Ingham | 7121908 | 2010-08-12 02:14:28 +0000 | [diff] [blame] | 806 | uint32_t |
| 807 | Thread::GetStackFrameCount() |
| 808 | { |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 809 | return GetStackFrameList().GetNumFrames(); |
| 810 | } |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 811 | |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 812 | |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 813 | void |
| 814 | Thread::ClearStackFrames () |
| 815 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 816 | if (m_curr_frames_ap.get()) |
| 817 | m_prev_frames_ap = m_curr_frames_ap; |
Jim Ingham | 7121908 | 2010-08-12 02:14:28 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | lldb::StackFrameSP |
| 821 | Thread::GetStackFrameAtIndex (uint32_t idx) |
| 822 | { |
Greg Clayton | 72b7158 | 2010-09-02 21:44:10 +0000 | [diff] [blame^] | 823 | return GetStackFrameList().GetFrameAtIndex(idx); |
Jim Ingham | 7121908 | 2010-08-12 02:14:28 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 826 | lldb::StackFrameSP |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 827 | Thread::GetSelectedFrame () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 829 | return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | uint32_t |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 833 | Thread::SetSelectedFrame (lldb_private::StackFrame *frame) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 834 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 835 | return GetStackFrameList().SetSelectedFrame(frame); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | void |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 839 | Thread::SetSelectedFrameByIndex (uint32_t idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 840 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 841 | GetStackFrameList().SetSelectedFrameByIndex(idx); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | void |
| 845 | Thread::DumpInfo |
| 846 | ( |
| 847 | Stream &strm, |
| 848 | bool show_stop_reason, |
| 849 | bool show_name, |
| 850 | bool show_queue, |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 851 | uint32_t idx |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 852 | ) |
| 853 | { |
| 854 | strm.Printf("thread #%u: tid = 0x%4.4x", GetIndexID(), GetID()); |
| 855 | |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 856 | if (idx != LLDB_INVALID_INDEX32) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 857 | { |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 858 | StackFrameSP frame_sp(GetStackFrameAtIndex (idx)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 859 | if (frame_sp) |
| 860 | { |
| 861 | strm.PutCString(", "); |
Greg Clayton | 72b7158 | 2010-09-02 21:44:10 +0000 | [diff] [blame^] | 862 | frame_sp->Dump (&strm, false, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
| 866 | if (show_stop_reason) |
| 867 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 868 | StopInfo *stop_info = GetStopInfo(); |
| 869 | |
| 870 | if (stop_info) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 871 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 872 | const char *stop_description = stop_info->GetDescription(); |
| 873 | if (stop_description) |
| 874 | strm.Printf (", stop reason = %s", stop_description); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | if (show_name) |
| 879 | { |
| 880 | const char *name = GetName(); |
| 881 | if (name && name[0]) |
| 882 | strm.Printf(", name = %s", name); |
| 883 | } |
| 884 | |
| 885 | if (show_queue) |
| 886 | { |
| 887 | const char *queue = GetQueueName(); |
| 888 | if (queue && queue[0]) |
| 889 | strm.Printf(", queue = %s", queue); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | lldb::ThreadSP |
| 894 | Thread::GetSP () |
| 895 | { |
| 896 | return m_process.GetThreadList().GetThreadSPForThreadPtr(this); |
| 897 | } |