blob: 9bd7e76b8e03fad0f22705a6416d7ccc97717f7e [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_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 (),
Chris Lattner24943d22010-06-08 16:52:24 +000053 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);
Caroline Tice1ebef442010-09-27 00:30:10 +000065 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000066}
67
68
69Thread::~Thread()
70{
71 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
72 if (log)
73 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
74}
75
76int
77Thread::GetResumeSignal () const
78{
79 return m_resume_signal;
80}
81
82void
83Thread::SetResumeSignal (int signal)
84{
85 m_resume_signal = signal;
86}
87
88StateType
89Thread::GetResumeState () const
90{
91 return m_resume_state;
92}
93
94void
95Thread::SetResumeState (StateType state)
96{
97 m_resume_state = state;
98}
99
Jim Ingham6297a3a2010-10-20 00:39:53 +0000100lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000101Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000102{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000103 ThreadPlanSP plan_sp (GetCompletedPlan());
104 if (plan_sp)
105 return StopInfo::CreateStopReasonWithPlan (plan_sp);
106 else
107 return GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000108}
109
110bool
111Thread::ThreadStoppedForAReason (void)
112{
Greg Clayton643ee732010-08-04 01:40:35 +0000113 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000114}
115
116StateType
117Thread::GetState() const
118{
119 // If any other threads access this we will need a mutex for it
120 Mutex::Locker locker(m_state_mutex);
121 return m_state;
122}
123
124void
125Thread::SetState(StateType state)
126{
127 Mutex::Locker locker(m_state_mutex);
128 m_state = state;
129}
130
131void
132Thread::WillStop()
133{
134 ThreadPlan *current_plan = GetCurrentPlan();
135
136 // FIXME: I may decide to disallow threads with no plans. In which
137 // case this should go to an assert.
138
139 if (!current_plan)
140 return;
141
142 current_plan->WillStop();
143}
144
145void
146Thread::SetupForResume ()
147{
148 if (GetResumeState() != eStateSuspended)
149 {
150
151 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
152 // telling the current plan it will resume, since we might change what the current
153 // plan is.
154
155 lldb::addr_t pc = GetRegisterContext()->GetPC();
156 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
157 if (bp_site_sp && bp_site_sp->IsEnabled())
158 {
159 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
160 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000161
162 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000163
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000164 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
165 {
166 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
167 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000168 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000169 ThreadPlanSP step_bp_plan_sp;
170 step_bp_plan->SetPrivate (true);
171
Chris Lattner24943d22010-06-08 16:52:24 +0000172 if (GetCurrentPlan()->RunState() != eStateStepping)
173 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000174 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000175 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000176 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000177 QueueThreadPlan (step_bp_plan_sp, false);
178 }
179 }
180 }
181 }
182}
183
184bool
185Thread::WillResume (StateType resume_state)
186{
187 // At this point clear the completed plan stack.
188 m_completed_plan_stack.clear();
189 m_discarded_plan_stack.clear();
190
Greg Clayton643ee732010-08-04 01:40:35 +0000191 StopInfo *stop_info = GetPrivateStopReason().get();
192 if (stop_info)
193 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000194
195 // Tell all the plans that we are about to resume in case they need to clear any state.
196 // We distinguish between the plan on the top of the stack and the lower
197 // plans in case a plan needs to do any special business before it runs.
198
199 ThreadPlan *plan_ptr = GetCurrentPlan();
200 plan_ptr->WillResume(resume_state, true);
201
202 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
203 {
204 plan_ptr->WillResume (resume_state, false);
205 }
Greg Clayton643ee732010-08-04 01:40:35 +0000206
Greg Clayton643ee732010-08-04 01:40:35 +0000207 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000208 return true;
209}
210
211void
212Thread::DidResume ()
213{
214 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
215}
216
217bool
218Thread::ShouldStop (Event* event_ptr)
219{
220 ThreadPlan *current_plan = GetCurrentPlan();
221 bool should_stop = true;
222
223 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
224 if (log)
225 {
226 StreamString s;
227 DumpThreadPlans(&s);
228 log->PutCString (s.GetData());
229 }
230
231 if (current_plan->PlanExplainsStop())
232 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000233 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000234 while (1)
235 {
236 should_stop = current_plan->ShouldStop(event_ptr);
237 if (current_plan->MischiefManaged())
238 {
239 if (should_stop)
240 current_plan->WillStop();
241
242 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
243 // Otherwise, see if the plan's parent wants to stop.
244
245 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
246 {
247 PopPlan();
248 break;
249 }
250 else
251 {
252
253 PopPlan();
254
255 current_plan = GetCurrentPlan();
256 if (current_plan == NULL)
257 {
258 break;
259 }
260 }
261
262 }
263 else
264 {
265 break;
266 }
267 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000268 if (over_ride_stop)
269 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000270 }
271 else
272 {
273 // If the current plan doesn't explain the stop, then, find one that
274 // does and let it handle the situation.
275 ThreadPlan *plan_ptr = current_plan;
276 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
277 {
278 if (plan_ptr->PlanExplainsStop())
279 {
280 should_stop = plan_ptr->ShouldStop (event_ptr);
281 break;
282 }
283
284 }
285 }
286
287 return should_stop;
288}
289
290Vote
291Thread::ShouldReportStop (Event* event_ptr)
292{
293 StateType thread_state = GetResumeState ();
Greg Clayton5205f0b2010-09-03 17:10:42 +0000294 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
295
296 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
297 {
298 if (log)
299 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 +0000300 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000301 }
Chris Lattner24943d22010-06-08 16:52:24 +0000302
303 if (m_completed_plan_stack.size() > 0)
304 {
305 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000306 if (log)
307 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 +0000308 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
309 }
310 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000311 {
312 if (log)
313 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000314 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000315 }
Chris Lattner24943d22010-06-08 16:52:24 +0000316}
317
318Vote
319Thread::ShouldReportRun (Event* event_ptr)
320{
321 StateType thread_state = GetResumeState ();
322 if (thread_state == eStateSuspended
323 || thread_state == eStateInvalid)
324 return eVoteNoOpinion;
325
326 if (m_completed_plan_stack.size() > 0)
327 {
328 // Don't use GetCompletedPlan here, since that suppresses private plans.
329 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
330 }
331 else
332 return GetCurrentPlan()->ShouldReportRun (event_ptr);
333}
334
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000335bool
336Thread::MatchesSpec (const ThreadSpec *spec)
337{
338 if (spec == NULL)
339 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000340
Jim Ingham649492b2010-06-18 01:00:58 +0000341 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000342}
343
Chris Lattner24943d22010-06-08 16:52:24 +0000344void
345Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
346{
347 if (thread_plan_sp)
348 {
Jim Ingham6297a3a2010-10-20 00:39:53 +0000349 m_plan_stack.push_back (thread_plan_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000350
351 thread_plan_sp->DidPush();
352
353 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
354 if (log)
355 {
356 StreamString s;
357 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000358 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000359 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000360 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000361 }
362 }
363}
364
365void
366Thread::PopPlan ()
367{
368 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
369
Jim Ingham6297a3a2010-10-20 00:39:53 +0000370 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000371 return;
372 else
373 {
374 ThreadPlanSP &plan = m_plan_stack.back();
375 if (log)
376 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000377 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x, immediate = false.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379 m_completed_plan_stack.push_back (plan);
380 plan->WillPop();
381 m_plan_stack.pop_back();
382 }
383}
384
385void
386Thread::DiscardPlan ()
387{
388 if (m_plan_stack.size() > 1)
389 {
390 ThreadPlanSP &plan = m_plan_stack.back();
391 m_discarded_plan_stack.push_back (plan);
392 plan->WillPop();
393 m_plan_stack.pop_back();
394 }
395}
396
397ThreadPlan *
398Thread::GetCurrentPlan ()
399{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000400 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000401 return NULL;
402 else
403 return m_plan_stack.back().get();
404}
405
406ThreadPlanSP
407Thread::GetCompletedPlan ()
408{
409 ThreadPlanSP empty_plan_sp;
410 if (!m_completed_plan_stack.empty())
411 {
412 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
413 {
414 ThreadPlanSP completed_plan_sp;
415 completed_plan_sp = m_completed_plan_stack[i];
416 if (!completed_plan_sp->GetPrivate ())
417 return completed_plan_sp;
418 }
419 }
420 return empty_plan_sp;
421}
422
423bool
424Thread::IsThreadPlanDone (ThreadPlan *plan)
425{
426 ThreadPlanSP empty_plan_sp;
427 if (!m_completed_plan_stack.empty())
428 {
429 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
430 {
431 if (m_completed_plan_stack[i].get() == plan)
432 return true;
433 }
434 }
435 return false;
436}
437
438bool
439Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
440{
441 ThreadPlanSP empty_plan_sp;
442 if (!m_discarded_plan_stack.empty())
443 {
444 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
445 {
446 if (m_discarded_plan_stack[i].get() == plan)
447 return true;
448 }
449 }
450 return false;
451}
452
453ThreadPlan *
454Thread::GetPreviousPlan (ThreadPlan *current_plan)
455{
456 if (current_plan == NULL)
457 return NULL;
458
459 int stack_size = m_completed_plan_stack.size();
460 for (int i = stack_size - 1; i > 0; i--)
461 {
462 if (current_plan == m_completed_plan_stack[i].get())
463 return m_completed_plan_stack[i-1].get();
464 }
465
466 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
467 {
Chris Lattner24943d22010-06-08 16:52:24 +0000468 if (m_plan_stack.size() > 0)
469 return m_plan_stack.back().get();
470 else
471 return NULL;
472 }
473
474 stack_size = m_plan_stack.size();
475 for (int i = stack_size - 1; i > 0; i--)
476 {
477 if (current_plan == m_plan_stack[i].get())
478 return m_plan_stack[i-1].get();
479 }
480 return NULL;
481}
482
483void
484Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
485{
486 if (abort_other_plans)
487 DiscardThreadPlans(true);
488
489 PushPlan (thread_plan_sp);
490}
491
492void
493Thread::DiscardThreadPlans(bool force)
494{
495 // FIXME: It is not always safe to just discard plans. Some, like the step over
496 // breakpoint trap can't be discarded in general (though you can if you plan to
497 // force a return from a function, for instance.
498 // For now I'm just not clearing immediate plans, but I need a way for plans to
499 // say they really need to be kept on, and then a way to override that. Humm...
500
501 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
502 if (log)
503 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000504 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000505 }
506
507 if (force)
508 {
509 int stack_size = m_plan_stack.size();
510 for (int i = stack_size - 1; i > 0; i--)
511 {
512 DiscardPlan();
513 }
514 return;
515 }
516
517 while (1)
518 {
519
520 int master_plan_idx;
521 bool discard;
522
523 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
524 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
525 {
526 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
527 {
528 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
529 break;
530 }
531 }
532
533 if (discard)
534 {
535 // First pop all the dependent plans:
536 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
537 {
538
539 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
540 // for the plan leaves it in a state that it is safe to pop the plan
541 // with no more notice?
542 DiscardPlan();
543 }
544
545 // Now discard the master plan itself.
546 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
547 // discard it's dependent plans, but not it...
548 if (master_plan_idx > 0)
549 {
550 DiscardPlan();
551 }
552 }
553 else
554 {
555 // If the master plan doesn't want to get discarded, then we're done.
556 break;
557 }
558
559 }
560 // FIXME: What should we do about the immediate plans?
561}
562
563ThreadPlan *
564Thread::QueueFundamentalPlan (bool abort_other_plans)
565{
566 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
567 QueueThreadPlan (thread_plan_sp, abort_other_plans);
568 return thread_plan_sp.get();
569}
570
571ThreadPlan *
572Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
573{
574 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
575 QueueThreadPlan (thread_plan_sp, abort_other_plans);
576 return thread_plan_sp.get();
577}
578
579ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000580Thread::QueueThreadPlanForStepRange
581(
582 bool abort_other_plans,
583 StepType type,
584 const AddressRange &range,
585 const SymbolContext &addr_context,
586 lldb::RunMode stop_other_threads,
587 bool avoid_code_without_debug_info
588)
Chris Lattner24943d22010-06-08 16:52:24 +0000589{
590 ThreadPlanSP thread_plan_sp;
591 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000592 {
593 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
594 if (avoid_code_without_debug_info)
595 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
596 else
597 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
598 thread_plan_sp.reset (plan);
599 }
Chris Lattner24943d22010-06-08 16:52:24 +0000600 else
601 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
602
603 QueueThreadPlan (thread_plan_sp, abort_other_plans);
604 return thread_plan_sp.get();
605}
606
607
608ThreadPlan *
609Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
610{
611 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
612 QueueThreadPlan (thread_plan_sp, abort_other_plans);
613 return thread_plan_sp.get();
614}
615
616ThreadPlan *
617Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
618 bool stop_other_threads, Vote stop_vote, Vote run_vote)
619{
620 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
621 QueueThreadPlan (thread_plan_sp, abort_other_plans);
622 return thread_plan_sp.get();
623}
624
625ThreadPlan *
626Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
627{
Jim Inghamb66cd072010-09-28 01:25:32 +0000628 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000629 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000630 // If that didn't come up with anything, try the ObjC runtime plugin:
631 if (thread_plan_sp.get() == NULL)
632 {
633 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
634 if (objc_runtime)
635 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
636 }
637
Chris Lattner24943d22010-06-08 16:52:24 +0000638 if (thread_plan_sp.get() == NULL)
639 {
640 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
641 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000642 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000643 }
644 QueueThreadPlan (thread_plan_sp, abort_other_plans);
645 return thread_plan_sp.get();
646}
647
648ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000649Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
650 Address& function,
651 lldb::addr_t arg,
652 bool stop_other_threads,
653 bool discard_on_error)
654{
655 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
656 QueueThreadPlan (thread_plan_sp, abort_other_plans);
657 return thread_plan_sp.get();
658}
659
660ThreadPlan *
661Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
662 Address& function,
663 ValueList &args,
664 bool stop_other_threads,
665 bool discard_on_error)
666{
667 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error));
668 QueueThreadPlan (thread_plan_sp, abort_other_plans);
669 return thread_plan_sp.get();
670}
671
672ThreadPlan *
673Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
674 Address &target_addr,
675 bool stop_other_threads)
676{
677 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
678 QueueThreadPlan (thread_plan_sp, abort_other_plans);
679 return thread_plan_sp.get();
680}
681
682ThreadPlan *
683Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
684 lldb::addr_t *address_list,
685 size_t num_addresses,
686 bool stop_other_threads)
687{
688 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
689 QueueThreadPlan (thread_plan_sp, abort_other_plans);
690 return thread_plan_sp.get();
691
692}
693
694uint32_t
695Thread::GetIndexID () const
696{
697 return m_index_id;
698}
699
700void
701Thread::DumpThreadPlans (lldb_private::Stream *s) const
702{
703 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000704 int i;
705 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
706 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000707 {
708 s->Printf ("Element %d: ", i);
709 s->IndentMore();
710 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
711 s->IndentLess();
712 s->EOL();
713 }
714
Chris Lattner24943d22010-06-08 16:52:24 +0000715 stack_size = m_completed_plan_stack.size();
716 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000717 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000718 {
719 s->Printf ("Element %d: ", i);
720 s->IndentMore();
721 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
722 s->IndentLess();
723 s->EOL();
724 }
725
726 stack_size = m_discarded_plan_stack.size();
727 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000728 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000729 {
730 s->Printf ("Element %d: ", i);
731 s->IndentMore();
732 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
733 s->IndentLess();
734 s->EOL();
735 }
736
737}
738
739Target *
740Thread::CalculateTarget ()
741{
742 return m_process.CalculateTarget();
743}
744
745Process *
746Thread::CalculateProcess ()
747{
748 return &m_process;
749}
750
751Thread *
752Thread::CalculateThread ()
753{
754 return this;
755}
756
757StackFrame *
758Thread::CalculateStackFrame ()
759{
760 return NULL;
761}
762
763void
Greg Claytona830adb2010-10-04 01:05:56 +0000764Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000765{
Greg Claytona830adb2010-10-04 01:05:56 +0000766 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000767 exe_ctx.thread = this;
768 exe_ctx.frame = NULL;
769}
770
Greg Clayton782b9cc2010-08-25 00:35:26 +0000771
772StackFrameList &
773Thread::GetStackFrameList ()
774{
Greg Claytonf40e3082010-08-26 02:28:22 +0000775 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000776 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000777 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000778}
779
780
781
Jim Ingham71219082010-08-12 02:14:28 +0000782uint32_t
783Thread::GetStackFrameCount()
784{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000785 return GetStackFrameList().GetNumFrames();
786}
Greg Clayton33ed1702010-08-24 00:45:41 +0000787
Greg Clayton33ed1702010-08-24 00:45:41 +0000788
Greg Clayton782b9cc2010-08-25 00:35:26 +0000789void
790Thread::ClearStackFrames ()
791{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000792 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
793 m_prev_frames_sp.reset (m_curr_frames_ap.release());
794 else
795 m_curr_frames_ap.release();
796
797// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
798// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000799}
800
801lldb::StackFrameSP
802Thread::GetStackFrameAtIndex (uint32_t idx)
803{
Greg Clayton72b71582010-09-02 21:44:10 +0000804 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000805}
806
Greg Claytonc12b6b42010-10-10 22:28:11 +0000807uint32_t
808Thread::GetSelectedFrameIndex ()
809{
810 return GetStackFrameList().GetSelectedFrameIndex();
811}
812
813
Chris Lattner24943d22010-06-08 16:52:24 +0000814lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000815Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000816{
Jim Inghamc8332952010-08-26 21:32:51 +0000817 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000818}
819
820uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000821Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000822{
Jim Inghamc8332952010-08-26 21:32:51 +0000823 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000824}
825
826void
Jim Inghamc8332952010-08-26 21:32:51 +0000827Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000828{
Jim Inghamc8332952010-08-26 21:32:51 +0000829 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000830}
831
832void
Greg Claytona830adb2010-10-04 01:05:56 +0000833Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000834{
Greg Claytona830adb2010-10-04 01:05:56 +0000835 ExecutionContext exe_ctx;
836 SymbolContext frame_sc;
837 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000838
Greg Claytona830adb2010-10-04 01:05:56 +0000839 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000840 {
Greg Claytona830adb2010-10-04 01:05:56 +0000841 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000842 if (frame_sp)
843 {
Greg Claytona830adb2010-10-04 01:05:56 +0000844 exe_ctx.frame = frame_sp.get();
845 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000846 }
847 }
848
Greg Claytona830adb2010-10-04 01:05:56 +0000849 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
850 assert (thread_format);
851 const char *end = NULL;
852 Debugger::FormatPrompt (thread_format,
853 exe_ctx.frame ? &frame_sc : NULL,
854 &exe_ctx,
855 NULL,
856 strm,
857 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000858}
859
860lldb::ThreadSP
861Thread::GetSP ()
862{
863 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
864}
Jim Ingham20594b12010-09-08 03:14:33 +0000865
866lldb::UserSettingsControllerSP
867Thread::GetSettingsController (bool finish)
868{
869 static UserSettingsControllerSP g_settings_controller (new ThreadSettingsController);
870 static bool initialized = false;
871
872 if (!initialized)
873 {
874 initialized = UserSettingsController::InitializeSettingsController (g_settings_controller,
875 Thread::ThreadSettingsController::global_settings_table,
876 Thread::ThreadSettingsController::instance_settings_table);
877 }
878
879 if (finish)
880 {
881 UserSettingsController::FinalizeSettingsController (g_settings_controller);
882 g_settings_controller.reset();
883 initialized = false;
884 }
885
886 return g_settings_controller;
887}
888
Caroline Tice1ebef442010-09-27 00:30:10 +0000889void
890Thread::UpdateInstanceName ()
891{
892 StreamString sstr;
893 const char *name = GetName();
894
895 if (name && name[0] != '\0')
896 sstr.Printf ("%s", name);
897 else if ((GetIndexID() != 0) || (GetID() != 0))
898 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
899
900 if (sstr.GetSize() > 0)
901 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
902}
903
Jim Ingham20594b12010-09-08 03:14:33 +0000904//--------------------------------------------------------------
905// class Thread::ThreadSettingsController
906//--------------------------------------------------------------
907
908Thread::ThreadSettingsController::ThreadSettingsController () :
909 UserSettingsController ("thread", Process::GetSettingsController())
910{
Caroline Tice004afcb2010-09-08 17:48:55 +0000911 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
912 InstanceSettings::GetDefaultName().AsCString()));
Jim Ingham20594b12010-09-08 03:14:33 +0000913}
914
915Thread::ThreadSettingsController::~ThreadSettingsController ()
916{
917}
918
919lldb::InstanceSettingsSP
Greg Claytond0a5a232010-09-19 02:33:57 +0000920Thread::ThreadSettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham20594b12010-09-08 03:14:33 +0000921{
Caroline Tice004afcb2010-09-08 17:48:55 +0000922 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*(Thread::GetSettingsController().get()),
923 false, instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000924 lldb::InstanceSettingsSP new_settings_sp (new_settings);
925 return new_settings_sp;
926}
927
928//--------------------------------------------------------------
929// class ThreadInstanceSettings
930//--------------------------------------------------------------
931
Caroline Tice004afcb2010-09-08 17:48:55 +0000932ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Caroline Tice75b11a32010-09-16 19:05:55 +0000933 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance),
Jim Ingham20594b12010-09-08 03:14:33 +0000934 m_avoid_regexp_ap ()
935{
Caroline Tice396704b2010-09-09 18:26:37 +0000936 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
937 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
938 // 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 +0000939 // This is true for CreateInstanceName() too.
940
941 if (GetInstanceName() == InstanceSettings::InvalidName())
942 {
943 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
944 m_owner.RegisterInstanceSettings (this);
945 }
Caroline Tice396704b2010-09-09 18:26:37 +0000946
947 if (live_instance)
Jim Ingham20594b12010-09-08 03:14:33 +0000948 {
949 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
950 CopyInstanceSettings (pending_settings,false);
Caroline Tice396704b2010-09-09 18:26:37 +0000951 //m_owner.RemovePendingSettings (m_instance_name);
Jim Ingham20594b12010-09-08 03:14:33 +0000952 }
953}
954
955ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
956 InstanceSettings (*(Thread::GetSettingsController().get()), CreateInstanceName().AsCString()),
957 m_avoid_regexp_ap ()
958{
959 if (m_instance_name != InstanceSettings::GetDefaultName())
960 {
961 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
962 CopyInstanceSettings (pending_settings,false);
963 m_owner.RemovePendingSettings (m_instance_name);
964 }
965 if (rhs.m_avoid_regexp_ap.get() != NULL)
966 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
967}
968
969ThreadInstanceSettings::~ThreadInstanceSettings ()
970{
971}
972
973ThreadInstanceSettings&
974ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
975{
976 if (this != &rhs)
977 {
978 if (rhs.m_avoid_regexp_ap.get() != NULL)
979 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
980 else
981 m_avoid_regexp_ap.reset(NULL);
982 }
983
984 return *this;
985}
986
987
988void
989ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
990 const char *index_value,
991 const char *value,
992 const ConstString &instance_name,
993 const SettingEntry &entry,
994 lldb::VarSetOperationType op,
995 Error &err,
996 bool pending)
997{
998 if (var_name == StepAvoidRegexpVarName())
999 {
1000 std::string regexp_text;
1001 if (m_avoid_regexp_ap.get() != NULL)
1002 regexp_text.append (m_avoid_regexp_ap->GetText());
1003 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1004 if (regexp_text.empty())
1005 m_avoid_regexp_ap.reset();
1006 else
1007 {
1008 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1009
1010 }
1011 }
1012}
1013
1014void
1015ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1016 bool pending)
1017{
1018 if (new_settings.get() == NULL)
1019 return;
1020
1021 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1022 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1023 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1024 else
1025 m_avoid_regexp_ap.reset ();
1026}
1027
Caroline Ticebcb5b452010-09-20 21:37:42 +00001028bool
Jim Ingham20594b12010-09-08 03:14:33 +00001029ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001030 const ConstString &var_name,
1031 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001032 Error *err)
Jim Ingham20594b12010-09-08 03:14:33 +00001033{
1034 if (var_name == StepAvoidRegexpVarName())
1035 {
1036 if (m_avoid_regexp_ap.get() != NULL)
1037 {
1038 std::string regexp_text("\"");
1039 regexp_text.append(m_avoid_regexp_ap->GetText());
1040 regexp_text.append ("\"");
1041 value.AppendString (regexp_text.c_str());
1042 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001043
Jim Ingham20594b12010-09-08 03:14:33 +00001044 }
1045 else
Caroline Ticebcb5b452010-09-20 21:37:42 +00001046 {
1047 if (err)
1048 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1049 return false;
1050 }
1051 return true;
Jim Ingham20594b12010-09-08 03:14:33 +00001052}
1053
Jim Ingham20594b12010-09-08 03:14:33 +00001054const ConstString
1055ThreadInstanceSettings::CreateInstanceName ()
1056{
1057 static int instance_count = 1;
1058 StreamString sstr;
1059
1060 sstr.Printf ("thread_%d", instance_count);
1061 ++instance_count;
1062
1063 const ConstString ret_val (sstr.GetData());
1064 return ret_val;
1065}
1066
1067const ConstString &
1068ThreadInstanceSettings::StepAvoidRegexpVarName ()
1069{
1070 static ConstString run_args_var_name ("step-avoid-regexp");
1071
1072 return run_args_var_name;
1073}
1074
1075//--------------------------------------------------
1076// ThreadSettingsController Variable Tables
1077//--------------------------------------------------
1078
1079SettingEntry
1080Thread::ThreadSettingsController::global_settings_table[] =
1081{
1082 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1083 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1084};
1085
1086
1087SettingEntry
1088Thread::ThreadSettingsController::instance_settings_table[] =
1089{
1090 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1091 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1092 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1093};
1094
Jim Inghamccd584d2010-09-23 17:40:12 +00001095lldb::StackFrameSP
1096Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1097{
1098 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1099}
Caroline Tice7826c882010-10-26 03:11:13 +00001100
1101const char *
1102Thread::StopReasonAsCString (lldb::StopReason reason)
1103{
1104 switch (reason)
1105 {
1106 case eStopReasonInvalid: return "invalid";
1107 case eStopReasonNone: return "none";
1108 case eStopReasonTrace: return "trace";
1109 case eStopReasonBreakpoint: return "breakpoint";
1110 case eStopReasonWatchpoint: return "watchpoint";
1111 case eStopReasonSignal: return "signal";
1112 case eStopReasonException: return "exception";
1113 case eStopReasonPlanComplete: return "plan complete";
1114 }
1115
1116
1117 static char unknown_state_string[64];
1118 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1119 return unknown_state_string;
1120}
1121
1122const char *
1123Thread::RunModeAsCString (lldb::RunMode mode)
1124{
1125 switch (mode)
1126 {
1127 case eOnlyThisThread: return "only this thread";
1128 case eAllThreads: return "all threads";
1129 case eOnlyDuringStepping: return "only during stepping";
1130 }
1131
1132 static char unknown_state_string[64];
1133 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1134 return unknown_state_string;
1135}