blob: 6c1dc40a7b7bc16730e00858b3090248eec3e44b [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/Target/DynamicLoader.h"
18#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000019#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Target/Process.h"
21#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000022#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000023#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Thread.h"
25#include "lldb/Target/ThreadPlan.h"
26#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Target/ThreadPlanBase.h"
28#include "lldb/Target/ThreadPlanStepInstruction.h"
29#include "lldb/Target/ThreadPlanStepOut.h"
30#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
31#include "lldb/Target/ThreadPlanStepThrough.h"
32#include "lldb/Target/ThreadPlanStepInRange.h"
33#include "lldb/Target/ThreadPlanStepOverRange.h"
34#include "lldb/Target/ThreadPlanRunToAddress.h"
35#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000036#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000037#include "lldb/Target/Unwind.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
42Thread::Thread (Process &process, lldb::tid_t tid) :
43 UserID (tid),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000044 ThreadInstanceSettings (*GetSettingsController()),
Benjamin Kramer36a08102010-07-16 12:32:33 +000045 m_process (process),
Greg Clayton643ee732010-08-04 01:40:35 +000046 m_actual_stop_info_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_index_id (process.GetNextThreadIndexID ()),
48 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000049 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +000050 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000051 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +000052 m_completed_plan_stack(),
Greg Claytonf40e3082010-08-26 02:28:22 +000053 m_curr_frames_ap (),
Chris Lattner24943d22010-06-08 16:52:24 +000054 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000055 m_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +000056 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000057 m_destroy_called (false),
58 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +000059
Chris Lattner24943d22010-06-08 16:52:24 +000060{
Greg Claytone005f2c2010-11-06 01:53:30 +000061 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000062 if (log)
63 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
64
65 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000066 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000067}
68
69
70Thread::~Thread()
71{
Greg Claytone005f2c2010-11-06 01:53:30 +000072 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000073 if (log)
74 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000075 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
76 assert (m_destroy_called);
77}
78
79void
80Thread::DestroyThread ()
81{
82 m_plan_stack.clear();
83 m_discarded_plan_stack.clear();
84 m_completed_plan_stack.clear();
85 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +000086}
87
Jim Ingham6297a3a2010-10-20 00:39:53 +000088lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +000089Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000090{
Jim Ingham6297a3a2010-10-20 00:39:53 +000091 ThreadPlanSP plan_sp (GetCompletedPlan());
92 if (plan_sp)
93 return StopInfo::CreateStopReasonWithPlan (plan_sp);
94 else
95 return GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +000096}
97
Jim Ingham15dcb7c2011-01-20 02:03:18 +000098void
99Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
100{
101 m_actual_stop_info_sp = stop_info_sp;
102 m_thread_stop_reason_stop_id = GetProcess().GetStopID();
103}
104
105void
106Thread::SetStopInfoToNothing()
107{
108 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
109 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
110 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
111}
112
Chris Lattner24943d22010-06-08 16:52:24 +0000113bool
114Thread::ThreadStoppedForAReason (void)
115{
Greg Clayton643ee732010-08-04 01:40:35 +0000116 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000117}
118
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000119bool
120Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
121{
122 if (!SaveFrameZeroState(saved_state.register_backup))
123 return false;
124
125 saved_state.stop_info_sp = GetStopInfo();
126 saved_state.orig_stop_id = GetProcess().GetStopID();
127
128 return true;
129}
130
131bool
132Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
133{
134 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000135 if (saved_state.stop_info_sp)
136 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000137 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);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000262
263 // We're starting from the base plan, so just let it decide;
264 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000265 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000266 should_stop = current_plan->ShouldStop (event_ptr);
267 if (log)
268 log->Printf("Base plan says should stop: %d.", current_plan->GetName(), should_stop);
269 }
270 else
271 {
272 // Otherwise, don't let the base plan override what the other plans say to do, since
273 // presumably if there were other plans they would know what to do...
274 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000275 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000276 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000277 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000278
279 should_stop = current_plan->ShouldStop(event_ptr);
280 if (log)
281 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
282 if (current_plan->MischiefManaged())
283 {
284 if (should_stop)
285 current_plan->WillStop();
286
287 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
288 // Otherwise, see if the plan's parent wants to stop.
289
290 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
291 {
292 PopPlan();
293 break;
294 }
295 else
296 {
297
298 PopPlan();
299
300 current_plan = GetCurrentPlan();
301 if (current_plan == NULL)
302 {
303 break;
304 }
305 }
306
Chris Lattner24943d22010-06-08 16:52:24 +0000307 }
308 else
309 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000310 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000311 }
Chris Lattner24943d22010-06-08 16:52:24 +0000312 }
313 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000314 if (over_ride_stop)
315 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000316 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000317 else if (current_plan->TracerExplainsStop())
318 {
319 return false;
320 }
Chris Lattner24943d22010-06-08 16:52:24 +0000321 else
322 {
323 // If the current plan doesn't explain the stop, then, find one that
324 // does and let it handle the situation.
325 ThreadPlan *plan_ptr = current_plan;
326 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
327 {
328 if (plan_ptr->PlanExplainsStop())
329 {
330 should_stop = plan_ptr->ShouldStop (event_ptr);
331 break;
332 }
333
334 }
335 }
336
337 return should_stop;
338}
339
340Vote
341Thread::ShouldReportStop (Event* event_ptr)
342{
343 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000344 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000345
346 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
347 {
348 if (log)
349 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 +0000350 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000351 }
Chris Lattner24943d22010-06-08 16:52:24 +0000352
353 if (m_completed_plan_stack.size() > 0)
354 {
355 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000356 if (log)
357 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 +0000358 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
359 }
360 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000361 {
362 if (log)
363 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000364 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000365 }
Chris Lattner24943d22010-06-08 16:52:24 +0000366}
367
368Vote
369Thread::ShouldReportRun (Event* event_ptr)
370{
371 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000372
Chris Lattner24943d22010-06-08 16:52:24 +0000373 if (thread_state == eStateSuspended
374 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000375 {
Chris Lattner24943d22010-06-08 16:52:24 +0000376 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000377 }
378
379 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000380 if (m_completed_plan_stack.size() > 0)
381 {
382 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000383 if (log)
384 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
385 GetIndexID(),
386 GetID(),
387 m_completed_plan_stack.back()->GetName());
388
Chris Lattner24943d22010-06-08 16:52:24 +0000389 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
390 }
391 else
Jim Inghamac959662011-01-24 06:34:17 +0000392 {
393 if (log)
394 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
395 GetIndexID(),
396 GetID(),
397 GetCurrentPlan()->GetName());
398
Chris Lattner24943d22010-06-08 16:52:24 +0000399 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000400 }
Chris Lattner24943d22010-06-08 16:52:24 +0000401}
402
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000403bool
404Thread::MatchesSpec (const ThreadSpec *spec)
405{
406 if (spec == NULL)
407 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000408
Jim Ingham649492b2010-06-18 01:00:58 +0000409 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000410}
411
Chris Lattner24943d22010-06-08 16:52:24 +0000412void
413Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
414{
415 if (thread_plan_sp)
416 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000417 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
418 if (!thread_plan_sp->GetThreadPlanTracer())
419 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000420 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000421
Chris Lattner24943d22010-06-08 16:52:24 +0000422 thread_plan_sp->DidPush();
423
Greg Claytone005f2c2010-11-06 01:53:30 +0000424 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000425 if (log)
426 {
427 StreamString s;
428 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000429 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000430 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000431 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000432 }
433 }
434}
435
436void
437Thread::PopPlan ()
438{
Greg Claytone005f2c2010-11-06 01:53:30 +0000439 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000440
Jim Ingham6297a3a2010-10-20 00:39:53 +0000441 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000442 return;
443 else
444 {
445 ThreadPlanSP &plan = m_plan_stack.back();
446 if (log)
447 {
Jim Ingham83e8b9d2010-11-10 18:17:03 +0000448 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000449 }
450 m_completed_plan_stack.push_back (plan);
451 plan->WillPop();
452 m_plan_stack.pop_back();
453 }
454}
455
456void
457Thread::DiscardPlan ()
458{
459 if (m_plan_stack.size() > 1)
460 {
461 ThreadPlanSP &plan = m_plan_stack.back();
462 m_discarded_plan_stack.push_back (plan);
463 plan->WillPop();
464 m_plan_stack.pop_back();
465 }
466}
467
468ThreadPlan *
469Thread::GetCurrentPlan ()
470{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000471 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000472 return NULL;
473 else
474 return m_plan_stack.back().get();
475}
476
477ThreadPlanSP
478Thread::GetCompletedPlan ()
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 ThreadPlanSP completed_plan_sp;
486 completed_plan_sp = m_completed_plan_stack[i];
487 if (!completed_plan_sp->GetPrivate ())
488 return completed_plan_sp;
489 }
490 }
491 return empty_plan_sp;
492}
493
494bool
495Thread::IsThreadPlanDone (ThreadPlan *plan)
496{
Chris Lattner24943d22010-06-08 16:52:24 +0000497 if (!m_completed_plan_stack.empty())
498 {
499 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
500 {
501 if (m_completed_plan_stack[i].get() == plan)
502 return true;
503 }
504 }
505 return false;
506}
507
508bool
509Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
510{
Chris Lattner24943d22010-06-08 16:52:24 +0000511 if (!m_discarded_plan_stack.empty())
512 {
513 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
514 {
515 if (m_discarded_plan_stack[i].get() == plan)
516 return true;
517 }
518 }
519 return false;
520}
521
522ThreadPlan *
523Thread::GetPreviousPlan (ThreadPlan *current_plan)
524{
525 if (current_plan == NULL)
526 return NULL;
527
528 int stack_size = m_completed_plan_stack.size();
529 for (int i = stack_size - 1; i > 0; i--)
530 {
531 if (current_plan == m_completed_plan_stack[i].get())
532 return m_completed_plan_stack[i-1].get();
533 }
534
535 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
536 {
Chris Lattner24943d22010-06-08 16:52:24 +0000537 if (m_plan_stack.size() > 0)
538 return m_plan_stack.back().get();
539 else
540 return NULL;
541 }
542
543 stack_size = m_plan_stack.size();
544 for (int i = stack_size - 1; i > 0; i--)
545 {
546 if (current_plan == m_plan_stack[i].get())
547 return m_plan_stack[i-1].get();
548 }
549 return NULL;
550}
551
552void
553Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
554{
555 if (abort_other_plans)
556 DiscardThreadPlans(true);
557
558 PushPlan (thread_plan_sp);
559}
560
Jim Ingham745ac7a2010-11-11 19:26:09 +0000561
562void
563Thread::EnableTracer (bool value, bool single_stepping)
564{
565 int stack_size = m_plan_stack.size();
566 for (int i = 0; i < stack_size; i++)
567 {
568 if (m_plan_stack[i]->GetThreadPlanTracer())
569 {
570 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
571 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
572 }
573 }
574}
575
576void
577Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
578{
579 int stack_size = m_plan_stack.size();
580 for (int i = 0; i < stack_size; i++)
581 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
582}
583
Chris Lattner24943d22010-06-08 16:52:24 +0000584void
Jim Inghamea9d4262010-11-05 19:25:48 +0000585Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
586{
Greg Claytone005f2c2010-11-06 01:53:30 +0000587 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000588 if (log)
589 {
590 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get());
591 }
592
593 int stack_size = m_plan_stack.size();
594
595 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
596 // stack, and if so discard up to and including it.
597
598 if (up_to_plan_sp.get() == NULL)
599 {
600 for (int i = stack_size - 1; i > 0; i--)
601 DiscardPlan();
602 }
603 else
604 {
605 bool found_it = false;
606 for (int i = stack_size - 1; i > 0; i--)
607 {
608 if (m_plan_stack[i] == up_to_plan_sp)
609 found_it = true;
610 }
611 if (found_it)
612 {
613 bool last_one = false;
614 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
615 {
616 if (GetCurrentPlan() == up_to_plan_sp.get())
617 last_one = true;
618 DiscardPlan();
619 }
620 }
621 }
622 return;
623}
624
625void
Chris Lattner24943d22010-06-08 16:52:24 +0000626Thread::DiscardThreadPlans(bool force)
627{
Greg Claytone005f2c2010-11-06 01:53:30 +0000628 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000629 if (log)
630 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000631 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000632 }
633
634 if (force)
635 {
636 int stack_size = m_plan_stack.size();
637 for (int i = stack_size - 1; i > 0; i--)
638 {
639 DiscardPlan();
640 }
641 return;
642 }
643
644 while (1)
645 {
646
647 int master_plan_idx;
648 bool discard;
649
650 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
651 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
652 {
653 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
654 {
655 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
656 break;
657 }
658 }
659
660 if (discard)
661 {
662 // First pop all the dependent plans:
663 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
664 {
665
666 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
667 // for the plan leaves it in a state that it is safe to pop the plan
668 // with no more notice?
669 DiscardPlan();
670 }
671
672 // Now discard the master plan itself.
673 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
674 // discard it's dependent plans, but not it...
675 if (master_plan_idx > 0)
676 {
677 DiscardPlan();
678 }
679 }
680 else
681 {
682 // If the master plan doesn't want to get discarded, then we're done.
683 break;
684 }
685
686 }
Chris Lattner24943d22010-06-08 16:52:24 +0000687}
688
689ThreadPlan *
690Thread::QueueFundamentalPlan (bool abort_other_plans)
691{
692 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
693 QueueThreadPlan (thread_plan_sp, abort_other_plans);
694 return thread_plan_sp.get();
695}
696
697ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000698Thread::QueueThreadPlanForStepSingleInstruction
699(
700 bool step_over,
701 bool abort_other_plans,
702 bool stop_other_threads
703)
Chris Lattner24943d22010-06-08 16:52:24 +0000704{
705 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
706 QueueThreadPlan (thread_plan_sp, abort_other_plans);
707 return thread_plan_sp.get();
708}
709
710ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000711Thread::QueueThreadPlanForStepRange
712(
713 bool abort_other_plans,
714 StepType type,
715 const AddressRange &range,
716 const SymbolContext &addr_context,
717 lldb::RunMode stop_other_threads,
718 bool avoid_code_without_debug_info
719)
Chris Lattner24943d22010-06-08 16:52:24 +0000720{
721 ThreadPlanSP thread_plan_sp;
722 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000723 {
724 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
725 if (avoid_code_without_debug_info)
726 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
727 else
728 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
729 thread_plan_sp.reset (plan);
730 }
Chris Lattner24943d22010-06-08 16:52:24 +0000731 else
732 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
733
734 QueueThreadPlan (thread_plan_sp, abort_other_plans);
735 return thread_plan_sp.get();
736}
737
738
739ThreadPlan *
740Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
741{
742 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
743 QueueThreadPlan (thread_plan_sp, abort_other_plans);
744 return thread_plan_sp.get();
745}
746
747ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000748Thread::QueueThreadPlanForStepOut
749(
750 bool abort_other_plans,
751 SymbolContext *addr_context,
752 bool first_insn,
753 bool stop_other_threads,
754 Vote stop_vote,
755 Vote run_vote,
756 uint32_t frame_idx
757)
Chris Lattner24943d22010-06-08 16:52:24 +0000758{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000759 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
760 addr_context,
761 first_insn,
762 stop_other_threads,
763 stop_vote,
764 run_vote,
765 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000766 QueueThreadPlan (thread_plan_sp, abort_other_plans);
767 return thread_plan_sp.get();
768}
769
770ThreadPlan *
771Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
772{
Jim Inghamb66cd072010-09-28 01:25:32 +0000773 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000774 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000775 // If that didn't come up with anything, try the ObjC runtime plugin:
776 if (thread_plan_sp.get() == NULL)
777 {
778 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
779 if (objc_runtime)
780 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
781 }
782
Chris Lattner24943d22010-06-08 16:52:24 +0000783 if (thread_plan_sp.get() == NULL)
784 {
785 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
786 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000787 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000788 }
789 QueueThreadPlan (thread_plan_sp, abort_other_plans);
790 return thread_plan_sp.get();
791}
792
793ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000794Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
795 Address& function,
796 lldb::addr_t arg,
797 bool stop_other_threads,
798 bool discard_on_error)
799{
800 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
801 QueueThreadPlan (thread_plan_sp, abort_other_plans);
802 return thread_plan_sp.get();
803}
804
805ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000806Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
807 Address &target_addr,
808 bool stop_other_threads)
809{
810 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
811 QueueThreadPlan (thread_plan_sp, abort_other_plans);
812 return thread_plan_sp.get();
813}
814
815ThreadPlan *
816Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000817 lldb::addr_t *address_list,
818 size_t num_addresses,
819 bool stop_other_threads,
820 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000821{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000822 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000823 QueueThreadPlan (thread_plan_sp, abort_other_plans);
824 return thread_plan_sp.get();
825
826}
827
828uint32_t
829Thread::GetIndexID () const
830{
831 return m_index_id;
832}
833
834void
835Thread::DumpThreadPlans (lldb_private::Stream *s) const
836{
837 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000838 int i;
839 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
840 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000841 {
842 s->Printf ("Element %d: ", i);
843 s->IndentMore();
844 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
845 s->IndentLess();
846 s->EOL();
847 }
848
Chris Lattner24943d22010-06-08 16:52:24 +0000849 stack_size = m_completed_plan_stack.size();
850 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000851 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000852 {
853 s->Printf ("Element %d: ", i);
854 s->IndentMore();
855 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
856 s->IndentLess();
857 s->EOL();
858 }
859
860 stack_size = m_discarded_plan_stack.size();
861 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000862 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000863 {
864 s->Printf ("Element %d: ", i);
865 s->IndentMore();
866 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
867 s->IndentLess();
868 s->EOL();
869 }
870
871}
872
873Target *
874Thread::CalculateTarget ()
875{
876 return m_process.CalculateTarget();
877}
878
879Process *
880Thread::CalculateProcess ()
881{
882 return &m_process;
883}
884
885Thread *
886Thread::CalculateThread ()
887{
888 return this;
889}
890
891StackFrame *
892Thread::CalculateStackFrame ()
893{
894 return NULL;
895}
896
897void
Greg Claytona830adb2010-10-04 01:05:56 +0000898Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000899{
Greg Claytona830adb2010-10-04 01:05:56 +0000900 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000901 exe_ctx.thread = this;
902 exe_ctx.frame = NULL;
903}
904
Greg Clayton782b9cc2010-08-25 00:35:26 +0000905
906StackFrameList &
907Thread::GetStackFrameList ()
908{
Greg Claytonf40e3082010-08-26 02:28:22 +0000909 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000910 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000911 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000912}
913
914
915
Jim Ingham71219082010-08-12 02:14:28 +0000916uint32_t
917Thread::GetStackFrameCount()
918{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000919 return GetStackFrameList().GetNumFrames();
920}
Greg Clayton33ed1702010-08-24 00:45:41 +0000921
Greg Clayton33ed1702010-08-24 00:45:41 +0000922
Greg Clayton782b9cc2010-08-25 00:35:26 +0000923void
924Thread::ClearStackFrames ()
925{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000926 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
927 m_prev_frames_sp.reset (m_curr_frames_ap.release());
928 else
929 m_curr_frames_ap.release();
930
931// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
932// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000933}
934
935lldb::StackFrameSP
936Thread::GetStackFrameAtIndex (uint32_t idx)
937{
Greg Clayton72b71582010-09-02 21:44:10 +0000938 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000939}
940
Greg Claytonc12b6b42010-10-10 22:28:11 +0000941uint32_t
942Thread::GetSelectedFrameIndex ()
943{
944 return GetStackFrameList().GetSelectedFrameIndex();
945}
946
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000947lldb::StackFrameSP
948Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
949{
950 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
951}
952
953
Greg Claytonc12b6b42010-10-10 22:28:11 +0000954
Chris Lattner24943d22010-06-08 16:52:24 +0000955lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000956Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000957{
Jim Inghamc8332952010-08-26 21:32:51 +0000958 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000959}
960
961uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000962Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000963{
Jim Inghamc8332952010-08-26 21:32:51 +0000964 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000965}
966
967void
Jim Inghamc8332952010-08-26 21:32:51 +0000968Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000969{
Jim Inghamc8332952010-08-26 21:32:51 +0000970 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000971}
972
973void
Greg Claytona830adb2010-10-04 01:05:56 +0000974Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000975{
Greg Claytona830adb2010-10-04 01:05:56 +0000976 ExecutionContext exe_ctx;
977 SymbolContext frame_sc;
978 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000979
Greg Claytona830adb2010-10-04 01:05:56 +0000980 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000981 {
Greg Claytona830adb2010-10-04 01:05:56 +0000982 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000983 if (frame_sp)
984 {
Greg Claytona830adb2010-10-04 01:05:56 +0000985 exe_ctx.frame = frame_sp.get();
986 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000987 }
988 }
989
Greg Claytona830adb2010-10-04 01:05:56 +0000990 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
991 assert (thread_format);
992 const char *end = NULL;
993 Debugger::FormatPrompt (thread_format,
994 exe_ctx.frame ? &frame_sc : NULL,
995 &exe_ctx,
996 NULL,
997 strm,
998 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000999}
1000
1001lldb::ThreadSP
1002Thread::GetSP ()
1003{
1004 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
1005}
Jim Ingham20594b12010-09-08 03:14:33 +00001006
Greg Clayton990de7b2010-11-18 23:32:35 +00001007
1008void
Caroline Tice2a456812011-03-10 22:14:10 +00001009Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001010{
Greg Clayton990de7b2010-11-18 23:32:35 +00001011 UserSettingsControllerSP &usc = GetSettingsController();
1012 usc.reset (new SettingsController);
1013 UserSettingsController::InitializeSettingsController (usc,
1014 SettingsController::global_settings_table,
1015 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001016
1017 // Now call SettingsInitialize() on each 'child' setting of Thread.
1018 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001019}
Jim Ingham20594b12010-09-08 03:14:33 +00001020
Greg Clayton990de7b2010-11-18 23:32:35 +00001021void
Caroline Tice2a456812011-03-10 22:14:10 +00001022Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001023{
Caroline Tice2a456812011-03-10 22:14:10 +00001024 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1025 // Currently there are none.
1026
1027 // Now terminate Thread Settings.
1028
Greg Clayton990de7b2010-11-18 23:32:35 +00001029 UserSettingsControllerSP &usc = GetSettingsController();
1030 UserSettingsController::FinalizeSettingsController (usc);
1031 usc.reset();
1032}
Jim Ingham20594b12010-09-08 03:14:33 +00001033
Greg Clayton990de7b2010-11-18 23:32:35 +00001034UserSettingsControllerSP &
1035Thread::GetSettingsController ()
1036{
1037 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001038 return g_settings_controller;
1039}
1040
Caroline Tice1ebef442010-09-27 00:30:10 +00001041void
1042Thread::UpdateInstanceName ()
1043{
1044 StreamString sstr;
1045 const char *name = GetName();
1046
1047 if (name && name[0] != '\0')
1048 sstr.Printf ("%s", name);
1049 else if ((GetIndexID() != 0) || (GetID() != 0))
1050 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1051
1052 if (sstr.GetSize() > 0)
1053 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1054}
1055
Jim Inghamccd584d2010-09-23 17:40:12 +00001056lldb::StackFrameSP
1057Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1058{
1059 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1060}
Caroline Tice7826c882010-10-26 03:11:13 +00001061
1062const char *
1063Thread::StopReasonAsCString (lldb::StopReason reason)
1064{
1065 switch (reason)
1066 {
1067 case eStopReasonInvalid: return "invalid";
1068 case eStopReasonNone: return "none";
1069 case eStopReasonTrace: return "trace";
1070 case eStopReasonBreakpoint: return "breakpoint";
1071 case eStopReasonWatchpoint: return "watchpoint";
1072 case eStopReasonSignal: return "signal";
1073 case eStopReasonException: return "exception";
1074 case eStopReasonPlanComplete: return "plan complete";
1075 }
1076
1077
1078 static char unknown_state_string[64];
1079 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1080 return unknown_state_string;
1081}
1082
1083const char *
1084Thread::RunModeAsCString (lldb::RunMode mode)
1085{
1086 switch (mode)
1087 {
1088 case eOnlyThisThread: return "only this thread";
1089 case eAllThreads: return "all threads";
1090 case eOnlyDuringStepping: return "only during stepping";
1091 }
1092
1093 static char unknown_state_string[64];
1094 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1095 return unknown_state_string;
1096}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001097
Greg Clayton990de7b2010-11-18 23:32:35 +00001098#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001099//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001100// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001101//--------------------------------------------------------------
1102
Greg Clayton990de7b2010-11-18 23:32:35 +00001103Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001104 UserSettingsController ("thread", Process::GetSettingsController())
1105{
1106 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1107 InstanceSettings::GetDefaultName().AsCString()));
1108}
1109
Greg Clayton990de7b2010-11-18 23:32:35 +00001110Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001111{
1112}
1113
1114lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001115Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001116{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001117 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1118 false,
1119 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001120 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1121 return new_settings_sp;
1122}
1123
1124#pragma mark "ThreadInstanceSettings"
1125//--------------------------------------------------------------
1126// class ThreadInstanceSettings
1127//--------------------------------------------------------------
1128
1129ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001130 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001131 m_avoid_regexp_ap (),
1132 m_trace_enabled (false)
1133{
1134 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1135 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1136 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1137 // This is true for CreateInstanceName() too.
1138
1139 if (GetInstanceName() == InstanceSettings::InvalidName())
1140 {
1141 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1142 m_owner.RegisterInstanceSettings (this);
1143 }
1144
1145 if (live_instance)
1146 {
1147 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1148 CopyInstanceSettings (pending_settings,false);
1149 //m_owner.RemovePendingSettings (m_instance_name);
1150 }
1151}
1152
1153ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001154 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001155 m_avoid_regexp_ap (),
1156 m_trace_enabled (rhs.m_trace_enabled)
1157{
1158 if (m_instance_name != InstanceSettings::GetDefaultName())
1159 {
1160 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1161 CopyInstanceSettings (pending_settings,false);
1162 m_owner.RemovePendingSettings (m_instance_name);
1163 }
1164 if (rhs.m_avoid_regexp_ap.get() != NULL)
1165 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1166}
1167
1168ThreadInstanceSettings::~ThreadInstanceSettings ()
1169{
1170}
1171
1172ThreadInstanceSettings&
1173ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1174{
1175 if (this != &rhs)
1176 {
1177 if (rhs.m_avoid_regexp_ap.get() != NULL)
1178 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1179 else
1180 m_avoid_regexp_ap.reset(NULL);
1181 }
1182 m_trace_enabled = rhs.m_trace_enabled;
1183 return *this;
1184}
1185
1186
1187void
1188ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1189 const char *index_value,
1190 const char *value,
1191 const ConstString &instance_name,
1192 const SettingEntry &entry,
1193 lldb::VarSetOperationType op,
1194 Error &err,
1195 bool pending)
1196{
1197 if (var_name == StepAvoidRegexpVarName())
1198 {
1199 std::string regexp_text;
1200 if (m_avoid_regexp_ap.get() != NULL)
1201 regexp_text.append (m_avoid_regexp_ap->GetText());
1202 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1203 if (regexp_text.empty())
1204 m_avoid_regexp_ap.reset();
1205 else
1206 {
1207 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1208
1209 }
1210 }
1211 else if (var_name == GetTraceThreadVarName())
1212 {
1213 bool success;
1214 bool result = Args::StringToBoolean(value, false, &success);
1215
1216 if (success)
1217 {
1218 m_trace_enabled = result;
1219 if (!pending)
1220 {
1221 Thread *myself = static_cast<Thread *> (this);
1222 myself->EnableTracer(m_trace_enabled, true);
1223 }
1224 }
1225 else
1226 {
1227 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1228 }
1229
1230 }
1231}
1232
1233void
1234ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1235 bool pending)
1236{
1237 if (new_settings.get() == NULL)
1238 return;
1239
1240 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1241 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1242 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1243 else
1244 m_avoid_regexp_ap.reset ();
1245}
1246
1247bool
1248ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1249 const ConstString &var_name,
1250 StringList &value,
1251 Error *err)
1252{
1253 if (var_name == StepAvoidRegexpVarName())
1254 {
1255 if (m_avoid_regexp_ap.get() != NULL)
1256 {
1257 std::string regexp_text("\"");
1258 regexp_text.append(m_avoid_regexp_ap->GetText());
1259 regexp_text.append ("\"");
1260 value.AppendString (regexp_text.c_str());
1261 }
1262
1263 }
1264 else if (var_name == GetTraceThreadVarName())
1265 {
1266 value.AppendString(m_trace_enabled ? "true" : "false");
1267 }
1268 else
1269 {
1270 if (err)
1271 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1272 return false;
1273 }
1274 return true;
1275}
1276
1277const ConstString
1278ThreadInstanceSettings::CreateInstanceName ()
1279{
1280 static int instance_count = 1;
1281 StreamString sstr;
1282
1283 sstr.Printf ("thread_%d", instance_count);
1284 ++instance_count;
1285
1286 const ConstString ret_val (sstr.GetData());
1287 return ret_val;
1288}
1289
1290const ConstString &
1291ThreadInstanceSettings::StepAvoidRegexpVarName ()
1292{
1293 static ConstString step_avoid_var_name ("step-avoid-regexp");
1294
1295 return step_avoid_var_name;
1296}
1297
1298const ConstString &
1299ThreadInstanceSettings::GetTraceThreadVarName ()
1300{
1301 static ConstString trace_thread_var_name ("trace-thread");
1302
1303 return trace_thread_var_name;
1304}
1305
1306//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001307// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001308//--------------------------------------------------
1309
1310SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001311Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001312{
1313 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1314 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1315};
1316
1317
1318SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001319Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001320{
1321 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1322 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1323 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1324 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1325};