blob: 48a11a272eda0d18535fa94e79a35cb8ae3de27a [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 *
664Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
665{
666 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
667 QueueThreadPlan (thread_plan_sp, abort_other_plans);
668 return thread_plan_sp.get();
669}
670
671ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000672Thread::QueueThreadPlanForStepRange
673(
674 bool abort_other_plans,
675 StepType type,
676 const AddressRange &range,
677 const SymbolContext &addr_context,
678 lldb::RunMode stop_other_threads,
679 bool avoid_code_without_debug_info
680)
Chris Lattner24943d22010-06-08 16:52:24 +0000681{
682 ThreadPlanSP thread_plan_sp;
683 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000684 {
685 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
686 if (avoid_code_without_debug_info)
687 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
688 else
689 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
690 thread_plan_sp.reset (plan);
691 }
Chris Lattner24943d22010-06-08 16:52:24 +0000692 else
693 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
694
695 QueueThreadPlan (thread_plan_sp, abort_other_plans);
696 return thread_plan_sp.get();
697}
698
699
700ThreadPlan *
701Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
702{
703 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
704 QueueThreadPlan (thread_plan_sp, abort_other_plans);
705 return thread_plan_sp.get();
706}
707
708ThreadPlan *
709Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
710 bool stop_other_threads, Vote stop_vote, Vote run_vote)
711{
712 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
713 QueueThreadPlan (thread_plan_sp, abort_other_plans);
714 return thread_plan_sp.get();
715}
716
717ThreadPlan *
718Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
719{
Jim Inghamb66cd072010-09-28 01:25:32 +0000720 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000721 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000722 // If that didn't come up with anything, try the ObjC runtime plugin:
723 if (thread_plan_sp.get() == NULL)
724 {
725 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
726 if (objc_runtime)
727 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
728 }
729
Chris Lattner24943d22010-06-08 16:52:24 +0000730 if (thread_plan_sp.get() == NULL)
731 {
732 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
733 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000734 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000735 }
736 QueueThreadPlan (thread_plan_sp, abort_other_plans);
737 return thread_plan_sp.get();
738}
739
740ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000741Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
742 Address& function,
743 lldb::addr_t arg,
744 bool stop_other_threads,
745 bool discard_on_error)
746{
747 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
748 QueueThreadPlan (thread_plan_sp, abort_other_plans);
749 return thread_plan_sp.get();
750}
751
752ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000753Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
754 Address &target_addr,
755 bool stop_other_threads)
756{
757 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
758 QueueThreadPlan (thread_plan_sp, abort_other_plans);
759 return thread_plan_sp.get();
760}
761
762ThreadPlan *
763Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
764 lldb::addr_t *address_list,
765 size_t num_addresses,
766 bool stop_other_threads)
767{
768 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
769 QueueThreadPlan (thread_plan_sp, abort_other_plans);
770 return thread_plan_sp.get();
771
772}
773
774uint32_t
775Thread::GetIndexID () const
776{
777 return m_index_id;
778}
779
780void
781Thread::DumpThreadPlans (lldb_private::Stream *s) const
782{
783 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000784 int i;
785 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
786 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000787 {
788 s->Printf ("Element %d: ", i);
789 s->IndentMore();
790 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
791 s->IndentLess();
792 s->EOL();
793 }
794
Chris Lattner24943d22010-06-08 16:52:24 +0000795 stack_size = m_completed_plan_stack.size();
796 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000797 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000798 {
799 s->Printf ("Element %d: ", i);
800 s->IndentMore();
801 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
802 s->IndentLess();
803 s->EOL();
804 }
805
806 stack_size = m_discarded_plan_stack.size();
807 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000808 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000809 {
810 s->Printf ("Element %d: ", i);
811 s->IndentMore();
812 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
813 s->IndentLess();
814 s->EOL();
815 }
816
817}
818
819Target *
820Thread::CalculateTarget ()
821{
822 return m_process.CalculateTarget();
823}
824
825Process *
826Thread::CalculateProcess ()
827{
828 return &m_process;
829}
830
831Thread *
832Thread::CalculateThread ()
833{
834 return this;
835}
836
837StackFrame *
838Thread::CalculateStackFrame ()
839{
840 return NULL;
841}
842
843void
Greg Claytona830adb2010-10-04 01:05:56 +0000844Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000845{
Greg Claytona830adb2010-10-04 01:05:56 +0000846 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000847 exe_ctx.thread = this;
848 exe_ctx.frame = NULL;
849}
850
Greg Clayton782b9cc2010-08-25 00:35:26 +0000851
852StackFrameList &
853Thread::GetStackFrameList ()
854{
Greg Claytonf40e3082010-08-26 02:28:22 +0000855 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000856 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000857 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000858}
859
860
861
Jim Ingham71219082010-08-12 02:14:28 +0000862uint32_t
863Thread::GetStackFrameCount()
864{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000865 return GetStackFrameList().GetNumFrames();
866}
Greg Clayton33ed1702010-08-24 00:45:41 +0000867
Greg Clayton33ed1702010-08-24 00:45:41 +0000868
Greg Clayton782b9cc2010-08-25 00:35:26 +0000869void
870Thread::ClearStackFrames ()
871{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000872 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
873 m_prev_frames_sp.reset (m_curr_frames_ap.release());
874 else
875 m_curr_frames_ap.release();
876
877// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
878// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000879}
880
881lldb::StackFrameSP
882Thread::GetStackFrameAtIndex (uint32_t idx)
883{
Greg Clayton72b71582010-09-02 21:44:10 +0000884 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000885}
886
Greg Claytonc12b6b42010-10-10 22:28:11 +0000887uint32_t
888Thread::GetSelectedFrameIndex ()
889{
890 return GetStackFrameList().GetSelectedFrameIndex();
891}
892
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000893lldb::StackFrameSP
894Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
895{
896 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
897}
898
899
Greg Claytonc12b6b42010-10-10 22:28:11 +0000900
Chris Lattner24943d22010-06-08 16:52:24 +0000901lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000902Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000903{
Jim Inghamc8332952010-08-26 21:32:51 +0000904 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000905}
906
907uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000908Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000909{
Jim Inghamc8332952010-08-26 21:32:51 +0000910 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000911}
912
913void
Jim Inghamc8332952010-08-26 21:32:51 +0000914Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000915{
Jim Inghamc8332952010-08-26 21:32:51 +0000916 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000917}
918
919void
Greg Claytona830adb2010-10-04 01:05:56 +0000920Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000921{
Greg Claytona830adb2010-10-04 01:05:56 +0000922 ExecutionContext exe_ctx;
923 SymbolContext frame_sc;
924 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000925
Greg Claytona830adb2010-10-04 01:05:56 +0000926 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000927 {
Greg Claytona830adb2010-10-04 01:05:56 +0000928 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000929 if (frame_sp)
930 {
Greg Claytona830adb2010-10-04 01:05:56 +0000931 exe_ctx.frame = frame_sp.get();
932 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000933 }
934 }
935
Greg Claytona830adb2010-10-04 01:05:56 +0000936 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
937 assert (thread_format);
938 const char *end = NULL;
939 Debugger::FormatPrompt (thread_format,
940 exe_ctx.frame ? &frame_sc : NULL,
941 &exe_ctx,
942 NULL,
943 strm,
944 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000945}
946
947lldb::ThreadSP
948Thread::GetSP ()
949{
950 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
951}
Jim Ingham20594b12010-09-08 03:14:33 +0000952
Greg Clayton990de7b2010-11-18 23:32:35 +0000953
954void
955Thread::Initialize ()
Jim Ingham20594b12010-09-08 03:14:33 +0000956{
Greg Clayton990de7b2010-11-18 23:32:35 +0000957 UserSettingsControllerSP &usc = GetSettingsController();
958 usc.reset (new SettingsController);
959 UserSettingsController::InitializeSettingsController (usc,
960 SettingsController::global_settings_table,
961 SettingsController::instance_settings_table);
962}
Jim Ingham20594b12010-09-08 03:14:33 +0000963
Greg Clayton990de7b2010-11-18 23:32:35 +0000964void
965Thread::Terminate ()
966{
967 UserSettingsControllerSP &usc = GetSettingsController();
968 UserSettingsController::FinalizeSettingsController (usc);
969 usc.reset();
970}
Jim Ingham20594b12010-09-08 03:14:33 +0000971
Greg Clayton990de7b2010-11-18 23:32:35 +0000972UserSettingsControllerSP &
973Thread::GetSettingsController ()
974{
975 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +0000976 return g_settings_controller;
977}
978
Caroline Tice1ebef442010-09-27 00:30:10 +0000979void
980Thread::UpdateInstanceName ()
981{
982 StreamString sstr;
983 const char *name = GetName();
984
985 if (name && name[0] != '\0')
986 sstr.Printf ("%s", name);
987 else if ((GetIndexID() != 0) || (GetID() != 0))
988 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
989
990 if (sstr.GetSize() > 0)
991 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
992}
993
Jim Inghamccd584d2010-09-23 17:40:12 +0000994lldb::StackFrameSP
995Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
996{
997 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
998}
Caroline Tice7826c882010-10-26 03:11:13 +0000999
1000const char *
1001Thread::StopReasonAsCString (lldb::StopReason reason)
1002{
1003 switch (reason)
1004 {
1005 case eStopReasonInvalid: return "invalid";
1006 case eStopReasonNone: return "none";
1007 case eStopReasonTrace: return "trace";
1008 case eStopReasonBreakpoint: return "breakpoint";
1009 case eStopReasonWatchpoint: return "watchpoint";
1010 case eStopReasonSignal: return "signal";
1011 case eStopReasonException: return "exception";
1012 case eStopReasonPlanComplete: return "plan complete";
1013 }
1014
1015
1016 static char unknown_state_string[64];
1017 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1018 return unknown_state_string;
1019}
1020
1021const char *
1022Thread::RunModeAsCString (lldb::RunMode mode)
1023{
1024 switch (mode)
1025 {
1026 case eOnlyThisThread: return "only this thread";
1027 case eAllThreads: return "all threads";
1028 case eOnlyDuringStepping: return "only during stepping";
1029 }
1030
1031 static char unknown_state_string[64];
1032 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1033 return unknown_state_string;
1034}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001035
Greg Clayton990de7b2010-11-18 23:32:35 +00001036#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001037//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001038// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001039//--------------------------------------------------------------
1040
Greg Clayton990de7b2010-11-18 23:32:35 +00001041Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001042 UserSettingsController ("thread", Process::GetSettingsController())
1043{
1044 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1045 InstanceSettings::GetDefaultName().AsCString()));
1046}
1047
Greg Clayton990de7b2010-11-18 23:32:35 +00001048Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001049{
1050}
1051
1052lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001053Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001054{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001055 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1056 false,
1057 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001058 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1059 return new_settings_sp;
1060}
1061
1062#pragma mark "ThreadInstanceSettings"
1063//--------------------------------------------------------------
1064// class ThreadInstanceSettings
1065//--------------------------------------------------------------
1066
1067ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001068 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001069 m_avoid_regexp_ap (),
1070 m_trace_enabled (false)
1071{
1072 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1073 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1074 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1075 // This is true for CreateInstanceName() too.
1076
1077 if (GetInstanceName() == InstanceSettings::InvalidName())
1078 {
1079 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1080 m_owner.RegisterInstanceSettings (this);
1081 }
1082
1083 if (live_instance)
1084 {
1085 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1086 CopyInstanceSettings (pending_settings,false);
1087 //m_owner.RemovePendingSettings (m_instance_name);
1088 }
1089}
1090
1091ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001092 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001093 m_avoid_regexp_ap (),
1094 m_trace_enabled (rhs.m_trace_enabled)
1095{
1096 if (m_instance_name != InstanceSettings::GetDefaultName())
1097 {
1098 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1099 CopyInstanceSettings (pending_settings,false);
1100 m_owner.RemovePendingSettings (m_instance_name);
1101 }
1102 if (rhs.m_avoid_regexp_ap.get() != NULL)
1103 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1104}
1105
1106ThreadInstanceSettings::~ThreadInstanceSettings ()
1107{
1108}
1109
1110ThreadInstanceSettings&
1111ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1112{
1113 if (this != &rhs)
1114 {
1115 if (rhs.m_avoid_regexp_ap.get() != NULL)
1116 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1117 else
1118 m_avoid_regexp_ap.reset(NULL);
1119 }
1120 m_trace_enabled = rhs.m_trace_enabled;
1121 return *this;
1122}
1123
1124
1125void
1126ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1127 const char *index_value,
1128 const char *value,
1129 const ConstString &instance_name,
1130 const SettingEntry &entry,
1131 lldb::VarSetOperationType op,
1132 Error &err,
1133 bool pending)
1134{
1135 if (var_name == StepAvoidRegexpVarName())
1136 {
1137 std::string regexp_text;
1138 if (m_avoid_regexp_ap.get() != NULL)
1139 regexp_text.append (m_avoid_regexp_ap->GetText());
1140 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1141 if (regexp_text.empty())
1142 m_avoid_regexp_ap.reset();
1143 else
1144 {
1145 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1146
1147 }
1148 }
1149 else if (var_name == GetTraceThreadVarName())
1150 {
1151 bool success;
1152 bool result = Args::StringToBoolean(value, false, &success);
1153
1154 if (success)
1155 {
1156 m_trace_enabled = result;
1157 if (!pending)
1158 {
1159 Thread *myself = static_cast<Thread *> (this);
1160 myself->EnableTracer(m_trace_enabled, true);
1161 }
1162 }
1163 else
1164 {
1165 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1166 }
1167
1168 }
1169}
1170
1171void
1172ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1173 bool pending)
1174{
1175 if (new_settings.get() == NULL)
1176 return;
1177
1178 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1179 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1180 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1181 else
1182 m_avoid_regexp_ap.reset ();
1183}
1184
1185bool
1186ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1187 const ConstString &var_name,
1188 StringList &value,
1189 Error *err)
1190{
1191 if (var_name == StepAvoidRegexpVarName())
1192 {
1193 if (m_avoid_regexp_ap.get() != NULL)
1194 {
1195 std::string regexp_text("\"");
1196 regexp_text.append(m_avoid_regexp_ap->GetText());
1197 regexp_text.append ("\"");
1198 value.AppendString (regexp_text.c_str());
1199 }
1200
1201 }
1202 else if (var_name == GetTraceThreadVarName())
1203 {
1204 value.AppendString(m_trace_enabled ? "true" : "false");
1205 }
1206 else
1207 {
1208 if (err)
1209 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1210 return false;
1211 }
1212 return true;
1213}
1214
1215const ConstString
1216ThreadInstanceSettings::CreateInstanceName ()
1217{
1218 static int instance_count = 1;
1219 StreamString sstr;
1220
1221 sstr.Printf ("thread_%d", instance_count);
1222 ++instance_count;
1223
1224 const ConstString ret_val (sstr.GetData());
1225 return ret_val;
1226}
1227
1228const ConstString &
1229ThreadInstanceSettings::StepAvoidRegexpVarName ()
1230{
1231 static ConstString step_avoid_var_name ("step-avoid-regexp");
1232
1233 return step_avoid_var_name;
1234}
1235
1236const ConstString &
1237ThreadInstanceSettings::GetTraceThreadVarName ()
1238{
1239 static ConstString trace_thread_var_name ("trace-thread");
1240
1241 return trace_thread_var_name;
1242}
1243
1244//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001245// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001246//--------------------------------------------------
1247
1248SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001249Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001250{
1251 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1252 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1253};
1254
1255
1256SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001257Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001258{
1259 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1260 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1261 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1262 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1263};