blob: fb4ad9a3bf2d945de2d67a486d12aac7c3d7fea0 [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"
Jim Inghamb66cd072010-09-28 01:25:32 +000019#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Target/Process.h"
21#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000022#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000023#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Thread.h"
25#include "lldb/Target/ThreadPlan.h"
26#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/ThreadPlanBase.h"
28#include "lldb/Target/ThreadPlanStepInstruction.h"
29#include "lldb/Target/ThreadPlanStepOut.h"
30#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
31#include "lldb/Target/ThreadPlanStepThrough.h"
32#include "lldb/Target/ThreadPlanStepInRange.h"
33#include "lldb/Target/ThreadPlanStepOverRange.h"
34#include "lldb/Target/ThreadPlanRunToAddress.h"
35#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000036#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000037#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
42Thread::Thread (Process &process, lldb::tid_t tid) :
43 UserID (tid),
Jim Ingham20594b12010-09-08 03:14:33 +000044 ThreadInstanceSettings (*(Thread::GetSettingsController().get())),
Benjamin Kramer36a08102010-07-16 12:32:33 +000045 m_process (process),
Greg Clayton643ee732010-08-04 01:40:35 +000046 m_public_stop_info_sp (),
47 m_actual_stop_info_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000048 m_index_id (process.GetNextThreadIndexID ()),
49 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000050 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +000051 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000052 m_plan_stack (),
53 m_immediate_plan_stack(),
54 m_completed_plan_stack(),
Greg Claytonf40e3082010-08-26 02:28:22 +000055 m_curr_frames_ap (),
Chris Lattner24943d22010-06-08 16:52:24 +000056 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000057 m_resume_state (eStateRunning),
58 m_unwinder_ap ()
59
Chris Lattner24943d22010-06-08 16:52:24 +000060{
61 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
62 if (log)
63 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
64
65 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000066 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000067}
68
69
70Thread::~Thread()
71{
72 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
73 if (log)
74 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
75}
76
77int
78Thread::GetResumeSignal () const
79{
80 return m_resume_signal;
81}
82
83void
84Thread::SetResumeSignal (int signal)
85{
86 m_resume_signal = signal;
87}
88
89StateType
90Thread::GetResumeState () const
91{
92 return m_resume_state;
93}
94
95void
96Thread::SetResumeState (StateType state)
97{
98 m_resume_state = state;
99}
100
Greg Clayton643ee732010-08-04 01:40:35 +0000101StopInfo *
102Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000103{
Greg Clayton643ee732010-08-04 01:40:35 +0000104 if (m_public_stop_info_sp.get() == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000105 {
Greg Clayton643ee732010-08-04 01:40:35 +0000106 ThreadPlanSP plan_sp (GetCompletedPlan());
107 if (plan_sp)
108 m_public_stop_info_sp = StopInfo::CreateStopReasonWithPlan (plan_sp);
109 else
110 m_public_stop_info_sp = GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000111 }
Greg Clayton643ee732010-08-04 01:40:35 +0000112 return m_public_stop_info_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000113}
114
115bool
116Thread::ThreadStoppedForAReason (void)
117{
Greg Clayton643ee732010-08-04 01:40:35 +0000118 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000119}
120
121StateType
122Thread::GetState() const
123{
124 // If any other threads access this we will need a mutex for it
125 Mutex::Locker locker(m_state_mutex);
126 return m_state;
127}
128
129void
130Thread::SetState(StateType state)
131{
132 Mutex::Locker locker(m_state_mutex);
133 m_state = state;
134}
135
136void
137Thread::WillStop()
138{
139 ThreadPlan *current_plan = GetCurrentPlan();
140
141 // FIXME: I may decide to disallow threads with no plans. In which
142 // case this should go to an assert.
143
144 if (!current_plan)
145 return;
146
147 current_plan->WillStop();
148}
149
150void
151Thread::SetupForResume ()
152{
153 if (GetResumeState() != eStateSuspended)
154 {
155
156 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
157 // telling the current plan it will resume, since we might change what the current
158 // plan is.
159
160 lldb::addr_t pc = GetRegisterContext()->GetPC();
161 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
162 if (bp_site_sp && bp_site_sp->IsEnabled())
163 {
164 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
165 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000166
167 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000168
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000169 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
170 {
171 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
172 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000173 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000174 ThreadPlanSP step_bp_plan_sp;
175 step_bp_plan->SetPrivate (true);
176
Chris Lattner24943d22010-06-08 16:52:24 +0000177 if (GetCurrentPlan()->RunState() != eStateStepping)
178 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000179 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000181 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000182 QueueThreadPlan (step_bp_plan_sp, false);
183 }
184 }
185 }
186 }
187}
188
189bool
190Thread::WillResume (StateType resume_state)
191{
192 // At this point clear the completed plan stack.
193 m_completed_plan_stack.clear();
194 m_discarded_plan_stack.clear();
195
Greg Clayton643ee732010-08-04 01:40:35 +0000196 StopInfo *stop_info = GetPrivateStopReason().get();
197 if (stop_info)
198 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000199
200 // Tell all the plans that we are about to resume in case they need to clear any state.
201 // We distinguish between the plan on the top of the stack and the lower
202 // plans in case a plan needs to do any special business before it runs.
203
204 ThreadPlan *plan_ptr = GetCurrentPlan();
205 plan_ptr->WillResume(resume_state, true);
206
207 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
208 {
209 plan_ptr->WillResume (resume_state, false);
210 }
Greg Clayton643ee732010-08-04 01:40:35 +0000211
212 m_public_stop_info_sp.reset();
213 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000214 return true;
215}
216
217void
218Thread::DidResume ()
219{
220 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
221}
222
223bool
224Thread::ShouldStop (Event* event_ptr)
225{
226 ThreadPlan *current_plan = GetCurrentPlan();
227 bool should_stop = true;
228
229 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
230 if (log)
231 {
232 StreamString s;
233 DumpThreadPlans(&s);
234 log->PutCString (s.GetData());
235 }
236
237 if (current_plan->PlanExplainsStop())
238 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000239 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000240 while (1)
241 {
242 should_stop = current_plan->ShouldStop(event_ptr);
243 if (current_plan->MischiefManaged())
244 {
245 if (should_stop)
246 current_plan->WillStop();
247
248 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
249 // Otherwise, see if the plan's parent wants to stop.
250
251 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
252 {
253 PopPlan();
254 break;
255 }
256 else
257 {
258
259 PopPlan();
260
261 current_plan = GetCurrentPlan();
262 if (current_plan == NULL)
263 {
264 break;
265 }
266 }
267
268 }
269 else
270 {
271 break;
272 }
273 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000274 if (over_ride_stop)
275 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000276 }
277 else
278 {
279 // If the current plan doesn't explain the stop, then, find one that
280 // does and let it handle the situation.
281 ThreadPlan *plan_ptr = current_plan;
282 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
283 {
284 if (plan_ptr->PlanExplainsStop())
285 {
286 should_stop = plan_ptr->ShouldStop (event_ptr);
287 break;
288 }
289
290 }
291 }
292
293 return should_stop;
294}
295
296Vote
297Thread::ShouldReportStop (Event* event_ptr)
298{
299 StateType thread_state = GetResumeState ();
Greg Clayton5205f0b2010-09-03 17:10:42 +0000300 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
301
302 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
303 {
304 if (log)
305 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 +0000306 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000307 }
Chris Lattner24943d22010-06-08 16:52:24 +0000308
309 if (m_completed_plan_stack.size() > 0)
310 {
311 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000312 if (log)
313 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 +0000314 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
315 }
316 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000317 {
318 if (log)
319 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000320 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000321 }
Chris Lattner24943d22010-06-08 16:52:24 +0000322}
323
324Vote
325Thread::ShouldReportRun (Event* event_ptr)
326{
327 StateType thread_state = GetResumeState ();
328 if (thread_state == eStateSuspended
329 || thread_state == eStateInvalid)
330 return eVoteNoOpinion;
331
332 if (m_completed_plan_stack.size() > 0)
333 {
334 // Don't use GetCompletedPlan here, since that suppresses private plans.
335 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
336 }
337 else
338 return GetCurrentPlan()->ShouldReportRun (event_ptr);
339}
340
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000341bool
342Thread::MatchesSpec (const ThreadSpec *spec)
343{
344 if (spec == NULL)
345 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000346
Jim Ingham649492b2010-06-18 01:00:58 +0000347 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000348}
349
Chris Lattner24943d22010-06-08 16:52:24 +0000350void
351Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
352{
353 if (thread_plan_sp)
354 {
355 if (thread_plan_sp->IsImmediate())
356 m_immediate_plan_stack.push_back (thread_plan_sp);
357 else
358 m_plan_stack.push_back (thread_plan_sp);
359
360 thread_plan_sp->DidPush();
361
362 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
363 if (log)
364 {
365 StreamString s;
366 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Claytonf04d6612010-09-03 22:45:01 +0000367 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x, immediate = %s.",
Chris Lattner24943d22010-06-08 16:52:24 +0000368 s.GetData(),
369 thread_plan_sp->GetThread().GetID(),
370 thread_plan_sp->IsImmediate() ? "true" : "false");
371 }
372 }
373}
374
375void
376Thread::PopPlan ()
377{
378 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
379
380 if (!m_immediate_plan_stack.empty())
381 {
382 ThreadPlanSP &plan = m_immediate_plan_stack.back();
383 if (log)
384 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000385 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = true.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000386 }
387 plan->WillPop();
388 m_immediate_plan_stack.pop_back();
389 }
390 else if (m_plan_stack.empty())
391 return;
392 else
393 {
394 ThreadPlanSP &plan = m_plan_stack.back();
395 if (log)
396 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000397 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = false.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000398 }
399 m_completed_plan_stack.push_back (plan);
400 plan->WillPop();
401 m_plan_stack.pop_back();
402 }
403}
404
405void
406Thread::DiscardPlan ()
407{
408 if (m_plan_stack.size() > 1)
409 {
410 ThreadPlanSP &plan = m_plan_stack.back();
411 m_discarded_plan_stack.push_back (plan);
412 plan->WillPop();
413 m_plan_stack.pop_back();
414 }
415}
416
417ThreadPlan *
418Thread::GetCurrentPlan ()
419{
420 if (!m_immediate_plan_stack.empty())
421 return m_immediate_plan_stack.back().get();
422 else if (m_plan_stack.empty())
423 return NULL;
424 else
425 return m_plan_stack.back().get();
426}
427
428ThreadPlanSP
429Thread::GetCompletedPlan ()
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 ThreadPlanSP completed_plan_sp;
437 completed_plan_sp = m_completed_plan_stack[i];
438 if (!completed_plan_sp->GetPrivate ())
439 return completed_plan_sp;
440 }
441 }
442 return empty_plan_sp;
443}
444
445bool
446Thread::IsThreadPlanDone (ThreadPlan *plan)
447{
448 ThreadPlanSP empty_plan_sp;
449 if (!m_completed_plan_stack.empty())
450 {
451 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
452 {
453 if (m_completed_plan_stack[i].get() == plan)
454 return true;
455 }
456 }
457 return false;
458}
459
460bool
461Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
462{
463 ThreadPlanSP empty_plan_sp;
464 if (!m_discarded_plan_stack.empty())
465 {
466 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
467 {
468 if (m_discarded_plan_stack[i].get() == plan)
469 return true;
470 }
471 }
472 return false;
473}
474
475ThreadPlan *
476Thread::GetPreviousPlan (ThreadPlan *current_plan)
477{
478 if (current_plan == NULL)
479 return NULL;
480
481 int stack_size = m_completed_plan_stack.size();
482 for (int i = stack_size - 1; i > 0; i--)
483 {
484 if (current_plan == m_completed_plan_stack[i].get())
485 return m_completed_plan_stack[i-1].get();
486 }
487
488 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
489 {
490 if (m_immediate_plan_stack.size() > 0)
491 return m_immediate_plan_stack.back().get();
492 else if (m_plan_stack.size() > 0)
493 return m_plan_stack.back().get();
494 else
495 return NULL;
496 }
497
498 stack_size = m_immediate_plan_stack.size();
499 for (int i = stack_size - 1; i > 0; i--)
500 {
501 if (current_plan == m_immediate_plan_stack[i].get())
502 return m_immediate_plan_stack[i-1].get();
503 }
504 if (stack_size > 0 && m_immediate_plan_stack[0].get() == current_plan)
505 {
506 if (m_plan_stack.size() > 0)
507 return m_plan_stack.back().get();
508 else
509 return NULL;
510 }
511
512 stack_size = m_plan_stack.size();
513 for (int i = stack_size - 1; i > 0; i--)
514 {
515 if (current_plan == m_plan_stack[i].get())
516 return m_plan_stack[i-1].get();
517 }
518 return NULL;
519}
520
521void
522Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
523{
524 if (abort_other_plans)
525 DiscardThreadPlans(true);
526
527 PushPlan (thread_plan_sp);
528}
529
530void
531Thread::DiscardThreadPlans(bool force)
532{
533 // FIXME: It is not always safe to just discard plans. Some, like the step over
534 // breakpoint trap can't be discarded in general (though you can if you plan to
535 // force a return from a function, for instance.
536 // For now I'm just not clearing immediate plans, but I need a way for plans to
537 // say they really need to be kept on, and then a way to override that. Humm...
538
539 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
540 if (log)
541 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000542 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000543 }
544
545 if (force)
546 {
547 int stack_size = m_plan_stack.size();
548 for (int i = stack_size - 1; i > 0; i--)
549 {
550 DiscardPlan();
551 }
552 return;
553 }
554
555 while (1)
556 {
557
558 int master_plan_idx;
559 bool discard;
560
561 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
562 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
563 {
564 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
565 {
566 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
567 break;
568 }
569 }
570
571 if (discard)
572 {
573 // First pop all the dependent plans:
574 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
575 {
576
577 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
578 // for the plan leaves it in a state that it is safe to pop the plan
579 // with no more notice?
580 DiscardPlan();
581 }
582
583 // Now discard the master plan itself.
584 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
585 // discard it's dependent plans, but not it...
586 if (master_plan_idx > 0)
587 {
588 DiscardPlan();
589 }
590 }
591 else
592 {
593 // If the master plan doesn't want to get discarded, then we're done.
594 break;
595 }
596
597 }
598 // FIXME: What should we do about the immediate plans?
599}
600
601ThreadPlan *
602Thread::QueueFundamentalPlan (bool abort_other_plans)
603{
604 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
605 QueueThreadPlan (thread_plan_sp, abort_other_plans);
606 return thread_plan_sp.get();
607}
608
609ThreadPlan *
610Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
611{
612 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
613 QueueThreadPlan (thread_plan_sp, abort_other_plans);
614 return thread_plan_sp.get();
615}
616
617ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000618Thread::QueueThreadPlanForStepRange
619(
620 bool abort_other_plans,
621 StepType type,
622 const AddressRange &range,
623 const SymbolContext &addr_context,
624 lldb::RunMode stop_other_threads,
625 bool avoid_code_without_debug_info
626)
Chris Lattner24943d22010-06-08 16:52:24 +0000627{
628 ThreadPlanSP thread_plan_sp;
629 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000630 {
631 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
632 if (avoid_code_without_debug_info)
633 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
634 else
635 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
636 thread_plan_sp.reset (plan);
637 }
Chris Lattner24943d22010-06-08 16:52:24 +0000638 else
639 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
640
641 QueueThreadPlan (thread_plan_sp, abort_other_plans);
642 return thread_plan_sp.get();
643}
644
645
646ThreadPlan *
647Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
648{
649 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
650 QueueThreadPlan (thread_plan_sp, abort_other_plans);
651 return thread_plan_sp.get();
652}
653
654ThreadPlan *
655Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
656 bool stop_other_threads, Vote stop_vote, Vote run_vote)
657{
658 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
659 QueueThreadPlan (thread_plan_sp, abort_other_plans);
660 return thread_plan_sp.get();
661}
662
663ThreadPlan *
664Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
665{
Jim Inghamb66cd072010-09-28 01:25:32 +0000666 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000667 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000668 // If that didn't come up with anything, try the ObjC runtime plugin:
669 if (thread_plan_sp.get() == NULL)
670 {
671 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
672 if (objc_runtime)
673 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
674 }
675
Chris Lattner24943d22010-06-08 16:52:24 +0000676 if (thread_plan_sp.get() == NULL)
677 {
678 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
679 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000680 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000681 }
682 QueueThreadPlan (thread_plan_sp, abort_other_plans);
683 return thread_plan_sp.get();
684}
685
686ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000687Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
688 Address& function,
689 lldb::addr_t arg,
690 bool stop_other_threads,
691 bool discard_on_error)
692{
693 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
694 QueueThreadPlan (thread_plan_sp, abort_other_plans);
695 return thread_plan_sp.get();
696}
697
698ThreadPlan *
699Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
700 Address& function,
701 ValueList &args,
702 bool stop_other_threads,
703 bool discard_on_error)
704{
705 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error));
706 QueueThreadPlan (thread_plan_sp, abort_other_plans);
707 return thread_plan_sp.get();
708}
709
710ThreadPlan *
711Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
712 Address &target_addr,
713 bool stop_other_threads)
714{
715 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
716 QueueThreadPlan (thread_plan_sp, abort_other_plans);
717 return thread_plan_sp.get();
718}
719
720ThreadPlan *
721Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
722 lldb::addr_t *address_list,
723 size_t num_addresses,
724 bool stop_other_threads)
725{
726 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
727 QueueThreadPlan (thread_plan_sp, abort_other_plans);
728 return thread_plan_sp.get();
729
730}
731
732uint32_t
733Thread::GetIndexID () const
734{
735 return m_index_id;
736}
737
738void
739Thread::DumpThreadPlans (lldb_private::Stream *s) const
740{
741 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000742 int i;
743 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
744 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_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
749 s->IndentLess();
750 s->EOL();
751 }
752
753 stack_size = m_immediate_plan_stack.size();
754 s->Printf ("Immediate 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_immediate_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
760 s->IndentLess();
761 s->EOL();
762 }
763
764 stack_size = m_completed_plan_stack.size();
765 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000766 for (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_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
771 s->IndentLess();
772 s->EOL();
773 }
774
775 stack_size = m_discarded_plan_stack.size();
776 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000777 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000778 {
779 s->Printf ("Element %d: ", i);
780 s->IndentMore();
781 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
782 s->IndentLess();
783 s->EOL();
784 }
785
786}
787
788Target *
789Thread::CalculateTarget ()
790{
791 return m_process.CalculateTarget();
792}
793
794Process *
795Thread::CalculateProcess ()
796{
797 return &m_process;
798}
799
800Thread *
801Thread::CalculateThread ()
802{
803 return this;
804}
805
806StackFrame *
807Thread::CalculateStackFrame ()
808{
809 return NULL;
810}
811
812void
813Thread::Calculate (ExecutionContext &exe_ctx)
814{
815 m_process.Calculate (exe_ctx);
816 exe_ctx.thread = this;
817 exe_ctx.frame = NULL;
818}
819
Greg Clayton782b9cc2010-08-25 00:35:26 +0000820
821StackFrameList &
822Thread::GetStackFrameList ()
823{
Greg Claytonf40e3082010-08-26 02:28:22 +0000824 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000825 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000826 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000827}
828
829
830
Jim Ingham71219082010-08-12 02:14:28 +0000831uint32_t
832Thread::GetStackFrameCount()
833{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000834 return GetStackFrameList().GetNumFrames();
835}
Greg Clayton33ed1702010-08-24 00:45:41 +0000836
Greg Clayton33ed1702010-08-24 00:45:41 +0000837
Greg Clayton782b9cc2010-08-25 00:35:26 +0000838void
839Thread::ClearStackFrames ()
840{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000841 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
842 m_prev_frames_sp.reset (m_curr_frames_ap.release());
843 else
844 m_curr_frames_ap.release();
845
846// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
847// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000848}
849
850lldb::StackFrameSP
851Thread::GetStackFrameAtIndex (uint32_t idx)
852{
Greg Clayton72b71582010-09-02 21:44:10 +0000853 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000854}
855
Chris Lattner24943d22010-06-08 16:52:24 +0000856lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000857Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000858{
Jim Inghamc8332952010-08-26 21:32:51 +0000859 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000860}
861
862uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000863Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000864{
Jim Inghamc8332952010-08-26 21:32:51 +0000865 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000866}
867
868void
Jim Inghamc8332952010-08-26 21:32:51 +0000869Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000870{
Jim Inghamc8332952010-08-26 21:32:51 +0000871 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000872}
873
874void
875Thread::DumpInfo
876(
877 Stream &strm,
878 bool show_stop_reason,
879 bool show_name,
880 bool show_queue,
Greg Clayton33ed1702010-08-24 00:45:41 +0000881 uint32_t idx
Chris Lattner24943d22010-06-08 16:52:24 +0000882)
883{
884 strm.Printf("thread #%u: tid = 0x%4.4x", GetIndexID(), GetID());
885
Greg Clayton33ed1702010-08-24 00:45:41 +0000886 if (idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000887 {
Greg Clayton33ed1702010-08-24 00:45:41 +0000888 StackFrameSP frame_sp(GetStackFrameAtIndex (idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000889 if (frame_sp)
890 {
891 strm.PutCString(", ");
Greg Clayton72b71582010-09-02 21:44:10 +0000892 frame_sp->Dump (&strm, false, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000893 }
894 }
895
896 if (show_stop_reason)
897 {
Greg Clayton643ee732010-08-04 01:40:35 +0000898 StopInfo *stop_info = GetStopInfo();
899
900 if (stop_info)
Chris Lattner24943d22010-06-08 16:52:24 +0000901 {
Greg Clayton643ee732010-08-04 01:40:35 +0000902 const char *stop_description = stop_info->GetDescription();
903 if (stop_description)
904 strm.Printf (", stop reason = %s", stop_description);
Chris Lattner24943d22010-06-08 16:52:24 +0000905 }
906 }
907
908 if (show_name)
909 {
910 const char *name = GetName();
911 if (name && name[0])
912 strm.Printf(", name = %s", name);
913 }
914
915 if (show_queue)
916 {
917 const char *queue = GetQueueName();
918 if (queue && queue[0])
919 strm.Printf(", queue = %s", queue);
920 }
921}
922
923lldb::ThreadSP
924Thread::GetSP ()
925{
926 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
927}
Jim Ingham20594b12010-09-08 03:14:33 +0000928
929lldb::UserSettingsControllerSP
930Thread::GetSettingsController (bool finish)
931{
932 static UserSettingsControllerSP g_settings_controller (new ThreadSettingsController);
933 static bool initialized = false;
934
935 if (!initialized)
936 {
937 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
938 Thread::ThreadSettingsController::global_settings_table,
939 Thread::ThreadSettingsController::instance_settings_table);
940 }
941
942 if (finish)
943 {
944 UserSettingsController::FinalizeSettingsController (g_settings_controller);
945 g_settings_controller.reset();
946 initialized = false;
947 }
948
949 return g_settings_controller;
950}
951
Caroline Tice1ebef442010-09-27 00:30:10 +0000952void
953Thread::UpdateInstanceName ()
954{
955 StreamString sstr;
956 const char *name = GetName();
957
958 if (name && name[0] != '\0')
959 sstr.Printf ("%s", name);
960 else if ((GetIndexID() != 0) || (GetID() != 0))
961 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
962
963 if (sstr.GetSize() > 0)
964 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
965}
966
Jim Ingham20594b12010-09-08 03:14:33 +0000967//--------------------------------------------------------------
968// class Thread::ThreadSettingsController
969//--------------------------------------------------------------
970
971Thread::ThreadSettingsController::ThreadSettingsController () :
972 UserSettingsController ("thread", Process::GetSettingsController())
973{
Caroline Tice004afcb2010-09-08 17:48:55 +0000974 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
975 InstanceSettings::GetDefaultName().AsCString()));
Jim Ingham20594b12010-09-08 03:14:33 +0000976}
977
978Thread::ThreadSettingsController::~ThreadSettingsController ()
979{
980}
981
982lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +0000983Thread::ThreadSettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham20594b12010-09-08 03:14:33 +0000984{
Caroline Tice004afcb2010-09-08 17:48:55 +0000985 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*(Thread::GetSettingsController().get()),
986 false, instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000987 lldb::InstanceSettingsSP new_settings_sp (new_settings);
988 return new_settings_sp;
989}
990
991//--------------------------------------------------------------
992// class ThreadInstanceSettings
993//--------------------------------------------------------------
994
Caroline Tice004afcb2010-09-08 17:48:55 +0000995ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Caroline Tice75b11a32010-09-16 19:05:55 +0000996 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance),
Jim Ingham20594b12010-09-08 03:14:33 +0000997 m_avoid_regexp_ap ()
998{
Caroline Tice396704b2010-09-09 18:26:37 +0000999 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1000 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1001 // 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 +00001002 // This is true for CreateInstanceName() too.
1003
1004 if (GetInstanceName() == InstanceSettings::InvalidName())
1005 {
1006 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1007 m_owner.RegisterInstanceSettings (this);
1008 }
Caroline Tice396704b2010-09-09 18:26:37 +00001009
1010 if (live_instance)
Jim Ingham20594b12010-09-08 03:14:33 +00001011 {
1012 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1013 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +00001014 //m_owner.RemovePendingSettings (m_instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +00001015 }
1016}
1017
1018ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
1019 InstanceSettings (*(Thread::GetSettingsController().get()), CreateInstanceName().AsCString()),
1020 m_avoid_regexp_ap ()
1021{
1022 if (m_instance_name != InstanceSettings::GetDefaultName())
1023 {
1024 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1025 CopyInstanceSettings (pending_settings,false);
1026 m_owner.RemovePendingSettings (m_instance_name);
1027 }
1028 if (rhs.m_avoid_regexp_ap.get() != NULL)
1029 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1030}
1031
1032ThreadInstanceSettings::~ThreadInstanceSettings ()
1033{
1034}
1035
1036ThreadInstanceSettings&
1037ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1038{
1039 if (this != &rhs)
1040 {
1041 if (rhs.m_avoid_regexp_ap.get() != NULL)
1042 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1043 else
1044 m_avoid_regexp_ap.reset(NULL);
1045 }
1046
1047 return *this;
1048}
1049
1050
1051void
1052ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1053 const char *index_value,
1054 const char *value,
1055 const ConstString &instance_name,
1056 const SettingEntry &entry,
1057 lldb::VarSetOperationType op,
1058 Error &err,
1059 bool pending)
1060{
1061 if (var_name == StepAvoidRegexpVarName())
1062 {
1063 std::string regexp_text;
1064 if (m_avoid_regexp_ap.get() != NULL)
1065 regexp_text.append (m_avoid_regexp_ap->GetText());
1066 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1067 if (regexp_text.empty())
1068 m_avoid_regexp_ap.reset();
1069 else
1070 {
1071 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1072
1073 }
1074 }
1075}
1076
1077void
1078ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1079 bool pending)
1080{
1081 if (new_settings.get() == NULL)
1082 return;
1083
1084 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1085 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1086 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1087 else
1088 m_avoid_regexp_ap.reset ();
1089}
1090
Caroline Ticebcb5b452010-09-20 21:37:42 +00001091bool
Jim Ingham20594b12010-09-08 03:14:33 +00001092ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001093 const ConstString &var_name,
1094 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001095 Error *err)
Jim Ingham20594b12010-09-08 03:14:33 +00001096{
1097 if (var_name == StepAvoidRegexpVarName())
1098 {
1099 if (m_avoid_regexp_ap.get() != NULL)
1100 {
1101 std::string regexp_text("\"");
1102 regexp_text.append(m_avoid_regexp_ap->GetText());
1103 regexp_text.append ("\"");
1104 value.AppendString (regexp_text.c_str());
1105 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001106
Jim Ingham20594b12010-09-08 03:14:33 +00001107 }
1108 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00001109 {
1110 if (err)
1111 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1112 return false;
1113 }
1114 return true;
Jim Ingham20594b12010-09-08 03:14:33 +00001115}
1116
Jim Ingham20594b12010-09-08 03:14:33 +00001117const ConstString
1118ThreadInstanceSettings::CreateInstanceName ()
1119{
1120 static int instance_count = 1;
1121 StreamString sstr;
1122
1123 sstr.Printf ("thread_%d", instance_count);
1124 ++instance_count;
1125
1126 const ConstString ret_val (sstr.GetData());
1127 return ret_val;
1128}
1129
1130const ConstString &
1131ThreadInstanceSettings::StepAvoidRegexpVarName ()
1132{
1133 static ConstString run_args_var_name ("step-avoid-regexp");
1134
1135 return run_args_var_name;
1136}
1137
1138//--------------------------------------------------
1139// ThreadSettingsController Variable Tables
1140//--------------------------------------------------
1141
1142SettingEntry
1143Thread::ThreadSettingsController::global_settings_table[] =
1144{
1145 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1146 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1147};
1148
1149
1150SettingEntry
1151Thread::ThreadSettingsController::instance_settings_table[] =
1152{
1153 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1154 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1155 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1156};
1157
Jim Inghamccd584d2010-09-23 17:40:12 +00001158lldb::StackFrameSP
1159Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1160{
1161 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1162}