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/SBValue.h"
47 using namespace lldb_private;
50 SBThread::GetBroadcasterClassName ()
52 return Thread::GetStaticBroadcasterClass().AsCString();
55 //----------------------------------------------------------------------
57 //----------------------------------------------------------------------
58 SBThread::SBThread () :
59 m_opaque_sp (new ExecutionContextRef())
63 SBThread::SBThread (const ThreadSP& lldb_object_sp) :
64 m_opaque_sp (new ExecutionContextRef(lldb_object_sp))
68 SBThread::SBThread (const SBThread &rhs) :
69 m_opaque_sp (new ExecutionContextRef(*rhs.m_opaque_sp))
74 //----------------------------------------------------------------------
75 // Assignment operator
76 //----------------------------------------------------------------------
78 const lldb::SBThread &
79 SBThread::operator = (const SBThread &rhs)
82 *m_opaque_sp = *rhs.m_opaque_sp;
86 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
94 SBThread::GetQueue () const
98 Mutex::Locker api_locker;
99 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
102 if (exe_ctx.HasThreadScope())
104 Process::StopLocker stop_locker;
105 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
107 queue_sp = exe_ctx.GetThreadPtr()->GetQueue();
110 sb_queue.SetQueue (queue_sp);
116 log->Printf ("SBThread(%p)::GetQueueKind() => error: process is running",
117 static_cast<void*>(exe_ctx.GetThreadPtr()));
122 log->Printf ("SBThread(%p)::GetQueueKind () => SBQueue(%p)",
123 static_cast<void*>(exe_ctx.GetThreadPtr()), static_cast<void*>(queue_sp.get()));
130 SBThread::IsValid() const
132 return m_opaque_sp->GetThreadSP().get() != NULL;
138 m_opaque_sp->Clear();
143 SBThread::GetStopReason()
145 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
147 StopReason reason = eStopReasonInvalid;
148 Mutex::Locker api_locker;
149 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
151 if (exe_ctx.HasThreadScope())
153 Process::StopLocker stop_locker;
154 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
156 return exe_ctx.GetThreadPtr()->GetStopReason();
161 log->Printf ("SBThread(%p)::GetStopReason() => error: process is running",
162 static_cast<void*>(exe_ctx.GetThreadPtr()));
167 log->Printf ("SBThread(%p)::GetStopReason () => %s",
168 static_cast<void*>(exe_ctx.GetThreadPtr()),
169 Thread::StopReasonAsCString (reason));
175 SBThread::GetStopReasonDataCount ()
177 Mutex::Locker api_locker;
178 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
180 if (exe_ctx.HasThreadScope())
182 Process::StopLocker stop_locker;
183 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
185 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
188 StopReason reason = stop_info_sp->GetStopReason();
191 case eStopReasonInvalid:
192 case eStopReasonNone:
193 case eStopReasonTrace:
194 case eStopReasonExec:
195 case eStopReasonPlanComplete:
196 case eStopReasonThreadExiting:
197 // There is no data for these stop reasons.
200 case eStopReasonBreakpoint:
202 break_id_t site_id = stop_info_sp->GetValue();
203 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
205 return bp_site_sp->GetNumberOfOwners () * 2;
207 return 0; // Breakpoint must have cleared itself...
211 case eStopReasonWatchpoint:
214 case eStopReasonSignal:
217 case eStopReasonException:
224 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
226 log->Printf ("SBThread(%p)::GetStopReasonDataCount() => error: process is running",
227 static_cast<void*>(exe_ctx.GetThreadPtr()));
234 SBThread::GetStopReasonDataAtIndex (uint32_t idx)
236 Mutex::Locker api_locker;
237 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
239 if (exe_ctx.HasThreadScope())
241 Process::StopLocker stop_locker;
242 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
244 Thread *thread = exe_ctx.GetThreadPtr();
245 StopInfoSP stop_info_sp = thread->GetStopInfo ();
248 StopReason reason = stop_info_sp->GetStopReason();
251 case eStopReasonInvalid:
252 case eStopReasonNone:
253 case eStopReasonTrace:
254 case eStopReasonExec:
255 case eStopReasonPlanComplete:
256 case eStopReasonThreadExiting:
257 // There is no data for these stop reasons.
260 case eStopReasonBreakpoint:
262 break_id_t site_id = stop_info_sp->GetValue();
263 lldb::BreakpointSiteSP bp_site_sp (exe_ctx.GetProcessPtr()->GetBreakpointSiteList().FindByID (site_id));
266 uint32_t bp_index = idx / 2;
267 BreakpointLocationSP bp_loc_sp (bp_site_sp->GetOwnerAtIndex (bp_index));
272 // Odd idx, return the breakpoint location ID
273 return bp_loc_sp->GetID();
277 // Even idx, return the breakpoint ID
278 return bp_loc_sp->GetBreakpoint().GetID();
282 return LLDB_INVALID_BREAK_ID;
286 case eStopReasonWatchpoint:
287 return stop_info_sp->GetValue();
289 case eStopReasonSignal:
290 return stop_info_sp->GetValue();
292 case eStopReasonException:
293 return stop_info_sp->GetValue();
299 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
301 log->Printf ("SBThread(%p)::GetStopReasonDataAtIndex() => error: process is running",
302 static_cast<void*>(exe_ctx.GetThreadPtr()));
309 SBThread::GetStopDescription (char *dst, size_t dst_len)
311 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
313 Mutex::Locker api_locker;
314 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
316 if (exe_ctx.HasThreadScope())
318 Process::StopLocker stop_locker;
319 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
322 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
325 const char *stop_desc = stop_info_sp->GetDescription();
329 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => \"%s\"",
330 static_cast<void*>(exe_ctx.GetThreadPtr()),
333 return ::snprintf (dst, dst_len, "%s", stop_desc);
336 // NULL dst passed in, return the length needed to contain the description
337 return ::strlen (stop_desc) + 1; // Include the NULL byte for size
342 size_t stop_desc_len = 0;
343 switch (stop_info_sp->GetStopReason())
345 case eStopReasonTrace:
346 case eStopReasonPlanComplete:
348 static char trace_desc[] = "step";
349 stop_desc = trace_desc;
350 stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size
354 case eStopReasonBreakpoint:
356 static char bp_desc[] = "breakpoint hit";
358 stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size
362 case eStopReasonWatchpoint:
364 static char wp_desc[] = "watchpoint hit";
366 stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size
370 case eStopReasonSignal:
372 stop_desc = exe_ctx.GetProcessPtr()->GetUnixSignals ().GetSignalAsCString (stop_info_sp->GetValue());
373 if (stop_desc == NULL || stop_desc[0] == '\0')
375 static char signal_desc[] = "signal";
376 stop_desc = signal_desc;
377 stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size
382 case eStopReasonException:
384 char exc_desc[] = "exception";
385 stop_desc = exc_desc;
386 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
390 case eStopReasonExec:
392 char exc_desc[] = "exec";
393 stop_desc = exc_desc;
394 stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size
398 case eStopReasonThreadExiting:
400 char limbo_desc[] = "thread exiting";
401 stop_desc = limbo_desc;
402 stop_desc_len = sizeof(limbo_desc);
409 if (stop_desc && stop_desc[0])
412 log->Printf ("SBThread(%p)::GetStopDescription (dst, dst_len) => '%s'",
413 static_cast<void*>(exe_ctx.GetThreadPtr()),
417 return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte
419 if (stop_desc_len == 0)
420 stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte
422 return stop_desc_len;
429 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
431 log->Printf ("SBThread(%p)::GetStopDescription() => error: process is running",
432 static_cast<void*>(exe_ctx.GetThreadPtr()));
441 SBThread::GetStopReturnValue ()
443 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
444 ValueObjectSP return_valobj_sp;
445 Mutex::Locker api_locker;
446 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
448 if (exe_ctx.HasThreadScope())
450 Process::StopLocker stop_locker;
451 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
453 StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo ();
456 return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
462 log->Printf ("SBThread(%p)::GetStopReturnValue() => error: process is running",
463 static_cast<void*>(exe_ctx.GetThreadPtr()));
468 log->Printf ("SBThread(%p)::GetStopReturnValue () => %s",
469 static_cast<void*>(exe_ctx.GetThreadPtr()),
470 return_valobj_sp.get()
471 ? return_valobj_sp->GetValueAsCString()
472 : "<no return value>");
474 return SBValue (return_valobj_sp);
478 SBThread::SetThread (const ThreadSP& lldb_object_sp)
480 m_opaque_sp->SetThreadSP (lldb_object_sp);
484 SBThread::GetThreadID () const
486 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
488 return thread_sp->GetID();
489 return LLDB_INVALID_THREAD_ID;
493 SBThread::GetIndexID () const
495 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
497 return thread_sp->GetIndexID();
498 return LLDB_INVALID_INDEX32;
502 SBThread::GetName () const
504 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
505 const char *name = NULL;
506 Mutex::Locker api_locker;
507 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
509 if (exe_ctx.HasThreadScope())
511 Process::StopLocker stop_locker;
512 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
514 name = exe_ctx.GetThreadPtr()->GetName();
519 log->Printf ("SBThread(%p)::GetName() => error: process is running",
520 static_cast<void*>(exe_ctx.GetThreadPtr()));
525 log->Printf ("SBThread(%p)::GetName () => %s",
526 static_cast<void*>(exe_ctx.GetThreadPtr()),
527 name ? name : "NULL");
533 SBThread::GetQueueName () const
535 const char *name = NULL;
536 Mutex::Locker api_locker;
537 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
539 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
540 if (exe_ctx.HasThreadScope())
542 Process::StopLocker stop_locker;
543 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
545 name = exe_ctx.GetThreadPtr()->GetQueueName();
550 log->Printf ("SBThread(%p)::GetQueueName() => error: process is running",
551 static_cast<void*>(exe_ctx.GetThreadPtr()));
556 log->Printf ("SBThread(%p)::GetQueueName () => %s",
557 static_cast<void*>(exe_ctx.GetThreadPtr()),
558 name ? name : "NULL");
564 SBThread::GetQueueID () const
566 queue_id_t id = LLDB_INVALID_QUEUE_ID;
567 Mutex::Locker api_locker;
568 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
570 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
571 if (exe_ctx.HasThreadScope())
573 Process::StopLocker stop_locker;
574 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
576 id = exe_ctx.GetThreadPtr()->GetQueueID();
581 log->Printf ("SBThread(%p)::GetQueueID() => error: process is running",
582 static_cast<void*>(exe_ctx.GetThreadPtr()));
587 log->Printf ("SBThread(%p)::GetQueueID () => 0x%" PRIx64,
588 static_cast<void*>(exe_ctx.GetThreadPtr()), id);
594 SBThread::GetInfoItemByPathAsString (const char *path, SBStream &strm)
596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
597 bool success = false;
598 Mutex::Locker api_locker;
599 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
601 if (exe_ctx.HasThreadScope())
603 Process::StopLocker stop_locker;
604 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
606 Thread *thread = exe_ctx.GetThreadPtr();
607 StructuredData::ObjectSP info_root_sp = thread->GetExtendedInfo();
610 StructuredData::ObjectSP node = info_root_sp->GetObjectForDotSeparatedPath (path);
613 if (node->GetType() == StructuredData::Type::eTypeString)
615 strm.Printf ("%s", node->GetAsString()->GetValue().c_str());
618 if (node->GetType() == StructuredData::Type::eTypeInteger)
620 strm.Printf ("0x%" PRIx64, node->GetAsInteger()->GetValue());
623 if (node->GetType() == StructuredData::Type::eTypeFloat)
625 strm.Printf ("0x%f", node->GetAsFloat()->GetValue());
628 if (node->GetType() == StructuredData::Type::eTypeBoolean)
630 if (node->GetAsBoolean()->GetValue() == true)
631 strm.Printf ("true");
633 strm.Printf ("false");
636 if (node->GetType() == StructuredData::Type::eTypeNull)
638 strm.Printf ("null");
647 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString() => error: process is running",
648 static_cast<void*>(exe_ctx.GetThreadPtr()));
653 log->Printf ("SBThread(%p)::GetInfoItemByPathAsString () => %s",
654 static_cast<void*>(exe_ctx.GetThreadPtr()),
662 SBThread::ResumeNewPlan (ExecutionContext &exe_ctx, ThreadPlan *new_plan)
666 Process *process = exe_ctx.GetProcessPtr();
669 sb_error.SetErrorString("No process in SBThread::ResumeNewPlan");
673 Thread *thread = exe_ctx.GetThreadPtr();
676 sb_error.SetErrorString("No thread in SBThread::ResumeNewPlan");
680 // User level plans should be Master Plans so they can be interrupted, other plans executed, and
681 // then a "continue" will resume the plan.
682 if (new_plan != NULL)
684 new_plan->SetIsMasterPlan(true);
685 new_plan->SetOkayToDiscard(false);
688 // Why do we need to set the current thread by ID here???
689 process->GetThreadList().SetSelectedThreadByID (thread->GetID());
690 sb_error.ref() = process->Resume();
692 if (sb_error.Success())
694 // If we are doing synchronous mode, then wait for the
695 // process to stop yet again!
696 if (process->GetTarget().GetDebugger().GetAsyncExecution () == false)
697 process->WaitForProcessToStop (NULL);
704 SBThread::StepOver (lldb::RunMode stop_other_threads)
706 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
708 Mutex::Locker api_locker;
709 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
713 log->Printf ("SBThread(%p)::StepOver (stop_other_threads='%s')",
714 static_cast<void*>(exe_ctx.GetThreadPtr()),
715 Thread::RunModeAsCString (stop_other_threads));
717 if (exe_ctx.HasThreadScope())
719 Thread *thread = exe_ctx.GetThreadPtr();
720 bool abort_other_plans = false;
721 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
723 ThreadPlanSP new_plan_sp;
726 if (frame_sp->HasDebugInformation ())
728 const LazyBool avoid_no_debug = eLazyBoolCalculate;
729 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
730 new_plan_sp = thread->QueueThreadPlanForStepOverRange (abort_other_plans,
738 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (true,
744 // This returns an error, we should use it!
745 ResumeNewPlan (exe_ctx, new_plan_sp.get());
750 SBThread::StepInto (lldb::RunMode stop_other_threads)
752 StepInto (NULL, stop_other_threads);
756 SBThread::StepInto (const char *target_name, lldb::RunMode stop_other_threads)
758 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
760 Mutex::Locker api_locker;
761 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
764 log->Printf ("SBThread(%p)::StepInto (target_name='%s', stop_other_threads='%s')",
765 static_cast<void*>(exe_ctx.GetThreadPtr()),
766 target_name? target_name: "<NULL>",
767 Thread::RunModeAsCString (stop_other_threads));
769 if (exe_ctx.HasThreadScope())
771 bool abort_other_plans = false;
773 Thread *thread = exe_ctx.GetThreadPtr();
774 StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0));
775 ThreadPlanSP new_plan_sp;
777 if (frame_sp && frame_sp->HasDebugInformation ())
779 const LazyBool step_out_avoids_code_without_debug_info = eLazyBoolCalculate;
780 const LazyBool step_in_avoids_code_without_debug_info = eLazyBoolCalculate;
781 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
782 new_plan_sp = thread->QueueThreadPlanForStepInRange (abort_other_plans,
787 step_in_avoids_code_without_debug_info,
788 step_out_avoids_code_without_debug_info);
792 new_plan_sp = thread->QueueThreadPlanForStepSingleInstruction (false,
797 // This returns an error, we should use it!
798 ResumeNewPlan (exe_ctx, new_plan_sp.get());
805 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
807 Mutex::Locker api_locker;
808 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
812 log->Printf ("SBThread(%p)::StepOut ()",
813 static_cast<void*>(exe_ctx.GetThreadPtr()));
815 if (exe_ctx.HasThreadScope())
817 bool abort_other_plans = false;
818 bool stop_other_threads = false;
820 Thread *thread = exe_ctx.GetThreadPtr();
822 const LazyBool avoid_no_debug = eLazyBoolCalculate;
823 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
832 // This returns an error, we should use it!
833 ResumeNewPlan (exe_ctx, new_plan_sp.get());
838 SBThread::StepOutOfFrame (lldb::SBFrame &sb_frame)
840 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
842 Mutex::Locker api_locker;
843 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
845 StackFrameSP frame_sp (sb_frame.GetFrameSP());
848 SBStream frame_desc_strm;
849 sb_frame.GetDescription (frame_desc_strm);
850 log->Printf ("SBThread(%p)::StepOutOfFrame (frame = SBFrame(%p): %s)",
851 static_cast<void*>(exe_ctx.GetThreadPtr()),
852 static_cast<void*>(frame_sp.get()),
853 frame_desc_strm.GetData());
856 if (exe_ctx.HasThreadScope())
858 bool abort_other_plans = false;
859 bool stop_other_threads = false;
860 Thread *thread = exe_ctx.GetThreadPtr();
862 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepOut (abort_other_plans,
868 frame_sp->GetFrameIndex()));
870 // This returns an error, we should use it!
871 ResumeNewPlan (exe_ctx, new_plan_sp.get());
876 SBThread::StepInstruction (bool step_over)
878 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
880 Mutex::Locker api_locker;
881 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
886 log->Printf ("SBThread(%p)::StepInstruction (step_over=%i)",
887 static_cast<void*>(exe_ctx.GetThreadPtr()), step_over);
889 if (exe_ctx.HasThreadScope())
891 Thread *thread = exe_ctx.GetThreadPtr();
892 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepSingleInstruction (step_over, true, true));
894 // This returns an error, we should use it!
895 ResumeNewPlan (exe_ctx, new_plan_sp.get());
900 SBThread::RunToAddress (lldb::addr_t addr)
902 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
904 Mutex::Locker api_locker;
905 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
909 log->Printf ("SBThread(%p)::RunToAddress (addr=0x%" PRIx64 ")",
910 static_cast<void*>(exe_ctx.GetThreadPtr()), addr);
912 if (exe_ctx.HasThreadScope())
914 bool abort_other_plans = false;
915 bool stop_other_threads = true;
917 Address target_addr (addr);
919 Thread *thread = exe_ctx.GetThreadPtr();
921 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads));
923 // This returns an error, we should use it!
924 ResumeNewPlan (exe_ctx, new_plan_sp.get());
929 SBThread::StepOverUntil (lldb::SBFrame &sb_frame,
930 lldb::SBFileSpec &sb_file_spec,
934 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
937 Mutex::Locker api_locker;
938 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
940 StackFrameSP frame_sp (sb_frame.GetFrameSP());
944 SBStream frame_desc_strm;
945 sb_frame.GetDescription (frame_desc_strm);
946 sb_file_spec->GetPath (path, sizeof(path));
947 log->Printf ("SBThread(%p)::StepOverUntil (frame = SBFrame(%p): %s, file+line = %s:%u)",
948 static_cast<void*>(exe_ctx.GetThreadPtr()),
949 static_cast<void*>(frame_sp.get()),
950 frame_desc_strm.GetData(), path, line);
953 if (exe_ctx.HasThreadScope())
955 Target *target = exe_ctx.GetTargetPtr();
956 Thread *thread = exe_ctx.GetThreadPtr();
960 sb_error.SetErrorString("invalid line argument");
966 frame_sp = thread->GetSelectedFrame ();
968 frame_sp = thread->GetStackFrameAtIndex (0);
971 SymbolContext frame_sc;
974 sb_error.SetErrorString("no valid frames in thread to step");
978 // If we have a frame, get its line
979 frame_sc = frame_sp->GetSymbolContext (eSymbolContextCompUnit |
980 eSymbolContextFunction |
981 eSymbolContextLineEntry |
982 eSymbolContextSymbol );
984 if (frame_sc.comp_unit == NULL)
986 sb_error.SetErrorStringWithFormat("frame %u doesn't have debug information", frame_sp->GetFrameIndex());
990 FileSpec step_file_spec;
991 if (sb_file_spec.IsValid())
993 // The file spec passed in was valid, so use it
994 step_file_spec = sb_file_spec.ref();
998 if (frame_sc.line_entry.IsValid())
999 step_file_spec = frame_sc.line_entry.file;
1002 sb_error.SetErrorString("invalid file argument or no file for frame");
1007 // Grab the current function, then we will make sure the "until" address is
1008 // within the function. We discard addresses that are out of the current
1009 // function, and then if there are no addresses remaining, give an appropriate
1012 bool all_in_function = true;
1013 AddressRange fun_range = frame_sc.function->GetAddressRange();
1015 std::vector<addr_t> step_over_until_addrs;
1016 const bool abort_other_plans = false;
1017 const bool stop_other_threads = false;
1018 const bool check_inlines = true;
1019 const bool exact = false;
1021 SymbolContextList sc_list;
1022 const uint32_t num_matches = frame_sc.comp_unit->ResolveSymbolContext (step_file_spec,
1026 eSymbolContextLineEntry,
1028 if (num_matches > 0)
1031 for (uint32_t i=0; i<num_matches; ++i)
1033 if (sc_list.GetContextAtIndex(i, sc))
1035 addr_t step_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(target);
1036 if (step_addr != LLDB_INVALID_ADDRESS)
1038 if (fun_range.ContainsLoadAddress(step_addr, target))
1039 step_over_until_addrs.push_back(step_addr);
1041 all_in_function = false;
1047 if (step_over_until_addrs.empty())
1049 if (all_in_function)
1051 step_file_spec.GetPath (path, sizeof(path));
1052 sb_error.SetErrorStringWithFormat("No line entries for %s:%u", path, line);
1055 sb_error.SetErrorString ("step until target not in current function");
1059 ThreadPlanSP new_plan_sp(thread->QueueThreadPlanForStepUntil (abort_other_plans,
1060 &step_over_until_addrs[0],
1061 step_over_until_addrs.size(),
1063 frame_sp->GetFrameIndex()));
1065 sb_error = ResumeNewPlan (exe_ctx, new_plan_sp.get());
1070 sb_error.SetErrorString("this SBThread object is invalid");
1076 SBThread::JumpToLine (lldb::SBFileSpec &file_spec, uint32_t line)
1078 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1081 Mutex::Locker api_locker;
1082 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1085 log->Printf ("SBThread(%p)::JumpToLine (file+line = %s:%u)",
1086 static_cast<void*>(exe_ctx.GetThreadPtr()),
1087 file_spec->GetPath().c_str(), line);
1089 if (!exe_ctx.HasThreadScope())
1091 sb_error.SetErrorString("this SBThread object is invalid");
1095 Thread *thread = exe_ctx.GetThreadPtr();
1097 Error err = thread->JumpToLine (file_spec.get(), line, true);
1098 sb_error.SetError (err);
1103 SBThread::ReturnFromFrame (SBFrame &frame, SBValue &return_value)
1107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1109 Mutex::Locker api_locker;
1110 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1114 log->Printf ("SBThread(%p)::ReturnFromFrame (frame=%d)",
1115 static_cast<void*>(exe_ctx.GetThreadPtr()),
1116 frame.GetFrameID());
1118 if (exe_ctx.HasThreadScope())
1120 Thread *thread = exe_ctx.GetThreadPtr();
1121 sb_error.SetError (thread->ReturnFromFrame(frame.GetFrameSP(), return_value.GetSP()));
1131 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1132 ExecutionContext exe_ctx (m_opaque_sp.get());
1133 bool result = false;
1134 if (exe_ctx.HasThreadScope())
1136 Process::StopLocker stop_locker;
1137 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1139 exe_ctx.GetThreadPtr()->SetResumeState (eStateSuspended);
1145 log->Printf ("SBThread(%p)::Suspend() => error: process is running",
1146 static_cast<void*>(exe_ctx.GetThreadPtr()));
1150 log->Printf ("SBThread(%p)::Suspend() => %i",
1151 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1159 ExecutionContext exe_ctx (m_opaque_sp.get());
1160 bool result = false;
1161 if (exe_ctx.HasThreadScope())
1163 Process::StopLocker stop_locker;
1164 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1166 const bool override_suspend = true;
1167 exe_ctx.GetThreadPtr()->SetResumeState (eStateRunning, override_suspend);
1173 log->Printf ("SBThread(%p)::Resume() => error: process is running",
1174 static_cast<void*>(exe_ctx.GetThreadPtr()));
1178 log->Printf ("SBThread(%p)::Resume() => %i",
1179 static_cast<void*>(exe_ctx.GetThreadPtr()), result);
1184 SBThread::IsSuspended()
1186 ExecutionContext exe_ctx (m_opaque_sp.get());
1187 if (exe_ctx.HasThreadScope())
1188 return exe_ctx.GetThreadPtr()->GetResumeState () == eStateSuspended;
1193 SBThread::IsStopped()
1195 ExecutionContext exe_ctx (m_opaque_sp.get());
1196 if (exe_ctx.HasThreadScope())
1197 return StateIsStoppedState(exe_ctx.GetThreadPtr()->GetState(), true);
1202 SBThread::GetProcess ()
1204 SBProcess sb_process;
1205 ExecutionContext exe_ctx (m_opaque_sp.get());
1206 if (exe_ctx.HasThreadScope())
1208 // Have to go up to the target so we can get a shared pointer to our process...
1209 sb_process.SetSP (exe_ctx.GetProcessSP());
1212 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1215 SBStream frame_desc_strm;
1216 sb_process.GetDescription (frame_desc_strm);
1217 log->Printf ("SBThread(%p)::GetProcess () => SBProcess(%p): %s",
1218 static_cast<void*>(exe_ctx.GetThreadPtr()),
1219 static_cast<void*>(sb_process.GetSP().get()),
1220 frame_desc_strm.GetData());
1227 SBThread::GetNumFrames ()
1229 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1231 uint32_t num_frames = 0;
1232 Mutex::Locker api_locker;
1233 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1235 if (exe_ctx.HasThreadScope())
1237 Process::StopLocker stop_locker;
1238 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1240 num_frames = exe_ctx.GetThreadPtr()->GetStackFrameCount();
1245 log->Printf ("SBThread(%p)::GetNumFrames() => error: process is running",
1246 static_cast<void*>(exe_ctx.GetThreadPtr()));
1251 log->Printf ("SBThread(%p)::GetNumFrames () => %u",
1252 static_cast<void*>(exe_ctx.GetThreadPtr()), num_frames);
1258 SBThread::GetFrameAtIndex (uint32_t idx)
1260 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1263 StackFrameSP frame_sp;
1264 Mutex::Locker api_locker;
1265 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1267 if (exe_ctx.HasThreadScope())
1269 Process::StopLocker stop_locker;
1270 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1272 frame_sp = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex (idx);
1273 sb_frame.SetFrameSP (frame_sp);
1278 log->Printf ("SBThread(%p)::GetFrameAtIndex() => error: process is running",
1279 static_cast<void*>(exe_ctx.GetThreadPtr()));
1285 SBStream frame_desc_strm;
1286 sb_frame.GetDescription (frame_desc_strm);
1287 log->Printf ("SBThread(%p)::GetFrameAtIndex (idx=%d) => SBFrame(%p): %s",
1288 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1289 static_cast<void*>(frame_sp.get()),
1290 frame_desc_strm.GetData());
1297 SBThread::GetSelectedFrame ()
1299 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1302 StackFrameSP frame_sp;
1303 Mutex::Locker api_locker;
1304 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1306 if (exe_ctx.HasThreadScope())
1308 Process::StopLocker stop_locker;
1309 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1311 frame_sp = exe_ctx.GetThreadPtr()->GetSelectedFrame ();
1312 sb_frame.SetFrameSP (frame_sp);
1317 log->Printf ("SBThread(%p)::GetSelectedFrame() => error: process is running",
1318 static_cast<void*>(exe_ctx.GetThreadPtr()));
1324 SBStream frame_desc_strm;
1325 sb_frame.GetDescription (frame_desc_strm);
1326 log->Printf ("SBThread(%p)::GetSelectedFrame () => SBFrame(%p): %s",
1327 static_cast<void*>(exe_ctx.GetThreadPtr()),
1328 static_cast<void*>(frame_sp.get()),
1329 frame_desc_strm.GetData());
1336 SBThread::SetSelectedFrame (uint32_t idx)
1338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1341 StackFrameSP frame_sp;
1342 Mutex::Locker api_locker;
1343 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1345 if (exe_ctx.HasThreadScope())
1347 Process::StopLocker stop_locker;
1348 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1350 Thread *thread = exe_ctx.GetThreadPtr();
1351 frame_sp = thread->GetStackFrameAtIndex (idx);
1354 thread->SetSelectedFrame (frame_sp.get());
1355 sb_frame.SetFrameSP (frame_sp);
1361 log->Printf ("SBThread(%p)::SetSelectedFrame() => error: process is running",
1362 static_cast<void*>(exe_ctx.GetThreadPtr()));
1368 SBStream frame_desc_strm;
1369 sb_frame.GetDescription (frame_desc_strm);
1370 log->Printf ("SBThread(%p)::SetSelectedFrame (idx=%u) => SBFrame(%p): %s",
1371 static_cast<void*>(exe_ctx.GetThreadPtr()), idx,
1372 static_cast<void*>(frame_sp.get()),
1373 frame_desc_strm.GetData());
1379 SBThread::EventIsThreadEvent (const SBEvent &event)
1381 return Thread::ThreadEventData::GetEventDataFromEvent(event.get()) != NULL;
1385 SBThread::GetStackFrameFromEvent (const SBEvent &event)
1387 return Thread::ThreadEventData::GetStackFrameFromEvent (event.get());
1392 SBThread::GetThreadFromEvent (const SBEvent &event)
1394 return Thread::ThreadEventData::GetThreadFromEvent (event.get());
1398 SBThread::operator == (const SBThread &rhs) const
1400 return m_opaque_sp->GetThreadSP().get() == rhs.m_opaque_sp->GetThreadSP().get();
1404 SBThread::operator != (const SBThread &rhs) const
1406 return m_opaque_sp->GetThreadSP().get() != rhs.m_opaque_sp->GetThreadSP().get();
1410 SBThread::GetStatus (SBStream &status) const
1412 Stream &strm = status.ref();
1414 ExecutionContext exe_ctx (m_opaque_sp.get());
1415 if (exe_ctx.HasThreadScope())
1417 exe_ctx.GetThreadPtr()->GetStatus(strm, 0, 1, 1);
1420 strm.PutCString ("No status");
1426 SBThread::GetDescription (SBStream &description) const
1428 Stream &strm = description.ref();
1430 ExecutionContext exe_ctx (m_opaque_sp.get());
1431 if (exe_ctx.HasThreadScope())
1433 strm.Printf("SBThread: tid = 0x%4.4" PRIx64, exe_ctx.GetThreadPtr()->GetID());
1436 strm.PutCString ("No value");
1442 SBThread::GetExtendedBacktraceThread (const char *type)
1444 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1445 Mutex::Locker api_locker;
1446 ExecutionContext exe_ctx (m_opaque_sp.get(), api_locker);
1447 SBThread sb_origin_thread;
1449 if (exe_ctx.HasThreadScope())
1451 Process::StopLocker stop_locker;
1452 if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1454 ThreadSP real_thread(exe_ctx.GetThreadSP());
1457 ConstString type_const (type);
1458 Process *process = exe_ctx.GetProcessPtr();
1461 SystemRuntime *runtime = process->GetSystemRuntime();
1464 ThreadSP new_thread_sp (runtime->GetExtendedBacktraceThread (real_thread, type_const));
1467 // Save this in the Process' ExtendedThreadList so a strong pointer retains the
1469 process->GetExtendedThreadList().AddThread (new_thread_sp);
1470 sb_origin_thread.SetThread (new_thread_sp);
1473 const char *queue_name = new_thread_sp->GetQueueName();
1474 if (queue_name == NULL)
1476 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => new extended Thread created (%p) with queue_id 0x%" PRIx64 " queue name '%s'",
1477 static_cast<void*>(exe_ctx.GetThreadPtr()),
1478 static_cast<void*>(new_thread_sp.get()),
1479 new_thread_sp->GetQueueID(),
1490 log->Printf ("SBThread(%p)::GetExtendedBacktraceThread() => error: process is running",
1491 static_cast<void*>(exe_ctx.GetThreadPtr()));
1495 if (log && sb_origin_thread.IsValid() == false)
1496 log->Printf("SBThread(%p)::GetExtendedBacktraceThread() is not returning a Valid thread",
1497 static_cast<void*>(exe_ctx.GetThreadPtr()));
1498 return sb_origin_thread;
1502 SBThread::GetExtendedBacktraceOriginatingIndexID ()
1504 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1506 return thread_sp->GetExtendedBacktraceOriginatingIndexID();
1507 return LLDB_INVALID_INDEX32;
1511 SBThread::SafeToCallFunctions ()
1513 ThreadSP thread_sp(m_opaque_sp->GetThreadSP());
1515 return thread_sp->SafeToCallFunctions();