blob: 9429bfdb623b00db1fe7157a261f8693314bbc90 [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
Greg Claytonf4124de2012-02-21 00:09:25 +000046Thread::Thread (const ProcessSP &process_sp, lldb::tid_t tid) :
Chris Lattner24943d22010-06-08 16:52:24 +000047 UserID (tid),
Greg Clayton334d33a2012-01-30 07:41:31 +000048 ThreadInstanceSettings (GetSettingsController()),
Greg Claytonf4124de2012-02-21 00:09:25 +000049 m_process_wp (process_sp),
Greg Clayton643ee732010-08-04 01:40:35 +000050 m_actual_stop_info_sp (),
Greg Claytonf4124de2012-02-21 00:09:25 +000051 m_index_id (process_sp->GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +000052 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 Clayton2450cb12012-03-29 01:41:38 +000057 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +000058 m_curr_frames_sp (),
59 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000060 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +000061 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +000062 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +000063 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000064 m_destroy_called (false),
65 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +000066
Chris Lattner24943d22010-06-08 16:52:24 +000067{
Greg Claytone005f2c2010-11-06 01:53:30 +000068 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000069 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +000070 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +000071
72 QueueFundamentalPlan(true);
Caroline Tice1ebef442010-09-27 00:30:10 +000073 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +000074}
75
76
77Thread::~Thread()
78{
Greg Claytone005f2c2010-11-06 01:53:30 +000079 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000080 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +000081 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000082 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
83 assert (m_destroy_called);
84}
85
86void
87Thread::DestroyThread ()
88{
89 m_plan_stack.clear();
90 m_discarded_plan_stack.clear();
91 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +000092 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +000093 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +000094}
95
Jim Ingham6297a3a2010-10-20 00:39:53 +000096lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +000097Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +000098{
Jim Ingham6297a3a2010-10-20 00:39:53 +000099 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000100 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000101 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000102 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000103 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000104 ProcessSP process_sp (GetProcess());
105 if (process_sp
106 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000107 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000108 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000109 return m_actual_stop_info_sp;
110 else
111 return GetPrivateStopReason ();
112 }
Chris Lattner24943d22010-06-08 16:52:24 +0000113}
114
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000115void
116Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
117{
118 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000119 if (m_actual_stop_info_sp)
120 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000121 ProcessSP process_sp (GetProcess());
122 if (process_sp)
123 m_thread_stop_reason_stop_id = process_sp->GetStopID();
124 else
125 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000126}
127
128void
129Thread::SetStopInfoToNothing()
130{
131 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
132 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
133 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
134}
135
Chris Lattner24943d22010-06-08 16:52:24 +0000136bool
137Thread::ThreadStoppedForAReason (void)
138{
Greg Clayton643ee732010-08-04 01:40:35 +0000139 return GetPrivateStopReason () != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000140}
141
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000142bool
143Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
144{
145 if (!SaveFrameZeroState(saved_state.register_backup))
146 return false;
147
148 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000149 ProcessSP process_sp (GetProcess());
150 if (process_sp)
151 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000152 return true;
153}
154
155bool
156Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
157{
158 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000159 if (saved_state.stop_info_sp)
160 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000161 SetStopInfo(saved_state.stop_info_sp);
162 return true;
163}
164
Chris Lattner24943d22010-06-08 16:52:24 +0000165StateType
166Thread::GetState() const
167{
168 // If any other threads access this we will need a mutex for it
169 Mutex::Locker locker(m_state_mutex);
170 return m_state;
171}
172
173void
174Thread::SetState(StateType state)
175{
176 Mutex::Locker locker(m_state_mutex);
177 m_state = state;
178}
179
180void
181Thread::WillStop()
182{
183 ThreadPlan *current_plan = GetCurrentPlan();
184
185 // FIXME: I may decide to disallow threads with no plans. In which
186 // case this should go to an assert.
187
188 if (!current_plan)
189 return;
190
191 current_plan->WillStop();
192}
193
194void
195Thread::SetupForResume ()
196{
197 if (GetResumeState() != eStateSuspended)
198 {
199
200 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
201 // telling the current plan it will resume, since we might change what the current
202 // plan is.
203
204 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000205 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000206 if (bp_site_sp && bp_site_sp->IsEnabled())
207 {
208 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
209 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000210
211 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000212
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000213 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
214 {
215 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
216 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000217 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000218 ThreadPlanSP step_bp_plan_sp;
219 step_bp_plan->SetPrivate (true);
220
Chris Lattner24943d22010-06-08 16:52:24 +0000221 if (GetCurrentPlan()->RunState() != eStateStepping)
222 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000223 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000224 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000225 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000226 QueueThreadPlan (step_bp_plan_sp, false);
227 }
228 }
229 }
230 }
231}
232
233bool
234Thread::WillResume (StateType resume_state)
235{
236 // At this point clear the completed plan stack.
237 m_completed_plan_stack.clear();
238 m_discarded_plan_stack.clear();
239
Jim Ingham149d1f52012-01-31 23:09:20 +0000240 SetTemporaryResumeState(resume_state);
241
242 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
243 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
244 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
245 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000246 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000247 if (m_thread_stop_reason_stop_id == process_stop_id &&
248 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
249 {
250 StopInfo *stop_info = GetPrivateStopReason().get();
251 if (stop_info)
252 stop_info->WillResume (resume_state);
253 }
Chris Lattner24943d22010-06-08 16:52:24 +0000254
255 // Tell all the plans that we are about to resume in case they need to clear any state.
256 // We distinguish between the plan on the top of the stack and the lower
257 // plans in case a plan needs to do any special business before it runs.
258
259 ThreadPlan *plan_ptr = GetCurrentPlan();
260 plan_ptr->WillResume(resume_state, true);
261
262 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
263 {
264 plan_ptr->WillResume (resume_state, false);
265 }
Greg Clayton643ee732010-08-04 01:40:35 +0000266
Greg Clayton643ee732010-08-04 01:40:35 +0000267 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000268 return true;
269}
270
271void
272Thread::DidResume ()
273{
274 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
275}
276
277bool
278Thread::ShouldStop (Event* event_ptr)
279{
280 ThreadPlan *current_plan = GetCurrentPlan();
281 bool should_stop = true;
282
Greg Claytone005f2c2010-11-06 01:53:30 +0000283 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000284
285 if (GetResumeState () == eStateSuspended)
286 {
287 if (log)
288 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
289 __FUNCTION__,
290 GetID ());
291// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
292// __FUNCTION__,
293// GetID (),
294// GetRegisterContext()->GetPC());
295 return false;
296 }
297
298 if (GetTemporaryResumeState () == eStateSuspended)
299 {
300 if (log)
301 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
302 __FUNCTION__,
303 GetID ());
304// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
305// __FUNCTION__,
306// GetID (),
307// GetRegisterContext()->GetPC());
308 return false;
309 }
310
311 if (ThreadStoppedForAReason() == false)
312 {
313 if (log)
314 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
315 __FUNCTION__,
316 GetID (),
317 GetRegisterContext()->GetPC());
318 return false;
319 }
320
Chris Lattner24943d22010-06-08 16:52:24 +0000321 if (log)
322 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000323 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
324 __FUNCTION__,
325 GetID (),
326 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000327 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000328 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000329 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000330 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000331 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000332 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000333
334 // The top most plan always gets to do the trace log...
335 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000336
337 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
338 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
339 // do any more work on this stop.
340 StopInfoSP private_stop_info (GetPrivateStopReason());
341 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
342 {
343 if (log)
344 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
345 return false;
346 }
Chris Lattner24943d22010-06-08 16:52:24 +0000347
Jim Inghamad382c52011-12-03 01:52:59 +0000348 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
349 // If that plan is still working, then we don't need to do any more work. If the plan that explains
350 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
351 // whether they still need to do more work.
352
353 bool done_processing_current_plan = false;
354
355 if (!current_plan->PlanExplainsStop())
356 {
357 if (current_plan->TracerExplainsStop())
358 {
359 done_processing_current_plan = true;
360 should_stop = false;
361 }
362 else
363 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000364 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000365 // does and let it handle the situation.
366 ThreadPlan *plan_ptr = current_plan;
367 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
368 {
369 if (plan_ptr->PlanExplainsStop())
370 {
371 should_stop = plan_ptr->ShouldStop (event_ptr);
372
373 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
374 // and all the plans below it off the stack.
375
376 if (plan_ptr->MischiefManaged())
377 {
378 // We're going to pop the plans up to AND INCLUDING the plan that explains the stop.
379 plan_ptr = GetPreviousPlan(plan_ptr);
380
381 do
382 {
383 if (should_stop)
384 current_plan->WillStop();
385 PopPlan();
386 }
387 while ((current_plan = GetCurrentPlan()) != plan_ptr);
388 done_processing_current_plan = false;
389 }
390 else
391 done_processing_current_plan = true;
392
393 break;
394 }
395
396 }
397 }
398 }
399
400 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000401 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000402 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000403
Jim Inghame8f4e112011-10-15 00:23:43 +0000404 if (log)
405 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
406
Jim Inghamf9f40c22011-02-08 05:20:59 +0000407 // We're starting from the base plan, so just let it decide;
408 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000409 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000410 should_stop = current_plan->ShouldStop (event_ptr);
411 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000412 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000413 }
414 else
415 {
416 // Otherwise, don't let the base plan override what the other plans say to do, since
417 // presumably if there were other plans they would know what to do...
418 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000419 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000420 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000421 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000422
423 should_stop = current_plan->ShouldStop(event_ptr);
424 if (log)
425 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
426 if (current_plan->MischiefManaged())
427 {
428 if (should_stop)
429 current_plan->WillStop();
430
431 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
432 // Otherwise, see if the plan's parent wants to stop.
433
434 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
435 {
436 PopPlan();
437 break;
438 }
439 else
440 {
441
442 PopPlan();
443
444 current_plan = GetCurrentPlan();
445 if (current_plan == NULL)
446 {
447 break;
448 }
449 }
450
Chris Lattner24943d22010-06-08 16:52:24 +0000451 }
452 else
453 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000454 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000455 }
Chris Lattner24943d22010-06-08 16:52:24 +0000456 }
457 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000458
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000459 if (over_ride_stop)
460 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000461
462 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
463 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
464 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
465 // This code clears stale plans off the stack.
466
467 if (should_stop)
468 {
469 ThreadPlan *plan_ptr = GetCurrentPlan();
470 while (!PlanIsBasePlan(plan_ptr))
471 {
472 bool stale = plan_ptr->IsPlanStale ();
473 ThreadPlan *examined_plan = plan_ptr;
474 plan_ptr = GetPreviousPlan (examined_plan);
475
476 if (stale)
477 {
478 if (log)
479 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
480 DiscardThreadPlansUpToPlan(examined_plan);
481 }
482 }
483 }
484
Chris Lattner24943d22010-06-08 16:52:24 +0000485 }
Chris Lattner24943d22010-06-08 16:52:24 +0000486
Jim Inghame8f4e112011-10-15 00:23:43 +0000487 if (log)
488 {
489 StreamString s;
490 s.IndentMore();
491 DumpThreadPlans(&s);
492 log->Printf ("Plan stack final state:\n%s", s.GetData());
493 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
494 }
Chris Lattner24943d22010-06-08 16:52:24 +0000495 return should_stop;
496}
497
498Vote
499Thread::ShouldReportStop (Event* event_ptr)
500{
501 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000502 StateType temp_thread_state = GetTemporaryResumeState();
503
Greg Claytone005f2c2010-11-06 01:53:30 +0000504 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000505
506 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
507 {
508 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000509 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 +0000510 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000511 }
Chris Lattner24943d22010-06-08 16:52:24 +0000512
Jim Ingham149d1f52012-01-31 23:09:20 +0000513 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
514 {
515 if (log)
516 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
517 return eVoteNoOpinion;
518 }
519
520 if (!ThreadStoppedForAReason())
521 {
522 if (log)
523 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
524 return eVoteNoOpinion;
525 }
526
Chris Lattner24943d22010-06-08 16:52:24 +0000527 if (m_completed_plan_stack.size() > 0)
528 {
529 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000530 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000531 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 +0000532 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
533 }
534 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000535 {
536 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000537 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000538 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000539 }
Chris Lattner24943d22010-06-08 16:52:24 +0000540}
541
542Vote
543Thread::ShouldReportRun (Event* event_ptr)
544{
545 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000546
Chris Lattner24943d22010-06-08 16:52:24 +0000547 if (thread_state == eStateSuspended
548 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000549 {
Chris Lattner24943d22010-06-08 16:52:24 +0000550 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000551 }
552
553 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000554 if (m_completed_plan_stack.size() > 0)
555 {
556 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000557 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000558 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 +0000559 GetIndexID(),
560 GetID(),
561 m_completed_plan_stack.back()->GetName());
562
Chris Lattner24943d22010-06-08 16:52:24 +0000563 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
564 }
565 else
Jim Inghamac959662011-01-24 06:34:17 +0000566 {
567 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000568 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 +0000569 GetIndexID(),
570 GetID(),
571 GetCurrentPlan()->GetName());
572
Chris Lattner24943d22010-06-08 16:52:24 +0000573 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000574 }
Chris Lattner24943d22010-06-08 16:52:24 +0000575}
576
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000577bool
578Thread::MatchesSpec (const ThreadSpec *spec)
579{
580 if (spec == NULL)
581 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000582
Jim Inghama2664912012-03-07 22:03:04 +0000583 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000584}
585
Chris Lattner24943d22010-06-08 16:52:24 +0000586void
587Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
588{
589 if (thread_plan_sp)
590 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000591 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
592 if (!thread_plan_sp->GetThreadPlanTracer())
593 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000594 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000595
Chris Lattner24943d22010-06-08 16:52:24 +0000596 thread_plan_sp->DidPush();
597
Greg Claytone005f2c2010-11-06 01:53:30 +0000598 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000599 if (log)
600 {
601 StreamString s;
602 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000603 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000604 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000605 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000606 }
607 }
608}
609
610void
611Thread::PopPlan ()
612{
Greg Claytone005f2c2010-11-06 01:53:30 +0000613 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000614
Jim Ingham6297a3a2010-10-20 00:39:53 +0000615 if (m_plan_stack.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000616 return;
617 else
618 {
619 ThreadPlanSP &plan = m_plan_stack.back();
620 if (log)
621 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000622 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000623 }
624 m_completed_plan_stack.push_back (plan);
625 plan->WillPop();
626 m_plan_stack.pop_back();
627 }
628}
629
630void
631Thread::DiscardPlan ()
632{
633 if (m_plan_stack.size() > 1)
634 {
635 ThreadPlanSP &plan = m_plan_stack.back();
636 m_discarded_plan_stack.push_back (plan);
637 plan->WillPop();
638 m_plan_stack.pop_back();
639 }
640}
641
642ThreadPlan *
643Thread::GetCurrentPlan ()
644{
Jim Ingham2f41d272012-04-19 00:17:05 +0000645 // There will always be at least the base plan. If somebody is mucking with a
646 // thread with an empty plan stack, we should assert right away.
647 assert (!m_plan_stack.empty());
648
649 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000650}
651
652ThreadPlanSP
653Thread::GetCompletedPlan ()
654{
655 ThreadPlanSP empty_plan_sp;
656 if (!m_completed_plan_stack.empty())
657 {
658 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
659 {
660 ThreadPlanSP completed_plan_sp;
661 completed_plan_sp = m_completed_plan_stack[i];
662 if (!completed_plan_sp->GetPrivate ())
663 return completed_plan_sp;
664 }
665 }
666 return empty_plan_sp;
667}
668
Jim Ingham1586d972011-12-17 01:35:57 +0000669ValueObjectSP
670Thread::GetReturnValueObject ()
671{
672 if (!m_completed_plan_stack.empty())
673 {
674 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
675 {
676 ValueObjectSP return_valobj_sp;
677 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
678 if (return_valobj_sp)
679 return return_valobj_sp;
680 }
681 }
682 return ValueObjectSP();
683}
684
Chris Lattner24943d22010-06-08 16:52:24 +0000685bool
686Thread::IsThreadPlanDone (ThreadPlan *plan)
687{
Chris Lattner24943d22010-06-08 16:52:24 +0000688 if (!m_completed_plan_stack.empty())
689 {
690 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
691 {
692 if (m_completed_plan_stack[i].get() == plan)
693 return true;
694 }
695 }
696 return false;
697}
698
699bool
700Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
701{
Chris Lattner24943d22010-06-08 16:52:24 +0000702 if (!m_discarded_plan_stack.empty())
703 {
704 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
705 {
706 if (m_discarded_plan_stack[i].get() == plan)
707 return true;
708 }
709 }
710 return false;
711}
712
713ThreadPlan *
714Thread::GetPreviousPlan (ThreadPlan *current_plan)
715{
716 if (current_plan == NULL)
717 return NULL;
718
719 int stack_size = m_completed_plan_stack.size();
720 for (int i = stack_size - 1; i > 0; i--)
721 {
722 if (current_plan == m_completed_plan_stack[i].get())
723 return m_completed_plan_stack[i-1].get();
724 }
725
726 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
727 {
Chris Lattner24943d22010-06-08 16:52:24 +0000728 if (m_plan_stack.size() > 0)
729 return m_plan_stack.back().get();
730 else
731 return NULL;
732 }
733
734 stack_size = m_plan_stack.size();
735 for (int i = stack_size - 1; i > 0; i--)
736 {
737 if (current_plan == m_plan_stack[i].get())
738 return m_plan_stack[i-1].get();
739 }
740 return NULL;
741}
742
743void
744Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
745{
746 if (abort_other_plans)
747 DiscardThreadPlans(true);
748
749 PushPlan (thread_plan_sp);
750}
751
Jim Ingham745ac7a2010-11-11 19:26:09 +0000752
753void
754Thread::EnableTracer (bool value, bool single_stepping)
755{
756 int stack_size = m_plan_stack.size();
757 for (int i = 0; i < stack_size; i++)
758 {
759 if (m_plan_stack[i]->GetThreadPlanTracer())
760 {
761 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
762 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
763 }
764 }
765}
766
767void
768Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
769{
770 int stack_size = m_plan_stack.size();
771 for (int i = 0; i < stack_size; i++)
772 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
773}
774
Chris Lattner24943d22010-06-08 16:52:24 +0000775void
Jim Inghamea9d4262010-11-05 19:25:48 +0000776Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
777{
Jim Ingham88e3de22012-05-03 21:19:36 +0000778 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
779}
780
781void
782Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
783{
Greg Claytone005f2c2010-11-06 01:53:30 +0000784 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000785 if (log)
786 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000787 log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_ptr);
Jim Inghamea9d4262010-11-05 19:25:48 +0000788 }
789
790 int stack_size = m_plan_stack.size();
791
792 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
793 // stack, and if so discard up to and including it.
794
Jim Ingham88e3de22012-05-03 21:19:36 +0000795 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000796 {
797 for (int i = stack_size - 1; i > 0; i--)
798 DiscardPlan();
799 }
800 else
801 {
802 bool found_it = false;
803 for (int i = stack_size - 1; i > 0; i--)
804 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000805 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000806 found_it = true;
807 }
808 if (found_it)
809 {
810 bool last_one = false;
811 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
812 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000813 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000814 last_one = true;
815 DiscardPlan();
816 }
817 }
818 }
819 return;
820}
821
822void
Chris Lattner24943d22010-06-08 16:52:24 +0000823Thread::DiscardThreadPlans(bool force)
824{
Greg Claytone005f2c2010-11-06 01:53:30 +0000825 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000826 if (log)
827 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000828 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000829 }
830
831 if (force)
832 {
833 int stack_size = m_plan_stack.size();
834 for (int i = stack_size - 1; i > 0; i--)
835 {
836 DiscardPlan();
837 }
838 return;
839 }
840
841 while (1)
842 {
843
844 int master_plan_idx;
845 bool discard;
846
847 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
848 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
849 {
850 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
851 {
852 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
853 break;
854 }
855 }
856
857 if (discard)
858 {
859 // First pop all the dependent plans:
860 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
861 {
862
863 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
864 // for the plan leaves it in a state that it is safe to pop the plan
865 // with no more notice?
866 DiscardPlan();
867 }
868
869 // Now discard the master plan itself.
870 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
871 // discard it's dependent plans, but not it...
872 if (master_plan_idx > 0)
873 {
874 DiscardPlan();
875 }
876 }
877 else
878 {
879 // If the master plan doesn't want to get discarded, then we're done.
880 break;
881 }
882
883 }
Chris Lattner24943d22010-06-08 16:52:24 +0000884}
885
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000886bool
887Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
888{
889 if (plan_ptr->IsBasePlan())
890 return true;
891 else if (m_plan_stack.size() == 0)
892 return false;
893 else
894 return m_plan_stack[0].get() == plan_ptr;
895}
896
Chris Lattner24943d22010-06-08 16:52:24 +0000897ThreadPlan *
898Thread::QueueFundamentalPlan (bool abort_other_plans)
899{
900 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
901 QueueThreadPlan (thread_plan_sp, abort_other_plans);
902 return thread_plan_sp.get();
903}
904
905ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000906Thread::QueueThreadPlanForStepSingleInstruction
907(
908 bool step_over,
909 bool abort_other_plans,
910 bool stop_other_threads
911)
Chris Lattner24943d22010-06-08 16:52:24 +0000912{
913 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
914 QueueThreadPlan (thread_plan_sp, abort_other_plans);
915 return thread_plan_sp.get();
916}
917
918ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000919Thread::QueueThreadPlanForStepRange
920(
921 bool abort_other_plans,
922 StepType type,
923 const AddressRange &range,
924 const SymbolContext &addr_context,
925 lldb::RunMode stop_other_threads,
926 bool avoid_code_without_debug_info
927)
Chris Lattner24943d22010-06-08 16:52:24 +0000928{
929 ThreadPlanSP thread_plan_sp;
930 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +0000931 {
932 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
933 if (avoid_code_without_debug_info)
934 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
935 else
936 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
937 thread_plan_sp.reset (plan);
938 }
Chris Lattner24943d22010-06-08 16:52:24 +0000939 else
940 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
941
942 QueueThreadPlan (thread_plan_sp, abort_other_plans);
943 return thread_plan_sp.get();
944}
945
946
947ThreadPlan *
948Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
949{
950 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
951 QueueThreadPlan (thread_plan_sp, abort_other_plans);
952 return thread_plan_sp.get();
953}
954
955ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000956Thread::QueueThreadPlanForStepOut
957(
958 bool abort_other_plans,
959 SymbolContext *addr_context,
960 bool first_insn,
961 bool stop_other_threads,
962 Vote stop_vote,
963 Vote run_vote,
964 uint32_t frame_idx
965)
Chris Lattner24943d22010-06-08 16:52:24 +0000966{
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000967 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
968 addr_context,
969 first_insn,
970 stop_other_threads,
971 stop_vote,
972 run_vote,
973 frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000974 QueueThreadPlan (thread_plan_sp, abort_other_plans);
975 return thread_plan_sp.get();
976}
977
978ThreadPlan *
979Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
980{
Jim Inghamad382c52011-12-03 01:52:59 +0000981 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, stop_other_threads));
982 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
983 return NULL;
984
Chris Lattner24943d22010-06-08 16:52:24 +0000985 QueueThreadPlan (thread_plan_sp, abort_other_plans);
986 return thread_plan_sp.get();
987}
988
989ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +0000990Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
991 Address& function,
992 lldb::addr_t arg,
993 bool stop_other_threads,
994 bool discard_on_error)
995{
Jim Ingham016ef882011-12-22 19:12:40 +0000996 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +0000997 QueueThreadPlan (thread_plan_sp, abort_other_plans);
998 return thread_plan_sp.get();
999}
1000
1001ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001002Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1003 Address &target_addr,
1004 bool stop_other_threads)
1005{
1006 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1007 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1008 return thread_plan_sp.get();
1009}
1010
1011ThreadPlan *
1012Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001013 lldb::addr_t *address_list,
1014 size_t num_addresses,
1015 bool stop_other_threads,
1016 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001017{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001018 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001019 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1020 return thread_plan_sp.get();
1021
1022}
1023
1024uint32_t
1025Thread::GetIndexID () const
1026{
1027 return m_index_id;
1028}
1029
1030void
1031Thread::DumpThreadPlans (lldb_private::Stream *s) const
1032{
1033 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001034 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001035 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001036 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 +00001037 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001038 {
Chris Lattner24943d22010-06-08 16:52:24 +00001039 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001040 s->Indent();
1041 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001042 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001043 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001044 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001045 }
1046
Chris Lattner24943d22010-06-08 16:52:24 +00001047 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001048 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001049 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001050 s->Indent();
1051 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1052 for (i = stack_size - 1; i >= 0; i--)
1053 {
1054 s->IndentMore();
1055 s->Indent();
1056 s->Printf ("Element %d: ", i);
1057 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1058 s->EOL();
1059 s->IndentLess();
1060 }
Chris Lattner24943d22010-06-08 16:52:24 +00001061 }
1062
1063 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001064 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001065 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001066 s->Indent();
1067 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1068 for (i = stack_size - 1; i >= 0; i--)
1069 {
1070 s->IndentMore();
1071 s->Indent();
1072 s->Printf ("Element %d: ", i);
1073 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1074 s->EOL();
1075 s->IndentLess();
1076 }
Chris Lattner24943d22010-06-08 16:52:24 +00001077 }
1078
1079}
1080
Greg Clayton289afcb2012-02-18 05:35:26 +00001081TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001082Thread::CalculateTarget ()
1083{
Greg Claytonf4124de2012-02-21 00:09:25 +00001084 TargetSP target_sp;
1085 ProcessSP process_sp(GetProcess());
1086 if (process_sp)
1087 target_sp = process_sp->CalculateTarget();
1088 return target_sp;
1089
Chris Lattner24943d22010-06-08 16:52:24 +00001090}
1091
Greg Clayton289afcb2012-02-18 05:35:26 +00001092ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001093Thread::CalculateProcess ()
1094{
Greg Claytonf4124de2012-02-21 00:09:25 +00001095 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001096}
1097
Greg Clayton289afcb2012-02-18 05:35:26 +00001098ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001099Thread::CalculateThread ()
1100{
Greg Clayton289afcb2012-02-18 05:35:26 +00001101 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001102}
1103
Greg Clayton289afcb2012-02-18 05:35:26 +00001104StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001105Thread::CalculateStackFrame ()
1106{
Greg Clayton289afcb2012-02-18 05:35:26 +00001107 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001108}
1109
1110void
Greg Claytona830adb2010-10-04 01:05:56 +00001111Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001112{
Greg Claytonf4124de2012-02-21 00:09:25 +00001113 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001114}
1115
Greg Clayton782b9cc2010-08-25 00:35:26 +00001116
Greg Clayton2450cb12012-03-29 01:41:38 +00001117StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001118Thread::GetStackFrameList ()
1119{
Greg Clayton2450cb12012-03-29 01:41:38 +00001120 StackFrameListSP frame_list_sp;
1121 Mutex::Locker locker(m_frame_mutex);
1122 if (m_curr_frames_sp)
1123 {
1124 frame_list_sp = m_curr_frames_sp;
1125 }
1126 else
1127 {
1128 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1129 m_curr_frames_sp = frame_list_sp;
1130 }
1131 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001132}
1133
Greg Clayton782b9cc2010-08-25 00:35:26 +00001134void
1135Thread::ClearStackFrames ()
1136{
Greg Clayton2450cb12012-03-29 01:41:38 +00001137 Mutex::Locker locker(m_frame_mutex);
1138
Jim Inghambf97d742012-02-29 03:40:22 +00001139 // Only store away the old "reference" StackFrameList if we got all its frames:
1140 // FIXME: At some point we can try to splice in the frames we have fetched into
1141 // the new frame as we make it, but let's not try that now.
1142 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001143 m_prev_frames_sp.swap (m_curr_frames_sp);
1144 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001145}
1146
1147lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001148Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1149{
Greg Clayton2450cb12012-03-29 01:41:38 +00001150 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001151}
1152
Chris Lattner24943d22010-06-08 16:52:24 +00001153void
Greg Claytona830adb2010-10-04 01:05:56 +00001154Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001155{
Greg Claytonf4124de2012-02-21 00:09:25 +00001156 ExecutionContext exe_ctx (shared_from_this());
1157 Process *process = exe_ctx.GetProcessPtr();
1158 if (process == NULL)
1159 return;
1160
Greg Clayton567e7f32011-09-22 04:58:26 +00001161 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001162 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001163 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001164 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001165 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001166 if (frame_sp)
1167 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001168 exe_ctx.SetFrameSP(frame_sp);
1169 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001170 }
1171 }
1172
Greg Claytonf4124de2012-02-21 00:09:25 +00001173 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001174 assert (thread_format);
1175 const char *end = NULL;
1176 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001177 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001178 &exe_ctx,
1179 NULL,
1180 strm,
1181 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001182}
1183
Greg Clayton990de7b2010-11-18 23:32:35 +00001184void
Caroline Tice2a456812011-03-10 22:14:10 +00001185Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001186{
Greg Clayton334d33a2012-01-30 07:41:31 +00001187 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001188 SettingsController::global_settings_table,
1189 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001190
1191 // Now call SettingsInitialize() on each 'child' setting of Thread.
1192 // Currently there are none.
Greg Clayton990de7b2010-11-18 23:32:35 +00001193}
Jim Ingham20594b12010-09-08 03:14:33 +00001194
Greg Clayton990de7b2010-11-18 23:32:35 +00001195void
Caroline Tice2a456812011-03-10 22:14:10 +00001196Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001197{
Caroline Tice2a456812011-03-10 22:14:10 +00001198 // Must call SettingsTerminate() on each 'child' setting of Thread before terminating Thread settings.
1199 // Currently there are none.
1200
1201 // Now terminate Thread Settings.
1202
Greg Clayton990de7b2010-11-18 23:32:35 +00001203 UserSettingsControllerSP &usc = GetSettingsController();
1204 UserSettingsController::FinalizeSettingsController (usc);
1205 usc.reset();
1206}
Jim Ingham20594b12010-09-08 03:14:33 +00001207
Greg Clayton990de7b2010-11-18 23:32:35 +00001208UserSettingsControllerSP &
1209Thread::GetSettingsController ()
1210{
Greg Clayton334d33a2012-01-30 07:41:31 +00001211 static UserSettingsControllerSP g_settings_controller_sp;
1212 if (!g_settings_controller_sp)
1213 {
1214 g_settings_controller_sp.reset (new Thread::SettingsController);
1215 // The first shared pointer to Target::SettingsController in
1216 // g_settings_controller_sp must be fully created above so that
1217 // the TargetInstanceSettings can use a weak_ptr to refer back
1218 // to the master setttings controller
1219 InstanceSettingsSP default_instance_settings_sp (new ThreadInstanceSettings (g_settings_controller_sp,
1220 false,
1221 InstanceSettings::GetDefaultName().AsCString()));
1222
1223 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1224 }
1225 return g_settings_controller_sp;
Jim Ingham20594b12010-09-08 03:14:33 +00001226}
1227
Caroline Tice1ebef442010-09-27 00:30:10 +00001228void
1229Thread::UpdateInstanceName ()
1230{
1231 StreamString sstr;
1232 const char *name = GetName();
1233
1234 if (name && name[0] != '\0')
1235 sstr.Printf ("%s", name);
1236 else if ((GetIndexID() != 0) || (GetID() != 0))
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001237 sstr.Printf ("0x%4.4x", GetIndexID());
Caroline Tice1ebef442010-09-27 00:30:10 +00001238
1239 if (sstr.GetSize() > 0)
1240 Thread::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
1241}
1242
Jim Inghamccd584d2010-09-23 17:40:12 +00001243lldb::StackFrameSP
1244Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1245{
Greg Clayton2450cb12012-03-29 01:41:38 +00001246 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001247}
Caroline Tice7826c882010-10-26 03:11:13 +00001248
1249const char *
1250Thread::StopReasonAsCString (lldb::StopReason reason)
1251{
1252 switch (reason)
1253 {
1254 case eStopReasonInvalid: return "invalid";
1255 case eStopReasonNone: return "none";
1256 case eStopReasonTrace: return "trace";
1257 case eStopReasonBreakpoint: return "breakpoint";
1258 case eStopReasonWatchpoint: return "watchpoint";
1259 case eStopReasonSignal: return "signal";
1260 case eStopReasonException: return "exception";
1261 case eStopReasonPlanComplete: return "plan complete";
1262 }
1263
1264
1265 static char unknown_state_string[64];
1266 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1267 return unknown_state_string;
1268}
1269
1270const char *
1271Thread::RunModeAsCString (lldb::RunMode mode)
1272{
1273 switch (mode)
1274 {
1275 case eOnlyThisThread: return "only this thread";
1276 case eAllThreads: return "all threads";
1277 case eOnlyDuringStepping: return "only during stepping";
1278 }
1279
1280 static char unknown_state_string[64];
1281 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1282 return unknown_state_string;
1283}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001284
Greg Claytonabe0fed2011-04-18 08:33:37 +00001285size_t
1286Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1287{
Greg Claytonf4124de2012-02-21 00:09:25 +00001288 ExecutionContext exe_ctx (shared_from_this());
1289 Target *target = exe_ctx.GetTargetPtr();
1290 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001291 size_t num_frames_shown = 0;
1292 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001293 bool is_selected = false;
1294 if (process)
1295 {
1296 if (process->GetThreadList().GetSelectedThread().get() == this)
1297 is_selected = true;
1298 }
1299 strm.Printf("%c ", is_selected ? '*' : ' ');
1300 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001301 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001302 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001303 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001304 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001305 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1306 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1307 {
1308 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1309 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001310 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001311 }
1312
1313 DumpUsingSettingsFormat (strm, start_frame);
1314
1315 if (num_frames > 0)
1316 {
1317 strm.IndentMore();
1318
1319 const bool show_frame_info = true;
1320 const uint32_t source_lines_before = 3;
1321 const uint32_t source_lines_after = 3;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001322 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001323 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1324 start_frame,
1325 num_frames,
1326 show_frame_info,
1327 num_frames_with_source,
1328 source_lines_before,
1329 source_lines_after);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001330 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001331 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001332 }
1333 return num_frames_shown;
1334}
1335
1336size_t
1337Thread::GetStackFrameStatus (Stream& strm,
1338 uint32_t first_frame,
1339 uint32_t num_frames,
1340 bool show_frame_info,
1341 uint32_t num_frames_with_source,
1342 uint32_t source_lines_before,
1343 uint32_t source_lines_after)
1344{
Greg Clayton2450cb12012-03-29 01:41:38 +00001345 return GetStackFrameList()->GetStatus (strm,
1346 first_frame,
1347 num_frames,
1348 show_frame_info,
1349 num_frames_with_source,
1350 source_lines_before,
1351 source_lines_after);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001352}
1353
Peter Collingbournee426d852011-06-03 20:40:54 +00001354bool
1355Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1356{
1357 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1358 if (frame_sp)
1359 {
1360 checkpoint.SetStackID(frame_sp->GetStackID());
1361 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1362 }
1363 return false;
1364}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001365
Peter Collingbournee426d852011-06-03 20:40:54 +00001366bool
1367Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1368{
1369 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1370 if (frame_sp)
1371 {
1372 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1373
1374 // Clear out all stack frames as our world just changed.
1375 ClearStackFrames();
1376 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1377
1378 return ret;
1379 }
1380 return false;
1381}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001382
Greg Clayton37f962e2011-08-22 02:49:39 +00001383Unwind *
1384Thread::GetUnwinder ()
1385{
1386 if (m_unwinder_ap.get() == NULL)
1387 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001388 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001389 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1390 switch (machine)
1391 {
1392 case llvm::Triple::x86_64:
1393 case llvm::Triple::x86:
1394 case llvm::Triple::arm:
1395 case llvm::Triple::thumb:
1396 m_unwinder_ap.reset (new UnwindLLDB (*this));
1397 break;
1398
1399 default:
1400 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1401 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1402 break;
1403 }
1404 }
1405 return m_unwinder_ap.get();
1406}
1407
1408
Greg Clayton990de7b2010-11-18 23:32:35 +00001409#pragma mark "Thread::SettingsController"
Jim Ingham745ac7a2010-11-11 19:26:09 +00001410//--------------------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001411// class Thread::SettingsController
Jim Ingham745ac7a2010-11-11 19:26:09 +00001412//--------------------------------------------------------------
1413
Greg Clayton990de7b2010-11-18 23:32:35 +00001414Thread::SettingsController::SettingsController () :
Jim Ingham745ac7a2010-11-11 19:26:09 +00001415 UserSettingsController ("thread", Process::GetSettingsController())
1416{
Jim Ingham745ac7a2010-11-11 19:26:09 +00001417}
1418
Greg Clayton990de7b2010-11-18 23:32:35 +00001419Thread::SettingsController::~SettingsController ()
Jim Ingham745ac7a2010-11-11 19:26:09 +00001420{
1421}
1422
1423lldb::InstanceSettingsSP
Greg Clayton990de7b2010-11-18 23:32:35 +00001424Thread::SettingsController::CreateInstanceSettings (const char *instance_name)
Jim Ingham745ac7a2010-11-11 19:26:09 +00001425{
Greg Clayton334d33a2012-01-30 07:41:31 +00001426 lldb::InstanceSettingsSP new_settings_sp (new ThreadInstanceSettings (GetSettingsController(),
1427 false,
1428 instance_name));
Jim Ingham745ac7a2010-11-11 19:26:09 +00001429 return new_settings_sp;
1430}
1431
1432#pragma mark "ThreadInstanceSettings"
1433//--------------------------------------------------------------
1434// class ThreadInstanceSettings
1435//--------------------------------------------------------------
1436
Greg Clayton334d33a2012-01-30 07:41:31 +00001437ThreadInstanceSettings::ThreadInstanceSettings (const UserSettingsControllerSP &owner_sp, bool live_instance, const char *name) :
1438 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001439 m_avoid_regexp_ap (),
1440 m_trace_enabled (false)
1441{
1442 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1443 // until the vtables for ThreadInstanceSettings are properly set up, i.e. AFTER all the initializers.
1444 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1445 // This is true for CreateInstanceName() too.
1446
1447 if (GetInstanceName() == InstanceSettings::InvalidName())
1448 {
1449 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00001450 owner_sp->RegisterInstanceSettings (this);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001451 }
1452
1453 if (live_instance)
1454 {
Greg Clayton334d33a2012-01-30 07:41:31 +00001455 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Jim Ingham745ac7a2010-11-11 19:26:09 +00001456 }
1457}
1458
1459ThreadInstanceSettings::ThreadInstanceSettings (const ThreadInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00001460 InstanceSettings (Thread::GetSettingsController(), CreateInstanceName().AsCString()),
Jim Ingham745ac7a2010-11-11 19:26:09 +00001461 m_avoid_regexp_ap (),
1462 m_trace_enabled (rhs.m_trace_enabled)
1463{
1464 if (m_instance_name != InstanceSettings::GetDefaultName())
1465 {
Greg Clayton334d33a2012-01-30 07:41:31 +00001466 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
1467 if (owner_sp)
1468 {
1469 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name), false);
1470 owner_sp->RemovePendingSettings (m_instance_name);
1471 }
Jim Ingham745ac7a2010-11-11 19:26:09 +00001472 }
1473 if (rhs.m_avoid_regexp_ap.get() != NULL)
1474 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1475}
1476
1477ThreadInstanceSettings::~ThreadInstanceSettings ()
1478{
1479}
1480
1481ThreadInstanceSettings&
1482ThreadInstanceSettings::operator= (const ThreadInstanceSettings &rhs)
1483{
1484 if (this != &rhs)
1485 {
1486 if (rhs.m_avoid_regexp_ap.get() != NULL)
1487 m_avoid_regexp_ap.reset(new RegularExpression(rhs.m_avoid_regexp_ap->GetText()));
1488 else
1489 m_avoid_regexp_ap.reset(NULL);
1490 }
1491 m_trace_enabled = rhs.m_trace_enabled;
1492 return *this;
1493}
1494
1495
1496void
1497ThreadInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1498 const char *index_value,
1499 const char *value,
1500 const ConstString &instance_name,
1501 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001502 VarSetOperationType op,
Jim Ingham745ac7a2010-11-11 19:26:09 +00001503 Error &err,
1504 bool pending)
1505{
1506 if (var_name == StepAvoidRegexpVarName())
1507 {
1508 std::string regexp_text;
1509 if (m_avoid_regexp_ap.get() != NULL)
1510 regexp_text.append (m_avoid_regexp_ap->GetText());
1511 UserSettingsController::UpdateStringVariable (op, regexp_text, value, err);
1512 if (regexp_text.empty())
1513 m_avoid_regexp_ap.reset();
1514 else
1515 {
1516 m_avoid_regexp_ap.reset(new RegularExpression(regexp_text.c_str()));
1517
1518 }
1519 }
1520 else if (var_name == GetTraceThreadVarName())
1521 {
1522 bool success;
1523 bool result = Args::StringToBoolean(value, false, &success);
1524
1525 if (success)
1526 {
1527 m_trace_enabled = result;
1528 if (!pending)
1529 {
1530 Thread *myself = static_cast<Thread *> (this);
1531 myself->EnableTracer(m_trace_enabled, true);
1532 }
1533 }
1534 else
1535 {
1536 err.SetErrorStringWithFormat ("Bad value \"%s\" for trace-thread, should be Boolean.", value);
1537 }
1538
1539 }
1540}
1541
1542void
1543ThreadInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1544 bool pending)
1545{
1546 if (new_settings.get() == NULL)
1547 return;
1548
1549 ThreadInstanceSettings *new_process_settings = (ThreadInstanceSettings *) new_settings.get();
1550 if (new_process_settings->GetSymbolsToAvoidRegexp() != NULL)
1551 m_avoid_regexp_ap.reset (new RegularExpression (new_process_settings->GetSymbolsToAvoidRegexp()->GetText()));
1552 else
1553 m_avoid_regexp_ap.reset ();
1554}
1555
1556bool
1557ThreadInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1558 const ConstString &var_name,
1559 StringList &value,
1560 Error *err)
1561{
1562 if (var_name == StepAvoidRegexpVarName())
1563 {
1564 if (m_avoid_regexp_ap.get() != NULL)
1565 {
1566 std::string regexp_text("\"");
1567 regexp_text.append(m_avoid_regexp_ap->GetText());
1568 regexp_text.append ("\"");
1569 value.AppendString (regexp_text.c_str());
1570 }
1571
1572 }
1573 else if (var_name == GetTraceThreadVarName())
1574 {
1575 value.AppendString(m_trace_enabled ? "true" : "false");
1576 }
1577 else
1578 {
1579 if (err)
1580 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1581 return false;
1582 }
1583 return true;
1584}
1585
1586const ConstString
1587ThreadInstanceSettings::CreateInstanceName ()
1588{
1589 static int instance_count = 1;
1590 StreamString sstr;
1591
1592 sstr.Printf ("thread_%d", instance_count);
1593 ++instance_count;
1594
1595 const ConstString ret_val (sstr.GetData());
1596 return ret_val;
1597}
1598
1599const ConstString &
1600ThreadInstanceSettings::StepAvoidRegexpVarName ()
1601{
1602 static ConstString step_avoid_var_name ("step-avoid-regexp");
1603
1604 return step_avoid_var_name;
1605}
1606
1607const ConstString &
1608ThreadInstanceSettings::GetTraceThreadVarName ()
1609{
1610 static ConstString trace_thread_var_name ("trace-thread");
1611
1612 return trace_thread_var_name;
1613}
1614
1615//--------------------------------------------------
Greg Clayton990de7b2010-11-18 23:32:35 +00001616// SettingsController Variable Tables
Jim Ingham745ac7a2010-11-11 19:26:09 +00001617//--------------------------------------------------
1618
1619SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001620Thread::SettingsController::global_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001621{
1622 //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"},
1623 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1624};
1625
1626
1627SettingEntry
Greg Clayton990de7b2010-11-18 23:32:35 +00001628Thread::SettingsController::instance_settings_table[] =
Jim Ingham745ac7a2010-11-11 19:26:09 +00001629{
1630 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1631 { "step-avoid-regexp", eSetVarTypeString, "", NULL, false, false, "A regular expression defining functions step-in won't stop in." },
1632 { "trace-thread", eSetVarTypeBoolean, "false", NULL, false, false, "If true, this thread will single-step and log execution." },
1633 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1634};