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/API/SBThread.h"
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/State.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Core/StreamFile.h"
20 #include "lldb/Core/StructuredData.h"
21 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Symbol/CompileUnit.h"
25 #include "lldb/Target/SystemRuntime.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/Queue.h"
29 #include "lldb/Target/UnixSignals.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)::GetQueue() => error: process is running",
118 static_cast<void*>(exe_ctx.GetThreadPtr()));
123 log->Printf ("SBThread(%p)::GetQueue () => 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);
830 log->Printf ("SBThread(%p)::StepOut ()",
831 static_cast<void*>(exe_ctx.GetThreadPtr()));
833 if (exe_ctx.HasThreadScope())
835 bool abort_other_plans = false;
836 bool stop_other_threads = false;
838 Thread *thread = exe_ctx.GetThreadPtr();
840 const LazyBool avoid_no_debug = eLazyBoolCalculate;
841 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
850 // This returns an error, we should use it!
851 ResumeNewPlan (exe_ctx, new_plan_sp.get());
856 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
858 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
860 Mutex::Locker api_locker;
861 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
863 if (!sb_frame.IsValid())
866 log->Printf("SBThread(%p)::StepOutOfFrame passed an invalid frame, returning.",
867 static_cast<void*>(exe_ctx.GetThreadPtr()));
871 StackFrameSP frame_sp (sb_frame.GetFrameSP());
874 SBStream frame_desc_strm;
875 sb_frame.GetDescription (frame_desc_strm);
876 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
877 static_cast<void*>(exe_ctx.GetThreadPtr()),
878 static_cast<void*>(frame_sp.get()),
879 frame_desc_strm.GetData());
882 if (exe_ctx.HasThreadScope())
884 bool abort_other_plans = false;
885 bool stop_other_threads = false;
886 Thread *thread = exe_ctx.GetThreadPtr();
887 if (sb_frame.GetThread().GetThreadID() != thread->GetID())
889 log->Printf("SBThread(%p)::StepOutOfFrame passed a frame from another thread (0x%" PRIx64 " vrs. 0x%" PRIx64 ", returning.",
890 static_cast<void*>(exe_ctx.GetThreadPtr()),
891 sb_frame.GetThread().GetThreadID(),
895 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
901 frame_sp->GetFrameIndex()));
903 // This returns an error, we should use it!
904 ResumeNewPlan (exe_ctx, new_plan_sp.get());
909 SBThread::StepInstruction (bool step_over)
911 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
913 Mutex::Locker api_locker;
914 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
919 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
920 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
922 if (exe_ctx.HasThreadScope())
924 Thread *thread = exe_ctx.GetThreadPtr();
925 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
927 // This returns an error, we should use it!
928 ResumeNewPlan (exe_ctx, new_plan_sp.get());
933 SBThread::RunToAddress (lldb::addr_t addr)
935 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
937 Mutex::Locker api_locker;
938 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
942 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
943 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
945 if (exe_ctx.HasThreadScope())
947 bool abort_other_plans = false;
948 bool stop_other_threads = true;
950 Address target_addr (addr);
952 Thread *thread = exe_ctx.GetThreadPtr();
954 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
956 stop_other_threads));
958 // This returns an error, we should use it!
959 ResumeNewPlan (exe_ctx, new_plan_sp.get());
964 SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
965 lldb::SBFileSpec &sb_file_spec,
969 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
972 Mutex::Locker api_locker;
973 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
975 StackFrameSP frame_sp (sb_frame.GetFrameSP());
979 SBStream frame_desc_strm;
980 sb_frame.GetDescription (frame_desc_strm);
981 sb_file_spec->GetPath (path, sizeof(path));
982 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
983 static_cast<void*>(exe_ctx.GetThreadPtr()),
984 static_cast<void*>(frame_sp.get()),
985 frame_desc_strm.GetData(), path, line);
988 if (exe_ctx.HasThreadScope())
990 Target *target = exe_ctx.GetTargetPtr();
991 Thread *thread = exe_ctx.GetThreadPtr();
995 sb_error.SetErrorString("invalid line argument");
1001 frame_sp = thread->GetSelectedFrame ();
1003 frame_sp = thread->GetStackFrameAtIndex (0);
1006 SymbolContext frame_sc;
1009 sb_error.SetErrorString("no valid frames in thread to step");
1013 // If we have a frame, get its line
1014 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
1015 eSymbolContextFunction |
1016 eSymbolContextLineEntry |
1017 eSymbolContextSymbol );
1019 if (frame_sc.comp_unit == NULL)
1021 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
1025 FileSpec step_file_spec;
1026 if (sb_file_spec.IsValid())
1028 // The file spec passed in was valid, so use it
1029 step_file_spec = sb_file_spec.ref();
1033 if (frame_sc.line_entry.IsValid())
1034 step_file_spec = frame_sc.line_entry.file;
1037 sb_error.SetErrorString("invalid file argument or no file for frame");
1042 // Grab the current function, then we will make sure the "until" address is
1043 // within the function. We discard addresses that are out of the current
1044 // function, and then if there are no addresses remaining, give an appropriate
1047 bool all_in_function = true;
1048 AddressRange fun_range = frame_sc.function->GetAddressRange();
1050 std::vector<addr_t> step_over_until_addrs;
1051 const bool abort_other_plans = false;
1052 const bool stop_other_threads = false;
1053 const bool check_inlines = true;
1054 const bool exact = false;
1056 SymbolContextList sc_list;
1057 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1061 eSymbolContextLineEntry,
1063 if (num_matches > 0)
1066 for (uint32_t i=0; i<num_matches; ++i)
1068 if (sc_list.GetContextAtIndex(i, sc))
1070 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
1071 if (step_addr != LLDB_INVALID_ADDRESS)
1073 if (fun_range.ContainsLoadAddress(step_addr, target))
1074 step_over_until_addrs.push_back(step_addr);
1076 all_in_function = false;
1082 if (step_over_until_addrs.empty())
1084 if (all_in_function)
1086 step_file_spec.GetPath (path, sizeof(path));
1087 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
1090 sb_error.SetErrorString ("step until target not in current function");
1094 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
1095 &step_over_until_addrs[0],
1096 step_over_until_addrs.size(),
1098 frame_sp->GetFrameIndex()));
1100 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
1105 sb_error.SetErrorString("this SBThread object is invalid");
1111 SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
1113 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1116 Mutex::Locker api_locker;
1117 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1121 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1122 static_cast<void*>(exe_ctx.GetThreadPtr()),
1127 if (!exe_ctx.HasThreadScope())
1129 sb_error.SetErrorString("this SBThread object is invalid");
1133 Thread *thread = exe_ctx.GetThreadPtr();
1134 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
1137 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1140 sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
1142 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
1143 static_cast<void*>(exe_ctx.GetThreadPtr()),
1151 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1153 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1156 Mutex::Locker api_locker;
1157 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1160 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1161 static_cast<void*>(exe_ctx.GetThreadPtr()),
1162 file_spec->GetPath().c_str(), line);
1164 if (!exe_ctx.HasThreadScope())
1166 sb_error.SetErrorString("this SBThread object is invalid");
1170 Thread *thread = exe_ctx.GetThreadPtr();
1172 Error err = thread->JumpToLine (file_spec.get(), line, true);
1173 sb_error.SetError (err);
1178 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
1182 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1184 Mutex::Locker api_locker;
1185 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1189 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1190 static_cast<void*>(exe_ctx.GetThreadPtr()),
1191 frame.GetFrameID());
1193 if (exe_ctx.HasThreadScope())
1195 Thread *thread = exe_ctx.GetThreadPtr();
1196 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
1206 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1207 ExecutionContext exe_ctx (m_opaque_sp.get());
1208 bool result = false;
1209 if (exe_ctx.HasThreadScope())
1211 Process::StopLocker stop_locker;
1212 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1214 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1220 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1221 static_cast<void*>(exe_ctx.GetThreadPtr()));
1225 log->Printf ("SBThread(%p)::Suspend() => %i",
1226 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1233 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1234 ExecutionContext exe_ctx (m_opaque_sp.get());
1235 bool result = false;
1236 if (exe_ctx.HasThreadScope())
1238 Process::StopLocker stop_locker;
1239 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1241 const bool override_suspend = true;
1242 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
1248 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1249 static_cast<void*>(exe_ctx.GetThreadPtr()));
1253 log->Printf ("SBThread(%p)::Resume() => %i",
1254 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1259 SBThread::IsSuspended()
1261 ExecutionContext exe_ctx (m_opaque_sp.get());
1262 if (exe_ctx.HasThreadScope())
1263 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
1268 SBThread::IsStopped()
1270 ExecutionContext exe_ctx (m_opaque_sp.get());
1271 if (exe_ctx.HasThreadScope())
1272 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1277 SBThread::GetProcess ()
1279 SBProcess sb_process;
1280 ExecutionContext exe_ctx (m_opaque_sp.get());
1281 if (exe_ctx.HasThreadScope())
1283 // Have to go up to the target so we can get a shared pointer to our process...
1284 sb_process.SetSP (exe_ctx.GetProcessSP());
1287 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1290 SBStream frame_desc_strm;
1291 sb_process.GetDescription (frame_desc_strm);
1292 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1293 static_cast<void*>(exe_ctx.GetThreadPtr()),
1294 static_cast<void*>(sb_process.GetSP().get()),
1295 frame_desc_strm.GetData());
1302 SBThread::GetNumFrames ()
1304 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1306 uint32_t num_frames = 0;
1307 Mutex::Locker api_locker;
1308 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1310 if (exe_ctx.HasThreadScope())
1312 Process::StopLocker stop_locker;
1313 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1315 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1320 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1321 static_cast<void*>(exe_ctx.GetThreadPtr()));
1326 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1327 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
1333 SBThread::GetFrameAtIndex (uint32_t idx)
1335 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1338 StackFrameSP frame_sp;
1339 Mutex::Locker api_locker;
1340 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1342 if (exe_ctx.HasThreadScope())
1344 Process::StopLocker stop_locker;
1345 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1347 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1348 sb_frame.SetFrameSP (frame_sp);
1353 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1354 static_cast<void*>(exe_ctx.GetThreadPtr()));
1360 SBStream frame_desc_strm;
1361 sb_frame.GetDescription (frame_desc_strm);
1362 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1363 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1364 static_cast<void*>(frame_sp.get()),
1365 frame_desc_strm.GetData());
1372 SBThread::GetSelectedFrame ()
1374 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1377 StackFrameSP frame_sp;
1378 Mutex::Locker api_locker;
1379 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1381 if (exe_ctx.HasThreadScope())
1383 Process::StopLocker stop_locker;
1384 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1386 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1387 sb_frame.SetFrameSP (frame_sp);
1392 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1393 static_cast<void*>(exe_ctx.GetThreadPtr()));
1399 SBStream frame_desc_strm;
1400 sb_frame.GetDescription (frame_desc_strm);
1401 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1402 static_cast<void*>(exe_ctx.GetThreadPtr()),
1403 static_cast<void*>(frame_sp.get()),
1404 frame_desc_strm.GetData());
1411 SBThread::SetSelectedFrame (uint32_t idx)
1413 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1416 StackFrameSP frame_sp;
1417 Mutex::Locker api_locker;
1418 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1420 if (exe_ctx.HasThreadScope())
1422 Process::StopLocker stop_locker;
1423 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1425 Thread *thread = exe_ctx.GetThreadPtr();
1426 frame_sp = thread->GetStackFrameAtIndex (idx);
1429 thread->SetSelectedFrame (frame_sp.get());
1430 sb_frame.SetFrameSP (frame_sp);
1436 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1437 static_cast<void*>(exe_ctx.GetThreadPtr()));
1443 SBStream frame_desc_strm;
1444 sb_frame.GetDescription (frame_desc_strm);
1445 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1446 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1447 static_cast<void*>(frame_sp.get()),
1448 frame_desc_strm.GetData());
1454 SBThread::EventIsThreadEvent (const SBEvent &event)
1456 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1460 SBThread::GetStackFrameFromEvent (const SBEvent &event)
1462 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1467 SBThread::GetThreadFromEvent (const SBEvent &event)
1469 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1473 SBThread::operator == (const SBThread &rhs) const
1475 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
1479 SBThread::operator != (const SBThread &rhs) const
1481 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
1485 SBThread::GetStatus (SBStream &status) const
1487 Stream &strm = status.ref();
1489 ExecutionContext exe_ctx (m_opaque_sp.get());
1490 if (exe_ctx.HasThreadScope())
1492 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1495 strm.PutCString ("No status");
1501 SBThread::GetDescription (SBStream &description) const
1503 Stream &strm = description.ref();
1505 ExecutionContext exe_ctx (m_opaque_sp.get());
1506 if (exe_ctx.HasThreadScope())
1508 exe_ctx.GetThreadPtr()->DumpUsingSettingsFormat(strm, LLDB_INVALID_THREAD_ID);
1509 //strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1512 strm.PutCString ("No value");
1518 SBThread::GetExtendedBacktraceThread (const char *type)
1520 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1521 Mutex::Locker api_locker;
1522 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1523 SBThread sb_origin_thread;
1525 if (exe_ctx.HasThreadScope())
1527 Process::StopLocker stop_locker;
1528 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1530 ThreadSP real_thread(exe_ctx.GetThreadSP());
1533 ConstString type_const (type);
1534 Process *process = exe_ctx.GetProcessPtr();
1537 SystemRuntime *runtime = process->GetSystemRuntime();
1540 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
1543 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1545 process->GetExtendedThreadList().AddThread (new_thread_sp);
1546 sb_origin_thread.SetThread (new_thread_sp);
1549 const char *queue_name = new_thread_sp->GetQueueName();
1550 if (queue_name == NULL)
1552 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
1553 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1554 static_cast<void*>(exe_ctx.GetThreadPtr()),
1555 static_cast<void*>(new_thread_sp.get()),
1556 new_thread_sp->GetQueueID(),
1567 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1568 static_cast<void*>(exe_ctx.GetThreadPtr()));
1572 if (log && sb_origin_thread.IsValid() == false)
1573 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1574 static_cast<void*>(exe_ctx.GetThreadPtr()));
1575 return sb_origin_thread;
1579 SBThread::GetExtendedBacktraceOriginatingIndexID ()
1581 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1583 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1584 return LLDB_INVALID_INDEX32;
1588 SBThread::SafeToCallFunctions ()
1590 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1592 return thread_sp->SafeToCallFunctions();
1596 lldb_private::Thread *
1597 SBThread::operator->()
1599 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1601 return thread_sp.get();
1606 lldb_private::Thread *
1609 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1611 return thread_sp.get();