1 //===-- SBThread.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/API/SBThread.h"
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/Core/StructuredData.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Target/SystemRuntime.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Queue.h"
28 #include "lldb/Symbol/SymbolContext.h"
29 #include "lldb/Symbol/CompileUnit.h"
30 #include "lldb/Target/StopInfo.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/ThreadPlan.h"
33 #include "lldb/Target/ThreadPlanStepInstruction.h"
34 #include "lldb/Target/ThreadPlanStepOut.h"
35 #include "lldb/Target/ThreadPlanStepRange.h"
36 #include "lldb/Target/ThreadPlanStepInRange.h"
39 #include "lldb/API/SBAddress.h"
40 #include "lldb/API/SBDebugger.h"
41 #include "lldb/API/SBEvent.h"
42 #include "lldb/API/SBFrame.h"
43 #include "lldb/API/SBProcess.h"
44 #include "lldb/API/SBThreadPlan.h"
45 #include "lldb/API/SBValue.h"
48 using namespace lldb_private;
51 SBThread::GetBroadcasterClassName ()
53 return Thread::GetStaticBroadcasterClass().AsCString();
56 //----------------------------------------------------------------------
58 //----------------------------------------------------------------------
59 SBThread::SBThread () :
60 m_opaque_sp (new ExecutionContextRef())
64 SBThread::SBThread (const ThreadSP& lldb_object_sp) :
65 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
69 SBThread::SBThread (const SBThread &rhs) :
70 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
75 //----------------------------------------------------------------------
76 // Assignment operator
77 //----------------------------------------------------------------------
79 const lldb::SBThread &
80 SBThread::operator = (const SBThread &rhs)
83 *m_opaque_sp = *rhs.m_opaque_sp;
87 //----------------------------------------------------------------------
89 //----------------------------------------------------------------------
95 SBThread::GetQueue () const
99 Mutex::Locker api_locker;
100 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
102 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
103 if (exe_ctx.HasThreadScope())
105 Process::StopLocker stop_locker;
106 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
108 queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
111 sb_queue.SetQueue (queue_sp);
117 log->Printf ("SBThread(%p)::GetQueueKind() => error: process is running",
118 static_cast<void*>(exe_ctx.GetThreadPtr()));
123 log->Printf ("SBThread(%p)::GetQueueKind () => SBQueue(%p)",
124 static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get()));
131 SBThread::IsValid() const
133 return m_opaque_sp->GetThreadSP().get() != NULL;
139 m_opaque_sp->Clear();
144 SBThread::GetStopReason()
146 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
148 StopReason reason = eStopReasonInvalid;
149 Mutex::Locker api_locker;
150 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
152 if (exe_ctx.HasThreadScope())
154 Process::StopLocker stop_locker;
155 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
157 return exe_ctx.GetThreadPtr()->GetStopReason();
162 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
163 static_cast<void*>(exe_ctx.GetThreadPtr()));
168 log->Printf ("SBThread(%p)::GetStopReason () => %s",
169 static_cast<void*>(exe_ctx.GetThreadPtr()),
170 Thread::StopReasonAsCString (reason));
176 SBThread::GetStopReasonDataCount ()
178 Mutex::Locker api_locker;
179 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
181 if (exe_ctx.HasThreadScope())
183 Process::StopLocker stop_locker;
184 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
186 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
189 StopReason reason = stop_info_sp->GetStopReason();
192 case eStopReasonInvalid:
193 case eStopReasonNone:
194 case eStopReasonTrace:
195 case eStopReasonExec:
196 case eStopReasonPlanComplete:
197 case eStopReasonThreadExiting:
198 case eStopReasonInstrumentation:
199 // There is no data for these stop reasons.
202 case eStopReasonBreakpoint:
204 break_id_t site_id = stop_info_sp->GetValue();
205 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
207 return bp_site_sp->GetNumberOfOwners () * 2;
209 return 0; // Breakpoint must have cleared itself...
213 case eStopReasonWatchpoint:
216 case eStopReasonSignal:
219 case eStopReasonException:
226 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
228 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
229 static_cast<void*>(exe_ctx.GetThreadPtr()));
236 SBThread::GetStopReasonDataAtIndex (uint32_t idx)
238 Mutex::Locker api_locker;
239 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
241 if (exe_ctx.HasThreadScope())
243 Process::StopLocker stop_locker;
244 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
246 Thread *thread = exe_ctx.GetThreadPtr();
247 StopInfoSP stop_info_sp = thread->GetStopInfo ();
250 StopReason reason = stop_info_sp->GetStopReason();
253 case eStopReasonInvalid:
254 case eStopReasonNone:
255 case eStopReasonTrace:
256 case eStopReasonExec:
257 case eStopReasonPlanComplete:
258 case eStopReasonThreadExiting:
259 case eStopReasonInstrumentation:
260 // There is no data for these stop reasons.
263 case eStopReasonBreakpoint:
265 break_id_t site_id = stop_info_sp->GetValue();
266 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
269 uint32_t bp_index = idx / 2;
270 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
275 // Odd idx, return the breakpoint location ID
276 return bp_loc_sp->GetID();
280 // Even idx, return the breakpoint ID
281 return bp_loc_sp->GetBreakpoint().GetID();
285 return LLDB_INVALID_BREAK_ID;
289 case eStopReasonWatchpoint:
290 return stop_info_sp->GetValue();
292 case eStopReasonSignal:
293 return stop_info_sp->GetValue();
295 case eStopReasonException:
296 return stop_info_sp->GetValue();
302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
304 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
305 static_cast<void*>(exe_ctx.GetThreadPtr()));
312 SBThread::GetStopReasonExtendedInfoAsJSON (lldb::SBStream &stream)
314 Stream &strm = stream.ref();
316 ExecutionContext exe_ctx (m_opaque_sp.get());
317 if (! exe_ctx.HasThreadScope())
321 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
322 StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
332 SBThread::GetStopDescription (char *dst, size_t dst_len)
334 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
336 Mutex::Locker api_locker;
337 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
339 if (exe_ctx.HasThreadScope())
341 Process::StopLocker stop_locker;
342 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
345 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
348 const char *stop_desc = stop_info_sp->GetDescription();
352 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
353 static_cast<void*>(exe_ctx.GetThreadPtr()),
356 return ::snprintf (dst, dst_len, "%s", stop_desc);
359 // NULL dst passed in, return the length needed to contain the description
360 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
365 size_t stop_desc_len = 0;
366 switch (stop_info_sp->GetStopReason())
368 case eStopReasonTrace:
369 case eStopReasonPlanComplete:
371 static char trace_desc[] = "step";
372 stop_desc = trace_desc;
373 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
377 case eStopReasonBreakpoint:
379 static char bp_desc[] = "breakpoint hit";
381 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
385 case eStopReasonWatchpoint:
387 static char wp_desc[] = "watchpoint hit";
389 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
393 case eStopReasonSignal:
395 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
396 if (stop_desc == NULL || stop_desc[0] == '\0')
398 static char signal_desc[] = "signal";
399 stop_desc = signal_desc;
400 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
405 case eStopReasonException:
407 char exc_desc[] = "exception";
408 stop_desc = exc_desc;
409 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
413 case eStopReasonExec:
415 char exc_desc[] = "exec";
416 stop_desc = exc_desc;
417 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
421 case eStopReasonThreadExiting:
423 char limbo_desc[] = "thread exiting";
424 stop_desc = limbo_desc;
425 stop_desc_len = sizeof(limbo_desc);
432 if (stop_desc && stop_desc[0])
435 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
436 static_cast<void*>(exe_ctx.GetThreadPtr()),
440 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
442 if (stop_desc_len == 0)
443 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
445 return stop_desc_len;
452 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
454 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
455 static_cast<void*>(exe_ctx.GetThreadPtr()));
464 SBThread::GetStopReturnValue ()
466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
467 ValueObjectSP return_valobj_sp;
468 Mutex::Locker api_locker;
469 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
471 if (exe_ctx.HasThreadScope())
473 Process::StopLocker stop_locker;
474 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
476 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
479 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
485 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
486 static_cast<void*>(exe_ctx.GetThreadPtr()));
491 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
492 static_cast<void*>(exe_ctx.GetThreadPtr()),
493 return_valobj_sp.get()
494 ? return_valobj_sp->GetValueAsCString()
495 : "<no return value>");
497 return SBValue (return_valobj_sp);
501 SBThread::SetThread (const ThreadSP& lldb_object_sp)
503 m_opaque_sp->SetThreadSP (lldb_object_sp);
507 SBThread::GetThreadID () const
509 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
511 return thread_sp->GetID();
512 return LLDB_INVALID_THREAD_ID;
516 SBThread::GetIndexID () const
518 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
520 return thread_sp->GetIndexID();
521 return LLDB_INVALID_INDEX32;
525 SBThread::GetName () const
527 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
528 const char *name = NULL;
529 Mutex::Locker api_locker;
530 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
532 if (exe_ctx.HasThreadScope())
534 Process::StopLocker stop_locker;
535 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
537 name = exe_ctx.GetThreadPtr()->GetName();
542 log->Printf ("SBThread(%p)::GetName() => error: process is running",
543 static_cast<void*>(exe_ctx.GetThreadPtr()));
548 log->Printf ("SBThread(%p)::GetName () => %s",
549 static_cast<void*>(exe_ctx.GetThreadPtr()),
550 name ? name : "NULL");
556 SBThread::GetQueueName () const
558 const char *name = NULL;
559 Mutex::Locker api_locker;
560 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
563 if (exe_ctx.HasThreadScope())
565 Process::StopLocker stop_locker;
566 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
568 name = exe_ctx.GetThreadPtr()->GetQueueName();
573 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
574 static_cast<void*>(exe_ctx.GetThreadPtr()));
579 log->Printf ("SBThread(%p)::GetQueueName () => %s",
580 static_cast<void*>(exe_ctx.GetThreadPtr()),
581 name ? name : "NULL");
587 SBThread::GetQueueID () const
589 queue_id_t id = LLDB_INVALID_QUEUE_ID;
590 Mutex::Locker api_locker;
591 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
593 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
594 if (exe_ctx.HasThreadScope())
596 Process::StopLocker stop_locker;
597 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
599 id = exe_ctx.GetThreadPtr()->GetQueueID();
604 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
605 static_cast<void*>(exe_ctx.GetThreadPtr()));
610 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
611 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
617 SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
619 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
620 bool success = false;
621 Mutex::Locker api_locker;
622 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
624 if (exe_ctx.HasThreadScope())
626 Process::StopLocker stop_locker;
627 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
629 Thread *thread = exe_ctx.GetThreadPtr();
630 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
633 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
636 if (node->GetType() == StructuredData::Type::eTypeString)
638 strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
641 if (node->GetType() == StructuredData::Type::eTypeInteger)
643 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
646 if (node->GetType() == StructuredData::Type::eTypeFloat)
648 strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
651 if (node->GetType() == StructuredData::Type::eTypeBoolean)
653 if (node->GetAsBoolean()->GetValue() == true)
654 strm.Printf ("true");
656 strm.Printf ("false");
659 if (node->GetType() == StructuredData::Type::eTypeNull)
661 strm.Printf ("null");
670 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
671 static_cast<void*>(exe_ctx.GetThreadPtr()));
676 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
677 static_cast<void*>(exe_ctx.GetThreadPtr()),
685 SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
689 Process *process = exe_ctx.GetProcessPtr();
692 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
696 Thread *thread = exe_ctx.GetThreadPtr();
699 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
703 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
704 // then a "continue" will resume the plan.
705 if (new_plan != NULL)
707 new_plan->SetIsMasterPlan(true);
708 new_plan->SetOkayToDiscard(false);
711 // Why do we need to set the current thread by ID here???
712 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
714 if (process->GetTarget().GetDebugger().GetAsyncExecution ())
715 sb_error.ref() = process->Resume ();
717 sb_error.ref() = process->ResumeSynchronous (NULL);
723 SBThread::StepOver (lldb::RunMode stop_other_threads)
725 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
727 Mutex::Locker api_locker;
728 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
732 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
733 static_cast<void*>(exe_ctx.GetThreadPtr()),
734 Thread::RunModeAsCString (stop_other_threads));
736 if (exe_ctx.HasThreadScope())
738 Thread *thread = exe_ctx.GetThreadPtr();
739 bool abort_other_plans = false;
740 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
742 ThreadPlanSP new_plan_sp;
745 if (frame_sp->HasDebugInformation ())
747 const LazyBool avoid_no_debug = eLazyBoolCalculate;
748 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
749 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
757 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
763 // This returns an error, we should use it!
764 ResumeNewPlan (exe_ctx, new_plan_sp.get());
769 SBThread::StepInto (lldb::RunMode stop_other_threads)
771 StepInto (NULL, stop_other_threads);
775 SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
777 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
779 Mutex::Locker api_locker;
780 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
783 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
784 static_cast<void*>(exe_ctx.GetThreadPtr()),
785 target_name? target_name: "<NULL>",
786 Thread::RunModeAsCString (stop_other_threads));
788 if (exe_ctx.HasThreadScope())
790 bool abort_other_plans = false;
792 Thread *thread = exe_ctx.GetThreadPtr();
793 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
794 ThreadPlanSP new_plan_sp;
796 if (frame_sp && frame_sp->HasDebugInformation ())
798 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
799 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
800 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
801 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
806 step_in_avoids_code_without_debug_info,
807 step_out_avoids_code_without_debug_info);
811 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
816 // This returns an error, we should use it!
817 ResumeNewPlan (exe_ctx, new_plan_sp.get());
824 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
826 Mutex::Locker api_locker;
827 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
831 log->Printf ("SBThread(%p)::StepOut ()",
832 static_cast<void*>(exe_ctx.GetThreadPtr()));
834 if (exe_ctx.HasThreadScope())
836 bool abort_other_plans = false;
837 bool stop_other_threads = false;
839 Thread *thread = exe_ctx.GetThreadPtr();
841 const LazyBool avoid_no_debug = eLazyBoolCalculate;
842 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
851 // This returns an error, we should use it!
852 ResumeNewPlan (exe_ctx, new_plan_sp.get());
857 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
859 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
861 Mutex::Locker api_locker;
862 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
864 StackFrameSP frame_sp (sb_frame.GetFrameSP());
867 SBStream frame_desc_strm;
868 sb_frame.GetDescription (frame_desc_strm);
869 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
870 static_cast<void*>(exe_ctx.GetThreadPtr()),
871 static_cast<void*>(frame_sp.get()),
872 frame_desc_strm.GetData());
875 if (exe_ctx.HasThreadScope())
877 bool abort_other_plans = false;
878 bool stop_other_threads = false;
879 Thread *thread = exe_ctx.GetThreadPtr();
881 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
887 frame_sp->GetFrameIndex()));
889 // This returns an error, we should use it!
890 ResumeNewPlan (exe_ctx, new_plan_sp.get());
895 SBThread::StepInstruction (bool step_over)
897 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
899 Mutex::Locker api_locker;
900 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
905 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
906 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
908 if (exe_ctx.HasThreadScope())
910 Thread *thread = exe_ctx.GetThreadPtr();
911 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
913 // This returns an error, we should use it!
914 ResumeNewPlan (exe_ctx, new_plan_sp.get());
919 SBThread::RunToAddress (lldb::addr_t addr)
921 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
923 Mutex::Locker api_locker;
924 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
928 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
929 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
931 if (exe_ctx.HasThreadScope())
933 bool abort_other_plans = false;
934 bool stop_other_threads = true;
936 Address target_addr (addr);
938 Thread *thread = exe_ctx.GetThreadPtr();
940 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
942 stop_other_threads));
944 // This returns an error, we should use it!
945 ResumeNewPlan (exe_ctx, new_plan_sp.get());
950 SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
951 lldb::SBFileSpec &sb_file_spec,
955 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
958 Mutex::Locker api_locker;
959 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
961 StackFrameSP frame_sp (sb_frame.GetFrameSP());
965 SBStream frame_desc_strm;
966 sb_frame.GetDescription (frame_desc_strm);
967 sb_file_spec->GetPath (path, sizeof(path));
968 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
969 static_cast<void*>(exe_ctx.GetThreadPtr()),
970 static_cast<void*>(frame_sp.get()),
971 frame_desc_strm.GetData(), path, line);
974 if (exe_ctx.HasThreadScope())
976 Target *target = exe_ctx.GetTargetPtr();
977 Thread *thread = exe_ctx.GetThreadPtr();
981 sb_error.SetErrorString("invalid line argument");
987 frame_sp = thread->GetSelectedFrame ();
989 frame_sp = thread->GetStackFrameAtIndex (0);
992 SymbolContext frame_sc;
995 sb_error.SetErrorString("no valid frames in thread to step");
999 // If we have a frame, get its line
1000 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
1001 eSymbolContextFunction |
1002 eSymbolContextLineEntry |
1003 eSymbolContextSymbol );
1005 if (frame_sc.comp_unit == NULL)
1007 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
1011 FileSpec step_file_spec;
1012 if (sb_file_spec.IsValid())
1014 // The file spec passed in was valid, so use it
1015 step_file_spec = sb_file_spec.ref();
1019 if (frame_sc.line_entry.IsValid())
1020 step_file_spec = frame_sc.line_entry.file;
1023 sb_error.SetErrorString("invalid file argument or no file for frame");
1028 // Grab the current function, then we will make sure the "until" address is
1029 // within the function. We discard addresses that are out of the current
1030 // function, and then if there are no addresses remaining, give an appropriate
1033 bool all_in_function = true;
1034 AddressRange fun_range = frame_sc.function->GetAddressRange();
1036 std::vector<addr_t> step_over_until_addrs;
1037 const bool abort_other_plans = false;
1038 const bool stop_other_threads = false;
1039 const bool check_inlines = true;
1040 const bool exact = false;
1042 SymbolContextList sc_list;
1043 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1047 eSymbolContextLineEntry,
1049 if (num_matches > 0)
1052 for (uint32_t i=0; i<num_matches; ++i)
1054 if (sc_list.GetContextAtIndex(i, sc))
1056 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
1057 if (step_addr != LLDB_INVALID_ADDRESS)
1059 if (fun_range.ContainsLoadAddress(step_addr, target))
1060 step_over_until_addrs.push_back(step_addr);
1062 all_in_function = false;
1068 if (step_over_until_addrs.empty())
1070 if (all_in_function)
1072 step_file_spec.GetPath (path, sizeof(path));
1073 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
1076 sb_error.SetErrorString ("step until target not in current function");
1080 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
1081 &step_over_until_addrs[0],
1082 step_over_until_addrs.size(),
1084 frame_sp->GetFrameIndex()));
1086 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
1091 sb_error.SetErrorString("this SBThread object is invalid");
1097 SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
1099 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1102 Mutex::Locker api_locker;
1103 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1107 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1108 static_cast<void*>(exe_ctx.GetThreadPtr()),
1113 if (!exe_ctx.HasThreadScope())
1115 sb_error.SetErrorString("this SBThread object is invalid");
1119 Thread *thread = exe_ctx.GetThreadPtr();
1120 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
1123 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1126 sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
1128 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
1129 static_cast<void*>(exe_ctx.GetThreadPtr()),
1137 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1139 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1142 Mutex::Locker api_locker;
1143 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1146 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1147 static_cast<void*>(exe_ctx.GetThreadPtr()),
1148 file_spec->GetPath().c_str(), line);
1150 if (!exe_ctx.HasThreadScope())
1152 sb_error.SetErrorString("this SBThread object is invalid");
1156 Thread *thread = exe_ctx.GetThreadPtr();
1158 Error err = thread->JumpToLine (file_spec.get(), line, true);
1159 sb_error.SetError (err);
1164 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
1168 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1170 Mutex::Locker api_locker;
1171 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1175 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1176 static_cast<void*>(exe_ctx.GetThreadPtr()),
1177 frame.GetFrameID());
1179 if (exe_ctx.HasThreadScope())
1181 Thread *thread = exe_ctx.GetThreadPtr();
1182 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
1192 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1193 ExecutionContext exe_ctx (m_opaque_sp.get());
1194 bool result = false;
1195 if (exe_ctx.HasThreadScope())
1197 Process::StopLocker stop_locker;
1198 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1200 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1206 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1207 static_cast<void*>(exe_ctx.GetThreadPtr()));
1211 log->Printf ("SBThread(%p)::Suspend() => %i",
1212 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1219 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1220 ExecutionContext exe_ctx (m_opaque_sp.get());
1221 bool result = false;
1222 if (exe_ctx.HasThreadScope())
1224 Process::StopLocker stop_locker;
1225 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1227 const bool override_suspend = true;
1228 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
1234 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1235 static_cast<void*>(exe_ctx.GetThreadPtr()));
1239 log->Printf ("SBThread(%p)::Resume() => %i",
1240 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1245 SBThread::IsSuspended()
1247 ExecutionContext exe_ctx (m_opaque_sp.get());
1248 if (exe_ctx.HasThreadScope())
1249 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
1254 SBThread::IsStopped()
1256 ExecutionContext exe_ctx (m_opaque_sp.get());
1257 if (exe_ctx.HasThreadScope())
1258 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1263 SBThread::GetProcess ()
1265 SBProcess sb_process;
1266 ExecutionContext exe_ctx (m_opaque_sp.get());
1267 if (exe_ctx.HasThreadScope())
1269 // Have to go up to the target so we can get a shared pointer to our process...
1270 sb_process.SetSP (exe_ctx.GetProcessSP());
1273 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1276 SBStream frame_desc_strm;
1277 sb_process.GetDescription (frame_desc_strm);
1278 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1279 static_cast<void*>(exe_ctx.GetThreadPtr()),
1280 static_cast<void*>(sb_process.GetSP().get()),
1281 frame_desc_strm.GetData());
1288 SBThread::GetNumFrames ()
1290 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1292 uint32_t num_frames = 0;
1293 Mutex::Locker api_locker;
1294 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1296 if (exe_ctx.HasThreadScope())
1298 Process::StopLocker stop_locker;
1299 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1301 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1306 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1307 static_cast<void*>(exe_ctx.GetThreadPtr()));
1312 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1313 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
1319 SBThread::GetFrameAtIndex (uint32_t idx)
1321 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1324 StackFrameSP frame_sp;
1325 Mutex::Locker api_locker;
1326 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1328 if (exe_ctx.HasThreadScope())
1330 Process::StopLocker stop_locker;
1331 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1333 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1334 sb_frame.SetFrameSP (frame_sp);
1339 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1340 static_cast<void*>(exe_ctx.GetThreadPtr()));
1346 SBStream frame_desc_strm;
1347 sb_frame.GetDescription (frame_desc_strm);
1348 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1349 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1350 static_cast<void*>(frame_sp.get()),
1351 frame_desc_strm.GetData());
1358 SBThread::GetSelectedFrame ()
1360 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1363 StackFrameSP frame_sp;
1364 Mutex::Locker api_locker;
1365 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1367 if (exe_ctx.HasThreadScope())
1369 Process::StopLocker stop_locker;
1370 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1372 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1373 sb_frame.SetFrameSP (frame_sp);
1378 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1379 static_cast<void*>(exe_ctx.GetThreadPtr()));
1385 SBStream frame_desc_strm;
1386 sb_frame.GetDescription (frame_desc_strm);
1387 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1388 static_cast<void*>(exe_ctx.GetThreadPtr()),
1389 static_cast<void*>(frame_sp.get()),
1390 frame_desc_strm.GetData());
1397 SBThread::SetSelectedFrame (uint32_t idx)
1399 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1402 StackFrameSP frame_sp;
1403 Mutex::Locker api_locker;
1404 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1406 if (exe_ctx.HasThreadScope())
1408 Process::StopLocker stop_locker;
1409 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1411 Thread *thread = exe_ctx.GetThreadPtr();
1412 frame_sp = thread->GetStackFrameAtIndex (idx);
1415 thread->SetSelectedFrame (frame_sp.get());
1416 sb_frame.SetFrameSP (frame_sp);
1422 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1423 static_cast<void*>(exe_ctx.GetThreadPtr()));
1429 SBStream frame_desc_strm;
1430 sb_frame.GetDescription (frame_desc_strm);
1431 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1432 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1433 static_cast<void*>(frame_sp.get()),
1434 frame_desc_strm.GetData());
1440 SBThread::EventIsThreadEvent (const SBEvent &event)
1442 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1446 SBThread::GetStackFrameFromEvent (const SBEvent &event)
1448 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1453 SBThread::GetThreadFromEvent (const SBEvent &event)
1455 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1459 SBThread::operator == (const SBThread &rhs) const
1461 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
1465 SBThread::operator != (const SBThread &rhs) const
1467 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
1471 SBThread::GetStatus (SBStream &status) const
1473 Stream &strm = status.ref();
1475 ExecutionContext exe_ctx (m_opaque_sp.get());
1476 if (exe_ctx.HasThreadScope())
1478 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1481 strm.PutCString ("No status");
1487 SBThread::GetDescription (SBStream &description) const
1489 Stream &strm = description.ref();
1491 ExecutionContext exe_ctx (m_opaque_sp.get());
1492 if (exe_ctx.HasThreadScope())
1494 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1497 strm.PutCString ("No value");
1503 SBThread::GetExtendedBacktraceThread (const char *type)
1505 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1506 Mutex::Locker api_locker;
1507 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1508 SBThread sb_origin_thread;
1510 if (exe_ctx.HasThreadScope())
1512 Process::StopLocker stop_locker;
1513 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1515 ThreadSP real_thread(exe_ctx.GetThreadSP());
1518 ConstString type_const (type);
1519 Process *process = exe_ctx.GetProcessPtr();
1522 SystemRuntime *runtime = process->GetSystemRuntime();
1525 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
1528 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1530 process->GetExtendedThreadList().AddThread (new_thread_sp);
1531 sb_origin_thread.SetThread (new_thread_sp);
1534 const char *queue_name = new_thread_sp->GetQueueName();
1535 if (queue_name == NULL)
1537 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
1538 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1539 static_cast<void*>(exe_ctx.GetThreadPtr()),
1540 static_cast<void*>(new_thread_sp.get()),
1541 new_thread_sp->GetQueueID(),
1552 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1553 static_cast<void*>(exe_ctx.GetThreadPtr()));
1557 if (log && sb_origin_thread.IsValid() == false)
1558 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1559 static_cast<void*>(exe_ctx.GetThreadPtr()));
1560 return sb_origin_thread;
1564 SBThread::GetExtendedBacktraceOriginatingIndexID ()
1566 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1568 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1569 return LLDB_INVALID_INDEX32;
1573 SBThread::SafeToCallFunctions ()
1575 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1577 return thread_sp->SafeToCallFunctions();
1581 lldb_private::Thread *
1582 SBThread::operator->()
1584 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1586 return thread_sp.get();
1591 lldb_private::Thread *
1594 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1596 return thread_sp.get();