blob: cd4f8247c3d0d551a282a77d4b22c43fa9c329d9 [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"
Jim Ingham20594b12010-09-08 03:14:33 +000015#include "lldb/Core/RegularExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Host/Host.h"
17#include "lldb/Target/DynamicLoader.h"
18#include "lldb/Target/ExecutionContext.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000021#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000022#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Target/Thread.h"
24#include "lldb/Target/ThreadPlan.h"
25#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/ThreadPlanBase.h"
27#include "lldb/Target/ThreadPlanStepInstruction.h"
28#include "lldb/Target/ThreadPlanStepOut.h"
29#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
30#include "lldb/Target/ThreadPlanStepThrough.h"
31#include "lldb/Target/ThreadPlanStepInRange.h"
32#include "lldb/Target/ThreadPlanStepOverRange.h"
33#include "lldb/Target/ThreadPlanRunToAddress.h"
34#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000035#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000036#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41Thread::Thread (Process &process, lldb::tid_t tid) :
42 UserID (tid),
Jim Ingham20594b12010-09-08 03:14:33 +000043 ThreadInstanceSettings (*(Thread::GetSettingsController().get())),
Benjamin Kramer36a08102010-07-16 12:32:33 +000044 m_process (process),
Greg Clayton643ee732010-08-04 01:40:35 +000045 m_public_stop_info_sp (),
46 m_actual_stop_info_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_index_id (process.GetNextThreadIndexID ()),
48 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000049 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +000050 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000051 m_plan_stack (),
52 m_immediate_plan_stack(),
53 m_completed_plan_stack(),
Greg Claytonf40e3082010-08-26 02:28:22 +000054 m_curr_frames_ap (),
Chris Lattner24943d22010-06-08 16:52:24 +000055 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000056 m_resume_state (eStateRunning),
57 m_unwinder_ap ()
58
Chris Lattner24943d22010-06-08 16:52:24 +000059{
60 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
61 if (log)
62 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
63
64 QueueFundamentalPlan(true);
65}
66
67
68Thread::~Thread()
69{
70 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
71 if (log)
72 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
73}
74
75int
76Thread::GetResumeSignal () const
77{
78 return m_resume_signal;
79}
80
81void
82Thread::SetResumeSignal (int signal)
83{
84 m_resume_signal = signal;
85}
86
87StateType
88Thread::GetResumeState () const
89{
90 return m_resume_state;
91}
92
93void
94Thread::SetResumeState (StateType state)
95{
96 m_resume_state = state;
97}
98
Greg Clayton643ee732010-08-04 01:40:35 +000099StopInfo *
100Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000101{
Greg Clayton643ee732010-08-04 01:40:35 +0000102 if (m_public_stop_info_sp.get() == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000103 {
Greg Clayton643ee732010-08-04 01:40:35 +0000104 ThreadPlanSP plan_sp (GetCompletedPlan());
105 if (plan_sp)
106 m_public_stop_info_sp = StopInfo::CreateStopReasonWithPlan (plan_sp);
107 else
108 m_public_stop_info_sp = GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000109 }
Greg Clayton643ee732010-08-04 01:40:35 +0000110 return m_public_stop_info_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000111}
112
113bool
114Thread::ThreadStoppedForAReason (void)
115{
Greg Clayton643ee732010-08-04 01:40:35 +0000116 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000117}
118
119StateType
120Thread::GetState() const
121{
122 // If any other threads access this we will need a mutex for it
123 Mutex::Locker locker(m_state_mutex);
124 return m_state;
125}
126
127void
128Thread::SetState(StateType state)
129{
130 Mutex::Locker locker(m_state_mutex);
131 m_state = state;
132}
133
134void
135Thread::WillStop()
136{
137 ThreadPlan *current_plan = GetCurrentPlan();
138
139 // FIXME: I may decide to disallow threads with no plans. In which
140 // case this should go to an assert.
141
142 if (!current_plan)
143 return;
144
145 current_plan->WillStop();
146}
147
148void
149Thread::SetupForResume ()
150{
151 if (GetResumeState() != eStateSuspended)
152 {
153
154 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
155 // telling the current plan it will resume, since we might change what the current
156 // plan is.
157
158 lldb::addr_t pc = GetRegisterContext()->GetPC();
159 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
160 if (bp_site_sp && bp_site_sp->IsEnabled())
161 {
162 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
163 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000164
165 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000166
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000167 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
168 {
169 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
170 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000171 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000172 ThreadPlanSP step_bp_plan_sp;
173 step_bp_plan->SetPrivate (true);
174
Chris Lattner24943d22010-06-08 16:52:24 +0000175 if (GetCurrentPlan()->RunState() != eStateStepping)
176 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000177 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000178 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000179 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 QueueThreadPlan (step_bp_plan_sp, false);
181 }
182 }
183 }
184 }
185}
186
187bool
188Thread::WillResume (StateType resume_state)
189{
190 // At this point clear the completed plan stack.
191 m_completed_plan_stack.clear();
192 m_discarded_plan_stack.clear();
193
Greg Clayton643ee732010-08-04 01:40:35 +0000194 StopInfo *stop_info = GetPrivateStopReason().get();
195 if (stop_info)
196 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000197
198 // Tell all the plans that we are about to resume in case they need to clear any state.
199 // We distinguish between the plan on the top of the stack and the lower
200 // plans in case a plan needs to do any special business before it runs.
201
202 ThreadPlan *plan_ptr = GetCurrentPlan();
203 plan_ptr->WillResume(resume_state, true);
204
205 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
206 {
207 plan_ptr->WillResume (resume_state, false);
208 }
Greg Clayton643ee732010-08-04 01:40:35 +0000209
210 m_public_stop_info_sp.reset();
211 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000212 return true;
213}
214
215void
216Thread::DidResume ()
217{
218 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
219}
220
221bool
222Thread::ShouldStop (Event* event_ptr)
223{
224 ThreadPlan *current_plan = GetCurrentPlan();
225 bool should_stop = true;
226
227 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
228 if (log)
229 {
230 StreamString s;
231 DumpThreadPlans(&s);
232 log->PutCString (s.GetData());
233 }
234
235 if (current_plan->PlanExplainsStop())
236 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000237 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000238 while (1)
239 {
240 should_stop = current_plan->ShouldStop(event_ptr);
241 if (current_plan->MischiefManaged())
242 {
243 if (should_stop)
244 current_plan->WillStop();
245
246 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
247 // Otherwise, see if the plan's parent wants to stop.
248
249 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
250 {
251 PopPlan();
252 break;
253 }
254 else
255 {
256
257 PopPlan();
258
259 current_plan = GetCurrentPlan();
260 if (current_plan == NULL)
261 {
262 break;
263 }
264 }
265
266 }
267 else
268 {
269 break;
270 }
271 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000272 if (over_ride_stop)
273 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000274 }
275 else
276 {
277 // If the current plan doesn't explain the stop, then, find one that
278 // does and let it handle the situation.
279 ThreadPlan *plan_ptr = current_plan;
280 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
281 {
282 if (plan_ptr->PlanExplainsStop())
283 {
284 should_stop = plan_ptr->ShouldStop (event_ptr);
285 break;
286 }
287
288 }
289 }
290
291 return should_stop;
292}
293
294Vote
295Thread::ShouldReportStop (Event* event_ptr)
296{
297 StateType thread_state = GetResumeState ();
Greg Clayton5205f0b2010-09-03 17:10:42 +0000298 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
299
300 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
301 {
302 if (log)
303 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Chris Lattner24943d22010-06-08 16:52:24 +0000304 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000305 }
Chris Lattner24943d22010-06-08 16:52:24 +0000306
307 if (m_completed_plan_stack.size() > 0)
308 {
309 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000310 if (log)
311 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for complete stack's back plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000312 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
313 }
314 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000315 {
316 if (log)
317 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000318 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000319 }
Chris Lattner24943d22010-06-08 16:52:24 +0000320}
321
322Vote
323Thread::ShouldReportRun (Event* event_ptr)
324{
325 StateType thread_state = GetResumeState ();
326 if (thread_state == eStateSuspended
327 || thread_state == eStateInvalid)
328 return eVoteNoOpinion;
329
330 if (m_completed_plan_stack.size() > 0)
331 {
332 // Don't use GetCompletedPlan here, since that suppresses private plans.
333 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
334 }
335 else
336 return GetCurrentPlan()->ShouldReportRun (event_ptr);
337}
338
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000339bool
340Thread::MatchesSpec (const ThreadSpec *spec)
341{
342 if (spec == NULL)
343 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000344
Jim Ingham649492b2010-06-18 01:00:58 +0000345 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000346}
347
Chris Lattner24943d22010-06-08 16:52:24 +0000348void
349Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
350{
351 if (thread_plan_sp)
352 {
353 if (thread_plan_sp->IsImmediate())
354 m_immediate_plan_stack.push_back (thread_plan_sp);
355 else
356 m_plan_stack.push_back (thread_plan_sp);
357
358 thread_plan_sp->DidPush();
359
360 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
361 if (log)
362 {
363 StreamString s;
364 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Claytonf04d6612010-09-03 22:45:01 +0000365 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x, immediate = %s.",
Chris Lattner24943d22010-06-08 16:52:24 +0000366 s.GetData(),
367 thread_plan_sp->GetThread().GetID(),
368 thread_plan_sp->IsImmediate() ? "true" : "false");
369 }
370 }
371}
372
373void
374Thread::PopPlan ()
375{
376 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
377
378 if (!m_immediate_plan_stack.empty())
379 {
380 ThreadPlanSP &plan = m_immediate_plan_stack.back();
381 if (log)
382 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000383 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = true.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000384 }
385 plan->WillPop();
386 m_immediate_plan_stack.pop_back();
387 }
388 else if (m_plan_stack.empty())
389 return;
390 else
391 {
392 ThreadPlanSP &plan = m_plan_stack.back();
393 if (log)
394 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000395 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = false.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000396 }
397 m_completed_plan_stack.push_back (plan);
398 plan->WillPop();
399 m_plan_stack.pop_back();
400 }
401}
402
403void
404Thread::DiscardPlan ()
405{
406 if (m_plan_stack.size() > 1)
407 {
408 ThreadPlanSP &plan = m_plan_stack.back();
409 m_discarded_plan_stack.push_back (plan);
410 plan->WillPop();
411 m_plan_stack.pop_back();
412 }
413}
414
415ThreadPlan *
416Thread::GetCurrentPlan ()
417{
418 if (!m_immediate_plan_stack.empty())
419 return m_immediate_plan_stack.back().get();
420 else if (m_plan_stack.empty())
421 return NULL;
422 else
423 return m_plan_stack.back().get();
424}
425
426ThreadPlanSP
427Thread::GetCompletedPlan ()
428{
429 ThreadPlanSP empty_plan_sp;
430 if (!m_completed_plan_stack.empty())
431 {
432 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
433 {
434 ThreadPlanSP completed_plan_sp;
435 completed_plan_sp = m_completed_plan_stack[i];
436 if (!completed_plan_sp->GetPrivate ())
437 return completed_plan_sp;
438 }
439 }
440 return empty_plan_sp;
441}
442
443bool
444Thread::IsThreadPlanDone (ThreadPlan *plan)
445{
446 ThreadPlanSP empty_plan_sp;
447 if (!m_completed_plan_stack.empty())
448 {
449 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
450 {
451 if (m_completed_plan_stack[i].get() == plan)
452 return true;
453 }
454 }
455 return false;
456}
457
458bool
459Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
460{
461 ThreadPlanSP empty_plan_sp;
462 if (!m_discarded_plan_stack.empty())
463 {
464 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
465 {
466 if (m_discarded_plan_stack[i].get() == plan)
467 return true;
468 }
469 }
470 return false;
471}
472
473ThreadPlan *
474Thread::GetPreviousPlan (ThreadPlan *current_plan)
475{
476 if (current_plan == NULL)
477 return NULL;
478
479 int stack_size = m_completed_plan_stack.size();
480 for (int i = stack_size - 1; i > 0; i--)
481 {
482 if (current_plan == m_completed_plan_stack[i].get())
483 return m_completed_plan_stack[i-1].get();
484 }
485
486 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
487 {
488 if (m_immediate_plan_stack.size() > 0)
489 return m_immediate_plan_stack.back().get();
490 else if (m_plan_stack.size() > 0)
491 return m_plan_stack.back().get();
492 else
493 return NULL;
494 }
495
496 stack_size = m_immediate_plan_stack.size();
497 for (int i = stack_size - 1; i > 0; i--)
498 {
499 if (current_plan == m_immediate_plan_stack[i].get())
500 return m_immediate_plan_stack[i-1].get();
501 }
502 if (stack_size > 0 && m_immediate_plan_stack[0].get() == current_plan)
503 {
504 if (m_plan_stack.size() > 0)
505 return m_plan_stack.back().get();
506 else
507 return NULL;
508 }
509
510 stack_size = m_plan_stack.size();
511 for (int i = stack_size - 1; i > 0; i--)
512 {
513 if (current_plan == m_plan_stack[i].get())
514 return m_plan_stack[i-1].get();
515 }
516 return NULL;
517}
518
519void
520Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
521{
522 if (abort_other_plans)
523 DiscardThreadPlans(true);
524
525 PushPlan (thread_plan_sp);
526}
527
528void
529Thread::DiscardThreadPlans(bool force)
530{
531 // FIXME: It is not always safe to just discard plans. Some, like the step over
532 // breakpoint trap can't be discarded in general (though you can if you plan to
533 // force a return from a function, for instance.
534 // For now I'm just not clearing immediate plans, but I need a way for plans to
535 // say they really need to be kept on, and then a way to override that. Humm...
536
537 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
538 if (log)
539 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000540 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000541 }
542
543 if (force)
544 {
545 int stack_size = m_plan_stack.size();
546 for (int i = stack_size - 1; i > 0; i--)
547 {
548 DiscardPlan();
549 }
550 return;
551 }
552
553 while (1)
554 {
555
556 int master_plan_idx;
557 bool discard;
558
559 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
560 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
561 {
562 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
563 {
564 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
565 break;
566 }
567 }
568
569 if (discard)
570 {
571 // First pop all the dependent plans:
572 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
573 {
574
575 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
576 // for the plan leaves it in a state that it is safe to pop the plan
577 // with no more notice?
578 DiscardPlan();
579 }
580
581 // Now discard the master plan itself.
582 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
583 // discard it's dependent plans, but not it...
584 if (master_plan_idx > 0)
585 {
586 DiscardPlan();
587 }
588 }
589 else
590 {
591 // If the master plan doesn't want to get discarded, then we're done.
592 break;
593 }
594
595 }
596 // FIXME: What should we do about the immediate plans?
597}
598
599ThreadPlan *
600Thread::QueueFundamentalPlan (bool abort_other_plans)
601{
602 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
603 QueueThreadPlan (thread_plan_sp, abort_other_plans);
604 return thread_plan_sp.get();
605}
606
607ThreadPlan *
608Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
609{
610 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
611 QueueThreadPlan (thread_plan_sp, abort_other_plans);
612 return thread_plan_sp.get();
613}
614
615ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000616Thread::QueueThreadPlanForStepRange
617(
618 bool abort_other_plans,
619 StepType type,
620 const AddressRange &range,
621 const SymbolContext &addr_context,
622 lldb::RunMode stop_other_threads,
623 bool avoid_code_without_debug_info
624)
Chris Lattner24943d22010-06-08 16:52:24 +0000625{
626 ThreadPlanSP thread_plan_sp;
627 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000628 {
629 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
630 if (avoid_code_without_debug_info)
631 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
632 else
633 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
634 thread_plan_sp.reset (plan);
635 }
Chris Lattner24943d22010-06-08 16:52:24 +0000636 else
637 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
638
639 QueueThreadPlan (thread_plan_sp, abort_other_plans);
640 return thread_plan_sp.get();
641}
642
643
644ThreadPlan *
645Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
646{
647 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
648 QueueThreadPlan (thread_plan_sp, abort_other_plans);
649 return thread_plan_sp.get();
650}
651
652ThreadPlan *
653Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
654 bool stop_other_threads, Vote stop_vote, Vote run_vote)
655{
656 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
657 QueueThreadPlan (thread_plan_sp, abort_other_plans);
658 return thread_plan_sp.get();
659}
660
661ThreadPlan *
662Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
663{
664 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
665 if (thread_plan_sp.get() == NULL)
666 {
667 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
668 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000669 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000670 }
671 QueueThreadPlan (thread_plan_sp, abort_other_plans);
672 return thread_plan_sp.get();
673}
674
675ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000676Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
677 Address& function,
678 lldb::addr_t arg,
679 bool stop_other_threads,
680 bool discard_on_error)
681{
682 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
683 QueueThreadPlan (thread_plan_sp, abort_other_plans);
684 return thread_plan_sp.get();
685}
686
687ThreadPlan *
688Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
689 Address& function,
690 ValueList &args,
691 bool stop_other_threads,
692 bool discard_on_error)
693{
694 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error));
695 QueueThreadPlan (thread_plan_sp, abort_other_plans);
696 return thread_plan_sp.get();
697}
698
699ThreadPlan *
700Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
701 Address &target_addr,
702 bool stop_other_threads)
703{
704 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
705 QueueThreadPlan (thread_plan_sp, abort_other_plans);
706 return thread_plan_sp.get();
707}
708
709ThreadPlan *
710Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
711 lldb::addr_t *address_list,
712 size_t num_addresses,
713 bool stop_other_threads)
714{
715 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
716 QueueThreadPlan (thread_plan_sp, abort_other_plans);
717 return thread_plan_sp.get();
718
719}
720
721uint32_t
722Thread::GetIndexID () const
723{
724 return m_index_id;
725}
726
727void
728Thread::DumpThreadPlans (lldb_private::Stream *s) const
729{
730 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000731 int i;
732 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
733 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000734 {
735 s->Printf ("Element %d: ", i);
736 s->IndentMore();
737 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
738 s->IndentLess();
739 s->EOL();
740 }
741
742 stack_size = m_immediate_plan_stack.size();
743 s->Printf ("Immediate Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000744 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000745 {
746 s->Printf ("Element %d: ", i);
747 s->IndentMore();
748 m_immediate_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
749 s->IndentLess();
750 s->EOL();
751 }
752
753 stack_size = m_completed_plan_stack.size();
754 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000755 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000756 {
757 s->Printf ("Element %d: ", i);
758 s->IndentMore();
759 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
760 s->IndentLess();
761 s->EOL();
762 }
763
764 stack_size = m_discarded_plan_stack.size();
765 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000766 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000767 {
768 s->Printf ("Element %d: ", i);
769 s->IndentMore();
770 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
771 s->IndentLess();
772 s->EOL();
773 }
774
775}
776
777Target *
778Thread::CalculateTarget ()
779{
780 return m_process.CalculateTarget();
781}
782
783Process *
784Thread::CalculateProcess ()
785{
786 return &m_process;
787}
788
789Thread *
790Thread::CalculateThread ()
791{
792 return this;
793}
794
795StackFrame *
796Thread::CalculateStackFrame ()
797{
798 return NULL;
799}
800
801void
802Thread::Calculate (ExecutionContext &exe_ctx)
803{
804 m_process.Calculate (exe_ctx);
805 exe_ctx.thread = this;
806 exe_ctx.frame = NULL;
807}
808
Greg Clayton782b9cc2010-08-25 00:35:26 +0000809
810StackFrameList &
811Thread::GetStackFrameList ()
812{
Greg Claytonf40e3082010-08-26 02:28:22 +0000813 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000814 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000815 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000816}
817
818
819
Jim Ingham71219082010-08-12 02:14:28 +0000820uint32_t
821Thread::GetStackFrameCount()
822{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000823 return GetStackFrameList().GetNumFrames();
824}
Greg Clayton33ed1702010-08-24 00:45:41 +0000825
Greg Clayton33ed1702010-08-24 00:45:41 +0000826
Greg Clayton782b9cc2010-08-25 00:35:26 +0000827void
828Thread::ClearStackFrames ()
829{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000830 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
831 m_prev_frames_sp.reset (m_curr_frames_ap.release());
832 else
833 m_curr_frames_ap.release();
834
835// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
836// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000837}
838
839lldb::StackFrameSP
840Thread::GetStackFrameAtIndex (uint32_t idx)
841{
Greg Clayton72b71582010-09-02 21:44:10 +0000842 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000843}
844
Chris Lattner24943d22010-06-08 16:52:24 +0000845lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000846Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000847{
Jim Inghamc8332952010-08-26 21:32:51 +0000848 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000849}
850
851uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000852Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000853{
Jim Inghamc8332952010-08-26 21:32:51 +0000854 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000855}
856
857void
Jim Inghamc8332952010-08-26 21:32:51 +0000858Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000859{
Jim Inghamc8332952010-08-26 21:32:51 +0000860 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000861}
862
863void
864Thread::DumpInfo
865(
866 Stream &strm,
867 bool show_stop_reason,
868 bool show_name,
869 bool show_queue,
Greg Clayton33ed1702010-08-24 00:45:41 +0000870 uint32_t idx
Chris Lattner24943d22010-06-08 16:52:24 +0000871)
872{
873 strm.Printf("thread #%u: tid = 0x%4.4x", GetIndexID(), GetID());
874
Greg Clayton33ed1702010-08-24 00:45:41 +0000875 if (idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000876 {
Greg Clayton33ed1702010-08-24 00:45:41 +0000877 StackFrameSP frame_sp(GetStackFrameAtIndex (idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000878 if (frame_sp)
879 {
880 strm.PutCString(", ");
Greg Clayton72b71582010-09-02 21:44:10 +0000881 frame_sp->Dump (&strm, false, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000882 }
883 }
884
885 if (show_stop_reason)
886 {
Greg Clayton643ee732010-08-04 01:40:35 +0000887 StopInfo *stop_info = GetStopInfo();
888
889 if (stop_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000890 {
Greg Clayton643ee732010-08-04 01:40:35 +0000891 const char *stop_description = stop_info->GetDescription();
892 if (stop_description)
893 strm.Printf (", stop reason = %s", stop_description);
Chris Lattner24943d22010-06-08 16:52:24 +0000894 }
895 }
896
897 if (show_name)
898 {
899 const char *name = GetName();
900 if (name && name[0])
901 strm.Printf(", name = %s", name);
902 }
903
904 if (show_queue)
905 {
906 const char *queue = GetQueueName();
907 if (queue && queue[0])
908 strm.Printf(", queue = %s", queue);
909 }
910}
911
912lldb::ThreadSP
913Thread::GetSP ()
914{
915 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
916}
Jim Ingham20594b12010-09-08 03:14:33 +0000917
918lldb::UserSettingsControllerSP
919Thread::GetSettingsController (bool finish)
920{
921 static UserSettingsControllerSP g_settings_controller (new ThreadSettingsController);
922 static bool initialized = false;
923
924 if (!initialized)
925 {
926 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
927 Thread::ThreadSettingsController::global_settings_table,
928 Thread::ThreadSettingsController::instance_settings_table);
929 }
930
931 if (finish)
932 {
933 UserSettingsController::FinalizeSettingsController (g_settings_controller);
934 g_settings_controller.reset();
935 initialized = false;
936 }
937
938 return g_settings_controller;
939}
940
941//--------------------------------------------------------------
942// class Thread::ThreadSettingsController
943//--------------------------------------------------------------
944
945Thread::ThreadSettingsController::ThreadSettingsController () :
946 UserSettingsController ("thread", Process::GetSettingsController())
947{
Caroline Tice004afcb2010-09-08 17:48:55 +0000948 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
949 InstanceSettings::GetDefaultName().AsCString()));
Jim Ingham20594b12010-09-08 03:14:33 +0000950}
951
952Thread::ThreadSettingsController::~ThreadSettingsController ()
953{
954}
955
956lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +0000957Thread::ThreadSettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham20594b12010-09-08 03:14:33 +0000958{
Caroline Tice004afcb2010-09-08 17:48:55 +0000959 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*(Thread::GetSettingsController().get()),
960 false, instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000961 lldb::InstanceSettingsSP new_settings_sp (new_settings);
962 return new_settings_sp;
963}
964
965//--------------------------------------------------------------
966// class ThreadInstanceSettings
967//--------------------------------------------------------------
968
Caroline Tice004afcb2010-09-08 17:48:55 +0000969ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Caroline Tice75b11a32010-09-16 19:05:55 +0000970 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance),
Jim Ingham20594b12010-09-08 03:14:33 +0000971 m_avoid_regexp_ap ()
972{
Caroline Tice396704b2010-09-09 18:26:37 +0000973 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
974 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
975 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice75b11a32010-09-16 19:05:55 +0000976 // This is true for CreateInstanceName() too.
977
978 if (GetInstanceName() == InstanceSettings::InvalidName())
979 {
980 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
981 m_owner.RegisterInstanceSettings (this);
982 }
Caroline Tice396704b2010-09-09 18:26:37 +0000983
984 if (live_instance)
Jim Ingham20594b12010-09-08 03:14:33 +0000985 {
986 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
987 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +0000988 //m_owner.RemovePendingSettings (m_instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000989 }
990}
991
992ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
993 InstanceSettings (*(Thread::GetSettingsController().get()), CreateInstanceName().AsCString()),
994 m_avoid_regexp_ap ()
995{
996 if (m_instance_name != InstanceSettings::GetDefaultName())
997 {
998 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
999 CopyInstanceSettings (pending_settings,false);
1000 m_owner.RemovePendingSettings (m_instance_name);
1001 }
1002 if (rhs.m_avoid_regexp_ap.get() != NULL)
1003 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1004}
1005
1006ThreadInstanceSettings::~ThreadInstanceSettings ()
1007{
1008}
1009
1010ThreadInstanceSettings&
1011ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1012{
1013 if (this != &rhs)
1014 {
1015 if (rhs.m_avoid_regexp_ap.get() != NULL)
1016 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1017 else
1018 m_avoid_regexp_ap.reset(NULL);
1019 }
1020
1021 return *this;
1022}
1023
1024
1025void
1026ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1027 const char *index_value,
1028 const char *value,
1029 const ConstString &instance_name,
1030 const SettingEntry &entry,
1031 lldb::VarSetOperationType op,
1032 Error &err,
1033 bool pending)
1034{
1035 if (var_name == StepAvoidRegexpVarName())
1036 {
1037 std::string regexp_text;
1038 if (m_avoid_regexp_ap.get() != NULL)
1039 regexp_text.append (m_avoid_regexp_ap->GetText());
1040 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1041 if (regexp_text.empty())
1042 m_avoid_regexp_ap.reset();
1043 else
1044 {
1045 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1046
1047 }
1048 }
1049}
1050
1051void
1052ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1053 bool pending)
1054{
1055 if (new_settings.get() == NULL)
1056 return;
1057
1058 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1059 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1060 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1061 else
1062 m_avoid_regexp_ap.reset ();
1063}
1064
Caroline Ticebcb5b452010-09-20 21:37:42 +00001065bool
Jim Ingham20594b12010-09-08 03:14:33 +00001066ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001067 const ConstString &var_name,
1068 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001069 Error *err)
Jim Ingham20594b12010-09-08 03:14:33 +00001070{
1071 if (var_name == StepAvoidRegexpVarName())
1072 {
1073 if (m_avoid_regexp_ap.get() != NULL)
1074 {
1075 std::string regexp_text("\"");
1076 regexp_text.append(m_avoid_regexp_ap->GetText());
1077 regexp_text.append ("\"");
1078 value.AppendString (regexp_text.c_str());
1079 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001080
Jim Ingham20594b12010-09-08 03:14:33 +00001081 }
1082 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00001083 {
1084 if (err)
1085 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1086 return false;
1087 }
1088 return true;
Jim Ingham20594b12010-09-08 03:14:33 +00001089}
1090
Jim Ingham20594b12010-09-08 03:14:33 +00001091const ConstString
1092ThreadInstanceSettings::CreateInstanceName ()
1093{
1094 static int instance_count = 1;
1095 StreamString sstr;
1096
1097 sstr.Printf ("thread_%d", instance_count);
1098 ++instance_count;
1099
1100 const ConstString ret_val (sstr.GetData());
1101 return ret_val;
1102}
1103
1104const ConstString &
1105ThreadInstanceSettings::StepAvoidRegexpVarName ()
1106{
1107 static ConstString run_args_var_name ("step-avoid-regexp");
1108
1109 return run_args_var_name;
1110}
1111
1112//--------------------------------------------------
1113// ThreadSettingsController Variable Tables
1114//--------------------------------------------------
1115
1116SettingEntry
1117Thread::ThreadSettingsController::global_settings_table[] =
1118{
1119 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1120 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1121};
1122
1123
1124SettingEntry
1125Thread::ThreadSettingsController::instance_settings_table[] =
1126{
1127 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1128 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1129 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1130};
1131