blob: 1c8265add06f4280eabd5f59340e6e4c8d84e32b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham3c7b5b92010-06-16 02:00:15 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
Chris Lattner24943d22010-06-08 16:52:24 +000012#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 Clayton643ee732010-08-04 01:40:35 +000020#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000021#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Thread.h"
23#include "lldb/Target/ThreadPlan.h"
24#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#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 Ingham3c7b5b92010-06-16 02:00:15 +000034#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000035#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
40Thread::Thread (Process &process, lldb::tid_t tid) :
41 UserID (tid),
Benjamin Kramer36a08102010-07-16 12:32:33 +000042 m_process (process),
Greg Clayton643ee732010-08-04 01:40:35 +000043 m_public_stop_info_sp (),
44 m_actual_stop_info_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000045 m_index_id (process.GetNextThreadIndexID ()),
46 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +000048 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000049 m_plan_stack (),
50 m_immediate_plan_stack(),
51 m_completed_plan_stack(),
Greg Claytonf40e3082010-08-26 02:28:22 +000052 m_curr_frames_ap (),
Chris Lattner24943d22010-06-08 16:52:24 +000053 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000054 m_resume_state (eStateRunning),
55 m_unwinder_ap ()
56
Chris Lattner24943d22010-06-08 16:52:24 +000057{
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
66Thread::~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
73int
74Thread::GetResumeSignal () const
75{
76 return m_resume_signal;
77}
78
79void
80Thread::SetResumeSignal (int signal)
81{
82 m_resume_signal = signal;
83}
84
85StateType
86Thread::GetResumeState () const
87{
88 return m_resume_state;
89}
90
91void
92Thread::SetResumeState (StateType state)
93{
94 m_resume_state = state;
95}
96
Greg Clayton643ee732010-08-04 01:40:35 +000097StopInfo *
98Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000099{
Greg Clayton643ee732010-08-04 01:40:35 +0000100 if (m_public_stop_info_sp.get() == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000101 {
Greg Clayton643ee732010-08-04 01:40:35 +0000102 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 Lattner24943d22010-06-08 16:52:24 +0000107 }
Greg Clayton643ee732010-08-04 01:40:35 +0000108 return m_public_stop_info_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000109}
110
111bool
112Thread::ThreadStoppedForAReason (void)
113{
Greg Clayton643ee732010-08-04 01:40:35 +0000114 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
117StateType
118Thread::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
125void
126Thread::SetState(StateType state)
127{
128 Mutex::Locker locker(m_state_mutex);
129 m_state = state;
130}
131
132void
133Thread::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
146void
147Thread::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 Ingham5a47e8b2010-06-19 04:45:32 +0000162
163 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000164
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000165 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
166 {
167 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
168 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000169 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000170 ThreadPlanSP step_bp_plan_sp;
171 step_bp_plan->SetPrivate (true);
172
Chris Lattner24943d22010-06-08 16:52:24 +0000173 if (GetCurrentPlan()->RunState() != eStateStepping)
174 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000175 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000176 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000177 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000178 QueueThreadPlan (step_bp_plan_sp, false);
179 }
180 }
181 }
182 }
183}
184
185bool
186Thread::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 Clayton643ee732010-08-04 01:40:35 +0000192 StopInfo *stop_info = GetPrivateStopReason().get();
193 if (stop_info)
194 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000195
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 Clayton643ee732010-08-04 01:40:35 +0000207
208 m_public_stop_info_sp.reset();
209 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000210 return true;
211}
212
213void
214Thread::DidResume ()
215{
216 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
217}
218
219bool
220Thread::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 Ingham5a47e8b2010-06-19 04:45:32 +0000235 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000236 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 Ingham5a47e8b2010-06-19 04:45:32 +0000270 if (over_ride_stop)
271 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000272 }
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
292Vote
293Thread::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
309Vote
310Thread::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 Ingham3c7b5b92010-06-16 02:00:15 +0000326bool
327Thread::MatchesSpec (const ThreadSpec *spec)
328{
329 if (spec == NULL)
330 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000331
Jim Ingham649492b2010-06-18 01:00:58 +0000332 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000333}
334
Chris Lattner24943d22010-06-08 16:52:24 +0000335void
336Thread::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
360void
361Thread::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
390void
391Thread::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
402ThreadPlan *
403Thread::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
413ThreadPlanSP
414Thread::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
430bool
431Thread::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
445bool
446Thread::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
460ThreadPlan *
461Thread::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
506void
507Thread::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
515void
516Thread::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
586ThreadPlan *
587Thread::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
594ThreadPlan *
595Thread::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
602ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000603Thread::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 Lattner24943d22010-06-08 16:52:24 +0000612{
613 ThreadPlanSP thread_plan_sp;
614 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000615 {
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 Lattner24943d22010-06-08 16:52:24 +0000623 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
631ThreadPlan *
632Thread::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
639ThreadPlan *
640Thread::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
648ThreadPlan *
649Thread::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 Claytonf8e98a62010-07-23 15:37:46 +0000656 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000657 }
658 QueueThreadPlan (thread_plan_sp, abort_other_plans);
659 return thread_plan_sp.get();
660}
661
662ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000663Thread::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
674ThreadPlan *
675Thread::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
686ThreadPlan *
687Thread::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
696ThreadPlan *
697Thread::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
708uint32_t
709Thread::GetIndexID () const
710{
711 return m_index_id;
712}
713
714void
715Thread::DumpThreadPlans (lldb_private::Stream *s) const
716{
717 uint32_t stack_size = m_plan_stack.size();
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000718 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x - %d elements.\n", GetIndexID(), GetID(), stack_size);
Chris Lattner24943d22010-06-08 16:52:24 +0000719 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
763Target *
764Thread::CalculateTarget ()
765{
766 return m_process.CalculateTarget();
767}
768
769Process *
770Thread::CalculateProcess ()
771{
772 return &m_process;
773}
774
775Thread *
776Thread::CalculateThread ()
777{
778 return this;
779}
780
781StackFrame *
782Thread::CalculateStackFrame ()
783{
784 return NULL;
785}
786
787void
788Thread::Calculate (ExecutionContext &exe_ctx)
789{
790 m_process.Calculate (exe_ctx);
791 exe_ctx.thread = this;
792 exe_ctx.frame = NULL;
793}
794
Greg Clayton782b9cc2010-08-25 00:35:26 +0000795
796StackFrameList &
797Thread::GetStackFrameList ()
798{
Greg Claytonf40e3082010-08-26 02:28:22 +0000799 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 Clayton782b9cc2010-08-25 00:35:26 +0000802}
803
804
805
Jim Ingham71219082010-08-12 02:14:28 +0000806uint32_t
807Thread::GetStackFrameCount()
808{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000809 return GetStackFrameList().GetNumFrames();
810}
Greg Clayton33ed1702010-08-24 00:45:41 +0000811
Greg Clayton33ed1702010-08-24 00:45:41 +0000812
Greg Clayton782b9cc2010-08-25 00:35:26 +0000813void
814Thread::ClearStackFrames ()
815{
Greg Clayton1d66ef52010-08-27 18:24:16 +0000816 if (m_curr_frames_ap.get())
817 m_prev_frames_ap = m_curr_frames_ap;
Jim Ingham71219082010-08-12 02:14:28 +0000818}
819
820lldb::StackFrameSP
821Thread::GetStackFrameAtIndex (uint32_t idx)
822{
Greg Clayton72b71582010-09-02 21:44:10 +0000823 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000824}
825
Chris Lattner24943d22010-06-08 16:52:24 +0000826lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000827Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000828{
Jim Inghamc8332952010-08-26 21:32:51 +0000829 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000830}
831
832uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000833Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000834{
Jim Inghamc8332952010-08-26 21:32:51 +0000835 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000836}
837
838void
Jim Inghamc8332952010-08-26 21:32:51 +0000839Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000840{
Jim Inghamc8332952010-08-26 21:32:51 +0000841 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000842}
843
844void
845Thread::DumpInfo
846(
847 Stream &strm,
848 bool show_stop_reason,
849 bool show_name,
850 bool show_queue,
Greg Clayton33ed1702010-08-24 00:45:41 +0000851 uint32_t idx
Chris Lattner24943d22010-06-08 16:52:24 +0000852)
853{
854 strm.Printf("thread #%u: tid = 0x%4.4x", GetIndexID(), GetID());
855
Greg Clayton33ed1702010-08-24 00:45:41 +0000856 if (idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000857 {
Greg Clayton33ed1702010-08-24 00:45:41 +0000858 StackFrameSP frame_sp(GetStackFrameAtIndex (idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000859 if (frame_sp)
860 {
861 strm.PutCString(", ");
Greg Clayton72b71582010-09-02 21:44:10 +0000862 frame_sp->Dump (&strm, false, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000863 }
864 }
865
866 if (show_stop_reason)
867 {
Greg Clayton643ee732010-08-04 01:40:35 +0000868 StopInfo *stop_info = GetStopInfo();
869
870 if (stop_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000871 {
Greg Clayton643ee732010-08-04 01:40:35 +0000872 const char *stop_description = stop_info->GetDescription();
873 if (stop_description)
874 strm.Printf (", stop reason = %s", stop_description);
Chris Lattner24943d22010-06-08 16:52:24 +0000875 }
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
893lldb::ThreadSP
894Thread::GetSP ()
895{
896 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
897}