blob: e761503eec153f19672b318c1ab4fe126b386984 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/Log.h"
13#include "lldb/Core/Stream.h"
14#include "lldb/Core/StreamString.h"
15#include "lldb/Host/Host.h"
16#include "lldb/Target/DynamicLoader.h"
17#include "lldb/Target/ExecutionContext.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/RegisterContext.h"
Greg Clayton7661a982010-07-23 16:45:51 +000020#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/Thread.h"
22#include "lldb/Target/ThreadPlan.h"
23#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/ThreadPlanBase.h"
25#include "lldb/Target/ThreadPlanStepInstruction.h"
26#include "lldb/Target/ThreadPlanStepOut.h"
27#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
28#include "lldb/Target/ThreadPlanStepThrough.h"
29#include "lldb/Target/ThreadPlanStepInRange.h"
30#include "lldb/Target/ThreadPlanStepOverRange.h"
31#include "lldb/Target/ThreadPlanRunToAddress.h"
32#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000033#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034
35using namespace lldb;
36using namespace lldb_private;
37
38Thread::Thread (Process &process, lldb::tid_t tid) :
39 UserID (tid),
Benjamin Kramer36a08102010-07-16 12:32:33 +000040 m_process (process),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_index_id (process.GetNextThreadIndexID ()),
42 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000043 m_state (eStateUnloaded),
44 m_plan_stack (),
45 m_immediate_plan_stack(),
46 m_completed_plan_stack(),
47 m_state_mutex (Mutex::eMutexTypeRecursive),
48 m_frames (),
49 m_current_frame_idx (0),
50 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
51 m_resume_state (eStateRunning)
52{
53 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
54 if (log)
55 log->Printf ("%p Thread::Thread(tid = 0x%4.4x)", this, GetID());
56
57 QueueFundamentalPlan(true);
58}
59
60
61Thread::~Thread()
62{
63 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
64 if (log)
65 log->Printf ("%p Thread::~Thread(tid = 0x%4.4x)", this, GetID());
66}
67
68int
69Thread::GetResumeSignal () const
70{
71 return m_resume_signal;
72}
73
74void
75Thread::SetResumeSignal (int signal)
76{
77 m_resume_signal = signal;
78}
79
80StateType
81Thread::GetResumeState () const
82{
83 return m_resume_state;
84}
85
86void
87Thread::SetResumeState (StateType state)
88{
89 m_resume_state = state;
90}
91
92Thread::StopInfo::StopInfo(Thread *thread) :
93 m_reason (eStopReasonInvalid),
Chris Lattner24943d22010-06-08 16:52:24 +000094 m_thread (thread),
Benjamin Kramer36a08102010-07-16 12:32:33 +000095 m_description (),
Chris Lattner24943d22010-06-08 16:52:24 +000096 m_details ()
97{
98 m_description[0] = '\0';
99}
100
101Thread::StopInfo::~StopInfo()
102{
103}
104
105
106void
107Thread::StopInfo::Clear()
108{
109 m_reason = eStopReasonInvalid;
110 m_completed_plan_sp.reset();
111 m_description[0] = '\0';
112 ::bzero (&m_details, sizeof(m_details));
113}
114
115StopReason
116Thread::StopInfo::GetStopReason() const
117{
118 return m_reason;
119}
120
121const char *
122Thread::StopInfo::GetStopDescription() const
123{
124 if (m_description[0])
125 return m_description;
126 return NULL;
127}
128
129void
130Thread::StopInfo::SetStopDescription(const char *desc)
131{
132 if (desc && desc[0])
133 {
134 ::snprintf (m_description, sizeof(m_description), "%s", desc);
135 }
136 else
137 {
138 m_description[0] = '\0';
139 }
140}
141
142void
Greg Clayton7661a982010-07-23 16:45:51 +0000143Thread::StopInfo::SetStopReasonWithMachException
144(
145 uint32_t exc_type,
146 size_t exc_data_count,
147 const addr_t *exc_data
148)
149{
150 assert (exc_data_count < LLDB_THREAD_MAX_STOP_EXC_DATA);
151 assert (m_thread != NULL);
152 m_reason = eStopReasonException;
153 m_details.exception.type = exc_type;
154 m_details.exception.data_count = exc_data_count;
155 for (size_t i=0; i<exc_data_count; ++i)
156 m_details.exception.data[i] = exc_data[i];
157
158 if (m_details.exception.type != 0)
159 {
160 ArchSpec::CPU cpu = m_thread->GetProcess().GetTarget().GetArchitecture().GetGenericCPUType();
161
162 bool exc_translated = false;
163 const char *exc_desc = NULL;
164 const char *code_label = "code";
165 const char *code_desc = NULL;
166 const char *subcode_label = "subcode";
167 const char *subcode_desc = NULL;
168 switch (m_details.exception.type)
169 {
170 case 1: // EXC_BAD_ACCESS
171 exc_desc = "EXC_BAD_ACCESS";
172 subcode_label = "address";
173 switch (cpu)
174 {
175 case ArchSpec::eCPU_arm:
176 switch (m_details.exception.data[0])
177 {
178 case 0x101: code_desc = "EXC_ARM_DA_ALIGN"; break;
179 case 0x102: code_desc = "EXC_ARM_DA_DEBUG"; break;
180 }
181 break;
182
183 case ArchSpec::eCPU_ppc:
184 case ArchSpec::eCPU_ppc64:
185 switch (m_details.exception.data[0])
186 {
187 case 0x101: code_desc = "EXC_PPC_VM_PROT_READ"; break;
188 case 0x102: code_desc = "EXC_PPC_BADSPACE"; break;
189 case 0x103: code_desc = "EXC_PPC_UNALIGNED"; break;
190 }
191 break;
192
193 default:
194 break;
195 }
196 break;
197
198 case 2: // EXC_BAD_INSTRUCTION
199 exc_desc = "EXC_BAD_INSTRUCTION";
200 switch (cpu)
201 {
202 case ArchSpec::eCPU_i386:
203 case ArchSpec::eCPU_x86_64:
204 if (m_details.exception.data[0] == 1)
205 code_desc = "EXC_I386_INVOP";
206 break;
207
208 case ArchSpec::eCPU_ppc:
209 case ArchSpec::eCPU_ppc64:
210 switch (m_details.exception.data[0])
211 {
212 case 1: code_desc = "EXC_PPC_INVALID_SYSCALL"; break;
213 case 2: code_desc = "EXC_PPC_UNIPL_INST"; break;
214 case 3: code_desc = "EXC_PPC_PRIVINST"; break;
215 case 4: code_desc = "EXC_PPC_PRIVREG"; break;
216 case 5: // EXC_PPC_TRACE
217 SetStopReasonToTrace();
218 exc_translated = true;
219 break;
220 case 6: code_desc = "EXC_PPC_PERFMON"; break;
221 }
222 break;
223
224 case ArchSpec::eCPU_arm:
225 if (m_details.exception.data[0] == 1)
226 code_desc = "EXC_ARM_UNDEFINED";
227 break;
228
229 default:
230 break;
231 }
232 break;
233
234 case 3: // EXC_ARITHMETIC
235 exc_desc = "EXC_ARITHMETIC";
236 switch (cpu)
237 {
238 case ArchSpec::eCPU_i386:
239 case ArchSpec::eCPU_x86_64:
240 switch (m_details.exception.data[0])
241 {
242 case 1: code_desc = "EXC_I386_DIV"; break;
243 case 2: code_desc = "EXC_I386_INTO"; break;
244 case 3: code_desc = "EXC_I386_NOEXT"; break;
245 case 4: code_desc = "EXC_I386_EXTOVR"; break;
246 case 5: code_desc = "EXC_I386_EXTERR"; break;
247 case 6: code_desc = "EXC_I386_EMERR"; break;
248 case 7: code_desc = "EXC_I386_BOUND"; break;
249 case 8: code_desc = "EXC_I386_SSEEXTERR"; break;
250 }
251 break;
252
253 case ArchSpec::eCPU_ppc:
254 case ArchSpec::eCPU_ppc64:
255 switch (m_details.exception.data[0])
256 {
257 case 1: code_desc = "EXC_PPC_OVERFLOW"; break;
258 case 2: code_desc = "EXC_PPC_ZERO_DIVIDE"; break;
259 case 3: code_desc = "EXC_PPC_FLT_INEXACT"; break;
260 case 4: code_desc = "EXC_PPC_FLT_ZERO_DIVIDE"; break;
261 case 5: code_desc = "EXC_PPC_FLT_UNDERFLOW"; break;
262 case 6: code_desc = "EXC_PPC_FLT_OVERFLOW"; break;
263 case 7: code_desc = "EXC_PPC_FLT_NOT_A_NUMBER"; break;
264 }
265 break;
266
267 default:
268 break;
269 }
270 break;
271
272 case 4: // EXC_EMULATION
273 exc_desc = "EXC_EMULATION";
274 break;
275
276
277 case 5: // EXC_SOFTWARE
278 exc_desc = "EXC_SOFTWARE";
Greg Claytondc1c5642010-07-23 22:50:09 +0000279 // Check for EXC_SOFT_SIGNAL
280 if (m_details.exception.data[0] == 0x10003 && m_details.exception.data_count == 2)
Greg Clayton7661a982010-07-23 16:45:51 +0000281 {
282 SetStopReasonWithSignal(m_details.exception.data[1]);
283 exc_translated = true;
284 }
285 break;
286
287 case 6:
288 {
289 exc_desc = "EXC_SOFTWARE";
290 bool is_software_breakpoint = false;
291 switch (cpu)
292 {
293 case ArchSpec::eCPU_i386:
294 case ArchSpec::eCPU_x86_64:
295 if (m_details.exception.data[0] == 1) // EXC_I386_SGL
296 {
297 exc_translated = true;
298 SetStopReasonToTrace ();
299 }
300 else if (m_details.exception.data[0] == 2) // EXC_I386_BPT
301 {
302 is_software_breakpoint = true;
303 }
304 break;
305
306 case ArchSpec::eCPU_ppc:
307 case ArchSpec::eCPU_ppc64:
308 is_software_breakpoint = m_details.exception.data[0] == 1; // EXC_PPC_BREAKPOINT
309 break;
310
311 case ArchSpec::eCPU_arm:
312 is_software_breakpoint = m_details.exception.data[0] == 1; // EXC_ARM_BREAKPOINT
313 break;
314
315 default:
316 break;
317 }
318
319 if (is_software_breakpoint)
320 {
321 addr_t pc = m_thread->GetRegisterContext()->GetPC();
322 lldb::BreakpointSiteSP bp_site_sp = m_thread->GetProcess().GetBreakpointSiteList().FindByAddress(pc);
323 if (bp_site_sp)
324 {
325 exc_translated = true;
326 if (bp_site_sp->ValidForThisThread (m_thread))
327 {
328 Clear ();
329 SetStopReasonWithBreakpointSiteID (bp_site_sp->GetID());
330 }
331 else
332 {
333 Clear ();
334 SetStopReasonToNone();
335 }
336
337 }
338 }
339 }
340 break;
341
342 case 7:
343 exc_desc = "EXC_SYSCALL";
344 break;
345
346 case 8:
347 exc_desc = "EXC_MACH_SYSCALL";
348 break;
349
350 case 9:
351 exc_desc = "EXC_RPC_ALERT";
352 break;
353
354 case 10:
355 exc_desc = "EXC_CRASH";
356 break;
357 }
358
359 if (!exc_translated)
360 {
361 StreamString desc_strm;
362
363 if (exc_desc)
364 desc_strm.PutCString(exc_desc);
365 else
366 desc_strm.Printf("EXC_??? (%u)", exc_type);
367
368 if (m_details.exception.data_count >= 1)
369 {
370 if (code_desc)
371 desc_strm.Printf(" (%s=%s", code_label, code_desc);
372 else
373 desc_strm.Printf(" (%s=%llu", code_label, exc_data[0]);
374 }
375
376 if (m_details.exception.data_count >= 2)
377 {
378 if (subcode_desc)
379 desc_strm.Printf(", %s=%s", subcode_label, subcode_desc);
380 else
381 desc_strm.Printf(", %s=0x%llx", subcode_label, exc_data[1]);
382 }
383
384 if (m_details.exception.data_count > 0)
385 desc_strm.PutChar(')');
386
387 SetStopDescription(desc_strm.GetString().c_str());
388 }
389 }
390}
391void
Chris Lattner24943d22010-06-08 16:52:24 +0000392Thread::StopInfo::SetThread (Thread* thread)
393{
394 m_thread = thread;
395}
396
397Thread *
398Thread::StopInfo::GetThread ()
399{
400 return m_thread;
401}
402
403lldb::user_id_t
404Thread::StopInfo::GetBreakpointSiteID() const
405{
406 if (m_reason == eStopReasonBreakpoint)
407 return m_details.breakpoint.bp_site_id;
408 return LLDB_INVALID_BREAK_ID;
409}
410
411void
412Thread::StopInfo::SetStopReasonWithBreakpointSiteID (lldb::user_id_t bp_site_id)
413{
414 m_reason = eStopReasonBreakpoint;
415 m_details.breakpoint.bp_site_id = bp_site_id;
416}
417
418lldb::user_id_t
419Thread::StopInfo::GetWatchpointID() const
420{
421 if (m_reason == eStopReasonWatchpoint)
422 return m_details.watchpoint.watch_id;
423 return LLDB_INVALID_WATCH_ID;
424}
425
426void
427Thread::StopInfo::SetStopReasonWithWatchpointID (lldb::user_id_t watch_id)
428{
429 m_reason = eStopReasonWatchpoint;
430 m_details.watchpoint.watch_id = watch_id;
431}
432
433
434int
435Thread::StopInfo::GetSignal() const
436{
437 if (m_reason == eStopReasonSignal)
438 return m_details.signal.signo;
439 return 0;
440}
441
442lldb::user_id_t
443Thread::StopInfo::GetPlanID() const
444{
445 if (m_reason == eStopReasonPlanComplete)
446 return m_completed_plan_sp->GetID();
447 return LLDB_INVALID_UID;
448}
449
450void
451Thread::StopInfo::SetStopReasonWithSignal (int signo)
452{
453 m_reason = eStopReasonSignal;
454 m_details.signal.signo = signo;
455}
456
457void
458Thread::StopInfo::SetStopReasonToTrace ()
459{
460 m_reason = eStopReasonTrace;
461}
462
463uint32_t
464Thread::StopInfo::GetExceptionType() const
465{
466 if (m_reason == eStopReasonException)
467 return m_details.exception.type;
468 return 0;
469}
470
471size_t
472Thread::StopInfo::GetExceptionDataCount() const
473{
474 if (m_reason == eStopReasonException)
475 return m_details.exception.data_count;
476 return 0;
477}
478
479void
Greg Clayton7661a982010-07-23 16:45:51 +0000480Thread::StopInfo::SetStopReasonWithGenericException (uint32_t exc_type, size_t exc_data_count)
Chris Lattner24943d22010-06-08 16:52:24 +0000481{
482 m_reason = eStopReasonException;
483 m_details.exception.type = exc_type;
484 m_details.exception.data_count = exc_data_count;
485}
486
487void
488Thread::StopInfo::SetStopReasonWithPlan (ThreadPlanSP &thread_plan_sp)
489{
490 m_reason = eStopReasonPlanComplete;
491 m_completed_plan_sp = thread_plan_sp;
492}
493
494void
495Thread::StopInfo::SetStopReasonToNone ()
496{
497 Clear();
498 m_reason = eStopReasonNone;
499}
500
501lldb::addr_t
502Thread::StopInfo::GetExceptionDataAtIndex (uint32_t idx) const
503{
504 if (m_reason == eStopReasonException && idx < m_details.exception.data_count)
505 return m_details.exception.data[idx];
506 return 0;
507
508}
509
510
511bool
512Thread::StopInfo::SetExceptionDataAtIndex (uint32_t idx, lldb::addr_t data)
513{
514 if (m_reason == eStopReasonException && idx < m_details.exception.data_count)
515 {
516 m_details.exception.data[idx] = data;
517 return true;
518 }
519 return false;
520}
521
522void
523Thread::StopInfo::Dump (Stream *s) const
524{
525 if (m_description[0])
526 s->Printf("%s", m_description);
527 else
528 {
529 switch (m_reason)
530 {
531 case eStopReasonInvalid:
532 s->PutCString("invalid");
533 break;
534
535 case eStopReasonNone:
536 s->PutCString("none");
537 break;
538
539 case eStopReasonTrace:
540 s->PutCString("trace");
541 break;
542
543 case eStopReasonBreakpoint:
544 {
545 bool no_details = true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000546 s->PutCString ("breakpoint");
Chris Lattner24943d22010-06-08 16:52:24 +0000547 if (m_thread)
548 {
549 BreakpointSiteSP bp_site_sp = m_thread->GetProcess().GetBreakpointSiteList().FindByID(m_details.breakpoint.bp_site_id);
550 if (bp_site_sp)
551 {
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000552 // Only report the breakpoint locations that actually caused this hit - some of them may
553 // have options that would have caused us not to stop here...
554 uint32_t num_locations = bp_site_sp->GetNumberOfOwners();
555 for (uint32_t i = 0; i < num_locations; i++)
556 {
557 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(i);
558 if (bp_loc_sp->ValidForThisThread(m_thread))
559 {
560 s->PutCString(" ");
561 bp_loc_sp->GetDescription(s, lldb::eDescriptionLevelBrief);
562 no_details = false;
563 }
564 }
Chris Lattner24943d22010-06-08 16:52:24 +0000565 }
566 }
567
568 if (no_details)
569 s->Printf ("site id: %d", m_details.breakpoint.bp_site_id);
570 }
571 break;
572
573 case eStopReasonWatchpoint:
574 s->Printf("watchpoint (site id = %u)", m_details.watchpoint.watch_id);
575 break;
576
577 case eStopReasonSignal:
578 {
579 s->Printf("signal: signo = %i", m_details.signal.signo);
580 const char * signal_name = m_thread->GetProcess().GetUnixSignals().GetSignalAsCString (m_details.signal.signo);
581 if (signal_name)
582 s->Printf(" (%s)", signal_name);
583 }
584 break;
585
586 case eStopReasonException:
587 {
588 s->Printf("exception: type = 0x%8.8x, data_count = %zu", m_details.exception.type, m_details.exception.data_count);
589 uint32_t i;
590 for (i=0; i<m_details.exception.data_count; ++i)
591 {
592 s->Printf(", data[%u] = 0x%8.8llx", i, m_details.exception.data[i]);
593 }
594 }
595 break;
596
597 case eStopReasonPlanComplete:
598 {
599 m_completed_plan_sp->GetDescription (s, lldb::eDescriptionLevelBrief);
600 }
601 break;
602 }
603 }
604}
605
606bool
607Thread::GetStopInfo (Thread::StopInfo *stop_info)
608{
609 stop_info->SetThread(this);
610 ThreadPlanSP completed_plan = GetCompletedPlan();
611 if (completed_plan != NULL)
612 {
613 stop_info->Clear ();
614 stop_info->SetStopReasonWithPlan (completed_plan);
615 return true;
616 }
617 else
618 return GetRawStopReason (stop_info);
619}
620
621bool
622Thread::ThreadStoppedForAReason (void)
623{
624 Thread::StopInfo stop_info;
625 stop_info.SetThread(this);
626 if (GetRawStopReason (&stop_info))
627 {
628 StopReason reason = stop_info.GetStopReason();
629 if (reason == eStopReasonInvalid || reason == eStopReasonNone)
630 return false;
631 else
632 return true;
633 }
634 else
635 return false;
636}
637
638StateType
639Thread::GetState() const
640{
641 // If any other threads access this we will need a mutex for it
642 Mutex::Locker locker(m_state_mutex);
643 return m_state;
644}
645
646void
647Thread::SetState(StateType state)
648{
649 Mutex::Locker locker(m_state_mutex);
650 m_state = state;
651}
652
653void
654Thread::WillStop()
655{
656 ThreadPlan *current_plan = GetCurrentPlan();
657
658 // FIXME: I may decide to disallow threads with no plans. In which
659 // case this should go to an assert.
660
661 if (!current_plan)
662 return;
663
664 current_plan->WillStop();
665}
666
667void
668Thread::SetupForResume ()
669{
670 if (GetResumeState() != eStateSuspended)
671 {
672
673 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
674 // telling the current plan it will resume, since we might change what the current
675 // plan is.
676
677 lldb::addr_t pc = GetRegisterContext()->GetPC();
678 BreakpointSiteSP bp_site_sp = GetProcess().GetBreakpointSiteList().FindByAddress(pc);
679 if (bp_site_sp && bp_site_sp->IsEnabled())
680 {
681 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
682 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000683
684 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000685
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000686 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
687 {
688 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
689 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000690 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000691 ThreadPlanSP step_bp_plan_sp;
692 step_bp_plan->SetPrivate (true);
693
Chris Lattner24943d22010-06-08 16:52:24 +0000694 if (GetCurrentPlan()->RunState() != eStateStepping)
695 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000696 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000697 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000698 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000699 QueueThreadPlan (step_bp_plan_sp, false);
700 }
701 }
702 }
703 }
704}
705
706bool
707Thread::WillResume (StateType resume_state)
708{
709 // At this point clear the completed plan stack.
710 m_completed_plan_stack.clear();
711 m_discarded_plan_stack.clear();
712
713 // If this thread stopped with a signal, work out what its resume state should
714 // be. Note if the thread resume state is already set, then don't override it,
715 // the user must have asked us to resume with some other signal.
716
717 if (GetResumeSignal() == LLDB_INVALID_SIGNAL_NUMBER)
718 {
719 Thread::StopInfo stop_info;
720 GetRawStopReason(&stop_info);
721
722 StopReason reason = stop_info.GetStopReason();
723 if (reason == eStopReasonSignal)
724 {
725 UnixSignals &signals = GetProcess().GetUnixSignals();
726 int32_t signo = stop_info.GetSignal();
727 if (!signals.GetShouldSuppress(signo))
728 {
729 SetResumeSignal(signo);
730 }
731 }
732 }
733
734 // Tell all the plans that we are about to resume in case they need to clear any state.
735 // We distinguish between the plan on the top of the stack and the lower
736 // plans in case a plan needs to do any special business before it runs.
737
738 ThreadPlan *plan_ptr = GetCurrentPlan();
739 plan_ptr->WillResume(resume_state, true);
740
741 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
742 {
743 plan_ptr->WillResume (resume_state, false);
744 }
745 return true;
746}
747
748void
749Thread::DidResume ()
750{
751 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
752}
753
754bool
755Thread::ShouldStop (Event* event_ptr)
756{
757 ThreadPlan *current_plan = GetCurrentPlan();
758 bool should_stop = true;
759
760 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
761 if (log)
762 {
763 StreamString s;
764 DumpThreadPlans(&s);
765 log->PutCString (s.GetData());
766 }
767
768 if (current_plan->PlanExplainsStop())
769 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000770 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000771 while (1)
772 {
773 should_stop = current_plan->ShouldStop(event_ptr);
774 if (current_plan->MischiefManaged())
775 {
776 if (should_stop)
777 current_plan->WillStop();
778
779 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
780 // Otherwise, see if the plan's parent wants to stop.
781
782 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
783 {
784 PopPlan();
785 break;
786 }
787 else
788 {
789
790 PopPlan();
791
792 current_plan = GetCurrentPlan();
793 if (current_plan == NULL)
794 {
795 break;
796 }
797 }
798
799 }
800 else
801 {
802 break;
803 }
804 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000805 if (over_ride_stop)
806 should_stop = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000807 }
808 else
809 {
810 // If the current plan doesn't explain the stop, then, find one that
811 // does and let it handle the situation.
812 ThreadPlan *plan_ptr = current_plan;
813 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
814 {
815 if (plan_ptr->PlanExplainsStop())
816 {
817 should_stop = plan_ptr->ShouldStop (event_ptr);
818 break;
819 }
820
821 }
822 }
823
824 return should_stop;
825}
826
827Vote
828Thread::ShouldReportStop (Event* event_ptr)
829{
830 StateType thread_state = GetResumeState ();
831 if (thread_state == eStateSuspended
832 || thread_state == eStateInvalid)
833 return eVoteNoOpinion;
834
835 if (m_completed_plan_stack.size() > 0)
836 {
837 // Don't use GetCompletedPlan here, since that suppresses private plans.
838 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
839 }
840 else
841 return GetCurrentPlan()->ShouldReportStop (event_ptr);
842}
843
844Vote
845Thread::ShouldReportRun (Event* event_ptr)
846{
847 StateType thread_state = GetResumeState ();
848 if (thread_state == eStateSuspended
849 || thread_state == eStateInvalid)
850 return eVoteNoOpinion;
851
852 if (m_completed_plan_stack.size() > 0)
853 {
854 // Don't use GetCompletedPlan here, since that suppresses private plans.
855 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
856 }
857 else
858 return GetCurrentPlan()->ShouldReportRun (event_ptr);
859}
860
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000861bool
862Thread::MatchesSpec (const ThreadSpec *spec)
863{
864 if (spec == NULL)
865 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000866
Jim Ingham649492b2010-06-18 01:00:58 +0000867 return spec->ThreadPassesBasicTests(this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000868}
869
Chris Lattner24943d22010-06-08 16:52:24 +0000870void
871Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
872{
873 if (thread_plan_sp)
874 {
875 if (thread_plan_sp->IsImmediate())
876 m_immediate_plan_stack.push_back (thread_plan_sp);
877 else
878 m_plan_stack.push_back (thread_plan_sp);
879
880 thread_plan_sp->DidPush();
881
882 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
883 if (log)
884 {
885 StreamString s;
886 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
887 log->Printf("Pushing plan: \"%s\" for thread: %d immediate: %s.",
888 s.GetData(),
889 thread_plan_sp->GetThread().GetID(),
890 thread_plan_sp->IsImmediate() ? "true" : "false");
891 }
892 }
893}
894
895void
896Thread::PopPlan ()
897{
898 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
899
900 if (!m_immediate_plan_stack.empty())
901 {
902 ThreadPlanSP &plan = m_immediate_plan_stack.back();
903 if (log)
904 {
905 log->Printf("Popping plan: \"%s\" for thread: %d immediate: true.", plan->GetName(), plan->GetThread().GetID());
906 }
907 plan->WillPop();
908 m_immediate_plan_stack.pop_back();
909 }
910 else if (m_plan_stack.empty())
911 return;
912 else
913 {
914 ThreadPlanSP &plan = m_plan_stack.back();
915 if (log)
916 {
917 log->Printf("Popping plan: \"%s\" for thread: 0x%x immediate: false.", plan->GetName(), plan->GetThread().GetID());
918 }
919 m_completed_plan_stack.push_back (plan);
920 plan->WillPop();
921 m_plan_stack.pop_back();
922 }
923}
924
925void
926Thread::DiscardPlan ()
927{
928 if (m_plan_stack.size() > 1)
929 {
930 ThreadPlanSP &plan = m_plan_stack.back();
931 m_discarded_plan_stack.push_back (plan);
932 plan->WillPop();
933 m_plan_stack.pop_back();
934 }
935}
936
937ThreadPlan *
938Thread::GetCurrentPlan ()
939{
940 if (!m_immediate_plan_stack.empty())
941 return m_immediate_plan_stack.back().get();
942 else if (m_plan_stack.empty())
943 return NULL;
944 else
945 return m_plan_stack.back().get();
946}
947
948ThreadPlanSP
949Thread::GetCompletedPlan ()
950{
951 ThreadPlanSP empty_plan_sp;
952 if (!m_completed_plan_stack.empty())
953 {
954 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
955 {
956 ThreadPlanSP completed_plan_sp;
957 completed_plan_sp = m_completed_plan_stack[i];
958 if (!completed_plan_sp->GetPrivate ())
959 return completed_plan_sp;
960 }
961 }
962 return empty_plan_sp;
963}
964
965bool
966Thread::IsThreadPlanDone (ThreadPlan *plan)
967{
968 ThreadPlanSP empty_plan_sp;
969 if (!m_completed_plan_stack.empty())
970 {
971 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
972 {
973 if (m_completed_plan_stack[i].get() == plan)
974 return true;
975 }
976 }
977 return false;
978}
979
980bool
981Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
982{
983 ThreadPlanSP empty_plan_sp;
984 if (!m_discarded_plan_stack.empty())
985 {
986 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
987 {
988 if (m_discarded_plan_stack[i].get() == plan)
989 return true;
990 }
991 }
992 return false;
993}
994
995ThreadPlan *
996Thread::GetPreviousPlan (ThreadPlan *current_plan)
997{
998 if (current_plan == NULL)
999 return NULL;
1000
1001 int stack_size = m_completed_plan_stack.size();
1002 for (int i = stack_size - 1; i > 0; i--)
1003 {
1004 if (current_plan == m_completed_plan_stack[i].get())
1005 return m_completed_plan_stack[i-1].get();
1006 }
1007
1008 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1009 {
1010 if (m_immediate_plan_stack.size() > 0)
1011 return m_immediate_plan_stack.back().get();
1012 else if (m_plan_stack.size() > 0)
1013 return m_plan_stack.back().get();
1014 else
1015 return NULL;
1016 }
1017
1018 stack_size = m_immediate_plan_stack.size();
1019 for (int i = stack_size - 1; i > 0; i--)
1020 {
1021 if (current_plan == m_immediate_plan_stack[i].get())
1022 return m_immediate_plan_stack[i-1].get();
1023 }
1024 if (stack_size > 0 && m_immediate_plan_stack[0].get() == current_plan)
1025 {
1026 if (m_plan_stack.size() > 0)
1027 return m_plan_stack.back().get();
1028 else
1029 return NULL;
1030 }
1031
1032 stack_size = m_plan_stack.size();
1033 for (int i = stack_size - 1; i > 0; i--)
1034 {
1035 if (current_plan == m_plan_stack[i].get())
1036 return m_plan_stack[i-1].get();
1037 }
1038 return NULL;
1039}
1040
1041void
1042Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1043{
1044 if (abort_other_plans)
1045 DiscardThreadPlans(true);
1046
1047 PushPlan (thread_plan_sp);
1048}
1049
1050void
1051Thread::DiscardThreadPlans(bool force)
1052{
1053 // FIXME: It is not always safe to just discard plans. Some, like the step over
1054 // breakpoint trap can't be discarded in general (though you can if you plan to
1055 // force a return from a function, for instance.
1056 // For now I'm just not clearing immediate plans, but I need a way for plans to
1057 // say they really need to be kept on, and then a way to override that. Humm...
1058
1059 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
1060 if (log)
1061 {
1062 log->Printf("Discarding thread plans for thread: 0x%x: force %d.", GetID(), force);
1063 }
1064
1065 if (force)
1066 {
1067 int stack_size = m_plan_stack.size();
1068 for (int i = stack_size - 1; i > 0; i--)
1069 {
1070 DiscardPlan();
1071 }
1072 return;
1073 }
1074
1075 while (1)
1076 {
1077
1078 int master_plan_idx;
1079 bool discard;
1080
1081 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1082 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1083 {
1084 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1085 {
1086 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1087 break;
1088 }
1089 }
1090
1091 if (discard)
1092 {
1093 // First pop all the dependent plans:
1094 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1095 {
1096
1097 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1098 // for the plan leaves it in a state that it is safe to pop the plan
1099 // with no more notice?
1100 DiscardPlan();
1101 }
1102
1103 // Now discard the master plan itself.
1104 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1105 // discard it's dependent plans, but not it...
1106 if (master_plan_idx > 0)
1107 {
1108 DiscardPlan();
1109 }
1110 }
1111 else
1112 {
1113 // If the master plan doesn't want to get discarded, then we're done.
1114 break;
1115 }
1116
1117 }
1118 // FIXME: What should we do about the immediate plans?
1119}
1120
1121ThreadPlan *
1122Thread::QueueFundamentalPlan (bool abort_other_plans)
1123{
1124 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1125 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1126 return thread_plan_sp.get();
1127}
1128
1129ThreadPlan *
1130Thread::QueueThreadPlanForStepSingleInstruction (bool step_over, bool abort_other_plans, bool stop_other_threads)
1131{
1132 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1133 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1134 return thread_plan_sp.get();
1135}
1136
1137ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001138Thread::QueueThreadPlanForStepRange
1139(
1140 bool abort_other_plans,
1141 StepType type,
1142 const AddressRange &range,
1143 const SymbolContext &addr_context,
1144 lldb::RunMode stop_other_threads,
1145 bool avoid_code_without_debug_info
1146)
Chris Lattner24943d22010-06-08 16:52:24 +00001147{
1148 ThreadPlanSP thread_plan_sp;
1149 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001150 {
1151 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1152 if (avoid_code_without_debug_info)
1153 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1154 else
1155 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1156 thread_plan_sp.reset (plan);
1157 }
Chris Lattner24943d22010-06-08 16:52:24 +00001158 else
1159 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1160
1161 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1162 return thread_plan_sp.get();
1163}
1164
1165
1166ThreadPlan *
1167Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1168{
1169 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1170 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1171 return thread_plan_sp.get();
1172}
1173
1174ThreadPlan *
1175Thread::QueueThreadPlanForStepOut (bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1176 bool stop_other_threads, Vote stop_vote, Vote run_vote)
1177{
1178 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote));
1179 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1180 return thread_plan_sp.get();
1181}
1182
1183ThreadPlan *
1184Thread::QueueThreadPlanForStepThrough (bool abort_other_plans, bool stop_other_threads)
1185{
1186 ThreadPlanSP thread_plan_sp(GetProcess().GetDynamicLoader()->GetStepThroughTrampolinePlan (*this, stop_other_threads));
1187 if (thread_plan_sp.get() == NULL)
1188 {
1189 thread_plan_sp.reset(new ThreadPlanStepThrough (*this, stop_other_threads));
1190 if (thread_plan_sp && !thread_plan_sp->ValidatePlan (NULL))
Greg Claytonf8e98a62010-07-23 15:37:46 +00001191 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +00001192 }
1193 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1194 return thread_plan_sp.get();
1195}
1196
1197ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001198Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1199 Address& function,
1200 lldb::addr_t arg,
1201 bool stop_other_threads,
1202 bool discard_on_error)
1203{
1204 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, arg, stop_other_threads, discard_on_error));
1205 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1206 return thread_plan_sp.get();
1207}
1208
1209ThreadPlan *
1210Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1211 Address& function,
1212 ValueList &args,
1213 bool stop_other_threads,
1214 bool discard_on_error)
1215{
1216 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, args, stop_other_threads, discard_on_error));
1217 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1218 return thread_plan_sp.get();
1219}
1220
1221ThreadPlan *
1222Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1223 Address &target_addr,
1224 bool stop_other_threads)
1225{
1226 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1227 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1228 return thread_plan_sp.get();
1229}
1230
1231ThreadPlan *
1232Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
1233 lldb::addr_t *address_list,
1234 size_t num_addresses,
1235 bool stop_other_threads)
1236{
1237 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads));
1238 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1239 return thread_plan_sp.get();
1240
1241}
1242
1243uint32_t
1244Thread::GetIndexID () const
1245{
1246 return m_index_id;
1247}
1248
1249void
1250Thread::DumpThreadPlans (lldb_private::Stream *s) const
1251{
1252 uint32_t stack_size = m_plan_stack.size();
Jim Ingham3c7b5b92010-06-16 02:00:15 +00001253 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4x - %d elements.\n", GetIndexID(), GetID(), stack_size);
Chris Lattner24943d22010-06-08 16:52:24 +00001254 for (int i = stack_size - 1; i > 0; i--)
1255 {
1256 s->Printf ("Element %d: ", i);
1257 s->IndentMore();
1258 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1259 s->IndentLess();
1260 s->EOL();
1261 }
1262
1263 stack_size = m_immediate_plan_stack.size();
1264 s->Printf ("Immediate Plan Stack: %d elements.\n", stack_size);
1265 for (int i = stack_size - 1; i > 0; i--)
1266 {
1267 s->Printf ("Element %d: ", i);
1268 s->IndentMore();
1269 m_immediate_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1270 s->IndentLess();
1271 s->EOL();
1272 }
1273
1274 stack_size = m_completed_plan_stack.size();
1275 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1276 for (int i = stack_size - 1; i > 0; i--)
1277 {
1278 s->Printf ("Element %d: ", i);
1279 s->IndentMore();
1280 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1281 s->IndentLess();
1282 s->EOL();
1283 }
1284
1285 stack_size = m_discarded_plan_stack.size();
1286 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1287 for (int i = stack_size - 1; i > 0; i--)
1288 {
1289 s->Printf ("Element %d: ", i);
1290 s->IndentMore();
1291 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1292 s->IndentLess();
1293 s->EOL();
1294 }
1295
1296}
1297
1298Target *
1299Thread::CalculateTarget ()
1300{
1301 return m_process.CalculateTarget();
1302}
1303
1304Process *
1305Thread::CalculateProcess ()
1306{
1307 return &m_process;
1308}
1309
1310Thread *
1311Thread::CalculateThread ()
1312{
1313 return this;
1314}
1315
1316StackFrame *
1317Thread::CalculateStackFrame ()
1318{
1319 return NULL;
1320}
1321
1322void
1323Thread::Calculate (ExecutionContext &exe_ctx)
1324{
1325 m_process.Calculate (exe_ctx);
1326 exe_ctx.thread = this;
1327 exe_ctx.frame = NULL;
1328}
1329
1330lldb::StackFrameSP
1331Thread::GetCurrentFrame ()
1332{
1333 return GetStackFrameAtIndex (m_frames.GetCurrentFrameIndex());
1334}
1335
1336uint32_t
1337Thread::SetCurrentFrame (lldb_private::StackFrame *frame)
1338{
1339 return m_frames.SetCurrentFrame(frame);
1340}
1341
1342void
1343Thread::SetCurrentFrameByIndex (uint32_t frame_idx)
1344{
1345 m_frames.SetCurrentFrameByIndex(frame_idx);
1346}
1347
1348void
1349Thread::DumpInfo
1350(
1351 Stream &strm,
1352 bool show_stop_reason,
1353 bool show_name,
1354 bool show_queue,
1355 uint32_t frame_idx
1356)
1357{
1358 strm.Printf("thread #%u: tid = 0x%4.4x", GetIndexID(), GetID());
1359
1360 if (frame_idx != LLDB_INVALID_INDEX32)
1361 {
1362 StackFrameSP frame_sp(GetStackFrameAtIndex (frame_idx));
1363 if (frame_sp)
1364 {
1365 strm.PutCString(", ");
1366 frame_sp->Dump (&strm, false);
1367 }
1368 }
1369
1370 if (show_stop_reason)
1371 {
1372 Thread::StopInfo thread_stop_info;
1373 if (GetStopInfo(&thread_stop_info))
1374 {
1375 if (thread_stop_info.GetStopReason() != eStopReasonNone)
1376 {
1377 strm.PutCString(", stop reason = ");
1378 thread_stop_info.Dump(&strm);
1379 }
1380 }
1381 }
1382
1383 if (show_name)
1384 {
1385 const char *name = GetName();
1386 if (name && name[0])
1387 strm.Printf(", name = %s", name);
1388 }
1389
1390 if (show_queue)
1391 {
1392 const char *queue = GetQueueName();
1393 if (queue && queue[0])
1394 strm.Printf(", queue = %s", queue);
1395 }
1396}
1397
1398lldb::ThreadSP
1399Thread::GetSP ()
1400{
1401 return m_process.GetThreadList().GetThreadSPForThreadPtr(this);
1402}