blob: 068cc392f7d4c41b2e87647ebaa06675d8c42d1e [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"
11#include "lldb/Core/Log.h"
12#include "lldb/Core/Stream.h"
13#include "lldb/Core/StreamString.h"
14#include "lldb/Host/Host.h"
15#include "lldb/Target/DynamicLoader.h"
16#include "lldb/Target/ExecutionContext.h"
17#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
19#include "lldb/Target/Thread.h"
20#include "lldb/Target/ThreadPlan.h"
21#include "lldb/Target/ThreadPlanCallFunction.h"
22#include "lldb/Target/ThreadPlanContinue.h"
23#include "lldb/Target/ThreadPlanBase.h"
24#include "lldb/Target/ThreadPlanStepInstruction.h"
25#include "lldb/Target/ThreadPlanStepOut.h"
26#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
27#include "lldb/Target/ThreadPlanStepThrough.h"
28#include "lldb/Target/ThreadPlanStepInRange.h"
29#include "lldb/Target/ThreadPlanStepOverRange.h"
30#include "lldb/Target/ThreadPlanRunToAddress.h"
31#include "lldb/Target/ThreadPlanStepUntil.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36Thread::Thread (Process &process, lldb::tid_t tid) :
37 UserID (tid),
38 m_index_id (process.GetNextThreadIndexID ()),
39 m_reg_context_sp (),
40 m_process (process),
41 m_state (eStateUnloaded),
42 m_plan_stack (),
43 m_immediate_plan_stack(),
44 m_completed_plan_stack(),
45 m_state_mutex (Mutex::eMutexTypeRecursive),
46 m_frames (),
47 m_current_frame_idx (0),
48 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
49 m_resume_state (eStateRunning)
50{
51 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
52 if (log)
53 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
54
55 QueueFundamentalPlan(true);
56}
57
58
59Thread::~Thread()
60{
61 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
62 if (log)
63 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
64}
65
66int
67Thread::GetResumeSignal () const
68{
69 return m_resume_signal;
70}
71
72void
73Thread::SetResumeSignal (int signal)
74{
75 m_resume_signal = signal;
76}
77
78StateType
79Thread::GetResumeState () const
80{
81 return m_resume_state;
82}
83
84void
85Thread::SetResumeState (StateType state)
86{
87 m_resume_state = state;
88}
89
90Thread::StopInfo::StopInfo(Thread *thread) :
91 m_reason (eStopReasonInvalid),
92 m_description (),
93 m_thread (thread),
94 m_details ()
95{
96 m_description[0] = '\0';
97}
98
99Thread::StopInfo::~StopInfo()
100{
101}
102
103
104void
105Thread::StopInfo::Clear()
106{
107 m_reason = eStopReasonInvalid;
108 m_completed_plan_sp.reset();
109 m_description[0] = '\0';
110 ::bzero (&m_details, sizeof(m_details));
111}
112
113StopReason
114Thread::StopInfo::GetStopReason() const
115{
116 return m_reason;
117}
118
119const char *
120Thread::StopInfo::GetStopDescription() const
121{
122 if (m_description[0])
123 return m_description;
124 return NULL;
125}
126
127void
128Thread::StopInfo::SetStopDescription(const char *desc)
129{
130 if (desc && desc[0])
131 {
132 ::snprintf (m_description, sizeof(m_description), "%s", desc);
133 }
134 else
135 {
136 m_description[0] = '\0';
137 }
138}
139
140void
141Thread::StopInfo::SetThread (Thread* thread)
142{
143 m_thread = thread;
144}
145
146Thread *
147Thread::StopInfo::GetThread ()
148{
149 return m_thread;
150}
151
152lldb::user_id_t
153Thread::StopInfo::GetBreakpointSiteID() const
154{
155 if (m_reason == eStopReasonBreakpoint)
156 return m_details.breakpoint.bp_site_id;
157 return LLDB_INVALID_BREAK_ID;
158}
159
160void
161Thread::StopInfo::SetStopReasonWithBreakpointSiteID (lldb::user_id_t bp_site_id)
162{
163 m_reason = eStopReasonBreakpoint;
164 m_details.breakpoint.bp_site_id = bp_site_id;
165}
166
167lldb::user_id_t
168Thread::StopInfo::GetWatchpointID() const
169{
170 if (m_reason == eStopReasonWatchpoint)
171 return m_details.watchpoint.watch_id;
172 return LLDB_INVALID_WATCH_ID;
173}
174
175void
176Thread::StopInfo::SetStopReasonWithWatchpointID (lldb::user_id_t watch_id)
177{
178 m_reason = eStopReasonWatchpoint;
179 m_details.watchpoint.watch_id = watch_id;
180}
181
182
183int
184Thread::StopInfo::GetSignal() const
185{
186 if (m_reason == eStopReasonSignal)
187 return m_details.signal.signo;
188 return 0;
189}
190
191lldb::user_id_t
192Thread::StopInfo::GetPlanID() const
193{
194 if (m_reason == eStopReasonPlanComplete)
195 return m_completed_plan_sp->GetID();
196 return LLDB_INVALID_UID;
197}
198
199void
200Thread::StopInfo::SetStopReasonWithSignal (int signo)
201{
202 m_reason = eStopReasonSignal;
203 m_details.signal.signo = signo;
204}
205
206void
207Thread::StopInfo::SetStopReasonToTrace ()
208{
209 m_reason = eStopReasonTrace;
210}
211
212uint32_t
213Thread::StopInfo::GetExceptionType() const
214{
215 if (m_reason == eStopReasonException)
216 return m_details.exception.type;
217 return 0;
218}
219
220size_t
221Thread::StopInfo::GetExceptionDataCount() const
222{
223 if (m_reason == eStopReasonException)
224 return m_details.exception.data_count;
225 return 0;
226}
227
228void
229Thread::StopInfo::SetStopReasonWithException (uint32_t exc_type, size_t exc_data_count)
230{
231 m_reason = eStopReasonException;
232 m_details.exception.type = exc_type;
233 m_details.exception.data_count = exc_data_count;
234}
235
236void
237Thread::StopInfo::SetStopReasonWithPlan (ThreadPlanSP &thread_plan_sp)
238{
239 m_reason = eStopReasonPlanComplete;
240 m_completed_plan_sp = thread_plan_sp;
241}
242
243void
244Thread::StopInfo::SetStopReasonToNone ()
245{
246 Clear();
247 m_reason = eStopReasonNone;
248}
249
250lldb::addr_t
251Thread::StopInfo::GetExceptionDataAtIndex (uint32_t idx) const
252{
253 if (m_reason == eStopReasonException && idx < m_details.exception.data_count)
254 return m_details.exception.data[idx];
255 return 0;
256
257}
258
259
260bool
261Thread::StopInfo::SetExceptionDataAtIndex (uint32_t idx, lldb::addr_t data)
262{
263 if (m_reason == eStopReasonException && idx < m_details.exception.data_count)
264 {
265 m_details.exception.data[idx] = data;
266 return true;
267 }
268 return false;
269}
270
271void
272Thread::StopInfo::Dump (Stream *s) const
273{
274 if (m_description[0])
275 s->Printf("%s", m_description);
276 else
277 {
278 switch (m_reason)
279 {
280 case eStopReasonInvalid:
281 s->PutCString("invalid");
282 break;
283
284 case eStopReasonNone:
285 s->PutCString("none");
286 break;
287
288 case eStopReasonTrace:
289 s->PutCString("trace");
290 break;
291
292 case eStopReasonBreakpoint:
293 {
294 bool no_details = true;
295 s->PutCString ("breakpoint ");
296 if (m_thread)
297 {
298 BreakpointSiteSP bp_site_sp = m_thread->GetProcess().GetBreakpointSiteList().FindByID(m_details.breakpoint.bp_site_id);
299 if (bp_site_sp)
300 {
301 bp_site_sp->GetDescription(s, lldb::eDescriptionLevelBrief);
302 no_details = false;
303 }
304 }
305
306 if (no_details)
307 s->Printf ("site id: %d", m_details.breakpoint.bp_site_id);
308 }
309 break;
310
311 case eStopReasonWatchpoint:
312 s->Printf("watchpoint (site id = %u)", m_details.watchpoint.watch_id);
313 break;
314
315 case eStopReasonSignal:
316 {
317 s->Printf("signal: signo = %i", m_details.signal.signo);
318 const char * signal_name = m_thread->GetProcess().GetUnixSignals().GetSignalAsCString (m_details.signal.signo);
319 if (signal_name)
320 s->Printf(" (%s)", signal_name);
321 }
322 break;
323
324 case eStopReasonException:
325 {
326 s->Printf("exception: type = 0x%8.8x, data_count = %zu", m_details.exception.type, m_details.exception.data_count);
327 uint32_t i;
328 for (i=0; i<m_details.exception.data_count; ++i)
329 {
330 s->Printf(", data[%u] = 0x%8.8llx", i, m_details.exception.data[i]);
331 }
332 }
333 break;
334
335 case eStopReasonPlanComplete:
336 {
337 m_completed_plan_sp->GetDescription (s, lldb::eDescriptionLevelBrief);
338 }
339 break;
340 }
341 }
342}
343
344bool
345Thread::GetStopInfo (Thread::StopInfo *stop_info)
346{
347 stop_info->SetThread(this);
348 ThreadPlanSP completed_plan = GetCompletedPlan();
349 if (completed_plan != NULL)
350 {
351 stop_info->Clear ();
352 stop_info->SetStopReasonWithPlan (completed_plan);
353 return true;
354 }
355 else
356 return GetRawStopReason (stop_info);
357}
358
359bool
360Thread::ThreadStoppedForAReason (void)
361{
362 Thread::StopInfo stop_info;
363 stop_info.SetThread(this);
364 if (GetRawStopReason (&stop_info))
365 {
366 StopReason reason = stop_info.GetStopReason();
367 if (reason == eStopReasonInvalid || reason == eStopReasonNone)
368 return false;
369 else
370 return true;
371 }
372 else
373 return false;
374}
375
376StateType
377Thread::GetState() const
378{
379 // If any other threads access this we will need a mutex for it
380 Mutex::Locker locker(m_state_mutex);
381 return m_state;
382}
383
384void
385Thread::SetState(StateType state)
386{
387 Mutex::Locker locker(m_state_mutex);
388 m_state = state;
389}
390
391void
392Thread::WillStop()
393{
394 ThreadPlan *current_plan = GetCurrentPlan();
395
396 // FIXME: I may decide to disallow threads with no plans. In which
397 // case this should go to an assert.
398
399 if (!current_plan)
400 return;
401
402 current_plan->WillStop();
403}
404
405void
406Thread::SetupForResume ()
407{
408 if (GetResumeState() != eStateSuspended)
409 {
410
411 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
412 // telling the current plan it will resume, since we might change what the current
413 // plan is.
414
415 lldb::addr_t pc = GetRegisterContext()->GetPC();
416 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
417 if (bp_site_sp && bp_site_sp->IsEnabled())
418 {
419 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
420 // special to step over a breakpoint.
421
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000422 // TODO: Jim Ingham -- this is the only place left that does RTTI in
423 // all of LLDB. I am adding a hack right now to let us compile
424 // without RTTI, but we will need to look at and fix this. Right
425 // now it will always push the breakpoint thread plan which is
426 // probably wrong. We will need to work around this.
427
428// ThreadPlan *cur_plan = GetCurrentPlan();
429// ThreadPlanStepOverBreakpoint *step_over_bp = dynamic_cast<ThreadPlanStepOverBreakpoint *> (cur_plan);
430// if (step_over_bp == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000431 {
432
433 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
434 if (step_bp_plan_sp)
435 {
436 if (GetCurrentPlan()->RunState() != eStateStepping)
437 {
438 ThreadPlanSP continue_plan_sp (new ThreadPlanContinue(*this, false, eVoteNo, eVoteNoOpinion));
439 continue_plan_sp->SetPrivate (true);
440 QueueThreadPlan (continue_plan_sp, false);
441 }
442 step_bp_plan_sp->SetPrivate (true);
443 QueueThreadPlan (step_bp_plan_sp, false);
444 }
445 }
446 }
447 }
448}
449
450bool
451Thread::WillResume (StateType resume_state)
452{
453 // At this point clear the completed plan stack.
454 m_completed_plan_stack.clear();
455 m_discarded_plan_stack.clear();
456
457 // If this thread stopped with a signal, work out what its resume state should
458 // be. Note if the thread resume state is already set, then don't override it,
459 // the user must have asked us to resume with some other signal.
460
461 if (GetResumeSignal() == LLDB_INVALID_SIGNAL_NUMBER)
462 {
463 Thread::StopInfo stop_info;
464 GetRawStopReason(&stop_info);
465
466 StopReason reason = stop_info.GetStopReason();
467 if (reason == eStopReasonSignal)
468 {
469 UnixSignals &signals = GetProcess().GetUnixSignals();
470 int32_t signo = stop_info.GetSignal();
471 if (!signals.GetShouldSuppress(signo))
472 {
473 SetResumeSignal(signo);
474 }
475 }
476 }
477
478 // Tell all the plans that we are about to resume in case they need to clear any state.
479 // We distinguish between the plan on the top of the stack and the lower
480 // plans in case a plan needs to do any special business before it runs.
481
482 ThreadPlan *plan_ptr = GetCurrentPlan();
483 plan_ptr->WillResume(resume_state, true);
484
485 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
486 {
487 plan_ptr->WillResume (resume_state, false);
488 }
489 return true;
490}
491
492void
493Thread::DidResume ()
494{
495 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
496}
497
498bool
499Thread::ShouldStop (Event* event_ptr)
500{
501 ThreadPlan *current_plan = GetCurrentPlan();
502 bool should_stop = true;
503
504 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
505 if (log)
506 {
507 StreamString s;
508 DumpThreadPlans(&s);
509 log->PutCString (s.GetData());
510 }
511
512 if (current_plan->PlanExplainsStop())
513 {
514 while (1)
515 {
516 should_stop = current_plan->ShouldStop(event_ptr);
517 if (current_plan->MischiefManaged())
518 {
519 if (should_stop)
520 current_plan->WillStop();
521
522 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
523 // Otherwise, see if the plan's parent wants to stop.
524
525 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
526 {
527 PopPlan();
528 break;
529 }
530 else
531 {
532
533 PopPlan();
534
535 current_plan = GetCurrentPlan();
536 if (current_plan == NULL)
537 {
538 break;
539 }
540 }
541
542 }
543 else
544 {
545 break;
546 }
547 }
548 }
549 else
550 {
551 // If the current plan doesn't explain the stop, then, find one that
552 // does and let it handle the situation.
553 ThreadPlan *plan_ptr = current_plan;
554 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
555 {
556 if (plan_ptr->PlanExplainsStop())
557 {
558 should_stop = plan_ptr->ShouldStop (event_ptr);
559 break;
560 }
561
562 }
563 }
564
565 return should_stop;
566}
567
568Vote
569Thread::ShouldReportStop (Event* event_ptr)
570{
571 StateType thread_state = GetResumeState ();
572 if (thread_state == eStateSuspended
573 || thread_state == eStateInvalid)
574 return eVoteNoOpinion;
575
576 if (m_completed_plan_stack.size() > 0)
577 {
578 // Don't use GetCompletedPlan here, since that suppresses private plans.
579 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
580 }
581 else
582 return GetCurrentPlan()->ShouldReportStop (event_ptr);
583}
584
585Vote
586Thread::ShouldReportRun (Event* event_ptr)
587{
588 StateType thread_state = GetResumeState ();
589 if (thread_state == eStateSuspended
590 || thread_state == eStateInvalid)
591 return eVoteNoOpinion;
592
593 if (m_completed_plan_stack.size() > 0)
594 {
595 // Don't use GetCompletedPlan here, since that suppresses private plans.
596 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
597 }
598 else
599 return GetCurrentPlan()->ShouldReportRun (event_ptr);
600}
601
602void
603Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
604{
605 if (thread_plan_sp)
606 {
607 if (thread_plan_sp->IsImmediate())
608 m_immediate_plan_stack.push_back (thread_plan_sp);
609 else
610 m_plan_stack.push_back (thread_plan_sp);
611
612 thread_plan_sp->DidPush();
613
614 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
615 if (log)
616 {
617 StreamString s;
618 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
619 log->Printf("Pushing plan: \"%s\" for thread: %d immediate: %s.",
620 s.GetData(),
621 thread_plan_sp->GetThread().GetID(),
622 thread_plan_sp->IsImmediate() ? "true" : "false");
623 }
624 }
625}
626
627void
628Thread::PopPlan ()
629{
630 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
631
632 if (!m_immediate_plan_stack.empty())
633 {
634 ThreadPlanSP &plan = m_immediate_plan_stack.back();
635 if (log)
636 {
637 log->Printf("Popping plan: \"%s\" for thread: %d immediate: true.", plan->GetName(), plan->GetThread().GetID());
638 }
639 plan->WillPop();
640 m_immediate_plan_stack.pop_back();
641 }
642 else if (m_plan_stack.empty())
643 return;
644 else
645 {
646 ThreadPlanSP &plan = m_plan_stack.back();
647 if (log)
648 {
649 log->Printf("Popping plan: \"%s\" for thread: 0x%x immediate: false.", plan->GetName(), plan->GetThread().GetID());
650 }
651 m_completed_plan_stack.push_back (plan);
652 plan->WillPop();
653 m_plan_stack.pop_back();
654 }
655}
656
657void
658Thread::DiscardPlan ()
659{
660 if (m_plan_stack.size() > 1)
661 {
662 ThreadPlanSP &plan = m_plan_stack.back();
663 m_discarded_plan_stack.push_back (plan);
664 plan->WillPop();
665 m_plan_stack.pop_back();
666 }
667}
668
669ThreadPlan *
670Thread::GetCurrentPlan ()
671{
672 if (!m_immediate_plan_stack.empty())
673 return m_immediate_plan_stack.back().get();
674 else if (m_plan_stack.empty())
675 return NULL;
676 else
677 return m_plan_stack.back().get();
678}
679
680ThreadPlanSP
681Thread::GetCompletedPlan ()
682{
683 ThreadPlanSP empty_plan_sp;
684 if (!m_completed_plan_stack.empty())
685 {
686 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
687 {
688 ThreadPlanSP completed_plan_sp;
689 completed_plan_sp = m_completed_plan_stack[i];
690 if (!completed_plan_sp->GetPrivate ())
691 return completed_plan_sp;
692 }
693 }
694 return empty_plan_sp;
695}
696
697bool
698Thread::IsThreadPlanDone (ThreadPlan *plan)
699{
700 ThreadPlanSP empty_plan_sp;
701 if (!m_completed_plan_stack.empty())
702 {
703 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
704 {
705 if (m_completed_plan_stack[i].get() == plan)
706 return true;
707 }
708 }
709 return false;
710}
711
712bool
713Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
714{
715 ThreadPlanSP empty_plan_sp;
716 if (!m_discarded_plan_stack.empty())
717 {
718 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
719 {
720 if (m_discarded_plan_stack[i].get() == plan)
721 return true;
722 }
723 }
724 return false;
725}
726
727ThreadPlan *
728Thread::GetPreviousPlan (ThreadPlan *current_plan)
729{
730 if (current_plan == NULL)
731 return NULL;
732
733 int stack_size = m_completed_plan_stack.size();
734 for (int i = stack_size - 1; i > 0; i--)
735 {
736 if (current_plan == m_completed_plan_stack[i].get())
737 return m_completed_plan_stack[i-1].get();
738 }
739
740 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
741 {
742 if (m_immediate_plan_stack.size() > 0)
743 return m_immediate_plan_stack.back().get();
744 else if (m_plan_stack.size() > 0)
745 return m_plan_stack.back().get();
746 else
747 return NULL;
748 }
749
750 stack_size = m_immediate_plan_stack.size();
751 for (int i = stack_size - 1; i > 0; i--)
752 {
753 if (current_plan == m_immediate_plan_stack[i].get())
754 return m_immediate_plan_stack[i-1].get();
755 }
756 if (stack_size > 0 && m_immediate_plan_stack[0].get() == current_plan)
757 {
758 if (m_plan_stack.size() > 0)
759 return m_plan_stack.back().get();
760 else
761 return NULL;
762 }
763
764 stack_size = m_plan_stack.size();
765 for (int i = stack_size - 1; i > 0; i--)
766 {
767 if (current_plan == m_plan_stack[i].get())
768 return m_plan_stack[i-1].get();
769 }
770 return NULL;
771}
772
773void
774Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
775{
776 if (abort_other_plans)
777 DiscardThreadPlans(true);
778
779 PushPlan (thread_plan_sp);
780}
781
782void
783Thread::DiscardThreadPlans(bool force)
784{
785 // FIXME: It is not always safe to just discard plans. Some, like the step over
786 // breakpoint trap can't be discarded in general (though you can if you plan to
787 // force a return from a function, for instance.
788 // For now I'm just not clearing immediate plans, but I need a way for plans to
789 // say they really need to be kept on, and then a way to override that. Humm...
790
791 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
792 if (log)
793 {
794 log->Printf("Discarding thread plans for thread: 0x%x: force %d.", GetID(), force);
795 }
796
797 if (force)
798 {
799 int stack_size = m_plan_stack.size();
800 for (int i = stack_size - 1; i > 0; i--)
801 {
802 DiscardPlan();
803 }
804 return;
805 }
806
807 while (1)
808 {
809
810 int master_plan_idx;
811 bool discard;
812
813 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
814 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
815 {
816 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
817 {
818 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
819 break;
820 }
821 }
822
823 if (discard)
824 {
825 // First pop all the dependent plans:
826 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
827 {
828
829 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
830 // for the plan leaves it in a state that it is safe to pop the plan
831 // with no more notice?
832 DiscardPlan();
833 }
834
835 // Now discard the master plan itself.
836 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
837 // discard it's dependent plans, but not it...
838 if (master_plan_idx > 0)
839 {
840 DiscardPlan();
841 }
842 }
843 else
844 {
845 // If the master plan doesn't want to get discarded, then we're done.
846 break;
847 }
848
849 }
850 // FIXME: What should we do about the immediate plans?
851}
852
853ThreadPlan *
854Thread::QueueFundamentalPlan (bool abort_other_plans)
855{
856 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
857 QueueThreadPlan (thread_plan_sp, abort_other_plans);
858 return thread_plan_sp.get();
859}
860
861ThreadPlan *
862Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
863{
864 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
865 QueueThreadPlan (thread_plan_sp, abort_other_plans);
866 return thread_plan_sp.get();
867}
868
869ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000870Thread::QueueThreadPlanForStepRange
871(
872 bool abort_other_plans,
873 StepType type,
874 const AddressRange &range,
875 const SymbolContext &addr_context,
876 lldb::RunMode stop_other_threads,
877 bool avoid_code_without_debug_info
878)
Chris Lattner24943d22010-06-08 16:52:24 +0000879{
880 ThreadPlanSP thread_plan_sp;
881 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000882 {
883 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
884 if (avoid_code_without_debug_info)
885 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
886 else
887 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
888 thread_plan_sp.reset (plan);
889 }
Chris Lattner24943d22010-06-08 16:52:24 +0000890 else
891 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
892
893 QueueThreadPlan (thread_plan_sp, abort_other_plans);
894 return thread_plan_sp.get();
895}
896
897
898ThreadPlan *
899Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
900{
901 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
902 QueueThreadPlan (thread_plan_sp, abort_other_plans);
903 return thread_plan_sp.get();
904}
905
906ThreadPlan *
907Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
908 bool stop_other_threads, Vote stop_vote, Vote run_vote)
909{
910 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
911 QueueThreadPlan (thread_plan_sp, abort_other_plans);
912 return thread_plan_sp.get();
913}
914
915ThreadPlan *
916Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
917{
918 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
919 if (thread_plan_sp.get() == NULL)
920 {
921 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
922 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
923 return false;
924 }
925 QueueThreadPlan (thread_plan_sp, abort_other_plans);
926 return thread_plan_sp.get();
927}
928
929ThreadPlan *
930Thread::QueueThreadPlanForContinue (bool abort_other_plans, bool stop_other_threads, Vote stop_vote, Vote run_vote, bool immediate)
931{
932 ThreadPlanSP thread_plan_sp (new ThreadPlanContinue (*this, stop_other_threads, stop_vote, run_vote, immediate));
933 QueueThreadPlan (thread_plan_sp, abort_other_plans);
934 return thread_plan_sp.get();
935}
936
937ThreadPlan *
938Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
939 Address& function,
940 lldb::addr_t arg,
941 bool stop_other_threads,
942 bool discard_on_error)
943{
944 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
945 QueueThreadPlan (thread_plan_sp, abort_other_plans);
946 return thread_plan_sp.get();
947}
948
949ThreadPlan *
950Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
951 Address& function,
952 ValueList &args,
953 bool stop_other_threads,
954 bool discard_on_error)
955{
956 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error));
957 QueueThreadPlan (thread_plan_sp, abort_other_plans);
958 return thread_plan_sp.get();
959}
960
961ThreadPlan *
962Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
963 Address &target_addr,
964 bool stop_other_threads)
965{
966 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
967 QueueThreadPlan (thread_plan_sp, abort_other_plans);
968 return thread_plan_sp.get();
969}
970
971ThreadPlan *
972Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
973 lldb::addr_t *address_list,
974 size_t num_addresses,
975 bool stop_other_threads)
976{
977 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
978 QueueThreadPlan (thread_plan_sp, abort_other_plans);
979 return thread_plan_sp.get();
980
981}
982
983uint32_t
984Thread::GetIndexID () const
985{
986 return m_index_id;
987}
988
989void
990Thread::DumpThreadPlans (lldb_private::Stream *s) const
991{
992 uint32_t stack_size = m_plan_stack.size();
993 s->Printf ("Plan Stack: %d elements.\n", stack_size);
994 for (int i = stack_size - 1; i > 0; i--)
995 {
996 s->Printf ("Element %d: ", i);
997 s->IndentMore();
998 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
999 s->IndentLess();
1000 s->EOL();
1001 }
1002
1003 stack_size = m_immediate_plan_stack.size();
1004 s->Printf ("Immediate Plan Stack: %d elements.\n", stack_size);
1005 for (int i = stack_size - 1; i > 0; i--)
1006 {
1007 s->Printf ("Element %d: ", i);
1008 s->IndentMore();
1009 m_immediate_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1010 s->IndentLess();
1011 s->EOL();
1012 }
1013
1014 stack_size = m_completed_plan_stack.size();
1015 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1016 for (int i = stack_size - 1; i > 0; i--)
1017 {
1018 s->Printf ("Element %d: ", i);
1019 s->IndentMore();
1020 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1021 s->IndentLess();
1022 s->EOL();
1023 }
1024
1025 stack_size = m_discarded_plan_stack.size();
1026 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1027 for (int i = stack_size - 1; i > 0; i--)
1028 {
1029 s->Printf ("Element %d: ", i);
1030 s->IndentMore();
1031 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1032 s->IndentLess();
1033 s->EOL();
1034 }
1035
1036}
1037
1038Target *
1039Thread::CalculateTarget ()
1040{
1041 return m_process.CalculateTarget();
1042}
1043
1044Process *
1045Thread::CalculateProcess ()
1046{
1047 return &m_process;
1048}
1049
1050Thread *
1051Thread::CalculateThread ()
1052{
1053 return this;
1054}
1055
1056StackFrame *
1057Thread::CalculateStackFrame ()
1058{
1059 return NULL;
1060}
1061
1062void
1063Thread::Calculate (ExecutionContext &exe_ctx)
1064{
1065 m_process.Calculate (exe_ctx);
1066 exe_ctx.thread = this;
1067 exe_ctx.frame = NULL;
1068}
1069
1070lldb::StackFrameSP
1071Thread::GetCurrentFrame ()
1072{
1073 return GetStackFrameAtIndex (m_frames.GetCurrentFrameIndex());
1074}
1075
1076uint32_t
1077Thread::SetCurrentFrame (lldb_private::StackFrame *frame)
1078{
1079 return m_frames.SetCurrentFrame(frame);
1080}
1081
1082void
1083Thread::SetCurrentFrameByIndex (uint32_t frame_idx)
1084{
1085 m_frames.SetCurrentFrameByIndex(frame_idx);
1086}
1087
1088void
1089Thread::DumpInfo
1090(
1091 Stream &strm,
1092 bool show_stop_reason,
1093 bool show_name,
1094 bool show_queue,
1095 uint32_t frame_idx
1096)
1097{
1098 strm.Printf("thread #%u: tid = 0x%4.4x", GetIndexID(), GetID());
1099
1100 if (frame_idx != LLDB_INVALID_INDEX32)
1101 {
1102 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
1103 if (frame_sp)
1104 {
1105 strm.PutCString(", ");
1106 frame_sp->Dump (&strm, false);
1107 }
1108 }
1109
1110 if (show_stop_reason)
1111 {
1112 Thread::StopInfo thread_stop_info;
1113 if (GetStopInfo(&thread_stop_info))
1114 {
1115 if (thread_stop_info.GetStopReason() != eStopReasonNone)
1116 {
1117 strm.PutCString(", stop reason = ");
1118 thread_stop_info.Dump(&strm);
1119 }
1120 }
1121 }
1122
1123 if (show_name)
1124 {
1125 const char *name = GetName();
1126 if (name && name[0])
1127 strm.Printf(", name = %s", name);
1128 }
1129
1130 if (show_queue)
1131 {
1132 const char *queue = GetQueueName();
1133 if (queue && queue[0])
1134 strm.Printf(", queue = %s", queue);
1135 }
1136}
1137
1138lldb::ThreadSP
1139Thread::GetSP ()
1140{
1141 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
1142}