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/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Core/State.h"
23 #include "lldb/Core/Stream.h"
24 #include "lldb/Core/StreamFile.h"
25 #include "lldb/Interpreter/Args.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/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/SBUnixSignals.h"
50 using namespace lldb_private;
52 SBProcess::SBProcess() : m_opaque_wp() {}
54 //----------------------------------------------------------------------
55 // SBProcess constructor
56 //----------------------------------------------------------------------
58 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
60 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
61 : m_opaque_wp(process_sp) {}
63 const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
65 m_opaque_wp = rhs.m_opaque_wp;
69 //----------------------------------------------------------------------
71 //----------------------------------------------------------------------
72 SBProcess::~SBProcess() {}
74 const char *SBProcess::GetBroadcasterClassName() {
75 return Process::GetStaticBroadcasterClass().AsCString();
78 const char *SBProcess::GetPluginName() {
79 ProcessSP process_sp(GetSP());
81 return process_sp->GetPluginName().GetCString();
86 const char *SBProcess::GetShortPluginName() {
87 ProcessSP process_sp(GetSP());
89 return process_sp->GetPluginName().GetCString();
94 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
96 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
98 void SBProcess::Clear() { m_opaque_wp.reset(); }
100 bool SBProcess::IsValid() const {
101 ProcessSP process_sp(m_opaque_wp.lock());
102 return ((bool)process_sp && process_sp->IsValid());
105 bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
106 const char *stdin_path, const char *stdout_path,
107 const char *stderr_path,
108 const char *working_directory,
109 uint32_t launch_flags, bool stop_at_entry,
110 lldb::SBError &error) {
111 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
113 log->Printf("SBProcess(%p)::RemoteLaunch (argv=%p, envp=%p, stdin=%s, "
114 "stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, "
115 "stop_at_entry=%i, &error (%p))...",
116 static_cast<void *>(m_opaque_wp.lock().get()),
117 static_cast<void *>(argv), static_cast<void *>(envp),
118 stdin_path ? stdin_path : "NULL",
119 stdout_path ? stdout_path : "NULL",
120 stderr_path ? stderr_path : "NULL",
121 working_directory ? working_directory : "NULL", launch_flags,
122 stop_at_entry, static_cast<void *>(error.get()));
124 ProcessSP process_sp(GetSP());
126 std::lock_guard<std::recursive_mutex> guard(
127 process_sp->GetTarget().GetAPIMutex());
128 if (process_sp->GetState() == eStateConnected) {
130 launch_flags |= eLaunchFlagStopAtEntry;
131 ProcessLaunchInfo launch_info(
132 FileSpec{stdin_path, false}, FileSpec{stdout_path, false},
133 FileSpec{stderr_path, false}, FileSpec{working_directory, false},
135 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
137 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
139 launch_info.GetArguments().AppendArguments(argv);
141 launch_info.GetEnvironmentEntries().SetArguments(envp);
142 error.SetError(process_sp->Launch(launch_info));
144 error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
147 error.SetErrorString("unable to attach pid");
152 error.GetDescription(sstr);
153 log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
154 static_cast<void *>(process_sp.get()),
155 static_cast<void *>(error.get()), sstr.GetData());
158 return error.Success();
161 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
162 lldb::SBError &error) {
163 ProcessSP process_sp(GetSP());
165 std::lock_guard<std::recursive_mutex> guard(
166 process_sp->GetTarget().GetAPIMutex());
167 if (process_sp->GetState() == eStateConnected) {
168 ProcessAttachInfo attach_info;
169 attach_info.SetProcessID(pid);
170 error.SetError(process_sp->Attach(attach_info));
172 error.SetErrorString(
173 "must be in eStateConnected to call RemoteAttachToProcessWithID");
176 error.SetErrorString("unable to attach pid");
179 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
182 error.GetDescription(sstr);
183 log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
184 ") => SBError (%p): %s",
185 static_cast<void *>(process_sp.get()), pid,
186 static_cast<void *>(error.get()), sstr.GetData());
189 return error.Success();
192 uint32_t SBProcess::GetNumThreads() {
193 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
195 uint32_t num_threads = 0;
196 ProcessSP process_sp(GetSP());
198 Process::StopLocker stop_locker;
200 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
201 std::lock_guard<std::recursive_mutex> guard(
202 process_sp->GetTarget().GetAPIMutex());
203 num_threads = process_sp->GetThreadList().GetSize(can_update);
207 log->Printf("SBProcess(%p)::GetNumThreads () => %d",
208 static_cast<void *>(process_sp.get()), num_threads);
213 SBThread SBProcess::GetSelectedThread() const {
214 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
218 ProcessSP process_sp(GetSP());
220 std::lock_guard<std::recursive_mutex> guard(
221 process_sp->GetTarget().GetAPIMutex());
222 thread_sp = process_sp->GetThreadList().GetSelectedThread();
223 sb_thread.SetThread(thread_sp);
227 log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
228 static_cast<void *>(process_sp.get()),
229 static_cast<void *>(thread_sp.get()));
234 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
235 lldb::addr_t context) {
236 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
240 ProcessSP process_sp(GetSP());
242 std::lock_guard<std::recursive_mutex> guard(
243 process_sp->GetTarget().GetAPIMutex());
244 thread_sp = process_sp->CreateOSPluginThread(tid, context);
245 sb_thread.SetThread(thread_sp);
249 log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
250 ", context=0x%" PRIx64 ") => SBThread(%p)",
251 static_cast<void *>(process_sp.get()), tid, context,
252 static_cast<void *>(thread_sp.get()));
257 SBTarget SBProcess::GetTarget() const {
258 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
262 ProcessSP process_sp(GetSP());
264 target_sp = process_sp->GetTarget().shared_from_this();
265 sb_target.SetSP(target_sp);
269 log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
270 static_cast<void *>(process_sp.get()),
271 static_cast<void *>(target_sp.get()));
276 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
277 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
280 ProcessSP process_sp(GetSP());
283 ret_val = process_sp->PutSTDIN(src, src_len, error);
287 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
289 static_cast<void *>(process_sp.get()), src,
290 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
295 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
296 size_t bytes_read = 0;
297 ProcessSP process_sp(GetSP());
300 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
303 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
306 "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
308 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
309 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
314 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
315 size_t bytes_read = 0;
316 ProcessSP process_sp(GetSP());
319 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
322 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
325 "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
327 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
328 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
333 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
334 size_t bytes_read = 0;
335 ProcessSP process_sp(GetSP());
338 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
341 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
344 "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
346 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
347 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
352 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
356 ProcessSP process_sp(GetSP());
358 const StateType event_state = SBProcess::GetStateFromEvent(event);
360 int message_len = ::snprintf(
361 message, sizeof(message), "Process %" PRIu64 " %s\n",
362 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
365 ::fwrite(message, 1, message_len, out);
369 void SBProcess::AppendEventStateReport(const SBEvent &event,
370 SBCommandReturnObject &result) {
371 ProcessSP process_sp(GetSP());
373 const StateType event_state = SBProcess::GetStateFromEvent(event);
375 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
376 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
378 result.AppendMessage(message);
382 bool SBProcess::SetSelectedThread(const SBThread &thread) {
383 ProcessSP process_sp(GetSP());
385 std::lock_guard<std::recursive_mutex> guard(
386 process_sp->GetTarget().GetAPIMutex());
387 return process_sp->GetThreadList().SetSelectedThreadByID(
388 thread.GetThreadID());
393 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
394 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
396 bool ret_val = false;
397 ProcessSP process_sp(GetSP());
399 std::lock_guard<std::recursive_mutex> guard(
400 process_sp->GetTarget().GetAPIMutex());
401 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
405 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
407 static_cast<void *>(process_sp.get()), tid,
408 (ret_val ? "true" : "false"));
413 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
414 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
416 bool ret_val = false;
417 ProcessSP process_sp(GetSP());
419 std::lock_guard<std::recursive_mutex> guard(
420 process_sp->GetTarget().GetAPIMutex());
421 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
425 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
426 static_cast<void *>(process_sp.get()), index_id,
427 (ret_val ? "true" : "false"));
432 SBThread SBProcess::GetThreadAtIndex(size_t index) {
433 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
437 ProcessSP process_sp(GetSP());
439 Process::StopLocker stop_locker;
440 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
441 std::lock_guard<std::recursive_mutex> guard(
442 process_sp->GetTarget().GetAPIMutex());
443 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
444 sb_thread.SetThread(thread_sp);
448 log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
449 static_cast<void *>(process_sp.get()),
450 static_cast<uint32_t>(index),
451 static_cast<void *>(thread_sp.get()));
456 uint32_t SBProcess::GetNumQueues() {
457 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
459 uint32_t num_queues = 0;
460 ProcessSP process_sp(GetSP());
462 Process::StopLocker stop_locker;
463 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
464 std::lock_guard<std::recursive_mutex> guard(
465 process_sp->GetTarget().GetAPIMutex());
466 num_queues = process_sp->GetQueueList().GetSize();
471 log->Printf("SBProcess(%p)::GetNumQueues () => %d",
472 static_cast<void *>(process_sp.get()), num_queues);
477 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
478 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
482 ProcessSP process_sp(GetSP());
484 Process::StopLocker stop_locker;
485 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
486 std::lock_guard<std::recursive_mutex> guard(
487 process_sp->GetTarget().GetAPIMutex());
488 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
489 sb_queue.SetQueue(queue_sp);
494 log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
495 static_cast<void *>(process_sp.get()),
496 static_cast<uint32_t>(index),
497 static_cast<void *>(queue_sp.get()));
502 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
503 ProcessSP process_sp(GetSP());
505 std::lock_guard<std::recursive_mutex> guard(
506 process_sp->GetTarget().GetAPIMutex());
507 if (include_expression_stops)
508 return process_sp->GetStopID();
510 return process_sp->GetLastNaturalStopID();
515 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
516 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
520 ProcessSP process_sp(GetSP());
522 std::lock_guard<std::recursive_mutex> guard(
523 process_sp->GetTarget().GetAPIMutex());
524 event_sp = process_sp->GetStopEventForStopID(stop_id);
525 sb_event.reset(event_sp);
529 log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
531 static_cast<void *>(process_sp.get()), stop_id,
532 static_cast<void *>(event_sp.get()));
537 StateType SBProcess::GetState() {
539 StateType ret_val = eStateInvalid;
540 ProcessSP process_sp(GetSP());
542 std::lock_guard<std::recursive_mutex> guard(
543 process_sp->GetTarget().GetAPIMutex());
544 ret_val = process_sp->GetState();
547 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
549 log->Printf("SBProcess(%p)::GetState () => %s",
550 static_cast<void *>(process_sp.get()),
551 lldb_private::StateAsCString(ret_val));
556 int SBProcess::GetExitStatus() {
558 ProcessSP process_sp(GetSP());
560 std::lock_guard<std::recursive_mutex> guard(
561 process_sp->GetTarget().GetAPIMutex());
562 exit_status = process_sp->GetExitStatus();
564 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
566 log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
567 static_cast<void *>(process_sp.get()), exit_status,
573 const char *SBProcess::GetExitDescription() {
574 const char *exit_desc = NULL;
575 ProcessSP process_sp(GetSP());
577 std::lock_guard<std::recursive_mutex> guard(
578 process_sp->GetTarget().GetAPIMutex());
579 exit_desc = process_sp->GetExitDescription();
581 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
583 log->Printf("SBProcess(%p)::GetExitDescription () => %s",
584 static_cast<void *>(process_sp.get()), exit_desc);
588 lldb::pid_t SBProcess::GetProcessID() {
589 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
590 ProcessSP process_sp(GetSP());
592 ret_val = process_sp->GetID();
594 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
596 log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
597 static_cast<void *>(process_sp.get()), ret_val);
602 uint32_t SBProcess::GetUniqueID() {
603 uint32_t ret_val = 0;
604 ProcessSP process_sp(GetSP());
606 ret_val = process_sp->GetUniqueID();
607 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
609 log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
610 static_cast<void *>(process_sp.get()), ret_val);
614 ByteOrder SBProcess::GetByteOrder() const {
615 ByteOrder byteOrder = eByteOrderInvalid;
616 ProcessSP process_sp(GetSP());
618 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
620 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
622 log->Printf("SBProcess(%p)::GetByteOrder () => %d",
623 static_cast<void *>(process_sp.get()), byteOrder);
628 uint32_t SBProcess::GetAddressByteSize() const {
630 ProcessSP process_sp(GetSP());
632 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
634 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
636 log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
637 static_cast<void *>(process_sp.get()), size);
642 SBError SBProcess::Continue() {
643 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
646 ProcessSP process_sp(GetSP());
649 log->Printf("SBProcess(%p)::Continue ()...",
650 static_cast<void *>(process_sp.get()));
653 std::lock_guard<std::recursive_mutex> guard(
654 process_sp->GetTarget().GetAPIMutex());
656 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
657 sb_error.ref() = process_sp->Resume();
659 sb_error.ref() = process_sp->ResumeSynchronous(NULL);
661 sb_error.SetErrorString("SBProcess is invalid");
665 sb_error.GetDescription(sstr);
666 log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
667 static_cast<void *>(process_sp.get()),
668 static_cast<void *>(sb_error.get()), sstr.GetData());
674 SBError SBProcess::Destroy() {
676 ProcessSP process_sp(GetSP());
678 std::lock_guard<std::recursive_mutex> guard(
679 process_sp->GetTarget().GetAPIMutex());
680 sb_error.SetError(process_sp->Destroy(false));
682 sb_error.SetErrorString("SBProcess is invalid");
684 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
687 sb_error.GetDescription(sstr);
688 log->Printf("SBProcess(%p)::Destroy () => SBError (%p): %s",
689 static_cast<void *>(process_sp.get()),
690 static_cast<void *>(sb_error.get()), sstr.GetData());
696 SBError SBProcess::Stop() {
698 ProcessSP process_sp(GetSP());
700 std::lock_guard<std::recursive_mutex> guard(
701 process_sp->GetTarget().GetAPIMutex());
702 sb_error.SetError(process_sp->Halt());
704 sb_error.SetErrorString("SBProcess is invalid");
706 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
709 sb_error.GetDescription(sstr);
710 log->Printf("SBProcess(%p)::Stop () => SBError (%p): %s",
711 static_cast<void *>(process_sp.get()),
712 static_cast<void *>(sb_error.get()), sstr.GetData());
718 SBError SBProcess::Kill() {
720 ProcessSP process_sp(GetSP());
722 std::lock_guard<std::recursive_mutex> guard(
723 process_sp->GetTarget().GetAPIMutex());
724 sb_error.SetError(process_sp->Destroy(true));
726 sb_error.SetErrorString("SBProcess is invalid");
728 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
731 sb_error.GetDescription(sstr);
732 log->Printf("SBProcess(%p)::Kill () => SBError (%p): %s",
733 static_cast<void *>(process_sp.get()),
734 static_cast<void *>(sb_error.get()), sstr.GetData());
740 SBError SBProcess::Detach() {
741 // FIXME: This should come from a process default.
742 bool keep_stopped = false;
743 return Detach(keep_stopped);
746 SBError SBProcess::Detach(bool keep_stopped) {
748 ProcessSP process_sp(GetSP());
750 std::lock_guard<std::recursive_mutex> guard(
751 process_sp->GetTarget().GetAPIMutex());
752 sb_error.SetError(process_sp->Detach(keep_stopped));
754 sb_error.SetErrorString("SBProcess is invalid");
759 SBError SBProcess::Signal(int signo) {
761 ProcessSP process_sp(GetSP());
763 std::lock_guard<std::recursive_mutex> guard(
764 process_sp->GetTarget().GetAPIMutex());
765 sb_error.SetError(process_sp->Signal(signo));
767 sb_error.SetErrorString("SBProcess is invalid");
768 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
771 sb_error.GetDescription(sstr);
772 log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
773 static_cast<void *>(process_sp.get()), signo,
774 static_cast<void *>(sb_error.get()), sstr.GetData());
779 SBUnixSignals SBProcess::GetUnixSignals() {
780 if (auto process_sp = GetSP())
781 return SBUnixSignals{process_sp};
786 void SBProcess::SendAsyncInterrupt() {
787 ProcessSP process_sp(GetSP());
789 process_sp->SendAsyncInterrupt();
793 SBThread SBProcess::GetThreadByID(tid_t tid) {
796 ProcessSP process_sp(GetSP());
798 Process::StopLocker stop_locker;
799 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
800 std::lock_guard<std::recursive_mutex> guard(
801 process_sp->GetTarget().GetAPIMutex());
802 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
803 sb_thread.SetThread(thread_sp);
806 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
808 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
809 ") => SBThread (%p)",
810 static_cast<void *>(process_sp.get()), tid,
811 static_cast<void *>(thread_sp.get()));
816 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
819 ProcessSP process_sp(GetSP());
821 Process::StopLocker stop_locker;
822 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
823 std::lock_guard<std::recursive_mutex> guard(
824 process_sp->GetTarget().GetAPIMutex());
826 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
827 sb_thread.SetThread(thread_sp);
830 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
832 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
833 static_cast<void *>(process_sp.get()), index_id,
834 static_cast<void *>(thread_sp.get()));
839 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
840 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
842 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
845 log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
846 static_cast<void *>(event.get()),
847 lldb_private::StateAsCString(ret_val));
852 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
853 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
855 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
858 log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
859 static_cast<void *>(event.get()), ret_val);
864 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
865 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
869 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
871 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
874 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
875 ProcessSP process_sp =
876 Process::ProcessEventData::GetProcessFromEvent(event.get());
878 // StructuredData events also know the process they come from.
880 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
883 return SBProcess(process_sp);
886 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
887 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
890 lldb::SBStructuredData
891 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
892 return SBStructuredData(event.GetSP());
895 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
896 return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
897 !EventIsStructuredDataEvent(event);
900 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
901 EventSP event_sp = event.GetSP();
902 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
903 return event_data && (event_data->GetFlavor() ==
904 EventDataStructuredData::GetFlavorString());
907 SBBroadcaster SBProcess::GetBroadcaster() const {
908 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
910 ProcessSP process_sp(GetSP());
912 SBBroadcaster broadcaster(process_sp.get(), false);
915 log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
916 static_cast<void *>(process_sp.get()),
917 static_cast<void *>(broadcaster.get()));
922 const char *SBProcess::GetBroadcasterClass() {
923 return Process::GetStaticBroadcasterClass().AsCString();
926 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
928 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
930 size_t bytes_read = 0;
932 ProcessSP process_sp(GetSP());
935 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
936 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
937 static_cast<void *>(process_sp.get()), addr,
938 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
939 static_cast<void *>(sb_error.get()));
942 Process::StopLocker stop_locker;
943 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
944 std::lock_guard<std::recursive_mutex> guard(
945 process_sp->GetTarget().GetAPIMutex());
946 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
949 log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
950 static_cast<void *>(process_sp.get()));
951 sb_error.SetErrorString("process is running");
954 sb_error.SetErrorString("SBProcess is invalid");
959 sb_error.GetDescription(sstr);
960 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
961 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
962 static_cast<void *>(process_sp.get()), addr,
963 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
964 static_cast<void *>(sb_error.get()), sstr.GetData(),
965 static_cast<uint64_t>(bytes_read));
971 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
972 lldb::SBError &sb_error) {
973 size_t bytes_read = 0;
974 ProcessSP process_sp(GetSP());
976 Process::StopLocker stop_locker;
977 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
978 std::lock_guard<std::recursive_mutex> guard(
979 process_sp->GetTarget().GetAPIMutex());
980 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
983 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
985 log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
987 static_cast<void *>(process_sp.get()));
988 sb_error.SetErrorString("process is running");
991 sb_error.SetErrorString("SBProcess is invalid");
996 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
997 lldb::SBError &sb_error) {
999 ProcessSP process_sp(GetSP());
1001 Process::StopLocker stop_locker;
1002 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1003 std::lock_guard<std::recursive_mutex> guard(
1004 process_sp->GetTarget().GetAPIMutex());
1005 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1008 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1010 log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1012 static_cast<void *>(process_sp.get()));
1013 sb_error.SetErrorString("process is running");
1016 sb_error.SetErrorString("SBProcess is invalid");
1021 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1022 lldb::SBError &sb_error) {
1023 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1024 ProcessSP process_sp(GetSP());
1026 Process::StopLocker stop_locker;
1027 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1028 std::lock_guard<std::recursive_mutex> guard(
1029 process_sp->GetTarget().GetAPIMutex());
1030 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1032 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1034 log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1036 static_cast<void *>(process_sp.get()));
1037 sb_error.SetErrorString("process is running");
1040 sb_error.SetErrorString("SBProcess is invalid");
1045 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1046 SBError &sb_error) {
1047 size_t bytes_written = 0;
1049 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1051 ProcessSP process_sp(GetSP());
1054 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1055 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1056 static_cast<void *>(process_sp.get()), addr,
1057 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1058 static_cast<void *>(sb_error.get()));
1061 Process::StopLocker stop_locker;
1062 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1063 std::lock_guard<std::recursive_mutex> guard(
1064 process_sp->GetTarget().GetAPIMutex());
1066 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1069 log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1070 static_cast<void *>(process_sp.get()));
1071 sb_error.SetErrorString("process is running");
1077 sb_error.GetDescription(sstr);
1078 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1079 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1080 static_cast<void *>(process_sp.get()), addr,
1081 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1082 static_cast<void *>(sb_error.get()), sstr.GetData(),
1083 static_cast<uint64_t>(bytes_written));
1086 return bytes_written;
1089 bool SBProcess::GetDescription(SBStream &description) {
1090 Stream &strm = description.ref();
1092 ProcessSP process_sp(GetSP());
1094 char path[PATH_MAX];
1095 GetTarget().GetExecutable().GetPath(path, sizeof(path));
1096 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1097 const char *exe_name = NULL;
1099 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1101 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1102 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1103 GetNumThreads(), exe_name ? ", executable = " : "",
1104 exe_name ? exe_name : "");
1106 strm.PutCString("No value");
1112 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1116 ProcessSP process_sp(GetSP());
1118 std::lock_guard<std::recursive_mutex> guard(
1119 process_sp->GetTarget().GetAPIMutex());
1120 sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1122 log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1123 static_cast<void *>(process_sp.get()), num);
1125 sb_error.SetErrorString("SBProcess is invalid");
1130 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1131 lldb::SBError &sb_error) {
1132 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1135 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1136 const lldb::SBFileSpec &sb_remote_image_spec,
1137 lldb::SBError &sb_error) {
1138 ProcessSP process_sp(GetSP());
1140 Process::StopLocker stop_locker;
1141 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1142 std::lock_guard<std::recursive_mutex> guard(
1143 process_sp->GetTarget().GetAPIMutex());
1144 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1145 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1146 *sb_remote_image_spec, sb_error.ref());
1148 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1150 log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1151 static_cast<void *>(process_sp.get()));
1152 sb_error.SetErrorString("process is running");
1155 return LLDB_INVALID_IMAGE_TOKEN;
1158 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1159 lldb::SBError sb_error;
1160 ProcessSP process_sp(GetSP());
1162 Process::StopLocker stop_locker;
1163 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1164 std::lock_guard<std::recursive_mutex> guard(
1165 process_sp->GetTarget().GetAPIMutex());
1166 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1168 platform_sp->UnloadImage(process_sp.get(), image_token));
1170 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1172 log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1173 static_cast<void *>(process_sp.get()));
1174 sb_error.SetErrorString("process is running");
1177 sb_error.SetErrorString("invalid process");
1181 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1182 lldb::SBError sb_error;
1183 ProcessSP process_sp(GetSP());
1185 Process::StopLocker stop_locker;
1186 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1187 std::lock_guard<std::recursive_mutex> guard(
1188 process_sp->GetTarget().GetAPIMutex());
1189 sb_error.SetError(process_sp->SendEventData(event_data));
1191 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1194 "SBProcess(%p)::SendEventData() => error: process is running",
1195 static_cast<void *>(process_sp.get()));
1196 sb_error.SetErrorString("process is running");
1199 sb_error.SetErrorString("invalid process");
1203 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1204 ProcessSP process_sp(GetSP());
1205 if (process_sp && process_sp->GetSystemRuntime()) {
1206 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1207 return runtime->GetExtendedBacktraceTypes().size();
1212 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1213 ProcessSP process_sp(GetSP());
1214 if (process_sp && process_sp->GetSystemRuntime()) {
1215 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1216 const std::vector<ConstString> &names =
1217 runtime->GetExtendedBacktraceTypes();
1218 if (idx < names.size()) {
1219 return names[idx].AsCString();
1221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1223 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1224 "error: requested extended backtrace name out of bounds",
1225 static_cast<void *>(process_sp.get()));
1231 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1232 ProcessSP process_sp(GetSP());
1233 SBThreadCollection threads;
1235 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1240 bool SBProcess::IsInstrumentationRuntimePresent(
1241 InstrumentationRuntimeType type) {
1242 ProcessSP process_sp(GetSP());
1246 InstrumentationRuntimeSP runtime_sp =
1247 process_sp->GetInstrumentationRuntime(type);
1249 if (!runtime_sp.get())
1252 return runtime_sp->IsActive();
1255 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1256 lldb::SBError error;
1257 ProcessSP process_sp(GetSP());
1259 error.SetErrorString("SBProcess is invalid");
1263 std::lock_guard<std::recursive_mutex> guard(
1264 process_sp->GetTarget().GetAPIMutex());
1266 if (process_sp->GetState() != eStateStopped) {
1267 error.SetErrorString("the process is not stopped");
1271 FileSpec core_file(file_name, false);
1272 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1277 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1278 SBMemoryRegionInfo &sb_region_info) {
1279 lldb::SBError sb_error;
1280 ProcessSP process_sp(GetSP());
1281 MemoryRegionInfoSP region_info_sp =
1282 std::make_shared<lldb_private::MemoryRegionInfo>();
1284 Process::StopLocker stop_locker;
1285 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1286 std::lock_guard<std::recursive_mutex> guard(
1287 process_sp->GetTarget().GetAPIMutex());
1289 process_sp->GetMemoryRegionInfo(load_addr, *region_info_sp);
1290 if (sb_error.Success()) {
1291 sb_region_info.ref() = *region_info_sp;
1294 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1297 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1298 static_cast<void *>(process_sp.get()));
1299 sb_error.SetErrorString("process is running");
1302 sb_error.SetErrorString("SBProcess is invalid");
1307 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1308 lldb::SBError sb_error;
1309 lldb::SBMemoryRegionInfoList sb_region_list;
1310 ProcessSP process_sp(GetSP());
1312 Process::StopLocker stop_locker;
1313 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1314 std::lock_guard<std::recursive_mutex> guard(
1315 process_sp->GetTarget().GetAPIMutex());
1316 std::vector<MemoryRegionInfoSP> region_list;
1317 sb_error.ref() = process_sp->GetMemoryRegions(region_list);
1318 if (sb_error.Success()) {
1319 std::vector<MemoryRegionInfoSP>::iterator end = region_list.end();
1320 for (std::vector<MemoryRegionInfoSP>::iterator it = region_list.begin();
1322 SBMemoryRegionInfo sb_region_info(it->get());
1323 sb_region_list.Append(sb_region_info);
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");
1337 return sb_region_list;