blob: 267f1fc8999698b730901c1c74a3939516dfa530 [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 Claytonf40e3082010-08-26 02:28:22 +000054 m_curr_frames_ap (),
Chris Lattner24943d22010-06-08 16:52:24 +000055 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000056 m_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +000057 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000058 m_destroy_called (false),
59 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +000060
Chris Lattner24943d22010-06-08 16:52:24 +000061{
Greg Claytone005f2c2010-11-06 01:53:30 +000062 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000063 if (log)
64 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
65
66 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000067 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70
71Thread::~Thread()
72{
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (log)
75 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000076 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
77 assert (m_destroy_called);
78}
79
80void
81Thread::DestroyThread ()
82{
83 m_plan_stack.clear();
84 m_discarded_plan_stack.clear();
85 m_completed_plan_stack.clear();
86 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +000087}
88
Jim Ingham6297a3a2010-10-20 00:39:53 +000089lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +000090Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000091{
Jim Ingham6297a3a2010-10-20 00:39:53 +000092 ThreadPlanSP plan_sp (GetCompletedPlan());
93 if (plan_sp)
94 return StopInfo::CreateStopReasonWithPlan (plan_sp);
95 else
96 return GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
Jim Ingham15dcb7c2011-01-20 02:03:18 +000099void
100Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
101{
102 m_actual_stop_info_sp = stop_info_sp;
103 m_thread_stop_reason_stop_id = GetProcess().GetStopID();
104}
105
106void
107Thread::SetStopInfoToNothing()
108{
109 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
110 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
111 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
112}
113
Chris Lattner24943d22010-06-08 16:52:24 +0000114bool
115Thread::ThreadStoppedForAReason (void)
116{
Greg Clayton643ee732010-08-04 01:40:35 +0000117 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000118}
119
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000120bool
121Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
122{
123 if (!SaveFrameZeroState(saved_state.register_backup))
124 return false;
125
126 saved_state.stop_info_sp = GetStopInfo();
127 saved_state.orig_stop_id = GetProcess().GetStopID();
128
129 return true;
130}
131
132bool
133Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
134{
135 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000136 if (saved_state.stop_info_sp)
137 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000138 SetStopInfo(saved_state.stop_info_sp);
139 return true;
140}
141
Chris Lattner24943d22010-06-08 16:52:24 +0000142StateType
143Thread::GetState() const
144{
145 // If any other threads access this we will need a mutex for it
146 Mutex::Locker locker(m_state_mutex);
147 return m_state;
148}
149
150void
151Thread::SetState(StateType state)
152{
153 Mutex::Locker locker(m_state_mutex);
154 m_state = state;
155}
156
157void
158Thread::WillStop()
159{
160 ThreadPlan *current_plan = GetCurrentPlan();
161
162 // FIXME: I may decide to disallow threads with no plans. In which
163 // case this should go to an assert.
164
165 if (!current_plan)
166 return;
167
168 current_plan->WillStop();
169}
170
171void
172Thread::SetupForResume ()
173{
174 if (GetResumeState() != eStateSuspended)
175 {
176
177 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
178 // telling the current plan it will resume, since we might change what the current
179 // plan is.
180
181 lldb::addr_t pc = GetRegisterContext()->GetPC();
182 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
183 if (bp_site_sp && bp_site_sp->IsEnabled())
184 {
185 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
186 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000187
188 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000189
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000190 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
191 {
192 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
193 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000194 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000195 ThreadPlanSP step_bp_plan_sp;
196 step_bp_plan->SetPrivate (true);
197
Chris Lattner24943d22010-06-08 16:52:24 +0000198 if (GetCurrentPlan()->RunState() != eStateStepping)
199 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000200 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000201 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000202 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000203 QueueThreadPlan (step_bp_plan_sp, false);
204 }
205 }
206 }
207 }
208}
209
210bool
211Thread::WillResume (StateType resume_state)
212{
213 // At this point clear the completed plan stack.
214 m_completed_plan_stack.clear();
215 m_discarded_plan_stack.clear();
216
Greg Clayton643ee732010-08-04 01:40:35 +0000217 StopInfo *stop_info = GetPrivateStopReason().get();
218 if (stop_info)
219 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000220
221 // Tell all the plans that we are about to resume in case they need to clear any state.
222 // We distinguish between the plan on the top of the stack and the lower
223 // plans in case a plan needs to do any special business before it runs.
224
225 ThreadPlan *plan_ptr = GetCurrentPlan();
226 plan_ptr->WillResume(resume_state, true);
227
228 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
229 {
230 plan_ptr->WillResume (resume_state, false);
231 }
Greg Clayton643ee732010-08-04 01:40:35 +0000232
Greg Clayton643ee732010-08-04 01:40:35 +0000233 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000234 return true;
235}
236
237void
238Thread::DidResume ()
239{
240 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
241}
242
243bool
244Thread::ShouldStop (Event* event_ptr)
245{
246 ThreadPlan *current_plan = GetCurrentPlan();
247 bool should_stop = true;
248
Greg Claytone005f2c2010-11-06 01:53:30 +0000249 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000250 if (log)
251 {
252 StreamString s;
253 DumpThreadPlans(&s);
254 log->PutCString (s.GetData());
255 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000256
257 // The top most plan always gets to do the trace log...
258 current_plan->DoTraceLog ();
Chris Lattner24943d22010-06-08 16:52:24 +0000259
260 if (current_plan->PlanExplainsStop())
261 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000262 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000263
264 // We're starting from the base plan, so just let it decide;
265 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000266 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000267 should_stop = current_plan->ShouldStop (event_ptr);
268 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000269 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000270 }
271 else
272 {
273 // Otherwise, don't let the base plan override what the other plans say to do, since
274 // presumably if there were other plans they would know what to do...
275 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000276 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000277 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000278 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000279
280 should_stop = current_plan->ShouldStop(event_ptr);
281 if (log)
282 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
283 if (current_plan->MischiefManaged())
284 {
285 if (should_stop)
286 current_plan->WillStop();
287
288 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
289 // Otherwise, see if the plan's parent wants to stop.
290
291 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
292 {
293 PopPlan();
294 break;
295 }
296 else
297 {
298
299 PopPlan();
300
301 current_plan = GetCurrentPlan();
302 if (current_plan == NULL)
303 {
304 break;
305 }
306 }
307
Chris Lattner24943d22010-06-08 16:52:24 +0000308 }
309 else
310 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000311 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000312 }
Chris Lattner24943d22010-06-08 16:52:24 +0000313 }
314 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000315 if (over_ride_stop)
316 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000317 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000318 else if (current_plan->TracerExplainsStop())
319 {
320 return false;
321 }
Chris Lattner24943d22010-06-08 16:52:24 +0000322 else
323 {
324 // If the current plan doesn't explain the stop, then, find one that
325 // does and let it handle the situation.
326 ThreadPlan *plan_ptr = current_plan;
327 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
328 {
329 if (plan_ptr->PlanExplainsStop())
330 {
331 should_stop = plan_ptr->ShouldStop (event_ptr);
332 break;
333 }
334
335 }
336 }
337
338 return should_stop;
339}
340
341Vote
342Thread::ShouldReportStop (Event* event_ptr)
343{
344 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000345 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000346
347 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
348 {
349 if (log)
350 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 +0000351 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000352 }
Chris Lattner24943d22010-06-08 16:52:24 +0000353
354 if (m_completed_plan_stack.size() > 0)
355 {
356 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000357 if (log)
358 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 +0000359 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
360 }
361 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000362 {
363 if (log)
364 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000365 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000366 }
Chris Lattner24943d22010-06-08 16:52:24 +0000367}
368
369Vote
370Thread::ShouldReportRun (Event* event_ptr)
371{
372 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000373
Chris Lattner24943d22010-06-08 16:52:24 +0000374 if (thread_state == eStateSuspended
375 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000376 {
Chris Lattner24943d22010-06-08 16:52:24 +0000377 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000378 }
379
380 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000381 if (m_completed_plan_stack.size() > 0)
382 {
383 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000384 if (log)
385 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
386 GetIndexID(),
387 GetID(),
388 m_completed_plan_stack.back()->GetName());
389
Chris Lattner24943d22010-06-08 16:52:24 +0000390 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
391 }
392 else
Jim Inghamac959662011-01-24 06:34:17 +0000393 {
394 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 GetCurrentPlan()->GetName());
399
Chris Lattner24943d22010-06-08 16:52:24 +0000400 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000401 }
Chris Lattner24943d22010-06-08 16:52:24 +0000402}
403
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000404bool
405Thread::MatchesSpec (const ThreadSpec *spec)
406{
407 if (spec == NULL)
408 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000409
Jim Ingham649492b2010-06-18 01:00:58 +0000410 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000411}
412
Chris Lattner24943d22010-06-08 16:52:24 +0000413void
414Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
415{
416 if (thread_plan_sp)
417 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000418 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
419 if (!thread_plan_sp->GetThreadPlanTracer())
420 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000421 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000422
Chris Lattner24943d22010-06-08 16:52:24 +0000423 thread_plan_sp->DidPush();
424
Greg Claytone005f2c2010-11-06 01:53:30 +0000425 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000426 if (log)
427 {
428 StreamString s;
429 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000430 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000431 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000432 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000433 }
434 }
435}
436
437void
438Thread::PopPlan ()
439{
Greg Claytone005f2c2010-11-06 01:53:30 +0000440 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000441
Jim Ingham6297a3a2010-10-20 00:39:53 +0000442 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000443 return;
444 else
445 {
446 ThreadPlanSP &plan = m_plan_stack.back();
447 if (log)
448 {
Jim Ingham83e8b9d2010-11-10 18:17:03 +0000449 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000450 }
451 m_completed_plan_stack.push_back (plan);
452 plan->WillPop();
453 m_plan_stack.pop_back();
454 }
455}
456
457void
458Thread::DiscardPlan ()
459{
460 if (m_plan_stack.size() > 1)
461 {
462 ThreadPlanSP &plan = m_plan_stack.back();
463 m_discarded_plan_stack.push_back (plan);
464 plan->WillPop();
465 m_plan_stack.pop_back();
466 }
467}
468
469ThreadPlan *
470Thread::GetCurrentPlan ()
471{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000472 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000473 return NULL;
474 else
475 return m_plan_stack.back().get();
476}
477
478ThreadPlanSP
479Thread::GetCompletedPlan ()
480{
481 ThreadPlanSP empty_plan_sp;
482 if (!m_completed_plan_stack.empty())
483 {
484 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
485 {
486 ThreadPlanSP completed_plan_sp;
487 completed_plan_sp = m_completed_plan_stack[i];
488 if (!completed_plan_sp->GetPrivate ())
489 return completed_plan_sp;
490 }
491 }
492 return empty_plan_sp;
493}
494
495bool
496Thread::IsThreadPlanDone (ThreadPlan *plan)
497{
Chris Lattner24943d22010-06-08 16:52:24 +0000498 if (!m_completed_plan_stack.empty())
499 {
500 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
501 {
502 if (m_completed_plan_stack[i].get() == plan)
503 return true;
504 }
505 }
506 return false;
507}
508
509bool
510Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
511{
Chris Lattner24943d22010-06-08 16:52:24 +0000512 if (!m_discarded_plan_stack.empty())
513 {
514 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
515 {
516 if (m_discarded_plan_stack[i].get() == plan)
517 return true;
518 }
519 }
520 return false;
521}
522
523ThreadPlan *
524Thread::GetPreviousPlan (ThreadPlan *current_plan)
525{
526 if (current_plan == NULL)
527 return NULL;
528
529 int stack_size = m_completed_plan_stack.size();
530 for (int i = stack_size - 1; i > 0; i--)
531 {
532 if (current_plan == m_completed_plan_stack[i].get())
533 return m_completed_plan_stack[i-1].get();
534 }
535
536 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
537 {
Chris Lattner24943d22010-06-08 16:52:24 +0000538 if (m_plan_stack.size() > 0)
539 return m_plan_stack.back().get();
540 else
541 return NULL;
542 }
543
544 stack_size = m_plan_stack.size();
545 for (int i = stack_size - 1; i > 0; i--)
546 {
547 if (current_plan == m_plan_stack[i].get())
548 return m_plan_stack[i-1].get();
549 }
550 return NULL;
551}
552
553void
554Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
555{
556 if (abort_other_plans)
557 DiscardThreadPlans(true);
558
559 PushPlan (thread_plan_sp);
560}
561
Jim Ingham745ac7a2010-11-11 19:26:09 +0000562
563void
564Thread::EnableTracer (bool value, bool single_stepping)
565{
566 int stack_size = m_plan_stack.size();
567 for (int i = 0; i < stack_size; i++)
568 {
569 if (m_plan_stack[i]->GetThreadPlanTracer())
570 {
571 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
572 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
573 }
574 }
575}
576
577void
578Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
579{
580 int stack_size = m_plan_stack.size();
581 for (int i = 0; i < stack_size; i++)
582 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
583}
584
Chris Lattner24943d22010-06-08 16:52:24 +0000585void
Jim Inghamea9d4262010-11-05 19:25:48 +0000586Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
587{
Greg Claytone005f2c2010-11-06 01:53:30 +0000588 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000589 if (log)
590 {
591 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get());
592 }
593
594 int stack_size = m_plan_stack.size();
595
596 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
597 // stack, and if so discard up to and including it.
598
599 if (up_to_plan_sp.get() == NULL)
600 {
601 for (int i = stack_size - 1; i > 0; i--)
602 DiscardPlan();
603 }
604 else
605 {
606 bool found_it = false;
607 for (int i = stack_size - 1; i > 0; i--)
608 {
609 if (m_plan_stack[i] == up_to_plan_sp)
610 found_it = true;
611 }
612 if (found_it)
613 {
614 bool last_one = false;
615 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
616 {
617 if (GetCurrentPlan() == up_to_plan_sp.get())
618 last_one = true;
619 DiscardPlan();
620 }
621 }
622 }
623 return;
624}
625
626void
Chris Lattner24943d22010-06-08 16:52:24 +0000627Thread::DiscardThreadPlans(bool force)
628{
Greg Claytone005f2c2010-11-06 01:53:30 +0000629 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000630 if (log)
631 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000632 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000633 }
634
635 if (force)
636 {
637 int stack_size = m_plan_stack.size();
638 for (int i = stack_size - 1; i > 0; i--)
639 {
640 DiscardPlan();
641 }
642 return;
643 }
644
645 while (1)
646 {
647
648 int master_plan_idx;
649 bool discard;
650
651 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
652 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
653 {
654 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
655 {
656 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
657 break;
658 }
659 }
660
661 if (discard)
662 {
663 // First pop all the dependent plans:
664 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
665 {
666
667 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
668 // for the plan leaves it in a state that it is safe to pop the plan
669 // with no more notice?
670 DiscardPlan();
671 }
672
673 // Now discard the master plan itself.
674 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
675 // discard it's dependent plans, but not it...
676 if (master_plan_idx > 0)
677 {
678 DiscardPlan();
679 }
680 }
681 else
682 {
683 // If the master plan doesn't want to get discarded, then we're done.
684 break;
685 }
686
687 }
Chris Lattner24943d22010-06-08 16:52:24 +0000688}
689
690ThreadPlan *
691Thread::QueueFundamentalPlan (bool abort_other_plans)
692{
693 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
694 QueueThreadPlan (thread_plan_sp, abort_other_plans);
695 return thread_plan_sp.get();
696}
697
698ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000699Thread::QueueThreadPlanForStepSingleInstruction
700(
701 bool step_over,
702 bool abort_other_plans,
703 bool stop_other_threads
704)
Chris Lattner24943d22010-06-08 16:52:24 +0000705{
706 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
707 QueueThreadPlan (thread_plan_sp, abort_other_plans);
708 return thread_plan_sp.get();
709}
710
711ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000712Thread::QueueThreadPlanForStepRange
713(
714 bool abort_other_plans,
715 StepType type,
716 const AddressRange &range,
717 const SymbolContext &addr_context,
718 lldb::RunMode stop_other_threads,
719 bool avoid_code_without_debug_info
720)
Chris Lattner24943d22010-06-08 16:52:24 +0000721{
722 ThreadPlanSP thread_plan_sp;
723 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000724 {
725 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
726 if (avoid_code_without_debug_info)
727 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
728 else
729 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
730 thread_plan_sp.reset (plan);
731 }
Chris Lattner24943d22010-06-08 16:52:24 +0000732 else
733 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
734
735 QueueThreadPlan (thread_plan_sp, abort_other_plans);
736 return thread_plan_sp.get();
737}
738
739
740ThreadPlan *
741Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
742{
743 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
744 QueueThreadPlan (thread_plan_sp, abort_other_plans);
745 return thread_plan_sp.get();
746}
747
748ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000749Thread::QueueThreadPlanForStepOut
750(
751 bool abort_other_plans,
752 SymbolContext *addr_context,
753 bool first_insn,
754 bool stop_other_threads,
755 Vote stop_vote,
756 Vote run_vote,
757 uint32_t frame_idx
758)
Chris Lattner24943d22010-06-08 16:52:24 +0000759{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000760 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
761 addr_context,
762 first_insn,
763 stop_other_threads,
764 stop_vote,
765 run_vote,
766 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000767 QueueThreadPlan (thread_plan_sp, abort_other_plans);
768 return thread_plan_sp.get();
769}
770
771ThreadPlan *
772Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
773{
Jim Inghamb66cd072010-09-28 01:25:32 +0000774 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000775 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000776 // If that didn't come up with anything, try the ObjC runtime plugin:
777 if (thread_plan_sp.get() == NULL)
778 {
779 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
780 if (objc_runtime)
781 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
782 }
783
Chris Lattner24943d22010-06-08 16:52:24 +0000784 if (thread_plan_sp.get() == NULL)
785 {
786 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
787 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000788 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000789 }
790 QueueThreadPlan (thread_plan_sp, abort_other_plans);
791 return thread_plan_sp.get();
792}
793
794ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000795Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
796 Address& function,
797 lldb::addr_t arg,
798 bool stop_other_threads,
799 bool discard_on_error)
800{
801 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
802 QueueThreadPlan (thread_plan_sp, abort_other_plans);
803 return thread_plan_sp.get();
804}
805
806ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000807Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
808 Address &target_addr,
809 bool stop_other_threads)
810{
811 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
812 QueueThreadPlan (thread_plan_sp, abort_other_plans);
813 return thread_plan_sp.get();
814}
815
816ThreadPlan *
817Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000818 lldb::addr_t *address_list,
819 size_t num_addresses,
820 bool stop_other_threads,
821 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000822{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000823 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000824 QueueThreadPlan (thread_plan_sp, abort_other_plans);
825 return thread_plan_sp.get();
826
827}
828
829uint32_t
830Thread::GetIndexID () const
831{
832 return m_index_id;
833}
834
835void
836Thread::DumpThreadPlans (lldb_private::Stream *s) const
837{
838 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000839 int i;
840 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
841 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000842 {
843 s->Printf ("Element %d: ", i);
844 s->IndentMore();
845 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
846 s->IndentLess();
847 s->EOL();
848 }
849
Chris Lattner24943d22010-06-08 16:52:24 +0000850 stack_size = m_completed_plan_stack.size();
851 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000852 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000853 {
854 s->Printf ("Element %d: ", i);
855 s->IndentMore();
856 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
857 s->IndentLess();
858 s->EOL();
859 }
860
861 stack_size = m_discarded_plan_stack.size();
862 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000863 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000864 {
865 s->Printf ("Element %d: ", i);
866 s->IndentMore();
867 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
868 s->IndentLess();
869 s->EOL();
870 }
871
872}
873
874Target *
875Thread::CalculateTarget ()
876{
877 return m_process.CalculateTarget();
878}
879
880Process *
881Thread::CalculateProcess ()
882{
883 return &m_process;
884}
885
886Thread *
887Thread::CalculateThread ()
888{
889 return this;
890}
891
892StackFrame *
893Thread::CalculateStackFrame ()
894{
895 return NULL;
896}
897
898void
Greg Claytona830adb2010-10-04 01:05:56 +0000899Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000900{
Greg Claytona830adb2010-10-04 01:05:56 +0000901 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000902 exe_ctx.thread = this;
903 exe_ctx.frame = NULL;
904}
905
Greg Clayton782b9cc2010-08-25 00:35:26 +0000906
907StackFrameList &
908Thread::GetStackFrameList ()
909{
Greg Claytonf40e3082010-08-26 02:28:22 +0000910 if (m_curr_frames_ap.get() == NULL)
Greg Clayton5205f0b2010-09-03 17:10:42 +0000911 m_curr_frames_ap.reset (new StackFrameList (*this, m_prev_frames_sp, true));
Greg Claytonf40e3082010-08-26 02:28:22 +0000912 return *m_curr_frames_ap;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000913}
914
915
916
Jim Ingham71219082010-08-12 02:14:28 +0000917uint32_t
918Thread::GetStackFrameCount()
919{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000920 return GetStackFrameList().GetNumFrames();
921}
Greg Clayton33ed1702010-08-24 00:45:41 +0000922
Greg Clayton33ed1702010-08-24 00:45:41 +0000923
Greg Clayton782b9cc2010-08-25 00:35:26 +0000924void
925Thread::ClearStackFrames ()
926{
Greg Clayton5205f0b2010-09-03 17:10:42 +0000927 if (m_curr_frames_ap.get() && m_curr_frames_ap->GetNumFrames (false) > 1)
928 m_prev_frames_sp.reset (m_curr_frames_ap.release());
929 else
930 m_curr_frames_ap.release();
931
932// StackFrameList::Merge (m_curr_frames_ap, m_prev_frames_sp);
933// assert (m_curr_frames_ap.get() == NULL);
Jim Ingham71219082010-08-12 02:14:28 +0000934}
935
936lldb::StackFrameSP
937Thread::GetStackFrameAtIndex (uint32_t idx)
938{
Greg Clayton72b71582010-09-02 21:44:10 +0000939 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000940}
941
Greg Claytonc12b6b42010-10-10 22:28:11 +0000942uint32_t
943Thread::GetSelectedFrameIndex ()
944{
945 return GetStackFrameList().GetSelectedFrameIndex();
946}
947
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000948lldb::StackFrameSP
949Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
950{
951 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
952}
953
Jim Ingham5c4b1602011-03-31 00:15:49 +0000954lldb::StackFrameSP
955Thread::GetFrameWithStackID(StackID &stack_id)
956{
957 return GetStackFrameList().GetFrameWithStackID (stack_id);
958}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000959
Greg Claytonc12b6b42010-10-10 22:28:11 +0000960
Chris Lattner24943d22010-06-08 16:52:24 +0000961lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000962Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000963{
Jim Inghamc8332952010-08-26 21:32:51 +0000964 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000965}
966
967uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000968Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000969{
Jim Inghamc8332952010-08-26 21:32:51 +0000970 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000971}
972
973void
Jim Inghamc8332952010-08-26 21:32:51 +0000974Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000975{
Jim Inghamc8332952010-08-26 21:32:51 +0000976 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000977}
978
979void
Greg Claytona830adb2010-10-04 01:05:56 +0000980Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000981{
Greg Claytona830adb2010-10-04 01:05:56 +0000982 ExecutionContext exe_ctx;
983 SymbolContext frame_sc;
984 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000985
Greg Claytona830adb2010-10-04 01:05:56 +0000986 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000987 {
Greg Claytona830adb2010-10-04 01:05:56 +0000988 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000989 if (frame_sp)
990 {
Greg Claytona830adb2010-10-04 01:05:56 +0000991 exe_ctx.frame = frame_sp.get();
992 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000993 }
994 }
995
Greg Claytona830adb2010-10-04 01:05:56 +0000996 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
997 assert (thread_format);
998 const char *end = NULL;
999 Debugger::FormatPrompt (thread_format,
1000 exe_ctx.frame ? &frame_sc : NULL,
1001 &exe_ctx,
1002 NULL,
1003 strm,
1004 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001005}
1006
1007lldb::ThreadSP
1008Thread::GetSP ()
1009{
1010 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
1011}
Jim Ingham20594b12010-09-08 03:14:33 +00001012
Greg Clayton990de7b2010-11-18 23:32:35 +00001013
1014void
Caroline Tice2a456812011-03-10 22:14:10 +00001015Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001016{
Greg Clayton990de7b2010-11-18 23:32:35 +00001017 UserSettingsControllerSP &usc = GetSettingsController();
1018 usc.reset (new SettingsController);
1019 UserSettingsController::InitializeSettingsController (usc,
1020 SettingsController::global_settings_table,
1021 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001022
1023 // Now call SettingsInitialize() on each 'child' setting of Thread.
1024 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001025}
Jim Ingham20594b12010-09-08 03:14:33 +00001026
Greg Clayton990de7b2010-11-18 23:32:35 +00001027void
Caroline Tice2a456812011-03-10 22:14:10 +00001028Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001029{
Caroline Tice2a456812011-03-10 22:14:10 +00001030 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1031 // Currently there are none.
1032
1033 // Now terminate Thread Settings.
1034
Greg Clayton990de7b2010-11-18 23:32:35 +00001035 UserSettingsControllerSP &usc = GetSettingsController();
1036 UserSettingsController::FinalizeSettingsController (usc);
1037 usc.reset();
1038}
Jim Ingham20594b12010-09-08 03:14:33 +00001039
Greg Clayton990de7b2010-11-18 23:32:35 +00001040UserSettingsControllerSP &
1041Thread::GetSettingsController ()
1042{
1043 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001044 return g_settings_controller;
1045}
1046
Caroline Tice1ebef442010-09-27 00:30:10 +00001047void
1048Thread::UpdateInstanceName ()
1049{
1050 StreamString sstr;
1051 const char *name = GetName();
1052
1053 if (name && name[0] != '\0')
1054 sstr.Printf ("%s", name);
1055 else if ((GetIndexID() != 0) || (GetID() != 0))
1056 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1057
1058 if (sstr.GetSize() > 0)
1059 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1060}
1061
Jim Inghamccd584d2010-09-23 17:40:12 +00001062lldb::StackFrameSP
1063Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1064{
1065 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1066}
Caroline Tice7826c882010-10-26 03:11:13 +00001067
1068const char *
1069Thread::StopReasonAsCString (lldb::StopReason reason)
1070{
1071 switch (reason)
1072 {
1073 case eStopReasonInvalid: return "invalid";
1074 case eStopReasonNone: return "none";
1075 case eStopReasonTrace: return "trace";
1076 case eStopReasonBreakpoint: return "breakpoint";
1077 case eStopReasonWatchpoint: return "watchpoint";
1078 case eStopReasonSignal: return "signal";
1079 case eStopReasonException: return "exception";
1080 case eStopReasonPlanComplete: return "plan complete";
1081 }
1082
1083
1084 static char unknown_state_string[64];
1085 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1086 return unknown_state_string;
1087}
1088
1089const char *
1090Thread::RunModeAsCString (lldb::RunMode mode)
1091{
1092 switch (mode)
1093 {
1094 case eOnlyThisThread: return "only this thread";
1095 case eAllThreads: return "all threads";
1096 case eOnlyDuringStepping: return "only during stepping";
1097 }
1098
1099 static char unknown_state_string[64];
1100 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1101 return unknown_state_string;
1102}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001103
Greg Claytonabe0fed2011-04-18 08:33:37 +00001104size_t
1105Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1106{
1107 size_t num_frames_shown = 0;
1108 strm.Indent();
1109 strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' ');
1110
1111 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1112 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1113 if (frame_sc.line_entry.line != 0 &&
1114 frame_sc.line_entry.file &&
1115 GetProcess().GetTarget().GetDebugger().GetUseExternalEditor())
1116 {
1117 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1118 }
1119
1120 DumpUsingSettingsFormat (strm, start_frame);
1121
1122 if (num_frames > 0)
1123 {
1124 strm.IndentMore();
1125
1126 const bool show_frame_info = true;
1127 const uint32_t source_lines_before = 3;
1128 const uint32_t source_lines_after = 3;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001129 strm.IndentMore ();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001130 num_frames_shown = GetStackFrameList ().GetStatus (strm,
1131 start_frame,
1132 num_frames,
1133 show_frame_info,
1134 num_frames_with_source,
1135 source_lines_before,
1136 source_lines_after);
1137 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001138 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001139 }
1140 return num_frames_shown;
1141}
1142
1143size_t
1144Thread::GetStackFrameStatus (Stream& strm,
1145 uint32_t first_frame,
1146 uint32_t num_frames,
1147 bool show_frame_info,
1148 uint32_t num_frames_with_source,
1149 uint32_t source_lines_before,
1150 uint32_t source_lines_after)
1151{
1152 return GetStackFrameList().GetStatus (strm,
1153 first_frame,
1154 num_frames,
1155 show_frame_info,
1156 num_frames_with_source,
1157 source_lines_before,
1158 source_lines_after);
1159}
1160
Peter Collingbournee426d852011-06-03 20:40:54 +00001161bool
1162Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1163{
1164 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1165 if (frame_sp)
1166 {
1167 checkpoint.SetStackID(frame_sp->GetStackID());
1168 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1169 }
1170 return false;
1171}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001172
Peter Collingbournee426d852011-06-03 20:40:54 +00001173bool
1174Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1175{
1176 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1177 if (frame_sp)
1178 {
1179 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1180
1181 // Clear out all stack frames as our world just changed.
1182 ClearStackFrames();
1183 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1184
1185 return ret;
1186 }
1187 return false;
1188}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001189
Greg Clayton990de7b2010-11-18 23:32:35 +00001190#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001191//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001192// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001193//--------------------------------------------------------------
1194
Greg Clayton990de7b2010-11-18 23:32:35 +00001195Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001196 UserSettingsController ("thread", Process::GetSettingsController())
1197{
1198 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1199 InstanceSettings::GetDefaultName().AsCString()));
1200}
1201
Greg Clayton990de7b2010-11-18 23:32:35 +00001202Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001203{
1204}
1205
1206lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001207Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001208{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001209 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1210 false,
1211 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001212 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1213 return new_settings_sp;
1214}
1215
1216#pragma mark "ThreadInstanceSettings"
1217//--------------------------------------------------------------
1218// class ThreadInstanceSettings
1219//--------------------------------------------------------------
1220
1221ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001222 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001223 m_avoid_regexp_ap (),
1224 m_trace_enabled (false)
1225{
1226 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1227 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1228 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1229 // This is true for CreateInstanceName() too.
1230
1231 if (GetInstanceName() == InstanceSettings::InvalidName())
1232 {
1233 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1234 m_owner.RegisterInstanceSettings (this);
1235 }
1236
1237 if (live_instance)
1238 {
1239 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1240 CopyInstanceSettings (pending_settings,false);
1241 //m_owner.RemovePendingSettings (m_instance_name);
1242 }
1243}
1244
1245ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001246 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001247 m_avoid_regexp_ap (),
1248 m_trace_enabled (rhs.m_trace_enabled)
1249{
1250 if (m_instance_name != InstanceSettings::GetDefaultName())
1251 {
1252 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1253 CopyInstanceSettings (pending_settings,false);
1254 m_owner.RemovePendingSettings (m_instance_name);
1255 }
1256 if (rhs.m_avoid_regexp_ap.get() != NULL)
1257 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1258}
1259
1260ThreadInstanceSettings::~ThreadInstanceSettings ()
1261{
1262}
1263
1264ThreadInstanceSettings&
1265ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1266{
1267 if (this != &rhs)
1268 {
1269 if (rhs.m_avoid_regexp_ap.get() != NULL)
1270 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1271 else
1272 m_avoid_regexp_ap.reset(NULL);
1273 }
1274 m_trace_enabled = rhs.m_trace_enabled;
1275 return *this;
1276}
1277
1278
1279void
1280ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1281 const char *index_value,
1282 const char *value,
1283 const ConstString &instance_name,
1284 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001285 VarSetOperationType op,
Jim Ingham745ac7a2010-11-11 19:26:09 +00001286 Error &err,
1287 bool pending)
1288{
1289 if (var_name == StepAvoidRegexpVarName())
1290 {
1291 std::string regexp_text;
1292 if (m_avoid_regexp_ap.get() != NULL)
1293 regexp_text.append (m_avoid_regexp_ap->GetText());
1294 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1295 if (regexp_text.empty())
1296 m_avoid_regexp_ap.reset();
1297 else
1298 {
1299 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1300
1301 }
1302 }
1303 else if (var_name == GetTraceThreadVarName())
1304 {
1305 bool success;
1306 bool result = Args::StringToBoolean(value, false, &success);
1307
1308 if (success)
1309 {
1310 m_trace_enabled = result;
1311 if (!pending)
1312 {
1313 Thread *myself = static_cast<Thread *> (this);
1314 myself->EnableTracer(m_trace_enabled, true);
1315 }
1316 }
1317 else
1318 {
1319 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1320 }
1321
1322 }
1323}
1324
1325void
1326ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1327 bool pending)
1328{
1329 if (new_settings.get() == NULL)
1330 return;
1331
1332 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1333 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1334 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1335 else
1336 m_avoid_regexp_ap.reset ();
1337}
1338
1339bool
1340ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1341 const ConstString &var_name,
1342 StringList &value,
1343 Error *err)
1344{
1345 if (var_name == StepAvoidRegexpVarName())
1346 {
1347 if (m_avoid_regexp_ap.get() != NULL)
1348 {
1349 std::string regexp_text("\"");
1350 regexp_text.append(m_avoid_regexp_ap->GetText());
1351 regexp_text.append ("\"");
1352 value.AppendString (regexp_text.c_str());
1353 }
1354
1355 }
1356 else if (var_name == GetTraceThreadVarName())
1357 {
1358 value.AppendString(m_trace_enabled ? "true" : "false");
1359 }
1360 else
1361 {
1362 if (err)
1363 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1364 return false;
1365 }
1366 return true;
1367}
1368
1369const ConstString
1370ThreadInstanceSettings::CreateInstanceName ()
1371{
1372 static int instance_count = 1;
1373 StreamString sstr;
1374
1375 sstr.Printf ("thread_%d", instance_count);
1376 ++instance_count;
1377
1378 const ConstString ret_val (sstr.GetData());
1379 return ret_val;
1380}
1381
1382const ConstString &
1383ThreadInstanceSettings::StepAvoidRegexpVarName ()
1384{
1385 static ConstString step_avoid_var_name ("step-avoid-regexp");
1386
1387 return step_avoid_var_name;
1388}
1389
1390const ConstString &
1391ThreadInstanceSettings::GetTraceThreadVarName ()
1392{
1393 static ConstString trace_thread_var_name ("trace-thread");
1394
1395 return trace_thread_var_name;
1396}
1397
1398//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001399// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001400//--------------------------------------------------
1401
1402SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001403Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001404{
1405 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1406 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1407};
1408
1409
1410SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001411Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001412{
1413 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1414 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1415 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1416 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1417};