blob: d45f45ccfd8773f1d357238ec72846dcd93b03f8 [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"
Greg Claytona830adb2010-10-04 01:05:56 +000012#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000016#include "lldb/Core/RegularExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Host/Host.h"
18#include "lldb/Target/DynamicLoader.h"
19#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000023#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000024#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlan.h"
27#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Target/ThreadPlanBase.h"
29#include "lldb/Target/ThreadPlanStepInstruction.h"
30#include "lldb/Target/ThreadPlanStepOut.h"
31#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
32#include "lldb/Target/ThreadPlanStepThrough.h"
33#include "lldb/Target/ThreadPlanStepInRange.h"
34#include "lldb/Target/ThreadPlanStepOverRange.h"
35#include "lldb/Target/ThreadPlanRunToAddress.h"
36#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000037#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000038#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
43Thread::Thread (Process &process, lldb::tid_t tid) :
44 UserID (tid),
Jim Ingham20594b12010-09-08 03:14:33 +000045 ThreadInstanceSettings (*(Thread::GetSettingsController().get())),
Benjamin Kramer36a08102010-07-16 12:32:33 +000046 m_process (process),
Greg Clayton643ee732010-08-04 01:40:35 +000047 m_public_stop_info_sp (),
48 m_actual_stop_info_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000049 m_index_id (process.GetNextThreadIndexID ()),
50 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000051 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +000052 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000053 m_plan_stack (),
54 m_immediate_plan_stack(),
55 m_completed_plan_stack(),
Greg Claytonf40e3082010-08-26 02:28:22 +000056 m_curr_frames_ap (),
Chris Lattner24943d22010-06-08 16:52:24 +000057 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000058 m_resume_state (eStateRunning),
59 m_unwinder_ap ()
60
Chris Lattner24943d22010-06-08 16:52:24 +000061{
62 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
63 if (log)
64 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
65
66 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000067 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70
71Thread::~Thread()
72{
73 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
74 if (log)
75 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
76}
77
78int
79Thread::GetResumeSignal () const
80{
81 return m_resume_signal;
82}
83
84void
85Thread::SetResumeSignal (int signal)
86{
87 m_resume_signal = signal;
88}
89
90StateType
91Thread::GetResumeState () const
92{
93 return m_resume_state;
94}
95
96void
97Thread::SetResumeState (StateType state)
98{
99 m_resume_state = state;
100}
101
Greg Clayton643ee732010-08-04 01:40:35 +0000102StopInfo *
103Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000104{
Greg Clayton643ee732010-08-04 01:40:35 +0000105 if (m_public_stop_info_sp.get() == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000106 {
Greg Clayton643ee732010-08-04 01:40:35 +0000107 ThreadPlanSP plan_sp (GetCompletedPlan());
108 if (plan_sp)
109 m_public_stop_info_sp = StopInfo::CreateStopReasonWithPlan (plan_sp);
110 else
111 m_public_stop_info_sp = GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000112 }
Greg Clayton643ee732010-08-04 01:40:35 +0000113 return m_public_stop_info_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000114}
115
116bool
117Thread::ThreadStoppedForAReason (void)
118{
Greg Clayton643ee732010-08-04 01:40:35 +0000119 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000120}
121
122StateType
123Thread::GetState() const
124{
125 // If any other threads access this we will need a mutex for it
126 Mutex::Locker locker(m_state_mutex);
127 return m_state;
128}
129
130void
131Thread::SetState(StateType state)
132{
133 Mutex::Locker locker(m_state_mutex);
134 m_state = state;
135}
136
137void
138Thread::WillStop()
139{
140 ThreadPlan *current_plan = GetCurrentPlan();
141
142 // FIXME: I may decide to disallow threads with no plans. In which
143 // case this should go to an assert.
144
145 if (!current_plan)
146 return;
147
148 current_plan->WillStop();
149}
150
151void
152Thread::SetupForResume ()
153{
154 if (GetResumeState() != eStateSuspended)
155 {
156
157 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
158 // telling the current plan it will resume, since we might change what the current
159 // plan is.
160
161 lldb::addr_t pc = GetRegisterContext()->GetPC();
162 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
163 if (bp_site_sp && bp_site_sp->IsEnabled())
164 {
165 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
166 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000167
168 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000169
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000170 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
171 {
172 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
173 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000174 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000175 ThreadPlanSP step_bp_plan_sp;
176 step_bp_plan->SetPrivate (true);
177
Chris Lattner24943d22010-06-08 16:52:24 +0000178 if (GetCurrentPlan()->RunState() != eStateStepping)
179 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000180 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000181 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000182 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000183 QueueThreadPlan (step_bp_plan_sp, false);
184 }
185 }
186 }
187 }
188}
189
190bool
191Thread::WillResume (StateType resume_state)
192{
193 // At this point clear the completed plan stack.
194 m_completed_plan_stack.clear();
195 m_discarded_plan_stack.clear();
196
Greg Clayton643ee732010-08-04 01:40:35 +0000197 StopInfo *stop_info = GetPrivateStopReason().get();
198 if (stop_info)
199 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000200
201 // Tell all the plans that we are about to resume in case they need to clear any state.
202 // We distinguish between the plan on the top of the stack and the lower
203 // plans in case a plan needs to do any special business before it runs.
204
205 ThreadPlan *plan_ptr = GetCurrentPlan();
206 plan_ptr->WillResume(resume_state, true);
207
208 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
209 {
210 plan_ptr->WillResume (resume_state, false);
211 }
Greg Clayton643ee732010-08-04 01:40:35 +0000212
213 m_public_stop_info_sp.reset();
214 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000215 return true;
216}
217
218void
219Thread::DidResume ()
220{
221 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
222}
223
224bool
225Thread::ShouldStop (Event* event_ptr)
226{
227 ThreadPlan *current_plan = GetCurrentPlan();
228 bool should_stop = true;
229
230 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
231 if (log)
232 {
233 StreamString s;
234 DumpThreadPlans(&s);
235 log->PutCString (s.GetData());
236 }
237
238 if (current_plan->PlanExplainsStop())
239 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000240 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000241 while (1)
242 {
243 should_stop = current_plan->ShouldStop(event_ptr);
244 if (current_plan->MischiefManaged())
245 {
246 if (should_stop)
247 current_plan->WillStop();
248
249 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
250 // Otherwise, see if the plan's parent wants to stop.
251
252 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
253 {
254 PopPlan();
255 break;
256 }
257 else
258 {
259
260 PopPlan();
261
262 current_plan = GetCurrentPlan();
263 if (current_plan == NULL)
264 {
265 break;
266 }
267 }
268
269 }
270 else
271 {
272 break;
273 }
274 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000275 if (over_ride_stop)
276 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000277 }
278 else
279 {
280 // If the current plan doesn't explain the stop, then, find one that
281 // does and let it handle the situation.
282 ThreadPlan *plan_ptr = current_plan;
283 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
284 {
285 if (plan_ptr->PlanExplainsStop())
286 {
287 should_stop = plan_ptr->ShouldStop (event_ptr);
288 break;
289 }
290
291 }
292 }
293
294 return should_stop;
295}
296
297Vote
298Thread::ShouldReportStop (Event* event_ptr)
299{
300 StateType thread_state = GetResumeState ();
Greg Clayton5205f0b2010-09-03 17:10:42 +0000301 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
302
303 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
304 {
305 if (log)
306 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 +0000307 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000308 }
Chris Lattner24943d22010-06-08 16:52:24 +0000309
310 if (m_completed_plan_stack.size() > 0)
311 {
312 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000313 if (log)
314 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 +0000315 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
316 }
317 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000318 {
319 if (log)
320 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000321 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000322 }
Chris Lattner24943d22010-06-08 16:52:24 +0000323}
324
325Vote
326Thread::ShouldReportRun (Event* event_ptr)
327{
328 StateType thread_state = GetResumeState ();
329 if (thread_state == eStateSuspended
330 || thread_state == eStateInvalid)
331 return eVoteNoOpinion;
332
333 if (m_completed_plan_stack.size() > 0)
334 {
335 // Don't use GetCompletedPlan here, since that suppresses private plans.
336 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
337 }
338 else
339 return GetCurrentPlan()->ShouldReportRun (event_ptr);
340}
341
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000342bool
343Thread::MatchesSpec (const ThreadSpec *spec)
344{
345 if (spec == NULL)
346 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000347
Jim Ingham649492b2010-06-18 01:00:58 +0000348 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000349}
350
Chris Lattner24943d22010-06-08 16:52:24 +0000351void
352Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
353{
354 if (thread_plan_sp)
355 {
356 if (thread_plan_sp->IsImmediate())
357 m_immediate_plan_stack.push_back (thread_plan_sp);
358 else
359 m_plan_stack.push_back (thread_plan_sp);
360
361 thread_plan_sp->DidPush();
362
363 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
364 if (log)
365 {
366 StreamString s;
367 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Claytonf04d6612010-09-03 22:45:01 +0000368 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x, immediate = %s.",
Chris Lattner24943d22010-06-08 16:52:24 +0000369 s.GetData(),
370 thread_plan_sp->GetThread().GetID(),
371 thread_plan_sp->IsImmediate() ? "true" : "false");
372 }
373 }
374}
375
376void
377Thread::PopPlan ()
378{
379 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
380
381 if (!m_immediate_plan_stack.empty())
382 {
383 ThreadPlanSP &plan = m_immediate_plan_stack.back();
384 if (log)
385 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000386 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = true.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000387 }
388 plan->WillPop();
389 m_immediate_plan_stack.pop_back();
390 }
391 else if (m_plan_stack.empty())
392 return;
393 else
394 {
395 ThreadPlanSP &plan = m_plan_stack.back();
396 if (log)
397 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000398 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = false.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000399 }
400 m_completed_plan_stack.push_back (plan);
401 plan->WillPop();
402 m_plan_stack.pop_back();
403 }
404}
405
406void
407Thread::DiscardPlan ()
408{
409 if (m_plan_stack.size() > 1)
410 {
411 ThreadPlanSP &plan = m_plan_stack.back();
412 m_discarded_plan_stack.push_back (plan);
413 plan->WillPop();
414 m_plan_stack.pop_back();
415 }
416}
417
418ThreadPlan *
419Thread::GetCurrentPlan ()
420{
421 if (!m_immediate_plan_stack.empty())
422 return m_immediate_plan_stack.back().get();
423 else if (m_plan_stack.empty())
424 return NULL;
425 else
426 return m_plan_stack.back().get();
427}
428
429ThreadPlanSP
430Thread::GetCompletedPlan ()
431{
432 ThreadPlanSP empty_plan_sp;
433 if (!m_completed_plan_stack.empty())
434 {
435 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
436 {
437 ThreadPlanSP completed_plan_sp;
438 completed_plan_sp = m_completed_plan_stack[i];
439 if (!completed_plan_sp->GetPrivate ())
440 return completed_plan_sp;
441 }
442 }
443 return empty_plan_sp;
444}
445
446bool
447Thread::IsThreadPlanDone (ThreadPlan *plan)
448{
449 ThreadPlanSP empty_plan_sp;
450 if (!m_completed_plan_stack.empty())
451 {
452 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
453 {
454 if (m_completed_plan_stack[i].get() == plan)
455 return true;
456 }
457 }
458 return false;
459}
460
461bool
462Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
463{
464 ThreadPlanSP empty_plan_sp;
465 if (!m_discarded_plan_stack.empty())
466 {
467 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
468 {
469 if (m_discarded_plan_stack[i].get() == plan)
470 return true;
471 }
472 }
473 return false;
474}
475
476ThreadPlan *
477Thread::GetPreviousPlan (ThreadPlan *current_plan)
478{
479 if (current_plan == NULL)
480 return NULL;
481
482 int stack_size = m_completed_plan_stack.size();
483 for (int i = stack_size - 1; i > 0; i--)
484 {
485 if (current_plan == m_completed_plan_stack[i].get())
486 return m_completed_plan_stack[i-1].get();
487 }
488
489 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
490 {
491 if (m_immediate_plan_stack.size() > 0)
492 return m_immediate_plan_stack.back().get();
493 else if (m_plan_stack.size() > 0)
494 return m_plan_stack.back().get();
495 else
496 return NULL;
497 }
498
499 stack_size = m_immediate_plan_stack.size();
500 for (int i = stack_size - 1; i > 0; i--)
501 {
502 if (current_plan == m_immediate_plan_stack[i].get())
503 return m_immediate_plan_stack[i-1].get();
504 }
505 if (stack_size > 0 && m_immediate_plan_stack[0].get() == current_plan)
506 {
507 if (m_plan_stack.size() > 0)
508 return m_plan_stack.back().get();
509 else
510 return NULL;
511 }
512
513 stack_size = m_plan_stack.size();
514 for (int i = stack_size - 1; i > 0; i--)
515 {
516 if (current_plan == m_plan_stack[i].get())
517 return m_plan_stack[i-1].get();
518 }
519 return NULL;
520}
521
522void
523Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
524{
525 if (abort_other_plans)
526 DiscardThreadPlans(true);
527
528 PushPlan (thread_plan_sp);
529}
530
531void
532Thread::DiscardThreadPlans(bool force)
533{
534 // FIXME: It is not always safe to just discard plans. Some, like the step over
535 // breakpoint trap can't be discarded in general (though you can if you plan to
536 // force a return from a function, for instance.
537 // For now I'm just not clearing immediate plans, but I need a way for plans to
538 // say they really need to be kept on, and then a way to override that. Humm...
539
540 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
541 if (log)
542 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000543 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000544 }
545
546 if (force)
547 {
548 int stack_size = m_plan_stack.size();
549 for (int i = stack_size - 1; i > 0; i--)
550 {
551 DiscardPlan();
552 }
553 return;
554 }
555
556 while (1)
557 {
558
559 int master_plan_idx;
560 bool discard;
561
562 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
563 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
564 {
565 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
566 {
567 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
568 break;
569 }
570 }
571
572 if (discard)
573 {
574 // First pop all the dependent plans:
575 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
576 {
577
578 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
579 // for the plan leaves it in a state that it is safe to pop the plan
580 // with no more notice?
581 DiscardPlan();
582 }
583
584 // Now discard the master plan itself.
585 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
586 // discard it's dependent plans, but not it...
587 if (master_plan_idx > 0)
588 {
589 DiscardPlan();
590 }
591 }
592 else
593 {
594 // If the master plan doesn't want to get discarded, then we're done.
595 break;
596 }
597
598 }
599 // FIXME: What should we do about the immediate plans?
600}
601
602ThreadPlan *
603Thread::QueueFundamentalPlan (bool abort_other_plans)
604{
605 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
606 QueueThreadPlan (thread_plan_sp, abort_other_plans);
607 return thread_plan_sp.get();
608}
609
610ThreadPlan *
611Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
612{
613 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
614 QueueThreadPlan (thread_plan_sp, abort_other_plans);
615 return thread_plan_sp.get();
616}
617
618ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000619Thread::QueueThreadPlanForStepRange
620(
621 bool abort_other_plans,
622 StepType type,
623 const AddressRange &range,
624 const SymbolContext &addr_context,
625 lldb::RunMode stop_other_threads,
626 bool avoid_code_without_debug_info
627)
Chris Lattner24943d22010-06-08 16:52:24 +0000628{
629 ThreadPlanSP thread_plan_sp;
630 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000631 {
632 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
633 if (avoid_code_without_debug_info)
634 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
635 else
636 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
637 thread_plan_sp.reset (plan);
638 }
Chris Lattner24943d22010-06-08 16:52:24 +0000639 else
640 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
641
642 QueueThreadPlan (thread_plan_sp, abort_other_plans);
643 return thread_plan_sp.get();
644}
645
646
647ThreadPlan *
648Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
649{
650 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
651 QueueThreadPlan (thread_plan_sp, abort_other_plans);
652 return thread_plan_sp.get();
653}
654
655ThreadPlan *
656Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
657 bool stop_other_threads, Vote stop_vote, Vote run_vote)
658{
659 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
660 QueueThreadPlan (thread_plan_sp, abort_other_plans);
661 return thread_plan_sp.get();
662}
663
664ThreadPlan *
665Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
666{
Jim Inghamb66cd072010-09-28 01:25:32 +0000667 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000668 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000669 // If that didn't come up with anything, try the ObjC runtime plugin:
670 if (thread_plan_sp.get() == NULL)
671 {
672 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
673 if (objc_runtime)
674 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
675 }
676
Chris Lattner24943d22010-06-08 16:52:24 +0000677 if (thread_plan_sp.get() == NULL)
678 {
679 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
680 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000681 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000682 }
683 QueueThreadPlan (thread_plan_sp, abort_other_plans);
684 return thread_plan_sp.get();
685}
686
687ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000688Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
689 Address& function,
690 lldb::addr_t arg,
691 bool stop_other_threads,
692 bool discard_on_error)
693{
694 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
695 QueueThreadPlan (thread_plan_sp, abort_other_plans);
696 return thread_plan_sp.get();
697}
698
699ThreadPlan *
700Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
701 Address& function,
702 ValueList &args,
703 bool stop_other_threads,
704 bool discard_on_error)
705{
706 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error));
707 QueueThreadPlan (thread_plan_sp, abort_other_plans);
708 return thread_plan_sp.get();
709}
710
711ThreadPlan *
712Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
713 Address &target_addr,
714 bool stop_other_threads)
715{
716 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
717 QueueThreadPlan (thread_plan_sp, abort_other_plans);
718 return thread_plan_sp.get();
719}
720
721ThreadPlan *
722Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
723 lldb::addr_t *address_list,
724 size_t num_addresses,
725 bool stop_other_threads)
726{
727 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
728 QueueThreadPlan (thread_plan_sp, abort_other_plans);
729 return thread_plan_sp.get();
730
731}
732
733uint32_t
734Thread::GetIndexID () const
735{
736 return m_index_id;
737}
738
739void
740Thread::DumpThreadPlans (lldb_private::Stream *s) const
741{
742 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000743 int i;
744 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
745 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000746 {
747 s->Printf ("Element %d: ", i);
748 s->IndentMore();
749 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
750 s->IndentLess();
751 s->EOL();
752 }
753
754 stack_size = m_immediate_plan_stack.size();
755 s->Printf ("Immediate Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000756 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000757 {
758 s->Printf ("Element %d: ", i);
759 s->IndentMore();
760 m_immediate_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
761 s->IndentLess();
762 s->EOL();
763 }
764
765 stack_size = m_completed_plan_stack.size();
766 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000767 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000768 {
769 s->Printf ("Element %d: ", i);
770 s->IndentMore();
771 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
772 s->IndentLess();
773 s->EOL();
774 }
775
776 stack_size = m_discarded_plan_stack.size();
777 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000778 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000779 {
780 s->Printf ("Element %d: ", i);
781 s->IndentMore();
782 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
783 s->IndentLess();
784 s->EOL();
785 }
786
787}
788
789Target *
790Thread::CalculateTarget ()
791{
792 return m_process.CalculateTarget();
793}
794
795Process *
796Thread::CalculateProcess ()
797{
798 return &m_process;
799}
800
801Thread *
802Thread::CalculateThread ()
803{
804 return this;
805}
806
807StackFrame *
808Thread::CalculateStackFrame ()
809{
810 return NULL;
811}
812
813void
Greg Claytona830adb2010-10-04 01:05:56 +0000814Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000815{
Greg Claytona830adb2010-10-04 01:05:56 +0000816 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000817 exe_ctx.thread = this;
818 exe_ctx.frame = NULL;
819}
820
Greg Clayton782b9cc2010-08-25 00:35:26 +0000821
822StackFrameList &
823Thread::GetStackFrameList ()
824{
Greg Claytonf40e3082010-08-26 02:28:22 +0000825 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000826 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000827 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000828}
829
830
831
Jim Ingham71219082010-08-12 02:14:28 +0000832uint32_t
833Thread::GetStackFrameCount()
834{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000835 return GetStackFrameList().GetNumFrames();
836}
Greg Clayton33ed1702010-08-24 00:45:41 +0000837
Greg Clayton33ed1702010-08-24 00:45:41 +0000838
Greg Clayton782b9cc2010-08-25 00:35:26 +0000839void
840Thread::ClearStackFrames ()
841{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000842 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
843 m_prev_frames_sp.reset (m_curr_frames_ap.release());
844 else
845 m_curr_frames_ap.release();
846
847// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
848// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000849}
850
851lldb::StackFrameSP
852Thread::GetStackFrameAtIndex (uint32_t idx)
853{
Greg Clayton72b71582010-09-02 21:44:10 +0000854 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000855}
856
Chris Lattner24943d22010-06-08 16:52:24 +0000857lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000858Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000859{
Jim Inghamc8332952010-08-26 21:32:51 +0000860 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000861}
862
863uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000864Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000865{
Jim Inghamc8332952010-08-26 21:32:51 +0000866 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000867}
868
869void
Jim Inghamc8332952010-08-26 21:32:51 +0000870Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000871{
Jim Inghamc8332952010-08-26 21:32:51 +0000872 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000873}
874
875void
Greg Claytona830adb2010-10-04 01:05:56 +0000876Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000877{
Greg Claytona830adb2010-10-04 01:05:56 +0000878 ExecutionContext exe_ctx;
879 SymbolContext frame_sc;
880 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000881
Greg Claytona830adb2010-10-04 01:05:56 +0000882 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000883 {
Greg Claytona830adb2010-10-04 01:05:56 +0000884 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000885 if (frame_sp)
886 {
Greg Claytona830adb2010-10-04 01:05:56 +0000887 exe_ctx.frame = frame_sp.get();
888 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000889 }
890 }
891
Greg Claytona830adb2010-10-04 01:05:56 +0000892 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
893 assert (thread_format);
894 const char *end = NULL;
895 Debugger::FormatPrompt (thread_format,
896 exe_ctx.frame ? &frame_sc : NULL,
897 &exe_ctx,
898 NULL,
899 strm,
900 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000901}
902
903lldb::ThreadSP
904Thread::GetSP ()
905{
906 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
907}
Jim Ingham20594b12010-09-08 03:14:33 +0000908
909lldb::UserSettingsControllerSP
910Thread::GetSettingsController (bool finish)
911{
912 static UserSettingsControllerSP g_settings_controller (new ThreadSettingsController);
913 static bool initialized = false;
914
915 if (!initialized)
916 {
917 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
918 Thread::ThreadSettingsController::global_settings_table,
919 Thread::ThreadSettingsController::instance_settings_table);
920 }
921
922 if (finish)
923 {
924 UserSettingsController::FinalizeSettingsController (g_settings_controller);
925 g_settings_controller.reset();
926 initialized = false;
927 }
928
929 return g_settings_controller;
930}
931
Caroline Tice1ebef442010-09-27 00:30:10 +0000932void
933Thread::UpdateInstanceName ()
934{
935 StreamString sstr;
936 const char *name = GetName();
937
938 if (name && name[0] != '\0')
939 sstr.Printf ("%s", name);
940 else if ((GetIndexID() != 0) || (GetID() != 0))
941 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
942
943 if (sstr.GetSize() > 0)
944 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
945}
946
Jim Ingham20594b12010-09-08 03:14:33 +0000947//--------------------------------------------------------------
948// class Thread::ThreadSettingsController
949//--------------------------------------------------------------
950
951Thread::ThreadSettingsController::ThreadSettingsController () :
952 UserSettingsController ("thread", Process::GetSettingsController())
953{
Caroline Tice004afcb2010-09-08 17:48:55 +0000954 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
955 InstanceSettings::GetDefaultName().AsCString()));
Jim Ingham20594b12010-09-08 03:14:33 +0000956}
957
958Thread::ThreadSettingsController::~ThreadSettingsController ()
959{
960}
961
962lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +0000963Thread::ThreadSettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham20594b12010-09-08 03:14:33 +0000964{
Caroline Tice004afcb2010-09-08 17:48:55 +0000965 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*(Thread::GetSettingsController().get()),
966 false, instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000967 lldb::InstanceSettingsSP new_settings_sp (new_settings);
968 return new_settings_sp;
969}
970
971//--------------------------------------------------------------
972// class ThreadInstanceSettings
973//--------------------------------------------------------------
974
Caroline Tice004afcb2010-09-08 17:48:55 +0000975ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Caroline Tice75b11a32010-09-16 19:05:55 +0000976 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance),
Jim Ingham20594b12010-09-08 03:14:33 +0000977 m_avoid_regexp_ap ()
978{
Caroline Tice396704b2010-09-09 18:26:37 +0000979 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
980 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
981 // 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 +0000982 // This is true for CreateInstanceName() too.
983
984 if (GetInstanceName() == InstanceSettings::InvalidName())
985 {
986 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
987 m_owner.RegisterInstanceSettings (this);
988 }
Caroline Tice396704b2010-09-09 18:26:37 +0000989
990 if (live_instance)
Jim Ingham20594b12010-09-08 03:14:33 +0000991 {
992 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
993 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +0000994 //m_owner.RemovePendingSettings (m_instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000995 }
996}
997
998ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
999 InstanceSettings (*(Thread::GetSettingsController().get()), CreateInstanceName().AsCString()),
1000 m_avoid_regexp_ap ()
1001{
1002 if (m_instance_name != InstanceSettings::GetDefaultName())
1003 {
1004 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1005 CopyInstanceSettings (pending_settings,false);
1006 m_owner.RemovePendingSettings (m_instance_name);
1007 }
1008 if (rhs.m_avoid_regexp_ap.get() != NULL)
1009 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1010}
1011
1012ThreadInstanceSettings::~ThreadInstanceSettings ()
1013{
1014}
1015
1016ThreadInstanceSettings&
1017ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1018{
1019 if (this != &rhs)
1020 {
1021 if (rhs.m_avoid_regexp_ap.get() != NULL)
1022 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1023 else
1024 m_avoid_regexp_ap.reset(NULL);
1025 }
1026
1027 return *this;
1028}
1029
1030
1031void
1032ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1033 const char *index_value,
1034 const char *value,
1035 const ConstString &instance_name,
1036 const SettingEntry &entry,
1037 lldb::VarSetOperationType op,
1038 Error &err,
1039 bool pending)
1040{
1041 if (var_name == StepAvoidRegexpVarName())
1042 {
1043 std::string regexp_text;
1044 if (m_avoid_regexp_ap.get() != NULL)
1045 regexp_text.append (m_avoid_regexp_ap->GetText());
1046 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1047 if (regexp_text.empty())
1048 m_avoid_regexp_ap.reset();
1049 else
1050 {
1051 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1052
1053 }
1054 }
1055}
1056
1057void
1058ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1059 bool pending)
1060{
1061 if (new_settings.get() == NULL)
1062 return;
1063
1064 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1065 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1066 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1067 else
1068 m_avoid_regexp_ap.reset ();
1069}
1070
Caroline Ticebcb5b452010-09-20 21:37:42 +00001071bool
Jim Ingham20594b12010-09-08 03:14:33 +00001072ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001073 const ConstString &var_name,
1074 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001075 Error *err)
Jim Ingham20594b12010-09-08 03:14:33 +00001076{
1077 if (var_name == StepAvoidRegexpVarName())
1078 {
1079 if (m_avoid_regexp_ap.get() != NULL)
1080 {
1081 std::string regexp_text("\"");
1082 regexp_text.append(m_avoid_regexp_ap->GetText());
1083 regexp_text.append ("\"");
1084 value.AppendString (regexp_text.c_str());
1085 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001086
Jim Ingham20594b12010-09-08 03:14:33 +00001087 }
1088 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00001089 {
1090 if (err)
1091 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1092 return false;
1093 }
1094 return true;
Jim Ingham20594b12010-09-08 03:14:33 +00001095}
1096
Jim Ingham20594b12010-09-08 03:14:33 +00001097const ConstString
1098ThreadInstanceSettings::CreateInstanceName ()
1099{
1100 static int instance_count = 1;
1101 StreamString sstr;
1102
1103 sstr.Printf ("thread_%d", instance_count);
1104 ++instance_count;
1105
1106 const ConstString ret_val (sstr.GetData());
1107 return ret_val;
1108}
1109
1110const ConstString &
1111ThreadInstanceSettings::StepAvoidRegexpVarName ()
1112{
1113 static ConstString run_args_var_name ("step-avoid-regexp");
1114
1115 return run_args_var_name;
1116}
1117
1118//--------------------------------------------------
1119// ThreadSettingsController Variable Tables
1120//--------------------------------------------------
1121
1122SettingEntry
1123Thread::ThreadSettingsController::global_settings_table[] =
1124{
1125 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1126 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1127};
1128
1129
1130SettingEntry
1131Thread::ThreadSettingsController::instance_settings_table[] =
1132{
1133 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1134 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1135 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1136};
1137
Jim Inghamccd584d2010-09-23 17:40:12 +00001138lldb::StackFrameSP
1139Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1140{
1141 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1142}