blob: 11d28acd78d4124912bc96afb88c8e082aee1563 [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"
Greg Claytonabe0fed2011-04-18 08:33:37 +000017#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#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 Claytonc51ffbf2011-08-12 21:40:01 +000054 m_curr_frames_sp (),
55 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000056 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000057 m_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +000058 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000059 m_destroy_called (false),
60 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +000061
Chris Lattner24943d22010-06-08 16:52:24 +000062{
Greg Claytone005f2c2010-11-06 01:53:30 +000063 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000064 if (log)
65 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
66
67 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000068 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000069}
70
71
72Thread::~Thread()
73{
Greg Claytone005f2c2010-11-06 01:53:30 +000074 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000075 if (log)
76 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000077 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
78 assert (m_destroy_called);
79}
80
81void
82Thread::DestroyThread ()
83{
84 m_plan_stack.clear();
85 m_discarded_plan_stack.clear();
86 m_completed_plan_stack.clear();
87 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +000088}
89
Jim Ingham6297a3a2010-10-20 00:39:53 +000090lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +000091Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000092{
Jim Ingham6297a3a2010-10-20 00:39:53 +000093 ThreadPlanSP plan_sp (GetCompletedPlan());
94 if (plan_sp)
95 return StopInfo::CreateStopReasonWithPlan (plan_sp);
96 else
Jim Ingham24e0d612011-08-09 00:32:52 +000097 {
98 if (m_actual_stop_info_sp
99 && m_actual_stop_info_sp->IsValid()
100 && m_thread_stop_reason_stop_id == m_process.GetStopID())
101 return m_actual_stop_info_sp;
102 else
103 return GetPrivateStopReason ();
104 }
Chris Lattner24943d22010-06-08 16:52:24 +0000105}
106
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000107void
108Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
109{
110 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000111 if (m_actual_stop_info_sp)
112 m_actual_stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000113 m_thread_stop_reason_stop_id = GetProcess().GetStopID();
114}
115
116void
117Thread::SetStopInfoToNothing()
118{
119 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
120 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
121 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
122}
123
Chris Lattner24943d22010-06-08 16:52:24 +0000124bool
125Thread::ThreadStoppedForAReason (void)
126{
Greg Clayton643ee732010-08-04 01:40:35 +0000127 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000128}
129
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000130bool
131Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
132{
133 if (!SaveFrameZeroState(saved_state.register_backup))
134 return false;
135
136 saved_state.stop_info_sp = GetStopInfo();
137 saved_state.orig_stop_id = GetProcess().GetStopID();
138
139 return true;
140}
141
142bool
143Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
144{
145 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000146 if (saved_state.stop_info_sp)
147 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000148 SetStopInfo(saved_state.stop_info_sp);
149 return true;
150}
151
Chris Lattner24943d22010-06-08 16:52:24 +0000152StateType
153Thread::GetState() const
154{
155 // If any other threads access this we will need a mutex for it
156 Mutex::Locker locker(m_state_mutex);
157 return m_state;
158}
159
160void
161Thread::SetState(StateType state)
162{
163 Mutex::Locker locker(m_state_mutex);
164 m_state = state;
165}
166
167void
168Thread::WillStop()
169{
170 ThreadPlan *current_plan = GetCurrentPlan();
171
172 // FIXME: I may decide to disallow threads with no plans. In which
173 // case this should go to an assert.
174
175 if (!current_plan)
176 return;
177
178 current_plan->WillStop();
179}
180
181void
182Thread::SetupForResume ()
183{
184 if (GetResumeState() != eStateSuspended)
185 {
186
187 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
188 // telling the current plan it will resume, since we might change what the current
189 // plan is.
190
191 lldb::addr_t pc = GetRegisterContext()->GetPC();
192 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
193 if (bp_site_sp && bp_site_sp->IsEnabled())
194 {
195 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
196 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000197
198 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000199
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000200 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
201 {
202 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
203 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000204 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000205 ThreadPlanSP step_bp_plan_sp;
206 step_bp_plan->SetPrivate (true);
207
Chris Lattner24943d22010-06-08 16:52:24 +0000208 if (GetCurrentPlan()->RunState() != eStateStepping)
209 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000210 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000211 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000212 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000213 QueueThreadPlan (step_bp_plan_sp, false);
214 }
215 }
216 }
217 }
218}
219
220bool
221Thread::WillResume (StateType resume_state)
222{
223 // At this point clear the completed plan stack.
224 m_completed_plan_stack.clear();
225 m_discarded_plan_stack.clear();
226
Greg Clayton643ee732010-08-04 01:40:35 +0000227 StopInfo *stop_info = GetPrivateStopReason().get();
228 if (stop_info)
229 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000230
231 // Tell all the plans that we are about to resume in case they need to clear any state.
232 // We distinguish between the plan on the top of the stack and the lower
233 // plans in case a plan needs to do any special business before it runs.
234
235 ThreadPlan *plan_ptr = GetCurrentPlan();
236 plan_ptr->WillResume(resume_state, true);
237
238 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
239 {
240 plan_ptr->WillResume (resume_state, false);
241 }
Greg Clayton643ee732010-08-04 01:40:35 +0000242
Greg Clayton643ee732010-08-04 01:40:35 +0000243 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000244 return true;
245}
246
247void
248Thread::DidResume ()
249{
250 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
251}
252
253bool
254Thread::ShouldStop (Event* event_ptr)
255{
256 ThreadPlan *current_plan = GetCurrentPlan();
257 bool should_stop = true;
258
Greg Claytone005f2c2010-11-06 01:53:30 +0000259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000260 if (log)
261 {
262 StreamString s;
263 DumpThreadPlans(&s);
264 log->PutCString (s.GetData());
265 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000266
267 // The top most plan always gets to do the trace log...
268 current_plan->DoTraceLog ();
Chris Lattner24943d22010-06-08 16:52:24 +0000269
270 if (current_plan->PlanExplainsStop())
271 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000272 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000273
274 // We're starting from the base plan, so just let it decide;
275 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000276 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000277 should_stop = current_plan->ShouldStop (event_ptr);
278 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000279 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000280 }
281 else
282 {
283 // Otherwise, don't let the base plan override what the other plans say to do, since
284 // presumably if there were other plans they would know what to do...
285 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000286 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000287 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000288 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000289
290 should_stop = current_plan->ShouldStop(event_ptr);
291 if (log)
292 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
293 if (current_plan->MischiefManaged())
294 {
295 if (should_stop)
296 current_plan->WillStop();
297
298 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
299 // Otherwise, see if the plan's parent wants to stop.
300
301 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
302 {
303 PopPlan();
304 break;
305 }
306 else
307 {
308
309 PopPlan();
310
311 current_plan = GetCurrentPlan();
312 if (current_plan == NULL)
313 {
314 break;
315 }
316 }
317
Chris Lattner24943d22010-06-08 16:52:24 +0000318 }
319 else
320 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000321 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000322 }
Chris Lattner24943d22010-06-08 16:52:24 +0000323 }
324 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000325 if (over_ride_stop)
326 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000327 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000328 else if (current_plan->TracerExplainsStop())
329 {
330 return false;
331 }
Chris Lattner24943d22010-06-08 16:52:24 +0000332 else
333 {
334 // If the current plan doesn't explain the stop, then, find one that
335 // does and let it handle the situation.
336 ThreadPlan *plan_ptr = current_plan;
337 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
338 {
339 if (plan_ptr->PlanExplainsStop())
340 {
341 should_stop = plan_ptr->ShouldStop (event_ptr);
342 break;
343 }
344
345 }
346 }
347
348 return should_stop;
349}
350
351Vote
352Thread::ShouldReportStop (Event* event_ptr)
353{
354 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000355 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000356
357 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
358 {
359 if (log)
360 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 +0000361 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000362 }
Chris Lattner24943d22010-06-08 16:52:24 +0000363
364 if (m_completed_plan_stack.size() > 0)
365 {
366 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000367 if (log)
368 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 +0000369 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
370 }
371 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000372 {
373 if (log)
374 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000375 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000376 }
Chris Lattner24943d22010-06-08 16:52:24 +0000377}
378
379Vote
380Thread::ShouldReportRun (Event* event_ptr)
381{
382 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000383
Chris Lattner24943d22010-06-08 16:52:24 +0000384 if (thread_state == eStateSuspended
385 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000386 {
Chris Lattner24943d22010-06-08 16:52:24 +0000387 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000388 }
389
390 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000391 if (m_completed_plan_stack.size() > 0)
392 {
393 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000394 if (log)
395 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
396 GetIndexID(),
397 GetID(),
398 m_completed_plan_stack.back()->GetName());
399
Chris Lattner24943d22010-06-08 16:52:24 +0000400 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
401 }
402 else
Jim Inghamac959662011-01-24 06:34:17 +0000403 {
404 if (log)
405 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
406 GetIndexID(),
407 GetID(),
408 GetCurrentPlan()->GetName());
409
Chris Lattner24943d22010-06-08 16:52:24 +0000410 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000411 }
Chris Lattner24943d22010-06-08 16:52:24 +0000412}
413
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000414bool
415Thread::MatchesSpec (const ThreadSpec *spec)
416{
417 if (spec == NULL)
418 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000419
Jim Ingham649492b2010-06-18 01:00:58 +0000420 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000421}
422
Chris Lattner24943d22010-06-08 16:52:24 +0000423void
424Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
425{
426 if (thread_plan_sp)
427 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000428 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
429 if (!thread_plan_sp->GetThreadPlanTracer())
430 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000431 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000432
Chris Lattner24943d22010-06-08 16:52:24 +0000433 thread_plan_sp->DidPush();
434
Greg Claytone005f2c2010-11-06 01:53:30 +0000435 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000436 if (log)
437 {
438 StreamString s;
439 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000440 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000441 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000442 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000443 }
444 }
445}
446
447void
448Thread::PopPlan ()
449{
Greg Claytone005f2c2010-11-06 01:53:30 +0000450 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000451
Jim Ingham6297a3a2010-10-20 00:39:53 +0000452 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000453 return;
454 else
455 {
456 ThreadPlanSP &plan = m_plan_stack.back();
457 if (log)
458 {
Jim Ingham83e8b9d2010-11-10 18:17:03 +0000459 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000460 }
461 m_completed_plan_stack.push_back (plan);
462 plan->WillPop();
463 m_plan_stack.pop_back();
464 }
465}
466
467void
468Thread::DiscardPlan ()
469{
470 if (m_plan_stack.size() > 1)
471 {
472 ThreadPlanSP &plan = m_plan_stack.back();
473 m_discarded_plan_stack.push_back (plan);
474 plan->WillPop();
475 m_plan_stack.pop_back();
476 }
477}
478
479ThreadPlan *
480Thread::GetCurrentPlan ()
481{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000482 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000483 return NULL;
484 else
485 return m_plan_stack.back().get();
486}
487
488ThreadPlanSP
489Thread::GetCompletedPlan ()
490{
491 ThreadPlanSP empty_plan_sp;
492 if (!m_completed_plan_stack.empty())
493 {
494 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
495 {
496 ThreadPlanSP completed_plan_sp;
497 completed_plan_sp = m_completed_plan_stack[i];
498 if (!completed_plan_sp->GetPrivate ())
499 return completed_plan_sp;
500 }
501 }
502 return empty_plan_sp;
503}
504
505bool
506Thread::IsThreadPlanDone (ThreadPlan *plan)
507{
Chris Lattner24943d22010-06-08 16:52:24 +0000508 if (!m_completed_plan_stack.empty())
509 {
510 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
511 {
512 if (m_completed_plan_stack[i].get() == plan)
513 return true;
514 }
515 }
516 return false;
517}
518
519bool
520Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
521{
Chris Lattner24943d22010-06-08 16:52:24 +0000522 if (!m_discarded_plan_stack.empty())
523 {
524 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
525 {
526 if (m_discarded_plan_stack[i].get() == plan)
527 return true;
528 }
529 }
530 return false;
531}
532
533ThreadPlan *
534Thread::GetPreviousPlan (ThreadPlan *current_plan)
535{
536 if (current_plan == NULL)
537 return NULL;
538
539 int stack_size = m_completed_plan_stack.size();
540 for (int i = stack_size - 1; i > 0; i--)
541 {
542 if (current_plan == m_completed_plan_stack[i].get())
543 return m_completed_plan_stack[i-1].get();
544 }
545
546 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
547 {
Chris Lattner24943d22010-06-08 16:52:24 +0000548 if (m_plan_stack.size() > 0)
549 return m_plan_stack.back().get();
550 else
551 return NULL;
552 }
553
554 stack_size = m_plan_stack.size();
555 for (int i = stack_size - 1; i > 0; i--)
556 {
557 if (current_plan == m_plan_stack[i].get())
558 return m_plan_stack[i-1].get();
559 }
560 return NULL;
561}
562
563void
564Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
565{
566 if (abort_other_plans)
567 DiscardThreadPlans(true);
568
569 PushPlan (thread_plan_sp);
570}
571
Jim Ingham745ac7a2010-11-11 19:26:09 +0000572
573void
574Thread::EnableTracer (bool value, bool single_stepping)
575{
576 int stack_size = m_plan_stack.size();
577 for (int i = 0; i < stack_size; i++)
578 {
579 if (m_plan_stack[i]->GetThreadPlanTracer())
580 {
581 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
582 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
583 }
584 }
585}
586
587void
588Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
589{
590 int stack_size = m_plan_stack.size();
591 for (int i = 0; i < stack_size; i++)
592 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
593}
594
Chris Lattner24943d22010-06-08 16:52:24 +0000595void
Jim Inghamea9d4262010-11-05 19:25:48 +0000596Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
597{
Greg Claytone005f2c2010-11-06 01:53:30 +0000598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000599 if (log)
600 {
601 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get());
602 }
603
604 int stack_size = m_plan_stack.size();
605
606 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
607 // stack, and if so discard up to and including it.
608
609 if (up_to_plan_sp.get() == NULL)
610 {
611 for (int i = stack_size - 1; i > 0; i--)
612 DiscardPlan();
613 }
614 else
615 {
616 bool found_it = false;
617 for (int i = stack_size - 1; i > 0; i--)
618 {
619 if (m_plan_stack[i] == up_to_plan_sp)
620 found_it = true;
621 }
622 if (found_it)
623 {
624 bool last_one = false;
625 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
626 {
627 if (GetCurrentPlan() == up_to_plan_sp.get())
628 last_one = true;
629 DiscardPlan();
630 }
631 }
632 }
633 return;
634}
635
636void
Chris Lattner24943d22010-06-08 16:52:24 +0000637Thread::DiscardThreadPlans(bool force)
638{
Greg Claytone005f2c2010-11-06 01:53:30 +0000639 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000640 if (log)
641 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000642 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000643 }
644
645 if (force)
646 {
647 int stack_size = m_plan_stack.size();
648 for (int i = stack_size - 1; i > 0; i--)
649 {
650 DiscardPlan();
651 }
652 return;
653 }
654
655 while (1)
656 {
657
658 int master_plan_idx;
659 bool discard;
660
661 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
662 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
663 {
664 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
665 {
666 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
667 break;
668 }
669 }
670
671 if (discard)
672 {
673 // First pop all the dependent plans:
674 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
675 {
676
677 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
678 // for the plan leaves it in a state that it is safe to pop the plan
679 // with no more notice?
680 DiscardPlan();
681 }
682
683 // Now discard the master plan itself.
684 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
685 // discard it's dependent plans, but not it...
686 if (master_plan_idx > 0)
687 {
688 DiscardPlan();
689 }
690 }
691 else
692 {
693 // If the master plan doesn't want to get discarded, then we're done.
694 break;
695 }
696
697 }
Chris Lattner24943d22010-06-08 16:52:24 +0000698}
699
700ThreadPlan *
701Thread::QueueFundamentalPlan (bool abort_other_plans)
702{
703 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
704 QueueThreadPlan (thread_plan_sp, abort_other_plans);
705 return thread_plan_sp.get();
706}
707
708ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000709Thread::QueueThreadPlanForStepSingleInstruction
710(
711 bool step_over,
712 bool abort_other_plans,
713 bool stop_other_threads
714)
Chris Lattner24943d22010-06-08 16:52:24 +0000715{
716 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
717 QueueThreadPlan (thread_plan_sp, abort_other_plans);
718 return thread_plan_sp.get();
719}
720
721ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000722Thread::QueueThreadPlanForStepRange
723(
724 bool abort_other_plans,
725 StepType type,
726 const AddressRange &range,
727 const SymbolContext &addr_context,
728 lldb::RunMode stop_other_threads,
729 bool avoid_code_without_debug_info
730)
Chris Lattner24943d22010-06-08 16:52:24 +0000731{
732 ThreadPlanSP thread_plan_sp;
733 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000734 {
735 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
736 if (avoid_code_without_debug_info)
737 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
738 else
739 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
740 thread_plan_sp.reset (plan);
741 }
Chris Lattner24943d22010-06-08 16:52:24 +0000742 else
743 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
744
745 QueueThreadPlan (thread_plan_sp, abort_other_plans);
746 return thread_plan_sp.get();
747}
748
749
750ThreadPlan *
751Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
752{
753 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
754 QueueThreadPlan (thread_plan_sp, abort_other_plans);
755 return thread_plan_sp.get();
756}
757
758ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000759Thread::QueueThreadPlanForStepOut
760(
761 bool abort_other_plans,
762 SymbolContext *addr_context,
763 bool first_insn,
764 bool stop_other_threads,
765 Vote stop_vote,
766 Vote run_vote,
767 uint32_t frame_idx
768)
Chris Lattner24943d22010-06-08 16:52:24 +0000769{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000770 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
771 addr_context,
772 first_insn,
773 stop_other_threads,
774 stop_vote,
775 run_vote,
776 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000777 QueueThreadPlan (thread_plan_sp, abort_other_plans);
778 return thread_plan_sp.get();
779}
780
781ThreadPlan *
782Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
783{
Jim Inghamb66cd072010-09-28 01:25:32 +0000784 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000785 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000786 // If that didn't come up with anything, try the ObjC runtime plugin:
787 if (thread_plan_sp.get() == NULL)
788 {
789 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
790 if (objc_runtime)
791 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
792 }
793
Chris Lattner24943d22010-06-08 16:52:24 +0000794 if (thread_plan_sp.get() == NULL)
795 {
796 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
797 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000798 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000799 }
800 QueueThreadPlan (thread_plan_sp, abort_other_plans);
801 return thread_plan_sp.get();
802}
803
804ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000805Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
806 Address& function,
807 lldb::addr_t arg,
808 bool stop_other_threads,
809 bool discard_on_error)
810{
811 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
812 QueueThreadPlan (thread_plan_sp, abort_other_plans);
813 return thread_plan_sp.get();
814}
815
816ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000817Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
818 Address &target_addr,
819 bool stop_other_threads)
820{
821 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
822 QueueThreadPlan (thread_plan_sp, abort_other_plans);
823 return thread_plan_sp.get();
824}
825
826ThreadPlan *
827Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000828 lldb::addr_t *address_list,
829 size_t num_addresses,
830 bool stop_other_threads,
831 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000832{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000833 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000834 QueueThreadPlan (thread_plan_sp, abort_other_plans);
835 return thread_plan_sp.get();
836
837}
838
839uint32_t
840Thread::GetIndexID () const
841{
842 return m_index_id;
843}
844
845void
846Thread::DumpThreadPlans (lldb_private::Stream *s) const
847{
848 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000849 int i;
850 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
851 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_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
856 s->IndentLess();
857 s->EOL();
858 }
859
Chris Lattner24943d22010-06-08 16:52:24 +0000860 stack_size = m_completed_plan_stack.size();
861 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +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_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
867 s->IndentLess();
868 s->EOL();
869 }
870
871 stack_size = m_discarded_plan_stack.size();
872 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000873 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000874 {
875 s->Printf ("Element %d: ", i);
876 s->IndentMore();
877 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
878 s->IndentLess();
879 s->EOL();
880 }
881
882}
883
884Target *
885Thread::CalculateTarget ()
886{
887 return m_process.CalculateTarget();
888}
889
890Process *
891Thread::CalculateProcess ()
892{
893 return &m_process;
894}
895
896Thread *
897Thread::CalculateThread ()
898{
899 return this;
900}
901
902StackFrame *
903Thread::CalculateStackFrame ()
904{
905 return NULL;
906}
907
908void
Greg Claytona830adb2010-10-04 01:05:56 +0000909Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000910{
Greg Claytona830adb2010-10-04 01:05:56 +0000911 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000912 exe_ctx.thread = this;
913 exe_ctx.frame = NULL;
914}
915
Greg Clayton782b9cc2010-08-25 00:35:26 +0000916
917StackFrameList &
918Thread::GetStackFrameList ()
919{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000920 if (!m_curr_frames_sp)
921 m_curr_frames_sp.reset (new StackFrameList (*this, m_prev_frames_sp, true));
922 return *m_curr_frames_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000923}
924
925
926
Jim Ingham71219082010-08-12 02:14:28 +0000927uint32_t
928Thread::GetStackFrameCount()
929{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000930 return GetStackFrameList().GetNumFrames();
931}
Greg Clayton33ed1702010-08-24 00:45:41 +0000932
Greg Clayton33ed1702010-08-24 00:45:41 +0000933
Greg Clayton782b9cc2010-08-25 00:35:26 +0000934void
935Thread::ClearStackFrames ()
936{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000937 if (m_curr_frames_sp && m_curr_frames_sp->GetNumFrames (false) > 1)
938 m_prev_frames_sp.swap (m_curr_frames_sp);
939 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +0000940}
941
942lldb::StackFrameSP
943Thread::GetStackFrameAtIndex (uint32_t idx)
944{
Greg Clayton72b71582010-09-02 21:44:10 +0000945 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000946}
947
Greg Claytonc12b6b42010-10-10 22:28:11 +0000948uint32_t
949Thread::GetSelectedFrameIndex ()
950{
951 return GetStackFrameList().GetSelectedFrameIndex();
952}
953
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000954lldb::StackFrameSP
955Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
956{
957 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
958}
959
Jim Ingham5c4b1602011-03-31 00:15:49 +0000960lldb::StackFrameSP
961Thread::GetFrameWithStackID(StackID &stack_id)
962{
963 return GetStackFrameList().GetFrameWithStackID (stack_id);
964}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000965
Greg Claytonc12b6b42010-10-10 22:28:11 +0000966
Chris Lattner24943d22010-06-08 16:52:24 +0000967lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000968Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000969{
Jim Inghamc8332952010-08-26 21:32:51 +0000970 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000971}
972
973uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000974Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000975{
Jim Inghamc8332952010-08-26 21:32:51 +0000976 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000977}
978
979void
Jim Inghamc8332952010-08-26 21:32:51 +0000980Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000981{
Jim Inghamc8332952010-08-26 21:32:51 +0000982 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000983}
984
985void
Greg Claytona830adb2010-10-04 01:05:56 +0000986Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000987{
Greg Claytona830adb2010-10-04 01:05:56 +0000988 ExecutionContext exe_ctx;
989 SymbolContext frame_sc;
990 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000991
Greg Claytona830adb2010-10-04 01:05:56 +0000992 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000993 {
Greg Claytona830adb2010-10-04 01:05:56 +0000994 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000995 if (frame_sp)
996 {
Greg Claytona830adb2010-10-04 01:05:56 +0000997 exe_ctx.frame = frame_sp.get();
998 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000999 }
1000 }
1001
Greg Claytona830adb2010-10-04 01:05:56 +00001002 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
1003 assert (thread_format);
1004 const char *end = NULL;
1005 Debugger::FormatPrompt (thread_format,
1006 exe_ctx.frame ? &frame_sc : NULL,
1007 &exe_ctx,
1008 NULL,
1009 strm,
1010 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001011}
1012
1013lldb::ThreadSP
1014Thread::GetSP ()
1015{
1016 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
1017}
Jim Ingham20594b12010-09-08 03:14:33 +00001018
Greg Clayton990de7b2010-11-18 23:32:35 +00001019
1020void
Caroline Tice2a456812011-03-10 22:14:10 +00001021Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001022{
Greg Clayton990de7b2010-11-18 23:32:35 +00001023 UserSettingsControllerSP &usc = GetSettingsController();
1024 usc.reset (new SettingsController);
1025 UserSettingsController::InitializeSettingsController (usc,
1026 SettingsController::global_settings_table,
1027 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001028
1029 // Now call SettingsInitialize() on each 'child' setting of Thread.
1030 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001031}
Jim Ingham20594b12010-09-08 03:14:33 +00001032
Greg Clayton990de7b2010-11-18 23:32:35 +00001033void
Caroline Tice2a456812011-03-10 22:14:10 +00001034Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001035{
Caroline Tice2a456812011-03-10 22:14:10 +00001036 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1037 // Currently there are none.
1038
1039 // Now terminate Thread Settings.
1040
Greg Clayton990de7b2010-11-18 23:32:35 +00001041 UserSettingsControllerSP &usc = GetSettingsController();
1042 UserSettingsController::FinalizeSettingsController (usc);
1043 usc.reset();
1044}
Jim Ingham20594b12010-09-08 03:14:33 +00001045
Greg Clayton990de7b2010-11-18 23:32:35 +00001046UserSettingsControllerSP &
1047Thread::GetSettingsController ()
1048{
1049 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001050 return g_settings_controller;
1051}
1052
Caroline Tice1ebef442010-09-27 00:30:10 +00001053void
1054Thread::UpdateInstanceName ()
1055{
1056 StreamString sstr;
1057 const char *name = GetName();
1058
1059 if (name && name[0] != '\0')
1060 sstr.Printf ("%s", name);
1061 else if ((GetIndexID() != 0) || (GetID() != 0))
1062 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1063
1064 if (sstr.GetSize() > 0)
1065 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1066}
1067
Jim Inghamccd584d2010-09-23 17:40:12 +00001068lldb::StackFrameSP
1069Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1070{
1071 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1072}
Caroline Tice7826c882010-10-26 03:11:13 +00001073
1074const char *
1075Thread::StopReasonAsCString (lldb::StopReason reason)
1076{
1077 switch (reason)
1078 {
1079 case eStopReasonInvalid: return "invalid";
1080 case eStopReasonNone: return "none";
1081 case eStopReasonTrace: return "trace";
1082 case eStopReasonBreakpoint: return "breakpoint";
1083 case eStopReasonWatchpoint: return "watchpoint";
1084 case eStopReasonSignal: return "signal";
1085 case eStopReasonException: return "exception";
1086 case eStopReasonPlanComplete: return "plan complete";
1087 }
1088
1089
1090 static char unknown_state_string[64];
1091 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1092 return unknown_state_string;
1093}
1094
1095const char *
1096Thread::RunModeAsCString (lldb::RunMode mode)
1097{
1098 switch (mode)
1099 {
1100 case eOnlyThisThread: return "only this thread";
1101 case eAllThreads: return "all threads";
1102 case eOnlyDuringStepping: return "only during stepping";
1103 }
1104
1105 static char unknown_state_string[64];
1106 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1107 return unknown_state_string;
1108}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001109
Greg Claytonabe0fed2011-04-18 08:33:37 +00001110size_t
1111Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1112{
1113 size_t num_frames_shown = 0;
1114 strm.Indent();
1115 strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' ');
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001116 if (GetProcess().GetTarget().GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001117 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001118 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1119 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1120 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1121 {
1122 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1123 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001124 }
1125
1126 DumpUsingSettingsFormat (strm, start_frame);
1127
1128 if (num_frames > 0)
1129 {
1130 strm.IndentMore();
1131
1132 const bool show_frame_info = true;
1133 const uint32_t source_lines_before = 3;
1134 const uint32_t source_lines_after = 3;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001135 strm.IndentMore ();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001136 num_frames_shown = GetStackFrameList ().GetStatus (strm,
1137 start_frame,
1138 num_frames,
1139 show_frame_info,
1140 num_frames_with_source,
1141 source_lines_before,
1142 source_lines_after);
1143 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001144 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001145 }
1146 return num_frames_shown;
1147}
1148
1149size_t
1150Thread::GetStackFrameStatus (Stream& strm,
1151 uint32_t first_frame,
1152 uint32_t num_frames,
1153 bool show_frame_info,
1154 uint32_t num_frames_with_source,
1155 uint32_t source_lines_before,
1156 uint32_t source_lines_after)
1157{
1158 return GetStackFrameList().GetStatus (strm,
1159 first_frame,
1160 num_frames,
1161 show_frame_info,
1162 num_frames_with_source,
1163 source_lines_before,
1164 source_lines_after);
1165}
1166
Peter Collingbournee426d852011-06-03 20:40:54 +00001167bool
1168Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1169{
1170 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1171 if (frame_sp)
1172 {
1173 checkpoint.SetStackID(frame_sp->GetStackID());
1174 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1175 }
1176 return false;
1177}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001178
Peter Collingbournee426d852011-06-03 20:40:54 +00001179bool
1180Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1181{
1182 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1183 if (frame_sp)
1184 {
1185 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1186
1187 // Clear out all stack frames as our world just changed.
1188 ClearStackFrames();
1189 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1190
1191 return ret;
1192 }
1193 return false;
1194}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001195
Greg Clayton990de7b2010-11-18 23:32:35 +00001196#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001197//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001198// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001199//--------------------------------------------------------------
1200
Greg Clayton990de7b2010-11-18 23:32:35 +00001201Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001202 UserSettingsController ("thread", Process::GetSettingsController())
1203{
1204 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1205 InstanceSettings::GetDefaultName().AsCString()));
1206}
1207
Greg Clayton990de7b2010-11-18 23:32:35 +00001208Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001209{
1210}
1211
1212lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001213Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001214{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001215 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1216 false,
1217 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001218 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1219 return new_settings_sp;
1220}
1221
1222#pragma mark "ThreadInstanceSettings"
1223//--------------------------------------------------------------
1224// class ThreadInstanceSettings
1225//--------------------------------------------------------------
1226
1227ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001228 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001229 m_avoid_regexp_ap (),
1230 m_trace_enabled (false)
1231{
1232 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1233 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1234 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1235 // This is true for CreateInstanceName() too.
1236
1237 if (GetInstanceName() == InstanceSettings::InvalidName())
1238 {
1239 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1240 m_owner.RegisterInstanceSettings (this);
1241 }
1242
1243 if (live_instance)
1244 {
1245 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1246 CopyInstanceSettings (pending_settings,false);
1247 //m_owner.RemovePendingSettings (m_instance_name);
1248 }
1249}
1250
1251ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001252 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001253 m_avoid_regexp_ap (),
1254 m_trace_enabled (rhs.m_trace_enabled)
1255{
1256 if (m_instance_name != InstanceSettings::GetDefaultName())
1257 {
1258 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1259 CopyInstanceSettings (pending_settings,false);
1260 m_owner.RemovePendingSettings (m_instance_name);
1261 }
1262 if (rhs.m_avoid_regexp_ap.get() != NULL)
1263 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1264}
1265
1266ThreadInstanceSettings::~ThreadInstanceSettings ()
1267{
1268}
1269
1270ThreadInstanceSettings&
1271ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1272{
1273 if (this != &rhs)
1274 {
1275 if (rhs.m_avoid_regexp_ap.get() != NULL)
1276 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1277 else
1278 m_avoid_regexp_ap.reset(NULL);
1279 }
1280 m_trace_enabled = rhs.m_trace_enabled;
1281 return *this;
1282}
1283
1284
1285void
1286ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1287 const char *index_value,
1288 const char *value,
1289 const ConstString &instance_name,
1290 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001291 VarSetOperationType op,
Jim Ingham745ac7a2010-11-11 19:26:09 +00001292 Error &err,
1293 bool pending)
1294{
1295 if (var_name == StepAvoidRegexpVarName())
1296 {
1297 std::string regexp_text;
1298 if (m_avoid_regexp_ap.get() != NULL)
1299 regexp_text.append (m_avoid_regexp_ap->GetText());
1300 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1301 if (regexp_text.empty())
1302 m_avoid_regexp_ap.reset();
1303 else
1304 {
1305 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1306
1307 }
1308 }
1309 else if (var_name == GetTraceThreadVarName())
1310 {
1311 bool success;
1312 bool result = Args::StringToBoolean(value, false, &success);
1313
1314 if (success)
1315 {
1316 m_trace_enabled = result;
1317 if (!pending)
1318 {
1319 Thread *myself = static_cast<Thread *> (this);
1320 myself->EnableTracer(m_trace_enabled, true);
1321 }
1322 }
1323 else
1324 {
1325 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1326 }
1327
1328 }
1329}
1330
1331void
1332ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1333 bool pending)
1334{
1335 if (new_settings.get() == NULL)
1336 return;
1337
1338 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1339 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1340 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1341 else
1342 m_avoid_regexp_ap.reset ();
1343}
1344
1345bool
1346ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1347 const ConstString &var_name,
1348 StringList &value,
1349 Error *err)
1350{
1351 if (var_name == StepAvoidRegexpVarName())
1352 {
1353 if (m_avoid_regexp_ap.get() != NULL)
1354 {
1355 std::string regexp_text("\"");
1356 regexp_text.append(m_avoid_regexp_ap->GetText());
1357 regexp_text.append ("\"");
1358 value.AppendString (regexp_text.c_str());
1359 }
1360
1361 }
1362 else if (var_name == GetTraceThreadVarName())
1363 {
1364 value.AppendString(m_trace_enabled ? "true" : "false");
1365 }
1366 else
1367 {
1368 if (err)
1369 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1370 return false;
1371 }
1372 return true;
1373}
1374
1375const ConstString
1376ThreadInstanceSettings::CreateInstanceName ()
1377{
1378 static int instance_count = 1;
1379 StreamString sstr;
1380
1381 sstr.Printf ("thread_%d", instance_count);
1382 ++instance_count;
1383
1384 const ConstString ret_val (sstr.GetData());
1385 return ret_val;
1386}
1387
1388const ConstString &
1389ThreadInstanceSettings::StepAvoidRegexpVarName ()
1390{
1391 static ConstString step_avoid_var_name ("step-avoid-regexp");
1392
1393 return step_avoid_var_name;
1394}
1395
1396const ConstString &
1397ThreadInstanceSettings::GetTraceThreadVarName ()
1398{
1399 static ConstString trace_thread_var_name ("trace-thread");
1400
1401 return trace_thread_var_name;
1402}
1403
1404//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001405// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001406//--------------------------------------------------
1407
1408SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001409Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001410{
1411 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1412 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1413};
1414
1415
1416SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001417Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001418{
1419 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1420 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1421 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1422 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1423};