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