blob: 2c544a2ca5b9fddcf39bcba56f4e9efe463713f2 [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),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000045 ThreadInstanceSettings (*GetSettingsController()),
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),
Jim Inghamcdea2362010-11-18 02:47:07 +000057 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000058 m_destroy_called (false),
59 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +000060
Chris Lattner24943d22010-06-08 16:52:24 +000061{
Greg Claytone005f2c2010-11-06 01:53:30 +000062 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000063 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{
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (log)
75 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000076 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
77 assert (m_destroy_called);
78}
79
80void
81Thread::DestroyThread ()
82{
83 m_plan_stack.clear();
84 m_discarded_plan_stack.clear();
85 m_completed_plan_stack.clear();
86 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +000087}
88
Jim Ingham6297a3a2010-10-20 00:39:53 +000089lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +000090Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000091{
Jim Ingham6297a3a2010-10-20 00:39:53 +000092 ThreadPlanSP plan_sp (GetCompletedPlan());
93 if (plan_sp)
94 return StopInfo::CreateStopReasonWithPlan (plan_sp);
95 else
96 return GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
Jim Ingham15dcb7c2011-01-20 02:03:18 +000099void
100Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
101{
102 m_actual_stop_info_sp = stop_info_sp;
103 m_thread_stop_reason_stop_id = GetProcess().GetStopID();
104}
105
106void
107Thread::SetStopInfoToNothing()
108{
109 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
110 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
111 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
112}
113
Chris Lattner24943d22010-06-08 16:52:24 +0000114bool
115Thread::ThreadStoppedForAReason (void)
116{
Greg Clayton643ee732010-08-04 01:40:35 +0000117 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000118}
119
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000120bool
121Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
122{
123 if (!SaveFrameZeroState(saved_state.register_backup))
124 return false;
125
126 saved_state.stop_info_sp = GetStopInfo();
127 saved_state.orig_stop_id = GetProcess().GetStopID();
128
129 return true;
130}
131
132bool
133Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
134{
135 RestoreSaveFrameZero(saved_state.register_backup);
136 saved_state.stop_info_sp->MakeStopInfoValid();
137 SetStopInfo(saved_state.stop_info_sp);
138 return true;
139}
140
Chris Lattner24943d22010-06-08 16:52:24 +0000141StateType
142Thread::GetState() const
143{
144 // If any other threads access this we will need a mutex for it
145 Mutex::Locker locker(m_state_mutex);
146 return m_state;
147}
148
149void
150Thread::SetState(StateType state)
151{
152 Mutex::Locker locker(m_state_mutex);
153 m_state = state;
154}
155
156void
157Thread::WillStop()
158{
159 ThreadPlan *current_plan = GetCurrentPlan();
160
161 // FIXME: I may decide to disallow threads with no plans. In which
162 // case this should go to an assert.
163
164 if (!current_plan)
165 return;
166
167 current_plan->WillStop();
168}
169
170void
171Thread::SetupForResume ()
172{
173 if (GetResumeState() != eStateSuspended)
174 {
175
176 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
177 // telling the current plan it will resume, since we might change what the current
178 // plan is.
179
180 lldb::addr_t pc = GetRegisterContext()->GetPC();
181 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
182 if (bp_site_sp && bp_site_sp->IsEnabled())
183 {
184 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
185 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000186
187 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000188
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000189 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
190 {
191 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
192 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000193 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000194 ThreadPlanSP step_bp_plan_sp;
195 step_bp_plan->SetPrivate (true);
196
Chris Lattner24943d22010-06-08 16:52:24 +0000197 if (GetCurrentPlan()->RunState() != eStateStepping)
198 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000199 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000200 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000201 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000202 QueueThreadPlan (step_bp_plan_sp, false);
203 }
204 }
205 }
206 }
207}
208
209bool
210Thread::WillResume (StateType resume_state)
211{
212 // At this point clear the completed plan stack.
213 m_completed_plan_stack.clear();
214 m_discarded_plan_stack.clear();
215
Greg Clayton643ee732010-08-04 01:40:35 +0000216 StopInfo *stop_info = GetPrivateStopReason().get();
217 if (stop_info)
218 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000219
220 // Tell all the plans that we are about to resume in case they need to clear any state.
221 // We distinguish between the plan on the top of the stack and the lower
222 // plans in case a plan needs to do any special business before it runs.
223
224 ThreadPlan *plan_ptr = GetCurrentPlan();
225 plan_ptr->WillResume(resume_state, true);
226
227 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
228 {
229 plan_ptr->WillResume (resume_state, false);
230 }
Greg Clayton643ee732010-08-04 01:40:35 +0000231
Greg Clayton643ee732010-08-04 01:40:35 +0000232 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000233 return true;
234}
235
236void
237Thread::DidResume ()
238{
239 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
240}
241
242bool
243Thread::ShouldStop (Event* event_ptr)
244{
245 ThreadPlan *current_plan = GetCurrentPlan();
246 bool should_stop = true;
247
Greg Claytone005f2c2010-11-06 01:53:30 +0000248 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000249 if (log)
250 {
251 StreamString s;
252 DumpThreadPlans(&s);
253 log->PutCString (s.GetData());
254 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000255
256 // The top most plan always gets to do the trace log...
257 current_plan->DoTraceLog ();
Chris Lattner24943d22010-06-08 16:52:24 +0000258
259 if (current_plan->PlanExplainsStop())
260 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000261 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000262 while (1)
263 {
264 should_stop = current_plan->ShouldStop(event_ptr);
265 if (current_plan->MischiefManaged())
266 {
267 if (should_stop)
268 current_plan->WillStop();
269
270 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
271 // Otherwise, see if the plan's parent wants to stop.
272
273 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
274 {
275 PopPlan();
276 break;
277 }
278 else
279 {
280
281 PopPlan();
282
283 current_plan = GetCurrentPlan();
284 if (current_plan == NULL)
285 {
286 break;
287 }
288 }
289
290 }
291 else
292 {
293 break;
294 }
295 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000296 if (over_ride_stop)
297 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000298 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000299 else if (current_plan->TracerExplainsStop())
300 {
301 return false;
302 }
Chris Lattner24943d22010-06-08 16:52:24 +0000303 else
304 {
305 // If the current plan doesn't explain the stop, then, find one that
306 // does and let it handle the situation.
307 ThreadPlan *plan_ptr = current_plan;
308 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
309 {
310 if (plan_ptr->PlanExplainsStop())
311 {
312 should_stop = plan_ptr->ShouldStop (event_ptr);
313 break;
314 }
315
316 }
317 }
318
319 return should_stop;
320}
321
322Vote
323Thread::ShouldReportStop (Event* event_ptr)
324{
325 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000326 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000327
328 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
329 {
330 if (log)
331 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 +0000332 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000333 }
Chris Lattner24943d22010-06-08 16:52:24 +0000334
335 if (m_completed_plan_stack.size() > 0)
336 {
337 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000338 if (log)
339 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 +0000340 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
341 }
342 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000343 {
344 if (log)
345 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000346 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000347 }
Chris Lattner24943d22010-06-08 16:52:24 +0000348}
349
350Vote
351Thread::ShouldReportRun (Event* event_ptr)
352{
353 StateType thread_state = GetResumeState ();
354 if (thread_state == eStateSuspended
355 || thread_state == eStateInvalid)
356 return eVoteNoOpinion;
357
358 if (m_completed_plan_stack.size() > 0)
359 {
360 // Don't use GetCompletedPlan here, since that suppresses private plans.
361 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
362 }
363 else
364 return GetCurrentPlan()->ShouldReportRun (event_ptr);
365}
366
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000367bool
368Thread::MatchesSpec (const ThreadSpec *spec)
369{
370 if (spec == NULL)
371 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000372
Jim Ingham649492b2010-06-18 01:00:58 +0000373 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000374}
375
Chris Lattner24943d22010-06-08 16:52:24 +0000376void
377Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
378{
379 if (thread_plan_sp)
380 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000381 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
382 if (!thread_plan_sp->GetThreadPlanTracer())
383 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000384 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000385
Chris Lattner24943d22010-06-08 16:52:24 +0000386 thread_plan_sp->DidPush();
387
Greg Claytone005f2c2010-11-06 01:53:30 +0000388 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000389 if (log)
390 {
391 StreamString s;
392 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000393 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000394 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000395 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000396 }
397 }
398}
399
400void
401Thread::PopPlan ()
402{
Greg Claytone005f2c2010-11-06 01:53:30 +0000403 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000404
Jim Ingham6297a3a2010-10-20 00:39:53 +0000405 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000406 return;
407 else
408 {
409 ThreadPlanSP &plan = m_plan_stack.back();
410 if (log)
411 {
Jim Ingham83e8b9d2010-11-10 18:17:03 +0000412 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000413 }
414 m_completed_plan_stack.push_back (plan);
415 plan->WillPop();
416 m_plan_stack.pop_back();
417 }
418}
419
420void
421Thread::DiscardPlan ()
422{
423 if (m_plan_stack.size() > 1)
424 {
425 ThreadPlanSP &plan = m_plan_stack.back();
426 m_discarded_plan_stack.push_back (plan);
427 plan->WillPop();
428 m_plan_stack.pop_back();
429 }
430}
431
432ThreadPlan *
433Thread::GetCurrentPlan ()
434{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000435 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000436 return NULL;
437 else
438 return m_plan_stack.back().get();
439}
440
441ThreadPlanSP
442Thread::GetCompletedPlan ()
443{
444 ThreadPlanSP empty_plan_sp;
445 if (!m_completed_plan_stack.empty())
446 {
447 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
448 {
449 ThreadPlanSP completed_plan_sp;
450 completed_plan_sp = m_completed_plan_stack[i];
451 if (!completed_plan_sp->GetPrivate ())
452 return completed_plan_sp;
453 }
454 }
455 return empty_plan_sp;
456}
457
458bool
459Thread::IsThreadPlanDone (ThreadPlan *plan)
460{
461 ThreadPlanSP empty_plan_sp;
462 if (!m_completed_plan_stack.empty())
463 {
464 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
465 {
466 if (m_completed_plan_stack[i].get() == plan)
467 return true;
468 }
469 }
470 return false;
471}
472
473bool
474Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
475{
476 ThreadPlanSP empty_plan_sp;
477 if (!m_discarded_plan_stack.empty())
478 {
479 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
480 {
481 if (m_discarded_plan_stack[i].get() == plan)
482 return true;
483 }
484 }
485 return false;
486}
487
488ThreadPlan *
489Thread::GetPreviousPlan (ThreadPlan *current_plan)
490{
491 if (current_plan == NULL)
492 return NULL;
493
494 int stack_size = m_completed_plan_stack.size();
495 for (int i = stack_size - 1; i > 0; i--)
496 {
497 if (current_plan == m_completed_plan_stack[i].get())
498 return m_completed_plan_stack[i-1].get();
499 }
500
501 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
502 {
Chris Lattner24943d22010-06-08 16:52:24 +0000503 if (m_plan_stack.size() > 0)
504 return m_plan_stack.back().get();
505 else
506 return NULL;
507 }
508
509 stack_size = m_plan_stack.size();
510 for (int i = stack_size - 1; i > 0; i--)
511 {
512 if (current_plan == m_plan_stack[i].get())
513 return m_plan_stack[i-1].get();
514 }
515 return NULL;
516}
517
518void
519Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
520{
521 if (abort_other_plans)
522 DiscardThreadPlans(true);
523
524 PushPlan (thread_plan_sp);
525}
526
Jim Ingham745ac7a2010-11-11 19:26:09 +0000527
528void
529Thread::EnableTracer (bool value, bool single_stepping)
530{
531 int stack_size = m_plan_stack.size();
532 for (int i = 0; i < stack_size; i++)
533 {
534 if (m_plan_stack[i]->GetThreadPlanTracer())
535 {
536 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
537 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
538 }
539 }
540}
541
542void
543Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
544{
545 int stack_size = m_plan_stack.size();
546 for (int i = 0; i < stack_size; i++)
547 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
548}
549
Chris Lattner24943d22010-06-08 16:52:24 +0000550void
Jim Inghamea9d4262010-11-05 19:25:48 +0000551Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
552{
Greg Claytone005f2c2010-11-06 01:53:30 +0000553 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000554 if (log)
555 {
556 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get());
557 }
558
559 int stack_size = m_plan_stack.size();
560
561 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
562 // stack, and if so discard up to and including it.
563
564 if (up_to_plan_sp.get() == NULL)
565 {
566 for (int i = stack_size - 1; i > 0; i--)
567 DiscardPlan();
568 }
569 else
570 {
571 bool found_it = false;
572 for (int i = stack_size - 1; i > 0; i--)
573 {
574 if (m_plan_stack[i] == up_to_plan_sp)
575 found_it = true;
576 }
577 if (found_it)
578 {
579 bool last_one = false;
580 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
581 {
582 if (GetCurrentPlan() == up_to_plan_sp.get())
583 last_one = true;
584 DiscardPlan();
585 }
586 }
587 }
588 return;
589}
590
591void
Chris Lattner24943d22010-06-08 16:52:24 +0000592Thread::DiscardThreadPlans(bool force)
593{
Greg Claytone005f2c2010-11-06 01:53:30 +0000594 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000595 if (log)
596 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000597 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000598 }
599
600 if (force)
601 {
602 int stack_size = m_plan_stack.size();
603 for (int i = stack_size - 1; i > 0; i--)
604 {
605 DiscardPlan();
606 }
607 return;
608 }
609
610 while (1)
611 {
612
613 int master_plan_idx;
614 bool discard;
615
616 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
617 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
618 {
619 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
620 {
621 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
622 break;
623 }
624 }
625
626 if (discard)
627 {
628 // First pop all the dependent plans:
629 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
630 {
631
632 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
633 // for the plan leaves it in a state that it is safe to pop the plan
634 // with no more notice?
635 DiscardPlan();
636 }
637
638 // Now discard the master plan itself.
639 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
640 // discard it's dependent plans, but not it...
641 if (master_plan_idx > 0)
642 {
643 DiscardPlan();
644 }
645 }
646 else
647 {
648 // If the master plan doesn't want to get discarded, then we're done.
649 break;
650 }
651
652 }
Chris Lattner24943d22010-06-08 16:52:24 +0000653}
654
655ThreadPlan *
656Thread::QueueFundamentalPlan (bool abort_other_plans)
657{
658 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
659 QueueThreadPlan (thread_plan_sp, abort_other_plans);
660 return thread_plan_sp.get();
661}
662
663ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000664Thread::QueueThreadPlanForStepSingleInstruction
665(
666 bool step_over,
667 bool abort_other_plans,
668 bool stop_other_threads
669)
Chris Lattner24943d22010-06-08 16:52:24 +0000670{
671 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
672 QueueThreadPlan (thread_plan_sp, abort_other_plans);
673 return thread_plan_sp.get();
674}
675
676ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000677Thread::QueueThreadPlanForStepRange
678(
679 bool abort_other_plans,
680 StepType type,
681 const AddressRange &range,
682 const SymbolContext &addr_context,
683 lldb::RunMode stop_other_threads,
684 bool avoid_code_without_debug_info
685)
Chris Lattner24943d22010-06-08 16:52:24 +0000686{
687 ThreadPlanSP thread_plan_sp;
688 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000689 {
690 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
691 if (avoid_code_without_debug_info)
692 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
693 else
694 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
695 thread_plan_sp.reset (plan);
696 }
Chris Lattner24943d22010-06-08 16:52:24 +0000697 else
698 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
699
700 QueueThreadPlan (thread_plan_sp, abort_other_plans);
701 return thread_plan_sp.get();
702}
703
704
705ThreadPlan *
706Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
707{
708 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
709 QueueThreadPlan (thread_plan_sp, abort_other_plans);
710 return thread_plan_sp.get();
711}
712
713ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000714Thread::QueueThreadPlanForStepOut
715(
716 bool abort_other_plans,
717 SymbolContext *addr_context,
718 bool first_insn,
719 bool stop_other_threads,
720 Vote stop_vote,
721 Vote run_vote,
722 uint32_t frame_idx
723)
Chris Lattner24943d22010-06-08 16:52:24 +0000724{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000725 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
726 addr_context,
727 first_insn,
728 stop_other_threads,
729 stop_vote,
730 run_vote,
731 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000732 QueueThreadPlan (thread_plan_sp, abort_other_plans);
733 return thread_plan_sp.get();
734}
735
736ThreadPlan *
737Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
738{
Jim Inghamb66cd072010-09-28 01:25:32 +0000739 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000740 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000741 // If that didn't come up with anything, try the ObjC runtime plugin:
742 if (thread_plan_sp.get() == NULL)
743 {
744 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
745 if (objc_runtime)
746 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
747 }
748
Chris Lattner24943d22010-06-08 16:52:24 +0000749 if (thread_plan_sp.get() == NULL)
750 {
751 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
752 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000753 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000754 }
755 QueueThreadPlan (thread_plan_sp, abort_other_plans);
756 return thread_plan_sp.get();
757}
758
759ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000760Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
761 Address& function,
762 lldb::addr_t arg,
763 bool stop_other_threads,
764 bool discard_on_error)
765{
766 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
767 QueueThreadPlan (thread_plan_sp, abort_other_plans);
768 return thread_plan_sp.get();
769}
770
771ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000772Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
773 Address &target_addr,
774 bool stop_other_threads)
775{
776 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
777 QueueThreadPlan (thread_plan_sp, abort_other_plans);
778 return thread_plan_sp.get();
779}
780
781ThreadPlan *
782Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000783 lldb::addr_t *address_list,
784 size_t num_addresses,
785 bool stop_other_threads,
786 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000787{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000788 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000789 QueueThreadPlan (thread_plan_sp, abort_other_plans);
790 return thread_plan_sp.get();
791
792}
793
794uint32_t
795Thread::GetIndexID () const
796{
797 return m_index_id;
798}
799
800void
801Thread::DumpThreadPlans (lldb_private::Stream *s) const
802{
803 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000804 int i;
805 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
806 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000807 {
808 s->Printf ("Element %d: ", i);
809 s->IndentMore();
810 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
811 s->IndentLess();
812 s->EOL();
813 }
814
Chris Lattner24943d22010-06-08 16:52:24 +0000815 stack_size = m_completed_plan_stack.size();
816 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000817 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000818 {
819 s->Printf ("Element %d: ", i);
820 s->IndentMore();
821 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
822 s->IndentLess();
823 s->EOL();
824 }
825
826 stack_size = m_discarded_plan_stack.size();
827 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000828 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000829 {
830 s->Printf ("Element %d: ", i);
831 s->IndentMore();
832 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
833 s->IndentLess();
834 s->EOL();
835 }
836
837}
838
839Target *
840Thread::CalculateTarget ()
841{
842 return m_process.CalculateTarget();
843}
844
845Process *
846Thread::CalculateProcess ()
847{
848 return &m_process;
849}
850
851Thread *
852Thread::CalculateThread ()
853{
854 return this;
855}
856
857StackFrame *
858Thread::CalculateStackFrame ()
859{
860 return NULL;
861}
862
863void
Greg Claytona830adb2010-10-04 01:05:56 +0000864Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000865{
Greg Claytona830adb2010-10-04 01:05:56 +0000866 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000867 exe_ctx.thread = this;
868 exe_ctx.frame = NULL;
869}
870
Greg Clayton782b9cc2010-08-25 00:35:26 +0000871
872StackFrameList &
873Thread::GetStackFrameList ()
874{
Greg Claytonf40e3082010-08-26 02:28:22 +0000875 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000876 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000877 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000878}
879
880
881
Jim Ingham71219082010-08-12 02:14:28 +0000882uint32_t
883Thread::GetStackFrameCount()
884{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000885 return GetStackFrameList().GetNumFrames();
886}
Greg Clayton33ed1702010-08-24 00:45:41 +0000887
Greg Clayton33ed1702010-08-24 00:45:41 +0000888
Greg Clayton782b9cc2010-08-25 00:35:26 +0000889void
890Thread::ClearStackFrames ()
891{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000892 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
893 m_prev_frames_sp.reset (m_curr_frames_ap.release());
894 else
895 m_curr_frames_ap.release();
896
897// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
898// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000899}
900
901lldb::StackFrameSP
902Thread::GetStackFrameAtIndex (uint32_t idx)
903{
Greg Clayton72b71582010-09-02 21:44:10 +0000904 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000905}
906
Greg Claytonc12b6b42010-10-10 22:28:11 +0000907uint32_t
908Thread::GetSelectedFrameIndex ()
909{
910 return GetStackFrameList().GetSelectedFrameIndex();
911}
912
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000913lldb::StackFrameSP
914Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
915{
916 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
917}
918
919
Greg Claytonc12b6b42010-10-10 22:28:11 +0000920
Chris Lattner24943d22010-06-08 16:52:24 +0000921lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000922Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000923{
Jim Inghamc8332952010-08-26 21:32:51 +0000924 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000925}
926
927uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000928Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000929{
Jim Inghamc8332952010-08-26 21:32:51 +0000930 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000931}
932
933void
Jim Inghamc8332952010-08-26 21:32:51 +0000934Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000935{
Jim Inghamc8332952010-08-26 21:32:51 +0000936 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000937}
938
939void
Greg Claytona830adb2010-10-04 01:05:56 +0000940Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000941{
Greg Claytona830adb2010-10-04 01:05:56 +0000942 ExecutionContext exe_ctx;
943 SymbolContext frame_sc;
944 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000945
Greg Claytona830adb2010-10-04 01:05:56 +0000946 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000947 {
Greg Claytona830adb2010-10-04 01:05:56 +0000948 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000949 if (frame_sp)
950 {
Greg Claytona830adb2010-10-04 01:05:56 +0000951 exe_ctx.frame = frame_sp.get();
952 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000953 }
954 }
955
Greg Claytona830adb2010-10-04 01:05:56 +0000956 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
957 assert (thread_format);
958 const char *end = NULL;
959 Debugger::FormatPrompt (thread_format,
960 exe_ctx.frame ? &frame_sc : NULL,
961 &exe_ctx,
962 NULL,
963 strm,
964 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000965}
966
967lldb::ThreadSP
968Thread::GetSP ()
969{
970 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
971}
Jim Ingham20594b12010-09-08 03:14:33 +0000972
Greg Clayton990de7b2010-11-18 23:32:35 +0000973
974void
975Thread::Initialize ()
Jim Ingham20594b12010-09-08 03:14:33 +0000976{
Greg Clayton990de7b2010-11-18 23:32:35 +0000977 UserSettingsControllerSP &usc = GetSettingsController();
978 usc.reset (new SettingsController);
979 UserSettingsController::InitializeSettingsController (usc,
980 SettingsController::global_settings_table,
981 SettingsController::instance_settings_table);
982}
Jim Ingham20594b12010-09-08 03:14:33 +0000983
Greg Clayton990de7b2010-11-18 23:32:35 +0000984void
985Thread::Terminate ()
986{
987 UserSettingsControllerSP &usc = GetSettingsController();
988 UserSettingsController::FinalizeSettingsController (usc);
989 usc.reset();
990}
Jim Ingham20594b12010-09-08 03:14:33 +0000991
Greg Clayton990de7b2010-11-18 23:32:35 +0000992UserSettingsControllerSP &
993Thread::GetSettingsController ()
994{
995 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +0000996 return g_settings_controller;
997}
998
Caroline Tice1ebef442010-09-27 00:30:10 +0000999void
1000Thread::UpdateInstanceName ()
1001{
1002 StreamString sstr;
1003 const char *name = GetName();
1004
1005 if (name && name[0] != '\0')
1006 sstr.Printf ("%s", name);
1007 else if ((GetIndexID() != 0) || (GetID() != 0))
1008 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1009
1010 if (sstr.GetSize() > 0)
1011 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1012}
1013
Jim Inghamccd584d2010-09-23 17:40:12 +00001014lldb::StackFrameSP
1015Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1016{
1017 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1018}
Caroline Tice7826c882010-10-26 03:11:13 +00001019
1020const char *
1021Thread::StopReasonAsCString (lldb::StopReason reason)
1022{
1023 switch (reason)
1024 {
1025 case eStopReasonInvalid: return "invalid";
1026 case eStopReasonNone: return "none";
1027 case eStopReasonTrace: return "trace";
1028 case eStopReasonBreakpoint: return "breakpoint";
1029 case eStopReasonWatchpoint: return "watchpoint";
1030 case eStopReasonSignal: return "signal";
1031 case eStopReasonException: return "exception";
1032 case eStopReasonPlanComplete: return "plan complete";
1033 }
1034
1035
1036 static char unknown_state_string[64];
1037 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1038 return unknown_state_string;
1039}
1040
1041const char *
1042Thread::RunModeAsCString (lldb::RunMode mode)
1043{
1044 switch (mode)
1045 {
1046 case eOnlyThisThread: return "only this thread";
1047 case eAllThreads: return "all threads";
1048 case eOnlyDuringStepping: return "only during stepping";
1049 }
1050
1051 static char unknown_state_string[64];
1052 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1053 return unknown_state_string;
1054}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001055
Greg Clayton990de7b2010-11-18 23:32:35 +00001056#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001057//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001058// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001059//--------------------------------------------------------------
1060
Greg Clayton990de7b2010-11-18 23:32:35 +00001061Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001062 UserSettingsController ("thread", Process::GetSettingsController())
1063{
1064 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1065 InstanceSettings::GetDefaultName().AsCString()));
1066}
1067
Greg Clayton990de7b2010-11-18 23:32:35 +00001068Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001069{
1070}
1071
1072lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001073Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001074{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001075 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1076 false,
1077 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001078 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1079 return new_settings_sp;
1080}
1081
1082#pragma mark "ThreadInstanceSettings"
1083//--------------------------------------------------------------
1084// class ThreadInstanceSettings
1085//--------------------------------------------------------------
1086
1087ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001088 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001089 m_avoid_regexp_ap (),
1090 m_trace_enabled (false)
1091{
1092 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1093 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1094 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1095 // This is true for CreateInstanceName() too.
1096
1097 if (GetInstanceName() == InstanceSettings::InvalidName())
1098 {
1099 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1100 m_owner.RegisterInstanceSettings (this);
1101 }
1102
1103 if (live_instance)
1104 {
1105 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1106 CopyInstanceSettings (pending_settings,false);
1107 //m_owner.RemovePendingSettings (m_instance_name);
1108 }
1109}
1110
1111ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001112 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001113 m_avoid_regexp_ap (),
1114 m_trace_enabled (rhs.m_trace_enabled)
1115{
1116 if (m_instance_name != InstanceSettings::GetDefaultName())
1117 {
1118 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1119 CopyInstanceSettings (pending_settings,false);
1120 m_owner.RemovePendingSettings (m_instance_name);
1121 }
1122 if (rhs.m_avoid_regexp_ap.get() != NULL)
1123 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1124}
1125
1126ThreadInstanceSettings::~ThreadInstanceSettings ()
1127{
1128}
1129
1130ThreadInstanceSettings&
1131ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1132{
1133 if (this != &rhs)
1134 {
1135 if (rhs.m_avoid_regexp_ap.get() != NULL)
1136 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1137 else
1138 m_avoid_regexp_ap.reset(NULL);
1139 }
1140 m_trace_enabled = rhs.m_trace_enabled;
1141 return *this;
1142}
1143
1144
1145void
1146ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1147 const char *index_value,
1148 const char *value,
1149 const ConstString &instance_name,
1150 const SettingEntry &entry,
1151 lldb::VarSetOperationType op,
1152 Error &err,
1153 bool pending)
1154{
1155 if (var_name == StepAvoidRegexpVarName())
1156 {
1157 std::string regexp_text;
1158 if (m_avoid_regexp_ap.get() != NULL)
1159 regexp_text.append (m_avoid_regexp_ap->GetText());
1160 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1161 if (regexp_text.empty())
1162 m_avoid_regexp_ap.reset();
1163 else
1164 {
1165 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1166
1167 }
1168 }
1169 else if (var_name == GetTraceThreadVarName())
1170 {
1171 bool success;
1172 bool result = Args::StringToBoolean(value, false, &success);
1173
1174 if (success)
1175 {
1176 m_trace_enabled = result;
1177 if (!pending)
1178 {
1179 Thread *myself = static_cast<Thread *> (this);
1180 myself->EnableTracer(m_trace_enabled, true);
1181 }
1182 }
1183 else
1184 {
1185 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1186 }
1187
1188 }
1189}
1190
1191void
1192ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1193 bool pending)
1194{
1195 if (new_settings.get() == NULL)
1196 return;
1197
1198 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1199 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1200 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1201 else
1202 m_avoid_regexp_ap.reset ();
1203}
1204
1205bool
1206ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1207 const ConstString &var_name,
1208 StringList &value,
1209 Error *err)
1210{
1211 if (var_name == StepAvoidRegexpVarName())
1212 {
1213 if (m_avoid_regexp_ap.get() != NULL)
1214 {
1215 std::string regexp_text("\"");
1216 regexp_text.append(m_avoid_regexp_ap->GetText());
1217 regexp_text.append ("\"");
1218 value.AppendString (regexp_text.c_str());
1219 }
1220
1221 }
1222 else if (var_name == GetTraceThreadVarName())
1223 {
1224 value.AppendString(m_trace_enabled ? "true" : "false");
1225 }
1226 else
1227 {
1228 if (err)
1229 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1230 return false;
1231 }
1232 return true;
1233}
1234
1235const ConstString
1236ThreadInstanceSettings::CreateInstanceName ()
1237{
1238 static int instance_count = 1;
1239 StreamString sstr;
1240
1241 sstr.Printf ("thread_%d", instance_count);
1242 ++instance_count;
1243
1244 const ConstString ret_val (sstr.GetData());
1245 return ret_val;
1246}
1247
1248const ConstString &
1249ThreadInstanceSettings::StepAvoidRegexpVarName ()
1250{
1251 static ConstString step_avoid_var_name ("step-avoid-regexp");
1252
1253 return step_avoid_var_name;
1254}
1255
1256const ConstString &
1257ThreadInstanceSettings::GetTraceThreadVarName ()
1258{
1259 static ConstString trace_thread_var_name ("trace-thread");
1260
1261 return trace_thread_var_name;
1262}
1263
1264//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001265// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001266//--------------------------------------------------
1267
1268SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001269Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001270{
1271 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1272 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1273};
1274
1275
1276SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001277Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001278{
1279 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1280 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1281 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1282 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1283};