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