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