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"
14 #include "lldb/lldb-defines.h"
15 #include "lldb/lldb-types.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Target/MemoryRegionInfo.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/SystemRuntime.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Utility/Args.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/State.h"
30 #include "lldb/Utility/Stream.h"
33 #include "lldb/API/SBBroadcaster.h"
34 #include "lldb/API/SBCommandReturnObject.h"
35 #include "lldb/API/SBDebugger.h"
36 #include "lldb/API/SBEvent.h"
37 #include "lldb/API/SBFileSpec.h"
38 #include "lldb/API/SBMemoryRegionInfo.h"
39 #include "lldb/API/SBMemoryRegionInfoList.h"
40 #include "lldb/API/SBStream.h"
41 #include "lldb/API/SBStringList.h"
42 #include "lldb/API/SBStructuredData.h"
43 #include "lldb/API/SBThread.h"
44 #include "lldb/API/SBThreadCollection.h"
45 #include "lldb/API/SBTrace.h"
46 #include "lldb/API/SBTraceOptions.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(FileSpec(stdin_path), FileSpec(stdout_path),
132 FileSpec(stderr_path),
133 FileSpec(working_directory), launch_flags);
134 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
136 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
138 launch_info.GetArguments().AppendArguments(argv);
140 launch_info.GetEnvironment() = Environment(envp);
141 error.SetError(process_sp->Launch(launch_info));
143 error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
146 error.SetErrorString("unable to attach pid");
151 error.GetDescription(sstr);
152 log->Printf("SBProcess(%p)::RemoteLaunch (...) => SBError (%p): %s",
153 static_cast<void *>(process_sp.get()),
154 static_cast<void *>(error.get()), sstr.GetData());
157 return error.Success();
160 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
161 lldb::SBError &error) {
162 ProcessSP process_sp(GetSP());
164 std::lock_guard<std::recursive_mutex> guard(
165 process_sp->GetTarget().GetAPIMutex());
166 if (process_sp->GetState() == eStateConnected) {
167 ProcessAttachInfo attach_info;
168 attach_info.SetProcessID(pid);
169 error.SetError(process_sp->Attach(attach_info));
171 error.SetErrorString(
172 "must be in eStateConnected to call RemoteAttachToProcessWithID");
175 error.SetErrorString("unable to attach pid");
178 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
181 error.GetDescription(sstr);
182 log->Printf("SBProcess(%p)::RemoteAttachToProcessWithID (%" PRIu64
183 ") => SBError (%p): %s",
184 static_cast<void *>(process_sp.get()), pid,
185 static_cast<void *>(error.get()), sstr.GetData());
188 return error.Success();
191 uint32_t SBProcess::GetNumThreads() {
192 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
194 uint32_t num_threads = 0;
195 ProcessSP process_sp(GetSP());
197 Process::StopLocker stop_locker;
199 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
200 std::lock_guard<std::recursive_mutex> guard(
201 process_sp->GetTarget().GetAPIMutex());
202 num_threads = process_sp->GetThreadList().GetSize(can_update);
206 log->Printf("SBProcess(%p)::GetNumThreads () => %d",
207 static_cast<void *>(process_sp.get()), num_threads);
212 SBThread SBProcess::GetSelectedThread() const {
213 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
217 ProcessSP process_sp(GetSP());
219 std::lock_guard<std::recursive_mutex> guard(
220 process_sp->GetTarget().GetAPIMutex());
221 thread_sp = process_sp->GetThreadList().GetSelectedThread();
222 sb_thread.SetThread(thread_sp);
226 log->Printf("SBProcess(%p)::GetSelectedThread () => SBThread(%p)",
227 static_cast<void *>(process_sp.get()),
228 static_cast<void *>(thread_sp.get()));
233 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
234 lldb::addr_t context) {
235 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
239 ProcessSP process_sp(GetSP());
241 std::lock_guard<std::recursive_mutex> guard(
242 process_sp->GetTarget().GetAPIMutex());
243 thread_sp = process_sp->CreateOSPluginThread(tid, context);
244 sb_thread.SetThread(thread_sp);
248 log->Printf("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64
249 ", context=0x%" PRIx64 ") => SBThread(%p)",
250 static_cast<void *>(process_sp.get()), tid, context,
251 static_cast<void *>(thread_sp.get()));
256 SBTarget SBProcess::GetTarget() const {
257 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
261 ProcessSP process_sp(GetSP());
263 target_sp = process_sp->GetTarget().shared_from_this();
264 sb_target.SetSP(target_sp);
268 log->Printf("SBProcess(%p)::GetTarget () => SBTarget(%p)",
269 static_cast<void *>(process_sp.get()),
270 static_cast<void *>(target_sp.get()));
275 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
276 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
279 ProcessSP process_sp(GetSP());
282 ret_val = process_sp->PutSTDIN(src, src_len, error);
286 log->Printf("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%" PRIu64
288 static_cast<void *>(process_sp.get()), src,
289 static_cast<uint64_t>(src_len), static_cast<uint64_t>(ret_val));
294 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
295 size_t bytes_read = 0;
296 ProcessSP process_sp(GetSP());
299 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
302 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
305 "SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64
307 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
308 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
313 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
314 size_t bytes_read = 0;
315 ProcessSP process_sp(GetSP());
318 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
321 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
324 "SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64
326 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
327 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
332 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
333 size_t bytes_read = 0;
334 ProcessSP process_sp(GetSP());
337 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
340 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
343 "SBProcess(%p)::GetAsyncProfileData (dst=\"%.*s\", dst_len=%" PRIu64
345 static_cast<void *>(process_sp.get()), static_cast<int>(bytes_read),
346 dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read));
351 lldb::SBTrace SBProcess::StartTrace(SBTraceOptions &options,
352 lldb::SBError &error) {
353 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
354 ProcessSP process_sp(GetSP());
356 SBTrace trace_instance;
357 trace_instance.SetSP(process_sp);
358 lldb::user_id_t uid = LLDB_INVALID_UID;
361 error.SetErrorString("invalid process");
363 uid = process_sp->StartTrace(*(options.m_traceoptions_sp), error.ref());
364 trace_instance.SetTraceUID(uid);
365 LLDB_LOG(log, "SBProcess::returned uid - {0}", uid);
367 return trace_instance;
370 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
374 ProcessSP process_sp(GetSP());
376 const StateType event_state = SBProcess::GetStateFromEvent(event);
378 int message_len = ::snprintf(
379 message, sizeof(message), "Process %" PRIu64 " %s\n",
380 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
383 ::fwrite(message, 1, message_len, out);
387 void SBProcess::AppendEventStateReport(const SBEvent &event,
388 SBCommandReturnObject &result) {
389 ProcessSP process_sp(GetSP());
391 const StateType event_state = SBProcess::GetStateFromEvent(event);
393 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
394 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
396 result.AppendMessage(message);
400 bool SBProcess::SetSelectedThread(const SBThread &thread) {
401 ProcessSP process_sp(GetSP());
403 std::lock_guard<std::recursive_mutex> guard(
404 process_sp->GetTarget().GetAPIMutex());
405 return process_sp->GetThreadList().SetSelectedThreadByID(
406 thread.GetThreadID());
411 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
412 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
414 bool ret_val = false;
415 ProcessSP process_sp(GetSP());
417 std::lock_guard<std::recursive_mutex> guard(
418 process_sp->GetTarget().GetAPIMutex());
419 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
423 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64
425 static_cast<void *>(process_sp.get()), tid,
426 (ret_val ? "true" : "false"));
431 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
432 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
434 bool ret_val = false;
435 ProcessSP process_sp(GetSP());
437 std::lock_guard<std::recursive_mutex> guard(
438 process_sp->GetTarget().GetAPIMutex());
439 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
443 log->Printf("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
444 static_cast<void *>(process_sp.get()), index_id,
445 (ret_val ? "true" : "false"));
450 SBThread SBProcess::GetThreadAtIndex(size_t index) {
451 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
455 ProcessSP process_sp(GetSP());
457 Process::StopLocker stop_locker;
458 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
459 std::lock_guard<std::recursive_mutex> guard(
460 process_sp->GetTarget().GetAPIMutex());
461 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
462 sb_thread.SetThread(thread_sp);
466 log->Printf("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
467 static_cast<void *>(process_sp.get()),
468 static_cast<uint32_t>(index),
469 static_cast<void *>(thread_sp.get()));
474 uint32_t SBProcess::GetNumQueues() {
475 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
477 uint32_t num_queues = 0;
478 ProcessSP process_sp(GetSP());
480 Process::StopLocker stop_locker;
481 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
482 std::lock_guard<std::recursive_mutex> guard(
483 process_sp->GetTarget().GetAPIMutex());
484 num_queues = process_sp->GetQueueList().GetSize();
489 log->Printf("SBProcess(%p)::GetNumQueues () => %d",
490 static_cast<void *>(process_sp.get()), num_queues);
495 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
496 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
500 ProcessSP process_sp(GetSP());
502 Process::StopLocker stop_locker;
503 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
504 std::lock_guard<std::recursive_mutex> guard(
505 process_sp->GetTarget().GetAPIMutex());
506 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
507 sb_queue.SetQueue(queue_sp);
512 log->Printf("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
513 static_cast<void *>(process_sp.get()),
514 static_cast<uint32_t>(index),
515 static_cast<void *>(queue_sp.get()));
520 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
521 ProcessSP process_sp(GetSP());
523 std::lock_guard<std::recursive_mutex> guard(
524 process_sp->GetTarget().GetAPIMutex());
525 if (include_expression_stops)
526 return process_sp->GetStopID();
528 return process_sp->GetLastNaturalStopID();
533 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
534 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
538 ProcessSP process_sp(GetSP());
540 std::lock_guard<std::recursive_mutex> guard(
541 process_sp->GetTarget().GetAPIMutex());
542 event_sp = process_sp->GetStopEventForStopID(stop_id);
543 sb_event.reset(event_sp);
547 log->Printf("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32
549 static_cast<void *>(process_sp.get()), stop_id,
550 static_cast<void *>(event_sp.get()));
555 StateType SBProcess::GetState() {
557 StateType ret_val = eStateInvalid;
558 ProcessSP process_sp(GetSP());
560 std::lock_guard<std::recursive_mutex> guard(
561 process_sp->GetTarget().GetAPIMutex());
562 ret_val = process_sp->GetState();
565 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
567 log->Printf("SBProcess(%p)::GetState () => %s",
568 static_cast<void *>(process_sp.get()),
569 lldb_private::StateAsCString(ret_val));
574 int SBProcess::GetExitStatus() {
576 ProcessSP process_sp(GetSP());
578 std::lock_guard<std::recursive_mutex> guard(
579 process_sp->GetTarget().GetAPIMutex());
580 exit_status = process_sp->GetExitStatus();
582 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
584 log->Printf("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
585 static_cast<void *>(process_sp.get()), exit_status,
591 const char *SBProcess::GetExitDescription() {
592 const char *exit_desc = NULL;
593 ProcessSP process_sp(GetSP());
595 std::lock_guard<std::recursive_mutex> guard(
596 process_sp->GetTarget().GetAPIMutex());
597 exit_desc = process_sp->GetExitDescription();
599 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
601 log->Printf("SBProcess(%p)::GetExitDescription () => %s",
602 static_cast<void *>(process_sp.get()), exit_desc);
606 lldb::pid_t SBProcess::GetProcessID() {
607 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
608 ProcessSP process_sp(GetSP());
610 ret_val = process_sp->GetID();
612 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
614 log->Printf("SBProcess(%p)::GetProcessID () => %" PRIu64,
615 static_cast<void *>(process_sp.get()), ret_val);
620 uint32_t SBProcess::GetUniqueID() {
621 uint32_t ret_val = 0;
622 ProcessSP process_sp(GetSP());
624 ret_val = process_sp->GetUniqueID();
625 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
627 log->Printf("SBProcess(%p)::GetUniqueID () => %" PRIu32,
628 static_cast<void *>(process_sp.get()), ret_val);
632 ByteOrder SBProcess::GetByteOrder() const {
633 ByteOrder byteOrder = eByteOrderInvalid;
634 ProcessSP process_sp(GetSP());
636 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
638 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
640 log->Printf("SBProcess(%p)::GetByteOrder () => %d",
641 static_cast<void *>(process_sp.get()), byteOrder);
646 uint32_t SBProcess::GetAddressByteSize() const {
648 ProcessSP process_sp(GetSP());
650 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
652 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
654 log->Printf("SBProcess(%p)::GetAddressByteSize () => %d",
655 static_cast<void *>(process_sp.get()), size);
660 SBError SBProcess::Continue() {
661 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
664 ProcessSP process_sp(GetSP());
667 log->Printf("SBProcess(%p)::Continue ()...",
668 static_cast<void *>(process_sp.get()));
671 std::lock_guard<std::recursive_mutex> guard(
672 process_sp->GetTarget().GetAPIMutex());
674 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
675 sb_error.ref() = process_sp->Resume();
677 sb_error.ref() = process_sp->ResumeSynchronous(NULL);
679 sb_error.SetErrorString("SBProcess is invalid");
683 sb_error.GetDescription(sstr);
684 log->Printf("SBProcess(%p)::Continue () => SBError (%p): %s",
685 static_cast<void *>(process_sp.get()),
686 static_cast<void *>(sb_error.get()), sstr.GetData());
692 SBError SBProcess::Destroy() {
694 ProcessSP process_sp(GetSP());
696 std::lock_guard<std::recursive_mutex> guard(
697 process_sp->GetTarget().GetAPIMutex());
698 sb_error.SetError(process_sp->Destroy(false));
700 sb_error.SetErrorString("SBProcess is invalid");
702 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
705 sb_error.GetDescription(sstr);
706 log->Printf("SBProcess(%p)::Destroy () => SBError (%p): %s",
707 static_cast<void *>(process_sp.get()),
708 static_cast<void *>(sb_error.get()), sstr.GetData());
714 SBError SBProcess::Stop() {
716 ProcessSP process_sp(GetSP());
718 std::lock_guard<std::recursive_mutex> guard(
719 process_sp->GetTarget().GetAPIMutex());
720 sb_error.SetError(process_sp->Halt());
722 sb_error.SetErrorString("SBProcess is invalid");
724 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
727 sb_error.GetDescription(sstr);
728 log->Printf("SBProcess(%p)::Stop () => SBError (%p): %s",
729 static_cast<void *>(process_sp.get()),
730 static_cast<void *>(sb_error.get()), sstr.GetData());
736 SBError SBProcess::Kill() {
738 ProcessSP process_sp(GetSP());
740 std::lock_guard<std::recursive_mutex> guard(
741 process_sp->GetTarget().GetAPIMutex());
742 sb_error.SetError(process_sp->Destroy(true));
744 sb_error.SetErrorString("SBProcess is invalid");
746 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
749 sb_error.GetDescription(sstr);
750 log->Printf("SBProcess(%p)::Kill () => SBError (%p): %s",
751 static_cast<void *>(process_sp.get()),
752 static_cast<void *>(sb_error.get()), sstr.GetData());
758 SBError SBProcess::Detach() {
759 // FIXME: This should come from a process default.
760 bool keep_stopped = false;
761 return Detach(keep_stopped);
764 SBError SBProcess::Detach(bool keep_stopped) {
766 ProcessSP process_sp(GetSP());
768 std::lock_guard<std::recursive_mutex> guard(
769 process_sp->GetTarget().GetAPIMutex());
770 sb_error.SetError(process_sp->Detach(keep_stopped));
772 sb_error.SetErrorString("SBProcess is invalid");
777 SBError SBProcess::Signal(int signo) {
779 ProcessSP process_sp(GetSP());
781 std::lock_guard<std::recursive_mutex> guard(
782 process_sp->GetTarget().GetAPIMutex());
783 sb_error.SetError(process_sp->Signal(signo));
785 sb_error.SetErrorString("SBProcess is invalid");
786 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
789 sb_error.GetDescription(sstr);
790 log->Printf("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
791 static_cast<void *>(process_sp.get()), signo,
792 static_cast<void *>(sb_error.get()), sstr.GetData());
797 SBUnixSignals SBProcess::GetUnixSignals() {
798 if (auto process_sp = GetSP())
799 return SBUnixSignals{process_sp};
804 void SBProcess::SendAsyncInterrupt() {
805 ProcessSP process_sp(GetSP());
807 process_sp->SendAsyncInterrupt();
811 SBThread SBProcess::GetThreadByID(tid_t tid) {
814 ProcessSP process_sp(GetSP());
816 Process::StopLocker stop_locker;
817 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
818 std::lock_guard<std::recursive_mutex> guard(
819 process_sp->GetTarget().GetAPIMutex());
820 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
821 sb_thread.SetThread(thread_sp);
824 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
826 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64
827 ") => SBThread (%p)",
828 static_cast<void *>(process_sp.get()), tid,
829 static_cast<void *>(thread_sp.get()));
834 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
837 ProcessSP process_sp(GetSP());
839 Process::StopLocker stop_locker;
840 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
841 std::lock_guard<std::recursive_mutex> guard(
842 process_sp->GetTarget().GetAPIMutex());
844 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
845 sb_thread.SetThread(thread_sp);
848 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
850 log->Printf("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
851 static_cast<void *>(process_sp.get()), index_id,
852 static_cast<void *>(thread_sp.get()));
857 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
858 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
860 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
863 log->Printf("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
864 static_cast<void *>(event.get()),
865 lldb_private::StateAsCString(ret_val));
870 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
871 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
873 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
876 log->Printf("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
877 static_cast<void *>(event.get()), ret_val);
882 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
883 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
887 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
889 return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
892 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
893 ProcessSP process_sp =
894 Process::ProcessEventData::GetProcessFromEvent(event.get());
896 // StructuredData events also know the process they come from. Try that.
897 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
900 return SBProcess(process_sp);
903 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
904 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
907 lldb::SBStructuredData
908 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
909 return SBStructuredData(event.GetSP());
912 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
913 return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
914 !EventIsStructuredDataEvent(event);
917 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
918 EventSP event_sp = event.GetSP();
919 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
920 return event_data && (event_data->GetFlavor() ==
921 EventDataStructuredData::GetFlavorString());
924 SBBroadcaster SBProcess::GetBroadcaster() const {
925 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
927 ProcessSP process_sp(GetSP());
929 SBBroadcaster broadcaster(process_sp.get(), false);
932 log->Printf("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
933 static_cast<void *>(process_sp.get()),
934 static_cast<void *>(broadcaster.get()));
939 const char *SBProcess::GetBroadcasterClass() {
940 return Process::GetStaticBroadcasterClass().AsCString();
943 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
945 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
947 size_t bytes_read = 0;
949 ProcessSP process_sp(GetSP());
952 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
953 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
954 static_cast<void *>(process_sp.get()), addr,
955 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
956 static_cast<void *>(sb_error.get()));
959 Process::StopLocker stop_locker;
960 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
961 std::lock_guard<std::recursive_mutex> guard(
962 process_sp->GetTarget().GetAPIMutex());
963 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
966 log->Printf("SBProcess(%p)::ReadMemory() => error: process is running",
967 static_cast<void *>(process_sp.get()));
968 sb_error.SetErrorString("process is running");
971 sb_error.SetErrorString("SBProcess is invalid");
976 sb_error.GetDescription(sstr);
977 log->Printf("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64
978 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
979 static_cast<void *>(process_sp.get()), addr,
980 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
981 static_cast<void *>(sb_error.get()), sstr.GetData(),
982 static_cast<uint64_t>(bytes_read));
988 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
989 lldb::SBError &sb_error) {
990 size_t bytes_read = 0;
991 ProcessSP process_sp(GetSP());
993 Process::StopLocker stop_locker;
994 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
995 std::lock_guard<std::recursive_mutex> guard(
996 process_sp->GetTarget().GetAPIMutex());
997 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
1000 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1002 log->Printf("SBProcess(%p)::ReadCStringFromMemory() => error: process "
1004 static_cast<void *>(process_sp.get()));
1005 sb_error.SetErrorString("process is running");
1008 sb_error.SetErrorString("SBProcess is invalid");
1013 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
1014 lldb::SBError &sb_error) {
1016 ProcessSP process_sp(GetSP());
1018 Process::StopLocker stop_locker;
1019 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1020 std::lock_guard<std::recursive_mutex> guard(
1021 process_sp->GetTarget().GetAPIMutex());
1022 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
1025 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1027 log->Printf("SBProcess(%p)::ReadUnsignedFromMemory() => error: process "
1029 static_cast<void *>(process_sp.get()));
1030 sb_error.SetErrorString("process is running");
1033 sb_error.SetErrorString("SBProcess is invalid");
1038 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
1039 lldb::SBError &sb_error) {
1040 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1041 ProcessSP process_sp(GetSP());
1043 Process::StopLocker stop_locker;
1044 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1045 std::lock_guard<std::recursive_mutex> guard(
1046 process_sp->GetTarget().GetAPIMutex());
1047 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
1049 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1051 log->Printf("SBProcess(%p)::ReadPointerFromMemory() => error: process "
1053 static_cast<void *>(process_sp.get()));
1054 sb_error.SetErrorString("process is running");
1057 sb_error.SetErrorString("SBProcess is invalid");
1062 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
1063 SBError &sb_error) {
1064 size_t bytes_written = 0;
1066 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1068 ProcessSP process_sp(GetSP());
1071 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1072 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1073 static_cast<void *>(process_sp.get()), addr,
1074 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1075 static_cast<void *>(sb_error.get()));
1078 Process::StopLocker stop_locker;
1079 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1080 std::lock_guard<std::recursive_mutex> guard(
1081 process_sp->GetTarget().GetAPIMutex());
1083 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
1086 log->Printf("SBProcess(%p)::WriteMemory() => error: process is running",
1087 static_cast<void *>(process_sp.get()));
1088 sb_error.SetErrorString("process is running");
1094 sb_error.GetDescription(sstr);
1095 log->Printf("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64
1096 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1097 static_cast<void *>(process_sp.get()), addr,
1098 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
1099 static_cast<void *>(sb_error.get()), sstr.GetData(),
1100 static_cast<uint64_t>(bytes_written));
1103 return bytes_written;
1106 bool SBProcess::GetDescription(SBStream &description) {
1107 Stream &strm = description.ref();
1109 ProcessSP process_sp(GetSP());
1111 char path[PATH_MAX];
1112 GetTarget().GetExecutable().GetPath(path, sizeof(path));
1113 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1114 const char *exe_name = NULL;
1116 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1118 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1119 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1120 GetNumThreads(), exe_name ? ", executable = " : "",
1121 exe_name ? exe_name : "");
1123 strm.PutCString("No value");
1129 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1130 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1133 ProcessSP process_sp(GetSP());
1135 std::lock_guard<std::recursive_mutex> guard(
1136 process_sp->GetTarget().GetAPIMutex());
1137 sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
1139 log->Printf("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1140 static_cast<void *>(process_sp.get()), num);
1142 sb_error.SetErrorString("SBProcess is invalid");
1147 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1148 lldb::SBError &sb_error) {
1149 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1152 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1153 const lldb::SBFileSpec &sb_remote_image_spec,
1154 lldb::SBError &sb_error) {
1155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1156 ProcessSP process_sp(GetSP());
1158 Process::StopLocker stop_locker;
1159 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1161 log->Printf("SBProcess(%p)::LoadImage() => calling Platform::LoadImage"
1163 static_cast<void *>(process_sp.get()),
1164 sb_local_image_spec.GetFilename());
1166 std::lock_guard<std::recursive_mutex> guard(
1167 process_sp->GetTarget().GetAPIMutex());
1168 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1169 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1170 *sb_remote_image_spec, sb_error.ref());
1173 log->Printf("SBProcess(%p)::LoadImage() => error: process is running",
1174 static_cast<void *>(process_sp.get()));
1175 sb_error.SetErrorString("process is running");
1179 log->Printf("SBProcess(%p)::LoadImage() => error: called with invalid"
1181 static_cast<void *>(process_sp.get()));
1182 sb_error.SetErrorString("process is invalid");
1184 return LLDB_INVALID_IMAGE_TOKEN;
1187 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
1188 SBStringList &paths,
1189 lldb::SBFileSpec &loaded_path,
1190 lldb::SBError &error) {
1191 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1192 ProcessSP process_sp(GetSP());
1194 Process::StopLocker stop_locker;
1195 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1197 log->Printf("SBProcess(%p)::LoadImageUsingPaths() => "
1198 "calling Platform::LoadImageUsingPaths for: %s",
1199 static_cast<void *>(process_sp.get()),
1200 image_spec.GetFilename());
1202 std::lock_guard<std::recursive_mutex> guard(
1203 process_sp->GetTarget().GetAPIMutex());
1204 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1205 size_t num_paths = paths.GetSize();
1206 std::vector<std::string> paths_vec;
1207 paths_vec.reserve(num_paths);
1208 for (size_t i = 0; i < num_paths; i++)
1209 paths_vec.push_back(paths.GetStringAtIndex(i));
1210 FileSpec loaded_spec;
1212 uint32_t token = platform_sp->LoadImageUsingPaths(process_sp.get(),
1217 if (token != LLDB_INVALID_IMAGE_TOKEN)
1218 loaded_path = loaded_spec;
1222 log->Printf("SBProcess(%p)::LoadImageUsingPaths() => error: "
1223 "process is running",
1224 static_cast<void *>(process_sp.get()));
1225 error.SetErrorString("process is running");
1229 log->Printf("SBProcess(%p)::LoadImageUsingPaths() => error: "
1230 "called with invalid process",
1231 static_cast<void *>(process_sp.get()));
1232 error.SetErrorString("process is invalid");
1235 return LLDB_INVALID_IMAGE_TOKEN;
1238 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1239 lldb::SBError sb_error;
1240 ProcessSP process_sp(GetSP());
1242 Process::StopLocker stop_locker;
1243 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1244 std::lock_guard<std::recursive_mutex> guard(
1245 process_sp->GetTarget().GetAPIMutex());
1246 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1248 platform_sp->UnloadImage(process_sp.get(), image_token));
1250 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1252 log->Printf("SBProcess(%p)::UnloadImage() => error: process is running",
1253 static_cast<void *>(process_sp.get()));
1254 sb_error.SetErrorString("process is running");
1257 sb_error.SetErrorString("invalid process");
1261 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1262 lldb::SBError sb_error;
1263 ProcessSP process_sp(GetSP());
1265 Process::StopLocker stop_locker;
1266 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1267 std::lock_guard<std::recursive_mutex> guard(
1268 process_sp->GetTarget().GetAPIMutex());
1269 sb_error.SetError(process_sp->SendEventData(event_data));
1271 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1274 "SBProcess(%p)::SendEventData() => error: process is running",
1275 static_cast<void *>(process_sp.get()));
1276 sb_error.SetErrorString("process is running");
1279 sb_error.SetErrorString("invalid process");
1283 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1284 ProcessSP process_sp(GetSP());
1285 if (process_sp && process_sp->GetSystemRuntime()) {
1286 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1287 return runtime->GetExtendedBacktraceTypes().size();
1292 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1293 ProcessSP process_sp(GetSP());
1294 if (process_sp && process_sp->GetSystemRuntime()) {
1295 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1296 const std::vector<ConstString> &names =
1297 runtime->GetExtendedBacktraceTypes();
1298 if (idx < names.size()) {
1299 return names[idx].AsCString();
1301 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1303 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => "
1304 "error: requested extended backtrace name out of bounds",
1305 static_cast<void *>(process_sp.get()));
1311 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1312 ProcessSP process_sp(GetSP());
1313 SBThreadCollection threads;
1315 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1320 bool SBProcess::IsInstrumentationRuntimePresent(
1321 InstrumentationRuntimeType type) {
1322 ProcessSP process_sp(GetSP());
1326 InstrumentationRuntimeSP runtime_sp =
1327 process_sp->GetInstrumentationRuntime(type);
1329 if (!runtime_sp.get())
1332 return runtime_sp->IsActive();
1335 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1336 lldb::SBError error;
1337 ProcessSP process_sp(GetSP());
1339 error.SetErrorString("SBProcess is invalid");
1343 std::lock_guard<std::recursive_mutex> guard(
1344 process_sp->GetTarget().GetAPIMutex());
1346 if (process_sp->GetState() != eStateStopped) {
1347 error.SetErrorString("the process is not stopped");
1351 FileSpec core_file(file_name);
1352 error.ref() = PluginManager::SaveCore(process_sp, core_file);
1357 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1358 SBMemoryRegionInfo &sb_region_info) {
1359 lldb::SBError sb_error;
1360 ProcessSP process_sp(GetSP());
1362 Process::StopLocker stop_locker;
1363 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1364 std::lock_guard<std::recursive_mutex> guard(
1365 process_sp->GetTarget().GetAPIMutex());
1368 process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref());
1370 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1373 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1374 static_cast<void *>(process_sp.get()));
1375 sb_error.SetErrorString("process is running");
1378 sb_error.SetErrorString("SBProcess is invalid");
1383 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1384 lldb::SBMemoryRegionInfoList sb_region_list;
1386 ProcessSP process_sp(GetSP());
1387 Process::StopLocker stop_locker;
1388 if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1389 std::lock_guard<std::recursive_mutex> guard(
1390 process_sp->GetTarget().GetAPIMutex());
1392 process_sp->GetMemoryRegions(sb_region_list.ref());
1394 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1397 "SBProcess(%p)::GetMemoryRegionInfo() => error: process is running",
1398 static_cast<void *>(process_sp.get()));
1401 return sb_region_list;
1404 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1405 lldb::SBProcessInfo sb_proc_info;
1406 ProcessSP process_sp(GetSP());
1407 ProcessInstanceInfo proc_info;
1408 if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1409 sb_proc_info.SetProcessInfo(proc_info);
1411 return sb_proc_info;