1 //===-- SBProcess.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBProcess.h"
10 #include "lldb/Utility/Instrumentation.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/StructuredDataImpl.h"
21 #include "lldb/Host/StreamFile.h"
22 #include "lldb/Target/MemoryRegionInfo.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/SystemRuntime.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Utility/Args.h"
29 #include "lldb/Utility/ProcessInfo.h"
30 #include "lldb/Utility/State.h"
31 #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/SBFile.h"
38 #include "lldb/API/SBFileSpec.h"
39 #include "lldb/API/SBMemoryRegionInfo.h"
40 #include "lldb/API/SBMemoryRegionInfoList.h"
41 #include "lldb/API/SBScriptObject.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/SBUnixSignals.h"
51 using namespace lldb_private
;
53 SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }
55 // SBProcess constructor
57 SBProcess::SBProcess(const SBProcess
&rhs
) : m_opaque_wp(rhs
.m_opaque_wp
) {
58 LLDB_INSTRUMENT_VA(this, rhs
);
61 SBProcess::SBProcess(const lldb::ProcessSP
&process_sp
)
62 : m_opaque_wp(process_sp
) {
63 LLDB_INSTRUMENT_VA(this, process_sp
);
66 const SBProcess
&SBProcess::operator=(const SBProcess
&rhs
) {
67 LLDB_INSTRUMENT_VA(this, rhs
);
70 m_opaque_wp
= rhs
.m_opaque_wp
;
75 SBProcess::~SBProcess() = default;
77 const char *SBProcess::GetBroadcasterClassName() {
80 return Process::GetStaticBroadcasterClass().AsCString();
83 const char *SBProcess::GetPluginName() {
84 LLDB_INSTRUMENT_VA(this);
86 ProcessSP
process_sp(GetSP());
88 return ConstString(process_sp
->GetPluginName()).GetCString();
93 const char *SBProcess::GetShortPluginName() {
94 LLDB_INSTRUMENT_VA(this);
96 ProcessSP
process_sp(GetSP());
98 return ConstString(process_sp
->GetPluginName()).GetCString();
103 lldb::ProcessSP
SBProcess::GetSP() const { return m_opaque_wp
.lock(); }
105 void SBProcess::SetSP(const ProcessSP
&process_sp
) { m_opaque_wp
= process_sp
; }
107 void SBProcess::Clear() {
108 LLDB_INSTRUMENT_VA(this);
113 bool SBProcess::IsValid() const {
114 LLDB_INSTRUMENT_VA(this);
115 return this->operator bool();
117 SBProcess::operator bool() const {
118 LLDB_INSTRUMENT_VA(this);
120 ProcessSP
process_sp(m_opaque_wp
.lock());
121 return ((bool)process_sp
&& process_sp
->IsValid());
124 bool SBProcess::RemoteLaunch(char const **argv
, char const **envp
,
125 const char *stdin_path
, const char *stdout_path
,
126 const char *stderr_path
,
127 const char *working_directory
,
128 uint32_t launch_flags
, bool stop_at_entry
,
129 lldb::SBError
&error
) {
130 LLDB_INSTRUMENT_VA(this, argv
, envp
, stdin_path
, stdout_path
, stderr_path
,
131 working_directory
, launch_flags
, stop_at_entry
, error
);
133 ProcessSP
process_sp(GetSP());
135 std::lock_guard
<std::recursive_mutex
> guard(
136 process_sp
->GetTarget().GetAPIMutex());
137 if (process_sp
->GetState() == eStateConnected
) {
139 launch_flags
|= eLaunchFlagStopAtEntry
;
140 ProcessLaunchInfo
launch_info(FileSpec(stdin_path
), FileSpec(stdout_path
),
141 FileSpec(stderr_path
),
142 FileSpec(working_directory
), launch_flags
);
143 Module
*exe_module
= process_sp
->GetTarget().GetExecutableModulePointer();
145 launch_info
.SetExecutableFile(exe_module
->GetPlatformFileSpec(), true);
147 launch_info
.GetArguments().AppendArguments(argv
);
149 launch_info
.GetEnvironment() = Environment(envp
);
150 error
.SetError(process_sp
->Launch(launch_info
));
152 error
.SetErrorString("must be in eStateConnected to call RemoteLaunch");
155 error
.SetErrorString("unable to attach pid");
158 return error
.Success();
161 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid
,
162 lldb::SBError
&error
) {
163 LLDB_INSTRUMENT_VA(this, pid
, 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 return error
.Success();
184 uint32_t SBProcess::GetNumThreads() {
185 LLDB_INSTRUMENT_VA(this);
187 uint32_t num_threads
= 0;
188 ProcessSP
process_sp(GetSP());
190 Process::StopLocker stop_locker
;
192 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
193 std::lock_guard
<std::recursive_mutex
> guard(
194 process_sp
->GetTarget().GetAPIMutex());
195 num_threads
= process_sp
->GetThreadList().GetSize(can_update
);
201 SBThread
SBProcess::GetSelectedThread() const {
202 LLDB_INSTRUMENT_VA(this);
206 ProcessSP
process_sp(GetSP());
208 std::lock_guard
<std::recursive_mutex
> guard(
209 process_sp
->GetTarget().GetAPIMutex());
210 thread_sp
= process_sp
->GetThreadList().GetSelectedThread();
211 sb_thread
.SetThread(thread_sp
);
217 SBThread
SBProcess::CreateOSPluginThread(lldb::tid_t tid
,
218 lldb::addr_t context
) {
219 LLDB_INSTRUMENT_VA(this, tid
, context
);
223 ProcessSP
process_sp(GetSP());
225 std::lock_guard
<std::recursive_mutex
> guard(
226 process_sp
->GetTarget().GetAPIMutex());
227 thread_sp
= process_sp
->CreateOSPluginThread(tid
, context
);
228 sb_thread
.SetThread(thread_sp
);
234 SBTarget
SBProcess::GetTarget() const {
235 LLDB_INSTRUMENT_VA(this);
239 ProcessSP
process_sp(GetSP());
241 target_sp
= process_sp
->GetTarget().shared_from_this();
242 sb_target
.SetSP(target_sp
);
248 size_t SBProcess::PutSTDIN(const char *src
, size_t src_len
) {
249 LLDB_INSTRUMENT_VA(this, src
, src_len
);
252 ProcessSP
process_sp(GetSP());
255 ret_val
= process_sp
->PutSTDIN(src
, src_len
, error
);
261 size_t SBProcess::GetSTDOUT(char *dst
, size_t dst_len
) const {
262 LLDB_INSTRUMENT_VA(this, dst
, dst_len
);
264 size_t bytes_read
= 0;
265 ProcessSP
process_sp(GetSP());
268 bytes_read
= process_sp
->GetSTDOUT(dst
, dst_len
, error
);
274 size_t SBProcess::GetSTDERR(char *dst
, size_t dst_len
) const {
275 LLDB_INSTRUMENT_VA(this, dst
, dst_len
);
277 size_t bytes_read
= 0;
278 ProcessSP
process_sp(GetSP());
281 bytes_read
= process_sp
->GetSTDERR(dst
, dst_len
, error
);
287 size_t SBProcess::GetAsyncProfileData(char *dst
, size_t dst_len
) const {
288 LLDB_INSTRUMENT_VA(this, dst
, dst_len
);
290 size_t bytes_read
= 0;
291 ProcessSP
process_sp(GetSP());
294 bytes_read
= process_sp
->GetAsyncProfileData(dst
, dst_len
, error
);
300 void SBProcess::ReportEventState(const SBEvent
&event
, SBFile out
) const {
301 LLDB_INSTRUMENT_VA(this, event
, out
);
303 return ReportEventState(event
, out
.m_opaque_sp
);
306 void SBProcess::ReportEventState(const SBEvent
&event
, FILE *out
) const {
307 LLDB_INSTRUMENT_VA(this, event
, out
);
308 FileSP outfile
= std::make_shared
<NativeFile
>(out
, false);
309 return ReportEventState(event
, outfile
);
312 void SBProcess::ReportEventState(const SBEvent
&event
, FileSP out
) const {
314 LLDB_INSTRUMENT_VA(this, event
, out
);
316 if (!out
|| !out
->IsValid())
319 ProcessSP
process_sp(GetSP());
321 StreamFile
stream(out
);
322 const StateType event_state
= SBProcess::GetStateFromEvent(event
);
323 stream
.Printf("Process %" PRIu64
" %s\n",
324 process_sp
->GetID(), SBDebugger::StateAsCString(event_state
));
328 void SBProcess::AppendEventStateReport(const SBEvent
&event
,
329 SBCommandReturnObject
&result
) {
330 LLDB_INSTRUMENT_VA(this, event
, result
);
332 ProcessSP
process_sp(GetSP());
334 const StateType event_state
= SBProcess::GetStateFromEvent(event
);
336 ::snprintf(message
, sizeof(message
), "Process %" PRIu64
" %s\n",
337 process_sp
->GetID(), SBDebugger::StateAsCString(event_state
));
339 result
.AppendMessage(message
);
343 bool SBProcess::SetSelectedThread(const SBThread
&thread
) {
344 LLDB_INSTRUMENT_VA(this, thread
);
346 ProcessSP
process_sp(GetSP());
348 std::lock_guard
<std::recursive_mutex
> guard(
349 process_sp
->GetTarget().GetAPIMutex());
350 return process_sp
->GetThreadList().SetSelectedThreadByID(
351 thread
.GetThreadID());
356 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid
) {
357 LLDB_INSTRUMENT_VA(this, tid
);
359 bool ret_val
= false;
360 ProcessSP
process_sp(GetSP());
362 std::lock_guard
<std::recursive_mutex
> guard(
363 process_sp
->GetTarget().GetAPIMutex());
364 ret_val
= process_sp
->GetThreadList().SetSelectedThreadByID(tid
);
370 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id
) {
371 LLDB_INSTRUMENT_VA(this, index_id
);
373 bool ret_val
= false;
374 ProcessSP
process_sp(GetSP());
376 std::lock_guard
<std::recursive_mutex
> guard(
377 process_sp
->GetTarget().GetAPIMutex());
378 ret_val
= process_sp
->GetThreadList().SetSelectedThreadByIndexID(index_id
);
385 SBThread
SBProcess::GetThreadAtIndex(size_t index
) {
386 LLDB_INSTRUMENT_VA(this, index
);
390 ProcessSP
process_sp(GetSP());
392 Process::StopLocker stop_locker
;
393 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
394 std::lock_guard
<std::recursive_mutex
> guard(
395 process_sp
->GetTarget().GetAPIMutex());
396 thread_sp
= process_sp
->GetThreadList().GetThreadAtIndex(index
, can_update
);
397 sb_thread
.SetThread(thread_sp
);
403 uint32_t SBProcess::GetNumQueues() {
404 LLDB_INSTRUMENT_VA(this);
406 uint32_t num_queues
= 0;
407 ProcessSP
process_sp(GetSP());
409 Process::StopLocker stop_locker
;
410 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
411 std::lock_guard
<std::recursive_mutex
> guard(
412 process_sp
->GetTarget().GetAPIMutex());
413 num_queues
= process_sp
->GetQueueList().GetSize();
420 SBQueue
SBProcess::GetQueueAtIndex(size_t index
) {
421 LLDB_INSTRUMENT_VA(this, index
);
425 ProcessSP
process_sp(GetSP());
427 Process::StopLocker stop_locker
;
428 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
429 std::lock_guard
<std::recursive_mutex
> guard(
430 process_sp
->GetTarget().GetAPIMutex());
431 queue_sp
= process_sp
->GetQueueList().GetQueueAtIndex(index
);
432 sb_queue
.SetQueue(queue_sp
);
439 uint32_t SBProcess::GetStopID(bool include_expression_stops
) {
440 LLDB_INSTRUMENT_VA(this, include_expression_stops
);
442 ProcessSP
process_sp(GetSP());
444 std::lock_guard
<std::recursive_mutex
> guard(
445 process_sp
->GetTarget().GetAPIMutex());
446 if (include_expression_stops
)
447 return process_sp
->GetStopID();
449 return process_sp
->GetLastNaturalStopID();
454 SBEvent
SBProcess::GetStopEventForStopID(uint32_t stop_id
) {
455 LLDB_INSTRUMENT_VA(this, stop_id
);
459 ProcessSP
process_sp(GetSP());
461 std::lock_guard
<std::recursive_mutex
> guard(
462 process_sp
->GetTarget().GetAPIMutex());
463 event_sp
= process_sp
->GetStopEventForStopID(stop_id
);
464 sb_event
.reset(event_sp
);
470 void SBProcess::ForceScriptedState(StateType new_state
) {
471 LLDB_INSTRUMENT_VA(this, new_state
);
473 if (ProcessSP process_sp
= GetSP()) {
474 std::lock_guard
<std::recursive_mutex
> guard(
475 process_sp
->GetTarget().GetAPIMutex());
476 process_sp
->ForceScriptedState(new_state
);
480 StateType
SBProcess::GetState() {
481 LLDB_INSTRUMENT_VA(this);
483 StateType ret_val
= eStateInvalid
;
484 ProcessSP
process_sp(GetSP());
486 std::lock_guard
<std::recursive_mutex
> guard(
487 process_sp
->GetTarget().GetAPIMutex());
488 ret_val
= process_sp
->GetState();
494 int SBProcess::GetExitStatus() {
495 LLDB_INSTRUMENT_VA(this);
498 ProcessSP
process_sp(GetSP());
500 std::lock_guard
<std::recursive_mutex
> guard(
501 process_sp
->GetTarget().GetAPIMutex());
502 exit_status
= process_sp
->GetExitStatus();
508 const char *SBProcess::GetExitDescription() {
509 LLDB_INSTRUMENT_VA(this);
511 ProcessSP
process_sp(GetSP());
515 std::lock_guard
<std::recursive_mutex
> guard(
516 process_sp
->GetTarget().GetAPIMutex());
517 return ConstString(process_sp
->GetExitDescription()).GetCString();
520 lldb::pid_t
SBProcess::GetProcessID() {
521 LLDB_INSTRUMENT_VA(this);
523 lldb::pid_t ret_val
= LLDB_INVALID_PROCESS_ID
;
524 ProcessSP
process_sp(GetSP());
526 ret_val
= process_sp
->GetID();
531 uint32_t SBProcess::GetUniqueID() {
532 LLDB_INSTRUMENT_VA(this);
534 uint32_t ret_val
= 0;
535 ProcessSP
process_sp(GetSP());
537 ret_val
= process_sp
->GetUniqueID();
541 ByteOrder
SBProcess::GetByteOrder() const {
542 LLDB_INSTRUMENT_VA(this);
544 ByteOrder byteOrder
= eByteOrderInvalid
;
545 ProcessSP
process_sp(GetSP());
547 byteOrder
= process_sp
->GetTarget().GetArchitecture().GetByteOrder();
553 uint32_t SBProcess::GetAddressByteSize() const {
554 LLDB_INSTRUMENT_VA(this);
557 ProcessSP
process_sp(GetSP());
559 size
= process_sp
->GetTarget().GetArchitecture().GetAddressByteSize();
565 SBError
SBProcess::Continue() {
566 LLDB_INSTRUMENT_VA(this);
569 ProcessSP
process_sp(GetSP());
572 std::lock_guard
<std::recursive_mutex
> guard(
573 process_sp
->GetTarget().GetAPIMutex());
575 if (process_sp
->GetTarget().GetDebugger().GetAsyncExecution())
576 sb_error
.ref() = process_sp
->Resume();
578 sb_error
.ref() = process_sp
->ResumeSynchronous(nullptr);
580 sb_error
.SetErrorString("SBProcess is invalid");
585 SBError
SBProcess::Destroy() {
586 LLDB_INSTRUMENT_VA(this);
589 ProcessSP
process_sp(GetSP());
591 std::lock_guard
<std::recursive_mutex
> guard(
592 process_sp
->GetTarget().GetAPIMutex());
593 sb_error
.SetError(process_sp
->Destroy(false));
595 sb_error
.SetErrorString("SBProcess is invalid");
600 SBError
SBProcess::Stop() {
601 LLDB_INSTRUMENT_VA(this);
604 ProcessSP
process_sp(GetSP());
606 std::lock_guard
<std::recursive_mutex
> guard(
607 process_sp
->GetTarget().GetAPIMutex());
608 sb_error
.SetError(process_sp
->Halt());
610 sb_error
.SetErrorString("SBProcess is invalid");
615 SBError
SBProcess::Kill() {
616 LLDB_INSTRUMENT_VA(this);
619 ProcessSP
process_sp(GetSP());
621 std::lock_guard
<std::recursive_mutex
> guard(
622 process_sp
->GetTarget().GetAPIMutex());
623 sb_error
.SetError(process_sp
->Destroy(true));
625 sb_error
.SetErrorString("SBProcess is invalid");
630 SBError
SBProcess::Detach() {
631 LLDB_INSTRUMENT_VA(this);
633 // FIXME: This should come from a process default.
634 bool keep_stopped
= false;
635 return Detach(keep_stopped
);
638 SBError
SBProcess::Detach(bool keep_stopped
) {
639 LLDB_INSTRUMENT_VA(this, keep_stopped
);
642 ProcessSP
process_sp(GetSP());
644 std::lock_guard
<std::recursive_mutex
> guard(
645 process_sp
->GetTarget().GetAPIMutex());
646 sb_error
.SetError(process_sp
->Detach(keep_stopped
));
648 sb_error
.SetErrorString("SBProcess is invalid");
653 SBError
SBProcess::Signal(int signo
) {
654 LLDB_INSTRUMENT_VA(this, signo
);
657 ProcessSP
process_sp(GetSP());
659 std::lock_guard
<std::recursive_mutex
> guard(
660 process_sp
->GetTarget().GetAPIMutex());
661 sb_error
.SetError(process_sp
->Signal(signo
));
663 sb_error
.SetErrorString("SBProcess is invalid");
668 SBUnixSignals
SBProcess::GetUnixSignals() {
669 LLDB_INSTRUMENT_VA(this);
671 if (auto process_sp
= GetSP())
672 return SBUnixSignals
{process_sp
};
674 return SBUnixSignals
{};
677 void SBProcess::SendAsyncInterrupt() {
678 LLDB_INSTRUMENT_VA(this);
680 ProcessSP
process_sp(GetSP());
682 process_sp
->SendAsyncInterrupt();
686 SBThread
SBProcess::GetThreadByID(tid_t tid
) {
687 LLDB_INSTRUMENT_VA(this, tid
);
691 ProcessSP
process_sp(GetSP());
693 Process::StopLocker stop_locker
;
694 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
695 std::lock_guard
<std::recursive_mutex
> guard(
696 process_sp
->GetTarget().GetAPIMutex());
697 thread_sp
= process_sp
->GetThreadList().FindThreadByID(tid
, can_update
);
698 sb_thread
.SetThread(thread_sp
);
704 SBThread
SBProcess::GetThreadByIndexID(uint32_t index_id
) {
705 LLDB_INSTRUMENT_VA(this, index_id
);
709 ProcessSP
process_sp(GetSP());
711 Process::StopLocker stop_locker
;
712 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
713 std::lock_guard
<std::recursive_mutex
> guard(
714 process_sp
->GetTarget().GetAPIMutex());
716 process_sp
->GetThreadList().FindThreadByIndexID(index_id
, can_update
);
717 sb_thread
.SetThread(thread_sp
);
723 StateType
SBProcess::GetStateFromEvent(const SBEvent
&event
) {
724 LLDB_INSTRUMENT_VA(event
);
726 StateType ret_val
= Process::ProcessEventData::GetStateFromEvent(event
.get());
731 bool SBProcess::GetRestartedFromEvent(const SBEvent
&event
) {
732 LLDB_INSTRUMENT_VA(event
);
734 bool ret_val
= Process::ProcessEventData::GetRestartedFromEvent(event
.get());
739 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent
&event
) {
740 LLDB_INSTRUMENT_VA(event
);
742 return Process::ProcessEventData::GetNumRestartedReasons(event
.get());
746 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent
&event
,
748 LLDB_INSTRUMENT_VA(event
, idx
);
750 return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex(
755 SBProcess
SBProcess::GetProcessFromEvent(const SBEvent
&event
) {
756 LLDB_INSTRUMENT_VA(event
);
758 ProcessSP process_sp
=
759 Process::ProcessEventData::GetProcessFromEvent(event
.get());
761 // StructuredData events also know the process they come from. Try that.
762 process_sp
= EventDataStructuredData::GetProcessFromEvent(event
.get());
765 return SBProcess(process_sp
);
768 bool SBProcess::GetInterruptedFromEvent(const SBEvent
&event
) {
769 LLDB_INSTRUMENT_VA(event
);
771 return Process::ProcessEventData::GetInterruptedFromEvent(event
.get());
774 lldb::SBStructuredData
775 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent
&event
) {
776 LLDB_INSTRUMENT_VA(event
);
778 return SBStructuredData(event
.GetSP());
781 bool SBProcess::EventIsProcessEvent(const SBEvent
&event
) {
782 LLDB_INSTRUMENT_VA(event
);
784 return Process::ProcessEventData::GetEventDataFromEvent(event
.get()) !=
788 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent
&event
) {
789 LLDB_INSTRUMENT_VA(event
);
791 EventSP event_sp
= event
.GetSP();
792 EventData
*event_data
= event_sp
? event_sp
->GetData() : nullptr;
793 return event_data
&& (event_data
->GetFlavor() ==
794 EventDataStructuredData::GetFlavorString());
797 SBBroadcaster
SBProcess::GetBroadcaster() const {
798 LLDB_INSTRUMENT_VA(this);
800 ProcessSP
process_sp(GetSP());
802 SBBroadcaster
broadcaster(process_sp
.get(), false);
807 const char *SBProcess::GetBroadcasterClass() {
810 return Process::GetStaticBroadcasterClass().AsCString();
813 size_t SBProcess::ReadMemory(addr_t addr
, void *dst
, size_t dst_len
,
815 LLDB_INSTRUMENT_VA(this, addr
, dst
, dst_len
, sb_error
);
818 sb_error
.SetErrorStringWithFormat(
819 "no buffer provided to read %zu bytes into", dst_len
);
823 size_t bytes_read
= 0;
824 ProcessSP
process_sp(GetSP());
828 Process::StopLocker stop_locker
;
829 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
830 std::lock_guard
<std::recursive_mutex
> guard(
831 process_sp
->GetTarget().GetAPIMutex());
832 bytes_read
= process_sp
->ReadMemory(addr
, dst
, dst_len
, sb_error
.ref());
834 sb_error
.SetErrorString("process is running");
837 sb_error
.SetErrorString("SBProcess is invalid");
843 size_t SBProcess::ReadCStringFromMemory(addr_t addr
, void *buf
, size_t size
,
844 lldb::SBError
&sb_error
) {
845 LLDB_INSTRUMENT_VA(this, addr
, buf
, size
, sb_error
);
847 size_t bytes_read
= 0;
848 ProcessSP
process_sp(GetSP());
850 Process::StopLocker stop_locker
;
851 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
852 std::lock_guard
<std::recursive_mutex
> guard(
853 process_sp
->GetTarget().GetAPIMutex());
854 bytes_read
= process_sp
->ReadCStringFromMemory(addr
, (char *)buf
, size
,
857 sb_error
.SetErrorString("process is running");
860 sb_error
.SetErrorString("SBProcess is invalid");
865 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr
, uint32_t byte_size
,
866 lldb::SBError
&sb_error
) {
867 LLDB_INSTRUMENT_VA(this, addr
, byte_size
, sb_error
);
870 ProcessSP
process_sp(GetSP());
872 Process::StopLocker stop_locker
;
873 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
874 std::lock_guard
<std::recursive_mutex
> guard(
875 process_sp
->GetTarget().GetAPIMutex());
876 value
= process_sp
->ReadUnsignedIntegerFromMemory(addr
, byte_size
, 0,
879 sb_error
.SetErrorString("process is running");
882 sb_error
.SetErrorString("SBProcess is invalid");
887 lldb::addr_t
SBProcess::ReadPointerFromMemory(addr_t addr
,
888 lldb::SBError
&sb_error
) {
889 LLDB_INSTRUMENT_VA(this, addr
, sb_error
);
891 lldb::addr_t ptr
= LLDB_INVALID_ADDRESS
;
892 ProcessSP
process_sp(GetSP());
894 Process::StopLocker stop_locker
;
895 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
896 std::lock_guard
<std::recursive_mutex
> guard(
897 process_sp
->GetTarget().GetAPIMutex());
898 ptr
= process_sp
->ReadPointerFromMemory(addr
, sb_error
.ref());
900 sb_error
.SetErrorString("process is running");
903 sb_error
.SetErrorString("SBProcess is invalid");
908 size_t SBProcess::WriteMemory(addr_t addr
, const void *src
, size_t src_len
,
910 LLDB_INSTRUMENT_VA(this, addr
, src
, src_len
, sb_error
);
912 size_t bytes_written
= 0;
914 ProcessSP
process_sp(GetSP());
917 Process::StopLocker stop_locker
;
918 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
919 std::lock_guard
<std::recursive_mutex
> guard(
920 process_sp
->GetTarget().GetAPIMutex());
922 process_sp
->WriteMemory(addr
, src
, src_len
, sb_error
.ref());
924 sb_error
.SetErrorString("process is running");
928 return bytes_written
;
931 bool SBProcess::GetDescription(SBStream
&description
) {
932 LLDB_INSTRUMENT_VA(this, description
);
934 Stream
&strm
= description
.ref();
936 ProcessSP
process_sp(GetSP());
939 GetTarget().GetExecutable().GetPath(path
, sizeof(path
));
940 Module
*exe_module
= process_sp
->GetTarget().GetExecutableModulePointer();
941 const char *exe_name
= nullptr;
943 exe_name
= exe_module
->GetFileSpec().GetFilename().AsCString();
945 strm
.Printf("SBProcess: pid = %" PRIu64
", state = %s, threads = %d%s%s",
946 process_sp
->GetID(), lldb_private::StateAsCString(GetState()),
947 GetNumThreads(), exe_name
? ", executable = " : "",
948 exe_name
? exe_name
: "");
950 strm
.PutCString("No value");
955 SBStructuredData
SBProcess::GetExtendedCrashInformation() {
956 LLDB_INSTRUMENT_VA(this);
957 SBStructuredData data
;
958 ProcessSP
process_sp(GetSP());
962 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
968 platform_sp
->FetchExtendedCrashInformation(*process_sp
.get());
973 StructuredData::ObjectSP fetched_data
= *expected_data
;
974 data
.m_impl_up
->SetObjectSP(fetched_data
);
979 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError
&sb_error
) const {
980 LLDB_INSTRUMENT_VA(this, sb_error
);
983 ProcessSP
process_sp(GetSP());
985 std::lock_guard
<std::recursive_mutex
> guard(
986 process_sp
->GetTarget().GetAPIMutex());
987 std::optional
<uint32_t> actual_num
= process_sp
->GetWatchpointSlotCount();
991 sb_error
.SetErrorString("Unable to determine number of watchpoints");
994 sb_error
.SetErrorString("SBProcess is invalid");
999 uint32_t SBProcess::LoadImage(lldb::SBFileSpec
&sb_remote_image_spec
,
1000 lldb::SBError
&sb_error
) {
1001 LLDB_INSTRUMENT_VA(this, sb_remote_image_spec
, sb_error
);
1003 return LoadImage(SBFileSpec(), sb_remote_image_spec
, sb_error
);
1006 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec
&sb_local_image_spec
,
1007 const lldb::SBFileSpec
&sb_remote_image_spec
,
1008 lldb::SBError
&sb_error
) {
1009 LLDB_INSTRUMENT_VA(this, sb_local_image_spec
, sb_remote_image_spec
, sb_error
);
1011 ProcessSP
process_sp(GetSP());
1013 Process::StopLocker stop_locker
;
1014 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1015 std::lock_guard
<std::recursive_mutex
> guard(
1016 process_sp
->GetTarget().GetAPIMutex());
1017 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1018 return platform_sp
->LoadImage(process_sp
.get(), *sb_local_image_spec
,
1019 *sb_remote_image_spec
, sb_error
.ref());
1021 sb_error
.SetErrorString("process is running");
1024 sb_error
.SetErrorString("process is invalid");
1026 return LLDB_INVALID_IMAGE_TOKEN
;
1029 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec
&image_spec
,
1030 SBStringList
&paths
,
1031 lldb::SBFileSpec
&loaded_path
,
1032 lldb::SBError
&error
) {
1033 LLDB_INSTRUMENT_VA(this, image_spec
, paths
, loaded_path
, error
);
1035 ProcessSP
process_sp(GetSP());
1037 Process::StopLocker stop_locker
;
1038 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1039 std::lock_guard
<std::recursive_mutex
> guard(
1040 process_sp
->GetTarget().GetAPIMutex());
1041 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1042 size_t num_paths
= paths
.GetSize();
1043 std::vector
<std::string
> paths_vec
;
1044 paths_vec
.reserve(num_paths
);
1045 for (size_t i
= 0; i
< num_paths
; i
++)
1046 paths_vec
.push_back(paths
.GetStringAtIndex(i
));
1047 FileSpec loaded_spec
;
1049 uint32_t token
= platform_sp
->LoadImageUsingPaths(
1050 process_sp
.get(), *image_spec
, paths_vec
, error
.ref(), &loaded_spec
);
1051 if (token
!= LLDB_INVALID_IMAGE_TOKEN
)
1052 loaded_path
= loaded_spec
;
1055 error
.SetErrorString("process is running");
1058 error
.SetErrorString("process is invalid");
1061 return LLDB_INVALID_IMAGE_TOKEN
;
1064 lldb::SBError
SBProcess::UnloadImage(uint32_t image_token
) {
1065 LLDB_INSTRUMENT_VA(this, image_token
);
1067 lldb::SBError sb_error
;
1068 ProcessSP
process_sp(GetSP());
1070 Process::StopLocker stop_locker
;
1071 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1072 std::lock_guard
<std::recursive_mutex
> guard(
1073 process_sp
->GetTarget().GetAPIMutex());
1074 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1076 platform_sp
->UnloadImage(process_sp
.get(), image_token
));
1078 sb_error
.SetErrorString("process is running");
1081 sb_error
.SetErrorString("invalid process");
1085 lldb::SBError
SBProcess::SendEventData(const char *event_data
) {
1086 LLDB_INSTRUMENT_VA(this, event_data
);
1088 lldb::SBError sb_error
;
1089 ProcessSP
process_sp(GetSP());
1091 Process::StopLocker stop_locker
;
1092 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1093 std::lock_guard
<std::recursive_mutex
> guard(
1094 process_sp
->GetTarget().GetAPIMutex());
1095 sb_error
.SetError(process_sp
->SendEventData(event_data
));
1097 sb_error
.SetErrorString("process is running");
1100 sb_error
.SetErrorString("invalid process");
1104 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1105 LLDB_INSTRUMENT_VA(this);
1107 ProcessSP
process_sp(GetSP());
1108 if (process_sp
&& process_sp
->GetSystemRuntime()) {
1109 SystemRuntime
*runtime
= process_sp
->GetSystemRuntime();
1110 return runtime
->GetExtendedBacktraceTypes().size();
1115 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx
) {
1116 LLDB_INSTRUMENT_VA(this, idx
);
1118 ProcessSP
process_sp(GetSP());
1119 if (process_sp
&& process_sp
->GetSystemRuntime()) {
1120 SystemRuntime
*runtime
= process_sp
->GetSystemRuntime();
1121 const std::vector
<ConstString
> &names
=
1122 runtime
->GetExtendedBacktraceTypes();
1123 if (idx
< names
.size()) {
1124 return names
[idx
].AsCString();
1130 SBThreadCollection
SBProcess::GetHistoryThreads(addr_t addr
) {
1131 LLDB_INSTRUMENT_VA(this, addr
);
1133 ProcessSP
process_sp(GetSP());
1134 SBThreadCollection threads
;
1136 threads
= SBThreadCollection(process_sp
->GetHistoryThreads(addr
));
1141 bool SBProcess::IsInstrumentationRuntimePresent(
1142 InstrumentationRuntimeType type
) {
1143 LLDB_INSTRUMENT_VA(this, type
);
1145 ProcessSP
process_sp(GetSP());
1149 std::lock_guard
<std::recursive_mutex
> guard(
1150 process_sp
->GetTarget().GetAPIMutex());
1152 InstrumentationRuntimeSP runtime_sp
=
1153 process_sp
->GetInstrumentationRuntime(type
);
1155 if (!runtime_sp
.get())
1158 return runtime_sp
->IsActive();
1161 lldb::SBError
SBProcess::SaveCore(const char *file_name
) {
1162 LLDB_INSTRUMENT_VA(this, file_name
);
1163 return SaveCore(file_name
, "", SaveCoreStyle::eSaveCoreFull
);
1166 lldb::SBError
SBProcess::SaveCore(const char *file_name
,
1168 SaveCoreStyle core_style
) {
1169 LLDB_INSTRUMENT_VA(this, file_name
, flavor
, core_style
);
1171 lldb::SBError error
;
1172 ProcessSP
process_sp(GetSP());
1174 error
.SetErrorString("SBProcess is invalid");
1178 std::lock_guard
<std::recursive_mutex
> guard(
1179 process_sp
->GetTarget().GetAPIMutex());
1181 if (process_sp
->GetState() != eStateStopped
) {
1182 error
.SetErrorString("the process is not stopped");
1186 FileSpec
core_file(file_name
);
1187 FileSystem::Instance().Resolve(core_file
);
1188 error
.ref() = PluginManager::SaveCore(process_sp
, core_file
, core_style
,
1195 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr
,
1196 SBMemoryRegionInfo
&sb_region_info
) {
1197 LLDB_INSTRUMENT_VA(this, load_addr
, sb_region_info
);
1199 lldb::SBError sb_error
;
1200 ProcessSP
process_sp(GetSP());
1202 Process::StopLocker stop_locker
;
1203 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1204 std::lock_guard
<std::recursive_mutex
> guard(
1205 process_sp
->GetTarget().GetAPIMutex());
1208 process_sp
->GetMemoryRegionInfo(load_addr
, sb_region_info
.ref());
1210 sb_error
.SetErrorString("process is running");
1213 sb_error
.SetErrorString("SBProcess is invalid");
1218 lldb::SBMemoryRegionInfoList
SBProcess::GetMemoryRegions() {
1219 LLDB_INSTRUMENT_VA(this);
1221 lldb::SBMemoryRegionInfoList sb_region_list
;
1223 ProcessSP
process_sp(GetSP());
1224 Process::StopLocker stop_locker
;
1225 if (process_sp
&& stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1226 std::lock_guard
<std::recursive_mutex
> guard(
1227 process_sp
->GetTarget().GetAPIMutex());
1229 process_sp
->GetMemoryRegions(sb_region_list
.ref());
1232 return sb_region_list
;
1235 lldb::SBProcessInfo
SBProcess::GetProcessInfo() {
1236 LLDB_INSTRUMENT_VA(this);
1238 lldb::SBProcessInfo sb_proc_info
;
1239 ProcessSP
process_sp(GetSP());
1240 ProcessInstanceInfo proc_info
;
1241 if (process_sp
&& process_sp
->GetProcessInfo(proc_info
)) {
1242 sb_proc_info
.SetProcessInfo(proc_info
);
1244 return sb_proc_info
;
1247 lldb::addr_t
SBProcess::AllocateMemory(size_t size
, uint32_t permissions
,
1248 lldb::SBError
&sb_error
) {
1249 LLDB_INSTRUMENT_VA(this, size
, permissions
, sb_error
);
1251 lldb::addr_t addr
= LLDB_INVALID_ADDRESS
;
1252 ProcessSP
process_sp(GetSP());
1254 Process::StopLocker stop_locker
;
1255 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1256 std::lock_guard
<std::recursive_mutex
> guard(
1257 process_sp
->GetTarget().GetAPIMutex());
1258 addr
= process_sp
->AllocateMemory(size
, permissions
, sb_error
.ref());
1260 sb_error
.SetErrorString("process is running");
1263 sb_error
.SetErrorString("SBProcess is invalid");
1268 lldb::SBError
SBProcess::DeallocateMemory(lldb::addr_t ptr
) {
1269 LLDB_INSTRUMENT_VA(this, ptr
);
1271 lldb::SBError sb_error
;
1272 ProcessSP
process_sp(GetSP());
1274 Process::StopLocker stop_locker
;
1275 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1276 std::lock_guard
<std::recursive_mutex
> guard(
1277 process_sp
->GetTarget().GetAPIMutex());
1278 Status error
= process_sp
->DeallocateMemory(ptr
);
1279 sb_error
.SetError(error
);
1281 sb_error
.SetErrorString("process is running");
1284 sb_error
.SetErrorString("SBProcess is invalid");
1289 lldb::SBScriptObject
SBProcess::GetScriptedImplementation() {
1290 LLDB_INSTRUMENT_VA(this);
1291 ProcessSP
process_sp(GetSP());
1292 return lldb::SBScriptObject((process_sp
) ? process_sp
->GetImplementation()
1294 eScriptLanguageDefault
);