blob: e4415b7b73eb9abd06f5910f2890b93ad4e241b8 [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)
Greg Clayton444e35b2011-10-19 18:09:39 +000068 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +000069
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)
Greg Clayton444e35b2011-10-19 18:09:39 +000079 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", 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 {
Jim Inghame8f4e112011-10-15 00:23:43 +0000265 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000266 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000267 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000268 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000269 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000270 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000271
272 // The top most plan always gets to do the trace log...
273 current_plan->DoTraceLog ();
Chris Lattner24943d22010-06-08 16:52:24 +0000274
Jim Inghamad382c52011-12-03 01:52:59 +0000275 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
276 // If that plan is still working, then we don't need to do any more work. If the plan that explains
277 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
278 // whether they still need to do more work.
279
280 bool done_processing_current_plan = false;
281
282 if (!current_plan->PlanExplainsStop())
283 {
284 if (current_plan->TracerExplainsStop())
285 {
286 done_processing_current_plan = true;
287 should_stop = false;
288 }
289 else
290 {
291 // If the current plan doesn't explain the stop, then, find one that
292 // does and let it handle the situation.
293 ThreadPlan *plan_ptr = current_plan;
294 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
295 {
296 if (plan_ptr->PlanExplainsStop())
297 {
298 should_stop = plan_ptr->ShouldStop (event_ptr);
299
300 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
301 // and all the plans below it off the stack.
302
303 if (plan_ptr->MischiefManaged())
304 {
305 // We're going to pop the plans up to AND INCLUDING the plan that explains the stop.
306 plan_ptr = GetPreviousPlan(plan_ptr);
307
308 do
309 {
310 if (should_stop)
311 current_plan->WillStop();
312 PopPlan();
313 }
314 while ((current_plan = GetCurrentPlan()) != plan_ptr);
315 done_processing_current_plan = false;
316 }
317 else
318 done_processing_current_plan = true;
319
320 break;
321 }
322
323 }
324 }
325 }
326
327 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000328 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000329 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000330
Jim Inghame8f4e112011-10-15 00:23:43 +0000331 if (log)
332 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
333
Jim Inghamf9f40c22011-02-08 05:20:59 +0000334 // We're starting from the base plan, so just let it decide;
335 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000336 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000337 should_stop = current_plan->ShouldStop (event_ptr);
338 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000339 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000340 }
341 else
342 {
343 // Otherwise, don't let the base plan override what the other plans say to do, since
344 // presumably if there were other plans they would know what to do...
345 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000346 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000347 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000348 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000349
350 should_stop = current_plan->ShouldStop(event_ptr);
351 if (log)
352 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
353 if (current_plan->MischiefManaged())
354 {
355 if (should_stop)
356 current_plan->WillStop();
357
358 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
359 // Otherwise, see if the plan's parent wants to stop.
360
361 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
362 {
363 PopPlan();
364 break;
365 }
366 else
367 {
368
369 PopPlan();
370
371 current_plan = GetCurrentPlan();
372 if (current_plan == NULL)
373 {
374 break;
375 }
376 }
377
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379 else
380 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000381 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000382 }
Chris Lattner24943d22010-06-08 16:52:24 +0000383 }
384 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000385 if (over_ride_stop)
386 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000387 }
Chris Lattner24943d22010-06-08 16:52:24 +0000388
Jim Inghame8f4e112011-10-15 00:23:43 +0000389 if (log)
390 {
391 StreamString s;
392 s.IndentMore();
393 DumpThreadPlans(&s);
394 log->Printf ("Plan stack final state:\n%s", s.GetData());
395 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
396 }
Chris Lattner24943d22010-06-08 16:52:24 +0000397 return should_stop;
398}
399
400Vote
401Thread::ShouldReportStop (Event* event_ptr)
402{
403 StateType thread_state = GetResumeState ();
Greg Claytone005f2c2010-11-06 01:53:30 +0000404 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000405
406 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
407 {
408 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000409 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Chris Lattner24943d22010-06-08 16:52:24 +0000410 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000411 }
Chris Lattner24943d22010-06-08 16:52:24 +0000412
413 if (m_completed_plan_stack.size() > 0)
414 {
415 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000416 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000417 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for complete stack's back plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000418 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
419 }
420 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000421 {
422 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000423 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000424 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000425 }
Chris Lattner24943d22010-06-08 16:52:24 +0000426}
427
428Vote
429Thread::ShouldReportRun (Event* event_ptr)
430{
431 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000432
Chris Lattner24943d22010-06-08 16:52:24 +0000433 if (thread_state == eStateSuspended
434 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000435 {
Chris Lattner24943d22010-06-08 16:52:24 +0000436 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000437 }
438
439 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000440 if (m_completed_plan_stack.size() > 0)
441 {
442 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000443 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000444 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
Jim Inghamac959662011-01-24 06:34:17 +0000445 GetIndexID(),
446 GetID(),
447 m_completed_plan_stack.back()->GetName());
448
Chris Lattner24943d22010-06-08 16:52:24 +0000449 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
450 }
451 else
Jim Inghamac959662011-01-24 06:34:17 +0000452 {
453 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000454 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
Jim Inghamac959662011-01-24 06:34:17 +0000455 GetIndexID(),
456 GetID(),
457 GetCurrentPlan()->GetName());
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000460 }
Chris Lattner24943d22010-06-08 16:52:24 +0000461}
462
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000463bool
464Thread::MatchesSpec (const ThreadSpec *spec)
465{
466 if (spec == NULL)
467 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000468
Jim Ingham649492b2010-06-18 01:00:58 +0000469 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000470}
471
Chris Lattner24943d22010-06-08 16:52:24 +0000472void
473Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
474{
475 if (thread_plan_sp)
476 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000477 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
478 if (!thread_plan_sp->GetThreadPlanTracer())
479 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000480 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000481
Chris Lattner24943d22010-06-08 16:52:24 +0000482 thread_plan_sp->DidPush();
483
Greg Claytone005f2c2010-11-06 01:53:30 +0000484 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000485 if (log)
486 {
487 StreamString s;
488 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000489 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000490 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000491 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000492 }
493 }
494}
495
496void
497Thread::PopPlan ()
498{
Greg Claytone005f2c2010-11-06 01:53:30 +0000499 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000500
Jim Ingham6297a3a2010-10-20 00:39:53 +0000501 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000502 return;
503 else
504 {
505 ThreadPlanSP &plan = m_plan_stack.back();
506 if (log)
507 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000508 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000509 }
510 m_completed_plan_stack.push_back (plan);
511 plan->WillPop();
512 m_plan_stack.pop_back();
513 }
514}
515
516void
517Thread::DiscardPlan ()
518{
519 if (m_plan_stack.size() > 1)
520 {
521 ThreadPlanSP &plan = m_plan_stack.back();
522 m_discarded_plan_stack.push_back (plan);
523 plan->WillPop();
524 m_plan_stack.pop_back();
525 }
526}
527
528ThreadPlan *
529Thread::GetCurrentPlan ()
530{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000531 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000532 return NULL;
533 else
534 return m_plan_stack.back().get();
535}
536
537ThreadPlanSP
538Thread::GetCompletedPlan ()
539{
540 ThreadPlanSP empty_plan_sp;
541 if (!m_completed_plan_stack.empty())
542 {
543 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
544 {
545 ThreadPlanSP completed_plan_sp;
546 completed_plan_sp = m_completed_plan_stack[i];
547 if (!completed_plan_sp->GetPrivate ())
548 return completed_plan_sp;
549 }
550 }
551 return empty_plan_sp;
552}
553
554bool
555Thread::IsThreadPlanDone (ThreadPlan *plan)
556{
Chris Lattner24943d22010-06-08 16:52:24 +0000557 if (!m_completed_plan_stack.empty())
558 {
559 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
560 {
561 if (m_completed_plan_stack[i].get() == plan)
562 return true;
563 }
564 }
565 return false;
566}
567
568bool
569Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
570{
Chris Lattner24943d22010-06-08 16:52:24 +0000571 if (!m_discarded_plan_stack.empty())
572 {
573 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
574 {
575 if (m_discarded_plan_stack[i].get() == plan)
576 return true;
577 }
578 }
579 return false;
580}
581
582ThreadPlan *
583Thread::GetPreviousPlan (ThreadPlan *current_plan)
584{
585 if (current_plan == NULL)
586 return NULL;
587
588 int stack_size = m_completed_plan_stack.size();
589 for (int i = stack_size - 1; i > 0; i--)
590 {
591 if (current_plan == m_completed_plan_stack[i].get())
592 return m_completed_plan_stack[i-1].get();
593 }
594
595 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
596 {
Chris Lattner24943d22010-06-08 16:52:24 +0000597 if (m_plan_stack.size() > 0)
598 return m_plan_stack.back().get();
599 else
600 return NULL;
601 }
602
603 stack_size = m_plan_stack.size();
604 for (int i = stack_size - 1; i > 0; i--)
605 {
606 if (current_plan == m_plan_stack[i].get())
607 return m_plan_stack[i-1].get();
608 }
609 return NULL;
610}
611
612void
613Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
614{
615 if (abort_other_plans)
616 DiscardThreadPlans(true);
617
618 PushPlan (thread_plan_sp);
619}
620
Jim Ingham745ac7a2010-11-11 19:26:09 +0000621
622void
623Thread::EnableTracer (bool value, bool single_stepping)
624{
625 int stack_size = m_plan_stack.size();
626 for (int i = 0; i < stack_size; i++)
627 {
628 if (m_plan_stack[i]->GetThreadPlanTracer())
629 {
630 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
631 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
632 }
633 }
634}
635
636void
637Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
638{
639 int stack_size = m_plan_stack.size();
640 for (int i = 0; i < stack_size; i++)
641 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
642}
643
Chris Lattner24943d22010-06-08 16:52:24 +0000644void
Jim Inghamea9d4262010-11-05 19:25:48 +0000645Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
646{
Greg Claytone005f2c2010-11-06 01:53:30 +0000647 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000648 if (log)
649 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000650 log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_sp.get());
Jim Inghamea9d4262010-11-05 19:25:48 +0000651 }
652
653 int stack_size = m_plan_stack.size();
654
655 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
656 // stack, and if so discard up to and including it.
657
658 if (up_to_plan_sp.get() == NULL)
659 {
660 for (int i = stack_size - 1; i > 0; i--)
661 DiscardPlan();
662 }
663 else
664 {
665 bool found_it = false;
666 for (int i = stack_size - 1; i > 0; i--)
667 {
668 if (m_plan_stack[i] == up_to_plan_sp)
669 found_it = true;
670 }
671 if (found_it)
672 {
673 bool last_one = false;
674 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
675 {
676 if (GetCurrentPlan() == up_to_plan_sp.get())
677 last_one = true;
678 DiscardPlan();
679 }
680 }
681 }
682 return;
683}
684
685void
Chris Lattner24943d22010-06-08 16:52:24 +0000686Thread::DiscardThreadPlans(bool force)
687{
Greg Claytone005f2c2010-11-06 01:53:30 +0000688 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000689 if (log)
690 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000691 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000692 }
693
694 if (force)
695 {
696 int stack_size = m_plan_stack.size();
697 for (int i = stack_size - 1; i > 0; i--)
698 {
699 DiscardPlan();
700 }
701 return;
702 }
703
704 while (1)
705 {
706
707 int master_plan_idx;
708 bool discard;
709
710 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
711 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
712 {
713 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
714 {
715 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
716 break;
717 }
718 }
719
720 if (discard)
721 {
722 // First pop all the dependent plans:
723 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
724 {
725
726 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
727 // for the plan leaves it in a state that it is safe to pop the plan
728 // with no more notice?
729 DiscardPlan();
730 }
731
732 // Now discard the master plan itself.
733 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
734 // discard it's dependent plans, but not it...
735 if (master_plan_idx > 0)
736 {
737 DiscardPlan();
738 }
739 }
740 else
741 {
742 // If the master plan doesn't want to get discarded, then we're done.
743 break;
744 }
745
746 }
Chris Lattner24943d22010-06-08 16:52:24 +0000747}
748
749ThreadPlan *
750Thread::QueueFundamentalPlan (bool abort_other_plans)
751{
752 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
753 QueueThreadPlan (thread_plan_sp, abort_other_plans);
754 return thread_plan_sp.get();
755}
756
757ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000758Thread::QueueThreadPlanForStepSingleInstruction
759(
760 bool step_over,
761 bool abort_other_plans,
762 bool stop_other_threads
763)
Chris Lattner24943d22010-06-08 16:52:24 +0000764{
765 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
766 QueueThreadPlan (thread_plan_sp, abort_other_plans);
767 return thread_plan_sp.get();
768}
769
770ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000771Thread::QueueThreadPlanForStepRange
772(
773 bool abort_other_plans,
774 StepType type,
775 const AddressRange &range,
776 const SymbolContext &addr_context,
777 lldb::RunMode stop_other_threads,
778 bool avoid_code_without_debug_info
779)
Chris Lattner24943d22010-06-08 16:52:24 +0000780{
781 ThreadPlanSP thread_plan_sp;
782 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000783 {
784 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
785 if (avoid_code_without_debug_info)
786 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
787 else
788 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
789 thread_plan_sp.reset (plan);
790 }
Chris Lattner24943d22010-06-08 16:52:24 +0000791 else
792 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
793
794 QueueThreadPlan (thread_plan_sp, abort_other_plans);
795 return thread_plan_sp.get();
796}
797
798
799ThreadPlan *
800Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
801{
802 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
803 QueueThreadPlan (thread_plan_sp, abort_other_plans);
804 return thread_plan_sp.get();
805}
806
807ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000808Thread::QueueThreadPlanForStepOut
809(
810 bool abort_other_plans,
811 SymbolContext *addr_context,
812 bool first_insn,
813 bool stop_other_threads,
814 Vote stop_vote,
815 Vote run_vote,
816 uint32_t frame_idx
817)
Chris Lattner24943d22010-06-08 16:52:24 +0000818{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000819 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
820 addr_context,
821 first_insn,
822 stop_other_threads,
823 stop_vote,
824 run_vote,
825 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000826 QueueThreadPlan (thread_plan_sp, abort_other_plans);
827 return thread_plan_sp.get();
828}
829
830ThreadPlan *
831Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
832{
Jim Inghamad382c52011-12-03 01:52:59 +0000833 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, stop_other_threads));
834 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
835 return NULL;
836
Chris Lattner24943d22010-06-08 16:52:24 +0000837 QueueThreadPlan (thread_plan_sp, abort_other_plans);
838 return thread_plan_sp.get();
839}
840
841ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000842Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
843 Address& function,
844 lldb::addr_t arg,
845 bool stop_other_threads,
846 bool discard_on_error)
847{
848 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
849 QueueThreadPlan (thread_plan_sp, abort_other_plans);
850 return thread_plan_sp.get();
851}
852
853ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000854Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
855 Address &target_addr,
856 bool stop_other_threads)
857{
858 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
859 QueueThreadPlan (thread_plan_sp, abort_other_plans);
860 return thread_plan_sp.get();
861}
862
863ThreadPlan *
864Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000865 lldb::addr_t *address_list,
866 size_t num_addresses,
867 bool stop_other_threads,
868 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000869{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000870 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000871 QueueThreadPlan (thread_plan_sp, abort_other_plans);
872 return thread_plan_sp.get();
873
874}
875
876uint32_t
877Thread::GetIndexID () const
878{
879 return m_index_id;
880}
881
882void
883Thread::DumpThreadPlans (lldb_private::Stream *s) const
884{
885 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +0000886 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +0000887 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +0000888 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4llx, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +0000889 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +0000890 {
Chris Lattner24943d22010-06-08 16:52:24 +0000891 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +0000892 s->Indent();
893 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000894 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +0000895 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +0000896 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +0000897 }
898
Chris Lattner24943d22010-06-08 16:52:24 +0000899 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +0000900 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000901 {
Jim Inghame8f4e112011-10-15 00:23:43 +0000902 s->Indent();
903 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
904 for (i = stack_size - 1; i >= 0; i--)
905 {
906 s->IndentMore();
907 s->Indent();
908 s->Printf ("Element %d: ", i);
909 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
910 s->EOL();
911 s->IndentLess();
912 }
Chris Lattner24943d22010-06-08 16:52:24 +0000913 }
914
915 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +0000916 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000917 {
Jim Inghame8f4e112011-10-15 00:23:43 +0000918 s->Indent();
919 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
920 for (i = stack_size - 1; i >= 0; i--)
921 {
922 s->IndentMore();
923 s->Indent();
924 s->Printf ("Element %d: ", i);
925 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
926 s->EOL();
927 s->IndentLess();
928 }
Chris Lattner24943d22010-06-08 16:52:24 +0000929 }
930
931}
932
933Target *
934Thread::CalculateTarget ()
935{
936 return m_process.CalculateTarget();
937}
938
939Process *
940Thread::CalculateProcess ()
941{
942 return &m_process;
943}
944
945Thread *
946Thread::CalculateThread ()
947{
948 return this;
949}
950
951StackFrame *
952Thread::CalculateStackFrame ()
953{
954 return NULL;
955}
956
957void
Greg Claytona830adb2010-10-04 01:05:56 +0000958Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000959{
Greg Claytona830adb2010-10-04 01:05:56 +0000960 m_process.CalculateExecutionContext (exe_ctx);
Greg Clayton567e7f32011-09-22 04:58:26 +0000961 exe_ctx.SetThreadPtr (this);
962 exe_ctx.SetFramePtr (NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000963}
964
Greg Clayton782b9cc2010-08-25 00:35:26 +0000965
966StackFrameList &
967Thread::GetStackFrameList ()
968{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000969 if (!m_curr_frames_sp)
970 m_curr_frames_sp.reset (new StackFrameList (*this, m_prev_frames_sp, true));
971 return *m_curr_frames_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +0000972}
973
Greg Clayton782b9cc2010-08-25 00:35:26 +0000974void
975Thread::ClearStackFrames ()
976{
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000977 if (m_curr_frames_sp && m_curr_frames_sp->GetNumFrames (false) > 1)
978 m_prev_frames_sp.swap (m_curr_frames_sp);
979 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +0000980}
981
982lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000983Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
984{
985 return GetStackFrameList().GetFrameWithConcreteFrameIndex (unwind_idx);
986}
987
Chris Lattner24943d22010-06-08 16:52:24 +0000988void
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;
Greg Clayton567e7f32011-09-22 04:58:26 +0000992 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +0000993 SymbolContext frame_sc;
994 CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +0000995
Greg Claytona830adb2010-10-04 01:05:56 +0000996 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +0000997 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000998 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000999 if (frame_sp)
1000 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001001 exe_ctx.SetFrameSP(frame_sp);
1002 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001003 }
1004 }
1005
Greg Claytona830adb2010-10-04 01:05:56 +00001006 const char *thread_format = GetProcess().GetTarget().GetDebugger().GetThreadFormat();
1007 assert (thread_format);
1008 const char *end = NULL;
1009 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001010 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001011 &exe_ctx,
1012 NULL,
1013 strm,
1014 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001015}
1016
1017lldb::ThreadSP
1018Thread::GetSP ()
1019{
Greg Clayton987c7eb2011-09-17 08:33:22 +00001020 // This object contains an instrusive ref count base class so we can
1021 // easily make a shared pointer to this object
1022 return ThreadSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001023}
Jim Ingham20594b12010-09-08 03:14:33 +00001024
Greg Clayton990de7b2010-11-18 23:32:35 +00001025
1026void
Caroline Tice2a456812011-03-10 22:14:10 +00001027Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001028{
Greg Clayton990de7b2010-11-18 23:32:35 +00001029 UserSettingsControllerSP &usc = GetSettingsController();
1030 usc.reset (new SettingsController);
1031 UserSettingsController::InitializeSettingsController (usc,
1032 SettingsController::global_settings_table,
1033 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001034
1035 // Now call SettingsInitialize() on each 'child' setting of Thread.
1036 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001037}
Jim Ingham20594b12010-09-08 03:14:33 +00001038
Greg Clayton990de7b2010-11-18 23:32:35 +00001039void
Caroline Tice2a456812011-03-10 22:14:10 +00001040Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001041{
Caroline Tice2a456812011-03-10 22:14:10 +00001042 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1043 // Currently there are none.
1044
1045 // Now terminate Thread Settings.
1046
Greg Clayton990de7b2010-11-18 23:32:35 +00001047 UserSettingsControllerSP &usc = GetSettingsController();
1048 UserSettingsController::FinalizeSettingsController (usc);
1049 usc.reset();
1050}
Jim Ingham20594b12010-09-08 03:14:33 +00001051
Greg Clayton990de7b2010-11-18 23:32:35 +00001052UserSettingsControllerSP &
1053Thread::GetSettingsController ()
1054{
1055 static UserSettingsControllerSP g_settings_controller;
Jim Ingham20594b12010-09-08 03:14:33 +00001056 return g_settings_controller;
1057}
1058
Caroline Tice1ebef442010-09-27 00:30:10 +00001059void
1060Thread::UpdateInstanceName ()
1061{
1062 StreamString sstr;
1063 const char *name = GetName();
1064
1065 if (name && name[0] != '\0')
1066 sstr.Printf ("%s", name);
1067 else if ((GetIndexID() != 0) || (GetID() != 0))
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001068 sstr.Printf ("0x%4.4x", GetIndexID());
Caroline Tice1ebef442010-09-27 00:30:10 +00001069
1070 if (sstr.GetSize() > 0)
1071 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1072}
1073
Jim Inghamccd584d2010-09-23 17:40:12 +00001074lldb::StackFrameSP
1075Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1076{
1077 return GetStackFrameList().GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1078}
Caroline Tice7826c882010-10-26 03:11:13 +00001079
1080const char *
1081Thread::StopReasonAsCString (lldb::StopReason reason)
1082{
1083 switch (reason)
1084 {
1085 case eStopReasonInvalid: return "invalid";
1086 case eStopReasonNone: return "none";
1087 case eStopReasonTrace: return "trace";
1088 case eStopReasonBreakpoint: return "breakpoint";
1089 case eStopReasonWatchpoint: return "watchpoint";
1090 case eStopReasonSignal: return "signal";
1091 case eStopReasonException: return "exception";
1092 case eStopReasonPlanComplete: return "plan complete";
1093 }
1094
1095
1096 static char unknown_state_string[64];
1097 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1098 return unknown_state_string;
1099}
1100
1101const char *
1102Thread::RunModeAsCString (lldb::RunMode mode)
1103{
1104 switch (mode)
1105 {
1106 case eOnlyThisThread: return "only this thread";
1107 case eAllThreads: return "all threads";
1108 case eOnlyDuringStepping: return "only during stepping";
1109 }
1110
1111 static char unknown_state_string[64];
1112 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1113 return unknown_state_string;
1114}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001115
Greg Claytonabe0fed2011-04-18 08:33:37 +00001116size_t
1117Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1118{
1119 size_t num_frames_shown = 0;
1120 strm.Indent();
1121 strm.Printf("%c ", GetProcess().GetThreadList().GetSelectedThread().get() == this ? '*' : ' ');
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001122 if (GetProcess().GetTarget().GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001123 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001124 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001125 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001126 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001127 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1128 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1129 {
1130 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1131 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001132 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001133 }
1134
1135 DumpUsingSettingsFormat (strm, start_frame);
1136
1137 if (num_frames > 0)
1138 {
1139 strm.IndentMore();
1140
1141 const bool show_frame_info = true;
1142 const uint32_t source_lines_before = 3;
1143 const uint32_t source_lines_after = 3;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001144 strm.IndentMore ();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001145 num_frames_shown = GetStackFrameList ().GetStatus (strm,
1146 start_frame,
1147 num_frames,
1148 show_frame_info,
1149 num_frames_with_source,
1150 source_lines_before,
1151 source_lines_after);
1152 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001153 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001154 }
1155 return num_frames_shown;
1156}
1157
1158size_t
1159Thread::GetStackFrameStatus (Stream& strm,
1160 uint32_t first_frame,
1161 uint32_t num_frames,
1162 bool show_frame_info,
1163 uint32_t num_frames_with_source,
1164 uint32_t source_lines_before,
1165 uint32_t source_lines_after)
1166{
1167 return GetStackFrameList().GetStatus (strm,
1168 first_frame,
1169 num_frames,
1170 show_frame_info,
1171 num_frames_with_source,
1172 source_lines_before,
1173 source_lines_after);
1174}
1175
Peter Collingbournee426d852011-06-03 20:40:54 +00001176bool
1177Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1178{
1179 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1180 if (frame_sp)
1181 {
1182 checkpoint.SetStackID(frame_sp->GetStackID());
1183 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1184 }
1185 return false;
1186}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001187
Peter Collingbournee426d852011-06-03 20:40:54 +00001188bool
1189Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1190{
1191 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1192 if (frame_sp)
1193 {
1194 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1195
1196 // Clear out all stack frames as our world just changed.
1197 ClearStackFrames();
1198 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1199
1200 return ret;
1201 }
1202 return false;
1203}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001204
Greg Clayton37f962e2011-08-22 02:49:39 +00001205Unwind *
1206Thread::GetUnwinder ()
1207{
1208 if (m_unwinder_ap.get() == NULL)
1209 {
1210 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
1211 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1212 switch (machine)
1213 {
1214 case llvm::Triple::x86_64:
1215 case llvm::Triple::x86:
1216 case llvm::Triple::arm:
1217 case llvm::Triple::thumb:
1218 m_unwinder_ap.reset (new UnwindLLDB (*this));
1219 break;
1220
1221 default:
1222 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1223 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1224 break;
1225 }
1226 }
1227 return m_unwinder_ap.get();
1228}
1229
1230
Greg Clayton990de7b2010-11-18 23:32:35 +00001231#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001232//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001233// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001234//--------------------------------------------------------------
1235
Greg Clayton990de7b2010-11-18 23:32:35 +00001236Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001237 UserSettingsController ("thread", Process::GetSettingsController())
1238{
1239 m_default_settings.reset (new ThreadInstanceSettings (*this, false,
1240 InstanceSettings::GetDefaultName().AsCString()));
1241}
1242
Greg Clayton990de7b2010-11-18 23:32:35 +00001243Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001244{
1245}
1246
1247lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001248Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001249{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001250 ThreadInstanceSettings *new_settings = new ThreadInstanceSettings (*GetSettingsController(),
1251 false,
1252 instance_name);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001253 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1254 return new_settings_sp;
1255}
1256
1257#pragma mark "ThreadInstanceSettings"
1258//--------------------------------------------------------------
1259// class ThreadInstanceSettings
1260//--------------------------------------------------------------
1261
1262ThreadInstanceSettings::ThreadInstanceSettings (UserSettingsController &owner, bool live_instance, const char *name) :
Greg Clayton638351a2010-12-04 00:10:17 +00001263 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001264 m_avoid_regexp_ap (),
1265 m_trace_enabled (false)
1266{
1267 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1268 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1269 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1270 // This is true for CreateInstanceName() too.
1271
1272 if (GetInstanceName() == InstanceSettings::InvalidName())
1273 {
1274 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1275 m_owner.RegisterInstanceSettings (this);
1276 }
1277
1278 if (live_instance)
1279 {
1280 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1281 CopyInstanceSettings (pending_settings,false);
1282 //m_owner.RemovePendingSettings (m_instance_name);
1283 }
1284}
1285
1286ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001287 InstanceSettings (*Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001288 m_avoid_regexp_ap (),
1289 m_trace_enabled (rhs.m_trace_enabled)
1290{
1291 if (m_instance_name != InstanceSettings::GetDefaultName())
1292 {
1293 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1294 CopyInstanceSettings (pending_settings,false);
1295 m_owner.RemovePendingSettings (m_instance_name);
1296 }
1297 if (rhs.m_avoid_regexp_ap.get() != NULL)
1298 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1299}
1300
1301ThreadInstanceSettings::~ThreadInstanceSettings ()
1302{
1303}
1304
1305ThreadInstanceSettings&
1306ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1307{
1308 if (this != &rhs)
1309 {
1310 if (rhs.m_avoid_regexp_ap.get() != NULL)
1311 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1312 else
1313 m_avoid_regexp_ap.reset(NULL);
1314 }
1315 m_trace_enabled = rhs.m_trace_enabled;
1316 return *this;
1317}
1318
1319
1320void
1321ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1322 const char *index_value,
1323 const char *value,
1324 const ConstString &instance_name,
1325 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001326 VarSetOperationType op,
Jim Ingham745ac7a2010-11-11 19:26:09 +00001327 Error &err,
1328 bool pending)
1329{
1330 if (var_name == StepAvoidRegexpVarName())
1331 {
1332 std::string regexp_text;
1333 if (m_avoid_regexp_ap.get() != NULL)
1334 regexp_text.append (m_avoid_regexp_ap->GetText());
1335 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1336 if (regexp_text.empty())
1337 m_avoid_regexp_ap.reset();
1338 else
1339 {
1340 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1341
1342 }
1343 }
1344 else if (var_name == GetTraceThreadVarName())
1345 {
1346 bool success;
1347 bool result = Args::StringToBoolean(value, false, &success);
1348
1349 if (success)
1350 {
1351 m_trace_enabled = result;
1352 if (!pending)
1353 {
1354 Thread *myself = static_cast<Thread *> (this);
1355 myself->EnableTracer(m_trace_enabled, true);
1356 }
1357 }
1358 else
1359 {
1360 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1361 }
1362
1363 }
1364}
1365
1366void
1367ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1368 bool pending)
1369{
1370 if (new_settings.get() == NULL)
1371 return;
1372
1373 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1374 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1375 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1376 else
1377 m_avoid_regexp_ap.reset ();
1378}
1379
1380bool
1381ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1382 const ConstString &var_name,
1383 StringList &value,
1384 Error *err)
1385{
1386 if (var_name == StepAvoidRegexpVarName())
1387 {
1388 if (m_avoid_regexp_ap.get() != NULL)
1389 {
1390 std::string regexp_text("\"");
1391 regexp_text.append(m_avoid_regexp_ap->GetText());
1392 regexp_text.append ("\"");
1393 value.AppendString (regexp_text.c_str());
1394 }
1395
1396 }
1397 else if (var_name == GetTraceThreadVarName())
1398 {
1399 value.AppendString(m_trace_enabled ? "true" : "false");
1400 }
1401 else
1402 {
1403 if (err)
1404 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1405 return false;
1406 }
1407 return true;
1408}
1409
1410const ConstString
1411ThreadInstanceSettings::CreateInstanceName ()
1412{
1413 static int instance_count = 1;
1414 StreamString sstr;
1415
1416 sstr.Printf ("thread_%d", instance_count);
1417 ++instance_count;
1418
1419 const ConstString ret_val (sstr.GetData());
1420 return ret_val;
1421}
1422
1423const ConstString &
1424ThreadInstanceSettings::StepAvoidRegexpVarName ()
1425{
1426 static ConstString step_avoid_var_name ("step-avoid-regexp");
1427
1428 return step_avoid_var_name;
1429}
1430
1431const ConstString &
1432ThreadInstanceSettings::GetTraceThreadVarName ()
1433{
1434 static ConstString trace_thread_var_name ("trace-thread");
1435
1436 return trace_thread_var_name;
1437}
1438
1439//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001440// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001441//--------------------------------------------------
1442
1443SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001444Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001445{
1446 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1447 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1448};
1449
1450
1451SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001452Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001453{
1454 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1455 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1456 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1457 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1458};