blob: babb2dd6848b7b32f6a4827d331a91f6a1df7003 [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
Greg Clayton782b9cc2010-08-25 00:35:26 +0000928void
929Thread::ClearStackFrames ()
930{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000931 if (m_curr_frames_sp && m_curr_frames_sp->GetNumFrames (false) > 1)
932 m_prev_frames_sp.swap (m_curr_frames_sp);
933 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +0000934}
935
936lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000937Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
938{
939 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
940}
941
Chris Lattner24943d22010-06-08 16:52:24 +0000942void
Greg Claytona830adb2010-10-04 01:05:56 +0000943Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000944{
Greg Claytona830adb2010-10-04 01:05:56 +0000945 ExecutionContext exe_ctx;
946 SymbolContext frame_sc;
947 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000948
Greg Claytona830adb2010-10-04 01:05:56 +0000949 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000950 {
Greg Claytona830adb2010-10-04 01:05:56 +0000951 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000952 if (frame_sp)
953 {
Greg Claytona830adb2010-10-04 01:05:56 +0000954 exe_ctx.frame = frame_sp.get();
955 frame_sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +0000956 }
957 }
958
Greg Claytona830adb2010-10-04 01:05:56 +0000959 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
960 assert (thread_format);
961 const char *end = NULL;
962 Debugger::FormatPrompt (thread_format,
963 exe_ctx.frame ? &frame_sc : NULL,
964 &exe_ctx,
965 NULL,
966 strm,
967 &end);
Chris Lattner24943d22010-06-08 16:52:24 +0000968}
969
970lldb::ThreadSP
971Thread::GetSP ()
972{
973 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
974}
Jim Ingham20594b12010-09-08 03:14:33 +0000975
Greg Clayton990de7b2010-11-18 23:32:35 +0000976
977void
Caroline Tice2a456812011-03-10 22:14:10 +0000978Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +0000979{
Greg Clayton990de7b2010-11-18 23:32:35 +0000980 UserSettingsControllerSP &usc = GetSettingsController();
981 usc.reset (new SettingsController);
982 UserSettingsController::InitializeSettingsController (usc,
983 SettingsController::global_settings_table,
984 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000985
986 // Now call SettingsInitialize() on each 'child' setting of Thread.
987 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +0000988}
Jim Ingham20594b12010-09-08 03:14:33 +0000989
Greg Clayton990de7b2010-11-18 23:32:35 +0000990void
Caroline Tice2a456812011-03-10 22:14:10 +0000991Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000992{
Caroline Tice2a456812011-03-10 22:14:10 +0000993 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
994 // Currently there are none.
995
996 // Now terminate Thread Settings.
997
Greg Clayton990de7b2010-11-18 23:32:35 +0000998 UserSettingsControllerSP &usc = GetSettingsController();
999 UserSettingsController::FinalizeSettingsController (usc);
1000 usc.reset();
1001}
Jim Ingham20594b12010-09-08 03:14:33 +00001002
Greg Clayton990de7b2010-11-18 23:32:35 +00001003UserSettingsControllerSP &
1004Thread::GetSettingsController ()
1005{
1006 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001007 return g_settings_controller;
1008}
1009
Caroline Tice1ebef442010-09-27 00:30:10 +00001010void
1011Thread::UpdateInstanceName ()
1012{
1013 StreamString sstr;
1014 const char *name = GetName();
1015
1016 if (name && name[0] != '\0')
1017 sstr.Printf ("%s", name);
1018 else if ((GetIndexID() != 0) || (GetID() != 0))
1019 sstr.Printf ("0x%4.4x", GetIndexID(), GetID());
1020
1021 if (sstr.GetSize() > 0)
1022 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1023}
1024
Jim Inghamccd584d2010-09-23 17:40:12 +00001025lldb::StackFrameSP
1026Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1027{
1028 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1029}
Caroline Tice7826c882010-10-26 03:11:13 +00001030
1031const char *
1032Thread::StopReasonAsCString (lldb::StopReason reason)
1033{
1034 switch (reason)
1035 {
1036 case eStopReasonInvalid: return "invalid";
1037 case eStopReasonNone: return "none";
1038 case eStopReasonTrace: return "trace";
1039 case eStopReasonBreakpoint: return "breakpoint";
1040 case eStopReasonWatchpoint: return "watchpoint";
1041 case eStopReasonSignal: return "signal";
1042 case eStopReasonException: return "exception";
1043 case eStopReasonPlanComplete: return "plan complete";
1044 }
1045
1046
1047 static char unknown_state_string[64];
1048 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1049 return unknown_state_string;
1050}
1051
1052const char *
1053Thread::RunModeAsCString (lldb::RunMode mode)
1054{
1055 switch (mode)
1056 {
1057 case eOnlyThisThread: return "only this thread";
1058 case eAllThreads: return "all threads";
1059 case eOnlyDuringStepping: return "only during stepping";
1060 }
1061
1062 static char unknown_state_string[64];
1063 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1064 return unknown_state_string;
1065}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001066
Greg Claytonabe0fed2011-04-18 08:33:37 +00001067size_t
1068Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1069{
1070 size_t num_frames_shown = 0;
1071 strm.Indent();
1072 strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' ');
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001073 if (GetProcess().GetTarget().GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001074 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001075 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001076 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001077 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001078 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1079 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1080 {
1081 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1082 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001083 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001084 }
1085
1086 DumpUsingSettingsFormat (strm, start_frame);
1087
1088 if (num_frames > 0)
1089 {
1090 strm.IndentMore();
1091
1092 const bool show_frame_info = true;
1093 const uint32_t source_lines_before = 3;
1094 const uint32_t source_lines_after = 3;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001095 strm.IndentMore ();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001096 num_frames_shown = GetStackFrameList ().GetStatus (strm,
1097 start_frame,
1098 num_frames,
1099 show_frame_info,
1100 num_frames_with_source,
1101 source_lines_before,
1102 source_lines_after);
1103 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001104 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001105 }
1106 return num_frames_shown;
1107}
1108
1109size_t
1110Thread::GetStackFrameStatus (Stream& strm,
1111 uint32_t first_frame,
1112 uint32_t num_frames,
1113 bool show_frame_info,
1114 uint32_t num_frames_with_source,
1115 uint32_t source_lines_before,
1116 uint32_t source_lines_after)
1117{
1118 return GetStackFrameList().GetStatus (strm,
1119 first_frame,
1120 num_frames,
1121 show_frame_info,
1122 num_frames_with_source,
1123 source_lines_before,
1124 source_lines_after);
1125}
1126
Peter Collingbournee426d852011-06-03 20:40:54 +00001127bool
1128Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1129{
1130 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1131 if (frame_sp)
1132 {
1133 checkpoint.SetStackID(frame_sp->GetStackID());
1134 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1135 }
1136 return false;
1137}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001138
Peter Collingbournee426d852011-06-03 20:40:54 +00001139bool
1140Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1141{
1142 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1143 if (frame_sp)
1144 {
1145 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1146
1147 // Clear out all stack frames as our world just changed.
1148 ClearStackFrames();
1149 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1150
1151 return ret;
1152 }
1153 return false;
1154}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001155
Greg Clayton37f962e2011-08-22 02:49:39 +00001156Unwind *
1157Thread::GetUnwinder ()
1158{
1159 if (m_unwinder_ap.get() == NULL)
1160 {
1161 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
1162 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1163 switch (machine)
1164 {
1165 case llvm::Triple::x86_64:
1166 case llvm::Triple::x86:
1167 case llvm::Triple::arm:
1168 case llvm::Triple::thumb:
1169 m_unwinder_ap.reset (new UnwindLLDB (*this));
1170 break;
1171
1172 default:
1173 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1174 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1175 break;
1176 }
1177 }
1178 return m_unwinder_ap.get();
1179}
1180
1181
Greg Clayton990de7b2010-11-18 23:32:35 +00001182#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001183//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001184// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001185//--------------------------------------------------------------
1186
Greg Clayton990de7b2010-11-18 23:32:35 +00001187Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001188 UserSettingsController ("thread", Process::GetSettingsController())
1189{
1190 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1191 InstanceSettings::GetDefaultName().AsCString()));
1192}
1193
Greg Clayton990de7b2010-11-18 23:32:35 +00001194Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001195{
1196}
1197
1198lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001199Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001200{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001201 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1202 false,
1203 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001204 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1205 return new_settings_sp;
1206}
1207
1208#pragma mark "ThreadInstanceSettings"
1209//--------------------------------------------------------------
1210// class ThreadInstanceSettings
1211//--------------------------------------------------------------
1212
1213ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001214 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001215 m_avoid_regexp_ap (),
1216 m_trace_enabled (false)
1217{
1218 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1219 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1220 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1221 // This is true for CreateInstanceName() too.
1222
1223 if (GetInstanceName() == InstanceSettings::InvalidName())
1224 {
1225 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1226 m_owner.RegisterInstanceSettings (this);
1227 }
1228
1229 if (live_instance)
1230 {
1231 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1232 CopyInstanceSettings (pending_settings,false);
1233 //m_owner.RemovePendingSettings (m_instance_name);
1234 }
1235}
1236
1237ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001238 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001239 m_avoid_regexp_ap (),
1240 m_trace_enabled (rhs.m_trace_enabled)
1241{
1242 if (m_instance_name != InstanceSettings::GetDefaultName())
1243 {
1244 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1245 CopyInstanceSettings (pending_settings,false);
1246 m_owner.RemovePendingSettings (m_instance_name);
1247 }
1248 if (rhs.m_avoid_regexp_ap.get() != NULL)
1249 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1250}
1251
1252ThreadInstanceSettings::~ThreadInstanceSettings ()
1253{
1254}
1255
1256ThreadInstanceSettings&
1257ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1258{
1259 if (this != &rhs)
1260 {
1261 if (rhs.m_avoid_regexp_ap.get() != NULL)
1262 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1263 else
1264 m_avoid_regexp_ap.reset(NULL);
1265 }
1266 m_trace_enabled = rhs.m_trace_enabled;
1267 return *this;
1268}
1269
1270
1271void
1272ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1273 const char *index_value,
1274 const char *value,
1275 const ConstString &instance_name,
1276 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001277 VarSetOperationType op,
Jim Ingham745ac7a2010-11-11 19:26:09 +00001278 Error &err,
1279 bool pending)
1280{
1281 if (var_name == StepAvoidRegexpVarName())
1282 {
1283 std::string regexp_text;
1284 if (m_avoid_regexp_ap.get() != NULL)
1285 regexp_text.append (m_avoid_regexp_ap->GetText());
1286 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1287 if (regexp_text.empty())
1288 m_avoid_regexp_ap.reset();
1289 else
1290 {
1291 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1292
1293 }
1294 }
1295 else if (var_name == GetTraceThreadVarName())
1296 {
1297 bool success;
1298 bool result = Args::StringToBoolean(value, false, &success);
1299
1300 if (success)
1301 {
1302 m_trace_enabled = result;
1303 if (!pending)
1304 {
1305 Thread *myself = static_cast<Thread *> (this);
1306 myself->EnableTracer(m_trace_enabled, true);
1307 }
1308 }
1309 else
1310 {
1311 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1312 }
1313
1314 }
1315}
1316
1317void
1318ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1319 bool pending)
1320{
1321 if (new_settings.get() == NULL)
1322 return;
1323
1324 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1325 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1326 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1327 else
1328 m_avoid_regexp_ap.reset ();
1329}
1330
1331bool
1332ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1333 const ConstString &var_name,
1334 StringList &value,
1335 Error *err)
1336{
1337 if (var_name == StepAvoidRegexpVarName())
1338 {
1339 if (m_avoid_regexp_ap.get() != NULL)
1340 {
1341 std::string regexp_text("\"");
1342 regexp_text.append(m_avoid_regexp_ap->GetText());
1343 regexp_text.append ("\"");
1344 value.AppendString (regexp_text.c_str());
1345 }
1346
1347 }
1348 else if (var_name == GetTraceThreadVarName())
1349 {
1350 value.AppendString(m_trace_enabled ? "true" : "false");
1351 }
1352 else
1353 {
1354 if (err)
1355 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1356 return false;
1357 }
1358 return true;
1359}
1360
1361const ConstString
1362ThreadInstanceSettings::CreateInstanceName ()
1363{
1364 static int instance_count = 1;
1365 StreamString sstr;
1366
1367 sstr.Printf ("thread_%d", instance_count);
1368 ++instance_count;
1369
1370 const ConstString ret_val (sstr.GetData());
1371 return ret_val;
1372}
1373
1374const ConstString &
1375ThreadInstanceSettings::StepAvoidRegexpVarName ()
1376{
1377 static ConstString step_avoid_var_name ("step-avoid-regexp");
1378
1379 return step_avoid_var_name;
1380}
1381
1382const ConstString &
1383ThreadInstanceSettings::GetTraceThreadVarName ()
1384{
1385 static ConstString trace_thread_var_name ("trace-thread");
1386
1387 return trace_thread_var_name;
1388}
1389
1390//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001391// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001392//--------------------------------------------------
1393
1394SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001395Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001396{
1397 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1398 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1399};
1400
1401
1402SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001403Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001404{
1405 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1406 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1407 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1408 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1409};