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"
38 #include "lldb/API/SBAddress.h"
39 #include "lldb/API/SBDebugger.h"
40 #include "lldb/API/SBEvent.h"
41 #include "lldb/API/SBFrame.h"
42 #include "lldb/API/SBProcess.h"
43 #include "lldb/API/SBThreadCollection.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 std::unique_lock<std::recursive_mutex> lock;
100 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
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 std::unique_lock<std::recursive_mutex> lock;
134 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
136 Target *target = exe_ctx.GetTargetPtr();
137 Process *process = exe_ctx.GetProcessPtr();
138 if (target && process)
140 Process::StopLocker stop_locker;
141 if (stop_locker.TryLock(&process->GetRunLock()))
142 return m_opaque_sp->GetThreadSP().get() != NULL;
144 // Without a valid target & process, this thread can't be valid.
151 m_opaque_sp->Clear();
156 SBThread::GetStopReason()
158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
160 StopReason reason = eStopReasonInvalid;
161 std::unique_lock<std::recursive_mutex> lock;
162 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
164 if (exe_ctx.HasThreadScope())
166 Process::StopLocker stop_locker;
167 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
169 return exe_ctx.GetThreadPtr()->GetStopReason();
174 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
175 static_cast<void*>(exe_ctx.GetThreadPtr()));
180 log->Printf ("SBThread(%p)::GetStopReason () => %s",
181 static_cast<void*>(exe_ctx.GetThreadPtr()),
182 Thread::StopReasonAsCString (reason));
188 SBThread::GetStopReasonDataCount ()
190 std::unique_lock<std::recursive_mutex> lock;
191 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
193 if (exe_ctx.HasThreadScope())
195 Process::StopLocker stop_locker;
196 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
198 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
201 StopReason reason = stop_info_sp->GetStopReason();
204 case eStopReasonInvalid:
205 case eStopReasonNone:
206 case eStopReasonTrace:
207 case eStopReasonExec:
208 case eStopReasonPlanComplete:
209 case eStopReasonThreadExiting:
210 case eStopReasonInstrumentation:
211 // There is no data for these stop reasons.
214 case eStopReasonBreakpoint:
216 break_id_t site_id = stop_info_sp->GetValue();
217 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
219 return bp_site_sp->GetNumberOfOwners () * 2;
221 return 0; // Breakpoint must have cleared itself...
225 case eStopReasonWatchpoint:
228 case eStopReasonSignal:
231 case eStopReasonException:
238 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
240 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
241 static_cast<void*>(exe_ctx.GetThreadPtr()));
248 SBThread::GetStopReasonDataAtIndex (uint32_t idx)
250 std::unique_lock<std::recursive_mutex> lock;
251 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
253 if (exe_ctx.HasThreadScope())
255 Process::StopLocker stop_locker;
256 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
258 Thread *thread = exe_ctx.GetThreadPtr();
259 StopInfoSP stop_info_sp = thread->GetStopInfo ();
262 StopReason reason = stop_info_sp->GetStopReason();
265 case eStopReasonInvalid:
266 case eStopReasonNone:
267 case eStopReasonTrace:
268 case eStopReasonExec:
269 case eStopReasonPlanComplete:
270 case eStopReasonThreadExiting:
271 case eStopReasonInstrumentation:
272 // There is no data for these stop reasons.
275 case eStopReasonBreakpoint:
277 break_id_t site_id = stop_info_sp->GetValue();
278 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
281 uint32_t bp_index = idx / 2;
282 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
287 // Odd idx, return the breakpoint location ID
288 return bp_loc_sp->GetID();
292 // Even idx, return the breakpoint ID
293 return bp_loc_sp->GetBreakpoint().GetID();
297 return LLDB_INVALID_BREAK_ID;
301 case eStopReasonWatchpoint:
302 return stop_info_sp->GetValue();
304 case eStopReasonSignal:
305 return stop_info_sp->GetValue();
307 case eStopReasonException:
308 return stop_info_sp->GetValue();
314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
316 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
317 static_cast<void*>(exe_ctx.GetThreadPtr()));
324 SBThread::GetStopReasonExtendedInfoAsJSON (lldb::SBStream &stream)
326 Stream &strm = stream.ref();
328 std::unique_lock<std::recursive_mutex> lock;
329 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
331 if (! exe_ctx.HasThreadScope())
335 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
336 StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
346 SBThread::GetStopReasonExtendedBacktraces (InstrumentationRuntimeType type)
348 ThreadCollectionSP threads;
349 threads.reset(new ThreadCollection());
351 // We currently only support ThreadSanitizer.
352 if (type != eInstrumentationRuntimeTypeThreadSanitizer)
355 std::unique_lock<std::recursive_mutex> lock;
356 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
358 if (! exe_ctx.HasThreadScope())
361 ProcessSP process_sp = exe_ctx.GetProcessSP();
363 StopInfoSP stop_info = exe_ctx.GetThreadPtr()->GetStopInfo();
364 StructuredData::ObjectSP info = stop_info->GetExtendedInfo();
368 return process_sp->GetInstrumentationRuntime(type)->GetBacktracesFromExtendedStopInfo(info);
372 SBThread::GetStopDescription (char *dst, size_t dst_len)
374 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
376 std::unique_lock<std::recursive_mutex> lock;
377 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
379 if (exe_ctx.HasThreadScope())
381 Process::StopLocker stop_locker;
382 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
385 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
388 const char *stop_desc = stop_info_sp->GetDescription();
392 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
393 static_cast<void*>(exe_ctx.GetThreadPtr()),
396 return ::snprintf (dst, dst_len, "%s", stop_desc);
399 // NULL dst passed in, return the length needed to contain the description
400 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
405 size_t stop_desc_len = 0;
406 switch (stop_info_sp->GetStopReason())
408 case eStopReasonTrace:
409 case eStopReasonPlanComplete:
411 static char trace_desc[] = "step";
412 stop_desc = trace_desc;
413 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
417 case eStopReasonBreakpoint:
419 static char bp_desc[] = "breakpoint hit";
421 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
425 case eStopReasonWatchpoint:
427 static char wp_desc[] = "watchpoint hit";
429 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
433 case eStopReasonSignal:
435 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals()->GetSignalAsCString(stop_info_sp->GetValue());
436 if (stop_desc == NULL || stop_desc[0] == '\0')
438 static char signal_desc[] = "signal";
439 stop_desc = signal_desc;
440 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
445 case eStopReasonException:
447 char exc_desc[] = "exception";
448 stop_desc = exc_desc;
449 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
453 case eStopReasonExec:
455 char exc_desc[] = "exec";
456 stop_desc = exc_desc;
457 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
461 case eStopReasonThreadExiting:
463 char limbo_desc[] = "thread exiting";
464 stop_desc = limbo_desc;
465 stop_desc_len = sizeof(limbo_desc);
472 if (stop_desc && stop_desc[0])
475 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
476 static_cast<void*>(exe_ctx.GetThreadPtr()),
480 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
482 if (stop_desc_len == 0)
483 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
485 return stop_desc_len;
492 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
494 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
495 static_cast<void*>(exe_ctx.GetThreadPtr()));
504 SBThread::GetStopReturnValue ()
506 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
507 ValueObjectSP return_valobj_sp;
508 std::unique_lock<std::recursive_mutex> lock;
509 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
511 if (exe_ctx.HasThreadScope())
513 Process::StopLocker stop_locker;
514 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
516 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
519 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
525 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
526 static_cast<void*>(exe_ctx.GetThreadPtr()));
531 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
532 static_cast<void*>(exe_ctx.GetThreadPtr()),
533 return_valobj_sp.get()
534 ? return_valobj_sp->GetValueAsCString()
535 : "<no return value>");
537 return SBValue (return_valobj_sp);
541 SBThread::SetThread (const ThreadSP& lldb_object_sp)
543 m_opaque_sp->SetThreadSP (lldb_object_sp);
547 SBThread::GetThreadID () const
549 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
551 return thread_sp->GetID();
552 return LLDB_INVALID_THREAD_ID;
556 SBThread::GetIndexID () const
558 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
560 return thread_sp->GetIndexID();
561 return LLDB_INVALID_INDEX32;
565 SBThread::GetName () const
567 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
568 const char *name = NULL;
569 std::unique_lock<std::recursive_mutex> lock;
570 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
572 if (exe_ctx.HasThreadScope())
574 Process::StopLocker stop_locker;
575 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
577 name = exe_ctx.GetThreadPtr()->GetName();
582 log->Printf ("SBThread(%p)::GetName() => error: process is running",
583 static_cast<void*>(exe_ctx.GetThreadPtr()));
588 log->Printf ("SBThread(%p)::GetName () => %s",
589 static_cast<void*>(exe_ctx.GetThreadPtr()),
590 name ? name : "NULL");
596 SBThread::GetQueueName () const
598 const char *name = NULL;
599 std::unique_lock<std::recursive_mutex> lock;
600 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
602 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
603 if (exe_ctx.HasThreadScope())
605 Process::StopLocker stop_locker;
606 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
608 name = exe_ctx.GetThreadPtr()->GetQueueName();
613 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
614 static_cast<void*>(exe_ctx.GetThreadPtr()));
619 log->Printf ("SBThread(%p)::GetQueueName () => %s",
620 static_cast<void*>(exe_ctx.GetThreadPtr()),
621 name ? name : "NULL");
627 SBThread::GetQueueID () const
629 queue_id_t id = LLDB_INVALID_QUEUE_ID;
630 std::unique_lock<std::recursive_mutex> lock;
631 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
633 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
634 if (exe_ctx.HasThreadScope())
636 Process::StopLocker stop_locker;
637 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
639 id = exe_ctx.GetThreadPtr()->GetQueueID();
644 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
645 static_cast<void*>(exe_ctx.GetThreadPtr()));
650 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
651 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
657 SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
659 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
660 bool success = false;
661 std::unique_lock<std::recursive_mutex> lock;
662 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
664 if (exe_ctx.HasThreadScope())
666 Process::StopLocker stop_locker;
667 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
669 Thread *thread = exe_ctx.GetThreadPtr();
670 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
673 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
676 if (node->GetType() == StructuredData::Type::eTypeString)
678 strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
681 if (node->GetType() == StructuredData::Type::eTypeInteger)
683 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
686 if (node->GetType() == StructuredData::Type::eTypeFloat)
688 strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
691 if (node->GetType() == StructuredData::Type::eTypeBoolean)
693 if (node->GetAsBoolean()->GetValue() == true)
694 strm.Printf ("true");
696 strm.Printf ("false");
699 if (node->GetType() == StructuredData::Type::eTypeNull)
701 strm.Printf ("null");
710 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
711 static_cast<void*>(exe_ctx.GetThreadPtr()));
716 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
717 static_cast<void*>(exe_ctx.GetThreadPtr()),
725 SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
729 Process *process = exe_ctx.GetProcessPtr();
732 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
736 Thread *thread = exe_ctx.GetThreadPtr();
739 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
743 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
744 // then a "continue" will resume the plan.
745 if (new_plan != NULL)
747 new_plan->SetIsMasterPlan(true);
748 new_plan->SetOkayToDiscard(false);
751 // Why do we need to set the current thread by ID here???
752 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
754 if (process->GetTarget().GetDebugger().GetAsyncExecution ())
755 sb_error.ref() = process->Resume ();
757 sb_error.ref() = process->ResumeSynchronous (NULL);
763 SBThread::StepOver (lldb::RunMode stop_other_threads)
765 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
767 std::unique_lock<std::recursive_mutex> lock;
768 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
771 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
772 static_cast<void*>(exe_ctx.GetThreadPtr()),
773 Thread::RunModeAsCString (stop_other_threads));
775 if (exe_ctx.HasThreadScope())
777 Thread *thread = exe_ctx.GetThreadPtr();
778 bool abort_other_plans = false;
779 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
781 ThreadPlanSP new_plan_sp;
784 if (frame_sp->HasDebugInformation ())
786 const LazyBool avoid_no_debug = eLazyBoolCalculate;
787 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
788 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
796 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
802 // This returns an error, we should use it!
803 ResumeNewPlan (exe_ctx, new_plan_sp.get());
808 SBThread::StepInto (lldb::RunMode stop_other_threads)
810 StepInto (NULL, stop_other_threads);
814 SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
817 StepInto(target_name, LLDB_INVALID_LINE_NUMBER, error, stop_other_threads);
821 SBThread::StepInto (const char *target_name, uint32_t end_line, SBError &error, lldb::RunMode stop_other_threads)
823 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
825 std::unique_lock<std::recursive_mutex> lock;
826 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
829 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
830 static_cast<void*>(exe_ctx.GetThreadPtr()),
831 target_name? target_name: "<NULL>",
832 Thread::RunModeAsCString (stop_other_threads));
834 if (exe_ctx.HasThreadScope())
836 bool abort_other_plans = false;
838 Thread *thread = exe_ctx.GetThreadPtr();
839 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
840 ThreadPlanSP new_plan_sp;
842 if (frame_sp && frame_sp->HasDebugInformation ())
844 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
846 if (end_line == LLDB_INVALID_LINE_NUMBER)
847 range = sc.line_entry.range;
850 if (!sc.GetAddressRangeFromHereToEndLine(end_line, range, error.ref()))
854 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
855 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
856 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
861 step_in_avoids_code_without_debug_info,
862 step_out_avoids_code_without_debug_info);
866 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
871 error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
878 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
880 std::unique_lock<std::recursive_mutex> lock;
881 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
884 log->Printf ("SBThread(%p)::StepOut ()",
885 static_cast<void*>(exe_ctx.GetThreadPtr()));
887 if (exe_ctx.HasThreadScope())
889 bool abort_other_plans = false;
890 bool stop_other_threads = false;
892 Thread *thread = exe_ctx.GetThreadPtr();
894 const LazyBool avoid_no_debug = eLazyBoolCalculate;
895 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
904 // This returns an error, we should use it!
905 ResumeNewPlan (exe_ctx, new_plan_sp.get());
910 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
912 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
914 std::unique_lock<std::recursive_mutex> lock;
915 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
917 if (!sb_frame.IsValid())
920 log->Printf("SBThread(%p)::StepOutOfFrame passed an invalid frame, returning.",
921 static_cast<void*>(exe_ctx.GetThreadPtr()));
925 StackFrameSP frame_sp (sb_frame.GetFrameSP());
928 SBStream frame_desc_strm;
929 sb_frame.GetDescription (frame_desc_strm);
930 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
931 static_cast<void*>(exe_ctx.GetThreadPtr()),
932 static_cast<void*>(frame_sp.get()),
933 frame_desc_strm.GetData());
936 if (exe_ctx.HasThreadScope())
938 bool abort_other_plans = false;
939 bool stop_other_threads = false;
940 Thread *thread = exe_ctx.GetThreadPtr();
941 if (sb_frame.GetThread().GetThreadID() != thread->GetID())
943 log->Printf("SBThread(%p)::StepOutOfFrame passed a frame from another thread (0x%" PRIx64 " vrs. 0x%" PRIx64 ", returning.",
944 static_cast<void*>(exe_ctx.GetThreadPtr()),
945 sb_frame.GetThread().GetThreadID(),
949 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
955 frame_sp->GetFrameIndex()));
957 // This returns an error, we should use it!
958 ResumeNewPlan (exe_ctx, new_plan_sp.get());
963 SBThread::StepInstruction (bool step_over)
965 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
967 std::unique_lock<std::recursive_mutex> lock;
968 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
971 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
972 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
974 if (exe_ctx.HasThreadScope())
976 Thread *thread = exe_ctx.GetThreadPtr();
977 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
979 // This returns an error, we should use it!
980 ResumeNewPlan (exe_ctx, new_plan_sp.get());
985 SBThread::RunToAddress (lldb::addr_t addr)
987 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
989 std::unique_lock<std::recursive_mutex> lock;
990 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
993 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
994 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
996 if (exe_ctx.HasThreadScope())
998 bool abort_other_plans = false;
999 bool stop_other_threads = true;
1001 Address target_addr (addr);
1003 Thread *thread = exe_ctx.GetThreadPtr();
1005 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans,
1007 stop_other_threads));
1009 // This returns an error, we should use it!
1010 ResumeNewPlan (exe_ctx, new_plan_sp.get());
1015 SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
1016 lldb::SBFileSpec &sb_file_spec,
1020 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1021 char path[PATH_MAX];
1023 std::unique_lock<std::recursive_mutex> lock;
1024 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1026 StackFrameSP frame_sp (sb_frame.GetFrameSP());
1030 SBStream frame_desc_strm;
1031 sb_frame.GetDescription (frame_desc_strm);
1032 sb_file_spec->GetPath (path, sizeof(path));
1033 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
1034 static_cast<void*>(exe_ctx.GetThreadPtr()),
1035 static_cast<void*>(frame_sp.get()),
1036 frame_desc_strm.GetData(), path, line);
1039 if (exe_ctx.HasThreadScope())
1041 Target *target = exe_ctx.GetTargetPtr();
1042 Thread *thread = exe_ctx.GetThreadPtr();
1046 sb_error.SetErrorString("invalid line argument");
1052 frame_sp = thread->GetSelectedFrame ();
1054 frame_sp = thread->GetStackFrameAtIndex (0);
1057 SymbolContext frame_sc;
1060 sb_error.SetErrorString("no valid frames in thread to step");
1064 // If we have a frame, get its line
1065 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
1066 eSymbolContextFunction |
1067 eSymbolContextLineEntry |
1068 eSymbolContextSymbol );
1070 if (frame_sc.comp_unit == NULL)
1072 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
1076 FileSpec step_file_spec;
1077 if (sb_file_spec.IsValid())
1079 // The file spec passed in was valid, so use it
1080 step_file_spec = sb_file_spec.ref();
1084 if (frame_sc.line_entry.IsValid())
1085 step_file_spec = frame_sc.line_entry.file;
1088 sb_error.SetErrorString("invalid file argument or no file for frame");
1093 // Grab the current function, then we will make sure the "until" address is
1094 // within the function. We discard addresses that are out of the current
1095 // function, and then if there are no addresses remaining, give an appropriate
1098 bool all_in_function = true;
1099 AddressRange fun_range = frame_sc.function->GetAddressRange();
1101 std::vector<addr_t> step_over_until_addrs;
1102 const bool abort_other_plans = false;
1103 const bool stop_other_threads = false;
1104 const bool check_inlines = true;
1105 const bool exact = false;
1107 SymbolContextList sc_list;
1108 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1112 eSymbolContextLineEntry,
1114 if (num_matches > 0)
1117 for (uint32_t i=0; i<num_matches; ++i)
1119 if (sc_list.GetContextAtIndex(i, sc))
1121 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
1122 if (step_addr != LLDB_INVALID_ADDRESS)
1124 if (fun_range.ContainsLoadAddress(step_addr, target))
1125 step_over_until_addrs.push_back(step_addr);
1127 all_in_function = false;
1133 if (step_over_until_addrs.empty())
1135 if (all_in_function)
1137 step_file_spec.GetPath (path, sizeof(path));
1138 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
1141 sb_error.SetErrorString ("step until target not in current function");
1145 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
1146 &step_over_until_addrs[0],
1147 step_over_until_addrs.size(),
1149 frame_sp->GetFrameIndex()));
1151 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
1156 sb_error.SetErrorString("this SBThread object is invalid");
1162 SBThread::StepUsingScriptedThreadPlan (const char *script_class_name)
1164 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1167 std::unique_lock<std::recursive_mutex> lock;
1168 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1172 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: class name: %s",
1173 static_cast<void*>(exe_ctx.GetThreadPtr()),
1178 if (!exe_ctx.HasThreadScope())
1180 sb_error.SetErrorString("this SBThread object is invalid");
1184 Thread *thread = exe_ctx.GetThreadPtr();
1185 ThreadPlanSP thread_plan_sp = thread->QueueThreadPlanForStepScripted(false, script_class_name, false);
1188 sb_error = ResumeNewPlan(exe_ctx, thread_plan_sp.get());
1191 sb_error.SetErrorStringWithFormat("Error queuing thread plan for class: %s.", script_class_name);
1193 log->Printf ("SBThread(%p)::StepUsingScriptedThreadPlan: Error queuing thread plan for class: %s",
1194 static_cast<void*>(exe_ctx.GetThreadPtr()),
1202 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1204 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1207 std::unique_lock<std::recursive_mutex> lock;
1208 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1211 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1212 static_cast<void*>(exe_ctx.GetThreadPtr()),
1213 file_spec->GetPath().c_str(), line);
1215 if (!exe_ctx.HasThreadScope())
1217 sb_error.SetErrorString("this SBThread object is invalid");
1221 Thread *thread = exe_ctx.GetThreadPtr();
1223 Error err = thread->JumpToLine (file_spec.get(), line, true);
1224 sb_error.SetError (err);
1229 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
1233 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1235 std::unique_lock<std::recursive_mutex> lock;
1236 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1239 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1240 static_cast<void*>(exe_ctx.GetThreadPtr()),
1241 frame.GetFrameID());
1243 if (exe_ctx.HasThreadScope())
1245 Thread *thread = exe_ctx.GetThreadPtr();
1246 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
1253 SBThread::UnwindInnermostExpression()
1257 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1259 std::unique_lock<std::recursive_mutex> lock;
1260 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1263 log->Printf ("SBThread(%p)::UnwindExpressionEvaluation",
1264 static_cast<void*>(exe_ctx.GetThreadPtr()));
1266 if (exe_ctx.HasThreadScope())
1268 Thread *thread = exe_ctx.GetThreadPtr();
1269 sb_error.SetError (thread->UnwindInnermostExpression());
1270 if (sb_error.Success())
1271 thread->SetSelectedFrameByIndex(0, false);
1281 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1282 std::unique_lock<std::recursive_mutex> lock;
1283 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1285 bool result = false;
1286 if (exe_ctx.HasThreadScope())
1288 Process::StopLocker stop_locker;
1289 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1291 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1297 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1298 static_cast<void*>(exe_ctx.GetThreadPtr()));
1302 log->Printf ("SBThread(%p)::Suspend() => %i",
1303 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1310 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1311 std::unique_lock<std::recursive_mutex> lock;
1312 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1314 bool result = false;
1315 if (exe_ctx.HasThreadScope())
1317 Process::StopLocker stop_locker;
1318 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1320 const bool override_suspend = true;
1321 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
1327 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1328 static_cast<void*>(exe_ctx.GetThreadPtr()));
1332 log->Printf ("SBThread(%p)::Resume() => %i",
1333 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1338 SBThread::IsSuspended()
1340 std::unique_lock<std::recursive_mutex> lock;
1341 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1343 if (exe_ctx.HasThreadScope())
1344 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
1349 SBThread::IsStopped()
1351 std::unique_lock<std::recursive_mutex> lock;
1352 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1354 if (exe_ctx.HasThreadScope())
1355 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1360 SBThread::GetProcess ()
1362 SBProcess sb_process;
1363 std::unique_lock<std::recursive_mutex> lock;
1364 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1366 if (exe_ctx.HasThreadScope())
1368 // Have to go up to the target so we can get a shared pointer to our process...
1369 sb_process.SetSP (exe_ctx.GetProcessSP());
1372 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1375 SBStream frame_desc_strm;
1376 sb_process.GetDescription (frame_desc_strm);
1377 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1378 static_cast<void*>(exe_ctx.GetThreadPtr()),
1379 static_cast<void*>(sb_process.GetSP().get()),
1380 frame_desc_strm.GetData());
1387 SBThread::GetNumFrames ()
1389 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1391 uint32_t num_frames = 0;
1392 std::unique_lock<std::recursive_mutex> lock;
1393 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1395 if (exe_ctx.HasThreadScope())
1397 Process::StopLocker stop_locker;
1398 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1400 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1405 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1406 static_cast<void*>(exe_ctx.GetThreadPtr()));
1411 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1412 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
1418 SBThread::GetFrameAtIndex (uint32_t idx)
1420 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1423 StackFrameSP frame_sp;
1424 std::unique_lock<std::recursive_mutex> lock;
1425 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1427 if (exe_ctx.HasThreadScope())
1429 Process::StopLocker stop_locker;
1430 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1432 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1433 sb_frame.SetFrameSP (frame_sp);
1438 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1439 static_cast<void*>(exe_ctx.GetThreadPtr()));
1445 SBStream frame_desc_strm;
1446 sb_frame.GetDescription (frame_desc_strm);
1447 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1448 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1449 static_cast<void*>(frame_sp.get()),
1450 frame_desc_strm.GetData());
1457 SBThread::GetSelectedFrame ()
1459 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1462 StackFrameSP frame_sp;
1463 std::unique_lock<std::recursive_mutex> lock;
1464 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1466 if (exe_ctx.HasThreadScope())
1468 Process::StopLocker stop_locker;
1469 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1471 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1472 sb_frame.SetFrameSP (frame_sp);
1477 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1478 static_cast<void*>(exe_ctx.GetThreadPtr()));
1484 SBStream frame_desc_strm;
1485 sb_frame.GetDescription (frame_desc_strm);
1486 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1487 static_cast<void*>(exe_ctx.GetThreadPtr()),
1488 static_cast<void*>(frame_sp.get()),
1489 frame_desc_strm.GetData());
1496 SBThread::SetSelectedFrame (uint32_t idx)
1498 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1501 StackFrameSP frame_sp;
1502 std::unique_lock<std::recursive_mutex> lock;
1503 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1505 if (exe_ctx.HasThreadScope())
1507 Process::StopLocker stop_locker;
1508 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1510 Thread *thread = exe_ctx.GetThreadPtr();
1511 frame_sp = thread->GetStackFrameAtIndex (idx);
1514 thread->SetSelectedFrame (frame_sp.get());
1515 sb_frame.SetFrameSP (frame_sp);
1521 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1522 static_cast<void*>(exe_ctx.GetThreadPtr()));
1528 SBStream frame_desc_strm;
1529 sb_frame.GetDescription (frame_desc_strm);
1530 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1531 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1532 static_cast<void*>(frame_sp.get()),
1533 frame_desc_strm.GetData());
1539 SBThread::EventIsThreadEvent (const SBEvent &event)
1541 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1545 SBThread::GetStackFrameFromEvent (const SBEvent &event)
1547 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1552 SBThread::GetThreadFromEvent (const SBEvent &event)
1554 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1558 SBThread::operator == (const SBThread &rhs) const
1560 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
1564 SBThread::operator != (const SBThread &rhs) const
1566 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
1570 SBThread::GetStatus (SBStream &status) const
1572 Stream &strm = status.ref();
1574 std::unique_lock<std::recursive_mutex> lock;
1575 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1577 if (exe_ctx.HasThreadScope())
1579 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1582 strm.PutCString ("No status");
1588 SBThread::GetDescription (SBStream &description) const
1590 Stream &strm = description.ref();
1592 std::unique_lock<std::recursive_mutex> lock;
1593 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1595 if (exe_ctx.HasThreadScope())
1597 exe_ctx.GetThreadPtr()->DumpUsingSettingsFormat(strm, LLDB_INVALID_THREAD_ID);
1598 //strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1601 strm.PutCString ("No value");
1607 SBThread::GetExtendedBacktraceThread (const char *type)
1609 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1610 std::unique_lock<std::recursive_mutex> lock;
1611 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1612 SBThread sb_origin_thread;
1614 if (exe_ctx.HasThreadScope())
1616 Process::StopLocker stop_locker;
1617 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1619 ThreadSP real_thread(exe_ctx.GetThreadSP());
1622 ConstString type_const (type);
1623 Process *process = exe_ctx.GetProcessPtr();
1626 SystemRuntime *runtime = process->GetSystemRuntime();
1629 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
1632 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1634 process->GetExtendedThreadList().AddThread (new_thread_sp);
1635 sb_origin_thread.SetThread (new_thread_sp);
1638 const char *queue_name = new_thread_sp->GetQueueName();
1639 if (queue_name == NULL)
1641 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread "
1642 "created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1643 static_cast<void*>(exe_ctx.GetThreadPtr()),
1644 static_cast<void*>(new_thread_sp.get()),
1645 new_thread_sp->GetQueueID(),
1656 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1657 static_cast<void*>(exe_ctx.GetThreadPtr()));
1661 if (log && sb_origin_thread.IsValid() == false)
1662 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1663 static_cast<void*>(exe_ctx.GetThreadPtr()));
1664 return sb_origin_thread;
1668 SBThread::GetExtendedBacktraceOriginatingIndexID ()
1670 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1672 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1673 return LLDB_INVALID_INDEX32;
1677 SBThread::SafeToCallFunctions ()
1679 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1681 return thread_sp->SafeToCallFunctions();
1685 lldb_private::Thread *
1686 SBThread::operator->()
1688 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1690 return thread_sp.get();
1695 lldb_private::Thread *
1698 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1700 return thread_sp.get();