1 //===-- SBProcess.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/SBProcess.h"
15 #include "lldb/lldb-defines.h"
16 #include "lldb/lldb-types.h"
18 #include "lldb/Interpreter/Args.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/Log.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/PluginManager.h"
23 #include "lldb/Core/State.h"
24 #include "lldb/Core/Stream.h"
25 #include "lldb/Core/StreamFile.h"
26 #include "lldb/Target/MemoryRegionInfo.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/SystemRuntime.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
35 #include "lldb/API/SBBroadcaster.h"
36 #include "lldb/API/SBCommandReturnObject.h"
37 #include "lldb/API/SBDebugger.h"
38 #include "lldb/API/SBEvent.h"
39 #include "lldb/API/SBFileSpec.h"
40 #include "lldb/API/SBMemoryRegionInfo.h"
41 #include "lldb/API/SBMemoryRegionInfoList.h"
42 #include "lldb/API/SBThread.h"
43 #include "lldb/API/SBThreadCollection.h"
44 #include "lldb/API/SBStream.h"
45 #include "lldb/API/SBStringList.h"
46 #include "lldb/API/SBUnixSignals.h"
49 using namespace lldb_private;
52 SBProcess::SBProcess () :
58 //----------------------------------------------------------------------
59 // SBProcess constructor
60 //----------------------------------------------------------------------
62 SBProcess::SBProcess (const SBProcess& rhs) :
63 m_opaque_wp (rhs.m_opaque_wp)
68 SBProcess::SBProcess (const lldb::ProcessSP &process_sp) :
69 m_opaque_wp (process_sp)
74 SBProcess::operator = (const SBProcess& rhs)
77 m_opaque_wp = rhs.m_opaque_wp;
81 //----------------------------------------------------------------------
83 //----------------------------------------------------------------------
84 SBProcess::~SBProcess()
89 SBProcess::GetBroadcasterClassName ()
91 return Process::GetStaticBroadcasterClass().AsCString();
95 SBProcess::GetPluginName ()
97 ProcessSP process_sp(GetSP());
100 return process_sp->GetPluginName().GetCString();
106 SBProcess::GetShortPluginName ()
108 ProcessSP process_sp(GetSP());
111 return process_sp->GetPluginName().GetCString();
118 SBProcess::GetSP() const
120 return m_opaque_wp.lock();
124 SBProcess::SetSP (const ProcessSP &process_sp)
126 m_opaque_wp = process_sp;
137 SBProcess::IsValid() const
139 ProcessSP process_sp(m_opaque_wp.lock());
140 return ((bool) process_sp && process_sp->IsValid());
144 SBProcess::RemoteLaunch (char const **argv,
146 const char *stdin_path,
147 const char *stdout_path,
148 const char *stderr_path,
149 const char *working_directory,
150 uint32_t launch_flags,
152 lldb::SBError& error)
154 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
156 log->Printf ("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...",
157 static_cast<void*>(m_opaque_wp.lock().get()),
158 static_cast<void*>(argv), static_cast<void*>(envp),
159 stdin_path ? stdin_path : "NULL",
160 stdout_path ? stdout_path : "NULL",
161 stderr_path ? stderr_path : "NULL",
162 working_directory ? working_directory : "NULL",
163 launch_flags, stop_at_entry,
164 static_cast<void*>(error.get()));
166 ProcessSP process_sp(GetSP());
169 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
170 if (process_sp->GetState() == eStateConnected)
173 launch_flags |= eLaunchFlagStopAtEntry;
174 ProcessLaunchInfo launch_info(FileSpec{stdin_path, false},
175 FileSpec{stdout_path, false},
176 FileSpec{stderr_path, false},
177 FileSpec{working_directory, false},
179 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
181 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
183 launch_info.GetArguments().AppendArguments (argv);
185 launch_info.GetEnvironmentEntries ().SetArguments (envp);
186 error.SetError (process_sp->Launch (launch_info));
190 error.SetErrorString ("must be in eStateConnected to call RemoteLaunch");
195 error.SetErrorString ("unable to attach pid");
200 error.GetDescription (sstr);
201 log->Printf ("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
202 static_cast<void*>(process_sp.get()),
203 static_cast<void*>(error.get()), sstr.GetData());
206 return error.Success();
210 SBProcess::RemoteAttachToProcessWithID (lldb::pid_t pid, lldb::SBError& error)
212 ProcessSP process_sp(GetSP());
215 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
216 if (process_sp->GetState() == eStateConnected)
218 ProcessAttachInfo attach_info;
219 attach_info.SetProcessID (pid);
220 error.SetError (process_sp->Attach (attach_info));
224 error.SetErrorString ("must be in eStateConnected to call RemoteAttachToProcessWithID");
229 error.SetErrorString ("unable to attach pid");
232 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
235 error.GetDescription (sstr);
236 log->Printf ("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64 ") => SBError (%p): %s",
237 static_cast<void*>(process_sp.get()), pid,
238 static_cast<void*>(error.get()), sstr.GetData());
241 return error.Success();
246 SBProcess::GetNumThreads ()
248 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
250 uint32_t num_threads = 0;
251 ProcessSP process_sp(GetSP());
254 Process::StopLocker stop_locker;
256 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
257 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
258 num_threads = process_sp->GetThreadList().GetSize(can_update);
262 log->Printf ("SBProcess(%p)::GetNumThreads () => %d",
263 static_cast<void*>(process_sp.get()), num_threads);
269 SBProcess::GetSelectedThread () const
271 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
275 ProcessSP process_sp(GetSP());
278 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
279 thread_sp = process_sp->GetThreadList().GetSelectedThread();
280 sb_thread.SetThread (thread_sp);
284 log->Printf ("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
285 static_cast<void*>(process_sp.get()),
286 static_cast<void*>(thread_sp.get()));
292 SBProcess::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
294 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
298 ProcessSP process_sp(GetSP());
301 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
302 thread_sp = process_sp->CreateOSPluginThread(tid, context);
303 sb_thread.SetThread (thread_sp);
307 log->Printf ("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64 ", context=0x%" PRIx64 ") => SBThread(%p)",
308 static_cast<void*>(process_sp.get()), tid, context,
309 static_cast<void*>(thread_sp.get()));
315 SBProcess::GetTarget() const
317 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
321 ProcessSP process_sp(GetSP());
324 target_sp = process_sp->GetTarget().shared_from_this();
325 sb_target.SetSP (target_sp);
329 log->Printf ("SBProcess(%p)::GetTarget () => SBTarget(%p)",
330 static_cast<void*>(process_sp.get()),
331 static_cast<void*>(target_sp.get()));
338 SBProcess::PutSTDIN (const char *src, size_t src_len)
340 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
343 ProcessSP process_sp(GetSP());
347 ret_val = process_sp->PutSTDIN (src, src_len, error);
351 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64 ") => %" PRIu64,
352 static_cast<void*>(process_sp.get()), src,
353 static_cast<uint64_t>(src_len),
354 static_cast<uint64_t>(ret_val));
360 SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
362 size_t bytes_read = 0;
363 ProcessSP process_sp(GetSP());
367 bytes_read = process_sp->GetSTDOUT (dst, dst_len, error);
370 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
372 log->Printf ("SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
373 static_cast<void*>(process_sp.get()),
374 static_cast<int>(bytes_read), dst,
375 static_cast<uint64_t>(dst_len),
376 static_cast<uint64_t>(bytes_read));
382 SBProcess::GetSTDERR (char *dst, size_t dst_len) const
384 size_t bytes_read = 0;
385 ProcessSP process_sp(GetSP());
389 bytes_read = process_sp->GetSTDERR (dst, dst_len, error);
392 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
394 log->Printf ("SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
395 static_cast<void*>(process_sp.get()),
396 static_cast<int>(bytes_read), dst,
397 static_cast<uint64_t>(dst_len),
398 static_cast<uint64_t>(bytes_read));
404 SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const
406 size_t bytes_read = 0;
407 ProcessSP process_sp(GetSP());
411 bytes_read = process_sp->GetAsyncProfileData (dst, dst_len, error);
414 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
416 log->Printf ("SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
417 static_cast<void*>(process_sp.get()),
418 static_cast<int>(bytes_read), dst,
419 static_cast<uint64_t>(dst_len),
420 static_cast<uint64_t>(bytes_read));
426 SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
431 ProcessSP process_sp(GetSP());
434 const StateType event_state = SBProcess::GetStateFromEvent (event);
436 int message_len = ::snprintf (message,
438 "Process %" PRIu64 " %s\n",
440 SBDebugger::StateAsCString (event_state));
443 ::fwrite (message, 1, message_len, out);
448 SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
450 ProcessSP process_sp(GetSP());
453 const StateType event_state = SBProcess::GetStateFromEvent (event);
457 "Process %" PRIu64 " %s\n",
459 SBDebugger::StateAsCString (event_state));
461 result.AppendMessage (message);
466 SBProcess::SetSelectedThread (const SBThread &thread)
468 ProcessSP process_sp(GetSP());
471 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
472 return process_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
478 SBProcess::SetSelectedThreadByID (lldb::tid_t tid)
480 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
482 bool ret_val = false;
483 ProcessSP process_sp(GetSP());
486 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
487 ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid);
491 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64 ") => %s",
492 static_cast<void*>(process_sp.get()), tid,
493 (ret_val ? "true" : "false"));
499 SBProcess::SetSelectedThreadByIndexID (uint32_t index_id)
501 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
503 bool ret_val = false;
504 ProcessSP process_sp(GetSP());
507 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
508 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id);
512 log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
513 static_cast<void*>(process_sp.get()), index_id,
514 (ret_val ? "true" : "false"));
520 SBProcess::GetThreadAtIndex (size_t index)
522 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
526 ProcessSP process_sp(GetSP());
529 Process::StopLocker stop_locker;
530 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
531 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
532 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
533 sb_thread.SetThread (thread_sp);
537 log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
538 static_cast<void*>(process_sp.get()),
539 static_cast<uint32_t>(index),
540 static_cast<void*>(thread_sp.get()));
546 SBProcess::GetNumQueues ()
548 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
550 uint32_t num_queues = 0;
551 ProcessSP process_sp(GetSP());
554 Process::StopLocker stop_locker;
555 if (stop_locker.TryLock(&process_sp->GetRunLock()))
557 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
558 num_queues = process_sp->GetQueueList().GetSize();
563 log->Printf ("SBProcess(%p)::GetNumQueues () => %d",
564 static_cast<void*>(process_sp.get()), num_queues);
570 SBProcess::GetQueueAtIndex (size_t index)
572 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
576 ProcessSP process_sp(GetSP());
579 Process::StopLocker stop_locker;
580 if (stop_locker.TryLock(&process_sp->GetRunLock()))
582 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
583 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
584 sb_queue.SetQueue (queue_sp);
589 log->Printf ("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
590 static_cast<void*>(process_sp.get()),
591 static_cast<uint32_t>(index),
592 static_cast<void*>(queue_sp.get()));
599 SBProcess::GetStopID(bool include_expression_stops)
601 ProcessSP process_sp(GetSP());
604 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
605 if (include_expression_stops)
606 return process_sp->GetStopID();
608 return process_sp->GetLastNaturalStopID();
614 SBProcess::GetStopEventForStopID(uint32_t stop_id)
616 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
620 ProcessSP process_sp(GetSP());
623 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
624 event_sp = process_sp->GetStopEventForStopID(stop_id);
625 sb_event.reset(event_sp);
629 log->Printf ("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32 ") => SBEvent(%p)",
630 static_cast<void*>(process_sp.get()),
632 static_cast<void*>(event_sp.get()));
638 SBProcess::GetState ()
641 StateType ret_val = eStateInvalid;
642 ProcessSP process_sp(GetSP());
645 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
646 ret_val = process_sp->GetState();
649 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
651 log->Printf ("SBProcess(%p)::GetState () => %s",
652 static_cast<void*>(process_sp.get()),
653 lldb_private::StateAsCString (ret_val));
660 SBProcess::GetExitStatus ()
663 ProcessSP process_sp(GetSP());
666 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
667 exit_status = process_sp->GetExitStatus ();
669 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
671 log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
672 static_cast<void*>(process_sp.get()), exit_status,
679 SBProcess::GetExitDescription ()
681 const char *exit_desc = NULL;
682 ProcessSP process_sp(GetSP());
685 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
686 exit_desc = process_sp->GetExitDescription ();
688 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
690 log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
691 static_cast<void*>(process_sp.get()), exit_desc);
696 SBProcess::GetProcessID ()
698 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
699 ProcessSP process_sp(GetSP());
701 ret_val = process_sp->GetID();
703 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
705 log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64,
706 static_cast<void*>(process_sp.get()), ret_val);
712 SBProcess::GetUniqueID()
714 uint32_t ret_val = 0;
715 ProcessSP process_sp(GetSP());
717 ret_val = process_sp->GetUniqueID();
718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
720 log->Printf ("SBProcess(%p)::GetUniqueID () => %" PRIu32,
721 static_cast<void*>(process_sp.get()), ret_val);
726 SBProcess::GetByteOrder () const
728 ByteOrder byteOrder = eByteOrderInvalid;
729 ProcessSP process_sp(GetSP());
731 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
733 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
735 log->Printf ("SBProcess(%p)::GetByteOrder () => %d",
736 static_cast<void*>(process_sp.get()), byteOrder);
742 SBProcess::GetAddressByteSize () const
745 ProcessSP process_sp(GetSP());
747 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
749 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
751 log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d",
752 static_cast<void*>(process_sp.get()), size);
758 SBProcess::Continue ()
760 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
763 ProcessSP process_sp(GetSP());
766 log->Printf ("SBProcess(%p)::Continue ()...",
767 static_cast<void*>(process_sp.get()));
771 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
773 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution ())
774 sb_error.ref() = process_sp->Resume ();
776 sb_error.ref() = process_sp->ResumeSynchronous (NULL);
779 sb_error.SetErrorString ("SBProcess is invalid");
784 sb_error.GetDescription (sstr);
785 log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s",
786 static_cast<void*>(process_sp.get()),
787 static_cast<void*>(sb_error.get()), sstr.GetData());
795 SBProcess::Destroy ()
798 ProcessSP process_sp(GetSP());
801 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
802 sb_error.SetError(process_sp->Destroy(false));
805 sb_error.SetErrorString ("SBProcess is invalid");
807 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
811 sb_error.GetDescription (sstr);
812 log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s",
813 static_cast<void*>(process_sp.get()),
814 static_cast<void*>(sb_error.get()), sstr.GetData());
825 ProcessSP process_sp(GetSP());
828 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
829 sb_error.SetError (process_sp->Halt());
832 sb_error.SetErrorString ("SBProcess is invalid");
834 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
838 sb_error.GetDescription (sstr);
839 log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
840 static_cast<void*>(process_sp.get()),
841 static_cast<void*>(sb_error.get()), sstr.GetData());
851 ProcessSP process_sp(GetSP());
854 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
855 sb_error.SetError (process_sp->Destroy(true));
858 sb_error.SetErrorString ("SBProcess is invalid");
860 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
864 sb_error.GetDescription (sstr);
865 log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
866 static_cast<void*>(process_sp.get()),
867 static_cast<void*>(sb_error.get()), sstr.GetData());
876 // FIXME: This should come from a process default.
877 bool keep_stopped = false;
878 return Detach (keep_stopped);
882 SBProcess::Detach (bool keep_stopped)
885 ProcessSP process_sp(GetSP());
888 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
889 sb_error.SetError (process_sp->Detach(keep_stopped));
892 sb_error.SetErrorString ("SBProcess is invalid");
898 SBProcess::Signal (int signo)
901 ProcessSP process_sp(GetSP());
904 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
905 sb_error.SetError (process_sp->Signal (signo));
908 sb_error.SetErrorString ("SBProcess is invalid");
909 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
913 sb_error.GetDescription (sstr);
914 log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
915 static_cast<void*>(process_sp.get()), signo,
916 static_cast<void*>(sb_error.get()), sstr.GetData());
922 SBProcess::GetUnixSignals()
924 if (auto process_sp = GetSP())
925 return SBUnixSignals{process_sp};
931 SBProcess::SendAsyncInterrupt ()
933 ProcessSP process_sp(GetSP());
936 process_sp->SendAsyncInterrupt ();
941 SBProcess::GetThreadByID (tid_t tid)
945 ProcessSP process_sp(GetSP());
948 Process::StopLocker stop_locker;
949 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
950 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
951 thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
952 sb_thread.SetThread (thread_sp);
955 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
957 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
958 static_cast<void*>(process_sp.get()), tid,
959 static_cast<void*>(thread_sp.get()));
965 SBProcess::GetThreadByIndexID (uint32_t index_id)
969 ProcessSP process_sp(GetSP());
972 Process::StopLocker stop_locker;
973 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
974 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
975 thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
976 sb_thread.SetThread (thread_sp);
979 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
981 log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
982 static_cast<void*>(process_sp.get()), index_id,
983 static_cast<void*>(thread_sp.get()));
989 SBProcess::GetStateFromEvent (const SBEvent &event)
991 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
993 StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
996 log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
997 static_cast<void*>(event.get()),
998 lldb_private::StateAsCString (ret_val));
1004 SBProcess::GetRestartedFromEvent (const SBEvent &event)
1006 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1008 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent (event.get());
1011 log->Printf ("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
1012 static_cast<void*>(event.get()), ret_val);
1018 SBProcess::GetNumRestartedReasonsFromEvent (const lldb::SBEvent &event)
1020 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
1024 SBProcess::GetRestartedReasonAtIndexFromEvent (const lldb::SBEvent &event, size_t idx)
1026 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
1030 SBProcess::GetProcessFromEvent (const SBEvent &event)
1032 SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
1037 SBProcess::GetInterruptedFromEvent (const SBEvent &event)
1039 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
1043 SBProcess::EventIsProcessEvent (const SBEvent &event)
1045 return event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass();
1049 SBProcess::GetBroadcaster () const
1051 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1053 ProcessSP process_sp(GetSP());
1055 SBBroadcaster broadcaster(process_sp.get(), false);
1058 log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
1059 static_cast<void*>(process_sp.get()),
1060 static_cast<void*>(broadcaster.get()));
1066 SBProcess::GetBroadcasterClass ()
1068 return Process::GetStaticBroadcasterClass().AsCString();
1072 SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
1074 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1076 size_t bytes_read = 0;
1078 ProcessSP process_sp(GetSP());
1081 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
1082 static_cast<void*>(process_sp.get()), addr,
1083 static_cast<void*>(dst), static_cast<uint64_t>(dst_len),
1084 static_cast<void*>(sb_error.get()));
1088 Process::StopLocker stop_locker;
1089 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1091 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1092 bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
1097 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running",
1098 static_cast<void*>(process_sp.get()));
1099 sb_error.SetErrorString("process is running");
1104 sb_error.SetErrorString ("SBProcess is invalid");
1110 sb_error.GetDescription (sstr);
1111 log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1112 static_cast<void*>(process_sp.get()), addr,
1113 static_cast<void*>(dst), static_cast<uint64_t>(dst_len),
1114 static_cast<void*>(sb_error.get()), sstr.GetData(),
1115 static_cast<uint64_t>(bytes_read));
1122 SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
1124 size_t bytes_read = 0;
1125 ProcessSP process_sp(GetSP());
1128 Process::StopLocker stop_locker;
1129 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1131 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1132 bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
1136 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1138 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running",
1139 static_cast<void*>(process_sp.get()));
1140 sb_error.SetErrorString("process is running");
1145 sb_error.SetErrorString ("SBProcess is invalid");
1151 SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
1154 ProcessSP process_sp(GetSP());
1157 Process::StopLocker stop_locker;
1158 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1160 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1161 value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
1165 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1167 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running",
1168 static_cast<void*>(process_sp.get()));
1169 sb_error.SetErrorString("process is running");
1174 sb_error.SetErrorString ("SBProcess is invalid");
1180 SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
1182 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1183 ProcessSP process_sp(GetSP());
1186 Process::StopLocker stop_locker;
1187 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1189 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1190 ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
1194 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1196 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running",
1197 static_cast<void*>(process_sp.get()));
1198 sb_error.SetErrorString("process is running");
1203 sb_error.SetErrorString ("SBProcess is invalid");
1209 SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
1211 size_t bytes_written = 0;
1213 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1215 ProcessSP process_sp(GetSP());
1218 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1219 static_cast<void*>(process_sp.get()), addr,
1220 static_cast<const void*>(src),
1221 static_cast<uint64_t>(src_len),
1222 static_cast<void*>(sb_error.get()));
1226 Process::StopLocker stop_locker;
1227 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1229 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1230 bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
1235 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running",
1236 static_cast<void*>(process_sp.get()));
1237 sb_error.SetErrorString("process is running");
1244 sb_error.GetDescription (sstr);
1245 log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1246 static_cast<void*>(process_sp.get()), addr,
1247 static_cast<const void*>(src),
1248 static_cast<uint64_t>(src_len),
1249 static_cast<void*>(sb_error.get()), sstr.GetData(),
1250 static_cast<uint64_t>(bytes_written));
1253 return bytes_written;
1257 SBProcess::GetDescription (SBStream &description)
1259 Stream &strm = description.ref();
1261 ProcessSP process_sp(GetSP());
1264 char path[PATH_MAX];
1265 GetTarget().GetExecutable().GetPath (path, sizeof(path));
1266 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1267 const char *exe_name = NULL;
1269 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1271 strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1272 process_sp->GetID(),
1273 lldb_private::StateAsCString (GetState()),
1275 exe_name ? ", executable = " : "",
1276 exe_name ? exe_name : "");
1279 strm.PutCString ("No value");
1285 SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
1287 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1290 ProcessSP process_sp(GetSP());
1293 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1294 sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
1296 log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1297 static_cast<void*>(process_sp.get()), num);
1301 sb_error.SetErrorString ("SBProcess is invalid");
1307 SBProcess::LoadImage (lldb::SBFileSpec &sb_remote_image_spec, lldb::SBError &sb_error)
1309 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1313 SBProcess::LoadImage (const lldb::SBFileSpec &sb_local_image_spec,
1314 const lldb::SBFileSpec &sb_remote_image_spec,
1315 lldb::SBError &sb_error)
1317 ProcessSP process_sp(GetSP());
1320 Process::StopLocker stop_locker;
1321 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1323 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1324 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1325 return platform_sp->LoadImage (process_sp.get(),
1326 *sb_local_image_spec,
1327 *sb_remote_image_spec,
1332 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1334 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running",
1335 static_cast<void*>(process_sp.get()));
1336 sb_error.SetErrorString("process is running");
1339 return LLDB_INVALID_IMAGE_TOKEN;
1343 SBProcess::UnloadImage (uint32_t image_token)
1345 lldb::SBError sb_error;
1346 ProcessSP process_sp(GetSP());
1349 Process::StopLocker stop_locker;
1350 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1352 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1353 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1354 sb_error.SetError (platform_sp->UnloadImage (process_sp.get(), image_token));
1358 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1360 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running",
1361 static_cast<void*>(process_sp.get()));
1362 sb_error.SetErrorString("process is running");
1366 sb_error.SetErrorString("invalid process");
1371 SBProcess::SendEventData (const char *event_data)
1373 lldb::SBError sb_error;
1374 ProcessSP process_sp(GetSP());
1377 Process::StopLocker stop_locker;
1378 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1380 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1381 sb_error.SetError (process_sp->SendEventData (event_data));
1385 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1387 log->Printf ("SBProcess(%p)::SendEventData() => error: process is running",
1388 static_cast<void*>(process_sp.get()));
1389 sb_error.SetErrorString("process is running");
1393 sb_error.SetErrorString("invalid process");
1398 SBProcess::GetNumExtendedBacktraceTypes ()
1400 ProcessSP process_sp(GetSP());
1401 if (process_sp && process_sp->GetSystemRuntime())
1403 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1404 return runtime->GetExtendedBacktraceTypes().size();
1410 SBProcess::GetExtendedBacktraceTypeAtIndex (uint32_t idx)
1412 ProcessSP process_sp(GetSP());
1413 if (process_sp && process_sp->GetSystemRuntime())
1415 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1416 const std::vector<ConstString> &names = runtime->GetExtendedBacktraceTypes();
1417 if (idx < names.size())
1419 return names[idx].AsCString();
1423 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1425 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => error: requested extended backtrace name out of bounds",
1426 static_cast<void*>(process_sp.get()));
1433 SBProcess::GetHistoryThreads (addr_t addr)
1435 ProcessSP process_sp(GetSP());
1436 SBThreadCollection threads;
1439 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1445 SBProcess::IsInstrumentationRuntimePresent(InstrumentationRuntimeType type)
1447 ProcessSP process_sp(GetSP());
1451 InstrumentationRuntimeSP runtime_sp = process_sp->GetInstrumentationRuntime(type);
1453 if (! runtime_sp.get())
1456 return runtime_sp->IsActive();
1460 SBProcess::SaveCore(const char *file_name)
1462 lldb::SBError error;
1463 ProcessSP process_sp(GetSP());
1466 error.SetErrorString("SBProcess is invalid");
1470 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1472 if (process_sp->GetState() != eStateStopped)
1474 error.SetErrorString("the process is not stopped");
1478 FileSpec core_file(file_name, false);
1479 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1484 SBProcess::GetMemoryRegionInfo (lldb::addr_t load_addr, SBMemoryRegionInfo &sb_region_info)
1486 lldb::SBError sb_error;
1487 ProcessSP process_sp(GetSP());
1488 MemoryRegionInfoSP region_info_sp = std::make_shared<lldb_private::MemoryRegionInfo>();
1491 Process::StopLocker stop_locker;
1492 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1494 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1495 sb_error.ref() = process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1496 if( sb_error.Success() ) {
1497 sb_region_info.ref() = *region_info_sp;
1502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1504 log->Printf ("SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1505 static_cast<void*>(process_sp.get()));
1506 sb_error.SetErrorString("process is running");
1511 sb_error.SetErrorString ("SBProcess is invalid");
1516 lldb::SBMemoryRegionInfoList
1517 SBProcess::GetMemoryRegions()
1519 lldb::SBError sb_error;
1520 lldb::SBMemoryRegionInfoList sb_region_list;
1521 ProcessSP process_sp(GetSP());
1524 Process::StopLocker stop_locker;
1525 if (stop_locker.TryLock(&process_sp->GetRunLock()))
1527 std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1528 std::vector<MemoryRegionInfoSP> region_list;
1529 sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1530 if( sb_error.Success() ) {
1531 std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1532 for( std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin(); it != end; it++ ) {
1533 SBMemoryRegionInfo sb_region_info(it->get());
1534 sb_region_list.Append(sb_region_info);
1540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1542 log->Printf ("SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1543 static_cast<void*>(process_sp.get()));
1544 sb_error.SetErrorString("process is running");
1549 sb_error.SetErrorString ("SBProcess is invalid");
1551 return sb_region_list;