blob: 13d21936fc46b7b165f982bb8e392ccd1598e77e [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"
Greg Clayton37f962e2011-08-22 02:49:39 +000039#include "Plugins/Process/Utility/UnwindLLDB.h"
40#include "UnwindMacOSXFrameBackchain.h"
41
Chris Lattner24943d22010-06-08 16:52:24 +000042
43using namespace lldb;
44using namespace lldb_private;
45
46Thread::Thread (Process &process, lldb::tid_t tid) :
47 UserID (tid),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000048 ThreadInstanceSettings (*GetSettingsController()),
Benjamin Kramer36a08102010-07-16 12:32:33 +000049 m_process (process),
Greg Clayton643ee732010-08-04 01:40:35 +000050 m_actual_stop_info_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000051 m_index_id (process.GetNextThreadIndexID ()),
52 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000053 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +000054 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000055 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +000056 m_completed_plan_stack(),
Greg Claytonc51ffbf2011-08-12 21:40:01 +000057 m_curr_frames_sp (),
58 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000059 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000060 m_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +000061 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000062 m_destroy_called (false),
63 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +000064
Chris Lattner24943d22010-06-08 16:52:24 +000065{
Greg Claytone005f2c2010-11-06 01:53:30 +000066 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000067 if (log)
68 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
69
70 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000071 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000072}
73
74
75Thread::~Thread()
76{
Greg Claytone005f2c2010-11-06 01:53:30 +000077 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000078 if (log)
79 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000080 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
81 assert (m_destroy_called);
82}
83
84void
85Thread::DestroyThread ()
86{
87 m_plan_stack.clear();
88 m_discarded_plan_stack.clear();
89 m_completed_plan_stack.clear();
90 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +000091}
92
Jim Ingham6297a3a2010-10-20 00:39:53 +000093lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +000094Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000095{
Jim Ingham6297a3a2010-10-20 00:39:53 +000096 ThreadPlanSP plan_sp (GetCompletedPlan());
97 if (plan_sp)
98 return StopInfo::CreateStopReasonWithPlan (plan_sp);
99 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000100 {
101 if (m_actual_stop_info_sp
102 && m_actual_stop_info_sp->IsValid()
103 && m_thread_stop_reason_stop_id == m_process.GetStopID())
104 return m_actual_stop_info_sp;
105 else
106 return GetPrivateStopReason ();
107 }
Chris Lattner24943d22010-06-08 16:52:24 +0000108}
109
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000110void
111Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
112{
113 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000114 if (m_actual_stop_info_sp)
115 m_actual_stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000116 m_thread_stop_reason_stop_id = GetProcess().GetStopID();
117}
118
119void
120Thread::SetStopInfoToNothing()
121{
122 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
123 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
124 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
125}
126
Chris Lattner24943d22010-06-08 16:52:24 +0000127bool
128Thread::ThreadStoppedForAReason (void)
129{
Greg Clayton643ee732010-08-04 01:40:35 +0000130 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000131}
132
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000133bool
134Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
135{
136 if (!SaveFrameZeroState(saved_state.register_backup))
137 return false;
138
139 saved_state.stop_info_sp = GetStopInfo();
140 saved_state.orig_stop_id = GetProcess().GetStopID();
141
142 return true;
143}
144
145bool
146Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
147{
148 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000149 if (saved_state.stop_info_sp)
150 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000151 SetStopInfo(saved_state.stop_info_sp);
152 return true;
153}
154
Chris Lattner24943d22010-06-08 16:52:24 +0000155StateType
156Thread::GetState() const
157{
158 // If any other threads access this we will need a mutex for it
159 Mutex::Locker locker(m_state_mutex);
160 return m_state;
161}
162
163void
164Thread::SetState(StateType state)
165{
166 Mutex::Locker locker(m_state_mutex);
167 m_state = state;
168}
169
170void
171Thread::WillStop()
172{
173 ThreadPlan *current_plan = GetCurrentPlan();
174
175 // FIXME: I may decide to disallow threads with no plans. In which
176 // case this should go to an assert.
177
178 if (!current_plan)
179 return;
180
181 current_plan->WillStop();
182}
183
184void
185Thread::SetupForResume ()
186{
187 if (GetResumeState() != eStateSuspended)
188 {
189
190 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
191 // telling the current plan it will resume, since we might change what the current
192 // plan is.
193
194 lldb::addr_t pc = GetRegisterContext()->GetPC();
195 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
196 if (bp_site_sp && bp_site_sp->IsEnabled())
197 {
198 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
199 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000200
201 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000202
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000203 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
204 {
205 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
206 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000207 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000208 ThreadPlanSP step_bp_plan_sp;
209 step_bp_plan->SetPrivate (true);
210
Chris Lattner24943d22010-06-08 16:52:24 +0000211 if (GetCurrentPlan()->RunState() != eStateStepping)
212 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000213 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000214 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000215 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000216 QueueThreadPlan (step_bp_plan_sp, false);
217 }
218 }
219 }
220 }
221}
222
223bool
224Thread::WillResume (StateType resume_state)
225{
226 // At this point clear the completed plan stack.
227 m_completed_plan_stack.clear();
228 m_discarded_plan_stack.clear();
229
Greg Clayton643ee732010-08-04 01:40:35 +0000230 StopInfo *stop_info = GetPrivateStopReason().get();
231 if (stop_info)
232 stop_info->WillResume (resume_state);
Chris Lattner24943d22010-06-08 16:52:24 +0000233
234 // Tell all the plans that we are about to resume in case they need to clear any state.
235 // We distinguish between the plan on the top of the stack and the lower
236 // plans in case a plan needs to do any special business before it runs.
237
238 ThreadPlan *plan_ptr = GetCurrentPlan();
239 plan_ptr->WillResume(resume_state, true);
240
241 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
242 {
243 plan_ptr->WillResume (resume_state, false);
244 }
Greg Clayton643ee732010-08-04 01:40:35 +0000245
Greg Clayton643ee732010-08-04 01:40:35 +0000246 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000247 return true;
248}
249
250void
251Thread::DidResume ()
252{
253 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
254}
255
256bool
257Thread::ShouldStop (Event* event_ptr)
258{
259 ThreadPlan *current_plan = GetCurrentPlan();
260 bool should_stop = true;
261
Greg Claytone005f2c2010-11-06 01:53:30 +0000262 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000263 if (log)
264 {
265 StreamString s;
266 DumpThreadPlans(&s);
267 log->PutCString (s.GetData());
268 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000269
270 // The top most plan always gets to do the trace log...
271 current_plan->DoTraceLog ();
Chris Lattner24943d22010-06-08 16:52:24 +0000272
273 if (current_plan->PlanExplainsStop())
274 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000275 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000276
277 // We're starting from the base plan, so just let it decide;
278 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000279 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000280 should_stop = current_plan->ShouldStop (event_ptr);
281 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000282 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000283 }
284 else
285 {
286 // Otherwise, don't let the base plan override what the other plans say to do, since
287 // presumably if there were other plans they would know what to do...
288 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000289 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000290 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000291 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000292
293 should_stop = current_plan->ShouldStop(event_ptr);
294 if (log)
295 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
296 if (current_plan->MischiefManaged())
297 {
298 if (should_stop)
299 current_plan->WillStop();
300
301 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
302 // Otherwise, see if the plan's parent wants to stop.
303
304 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
305 {
306 PopPlan();
307 break;
308 }
309 else
310 {
311
312 PopPlan();
313
314 current_plan = GetCurrentPlan();
315 if (current_plan == NULL)
316 {
317 break;
318 }
319 }
320
Chris Lattner24943d22010-06-08 16:52:24 +0000321 }
322 else
323 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000324 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000325 }
Chris Lattner24943d22010-06-08 16:52:24 +0000326 }
327 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000328 if (over_ride_stop)
329 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000330 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000331 else if (current_plan->TracerExplainsStop())
332 {
333 return false;
334 }
Chris Lattner24943d22010-06-08 16:52:24 +0000335 else
336 {
337 // If the current plan doesn't explain the stop, then, find one that
338 // does and let it handle the situation.
339 ThreadPlan *plan_ptr = current_plan;
340 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
341 {
342 if (plan_ptr->PlanExplainsStop())
343 {
344 should_stop = plan_ptr->ShouldStop (event_ptr);
345 break;
346 }
347
348 }
349 }
350
351 return should_stop;
352}
353
354Vote
355Thread::ShouldReportStop (Event* event_ptr)
356{
357 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000358 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000359
360 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
361 {
362 if (log)
363 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 +0000364 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000365 }
Chris Lattner24943d22010-06-08 16:52:24 +0000366
367 if (m_completed_plan_stack.size() > 0)
368 {
369 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000370 if (log)
371 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 +0000372 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
373 }
374 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000375 {
376 if (log)
377 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4x: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000378 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000379 }
Chris Lattner24943d22010-06-08 16:52:24 +0000380}
381
382Vote
383Thread::ShouldReportRun (Event* event_ptr)
384{
385 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000386
Chris Lattner24943d22010-06-08 16:52:24 +0000387 if (thread_state == eStateSuspended
388 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000389 {
Chris Lattner24943d22010-06-08 16:52:24 +0000390 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000391 }
392
393 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000394 if (m_completed_plan_stack.size() > 0)
395 {
396 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000397 if (log)
398 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
399 GetIndexID(),
400 GetID(),
401 m_completed_plan_stack.back()->GetName());
402
Chris Lattner24943d22010-06-08 16:52:24 +0000403 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
404 }
405 else
Jim Inghamac959662011-01-24 06:34:17 +0000406 {
407 if (log)
408 log->Printf ("Current Plan for thread %d (0x%4.4x): %s being asked whether we should report run.",
409 GetIndexID(),
410 GetID(),
411 GetCurrentPlan()->GetName());
412
Chris Lattner24943d22010-06-08 16:52:24 +0000413 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000414 }
Chris Lattner24943d22010-06-08 16:52:24 +0000415}
416
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000417bool
418Thread::MatchesSpec (const ThreadSpec *spec)
419{
420 if (spec == NULL)
421 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000422
Jim Ingham649492b2010-06-18 01:00:58 +0000423 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000424}
425
Chris Lattner24943d22010-06-08 16:52:24 +0000426void
427Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
428{
429 if (thread_plan_sp)
430 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000431 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
432 if (!thread_plan_sp->GetThreadPlanTracer())
433 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000434 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000435
Chris Lattner24943d22010-06-08 16:52:24 +0000436 thread_plan_sp->DidPush();
437
Greg Claytone005f2c2010-11-06 01:53:30 +0000438 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000439 if (log)
440 {
441 StreamString s;
442 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Ingham6297a3a2010-10-20 00:39:53 +0000443 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4x.",
Chris Lattner24943d22010-06-08 16:52:24 +0000444 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000445 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000446 }
447 }
448}
449
450void
451Thread::PopPlan ()
452{
Greg Claytone005f2c2010-11-06 01:53:30 +0000453 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000454
Jim Ingham6297a3a2010-10-20 00:39:53 +0000455 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000456 return;
457 else
458 {
459 ThreadPlanSP &plan = m_plan_stack.back();
460 if (log)
461 {
Jim Ingham83e8b9d2010-11-10 18:17:03 +0000462 log->Printf("Popping plan: \"%s\", tid = 0x%4.4x.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000463 }
464 m_completed_plan_stack.push_back (plan);
465 plan->WillPop();
466 m_plan_stack.pop_back();
467 }
468}
469
470void
471Thread::DiscardPlan ()
472{
473 if (m_plan_stack.size() > 1)
474 {
475 ThreadPlanSP &plan = m_plan_stack.back();
476 m_discarded_plan_stack.push_back (plan);
477 plan->WillPop();
478 m_plan_stack.pop_back();
479 }
480}
481
482ThreadPlan *
483Thread::GetCurrentPlan ()
484{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000485 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000486 return NULL;
487 else
488 return m_plan_stack.back().get();
489}
490
491ThreadPlanSP
492Thread::GetCompletedPlan ()
493{
494 ThreadPlanSP empty_plan_sp;
495 if (!m_completed_plan_stack.empty())
496 {
497 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
498 {
499 ThreadPlanSP completed_plan_sp;
500 completed_plan_sp = m_completed_plan_stack[i];
501 if (!completed_plan_sp->GetPrivate ())
502 return completed_plan_sp;
503 }
504 }
505 return empty_plan_sp;
506}
507
508bool
509Thread::IsThreadPlanDone (ThreadPlan *plan)
510{
Chris Lattner24943d22010-06-08 16:52:24 +0000511 if (!m_completed_plan_stack.empty())
512 {
513 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
514 {
515 if (m_completed_plan_stack[i].get() == plan)
516 return true;
517 }
518 }
519 return false;
520}
521
522bool
523Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
524{
Chris Lattner24943d22010-06-08 16:52:24 +0000525 if (!m_discarded_plan_stack.empty())
526 {
527 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
528 {
529 if (m_discarded_plan_stack[i].get() == plan)
530 return true;
531 }
532 }
533 return false;
534}
535
536ThreadPlan *
537Thread::GetPreviousPlan (ThreadPlan *current_plan)
538{
539 if (current_plan == NULL)
540 return NULL;
541
542 int stack_size = m_completed_plan_stack.size();
543 for (int i = stack_size - 1; i > 0; i--)
544 {
545 if (current_plan == m_completed_plan_stack[i].get())
546 return m_completed_plan_stack[i-1].get();
547 }
548
549 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
550 {
Chris Lattner24943d22010-06-08 16:52:24 +0000551 if (m_plan_stack.size() > 0)
552 return m_plan_stack.back().get();
553 else
554 return NULL;
555 }
556
557 stack_size = m_plan_stack.size();
558 for (int i = stack_size - 1; i > 0; i--)
559 {
560 if (current_plan == m_plan_stack[i].get())
561 return m_plan_stack[i-1].get();
562 }
563 return NULL;
564}
565
566void
567Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
568{
569 if (abort_other_plans)
570 DiscardThreadPlans(true);
571
572 PushPlan (thread_plan_sp);
573}
574
Jim Ingham745ac7a2010-11-11 19:26:09 +0000575
576void
577Thread::EnableTracer (bool value, bool single_stepping)
578{
579 int stack_size = m_plan_stack.size();
580 for (int i = 0; i < stack_size; i++)
581 {
582 if (m_plan_stack[i]->GetThreadPlanTracer())
583 {
584 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
585 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
586 }
587 }
588}
589
590void
591Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
592{
593 int stack_size = m_plan_stack.size();
594 for (int i = 0; i < stack_size; i++)
595 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
596}
597
Chris Lattner24943d22010-06-08 16:52:24 +0000598void
Jim Inghamea9d4262010-11-05 19:25:48 +0000599Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
600{
Greg Claytone005f2c2010-11-06 01:53:30 +0000601 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000602 if (log)
603 {
604 log->Printf("Discarding thread plans for thread tid = 0x%4.4x, up to %p", GetID(), up_to_plan_sp.get());
605 }
606
607 int stack_size = m_plan_stack.size();
608
609 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
610 // stack, and if so discard up to and including it.
611
612 if (up_to_plan_sp.get() == NULL)
613 {
614 for (int i = stack_size - 1; i > 0; i--)
615 DiscardPlan();
616 }
617 else
618 {
619 bool found_it = false;
620 for (int i = stack_size - 1; i > 0; i--)
621 {
622 if (m_plan_stack[i] == up_to_plan_sp)
623 found_it = true;
624 }
625 if (found_it)
626 {
627 bool last_one = false;
628 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
629 {
630 if (GetCurrentPlan() == up_to_plan_sp.get())
631 last_one = true;
632 DiscardPlan();
633 }
634 }
635 }
636 return;
637}
638
639void
Chris Lattner24943d22010-06-08 16:52:24 +0000640Thread::DiscardThreadPlans(bool force)
641{
Greg Claytone005f2c2010-11-06 01:53:30 +0000642 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000643 if (log)
644 {
Greg Claytonf04d6612010-09-03 22:45:01 +0000645 log->Printf("Discarding thread plans for thread (tid = 0x%4.4x, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000646 }
647
648 if (force)
649 {
650 int stack_size = m_plan_stack.size();
651 for (int i = stack_size - 1; i > 0; i--)
652 {
653 DiscardPlan();
654 }
655 return;
656 }
657
658 while (1)
659 {
660
661 int master_plan_idx;
662 bool discard;
663
664 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
665 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
666 {
667 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
668 {
669 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
670 break;
671 }
672 }
673
674 if (discard)
675 {
676 // First pop all the dependent plans:
677 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
678 {
679
680 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
681 // for the plan leaves it in a state that it is safe to pop the plan
682 // with no more notice?
683 DiscardPlan();
684 }
685
686 // Now discard the master plan itself.
687 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
688 // discard it's dependent plans, but not it...
689 if (master_plan_idx > 0)
690 {
691 DiscardPlan();
692 }
693 }
694 else
695 {
696 // If the master plan doesn't want to get discarded, then we're done.
697 break;
698 }
699
700 }
Chris Lattner24943d22010-06-08 16:52:24 +0000701}
702
703ThreadPlan *
704Thread::QueueFundamentalPlan (bool abort_other_plans)
705{
706 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
707 QueueThreadPlan (thread_plan_sp, abort_other_plans);
708 return thread_plan_sp.get();
709}
710
711ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000712Thread::QueueThreadPlanForStepSingleInstruction
713(
714 bool step_over,
715 bool abort_other_plans,
716 bool stop_other_threads
717)
Chris Lattner24943d22010-06-08 16:52:24 +0000718{
719 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
720 QueueThreadPlan (thread_plan_sp, abort_other_plans);
721 return thread_plan_sp.get();
722}
723
724ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000725Thread::QueueThreadPlanForStepRange
726(
727 bool abort_other_plans,
728 StepType type,
729 const AddressRange &range,
730 const SymbolContext &addr_context,
731 lldb::RunMode stop_other_threads,
732 bool avoid_code_without_debug_info
733)
Chris Lattner24943d22010-06-08 16:52:24 +0000734{
735 ThreadPlanSP thread_plan_sp;
736 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000737 {
738 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
739 if (avoid_code_without_debug_info)
740 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
741 else
742 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
743 thread_plan_sp.reset (plan);
744 }
Chris Lattner24943d22010-06-08 16:52:24 +0000745 else
746 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
747
748 QueueThreadPlan (thread_plan_sp, abort_other_plans);
749 return thread_plan_sp.get();
750}
751
752
753ThreadPlan *
754Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
755{
756 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
757 QueueThreadPlan (thread_plan_sp, abort_other_plans);
758 return thread_plan_sp.get();
759}
760
761ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000762Thread::QueueThreadPlanForStepOut
763(
764 bool abort_other_plans,
765 SymbolContext *addr_context,
766 bool first_insn,
767 bool stop_other_threads,
768 Vote stop_vote,
769 Vote run_vote,
770 uint32_t frame_idx
771)
Chris Lattner24943d22010-06-08 16:52:24 +0000772{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000773 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
774 addr_context,
775 first_insn,
776 stop_other_threads,
777 stop_vote,
778 run_vote,
779 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000780 QueueThreadPlan (thread_plan_sp, abort_other_plans);
781 return thread_plan_sp.get();
782}
783
784ThreadPlan *
785Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
786{
Jim Inghamb66cd072010-09-28 01:25:32 +0000787 // Try the dynamic loader first:
Chris Lattner24943d22010-06-08 16:52:24 +0000788 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
Jim Inghamb66cd072010-09-28 01:25:32 +0000789 // If that didn't come up with anything, try the ObjC runtime plugin:
790 if (thread_plan_sp.get() == NULL)
791 {
792 ObjCLanguageRuntime *objc_runtime = GetProcess().GetObjCLanguageRuntime();
793 if (objc_runtime)
794 thread_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (*this, stop_other_threads);
795 }
796
Chris Lattner24943d22010-06-08 16:52:24 +0000797 if (thread_plan_sp.get() == NULL)
798 {
799 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
800 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +0000801 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000802 }
803 QueueThreadPlan (thread_plan_sp, abort_other_plans);
804 return thread_plan_sp.get();
805}
806
807ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000808Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
809 Address& function,
810 lldb::addr_t arg,
811 bool stop_other_threads,
812 bool discard_on_error)
813{
814 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
815 QueueThreadPlan (thread_plan_sp, abort_other_plans);
816 return thread_plan_sp.get();
817}
818
819ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000820Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
821 Address &target_addr,
822 bool stop_other_threads)
823{
824 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
825 QueueThreadPlan (thread_plan_sp, abort_other_plans);
826 return thread_plan_sp.get();
827}
828
829ThreadPlan *
830Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000831 lldb::addr_t *address_list,
832 size_t num_addresses,
833 bool stop_other_threads,
834 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000835{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000836 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000837 QueueThreadPlan (thread_plan_sp, abort_other_plans);
838 return thread_plan_sp.get();
839
840}
841
842uint32_t
843Thread::GetIndexID () const
844{
845 return m_index_id;
846}
847
848void
849Thread::DumpThreadPlans (lldb_private::Stream *s) const
850{
851 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000852 int i;
853 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
854 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000855 {
856 s->Printf ("Element %d: ", i);
857 s->IndentMore();
858 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
859 s->IndentLess();
860 s->EOL();
861 }
862
Chris Lattner24943d22010-06-08 16:52:24 +0000863 stack_size = m_completed_plan_stack.size();
864 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000865 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000866 {
867 s->Printf ("Element %d: ", i);
868 s->IndentMore();
869 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
870 s->IndentLess();
871 s->EOL();
872 }
873
874 stack_size = m_discarded_plan_stack.size();
875 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000876 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000877 {
878 s->Printf ("Element %d: ", i);
879 s->IndentMore();
880 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
881 s->IndentLess();
882 s->EOL();
883 }
884
885}
886
887Target *
888Thread::CalculateTarget ()
889{
890 return m_process.CalculateTarget();
891}
892
893Process *
894Thread::CalculateProcess ()
895{
896 return &m_process;
897}
898
899Thread *
900Thread::CalculateThread ()
901{
902 return this;
903}
904
905StackFrame *
906Thread::CalculateStackFrame ()
907{
908 return NULL;
909}
910
911void
Greg Claytona830adb2010-10-04 01:05:56 +0000912Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000913{
Greg Claytona830adb2010-10-04 01:05:56 +0000914 m_process.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000915 exe_ctx.thread = this;
916 exe_ctx.frame = NULL;
917}
918
Greg Clayton782b9cc2010-08-25 00:35:26 +0000919
920StackFrameList &
921Thread::GetStackFrameList ()
922{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000923 if (!m_curr_frames_sp)
924 m_curr_frames_sp.reset (new StackFrameList (*this, m_prev_frames_sp, true));
925 return *m_curr_frames_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000926}
927
928
929
Jim Ingham71219082010-08-12 02:14:28 +0000930uint32_t
931Thread::GetStackFrameCount()
932{
Greg Clayton782b9cc2010-08-25 00:35:26 +0000933 return GetStackFrameList().GetNumFrames();
934}
Greg Clayton33ed1702010-08-24 00:45:41 +0000935
Greg Clayton33ed1702010-08-24 00:45:41 +0000936
Greg Clayton782b9cc2010-08-25 00:35:26 +0000937void
938Thread::ClearStackFrames ()
939{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000940 if (m_curr_frames_sp && m_curr_frames_sp->GetNumFrames (false) > 1)
941 m_prev_frames_sp.swap (m_curr_frames_sp);
942 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +0000943}
944
945lldb::StackFrameSP
946Thread::GetStackFrameAtIndex (uint32_t idx)
947{
Greg Clayton72b71582010-09-02 21:44:10 +0000948 return GetStackFrameList().GetFrameAtIndex(idx);
Jim Ingham71219082010-08-12 02:14:28 +0000949}
950
Greg Claytonc12b6b42010-10-10 22:28:11 +0000951uint32_t
952Thread::GetSelectedFrameIndex ()
953{
954 return GetStackFrameList().GetSelectedFrameIndex();
955}
956
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000957lldb::StackFrameSP
958Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
959{
960 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
961}
962
Jim Ingham5c4b1602011-03-31 00:15:49 +0000963lldb::StackFrameSP
964Thread::GetFrameWithStackID(StackID &stack_id)
965{
966 return GetStackFrameList().GetFrameWithStackID (stack_id);
967}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000968
Greg Claytonc12b6b42010-10-10 22:28:11 +0000969
Chris Lattner24943d22010-06-08 16:52:24 +0000970lldb::StackFrameSP
Jim Inghamc8332952010-08-26 21:32:51 +0000971Thread::GetSelectedFrame ()
Chris Lattner24943d22010-06-08 16:52:24 +0000972{
Jim Inghamc8332952010-08-26 21:32:51 +0000973 return GetStackFrameAtIndex (GetStackFrameList().GetSelectedFrameIndex());
Chris Lattner24943d22010-06-08 16:52:24 +0000974}
975
976uint32_t
Jim Inghamc8332952010-08-26 21:32:51 +0000977Thread::SetSelectedFrame (lldb_private::StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000978{
Jim Inghamc8332952010-08-26 21:32:51 +0000979 return GetStackFrameList().SetSelectedFrame(frame);
Chris Lattner24943d22010-06-08 16:52:24 +0000980}
981
982void
Jim Inghamc8332952010-08-26 21:32:51 +0000983Thread::SetSelectedFrameByIndex (uint32_t idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000984{
Jim Inghamc8332952010-08-26 21:32:51 +0000985 GetStackFrameList().SetSelectedFrameByIndex(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000986}
987
988void
Greg Claytona830adb2010-10-04 01:05:56 +0000989Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000990{
Greg Claytona830adb2010-10-04 01:05:56 +0000991 ExecutionContext exe_ctx;
992 SymbolContext frame_sc;
993 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000994
Greg Claytona830adb2010-10-04 01:05:56 +0000995 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000996 {
Greg Claytona830adb2010-10-04 01:05:56 +0000997 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000998 if (frame_sp)
999 {
Greg Claytona830adb2010-10-04 01:05:56 +00001000 exe_ctx.frame = frame_sp.get();
1001 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001002 }
1003 }
1004
Greg Claytona830adb2010-10-04 01:05:56 +00001005 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
1006 assert (thread_format);
1007 const char *end = NULL;
1008 Debugger::FormatPrompt (thread_format,
1009 exe_ctx.frame ? &frame_sc : NULL,
1010 &exe_ctx,
1011 NULL,
1012 strm,
1013 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001014}
1015
1016lldb::ThreadSP
1017Thread::GetSP ()
1018{
1019 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
1020}
Jim Ingham20594b12010-09-08 03:14:33 +00001021
Greg Clayton990de7b2010-11-18 23:32:35 +00001022
1023void
Caroline Tice2a456812011-03-10 22:14:10 +00001024Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001025{
Greg Clayton990de7b2010-11-18 23:32:35 +00001026 UserSettingsControllerSP &usc = GetSettingsController();
1027 usc.reset (new SettingsController);
1028 UserSettingsController::InitializeSettingsController (usc,
1029 SettingsController::global_settings_table,
1030 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001031
1032 // Now call SettingsInitialize() on each 'child' setting of Thread.
1033 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001034}
Jim Ingham20594b12010-09-08 03:14:33 +00001035
Greg Clayton990de7b2010-11-18 23:32:35 +00001036void
Caroline Tice2a456812011-03-10 22:14:10 +00001037Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001038{
Caroline Tice2a456812011-03-10 22:14:10 +00001039 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1040 // Currently there are none.
1041
1042 // Now terminate Thread Settings.
1043
Greg Clayton990de7b2010-11-18 23:32:35 +00001044 UserSettingsControllerSP &usc = GetSettingsController();
1045 UserSettingsController::FinalizeSettingsController (usc);
1046 usc.reset();
1047}
Jim Ingham20594b12010-09-08 03:14:33 +00001048
Greg Clayton990de7b2010-11-18 23:32:35 +00001049UserSettingsControllerSP &
1050Thread::GetSettingsController ()
1051{
1052 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001053 return g_settings_controller;
1054}
1055
Caroline Tice1ebef442010-09-27 00:30:10 +00001056void
1057Thread::UpdateInstanceName ()
1058{
1059 StreamString sstr;
1060 const char *name = GetName();
1061
1062 if (name && name[0] != '\0')
1063 sstr.Printf ("%s", name);
1064 else if ((GetIndexID() != 0) || (GetID() != 0))
1065 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1066
1067 if (sstr.GetSize() > 0)
1068 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1069}
1070
Jim Inghamccd584d2010-09-23 17:40:12 +00001071lldb::StackFrameSP
1072Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1073{
1074 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1075}
Caroline Tice7826c882010-10-26 03:11:13 +00001076
1077const char *
1078Thread::StopReasonAsCString (lldb::StopReason reason)
1079{
1080 switch (reason)
1081 {
1082 case eStopReasonInvalid: return "invalid";
1083 case eStopReasonNone: return "none";
1084 case eStopReasonTrace: return "trace";
1085 case eStopReasonBreakpoint: return "breakpoint";
1086 case eStopReasonWatchpoint: return "watchpoint";
1087 case eStopReasonSignal: return "signal";
1088 case eStopReasonException: return "exception";
1089 case eStopReasonPlanComplete: return "plan complete";
1090 }
1091
1092
1093 static char unknown_state_string[64];
1094 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1095 return unknown_state_string;
1096}
1097
1098const char *
1099Thread::RunModeAsCString (lldb::RunMode mode)
1100{
1101 switch (mode)
1102 {
1103 case eOnlyThisThread: return "only this thread";
1104 case eAllThreads: return "all threads";
1105 case eOnlyDuringStepping: return "only during stepping";
1106 }
1107
1108 static char unknown_state_string[64];
1109 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1110 return unknown_state_string;
1111}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001112
Greg Claytonabe0fed2011-04-18 08:33:37 +00001113size_t
1114Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1115{
1116 size_t num_frames_shown = 0;
1117 strm.Indent();
1118 strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' ');
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001119 if (GetProcess().GetTarget().GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001120 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001121 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001122 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001123 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001124 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1125 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1126 {
1127 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1128 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001129 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001130 }
1131
1132 DumpUsingSettingsFormat (strm, start_frame);
1133
1134 if (num_frames > 0)
1135 {
1136 strm.IndentMore();
1137
1138 const bool show_frame_info = true;
1139 const uint32_t source_lines_before = 3;
1140 const uint32_t source_lines_after = 3;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001141 strm.IndentMore ();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001142 num_frames_shown = GetStackFrameList ().GetStatus (strm,
1143 start_frame,
1144 num_frames,
1145 show_frame_info,
1146 num_frames_with_source,
1147 source_lines_before,
1148 source_lines_after);
1149 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001150 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001151 }
1152 return num_frames_shown;
1153}
1154
1155size_t
1156Thread::GetStackFrameStatus (Stream& strm,
1157 uint32_t first_frame,
1158 uint32_t num_frames,
1159 bool show_frame_info,
1160 uint32_t num_frames_with_source,
1161 uint32_t source_lines_before,
1162 uint32_t source_lines_after)
1163{
1164 return GetStackFrameList().GetStatus (strm,
1165 first_frame,
1166 num_frames,
1167 show_frame_info,
1168 num_frames_with_source,
1169 source_lines_before,
1170 source_lines_after);
1171}
1172
Peter Collingbournee426d852011-06-03 20:40:54 +00001173bool
1174Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1175{
1176 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1177 if (frame_sp)
1178 {
1179 checkpoint.SetStackID(frame_sp->GetStackID());
1180 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1181 }
1182 return false;
1183}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001184
Peter Collingbournee426d852011-06-03 20:40:54 +00001185bool
1186Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1187{
1188 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1189 if (frame_sp)
1190 {
1191 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1192
1193 // Clear out all stack frames as our world just changed.
1194 ClearStackFrames();
1195 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1196
1197 return ret;
1198 }
1199 return false;
1200}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001201
Greg Clayton37f962e2011-08-22 02:49:39 +00001202Unwind *
1203Thread::GetUnwinder ()
1204{
1205 if (m_unwinder_ap.get() == NULL)
1206 {
1207 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
1208 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1209 switch (machine)
1210 {
1211 case llvm::Triple::x86_64:
1212 case llvm::Triple::x86:
1213 case llvm::Triple::arm:
1214 case llvm::Triple::thumb:
1215 m_unwinder_ap.reset (new UnwindLLDB (*this));
1216 break;
1217
1218 default:
1219 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1220 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1221 break;
1222 }
1223 }
1224 return m_unwinder_ap.get();
1225}
1226
1227
Greg Clayton990de7b2010-11-18 23:32:35 +00001228#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001229//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001230// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001231//--------------------------------------------------------------
1232
Greg Clayton990de7b2010-11-18 23:32:35 +00001233Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001234 UserSettingsController ("thread", Process::GetSettingsController())
1235{
1236 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1237 InstanceSettings::GetDefaultName().AsCString()));
1238}
1239
Greg Clayton990de7b2010-11-18 23:32:35 +00001240Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001241{
1242}
1243
1244lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001245Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001246{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001247 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1248 false,
1249 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001250 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1251 return new_settings_sp;
1252}
1253
1254#pragma mark "ThreadInstanceSettings"
1255//--------------------------------------------------------------
1256// class ThreadInstanceSettings
1257//--------------------------------------------------------------
1258
1259ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001260 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001261 m_avoid_regexp_ap (),
1262 m_trace_enabled (false)
1263{
1264 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1265 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1266 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1267 // This is true for CreateInstanceName() too.
1268
1269 if (GetInstanceName() == InstanceSettings::InvalidName())
1270 {
1271 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1272 m_owner.RegisterInstanceSettings (this);
1273 }
1274
1275 if (live_instance)
1276 {
1277 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1278 CopyInstanceSettings (pending_settings,false);
1279 //m_owner.RemovePendingSettings (m_instance_name);
1280 }
1281}
1282
1283ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001284 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001285 m_avoid_regexp_ap (),
1286 m_trace_enabled (rhs.m_trace_enabled)
1287{
1288 if (m_instance_name != InstanceSettings::GetDefaultName())
1289 {
1290 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1291 CopyInstanceSettings (pending_settings,false);
1292 m_owner.RemovePendingSettings (m_instance_name);
1293 }
1294 if (rhs.m_avoid_regexp_ap.get() != NULL)
1295 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1296}
1297
1298ThreadInstanceSettings::~ThreadInstanceSettings ()
1299{
1300}
1301
1302ThreadInstanceSettings&
1303ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1304{
1305 if (this != &rhs)
1306 {
1307 if (rhs.m_avoid_regexp_ap.get() != NULL)
1308 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1309 else
1310 m_avoid_regexp_ap.reset(NULL);
1311 }
1312 m_trace_enabled = rhs.m_trace_enabled;
1313 return *this;
1314}
1315
1316
1317void
1318ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1319 const char *index_value,
1320 const char *value,
1321 const ConstString &instance_name,
1322 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001323 VarSetOperationType op,
Jim Ingham745ac7a2010-11-11 19:26:09 +00001324 Error &err,
1325 bool pending)
1326{
1327 if (var_name == StepAvoidRegexpVarName())
1328 {
1329 std::string regexp_text;
1330 if (m_avoid_regexp_ap.get() != NULL)
1331 regexp_text.append (m_avoid_regexp_ap->GetText());
1332 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1333 if (regexp_text.empty())
1334 m_avoid_regexp_ap.reset();
1335 else
1336 {
1337 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1338
1339 }
1340 }
1341 else if (var_name == GetTraceThreadVarName())
1342 {
1343 bool success;
1344 bool result = Args::StringToBoolean(value, false, &success);
1345
1346 if (success)
1347 {
1348 m_trace_enabled = result;
1349 if (!pending)
1350 {
1351 Thread *myself = static_cast<Thread *> (this);
1352 myself->EnableTracer(m_trace_enabled, true);
1353 }
1354 }
1355 else
1356 {
1357 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1358 }
1359
1360 }
1361}
1362
1363void
1364ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1365 bool pending)
1366{
1367 if (new_settings.get() == NULL)
1368 return;
1369
1370 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1371 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1372 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1373 else
1374 m_avoid_regexp_ap.reset ();
1375}
1376
1377bool
1378ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1379 const ConstString &var_name,
1380 StringList &value,
1381 Error *err)
1382{
1383 if (var_name == StepAvoidRegexpVarName())
1384 {
1385 if (m_avoid_regexp_ap.get() != NULL)
1386 {
1387 std::string regexp_text("\"");
1388 regexp_text.append(m_avoid_regexp_ap->GetText());
1389 regexp_text.append ("\"");
1390 value.AppendString (regexp_text.c_str());
1391 }
1392
1393 }
1394 else if (var_name == GetTraceThreadVarName())
1395 {
1396 value.AppendString(m_trace_enabled ? "true" : "false");
1397 }
1398 else
1399 {
1400 if (err)
1401 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1402 return false;
1403 }
1404 return true;
1405}
1406
1407const ConstString
1408ThreadInstanceSettings::CreateInstanceName ()
1409{
1410 static int instance_count = 1;
1411 StreamString sstr;
1412
1413 sstr.Printf ("thread_%d", instance_count);
1414 ++instance_count;
1415
1416 const ConstString ret_val (sstr.GetData());
1417 return ret_val;
1418}
1419
1420const ConstString &
1421ThreadInstanceSettings::StepAvoidRegexpVarName ()
1422{
1423 static ConstString step_avoid_var_name ("step-avoid-regexp");
1424
1425 return step_avoid_var_name;
1426}
1427
1428const ConstString &
1429ThreadInstanceSettings::GetTraceThreadVarName ()
1430{
1431 static ConstString trace_thread_var_name ("trace-thread");
1432
1433 return trace_thread_var_name;
1434}
1435
1436//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001437// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001438//--------------------------------------------------
1439
1440SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001441Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001442{
1443 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1444 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1445};
1446
1447
1448SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001449Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001450{
1451 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1452 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1453 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1454 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1455};