blob: 2dca598a52aad75559f4a626c7aec82580ba728c [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);
Jim Ingham11a837d2011-01-25 02:47:23 +0000136 if (saved_state.stop_info_sp)
137 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000138 SetStopInfo(saved_state.stop_info_sp);
139 return true;
140}
141
Chris Lattner24943d22010-06-08 16:52:24 +0000142StateType
143Thread::GetState() const
144{
145 // If any other threads access this we will need a mutex for it
146 Mutex::Locker locker(m_state_mutex);
147 return m_state;
148}
149
150void
151Thread::SetState(StateType state)
152{
153 Mutex::Locker locker(m_state_mutex);
154 m_state = state;
155}
156
157void
158Thread::WillStop()
159{
160 ThreadPlan *current_plan = GetCurrentPlan();
161
162 // FIXME: I may decide to disallow threads with no plans. In which
163 // case this should go to an assert.
164
165 if (!current_plan)
166 return;
167
168 current_plan->WillStop();
169}
170
171void
172Thread::SetupForResume ()
173{
174 if (GetResumeState() != eStateSuspended)
175 {
176
177 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
178 // telling the current plan it will resume, since we might change what the current
179 // plan is.
180
181 lldb::addr_t pc = GetRegisterContext()->GetPC();
182 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
183 if (bp_site_sp && bp_site_sp->IsEnabled())
184 {
185 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
186 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000187
188 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000189
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000190 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
191 {
192 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
193 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000194 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000195 ThreadPlanSP step_bp_plan_sp;
196 step_bp_plan->SetPrivate (true);
197
Chris Lattner24943d22010-06-08 16:52:24 +0000198 if (GetCurrentPlan()->RunState() != eStateStepping)
199 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000200 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000201 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000202 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000203 QueueThreadPlan (step_bp_plan_sp, false);
204 }
205 }
206 }
207 }
208}
209
210bool
211Thread::WillResume (StateType resume_state)
212{
213 // At this point clear the completed plan stack.
214 m_completed_plan_stack.clear();
215 m_discarded_plan_stack.clear();
216
Greg Clayton643ee732010-08-04 01:40:35 +0000217 StopInfo *stop_info = GetPrivateStopReason().get();
218 if (stop_info)
219 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000220
221 // Tell all the plans that we are about to resume in case they need to clear any state.
222 // We distinguish between the plan on the top of the stack and the lower
223 // plans in case a plan needs to do any special business before it runs.
224
225 ThreadPlan *plan_ptr = GetCurrentPlan();
226 plan_ptr->WillResume(resume_state, true);
227
228 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
229 {
230 plan_ptr->WillResume (resume_state, false);
231 }
Greg Clayton643ee732010-08-04 01:40:35 +0000232
Greg Clayton643ee732010-08-04 01:40:35 +0000233 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000234 return true;
235}
236
237void
238Thread::DidResume ()
239{
240 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
241}
242
243bool
244Thread::ShouldStop (Event* event_ptr)
245{
246 ThreadPlan *current_plan = GetCurrentPlan();
247 bool should_stop = true;
248
Greg Claytone005f2c2010-11-06 01:53:30 +0000249 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000250 if (log)
251 {
252 StreamString s;
253 DumpThreadPlans(&s);
254 log->PutCString (s.GetData());
255 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000256
257 // The top most plan always gets to do the trace log...
258 current_plan->DoTraceLog ();
Chris Lattner24943d22010-06-08 16:52:24 +0000259
260 if (current_plan->PlanExplainsStop())
261 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000262 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000263 while (1)
264 {
265 should_stop = current_plan->ShouldStop(event_ptr);
266 if (current_plan->MischiefManaged())
267 {
268 if (should_stop)
269 current_plan->WillStop();
270
271 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
272 // Otherwise, see if the plan's parent wants to stop.
273
274 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
275 {
276 PopPlan();
277 break;
278 }
279 else
280 {
281
282 PopPlan();
283
284 current_plan = GetCurrentPlan();
285 if (current_plan == NULL)
286 {
287 break;
288 }
289 }
290
291 }
292 else
293 {
294 break;
295 }
296 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000297 if (over_ride_stop)
298 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000299 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000300 else if (current_plan->TracerExplainsStop())
301 {
302 return false;
303 }
Chris Lattner24943d22010-06-08 16:52:24 +0000304 else
305 {
306 // If the current plan doesn't explain the stop, then, find one that
307 // does and let it handle the situation.
308 ThreadPlan *plan_ptr = current_plan;
309 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
310 {
311 if (plan_ptr->PlanExplainsStop())
312 {
313 should_stop = plan_ptr->ShouldStop (event_ptr);
314 break;
315 }
316
317 }
318 }
319
320 return should_stop;
321}
322
323Vote
324Thread::ShouldReportStop (Event* event_ptr)
325{
326 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000327 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000328
329 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
330 {
331 if (log)
332 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 +0000333 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000334 }
Chris Lattner24943d22010-06-08 16:52:24 +0000335
336 if (m_completed_plan_stack.size() > 0)
337 {
338 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000339 if (log)
340 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 +0000341 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
342 }
343 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000344 {
345 if (log)
346 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000347 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000348 }
Chris Lattner24943d22010-06-08 16:52:24 +0000349}
350
351Vote
352Thread::ShouldReportRun (Event* event_ptr)
353{
354 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000355
Chris Lattner24943d22010-06-08 16:52:24 +0000356 if (thread_state == eStateSuspended
357 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000358 {
Chris Lattner24943d22010-06-08 16:52:24 +0000359 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000360 }
361
362 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000363 if (m_completed_plan_stack.size() > 0)
364 {
365 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000366 if (log)
367 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
368 GetIndexID(),
369 GetID(),
370 m_completed_plan_stack.back()->GetName());
371
Chris Lattner24943d22010-06-08 16:52:24 +0000372 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
373 }
374 else
Jim Inghamac959662011-01-24 06:34:17 +0000375 {
376 if (log)
377 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
378 GetIndexID(),
379 GetID(),
380 GetCurrentPlan()->GetName());
381
Chris Lattner24943d22010-06-08 16:52:24 +0000382 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000383 }
Chris Lattner24943d22010-06-08 16:52:24 +0000384}
385
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000386bool
387Thread::MatchesSpec (const ThreadSpec *spec)
388{
389 if (spec == NULL)
390 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000391
Jim Ingham649492b2010-06-18 01:00:58 +0000392 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000393}
394
Chris Lattner24943d22010-06-08 16:52:24 +0000395void
396Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
397{
398 if (thread_plan_sp)
399 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000400 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
401 if (!thread_plan_sp->GetThreadPlanTracer())
402 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000403 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000404
Chris Lattner24943d22010-06-08 16:52:24 +0000405 thread_plan_sp->DidPush();
406
Greg Claytone005f2c2010-11-06 01:53:30 +0000407 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000408 if (log)
409 {
410 StreamString s;
411 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000412 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000413 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000414 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000415 }
416 }
417}
418
419void
420Thread::PopPlan ()
421{
Greg Claytone005f2c2010-11-06 01:53:30 +0000422 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000423
Jim Ingham6297a3a2010-10-20 00:39:53 +0000424 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000425 return;
426 else
427 {
428 ThreadPlanSP &plan = m_plan_stack.back();
429 if (log)
430 {
Jim Ingham83e8b9d2010-11-10 18:17:03 +0000431 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000432 }
433 m_completed_plan_stack.push_back (plan);
434 plan->WillPop();
435 m_plan_stack.pop_back();
436 }
437}
438
439void
440Thread::DiscardPlan ()
441{
442 if (m_plan_stack.size() > 1)
443 {
444 ThreadPlanSP &plan = m_plan_stack.back();
445 m_discarded_plan_stack.push_back (plan);
446 plan->WillPop();
447 m_plan_stack.pop_back();
448 }
449}
450
451ThreadPlan *
452Thread::GetCurrentPlan ()
453{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000454 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000455 return NULL;
456 else
457 return m_plan_stack.back().get();
458}
459
460ThreadPlanSP
461Thread::GetCompletedPlan ()
462{
463 ThreadPlanSP empty_plan_sp;
464 if (!m_completed_plan_stack.empty())
465 {
466 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
467 {
468 ThreadPlanSP completed_plan_sp;
469 completed_plan_sp = m_completed_plan_stack[i];
470 if (!completed_plan_sp->GetPrivate ())
471 return completed_plan_sp;
472 }
473 }
474 return empty_plan_sp;
475}
476
477bool
478Thread::IsThreadPlanDone (ThreadPlan *plan)
479{
480 ThreadPlanSP empty_plan_sp;
481 if (!m_completed_plan_stack.empty())
482 {
483 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
484 {
485 if (m_completed_plan_stack[i].get() == plan)
486 return true;
487 }
488 }
489 return false;
490}
491
492bool
493Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
494{
495 ThreadPlanSP empty_plan_sp;
496 if (!m_discarded_plan_stack.empty())
497 {
498 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
499 {
500 if (m_discarded_plan_stack[i].get() == plan)
501 return true;
502 }
503 }
504 return false;
505}
506
507ThreadPlan *
508Thread::GetPreviousPlan (ThreadPlan *current_plan)
509{
510 if (current_plan == NULL)
511 return NULL;
512
513 int stack_size = m_completed_plan_stack.size();
514 for (int i = stack_size - 1; i > 0; i--)
515 {
516 if (current_plan == m_completed_plan_stack[i].get())
517 return m_completed_plan_stack[i-1].get();
518 }
519
520 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
521 {
Chris Lattner24943d22010-06-08 16:52:24 +0000522 if (m_plan_stack.size() > 0)
523 return m_plan_stack.back().get();
524 else
525 return NULL;
526 }
527
528 stack_size = m_plan_stack.size();
529 for (int i = stack_size - 1; i > 0; i--)
530 {
531 if (current_plan == m_plan_stack[i].get())
532 return m_plan_stack[i-1].get();
533 }
534 return NULL;
535}
536
537void
538Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
539{
540 if (abort_other_plans)
541 DiscardThreadPlans(true);
542
543 PushPlan (thread_plan_sp);
544}
545
Jim Ingham745ac7a2010-11-11 19:26:09 +0000546
547void
548Thread::EnableTracer (bool value, bool single_stepping)
549{
550 int stack_size = m_plan_stack.size();
551 for (int i = 0; i < stack_size; i++)
552 {
553 if (m_plan_stack[i]->GetThreadPlanTracer())
554 {
555 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
556 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
557 }
558 }
559}
560
561void
562Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
563{
564 int stack_size = m_plan_stack.size();
565 for (int i = 0; i < stack_size; i++)
566 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
567}
568
Chris Lattner24943d22010-06-08 16:52:24 +0000569void
Jim Inghamea9d4262010-11-05 19:25:48 +0000570Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
571{
Greg Claytone005f2c2010-11-06 01:53:30 +0000572 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000573 if (log)
574 {
575 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get());
576 }
577
578 int stack_size = m_plan_stack.size();
579
580 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
581 // stack, and if so discard up to and including it.
582
583 if (up_to_plan_sp.get() == NULL)
584 {
585 for (int i = stack_size - 1; i > 0; i--)
586 DiscardPlan();
587 }
588 else
589 {
590 bool found_it = false;
591 for (int i = stack_size - 1; i > 0; i--)
592 {
593 if (m_plan_stack[i] == up_to_plan_sp)
594 found_it = true;
595 }
596 if (found_it)
597 {
598 bool last_one = false;
599 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
600 {
601 if (GetCurrentPlan() == up_to_plan_sp.get())
602 last_one = true;
603 DiscardPlan();
604 }
605 }
606 }
607 return;
608}
609
610void
Chris Lattner24943d22010-06-08 16:52:24 +0000611Thread::DiscardThreadPlans(bool force)
612{
Greg Claytone005f2c2010-11-06 01:53:30 +0000613 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000614 if (log)
615 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000616 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000617 }
618
619 if (force)
620 {
621 int stack_size = m_plan_stack.size();
622 for (int i = stack_size - 1; i > 0; i--)
623 {
624 DiscardPlan();
625 }
626 return;
627 }
628
629 while (1)
630 {
631
632 int master_plan_idx;
633 bool discard;
634
635 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
636 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
637 {
638 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
639 {
640 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
641 break;
642 }
643 }
644
645 if (discard)
646 {
647 // First pop all the dependent plans:
648 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
649 {
650
651 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
652 // for the plan leaves it in a state that it is safe to pop the plan
653 // with no more notice?
654 DiscardPlan();
655 }
656
657 // Now discard the master plan itself.
658 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
659 // discard it's dependent plans, but not it...
660 if (master_plan_idx > 0)
661 {
662 DiscardPlan();
663 }
664 }
665 else
666 {
667 // If the master plan doesn't want to get discarded, then we're done.
668 break;
669 }
670
671 }
Chris Lattner24943d22010-06-08 16:52:24 +0000672}
673
674ThreadPlan *
675Thread::QueueFundamentalPlan (bool abort_other_plans)
676{
677 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
678 QueueThreadPlan (thread_plan_sp, abort_other_plans);
679 return thread_plan_sp.get();
680}
681
682ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000683Thread::QueueThreadPlanForStepSingleInstruction
684(
685 bool step_over,
686 bool abort_other_plans,
687 bool stop_other_threads
688)
Chris Lattner24943d22010-06-08 16:52:24 +0000689{
690 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
691 QueueThreadPlan (thread_plan_sp, abort_other_plans);
692 return thread_plan_sp.get();
693}
694
695ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000696Thread::QueueThreadPlanForStepRange
697(
698 bool abort_other_plans,
699 StepType type,
700 const AddressRange &range,
701 const SymbolContext &addr_context,
702 lldb::RunMode stop_other_threads,
703 bool avoid_code_without_debug_info
704)
Chris Lattner24943d22010-06-08 16:52:24 +0000705{
706 ThreadPlanSP thread_plan_sp;
707 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000708 {
709 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
710 if (avoid_code_without_debug_info)
711 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
712 else
713 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
714 thread_plan_sp.reset (plan);
715 }
Chris Lattner24943d22010-06-08 16:52:24 +0000716 else
717 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
718
719 QueueThreadPlan (thread_plan_sp, abort_other_plans);
720 return thread_plan_sp.get();
721}
722
723
724ThreadPlan *
725Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
726{
727 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
728 QueueThreadPlan (thread_plan_sp, abort_other_plans);
729 return thread_plan_sp.get();
730}
731
732ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000733Thread::QueueThreadPlanForStepOut
734(
735 bool abort_other_plans,
736 SymbolContext *addr_context,
737 bool first_insn,
738 bool stop_other_threads,
739 Vote stop_vote,
740 Vote run_vote,
741 uint32_t frame_idx
742)
Chris Lattner24943d22010-06-08 16:52:24 +0000743{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000744 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
745 addr_context,
746 first_insn,
747 stop_other_threads,
748 stop_vote,
749 run_vote,
750 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000751 QueueThreadPlan (thread_plan_sp, abort_other_plans);
752 return thread_plan_sp.get();
753}
754
755ThreadPlan *
756Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
757{
Jim Inghamb66cd072010-09-28 01:25:32 +0000758 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000759 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000760 // If that didn't come up with anything, try the ObjC runtime plugin:
761 if (thread_plan_sp.get() == NULL)
762 {
763 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
764 if (objc_runtime)
765 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
766 }
767
Chris Lattner24943d22010-06-08 16:52:24 +0000768 if (thread_plan_sp.get() == NULL)
769 {
770 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
771 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000772 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000773 }
774 QueueThreadPlan (thread_plan_sp, abort_other_plans);
775 return thread_plan_sp.get();
776}
777
778ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000779Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
780 Address& function,
781 lldb::addr_t arg,
782 bool stop_other_threads,
783 bool discard_on_error)
784{
785 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
786 QueueThreadPlan (thread_plan_sp, abort_other_plans);
787 return thread_plan_sp.get();
788}
789
790ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000791Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
792 Address &target_addr,
793 bool stop_other_threads)
794{
795 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
796 QueueThreadPlan (thread_plan_sp, abort_other_plans);
797 return thread_plan_sp.get();
798}
799
800ThreadPlan *
801Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000802 lldb::addr_t *address_list,
803 size_t num_addresses,
804 bool stop_other_threads,
805 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000806{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000807 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000808 QueueThreadPlan (thread_plan_sp, abort_other_plans);
809 return thread_plan_sp.get();
810
811}
812
813uint32_t
814Thread::GetIndexID () const
815{
816 return m_index_id;
817}
818
819void
820Thread::DumpThreadPlans (lldb_private::Stream *s) const
821{
822 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000823 int i;
824 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
825 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000826 {
827 s->Printf ("Element %d: ", i);
828 s->IndentMore();
829 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
830 s->IndentLess();
831 s->EOL();
832 }
833
Chris Lattner24943d22010-06-08 16:52:24 +0000834 stack_size = m_completed_plan_stack.size();
835 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000836 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000837 {
838 s->Printf ("Element %d: ", i);
839 s->IndentMore();
840 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
841 s->IndentLess();
842 s->EOL();
843 }
844
845 stack_size = m_discarded_plan_stack.size();
846 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000847 for (int i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000848 {
849 s->Printf ("Element %d: ", i);
850 s->IndentMore();
851 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
852 s->IndentLess();
853 s->EOL();
854 }
855
856}
857
858Target *
859Thread::CalculateTarget ()
860{
861 return m_process.CalculateTarget();
862}
863
864Process *
865Thread::CalculateProcess ()
866{
867 return &m_process;
868}
869
870Thread *
871Thread::CalculateThread ()
872{
873 return this;
874}
875
876StackFrame *
877Thread::CalculateStackFrame ()
878{
879 return NULL;
880}
881
882void
Greg Claytona830adb2010-10-04 01:05:56 +0000883Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000884{
Greg Claytona830adb2010-10-04 01:05:56 +0000885 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000886 exe_ctx.thread = this;
887 exe_ctx.frame = NULL;
888}
889
Greg Clayton782b9cc2010-08-25 00:35:26 +0000890
891StackFrameList &
892Thread::GetStackFrameList ()
893{
Greg Claytonf40e3082010-08-26 02:28:22 +0000894 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000895 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000896 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000897}
898
899
900
Jim Ingham71219082010-08-12 02:14:28 +0000901uint32_t
902Thread::GetStackFrameCount()
903{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000904 return GetStackFrameList().GetNumFrames();
905}
Greg Clayton33ed1702010-08-24 00:45:41 +0000906
Greg Clayton33ed1702010-08-24 00:45:41 +0000907
Greg Clayton782b9cc2010-08-25 00:35:26 +0000908void
909Thread::ClearStackFrames ()
910{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000911 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
912 m_prev_frames_sp.reset (m_curr_frames_ap.release());
913 else
914 m_curr_frames_ap.release();
915
916// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
917// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000918}
919
920lldb::StackFrameSP
921Thread::GetStackFrameAtIndex (uint32_t idx)
922{
Greg Clayton72b71582010-09-02 21:44:10 +0000923 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000924}
925
Greg Claytonc12b6b42010-10-10 22:28:11 +0000926uint32_t
927Thread::GetSelectedFrameIndex ()
928{
929 return GetStackFrameList().GetSelectedFrameIndex();
930}
931
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000932lldb::StackFrameSP
933Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
934{
935 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
936}
937
938
Greg Claytonc12b6b42010-10-10 22:28:11 +0000939
Chris Lattner24943d22010-06-08 16:52:24 +0000940lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000941Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000942{
Jim Inghamc8332952010-08-26 21:32:51 +0000943 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000944}
945
946uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000947Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000948{
Jim Inghamc8332952010-08-26 21:32:51 +0000949 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000950}
951
952void
Jim Inghamc8332952010-08-26 21:32:51 +0000953Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000954{
Jim Inghamc8332952010-08-26 21:32:51 +0000955 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000956}
957
958void
Greg Claytona830adb2010-10-04 01:05:56 +0000959Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000960{
Greg Claytona830adb2010-10-04 01:05:56 +0000961 ExecutionContext exe_ctx;
962 SymbolContext frame_sc;
963 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000964
Greg Claytona830adb2010-10-04 01:05:56 +0000965 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000966 {
Greg Claytona830adb2010-10-04 01:05:56 +0000967 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000968 if (frame_sp)
969 {
Greg Claytona830adb2010-10-04 01:05:56 +0000970 exe_ctx.frame = frame_sp.get();
971 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000972 }
973 }
974
Greg Claytona830adb2010-10-04 01:05:56 +0000975 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
976 assert (thread_format);
977 const char *end = NULL;
978 Debugger::FormatPrompt (thread_format,
979 exe_ctx.frame ? &frame_sc : NULL,
980 &exe_ctx,
981 NULL,
982 strm,
983 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000984}
985
986lldb::ThreadSP
987Thread::GetSP ()
988{
989 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
990}
Jim Ingham20594b12010-09-08 03:14:33 +0000991
Greg Clayton990de7b2010-11-18 23:32:35 +0000992
993void
994Thread::Initialize ()
Jim Ingham20594b12010-09-08 03:14:33 +0000995{
Greg Clayton990de7b2010-11-18 23:32:35 +0000996 UserSettingsControllerSP &usc = GetSettingsController();
997 usc.reset (new SettingsController);
998 UserSettingsController::InitializeSettingsController (usc,
999 SettingsController::global_settings_table,
1000 SettingsController::instance_settings_table);
1001}
Jim Ingham20594b12010-09-08 03:14:33 +00001002
Greg Clayton990de7b2010-11-18 23:32:35 +00001003void
1004Thread::Terminate ()
1005{
1006 UserSettingsControllerSP &usc = GetSettingsController();
1007 UserSettingsController::FinalizeSettingsController (usc);
1008 usc.reset();
1009}
Jim Ingham20594b12010-09-08 03:14:33 +00001010
Greg Clayton990de7b2010-11-18 23:32:35 +00001011UserSettingsControllerSP &
1012Thread::GetSettingsController ()
1013{
1014 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001015 return g_settings_controller;
1016}
1017
Caroline Tice1ebef442010-09-27 00:30:10 +00001018void
1019Thread::UpdateInstanceName ()
1020{
1021 StreamString sstr;
1022 const char *name = GetName();
1023
1024 if (name && name[0] != '\0')
1025 sstr.Printf ("%s", name);
1026 else if ((GetIndexID() != 0) || (GetID() != 0))
1027 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1028
1029 if (sstr.GetSize() > 0)
1030 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1031}
1032
Jim Inghamccd584d2010-09-23 17:40:12 +00001033lldb::StackFrameSP
1034Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1035{
1036 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1037}
Caroline Tice7826c882010-10-26 03:11:13 +00001038
1039const char *
1040Thread::StopReasonAsCString (lldb::StopReason reason)
1041{
1042 switch (reason)
1043 {
1044 case eStopReasonInvalid: return "invalid";
1045 case eStopReasonNone: return "none";
1046 case eStopReasonTrace: return "trace";
1047 case eStopReasonBreakpoint: return "breakpoint";
1048 case eStopReasonWatchpoint: return "watchpoint";
1049 case eStopReasonSignal: return "signal";
1050 case eStopReasonException: return "exception";
1051 case eStopReasonPlanComplete: return "plan complete";
1052 }
1053
1054
1055 static char unknown_state_string[64];
1056 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1057 return unknown_state_string;
1058}
1059
1060const char *
1061Thread::RunModeAsCString (lldb::RunMode mode)
1062{
1063 switch (mode)
1064 {
1065 case eOnlyThisThread: return "only this thread";
1066 case eAllThreads: return "all threads";
1067 case eOnlyDuringStepping: return "only during stepping";
1068 }
1069
1070 static char unknown_state_string[64];
1071 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1072 return unknown_state_string;
1073}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001074
Greg Clayton990de7b2010-11-18 23:32:35 +00001075#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001076//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001077// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001078//--------------------------------------------------------------
1079
Greg Clayton990de7b2010-11-18 23:32:35 +00001080Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001081 UserSettingsController ("thread", Process::GetSettingsController())
1082{
1083 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1084 InstanceSettings::GetDefaultName().AsCString()));
1085}
1086
Greg Clayton990de7b2010-11-18 23:32:35 +00001087Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001088{
1089}
1090
1091lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001092Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001093{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001094 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1095 false,
1096 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001097 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1098 return new_settings_sp;
1099}
1100
1101#pragma mark "ThreadInstanceSettings"
1102//--------------------------------------------------------------
1103// class ThreadInstanceSettings
1104//--------------------------------------------------------------
1105
1106ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001107 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001108 m_avoid_regexp_ap (),
1109 m_trace_enabled (false)
1110{
1111 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1112 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1113 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1114 // This is true for CreateInstanceName() too.
1115
1116 if (GetInstanceName() == InstanceSettings::InvalidName())
1117 {
1118 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1119 m_owner.RegisterInstanceSettings (this);
1120 }
1121
1122 if (live_instance)
1123 {
1124 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1125 CopyInstanceSettings (pending_settings,false);
1126 //m_owner.RemovePendingSettings (m_instance_name);
1127 }
1128}
1129
1130ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001131 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001132 m_avoid_regexp_ap (),
1133 m_trace_enabled (rhs.m_trace_enabled)
1134{
1135 if (m_instance_name != InstanceSettings::GetDefaultName())
1136 {
1137 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1138 CopyInstanceSettings (pending_settings,false);
1139 m_owner.RemovePendingSettings (m_instance_name);
1140 }
1141 if (rhs.m_avoid_regexp_ap.get() != NULL)
1142 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1143}
1144
1145ThreadInstanceSettings::~ThreadInstanceSettings ()
1146{
1147}
1148
1149ThreadInstanceSettings&
1150ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1151{
1152 if (this != &rhs)
1153 {
1154 if (rhs.m_avoid_regexp_ap.get() != NULL)
1155 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1156 else
1157 m_avoid_regexp_ap.reset(NULL);
1158 }
1159 m_trace_enabled = rhs.m_trace_enabled;
1160 return *this;
1161}
1162
1163
1164void
1165ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1166 const char *index_value,
1167 const char *value,
1168 const ConstString &instance_name,
1169 const SettingEntry &entry,
1170 lldb::VarSetOperationType op,
1171 Error &err,
1172 bool pending)
1173{
1174 if (var_name == StepAvoidRegexpVarName())
1175 {
1176 std::string regexp_text;
1177 if (m_avoid_regexp_ap.get() != NULL)
1178 regexp_text.append (m_avoid_regexp_ap->GetText());
1179 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1180 if (regexp_text.empty())
1181 m_avoid_regexp_ap.reset();
1182 else
1183 {
1184 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1185
1186 }
1187 }
1188 else if (var_name == GetTraceThreadVarName())
1189 {
1190 bool success;
1191 bool result = Args::StringToBoolean(value, false, &success);
1192
1193 if (success)
1194 {
1195 m_trace_enabled = result;
1196 if (!pending)
1197 {
1198 Thread *myself = static_cast<Thread *> (this);
1199 myself->EnableTracer(m_trace_enabled, true);
1200 }
1201 }
1202 else
1203 {
1204 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1205 }
1206
1207 }
1208}
1209
1210void
1211ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1212 bool pending)
1213{
1214 if (new_settings.get() == NULL)
1215 return;
1216
1217 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1218 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1219 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1220 else
1221 m_avoid_regexp_ap.reset ();
1222}
1223
1224bool
1225ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1226 const ConstString &var_name,
1227 StringList &value,
1228 Error *err)
1229{
1230 if (var_name == StepAvoidRegexpVarName())
1231 {
1232 if (m_avoid_regexp_ap.get() != NULL)
1233 {
1234 std::string regexp_text("\"");
1235 regexp_text.append(m_avoid_regexp_ap->GetText());
1236 regexp_text.append ("\"");
1237 value.AppendString (regexp_text.c_str());
1238 }
1239
1240 }
1241 else if (var_name == GetTraceThreadVarName())
1242 {
1243 value.AppendString(m_trace_enabled ? "true" : "false");
1244 }
1245 else
1246 {
1247 if (err)
1248 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1249 return false;
1250 }
1251 return true;
1252}
1253
1254const ConstString
1255ThreadInstanceSettings::CreateInstanceName ()
1256{
1257 static int instance_count = 1;
1258 StreamString sstr;
1259
1260 sstr.Printf ("thread_%d", instance_count);
1261 ++instance_count;
1262
1263 const ConstString ret_val (sstr.GetData());
1264 return ret_val;
1265}
1266
1267const ConstString &
1268ThreadInstanceSettings::StepAvoidRegexpVarName ()
1269{
1270 static ConstString step_avoid_var_name ("step-avoid-regexp");
1271
1272 return step_avoid_var_name;
1273}
1274
1275const ConstString &
1276ThreadInstanceSettings::GetTraceThreadVarName ()
1277{
1278 static ConstString trace_thread_var_name ("trace-thread");
1279
1280 return trace_thread_var_name;
1281}
1282
1283//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001284// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001285//--------------------------------------------------
1286
1287SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001288Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001289{
1290 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1291 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1292};
1293
1294
1295SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001296Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001297{
1298 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1299 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1300 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1301 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1302};