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/Core/Debugger.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Interpreter/Args.h"
24 #include "lldb/Target/MemoryRegionInfo.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/SystemRuntime.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/Stream.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/SBStream.h"
43 #include "lldb/API/SBStringList.h"
44 #include "lldb/API/SBStructuredData.h"
45 #include "lldb/API/SBThread.h"
46 #include "lldb/API/SBThreadCollection.h"
47 #include "lldb/API/SBTrace.h"
48 #include "lldb/API/SBTraceOptions.h"
49 #include "lldb/API/SBUnixSignals.h"
52 using namespace lldb_private;
54 SBProcess::SBProcess() : m_opaque_wp() {}
56 //----------------------------------------------------------------------
57 // SBProcess constructor
58 //----------------------------------------------------------------------
60 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
62 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
63 : m_opaque_wp(process_sp) {}
65 const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
67 m_opaque_wp = rhs.m_opaque_wp;
71 //----------------------------------------------------------------------
73 //----------------------------------------------------------------------
74 SBProcess::~SBProcess() {}
76 const char *SBProcess::GetBroadcasterClassName() {
77 return Process::GetStaticBroadcasterClass().AsCString();
80 const char *SBProcess::GetPluginName() {
81 ProcessSP process_sp(GetSP());
83 return process_sp->GetPluginName().GetCString();
88 const char *SBProcess::GetShortPluginName() {
89 ProcessSP process_sp(GetSP());
91 return process_sp->GetPluginName().GetCString();
96 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
98 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
100 void SBProcess::Clear() { m_opaque_wp.reset(); }
102 bool SBProcess::IsValid() const {
103 ProcessSP process_sp(m_opaque_wp.lock());
104 return ((bool)process_sp && process_sp->IsValid());
107 bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
108 const char *stdin_path, const char *stdout_path,
109 const char *stderr_path,
110 const char *working_directory,
111 uint32_t launch_flags, bool stop_at_entry,
112 lldb::SBError &error) {
113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
115 log->Printf("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, "
116 "stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, "
117 "stop_at_entry=%i, &error (%p))...",
118 static_cast<void *>(m_opaque_wp.lock().get()),
119 static_cast<void *>(argv), static_cast<void *>(envp),
120 stdin_path ? stdin_path : "NULL",
121 stdout_path ? stdout_path : "NULL",
122 stderr_path ? stderr_path : "NULL",
123 working_directory ? working_directory : "NULL", launch_flags,
124 stop_at_entry, static_cast<void *>(error.get()));
126 ProcessSP process_sp(GetSP());
128 std::lock_guard<std::recursive_mutex> guard(
129 process_sp->GetTarget().GetAPIMutex());
130 if (process_sp->GetState() == eStateConnected) {
132 launch_flags |= eLaunchFlagStopAtEntry;
133 ProcessLaunchInfo launch_info(
134 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
135 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
137 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
139 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
141 launch_info.GetArguments().AppendArguments(argv);
143 launch_info.GetEnvironmentEntries().SetArguments(envp);
144 error.SetError(process_sp->Launch(launch_info));
146 error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
149 error.SetErrorString("unable to attach pid");
154 error.GetDescription(sstr);
155 log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
156 static_cast<void *>(process_sp.get()),
157 static_cast<void *>(error.get()), sstr.GetData());
160 return error.Success();
163 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
164 lldb::SBError &error) {
165 ProcessSP process_sp(GetSP());
167 std::lock_guard<std::recursive_mutex> guard(
168 process_sp->GetTarget().GetAPIMutex());
169 if (process_sp->GetState() == eStateConnected) {
170 ProcessAttachInfo attach_info;
171 attach_info.SetProcessID(pid);
172 error.SetError(process_sp->Attach(attach_info));
174 error.SetErrorString(
175 "must be in eStateConnected to call RemoteAttachToProcessWithID");
178 error.SetErrorString("unable to attach pid");
181 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
184 error.GetDescription(sstr);
185 log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
186 ") => SBError (%p): %s",
187 static_cast<void *>(process_sp.get()), pid,
188 static_cast<void *>(error.get()), sstr.GetData());
191 return error.Success();
194 uint32_t SBProcess::GetNumThreads() {
195 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
197 uint32_t num_threads = 0;
198 ProcessSP process_sp(GetSP());
200 Process::StopLocker stop_locker;
202 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
203 std::lock_guard<std::recursive_mutex> guard(
204 process_sp->GetTarget().GetAPIMutex());
205 num_threads = process_sp->GetThreadList().GetSize(can_update);
209 log->Printf("SBProcess(%p)::GetNumThreads () => %d",
210 static_cast<void *>(process_sp.get()), num_threads);
215 SBThread SBProcess::GetSelectedThread() const {
216 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
220 ProcessSP process_sp(GetSP());
222 std::lock_guard<std::recursive_mutex> guard(
223 process_sp->GetTarget().GetAPIMutex());
224 thread_sp = process_sp->GetThreadList().GetSelectedThread();
225 sb_thread.SetThread(thread_sp);
229 log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
230 static_cast<void *>(process_sp.get()),
231 static_cast<void *>(thread_sp.get()));
236 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
237 lldb::addr_t context) {
238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
242 ProcessSP process_sp(GetSP());
244 std::lock_guard<std::recursive_mutex> guard(
245 process_sp->GetTarget().GetAPIMutex());
246 thread_sp = process_sp->CreateOSPluginThread(tid, context);
247 sb_thread.SetThread(thread_sp);
251 log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
252 ", context=0x%" PRIx64 ") => SBThread(%p)",
253 static_cast<void *>(process_sp.get()), tid, context,
254 static_cast<void *>(thread_sp.get()));
259 SBTarget SBProcess::GetTarget() const {
260 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
264 ProcessSP process_sp(GetSP());
266 target_sp = process_sp->GetTarget().shared_from_this();
267 sb_target.SetSP(target_sp);
271 log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
272 static_cast<void *>(process_sp.get()),
273 static_cast<void *>(target_sp.get()));
278 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
279 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
282 ProcessSP process_sp(GetSP());
285 ret_val = process_sp->PutSTDIN(src, src_len, error);
289 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
291 static_cast<void *>(process_sp.get()), src,
292 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
297 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
298 size_t bytes_read = 0;
299 ProcessSP process_sp(GetSP());
302 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
305 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
308 "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
310 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
311 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
316 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
317 size_t bytes_read = 0;
318 ProcessSP process_sp(GetSP());
321 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
324 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
327 "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
329 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
330 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
335 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
336 size_t bytes_read = 0;
337 ProcessSP process_sp(GetSP());
340 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
343 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
346 "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
348 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
349 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
354 lldb::SBTrace SBProcess::StartTrace(SBTraceOptions &options,
355 lldb::SBError &error) {
356 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
357 ProcessSP process_sp(GetSP());
359 SBTrace trace_instance;
360 trace_instance.SetSP(process_sp);
361 lldb::user_id_t uid = LLDB_INVALID_UID;
364 error.SetErrorString("invalid process");
366 uid = process_sp->StartTrace(*(options.m_traceoptions_sp), error.ref());
367 trace_instance.SetTraceUID(uid);
368 LLDB_LOG(log, "SBProcess::returned uid - {0}", uid);
370 return trace_instance;
373 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
377 ProcessSP process_sp(GetSP());
379 const StateType event_state = SBProcess::GetStateFromEvent(event);
381 int message_len = ::snprintf(
382 message, sizeof(message), "Process %" PRIu64 " %s\n",
383 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
386 ::fwrite(message, 1, message_len, out);
390 void SBProcess::AppendEventStateReport(const SBEvent &event,
391 SBCommandReturnObject &result) {
392 ProcessSP process_sp(GetSP());
394 const StateType event_state = SBProcess::GetStateFromEvent(event);
396 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
397 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
399 result.AppendMessage(message);
403 bool SBProcess::SetSelectedThread(const SBThread &thread) {
404 ProcessSP process_sp(GetSP());
406 std::lock_guard<std::recursive_mutex> guard(
407 process_sp->GetTarget().GetAPIMutex());
408 return process_sp->GetThreadList().SetSelectedThreadByID(
409 thread.GetThreadID());
414 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
415 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
417 bool ret_val = false;
418 ProcessSP process_sp(GetSP());
420 std::lock_guard<std::recursive_mutex> guard(
421 process_sp->GetTarget().GetAPIMutex());
422 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
426 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
428 static_cast<void *>(process_sp.get()), tid,
429 (ret_val ? "true" : "false"));
434 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
435 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
437 bool ret_val = false;
438 ProcessSP process_sp(GetSP());
440 std::lock_guard<std::recursive_mutex> guard(
441 process_sp->GetTarget().GetAPIMutex());
442 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
446 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
447 static_cast<void *>(process_sp.get()), index_id,
448 (ret_val ? "true" : "false"));
453 SBThread SBProcess::GetThreadAtIndex(size_t index) {
454 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
458 ProcessSP process_sp(GetSP());
460 Process::StopLocker stop_locker;
461 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
462 std::lock_guard<std::recursive_mutex> guard(
463 process_sp->GetTarget().GetAPIMutex());
464 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
465 sb_thread.SetThread(thread_sp);
469 log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
470 static_cast<void *>(process_sp.get()),
471 static_cast<uint32_t>(index),
472 static_cast<void *>(thread_sp.get()));
477 uint32_t SBProcess::GetNumQueues() {
478 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
480 uint32_t num_queues = 0;
481 ProcessSP process_sp(GetSP());
483 Process::StopLocker stop_locker;
484 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
485 std::lock_guard<std::recursive_mutex> guard(
486 process_sp->GetTarget().GetAPIMutex());
487 num_queues = process_sp->GetQueueList().GetSize();
492 log->Printf("SBProcess(%p)::GetNumQueues () => %d",
493 static_cast<void *>(process_sp.get()), num_queues);
498 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
499 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
503 ProcessSP process_sp(GetSP());
505 Process::StopLocker stop_locker;
506 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
507 std::lock_guard<std::recursive_mutex> guard(
508 process_sp->GetTarget().GetAPIMutex());
509 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
510 sb_queue.SetQueue(queue_sp);
515 log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
516 static_cast<void *>(process_sp.get()),
517 static_cast<uint32_t>(index),
518 static_cast<void *>(queue_sp.get()));
523 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
524 ProcessSP process_sp(GetSP());
526 std::lock_guard<std::recursive_mutex> guard(
527 process_sp->GetTarget().GetAPIMutex());
528 if (include_expression_stops)
529 return process_sp->GetStopID();
531 return process_sp->GetLastNaturalStopID();
536 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
537 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
541 ProcessSP process_sp(GetSP());
543 std::lock_guard<std::recursive_mutex> guard(
544 process_sp->GetTarget().GetAPIMutex());
545 event_sp = process_sp->GetStopEventForStopID(stop_id);
546 sb_event.reset(event_sp);
550 log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
552 static_cast<void *>(process_sp.get()), stop_id,
553 static_cast<void *>(event_sp.get()));
558 StateType SBProcess::GetState() {
560 StateType ret_val = eStateInvalid;
561 ProcessSP process_sp(GetSP());
563 std::lock_guard<std::recursive_mutex> guard(
564 process_sp->GetTarget().GetAPIMutex());
565 ret_val = process_sp->GetState();
568 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
570 log->Printf("SBProcess(%p)::GetState () => %s",
571 static_cast<void *>(process_sp.get()),
572 lldb_private::StateAsCString(ret_val));
577 int SBProcess::GetExitStatus() {
579 ProcessSP process_sp(GetSP());
581 std::lock_guard<std::recursive_mutex> guard(
582 process_sp->GetTarget().GetAPIMutex());
583 exit_status = process_sp->GetExitStatus();
585 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
587 log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
588 static_cast<void *>(process_sp.get()), exit_status,
594 const char *SBProcess::GetExitDescription() {
595 const char *exit_desc = NULL;
596 ProcessSP process_sp(GetSP());
598 std::lock_guard<std::recursive_mutex> guard(
599 process_sp->GetTarget().GetAPIMutex());
600 exit_desc = process_sp->GetExitDescription();
602 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
604 log->Printf("SBProcess(%p)::GetExitDescription () => %s",
605 static_cast<void *>(process_sp.get()), exit_desc);
609 lldb::pid_t SBProcess::GetProcessID() {
610 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
611 ProcessSP process_sp(GetSP());
613 ret_val = process_sp->GetID();
615 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
617 log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
618 static_cast<void *>(process_sp.get()), ret_val);
623 uint32_t SBProcess::GetUniqueID() {
624 uint32_t ret_val = 0;
625 ProcessSP process_sp(GetSP());
627 ret_val = process_sp->GetUniqueID();
628 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
630 log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
631 static_cast<void *>(process_sp.get()), ret_val);
635 ByteOrder SBProcess::GetByteOrder() const {
636 ByteOrder byteOrder = eByteOrderInvalid;
637 ProcessSP process_sp(GetSP());
639 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
641 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
643 log->Printf("SBProcess(%p)::GetByteOrder () => %d",
644 static_cast<void *>(process_sp.get()), byteOrder);
649 uint32_t SBProcess::GetAddressByteSize() const {
651 ProcessSP process_sp(GetSP());
653 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
655 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
657 log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
658 static_cast<void *>(process_sp.get()), size);
663 SBError SBProcess::Continue() {
664 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
667 ProcessSP process_sp(GetSP());
670 log->Printf("SBProcess(%p)::Continue ()...",
671 static_cast<void *>(process_sp.get()));
674 std::lock_guard<std::recursive_mutex> guard(
675 process_sp->GetTarget().GetAPIMutex());
677 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
678 sb_error.ref() = process_sp->Resume();
680 sb_error.ref() = process_sp->ResumeSynchronous(NULL);
682 sb_error.SetErrorString("SBProcess is invalid");
686 sb_error.GetDescription(sstr);
687 log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
688 static_cast<void *>(process_sp.get()),
689 static_cast<void *>(sb_error.get()), sstr.GetData());
695 SBError SBProcess::Destroy() {
697 ProcessSP process_sp(GetSP());
699 std::lock_guard<std::recursive_mutex> guard(
700 process_sp->GetTarget().GetAPIMutex());
701 sb_error.SetError(process_sp->Destroy(false));
703 sb_error.SetErrorString("SBProcess is invalid");
705 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
708 sb_error.GetDescription(sstr);
709 log->Printf("SBProcess(%p)::Destroy () => SBError (%p): %s",
710 static_cast<void *>(process_sp.get()),
711 static_cast<void *>(sb_error.get()), sstr.GetData());
717 SBError SBProcess::Stop() {
719 ProcessSP process_sp(GetSP());
721 std::lock_guard<std::recursive_mutex> guard(
722 process_sp->GetTarget().GetAPIMutex());
723 sb_error.SetError(process_sp->Halt());
725 sb_error.SetErrorString("SBProcess is invalid");
727 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
730 sb_error.GetDescription(sstr);
731 log->Printf("SBProcess(%p)::Stop () => SBError (%p): %s",
732 static_cast<void *>(process_sp.get()),
733 static_cast<void *>(sb_error.get()), sstr.GetData());
739 SBError SBProcess::Kill() {
741 ProcessSP process_sp(GetSP());
743 std::lock_guard<std::recursive_mutex> guard(
744 process_sp->GetTarget().GetAPIMutex());
745 sb_error.SetError(process_sp->Destroy(true));
747 sb_error.SetErrorString("SBProcess is invalid");
749 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
752 sb_error.GetDescription(sstr);
753 log->Printf("SBProcess(%p)::Kill () => SBError (%p): %s",
754 static_cast<void *>(process_sp.get()),
755 static_cast<void *>(sb_error.get()), sstr.GetData());
761 SBError SBProcess::Detach() {
762 // FIXME: This should come from a process default.
763 bool keep_stopped = false;
764 return Detach(keep_stopped);
767 SBError SBProcess::Detach(bool keep_stopped) {
769 ProcessSP process_sp(GetSP());
771 std::lock_guard<std::recursive_mutex> guard(
772 process_sp->GetTarget().GetAPIMutex());
773 sb_error.SetError(process_sp->Detach(keep_stopped));
775 sb_error.SetErrorString("SBProcess is invalid");
780 SBError SBProcess::Signal(int signo) {
782 ProcessSP process_sp(GetSP());
784 std::lock_guard<std::recursive_mutex> guard(
785 process_sp->GetTarget().GetAPIMutex());
786 sb_error.SetError(process_sp->Signal(signo));
788 sb_error.SetErrorString("SBProcess is invalid");
789 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
792 sb_error.GetDescription(sstr);
793 log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
794 static_cast<void *>(process_sp.get()), signo,
795 static_cast<void *>(sb_error.get()), sstr.GetData());
800 SBUnixSignals SBProcess::GetUnixSignals() {
801 if (auto process_sp = GetSP())
802 return SBUnixSignals{process_sp};
807 void SBProcess::SendAsyncInterrupt() {
808 ProcessSP process_sp(GetSP());
810 process_sp->SendAsyncInterrupt();
814 SBThread SBProcess::GetThreadByID(tid_t tid) {
817 ProcessSP process_sp(GetSP());
819 Process::StopLocker stop_locker;
820 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
821 std::lock_guard<std::recursive_mutex> guard(
822 process_sp->GetTarget().GetAPIMutex());
823 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
824 sb_thread.SetThread(thread_sp);
827 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
829 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
830 ") => SBThread (%p)",
831 static_cast<void *>(process_sp.get()), tid,
832 static_cast<void *>(thread_sp.get()));
837 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
840 ProcessSP process_sp(GetSP());
842 Process::StopLocker stop_locker;
843 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
844 std::lock_guard<std::recursive_mutex> guard(
845 process_sp->GetTarget().GetAPIMutex());
847 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
848 sb_thread.SetThread(thread_sp);
851 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
853 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
854 static_cast<void *>(process_sp.get()), index_id,
855 static_cast<void *>(thread_sp.get()));
860 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
861 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
863 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
866 log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
867 static_cast<void *>(event.get()),
868 lldb_private::StateAsCString(ret_val));
873 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
874 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
876 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
879 log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
880 static_cast<void *>(event.get()), ret_val);
885 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
886 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
890 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
892 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
895 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
896 ProcessSP process_sp =
897 Process::ProcessEventData::GetProcessFromEvent(event.get());
899 // StructuredData events also know the process they come from.
901 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
904 return SBProcess(process_sp);
907 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
908 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
911 lldb::SBStructuredData
912 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
913 return SBStructuredData(event.GetSP());
916 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
917 return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
918 !EventIsStructuredDataEvent(event);
921 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
922 EventSP event_sp = event.GetSP();
923 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
924 return event_data && (event_data->GetFlavor() ==
925 EventDataStructuredData::GetFlavorString());
928 SBBroadcaster SBProcess::GetBroadcaster() const {
929 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
931 ProcessSP process_sp(GetSP());
933 SBBroadcaster broadcaster(process_sp.get(), false);
936 log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
937 static_cast<void *>(process_sp.get()),
938 static_cast<void *>(broadcaster.get()));
943 const char *SBProcess::GetBroadcasterClass() {
944 return Process::GetStaticBroadcasterClass().AsCString();
947 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
949 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
951 size_t bytes_read = 0;
953 ProcessSP process_sp(GetSP());
956 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
957 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
958 static_cast<void *>(process_sp.get()), addr,
959 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
960 static_cast<void *>(sb_error.get()));
963 Process::StopLocker stop_locker;
964 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
965 std::lock_guard<std::recursive_mutex> guard(
966 process_sp->GetTarget().GetAPIMutex());
967 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
970 log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
971 static_cast<void *>(process_sp.get()));
972 sb_error.SetErrorString("process is running");
975 sb_error.SetErrorString("SBProcess is invalid");
980 sb_error.GetDescription(sstr);
981 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
982 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
983 static_cast<void *>(process_sp.get()), addr,
984 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
985 static_cast<void *>(sb_error.get()), sstr.GetData(),
986 static_cast<uint64_t>(bytes_read));
992 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
993 lldb::SBError &sb_error) {
994 size_t bytes_read = 0;
995 ProcessSP process_sp(GetSP());
997 Process::StopLocker stop_locker;
998 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
999 std::lock_guard<std::recursive_mutex> guard(
1000 process_sp->GetTarget().GetAPIMutex());
1001 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
1004 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1006 log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
1008 static_cast<void *>(process_sp.get()));
1009 sb_error.SetErrorString("process is running");
1012 sb_error.SetErrorString("SBProcess is invalid");
1017 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
1018 lldb::SBError &sb_error) {
1020 ProcessSP process_sp(GetSP());
1022 Process::StopLocker stop_locker;
1023 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1024 std::lock_guard<std::recursive_mutex> guard(
1025 process_sp->GetTarget().GetAPIMutex());
1026 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1029 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1031 log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1033 static_cast<void *>(process_sp.get()));
1034 sb_error.SetErrorString("process is running");
1037 sb_error.SetErrorString("SBProcess is invalid");
1042 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1043 lldb::SBError &sb_error) {
1044 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1045 ProcessSP process_sp(GetSP());
1047 Process::StopLocker stop_locker;
1048 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1049 std::lock_guard<std::recursive_mutex> guard(
1050 process_sp->GetTarget().GetAPIMutex());
1051 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1053 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1055 log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1057 static_cast<void *>(process_sp.get()));
1058 sb_error.SetErrorString("process is running");
1061 sb_error.SetErrorString("SBProcess is invalid");
1066 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1067 SBError &sb_error) {
1068 size_t bytes_written = 0;
1070 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1072 ProcessSP process_sp(GetSP());
1075 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1076 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1077 static_cast<void *>(process_sp.get()), addr,
1078 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1079 static_cast<void *>(sb_error.get()));
1082 Process::StopLocker stop_locker;
1083 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1084 std::lock_guard<std::recursive_mutex> guard(
1085 process_sp->GetTarget().GetAPIMutex());
1087 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1090 log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1091 static_cast<void *>(process_sp.get()));
1092 sb_error.SetErrorString("process is running");
1098 sb_error.GetDescription(sstr);
1099 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1100 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1101 static_cast<void *>(process_sp.get()), addr,
1102 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1103 static_cast<void *>(sb_error.get()), sstr.GetData(),
1104 static_cast<uint64_t>(bytes_written));
1107 return bytes_written;
1110 bool SBProcess::GetDescription(SBStream &description) {
1111 Stream &strm = description.ref();
1113 ProcessSP process_sp(GetSP());
1115 char path[PATH_MAX];
1116 GetTarget().GetExecutable().GetPath(path, sizeof(path));
1117 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1118 const char *exe_name = NULL;
1120 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1122 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1123 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1124 GetNumThreads(), exe_name ? ", executable = " : "",
1125 exe_name ? exe_name : "");
1127 strm.PutCString("No value");
1133 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1134 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1137 ProcessSP process_sp(GetSP());
1139 std::lock_guard<std::recursive_mutex> guard(
1140 process_sp->GetTarget().GetAPIMutex());
1141 sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1143 log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1144 static_cast<void *>(process_sp.get()), num);
1146 sb_error.SetErrorString("SBProcess is invalid");
1151 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1152 lldb::SBError &sb_error) {
1153 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1156 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1157 const lldb::SBFileSpec &sb_remote_image_spec,
1158 lldb::SBError &sb_error) {
1159 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1160 ProcessSP process_sp(GetSP());
1162 Process::StopLocker stop_locker;
1163 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1165 log->Printf("SBProcess(%p)::LoadImage() => calling Platform::LoadImage"
1167 static_cast<void *>(process_sp.get()),
1168 sb_local_image_spec.GetFilename());
1170 std::lock_guard<std::recursive_mutex> guard(
1171 process_sp->GetTarget().GetAPIMutex());
1172 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1173 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1174 *sb_remote_image_spec, sb_error.ref());
1177 log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1178 static_cast<void *>(process_sp.get()));
1179 sb_error.SetErrorString("process is running");
1183 log->Printf("SBProcess(%p)::LoadImage() => error: called with invalid"
1185 static_cast<void *>(process_sp.get()));
1186 sb_error.SetErrorString("process is invalid");
1188 return LLDB_INVALID_IMAGE_TOKEN;
1191 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1192 lldb::SBError sb_error;
1193 ProcessSP process_sp(GetSP());
1195 Process::StopLocker stop_locker;
1196 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1197 std::lock_guard<std::recursive_mutex> guard(
1198 process_sp->GetTarget().GetAPIMutex());
1199 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1201 platform_sp->UnloadImage(process_sp.get(), image_token));
1203 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1205 log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1206 static_cast<void *>(process_sp.get()));
1207 sb_error.SetErrorString("process is running");
1210 sb_error.SetErrorString("invalid process");
1214 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1215 lldb::SBError sb_error;
1216 ProcessSP process_sp(GetSP());
1218 Process::StopLocker stop_locker;
1219 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1220 std::lock_guard<std::recursive_mutex> guard(
1221 process_sp->GetTarget().GetAPIMutex());
1222 sb_error.SetError(process_sp->SendEventData(event_data));
1224 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1227 "SBProcess(%p)::SendEventData() => error: process is running",
1228 static_cast<void *>(process_sp.get()));
1229 sb_error.SetErrorString("process is running");
1232 sb_error.SetErrorString("invalid process");
1236 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1237 ProcessSP process_sp(GetSP());
1238 if (process_sp && process_sp->GetSystemRuntime()) {
1239 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1240 return runtime->GetExtendedBacktraceTypes().size();
1245 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1246 ProcessSP process_sp(GetSP());
1247 if (process_sp && process_sp->GetSystemRuntime()) {
1248 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1249 const std::vector<ConstString> &names =
1250 runtime->GetExtendedBacktraceTypes();
1251 if (idx < names.size()) {
1252 return names[idx].AsCString();
1254 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1256 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1257 "error: requested extended backtrace name out of bounds",
1258 static_cast<void *>(process_sp.get()));
1264 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1265 ProcessSP process_sp(GetSP());
1266 SBThreadCollection threads;
1268 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1273 bool SBProcess::IsInstrumentationRuntimePresent(
1274 InstrumentationRuntimeType type) {
1275 ProcessSP process_sp(GetSP());
1279 InstrumentationRuntimeSP runtime_sp =
1280 process_sp->GetInstrumentationRuntime(type);
1282 if (!runtime_sp.get())
1285 return runtime_sp->IsActive();
1288 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1289 lldb::SBError error;
1290 ProcessSP process_sp(GetSP());
1292 error.SetErrorString("SBProcess is invalid");
1296 std::lock_guard<std::recursive_mutex> guard(
1297 process_sp->GetTarget().GetAPIMutex());
1299 if (process_sp->GetState() != eStateStopped) {
1300 error.SetErrorString("the process is not stopped");
1304 FileSpec core_file(file_name, false);
1305 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1310 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1311 SBMemoryRegionInfo &sb_region_info) {
1312 lldb::SBError sb_error;
1313 ProcessSP process_sp(GetSP());
1314 MemoryRegionInfoSP region_info_sp =
1315 std::make_shared<lldb_private::MemoryRegionInfo>();
1317 Process::StopLocker stop_locker;
1318 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1319 std::lock_guard<std::recursive_mutex> guard(
1320 process_sp->GetTarget().GetAPIMutex());
1322 process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1323 if (sb_error.Success()) {
1324 sb_region_info.ref() = *region_info_sp;
1327 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1330 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1331 static_cast<void *>(process_sp.get()));
1332 sb_error.SetErrorString("process is running");
1335 sb_error.SetErrorString("SBProcess is invalid");
1340 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1341 lldb::SBError sb_error;
1342 lldb::SBMemoryRegionInfoList sb_region_list;
1343 ProcessSP process_sp(GetSP());
1345 Process::StopLocker stop_locker;
1346 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1347 std::lock_guard<std::recursive_mutex> guard(
1348 process_sp->GetTarget().GetAPIMutex());
1349 std::vector<MemoryRegionInfoSP> region_list;
1350 sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1351 if (sb_error.Success()) {
1352 std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1353 for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1355 SBMemoryRegionInfo sb_region_info(it->get());
1356 sb_region_list.Append(sb_region_info);
1360 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1363 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1364 static_cast<void *>(process_sp.get()));
1365 sb_error.SetErrorString("process is running");
1368 sb_error.SetErrorString("SBProcess is invalid");
1370 return sb_region_list;
1373 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1374 lldb::SBProcessInfo sb_proc_info;
1375 ProcessSP process_sp(GetSP());
1376 ProcessInstanceInfo proc_info;
1377 if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1378 sb_proc_info.SetProcessInfo(proc_info);
1380 return sb_proc_info;