1 //===-- SBThread.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-python.h"
11 
12 #include "lldb/API/SBThread.h"
13 
14 #include "lldb/API/SBSymbolContext.h"
15 #include "lldb/API/SBFileSpec.h"
16 #include "lldb/API/SBStream.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/CompileUnit.h"
26 #include "lldb/Target/StopInfo.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/ThreadPlan.h"
29 #include "lldb/Target/ThreadPlanStepInstruction.h"
30 #include "lldb/Target/ThreadPlanStepOut.h"
31 #include "lldb/Target/ThreadPlanStepRange.h"
32 #include "lldb/Target/ThreadPlanStepInRange.h"
33 
34 
35 #include "lldb/API/SBAddress.h"
36 #include "lldb/API/SBDebugger.h"
37 #include "lldb/API/SBEvent.h"
38 #include "lldb/API/SBFrame.h"
39 #include "lldb/API/SBProcess.h"
40 #include "lldb/API/SBValue.h"
41 
42 using namespace lldb;
43 using namespace lldb_private;
44 
45 const char *
46 SBThread::GetBroadcasterClassName ()
47 {
48     return Thread::GetStaticBroadcasterClass().AsCString();
49 }
50 
51 //----------------------------------------------------------------------
52 // Constructors
53 //----------------------------------------------------------------------
54 SBThread::SBThread () :
55     m_opaque_sp (new ExecutionContextRef())
56 {
57 }
58 
59 SBThread::SBThread (const ThreadSP& lldb_object_sp) :
60     m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
61 {
62 }
63 
64 SBThread::SBThread (const SBThread &rhs) :
65     m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
66 {
67 
68 }
69 
70 //----------------------------------------------------------------------
71 // Assignment operator
72 //----------------------------------------------------------------------
73 
74 const lldb::SBThread &
75 SBThread::operator = (const SBThread &rhs)
76 {
77     if (this != &rhs)
78         *m_opaque_sp = *rhs.m_opaque_sp;
79     return *this;
80 }
81 
82 //----------------------------------------------------------------------
83 // Destructor
84 //----------------------------------------------------------------------
85 SBThread::~SBThread()
86 {
87 }
88 
89 bool
90 SBThread::IsValid() const
91 {
92     return m_opaque_sp->GetThreadSP().get() != NULL;
93 }
94 
95 void
96 SBThread::Clear ()
97 {
98     m_opaque_sp->Clear();
99 }
100 
101 
102 StopReason
103 SBThread::GetStopReason()
104 {
105     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
106 
107     StopReason reason = eStopReasonInvalid;
108     Mutex::Locker api_locker;
109     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
110 
111     if (exe_ctx.HasThreadScope())
112     {
113         Process::StopLocker stop_locker;
114         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
115         {
116             return exe_ctx.GetThreadPtr()->GetStopReason();
117         }
118         else
119         {
120             if (log)
121                 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running", exe_ctx.GetThreadPtr());
122         }
123     }
124 
125     if (log)
126         log->Printf ("SBThread(%p)::GetStopReason () => %s", exe_ctx.GetThreadPtr(),
127                      Thread::StopReasonAsCString (reason));
128 
129     return reason;
130 }
131 
132 size_t
133 SBThread::GetStopReasonDataCount ()
134 {
135     Mutex::Locker api_locker;
136     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
137 
138     if (exe_ctx.HasThreadScope())
139     {
140         Process::StopLocker stop_locker;
141         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
142         {
143             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
144             if (stop_info_sp)
145             {
146                 StopReason reason = stop_info_sp->GetStopReason();
147                 switch (reason)
148                 {
149                 case eStopReasonInvalid:
150                 case eStopReasonNone:
151                 case eStopReasonTrace:
152                 case eStopReasonExec:
153                 case eStopReasonPlanComplete:
154                     // There is no data for these stop reasons.
155                     return 0;
156 
157                 case eStopReasonBreakpoint:
158                     {
159                         break_id_t site_id = stop_info_sp->GetValue();
160                         lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
161                         if (bp_site_sp)
162                             return bp_site_sp->GetNumberOfOwners () * 2;
163                         else
164                             return 0; // Breakpoint must have cleared itself...
165                     }
166                     break;
167 
168                 case eStopReasonWatchpoint:
169                     return 1;
170 
171                 case eStopReasonSignal:
172                     return 1;
173 
174                 case eStopReasonException:
175                     return 1;
176                 }
177             }
178         }
179         else
180         {
181             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
182             if (log)
183                 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running", exe_ctx.GetThreadPtr());
184         }
185     }
186     return 0;
187 }
188 
189 uint64_t
190 SBThread::GetStopReasonDataAtIndex (uint32_t idx)
191 {
192     Mutex::Locker api_locker;
193     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
194 
195     if (exe_ctx.HasThreadScope())
196     {
197         Process::StopLocker stop_locker;
198         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
199         {
200             Thread *thread = exe_ctx.GetThreadPtr();
201             StopInfoSP stop_info_sp = thread->GetStopInfo ();
202             if (stop_info_sp)
203             {
204                 StopReason reason = stop_info_sp->GetStopReason();
205                 switch (reason)
206                 {
207                 case eStopReasonInvalid:
208                 case eStopReasonNone:
209                 case eStopReasonTrace:
210                 case eStopReasonExec:
211                 case eStopReasonPlanComplete:
212                     // There is no data for these stop reasons.
213                     return 0;
214 
215                 case eStopReasonBreakpoint:
216                     {
217                         break_id_t site_id = stop_info_sp->GetValue();
218                         lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
219                         if (bp_site_sp)
220                         {
221                             uint32_t bp_index = idx / 2;
222                             BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
223                             if (bp_loc_sp)
224                             {
225                                 if (bp_index & 1)
226                                 {
227                                     // Odd idx, return the breakpoint location ID
228                                     return bp_loc_sp->GetID();
229                                 }
230                                 else
231                                 {
232                                     // Even idx, return the breakpoint ID
233                                     return bp_loc_sp->GetBreakpoint().GetID();
234                                 }
235                             }
236                         }
237                         return LLDB_INVALID_BREAK_ID;
238                     }
239                     break;
240 
241                 case eStopReasonWatchpoint:
242                     return stop_info_sp->GetValue();
243 
244                 case eStopReasonSignal:
245                     return stop_info_sp->GetValue();
246 
247                 case eStopReasonException:
248                     return stop_info_sp->GetValue();
249                 }
250             }
251         }
252         else
253         {
254             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
255             if (log)
256                 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
257         }
258     }
259     return 0;
260 }
261 
262 size_t
263 SBThread::GetStopDescription (char *dst, size_t dst_len)
264 {
265     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
266 
267     Mutex::Locker api_locker;
268     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
269 
270     if (exe_ctx.HasThreadScope())
271     {
272         Process::StopLocker stop_locker;
273         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
274         {
275 
276             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
277             if (stop_info_sp)
278             {
279                 const char *stop_desc = stop_info_sp->GetDescription();
280                 if (stop_desc)
281                 {
282                     if (log)
283                         log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
284                                      exe_ctx.GetThreadPtr(), stop_desc);
285                     if (dst)
286                         return ::snprintf (dst, dst_len, "%s", stop_desc);
287                     else
288                     {
289                         // NULL dst passed in, return the length needed to contain the description
290                         return ::strlen (stop_desc) + 1; // Include the NULL byte for size
291                     }
292                 }
293                 else
294                 {
295                     size_t stop_desc_len = 0;
296                     switch (stop_info_sp->GetStopReason())
297                     {
298                     case eStopReasonTrace:
299                     case eStopReasonPlanComplete:
300                         {
301                             static char trace_desc[] = "step";
302                             stop_desc = trace_desc;
303                             stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
304                         }
305                         break;
306 
307                     case eStopReasonBreakpoint:
308                         {
309                             static char bp_desc[] = "breakpoint hit";
310                             stop_desc = bp_desc;
311                             stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
312                         }
313                         break;
314 
315                     case eStopReasonWatchpoint:
316                         {
317                             static char wp_desc[] = "watchpoint hit";
318                             stop_desc = wp_desc;
319                             stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
320                         }
321                         break;
322 
323                     case eStopReasonSignal:
324                         {
325                             stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
326                             if (stop_desc == NULL || stop_desc[0] == '\0')
327                             {
328                                 static char signal_desc[] = "signal";
329                                 stop_desc = signal_desc;
330                                 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
331                             }
332                         }
333                         break;
334 
335                     case eStopReasonException:
336                         {
337                             char exc_desc[] = "exception";
338                             stop_desc = exc_desc;
339                             stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
340                         }
341                         break;
342 
343                     case eStopReasonExec:
344                         {
345                             char exc_desc[] = "exec";
346                             stop_desc = exc_desc;
347                             stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
348                         }
349                         break;
350 
351                     default:
352                         break;
353                     }
354 
355                     if (stop_desc && stop_desc[0])
356                     {
357                         if (log)
358                             log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
359                                          exe_ctx.GetThreadPtr(), stop_desc);
360 
361                         if (dst)
362                             return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
363 
364                         if (stop_desc_len == 0)
365                             stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
366 
367                         return stop_desc_len;
368                     }
369                 }
370             }
371         }
372         else
373         {
374             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
375             if (log)
376                 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running", exe_ctx.GetThreadPtr());
377         }
378     }
379     if (dst)
380         *dst = 0;
381     return 0;
382 }
383 
384 SBValue
385 SBThread::GetStopReturnValue ()
386 {
387     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
388     ValueObjectSP return_valobj_sp;
389     Mutex::Locker api_locker;
390     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
391 
392     if (exe_ctx.HasThreadScope())
393     {
394         Process::StopLocker stop_locker;
395         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
396         {
397             StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
398             if (stop_info_sp)
399             {
400                 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
401             }
402         }
403         else
404         {
405             if (log)
406                 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running", exe_ctx.GetThreadPtr());
407         }
408     }
409 
410     if (log)
411         log->Printf ("SBThread(%p)::GetStopReturnValue () => %s", exe_ctx.GetThreadPtr(),
412                                                                   return_valobj_sp.get()
413                                                                       ? return_valobj_sp->GetValueAsCString()
414                                                                         : "<no return value>");
415 
416     return SBValue (return_valobj_sp);
417 }
418 
419 void
420 SBThread::SetThread (const ThreadSP& lldb_object_sp)
421 {
422     m_opaque_sp->SetThreadSP (lldb_object_sp);
423 }
424 
425 
426 lldb::tid_t
427 SBThread::GetThreadID () const
428 {
429     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
430     if (thread_sp)
431         return thread_sp->GetID();
432     return LLDB_INVALID_THREAD_ID;
433 }
434 
435 uint32_t
436 SBThread::GetIndexID () const
437 {
438     ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
439     if (thread_sp)
440         return thread_sp->GetIndexID();
441     return LLDB_INVALID_INDEX32;
442 }
443 
444 const char *
445 SBThread::GetName () const
446 {
447     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
448     const char *name = NULL;
449     Mutex::Locker api_locker;
450     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
451 
452     if (exe_ctx.HasThreadScope())
453     {
454         Process::StopLocker stop_locker;
455         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
456         {
457             name = exe_ctx.GetThreadPtr()->GetName();
458         }
459         else
460         {
461             if (log)
462                 log->Printf ("SBThread(%p)::GetName() => error: process is running", exe_ctx.GetThreadPtr());
463         }
464     }
465 
466     if (log)
467         log->Printf ("SBThread(%p)::GetName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
468 
469     return name;
470 }
471 
472 const char *
473 SBThread::GetQueueName () const
474 {
475     const char *name = NULL;
476     Mutex::Locker api_locker;
477     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
478 
479     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
480     if (exe_ctx.HasThreadScope())
481     {
482         Process::StopLocker stop_locker;
483         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
484         {
485             name = exe_ctx.GetThreadPtr()->GetQueueName();
486         }
487         else
488         {
489             if (log)
490                 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running", exe_ctx.GetThreadPtr());
491         }
492     }
493 
494     if (log)
495         log->Printf ("SBThread(%p)::GetQueueName () => %s", exe_ctx.GetThreadPtr(), name ? name : "NULL");
496 
497     return name;
498 }
499 
500 SBError
501 SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
502 {
503     SBError sb_error;
504 
505     Process *process = exe_ctx.GetProcessPtr();
506     if (!process)
507     {
508         sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
509         return sb_error;
510     }
511 
512     Thread *thread = exe_ctx.GetThreadPtr();
513     if (!thread)
514     {
515         sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
516         return sb_error;
517     }
518 
519     // User level plans should be Master Plans so they can be interrupted, other plans executed, and
520     // then a "continue" will resume the plan.
521     if (new_plan != NULL)
522     {
523         new_plan->SetIsMasterPlan(true);
524         new_plan->SetOkayToDiscard(false);
525     }
526 
527     // Why do we need to set the current thread by ID here???
528     process->GetThreadList().SetSelectedThreadByID (thread->GetID());
529     sb_error.ref() = process->Resume();
530 
531     if (sb_error.Success())
532     {
533         // If we are doing synchronous mode, then wait for the
534         // process to stop yet again!
535         if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
536             process->WaitForProcessToStop (NULL);
537     }
538 
539     return sb_error;
540 }
541 
542 void
543 SBThread::StepOver (lldb::RunMode stop_other_threads)
544 {
545     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
546 
547     Mutex::Locker api_locker;
548     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
549 
550 
551     if (log)
552         log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
553                      Thread::RunModeAsCString (stop_other_threads));
554 
555     if (exe_ctx.HasThreadScope())
556     {
557         Thread *thread = exe_ctx.GetThreadPtr();
558         bool abort_other_plans = false;
559         StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
560         ThreadPlan *new_plan = NULL;
561 
562         if (frame_sp)
563         {
564             if (frame_sp->HasDebugInformation ())
565             {
566                 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
567                 new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
568                                                                 eStepTypeOver,
569                                                                 sc.line_entry.range,
570                                                                 sc,
571                                                                 stop_other_threads,
572                                                                 false);
573 
574             }
575             else
576             {
577                 new_plan = thread->QueueThreadPlanForStepSingleInstruction (true,
578                                                                             abort_other_plans,
579                                                                             stop_other_threads);
580             }
581         }
582 
583         // This returns an error, we should use it!
584         ResumeNewPlan (exe_ctx, new_plan);
585     }
586 }
587 
588 void
589 SBThread::StepInto (lldb::RunMode stop_other_threads)
590 {
591     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
592 
593     Mutex::Locker api_locker;
594     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
595 
596     if (log)
597         log->Printf ("SBThread(%p)::StepInto (stop_other_threads='%s')", exe_ctx.GetThreadPtr(),
598                      Thread::RunModeAsCString (stop_other_threads));
599     if (exe_ctx.HasThreadScope())
600     {
601         bool abort_other_plans = false;
602 
603         Thread *thread = exe_ctx.GetThreadPtr();
604         StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
605         ThreadPlan *new_plan = NULL;
606 
607         if (frame_sp && frame_sp->HasDebugInformation ())
608         {
609             bool avoid_code_without_debug_info = true;
610             SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
611             new_plan = thread->QueueThreadPlanForStepRange (abort_other_plans,
612                                                             eStepTypeInto,
613                                                             sc.line_entry.range,
614                                                             sc,
615                                                             stop_other_threads,
616                                                             avoid_code_without_debug_info);
617         }
618         else
619         {
620             new_plan = thread->QueueThreadPlanForStepSingleInstruction (false,
621                                                                         abort_other_plans,
622                                                                         stop_other_threads);
623         }
624 
625         // This returns an error, we should use it!
626         ResumeNewPlan (exe_ctx, new_plan);
627     }
628 }
629 
630 void
631 SBThread::StepOut ()
632 {
633     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
634 
635     Mutex::Locker api_locker;
636     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
637 
638 
639     if (log)
640         log->Printf ("SBThread(%p)::StepOut ()", exe_ctx.GetThreadPtr());
641 
642     if (exe_ctx.HasThreadScope())
643     {
644         bool abort_other_plans = false;
645         bool stop_other_threads = false;
646 
647         Thread *thread = exe_ctx.GetThreadPtr();
648 
649         ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
650                                                                   NULL,
651                                                                   false,
652                                                                   stop_other_threads,
653                                                                   eVoteYes,
654                                                                   eVoteNoOpinion,
655                                                                   0);
656 
657         // This returns an error, we should use it!
658         ResumeNewPlan (exe_ctx, new_plan);
659     }
660 }
661 
662 void
663 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
664 {
665     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
666 
667     Mutex::Locker api_locker;
668     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
669 
670     StackFrameSP frame_sp (sb_frame.GetFrameSP());
671     if (log)
672     {
673         SBStream frame_desc_strm;
674         sb_frame.GetDescription (frame_desc_strm);
675         log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)", exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
676     }
677 
678     if (exe_ctx.HasThreadScope())
679     {
680         bool abort_other_plans = false;
681         bool stop_other_threads = false;
682         Thread *thread = exe_ctx.GetThreadPtr();
683 
684         ThreadPlan *new_plan = thread->QueueThreadPlanForStepOut (abort_other_plans,
685                                                                     NULL,
686                                                                     false,
687                                                                     stop_other_threads,
688                                                                     eVoteYes,
689                                                                     eVoteNoOpinion,
690                                                                     frame_sp->GetFrameIndex());
691 
692         // This returns an error, we should use it!
693         ResumeNewPlan (exe_ctx, new_plan);
694     }
695 }
696 
697 void
698 SBThread::StepInstruction (bool step_over)
699 {
700     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
701 
702     Mutex::Locker api_locker;
703     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
704 
705 
706 
707     if (log)
708         log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)", exe_ctx.GetThreadPtr(), step_over);
709 
710     if (exe_ctx.HasThreadScope())
711     {
712         Thread *thread = exe_ctx.GetThreadPtr();
713         ThreadPlan *new_plan = thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true);
714 
715         // This returns an error, we should use it!
716         ResumeNewPlan (exe_ctx, new_plan);
717     }
718 }
719 
720 void
721 SBThread::RunToAddress (lldb::addr_t addr)
722 {
723     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
724 
725     Mutex::Locker api_locker;
726     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
727 
728 
729     if (log)
730         log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")", exe_ctx.GetThreadPtr(), addr);
731 
732     if (exe_ctx.HasThreadScope())
733     {
734         bool abort_other_plans = false;
735         bool stop_other_threads = true;
736 
737         Address target_addr (addr);
738 
739         Thread *thread = exe_ctx.GetThreadPtr();
740 
741         ThreadPlan *new_plan = thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads);
742 
743         // This returns an error, we should use it!
744         ResumeNewPlan (exe_ctx, new_plan);
745     }
746 }
747 
748 SBError
749 SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
750                          lldb::SBFileSpec &sb_file_spec,
751                          uint32_t line)
752 {
753     SBError sb_error;
754     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
755     char path[PATH_MAX];
756 
757     Mutex::Locker api_locker;
758     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
759 
760     StackFrameSP frame_sp (sb_frame.GetFrameSP());
761 
762     if (log)
763     {
764         SBStream frame_desc_strm;
765         sb_frame.GetDescription (frame_desc_strm);
766         sb_file_spec->GetPath (path, sizeof(path));
767         log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
768                      exe_ctx.GetThreadPtr(),
769                      frame_sp.get(),
770                      frame_desc_strm.GetData(),
771                      path, line);
772     }
773 
774     if (exe_ctx.HasThreadScope())
775     {
776         Target *target = exe_ctx.GetTargetPtr();
777         Thread *thread = exe_ctx.GetThreadPtr();
778 
779         if (line == 0)
780         {
781             sb_error.SetErrorString("invalid line argument");
782             return sb_error;
783         }
784 
785         if (!frame_sp)
786         {
787             frame_sp = thread->GetSelectedFrame ();
788             if (!frame_sp)
789                 frame_sp = thread->GetStackFrameAtIndex (0);
790         }
791 
792         SymbolContext frame_sc;
793         if (!frame_sp)
794         {
795             sb_error.SetErrorString("no valid frames in thread to step");
796             return sb_error;
797         }
798 
799         // If we have a frame, get its line
800         frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit  |
801                                                eSymbolContextFunction  |
802                                                eSymbolContextLineEntry |
803                                                eSymbolContextSymbol    );
804 
805         if (frame_sc.comp_unit == NULL)
806         {
807             sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
808             return sb_error;
809         }
810 
811         FileSpec step_file_spec;
812         if (sb_file_spec.IsValid())
813         {
814             // The file spec passed in was valid, so use it
815             step_file_spec = sb_file_spec.ref();
816         }
817         else
818         {
819             if (frame_sc.line_entry.IsValid())
820                 step_file_spec = frame_sc.line_entry.file;
821             else
822             {
823                 sb_error.SetErrorString("invalid file argument or no file for frame");
824                 return sb_error;
825             }
826         }
827 
828         // Grab the current function, then we will make sure the "until" address is
829         // within the function.  We discard addresses that are out of the current
830         // function, and then if there are no addresses remaining, give an appropriate
831         // error message.
832 
833         bool all_in_function = true;
834         AddressRange fun_range = frame_sc.function->GetAddressRange();
835 
836         std::vector<addr_t> step_over_until_addrs;
837         const bool abort_other_plans = false;
838         const bool stop_other_threads = false;
839         const bool check_inlines = true;
840         const bool exact = false;
841 
842         SymbolContextList sc_list;
843         const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
844                                                                                line,
845                                                                                check_inlines,
846                                                                                exact,
847                                                                                eSymbolContextLineEntry,
848                                                                                sc_list);
849         if (num_matches > 0)
850         {
851             SymbolContext sc;
852             for (uint32_t i=0; i<num_matches; ++i)
853             {
854                 if (sc_list.GetContextAtIndex(i, sc))
855                 {
856                     addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
857                     if (step_addr != LLDB_INVALID_ADDRESS)
858                     {
859                         if (fun_range.ContainsLoadAddress(step_addr, target))
860                             step_over_until_addrs.push_back(step_addr);
861                         else
862                             all_in_function = false;
863                     }
864                 }
865             }
866         }
867 
868         if (step_over_until_addrs.empty())
869         {
870             if (all_in_function)
871             {
872                 step_file_spec.GetPath (path, sizeof(path));
873                 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
874             }
875             else
876                 sb_error.SetErrorString ("step until target not in current function");
877         }
878         else
879         {
880             ThreadPlan *new_plan = thread->QueueThreadPlanForStepUntil (abort_other_plans,
881                                                                         &step_over_until_addrs[0],
882                                                                         step_over_until_addrs.size(),
883                                                                         stop_other_threads,
884                                                                         frame_sp->GetFrameIndex());
885 
886             sb_error = ResumeNewPlan (exe_ctx, new_plan);
887         }
888     }
889     else
890     {
891         sb_error.SetErrorString("this SBThread object is invalid");
892     }
893     return sb_error;
894 }
895 
896 SBError
897 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
898 {
899     SBError sb_error;
900 
901     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
902 
903     Mutex::Locker api_locker;
904     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
905 
906 
907     if (log)
908         log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)", exe_ctx.GetThreadPtr(), frame.GetFrameID());
909 
910     if (exe_ctx.HasThreadScope())
911     {
912         Thread *thread = exe_ctx.GetThreadPtr();
913         sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
914     }
915 
916     return sb_error;
917 }
918 
919 
920 bool
921 SBThread::Suspend()
922 {
923     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
924     ExecutionContext exe_ctx (m_opaque_sp.get());
925     bool result = false;
926     if (exe_ctx.HasThreadScope())
927     {
928         Process::StopLocker stop_locker;
929         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
930         {
931             exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
932             result = true;
933         }
934         else
935         {
936             if (log)
937                 log->Printf ("SBThread(%p)::Suspend() => error: process is running", exe_ctx.GetThreadPtr());
938         }
939     }
940     if (log)
941         log->Printf ("SBThread(%p)::Suspend() => %i", exe_ctx.GetThreadPtr(), result);
942     return result;
943 }
944 
945 bool
946 SBThread::Resume ()
947 {
948     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
949     ExecutionContext exe_ctx (m_opaque_sp.get());
950     bool result = false;
951     if (exe_ctx.HasThreadScope())
952     {
953         Process::StopLocker stop_locker;
954         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
955         {
956             exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning);
957             result = true;
958         }
959         else
960         {
961             if (log)
962                 log->Printf ("SBThread(%p)::Resume() => error: process is running", exe_ctx.GetThreadPtr());
963         }
964     }
965     if (log)
966         log->Printf ("SBThread(%p)::Resume() => %i", exe_ctx.GetThreadPtr(), result);
967     return result;
968 }
969 
970 bool
971 SBThread::IsSuspended()
972 {
973     ExecutionContext exe_ctx (m_opaque_sp.get());
974     if (exe_ctx.HasThreadScope())
975         return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
976     return false;
977 }
978 
979 SBProcess
980 SBThread::GetProcess ()
981 {
982 
983     SBProcess sb_process;
984     ProcessSP process_sp;
985     ExecutionContext exe_ctx (m_opaque_sp.get());
986     if (exe_ctx.HasThreadScope())
987     {
988         // Have to go up to the target so we can get a shared pointer to our process...
989         sb_process.SetSP (exe_ctx.GetProcessSP());
990     }
991 
992     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
993     if (log)
994     {
995         SBStream frame_desc_strm;
996         sb_process.GetDescription (frame_desc_strm);
997         log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s", exe_ctx.GetThreadPtr(),
998                      process_sp.get(), frame_desc_strm.GetData());
999     }
1000 
1001     return sb_process;
1002 }
1003 
1004 uint32_t
1005 SBThread::GetNumFrames ()
1006 {
1007     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1008 
1009     uint32_t num_frames = 0;
1010     Mutex::Locker api_locker;
1011     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1012 
1013     if (exe_ctx.HasThreadScope())
1014     {
1015         Process::StopLocker stop_locker;
1016         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1017         {
1018             num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1019         }
1020         else
1021         {
1022             if (log)
1023                 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running", exe_ctx.GetThreadPtr());
1024         }
1025     }
1026 
1027     if (log)
1028         log->Printf ("SBThread(%p)::GetNumFrames () => %u", exe_ctx.GetThreadPtr(), num_frames);
1029 
1030     return num_frames;
1031 }
1032 
1033 SBFrame
1034 SBThread::GetFrameAtIndex (uint32_t idx)
1035 {
1036     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1037 
1038     SBFrame sb_frame;
1039     StackFrameSP frame_sp;
1040     Mutex::Locker api_locker;
1041     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1042 
1043     if (exe_ctx.HasThreadScope())
1044     {
1045         Process::StopLocker stop_locker;
1046         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1047         {
1048             frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1049             sb_frame.SetFrameSP (frame_sp);
1050         }
1051         else
1052         {
1053             if (log)
1054                 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running", exe_ctx.GetThreadPtr());
1055         }
1056     }
1057 
1058     if (log)
1059     {
1060         SBStream frame_desc_strm;
1061         sb_frame.GetDescription (frame_desc_strm);
1062         log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1063                      exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
1064     }
1065 
1066     return sb_frame;
1067 }
1068 
1069 lldb::SBFrame
1070 SBThread::GetSelectedFrame ()
1071 {
1072     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1073 
1074     SBFrame sb_frame;
1075     StackFrameSP frame_sp;
1076     Mutex::Locker api_locker;
1077     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1078 
1079     if (exe_ctx.HasThreadScope())
1080     {
1081         Process::StopLocker stop_locker;
1082         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1083         {
1084             frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1085             sb_frame.SetFrameSP (frame_sp);
1086         }
1087         else
1088         {
1089             if (log)
1090                 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1091         }
1092     }
1093 
1094     if (log)
1095     {
1096         SBStream frame_desc_strm;
1097         sb_frame.GetDescription (frame_desc_strm);
1098         log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1099                      exe_ctx.GetThreadPtr(), frame_sp.get(), frame_desc_strm.GetData());
1100     }
1101 
1102     return sb_frame;
1103 }
1104 
1105 lldb::SBFrame
1106 SBThread::SetSelectedFrame (uint32_t idx)
1107 {
1108     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1109 
1110     SBFrame sb_frame;
1111     StackFrameSP frame_sp;
1112     Mutex::Locker api_locker;
1113     ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1114 
1115     if (exe_ctx.HasThreadScope())
1116     {
1117         Process::StopLocker stop_locker;
1118         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1119         {
1120             Thread *thread = exe_ctx.GetThreadPtr();
1121             frame_sp = thread->GetStackFrameAtIndex (idx);
1122             if (frame_sp)
1123             {
1124                 thread->SetSelectedFrame (frame_sp.get());
1125                 sb_frame.SetFrameSP (frame_sp);
1126             }
1127         }
1128         else
1129         {
1130             if (log)
1131                 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running", exe_ctx.GetThreadPtr());
1132         }
1133     }
1134 
1135     if (log)
1136     {
1137         SBStream frame_desc_strm;
1138         sb_frame.GetDescription (frame_desc_strm);
1139         log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1140                      exe_ctx.GetThreadPtr(), idx, frame_sp.get(), frame_desc_strm.GetData());
1141     }
1142     return sb_frame;
1143 }
1144 
1145 bool
1146 SBThread::EventIsThreadEvent (const SBEvent &event)
1147 {
1148     return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1149 }
1150 
1151 SBFrame
1152 SBThread::GetStackFrameFromEvent (const SBEvent &event)
1153 {
1154     return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1155 
1156 }
1157 
1158 SBThread
1159 SBThread::GetThreadFromEvent (const SBEvent &event)
1160 {
1161     return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1162 }
1163 
1164 bool
1165 SBThread::operator == (const SBThread &rhs) const
1166 {
1167     return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
1168 }
1169 
1170 bool
1171 SBThread::operator != (const SBThread &rhs) const
1172 {
1173     return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
1174 }
1175 
1176 bool
1177 SBThread::GetStatus (SBStream &status) const
1178 {
1179     Stream &strm = status.ref();
1180 
1181     ExecutionContext exe_ctx (m_opaque_sp.get());
1182     if (exe_ctx.HasThreadScope())
1183     {
1184         exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1185     }
1186     else
1187         strm.PutCString ("No status");
1188 
1189     return true;
1190 }
1191 
1192 bool
1193 SBThread::GetDescription (SBStream &description) const
1194 {
1195     Stream &strm = description.ref();
1196 
1197     ExecutionContext exe_ctx (m_opaque_sp.get());
1198     if (exe_ctx.HasThreadScope())
1199     {
1200         strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1201     }
1202     else
1203         strm.PutCString ("No value");
1204 
1205     return true;
1206 }
1207