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/AddressRangeListImpl.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/StructuredDataImpl.h"
22 #include "lldb/Host/StreamFile.h"
23 #include "lldb/Target/MemoryRegionInfo.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/SystemRuntime.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 #include "lldb/Utility/Args.h"
30 #include "lldb/Utility/LLDBLog.h"
31 #include "lldb/Utility/ProcessInfo.h"
32 #include "lldb/Utility/State.h"
33 #include "lldb/Utility/Stream.h"
35 #include "lldb/API/SBBroadcaster.h"
36 #include "lldb/API/SBCommandReturnObject.h"
37 #include "lldb/API/SBDebugger.h"
38 #include "lldb/API/SBEvent.h"
39 #include "lldb/API/SBFile.h"
40 #include "lldb/API/SBFileSpec.h"
41 #include "lldb/API/SBMemoryRegionInfo.h"
42 #include "lldb/API/SBMemoryRegionInfoList.h"
43 #include "lldb/API/SBSaveCoreOptions.h"
44 #include "lldb/API/SBScriptObject.h"
45 #include "lldb/API/SBStream.h"
46 #include "lldb/API/SBStringList.h"
47 #include "lldb/API/SBStructuredData.h"
48 #include "lldb/API/SBThread.h"
49 #include "lldb/API/SBThreadCollection.h"
50 #include "lldb/API/SBTrace.h"
51 #include "lldb/API/SBUnixSignals.h"
54 using namespace lldb_private
;
56 SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }
58 // SBProcess constructor
60 SBProcess::SBProcess(const SBProcess
&rhs
) : m_opaque_wp(rhs
.m_opaque_wp
) {
61 LLDB_INSTRUMENT_VA(this, rhs
);
64 SBProcess::SBProcess(const lldb::ProcessSP
&process_sp
)
65 : m_opaque_wp(process_sp
) {
66 LLDB_INSTRUMENT_VA(this, process_sp
);
69 const SBProcess
&SBProcess::operator=(const SBProcess
&rhs
) {
70 LLDB_INSTRUMENT_VA(this, rhs
);
73 m_opaque_wp
= rhs
.m_opaque_wp
;
78 SBProcess::~SBProcess() = default;
80 const char *SBProcess::GetBroadcasterClassName() {
83 return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
86 const char *SBProcess::GetPluginName() {
87 LLDB_INSTRUMENT_VA(this);
89 ProcessSP
process_sp(GetSP());
91 return ConstString(process_sp
->GetPluginName()).GetCString();
96 const char *SBProcess::GetShortPluginName() {
97 LLDB_INSTRUMENT_VA(this);
99 ProcessSP
process_sp(GetSP());
101 return ConstString(process_sp
->GetPluginName()).GetCString();
106 lldb::ProcessSP
SBProcess::GetSP() const { return m_opaque_wp
.lock(); }
108 void SBProcess::SetSP(const ProcessSP
&process_sp
) { m_opaque_wp
= process_sp
; }
110 void SBProcess::Clear() {
111 LLDB_INSTRUMENT_VA(this);
116 bool SBProcess::IsValid() const {
117 LLDB_INSTRUMENT_VA(this);
118 return this->operator bool();
120 SBProcess::operator bool() const {
121 LLDB_INSTRUMENT_VA(this);
123 ProcessSP
process_sp(m_opaque_wp
.lock());
124 return ((bool)process_sp
&& process_sp
->IsValid());
127 bool SBProcess::RemoteLaunch(char const **argv
, char const **envp
,
128 const char *stdin_path
, const char *stdout_path
,
129 const char *stderr_path
,
130 const char *working_directory
,
131 uint32_t launch_flags
, bool stop_at_entry
,
132 lldb::SBError
&error
) {
133 LLDB_INSTRUMENT_VA(this, argv
, envp
, stdin_path
, stdout_path
, stderr_path
,
134 working_directory
, launch_flags
, stop_at_entry
, error
);
136 ProcessSP
process_sp(GetSP());
138 std::lock_guard
<std::recursive_mutex
> guard(
139 process_sp
->GetTarget().GetAPIMutex());
140 if (process_sp
->GetState() == eStateConnected
) {
142 launch_flags
|= eLaunchFlagStopAtEntry
;
143 ProcessLaunchInfo
launch_info(FileSpec(stdin_path
), FileSpec(stdout_path
),
144 FileSpec(stderr_path
),
145 FileSpec(working_directory
), launch_flags
);
146 Module
*exe_module
= process_sp
->GetTarget().GetExecutableModulePointer();
148 launch_info
.SetExecutableFile(exe_module
->GetPlatformFileSpec(), true);
150 launch_info
.GetArguments().AppendArguments(argv
);
152 launch_info
.GetEnvironment() = Environment(envp
);
153 error
.SetError(process_sp
->Launch(launch_info
));
155 error
= Status::FromErrorString(
156 "must be in eStateConnected to call RemoteLaunch");
159 error
= Status::FromErrorString("unable to attach pid");
162 return error
.Success();
165 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid
,
166 lldb::SBError
&error
) {
167 LLDB_INSTRUMENT_VA(this, pid
, error
);
169 ProcessSP
process_sp(GetSP());
171 std::lock_guard
<std::recursive_mutex
> guard(
172 process_sp
->GetTarget().GetAPIMutex());
173 if (process_sp
->GetState() == eStateConnected
) {
174 ProcessAttachInfo attach_info
;
175 attach_info
.SetProcessID(pid
);
176 error
.SetError(process_sp
->Attach(attach_info
));
178 error
= Status::FromErrorString(
179 "must be in eStateConnected to call RemoteAttachToProcessWithID");
182 error
= Status::FromErrorString("unable to attach pid");
185 return error
.Success();
188 uint32_t SBProcess::GetNumThreads() {
189 LLDB_INSTRUMENT_VA(this);
191 uint32_t num_threads
= 0;
192 ProcessSP
process_sp(GetSP());
194 Process::StopLocker stop_locker
;
196 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
197 std::lock_guard
<std::recursive_mutex
> guard(
198 process_sp
->GetTarget().GetAPIMutex());
199 num_threads
= process_sp
->GetThreadList().GetSize(can_update
);
205 SBThread
SBProcess::GetSelectedThread() const {
206 LLDB_INSTRUMENT_VA(this);
210 ProcessSP
process_sp(GetSP());
212 std::lock_guard
<std::recursive_mutex
> guard(
213 process_sp
->GetTarget().GetAPIMutex());
214 thread_sp
= process_sp
->GetThreadList().GetSelectedThread();
215 sb_thread
.SetThread(thread_sp
);
221 SBThread
SBProcess::CreateOSPluginThread(lldb::tid_t tid
,
222 lldb::addr_t context
) {
223 LLDB_INSTRUMENT_VA(this, tid
, context
);
227 ProcessSP
process_sp(GetSP());
229 std::lock_guard
<std::recursive_mutex
> guard(
230 process_sp
->GetTarget().GetAPIMutex());
231 thread_sp
= process_sp
->CreateOSPluginThread(tid
, context
);
232 sb_thread
.SetThread(thread_sp
);
238 SBTarget
SBProcess::GetTarget() const {
239 LLDB_INSTRUMENT_VA(this);
243 ProcessSP
process_sp(GetSP());
245 target_sp
= process_sp
->GetTarget().shared_from_this();
246 sb_target
.SetSP(target_sp
);
252 size_t SBProcess::PutSTDIN(const char *src
, size_t src_len
) {
253 LLDB_INSTRUMENT_VA(this, src
, src_len
);
256 ProcessSP
process_sp(GetSP());
259 ret_val
= process_sp
->PutSTDIN(src
, src_len
, error
);
265 size_t SBProcess::GetSTDOUT(char *dst
, size_t dst_len
) const {
266 LLDB_INSTRUMENT_VA(this, dst
, dst_len
);
268 size_t bytes_read
= 0;
269 ProcessSP
process_sp(GetSP());
272 bytes_read
= process_sp
->GetSTDOUT(dst
, dst_len
, error
);
278 size_t SBProcess::GetSTDERR(char *dst
, size_t dst_len
) const {
279 LLDB_INSTRUMENT_VA(this, dst
, dst_len
);
281 size_t bytes_read
= 0;
282 ProcessSP
process_sp(GetSP());
285 bytes_read
= process_sp
->GetSTDERR(dst
, dst_len
, error
);
291 size_t SBProcess::GetAsyncProfileData(char *dst
, size_t dst_len
) const {
292 LLDB_INSTRUMENT_VA(this, dst
, dst_len
);
294 size_t bytes_read
= 0;
295 ProcessSP
process_sp(GetSP());
298 bytes_read
= process_sp
->GetAsyncProfileData(dst
, dst_len
, error
);
304 void SBProcess::ReportEventState(const SBEvent
&event
, SBFile out
) const {
305 LLDB_INSTRUMENT_VA(this, event
, out
);
307 return ReportEventState(event
, out
.m_opaque_sp
);
310 void SBProcess::ReportEventState(const SBEvent
&event
, FILE *out
) const {
311 LLDB_INSTRUMENT_VA(this, event
, out
);
312 FileSP outfile
= std::make_shared
<NativeFile
>(out
, false);
313 return ReportEventState(event
, outfile
);
316 void SBProcess::ReportEventState(const SBEvent
&event
, FileSP out
) const {
318 LLDB_INSTRUMENT_VA(this, event
, out
);
320 if (!out
|| !out
->IsValid())
323 ProcessSP
process_sp(GetSP());
325 StreamFile
stream(out
);
326 const StateType event_state
= SBProcess::GetStateFromEvent(event
);
327 stream
.Printf("Process %" PRIu64
" %s\n", process_sp
->GetID(),
328 SBDebugger::StateAsCString(event_state
));
332 void SBProcess::AppendEventStateReport(const SBEvent
&event
,
333 SBCommandReturnObject
&result
) {
334 LLDB_INSTRUMENT_VA(this, event
, result
);
336 ProcessSP
process_sp(GetSP());
338 const StateType event_state
= SBProcess::GetStateFromEvent(event
);
340 ::snprintf(message
, sizeof(message
), "Process %" PRIu64
" %s\n",
341 process_sp
->GetID(), SBDebugger::StateAsCString(event_state
));
343 result
.AppendMessage(message
);
347 bool SBProcess::SetSelectedThread(const SBThread
&thread
) {
348 LLDB_INSTRUMENT_VA(this, thread
);
350 ProcessSP
process_sp(GetSP());
352 std::lock_guard
<std::recursive_mutex
> guard(
353 process_sp
->GetTarget().GetAPIMutex());
354 return process_sp
->GetThreadList().SetSelectedThreadByID(
355 thread
.GetThreadID());
360 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid
) {
361 LLDB_INSTRUMENT_VA(this, tid
);
363 bool ret_val
= false;
364 ProcessSP
process_sp(GetSP());
366 std::lock_guard
<std::recursive_mutex
> guard(
367 process_sp
->GetTarget().GetAPIMutex());
368 ret_val
= process_sp
->GetThreadList().SetSelectedThreadByID(tid
);
374 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id
) {
375 LLDB_INSTRUMENT_VA(this, index_id
);
377 bool ret_val
= false;
378 ProcessSP
process_sp(GetSP());
380 std::lock_guard
<std::recursive_mutex
> guard(
381 process_sp
->GetTarget().GetAPIMutex());
382 ret_val
= process_sp
->GetThreadList().SetSelectedThreadByIndexID(index_id
);
388 SBThread
SBProcess::GetThreadAtIndex(size_t index
) {
389 LLDB_INSTRUMENT_VA(this, index
);
393 ProcessSP
process_sp(GetSP());
395 Process::StopLocker stop_locker
;
396 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
397 std::lock_guard
<std::recursive_mutex
> guard(
398 process_sp
->GetTarget().GetAPIMutex());
399 thread_sp
= process_sp
->GetThreadList().GetThreadAtIndex(index
, can_update
);
400 sb_thread
.SetThread(thread_sp
);
406 uint32_t SBProcess::GetNumQueues() {
407 LLDB_INSTRUMENT_VA(this);
409 uint32_t num_queues
= 0;
410 ProcessSP
process_sp(GetSP());
412 Process::StopLocker stop_locker
;
413 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
414 std::lock_guard
<std::recursive_mutex
> guard(
415 process_sp
->GetTarget().GetAPIMutex());
416 num_queues
= process_sp
->GetQueueList().GetSize();
423 SBQueue
SBProcess::GetQueueAtIndex(size_t index
) {
424 LLDB_INSTRUMENT_VA(this, index
);
428 ProcessSP
process_sp(GetSP());
430 Process::StopLocker stop_locker
;
431 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
432 std::lock_guard
<std::recursive_mutex
> guard(
433 process_sp
->GetTarget().GetAPIMutex());
434 queue_sp
= process_sp
->GetQueueList().GetQueueAtIndex(index
);
435 sb_queue
.SetQueue(queue_sp
);
442 uint32_t SBProcess::GetStopID(bool include_expression_stops
) {
443 LLDB_INSTRUMENT_VA(this, include_expression_stops
);
445 ProcessSP
process_sp(GetSP());
447 std::lock_guard
<std::recursive_mutex
> guard(
448 process_sp
->GetTarget().GetAPIMutex());
449 if (include_expression_stops
)
450 return process_sp
->GetStopID();
452 return process_sp
->GetLastNaturalStopID();
457 SBEvent
SBProcess::GetStopEventForStopID(uint32_t stop_id
) {
458 LLDB_INSTRUMENT_VA(this, stop_id
);
462 ProcessSP
process_sp(GetSP());
464 std::lock_guard
<std::recursive_mutex
> guard(
465 process_sp
->GetTarget().GetAPIMutex());
466 event_sp
= process_sp
->GetStopEventForStopID(stop_id
);
467 sb_event
.reset(event_sp
);
473 void SBProcess::ForceScriptedState(StateType new_state
) {
474 LLDB_INSTRUMENT_VA(this, new_state
);
476 if (ProcessSP process_sp
= GetSP()) {
477 std::lock_guard
<std::recursive_mutex
> guard(
478 process_sp
->GetTarget().GetAPIMutex());
479 process_sp
->ForceScriptedState(new_state
);
483 StateType
SBProcess::GetState() {
484 LLDB_INSTRUMENT_VA(this);
486 StateType ret_val
= eStateInvalid
;
487 ProcessSP
process_sp(GetSP());
489 std::lock_guard
<std::recursive_mutex
> guard(
490 process_sp
->GetTarget().GetAPIMutex());
491 ret_val
= process_sp
->GetState();
497 int SBProcess::GetExitStatus() {
498 LLDB_INSTRUMENT_VA(this);
501 ProcessSP
process_sp(GetSP());
503 std::lock_guard
<std::recursive_mutex
> guard(
504 process_sp
->GetTarget().GetAPIMutex());
505 exit_status
= process_sp
->GetExitStatus();
511 const char *SBProcess::GetExitDescription() {
512 LLDB_INSTRUMENT_VA(this);
514 ProcessSP
process_sp(GetSP());
518 std::lock_guard
<std::recursive_mutex
> guard(
519 process_sp
->GetTarget().GetAPIMutex());
520 return ConstString(process_sp
->GetExitDescription()).GetCString();
523 lldb::pid_t
SBProcess::GetProcessID() {
524 LLDB_INSTRUMENT_VA(this);
526 lldb::pid_t ret_val
= LLDB_INVALID_PROCESS_ID
;
527 ProcessSP
process_sp(GetSP());
529 ret_val
= process_sp
->GetID();
534 uint32_t SBProcess::GetUniqueID() {
535 LLDB_INSTRUMENT_VA(this);
537 uint32_t ret_val
= 0;
538 ProcessSP
process_sp(GetSP());
540 ret_val
= process_sp
->GetUniqueID();
544 ByteOrder
SBProcess::GetByteOrder() const {
545 LLDB_INSTRUMENT_VA(this);
547 ByteOrder byteOrder
= eByteOrderInvalid
;
548 ProcessSP
process_sp(GetSP());
550 byteOrder
= process_sp
->GetTarget().GetArchitecture().GetByteOrder();
555 uint32_t SBProcess::GetAddressByteSize() const {
556 LLDB_INSTRUMENT_VA(this);
559 ProcessSP
process_sp(GetSP());
561 size
= process_sp
->GetTarget().GetArchitecture().GetAddressByteSize();
566 SBError
SBProcess::Continue() {
567 LLDB_INSTRUMENT_VA(this);
570 ProcessSP
process_sp(GetSP());
573 std::lock_guard
<std::recursive_mutex
> guard(
574 process_sp
->GetTarget().GetAPIMutex());
576 if (process_sp
->GetTarget().GetDebugger().GetAsyncExecution())
577 sb_error
.ref() = process_sp
->Resume();
579 sb_error
.ref() = process_sp
->ResumeSynchronous(nullptr);
581 sb_error
= Status::FromErrorString("SBProcess is invalid");
586 SBError
SBProcess::Destroy() {
587 LLDB_INSTRUMENT_VA(this);
590 ProcessSP
process_sp(GetSP());
592 std::lock_guard
<std::recursive_mutex
> guard(
593 process_sp
->GetTarget().GetAPIMutex());
594 sb_error
.SetError(process_sp
->Destroy(false));
596 sb_error
= Status::FromErrorString("SBProcess is invalid");
601 SBError
SBProcess::Stop() {
602 LLDB_INSTRUMENT_VA(this);
605 ProcessSP
process_sp(GetSP());
607 std::lock_guard
<std::recursive_mutex
> guard(
608 process_sp
->GetTarget().GetAPIMutex());
609 sb_error
.SetError(process_sp
->Halt());
611 sb_error
= Status::FromErrorString("SBProcess is invalid");
616 SBError
SBProcess::Kill() {
617 LLDB_INSTRUMENT_VA(this);
620 ProcessSP
process_sp(GetSP());
622 std::lock_guard
<std::recursive_mutex
> guard(
623 process_sp
->GetTarget().GetAPIMutex());
624 sb_error
.SetError(process_sp
->Destroy(true));
626 sb_error
= Status::FromErrorString("SBProcess is invalid");
631 SBError
SBProcess::Detach() {
632 LLDB_INSTRUMENT_VA(this);
634 // FIXME: This should come from a process default.
635 bool keep_stopped
= false;
636 return Detach(keep_stopped
);
639 SBError
SBProcess::Detach(bool keep_stopped
) {
640 LLDB_INSTRUMENT_VA(this, keep_stopped
);
643 ProcessSP
process_sp(GetSP());
645 std::lock_guard
<std::recursive_mutex
> guard(
646 process_sp
->GetTarget().GetAPIMutex());
647 sb_error
.SetError(process_sp
->Detach(keep_stopped
));
649 sb_error
= Status::FromErrorString("SBProcess is invalid");
654 SBError
SBProcess::Signal(int signo
) {
655 LLDB_INSTRUMENT_VA(this, signo
);
658 ProcessSP
process_sp(GetSP());
660 std::lock_guard
<std::recursive_mutex
> guard(
661 process_sp
->GetTarget().GetAPIMutex());
662 sb_error
.SetError(process_sp
->Signal(signo
));
664 sb_error
= Status::FromErrorString("SBProcess is invalid");
669 SBUnixSignals
SBProcess::GetUnixSignals() {
670 LLDB_INSTRUMENT_VA(this);
672 if (auto process_sp
= GetSP())
673 return SBUnixSignals
{process_sp
};
675 return SBUnixSignals
{};
678 void SBProcess::SendAsyncInterrupt() {
679 LLDB_INSTRUMENT_VA(this);
681 ProcessSP
process_sp(GetSP());
683 process_sp
->SendAsyncInterrupt();
687 SBThread
SBProcess::GetThreadByID(tid_t tid
) {
688 LLDB_INSTRUMENT_VA(this, tid
);
692 ProcessSP
process_sp(GetSP());
694 Process::StopLocker stop_locker
;
695 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
696 std::lock_guard
<std::recursive_mutex
> guard(
697 process_sp
->GetTarget().GetAPIMutex());
698 thread_sp
= process_sp
->GetThreadList().FindThreadByID(tid
, can_update
);
699 sb_thread
.SetThread(thread_sp
);
705 SBThread
SBProcess::GetThreadByIndexID(uint32_t index_id
) {
706 LLDB_INSTRUMENT_VA(this, index_id
);
710 ProcessSP
process_sp(GetSP());
712 Process::StopLocker stop_locker
;
713 const bool can_update
= stop_locker
.TryLock(&process_sp
->GetRunLock());
714 std::lock_guard
<std::recursive_mutex
> guard(
715 process_sp
->GetTarget().GetAPIMutex());
717 process_sp
->GetThreadList().FindThreadByIndexID(index_id
, can_update
);
718 sb_thread
.SetThread(thread_sp
);
724 StateType
SBProcess::GetStateFromEvent(const SBEvent
&event
) {
725 LLDB_INSTRUMENT_VA(event
);
727 StateType ret_val
= Process::ProcessEventData::GetStateFromEvent(event
.get());
732 bool SBProcess::GetRestartedFromEvent(const SBEvent
&event
) {
733 LLDB_INSTRUMENT_VA(event
);
735 bool ret_val
= Process::ProcessEventData::GetRestartedFromEvent(event
.get());
740 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent
&event
) {
741 LLDB_INSTRUMENT_VA(event
);
743 return Process::ProcessEventData::GetNumRestartedReasons(event
.get());
747 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent
&event
,
749 LLDB_INSTRUMENT_VA(event
, idx
);
751 return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex(
756 SBProcess
SBProcess::GetProcessFromEvent(const SBEvent
&event
) {
757 LLDB_INSTRUMENT_VA(event
);
759 ProcessSP process_sp
=
760 Process::ProcessEventData::GetProcessFromEvent(event
.get());
762 // StructuredData events also know the process they come from. Try that.
763 process_sp
= EventDataStructuredData::GetProcessFromEvent(event
.get());
766 return SBProcess(process_sp
);
769 bool SBProcess::GetInterruptedFromEvent(const SBEvent
&event
) {
770 LLDB_INSTRUMENT_VA(event
);
772 return Process::ProcessEventData::GetInterruptedFromEvent(event
.get());
775 lldb::SBStructuredData
776 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent
&event
) {
777 LLDB_INSTRUMENT_VA(event
);
779 return SBStructuredData(event
.GetSP());
782 bool SBProcess::EventIsProcessEvent(const SBEvent
&event
) {
783 LLDB_INSTRUMENT_VA(event
);
785 return Process::ProcessEventData::GetEventDataFromEvent(event
.get()) !=
789 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent
&event
) {
790 LLDB_INSTRUMENT_VA(event
);
792 EventSP event_sp
= event
.GetSP();
793 EventData
*event_data
= event_sp
? event_sp
->GetData() : nullptr;
794 return event_data
&& (event_data
->GetFlavor() ==
795 EventDataStructuredData::GetFlavorString());
798 SBBroadcaster
SBProcess::GetBroadcaster() const {
799 LLDB_INSTRUMENT_VA(this);
801 ProcessSP
process_sp(GetSP());
803 SBBroadcaster
broadcaster(process_sp
.get(), false);
808 const char *SBProcess::GetBroadcasterClass() {
811 return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
814 lldb::SBAddressRangeList
SBProcess::FindRangesInMemory(
815 const void *buf
, uint64_t size
, const SBAddressRangeList
&ranges
,
816 uint32_t alignment
, uint32_t max_matches
, SBError
&error
) {
817 LLDB_INSTRUMENT_VA(this, buf
, size
, ranges
, alignment
, max_matches
, error
);
819 lldb::SBAddressRangeList matches
;
821 ProcessSP
process_sp(GetSP());
823 error
= Status::FromErrorString("SBProcess is invalid");
826 Process::StopLocker stop_locker
;
827 if (!stop_locker
.TryLock(&process_sp
->GetRunLock())) {
828 error
= Status::FromErrorString("process is running");
831 std::lock_guard
<std::recursive_mutex
> guard(
832 process_sp
->GetTarget().GetAPIMutex());
833 matches
.m_opaque_up
->ref() = process_sp
->FindRangesInMemory(
834 reinterpret_cast<const uint8_t *>(buf
), size
, ranges
.ref().ref(),
835 alignment
, max_matches
, error
.ref());
839 lldb::addr_t
SBProcess::FindInMemory(const void *buf
, uint64_t size
,
840 const SBAddressRange
&range
,
841 uint32_t alignment
, SBError
&error
) {
842 LLDB_INSTRUMENT_VA(this, buf
, size
, range
, alignment
, error
);
844 ProcessSP
process_sp(GetSP());
847 error
= Status::FromErrorString("SBProcess is invalid");
848 return LLDB_INVALID_ADDRESS
;
851 Process::StopLocker stop_locker
;
852 if (!stop_locker
.TryLock(&process_sp
->GetRunLock())) {
853 error
= Status::FromErrorString("process is running");
854 return LLDB_INVALID_ADDRESS
;
857 std::lock_guard
<std::recursive_mutex
> guard(
858 process_sp
->GetTarget().GetAPIMutex());
859 return process_sp
->FindInMemory(reinterpret_cast<const uint8_t *>(buf
), size
,
860 range
.ref(), alignment
, error
.ref());
863 size_t SBProcess::ReadMemory(addr_t addr
, void *dst
, size_t dst_len
,
865 LLDB_INSTRUMENT_VA(this, addr
, dst
, dst_len
, sb_error
);
868 sb_error
= Status::FromErrorStringWithFormat(
869 "no buffer provided to read %zu bytes into", dst_len
);
873 size_t bytes_read
= 0;
874 ProcessSP
process_sp(GetSP());
878 Process::StopLocker stop_locker
;
879 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
880 std::lock_guard
<std::recursive_mutex
> guard(
881 process_sp
->GetTarget().GetAPIMutex());
882 bytes_read
= process_sp
->ReadMemory(addr
, dst
, dst_len
, sb_error
.ref());
884 sb_error
= Status::FromErrorString("process is running");
887 sb_error
= Status::FromErrorString("SBProcess is invalid");
893 size_t SBProcess::ReadCStringFromMemory(addr_t addr
, void *buf
, size_t size
,
894 lldb::SBError
&sb_error
) {
895 LLDB_INSTRUMENT_VA(this, addr
, buf
, size
, sb_error
);
897 size_t bytes_read
= 0;
898 ProcessSP
process_sp(GetSP());
900 Process::StopLocker stop_locker
;
901 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
902 std::lock_guard
<std::recursive_mutex
> guard(
903 process_sp
->GetTarget().GetAPIMutex());
904 bytes_read
= process_sp
->ReadCStringFromMemory(addr
, (char *)buf
, size
,
907 sb_error
= Status::FromErrorString("process is running");
910 sb_error
= Status::FromErrorString("SBProcess is invalid");
915 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr
, uint32_t byte_size
,
916 lldb::SBError
&sb_error
) {
917 LLDB_INSTRUMENT_VA(this, addr
, byte_size
, sb_error
);
920 ProcessSP
process_sp(GetSP());
922 Process::StopLocker stop_locker
;
923 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
924 std::lock_guard
<std::recursive_mutex
> guard(
925 process_sp
->GetTarget().GetAPIMutex());
926 value
= process_sp
->ReadUnsignedIntegerFromMemory(addr
, byte_size
, 0,
929 sb_error
= Status::FromErrorString("process is running");
932 sb_error
= Status::FromErrorString("SBProcess is invalid");
937 lldb::addr_t
SBProcess::ReadPointerFromMemory(addr_t addr
,
938 lldb::SBError
&sb_error
) {
939 LLDB_INSTRUMENT_VA(this, addr
, sb_error
);
941 lldb::addr_t ptr
= LLDB_INVALID_ADDRESS
;
942 ProcessSP
process_sp(GetSP());
944 Process::StopLocker stop_locker
;
945 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
946 std::lock_guard
<std::recursive_mutex
> guard(
947 process_sp
->GetTarget().GetAPIMutex());
948 ptr
= process_sp
->ReadPointerFromMemory(addr
, sb_error
.ref());
950 sb_error
= Status::FromErrorString("process is running");
953 sb_error
= Status::FromErrorString("SBProcess is invalid");
958 size_t SBProcess::WriteMemory(addr_t addr
, const void *src
, size_t src_len
,
960 LLDB_INSTRUMENT_VA(this, addr
, src
, src_len
, sb_error
);
962 size_t bytes_written
= 0;
964 ProcessSP
process_sp(GetSP());
967 Process::StopLocker stop_locker
;
968 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
969 std::lock_guard
<std::recursive_mutex
> guard(
970 process_sp
->GetTarget().GetAPIMutex());
972 process_sp
->WriteMemory(addr
, src
, src_len
, sb_error
.ref());
974 sb_error
= Status::FromErrorString("process is running");
978 return bytes_written
;
981 void SBProcess::GetStatus(SBStream
&status
) {
982 LLDB_INSTRUMENT_VA(this, status
);
984 ProcessSP
process_sp(GetSP());
986 process_sp
->GetStatus(status
.ref());
989 bool SBProcess::GetDescription(SBStream
&description
) {
990 LLDB_INSTRUMENT_VA(this, description
);
992 Stream
&strm
= description
.ref();
994 ProcessSP
process_sp(GetSP());
997 GetTarget().GetExecutable().GetPath(path
, sizeof(path
));
998 Module
*exe_module
= process_sp
->GetTarget().GetExecutableModulePointer();
999 const char *exe_name
= nullptr;
1001 exe_name
= exe_module
->GetFileSpec().GetFilename().AsCString();
1003 strm
.Printf("SBProcess: pid = %" PRIu64
", state = %s, threads = %d%s%s",
1004 process_sp
->GetID(), lldb_private::StateAsCString(GetState()),
1005 GetNumThreads(), exe_name
? ", executable = " : "",
1006 exe_name
? exe_name
: "");
1008 strm
.PutCString("No value");
1013 SBStructuredData
SBProcess::GetExtendedCrashInformation() {
1014 LLDB_INSTRUMENT_VA(this);
1015 SBStructuredData data
;
1016 ProcessSP
process_sp(GetSP());
1020 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1025 auto expected_data
=
1026 platform_sp
->FetchExtendedCrashInformation(*process_sp
.get());
1031 StructuredData::ObjectSP fetched_data
= *expected_data
;
1032 data
.m_impl_up
->SetObjectSP(fetched_data
);
1037 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError
&sb_error
) const {
1038 LLDB_INSTRUMENT_VA(this, sb_error
);
1041 ProcessSP
process_sp(GetSP());
1043 std::lock_guard
<std::recursive_mutex
> guard(
1044 process_sp
->GetTarget().GetAPIMutex());
1045 std::optional
<uint32_t> actual_num
= process_sp
->GetWatchpointSlotCount();
1050 Status::FromErrorString("Unable to determine number of watchpoints");
1053 sb_error
= Status::FromErrorString("SBProcess is invalid");
1058 uint32_t SBProcess::LoadImage(lldb::SBFileSpec
&sb_remote_image_spec
,
1059 lldb::SBError
&sb_error
) {
1060 LLDB_INSTRUMENT_VA(this, sb_remote_image_spec
, sb_error
);
1062 return LoadImage(SBFileSpec(), sb_remote_image_spec
, sb_error
);
1065 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec
&sb_local_image_spec
,
1066 const lldb::SBFileSpec
&sb_remote_image_spec
,
1067 lldb::SBError
&sb_error
) {
1068 LLDB_INSTRUMENT_VA(this, sb_local_image_spec
, sb_remote_image_spec
, sb_error
);
1070 ProcessSP
process_sp(GetSP());
1072 Process::StopLocker stop_locker
;
1073 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1074 std::lock_guard
<std::recursive_mutex
> guard(
1075 process_sp
->GetTarget().GetAPIMutex());
1076 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1077 return platform_sp
->LoadImage(process_sp
.get(), *sb_local_image_spec
,
1078 *sb_remote_image_spec
, sb_error
.ref());
1080 sb_error
= Status::FromErrorString("process is running");
1083 sb_error
= Status::FromErrorString("process is invalid");
1085 return LLDB_INVALID_IMAGE_TOKEN
;
1088 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec
&image_spec
,
1089 SBStringList
&paths
,
1090 lldb::SBFileSpec
&loaded_path
,
1091 lldb::SBError
&error
) {
1092 LLDB_INSTRUMENT_VA(this, image_spec
, paths
, loaded_path
, error
);
1094 ProcessSP
process_sp(GetSP());
1096 Process::StopLocker stop_locker
;
1097 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1098 std::lock_guard
<std::recursive_mutex
> guard(
1099 process_sp
->GetTarget().GetAPIMutex());
1100 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1101 size_t num_paths
= paths
.GetSize();
1102 std::vector
<std::string
> paths_vec
;
1103 paths_vec
.reserve(num_paths
);
1104 for (size_t i
= 0; i
< num_paths
; i
++)
1105 paths_vec
.push_back(paths
.GetStringAtIndex(i
));
1106 FileSpec loaded_spec
;
1108 uint32_t token
= platform_sp
->LoadImageUsingPaths(
1109 process_sp
.get(), *image_spec
, paths_vec
, error
.ref(), &loaded_spec
);
1110 if (token
!= LLDB_INVALID_IMAGE_TOKEN
)
1111 loaded_path
= loaded_spec
;
1114 error
= Status::FromErrorString("process is running");
1117 error
= Status::FromErrorString("process is invalid");
1120 return LLDB_INVALID_IMAGE_TOKEN
;
1123 lldb::SBError
SBProcess::UnloadImage(uint32_t image_token
) {
1124 LLDB_INSTRUMENT_VA(this, image_token
);
1126 lldb::SBError sb_error
;
1127 ProcessSP
process_sp(GetSP());
1129 Process::StopLocker stop_locker
;
1130 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1131 std::lock_guard
<std::recursive_mutex
> guard(
1132 process_sp
->GetTarget().GetAPIMutex());
1133 PlatformSP platform_sp
= process_sp
->GetTarget().GetPlatform();
1135 platform_sp
->UnloadImage(process_sp
.get(), image_token
));
1137 sb_error
= Status::FromErrorString("process is running");
1140 sb_error
= Status::FromErrorString("invalid process");
1144 lldb::SBError
SBProcess::SendEventData(const char *event_data
) {
1145 LLDB_INSTRUMENT_VA(this, event_data
);
1147 lldb::SBError sb_error
;
1148 ProcessSP
process_sp(GetSP());
1150 Process::StopLocker stop_locker
;
1151 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1152 std::lock_guard
<std::recursive_mutex
> guard(
1153 process_sp
->GetTarget().GetAPIMutex());
1154 sb_error
.SetError(process_sp
->SendEventData(event_data
));
1156 sb_error
= Status::FromErrorString("process is running");
1159 sb_error
= Status::FromErrorString("invalid process");
1163 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1164 LLDB_INSTRUMENT_VA(this);
1166 ProcessSP
process_sp(GetSP());
1167 if (process_sp
&& process_sp
->GetSystemRuntime()) {
1168 SystemRuntime
*runtime
= process_sp
->GetSystemRuntime();
1169 return runtime
->GetExtendedBacktraceTypes().size();
1174 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx
) {
1175 LLDB_INSTRUMENT_VA(this, idx
);
1177 ProcessSP
process_sp(GetSP());
1178 if (process_sp
&& process_sp
->GetSystemRuntime()) {
1179 SystemRuntime
*runtime
= process_sp
->GetSystemRuntime();
1180 const std::vector
<ConstString
> &names
=
1181 runtime
->GetExtendedBacktraceTypes();
1182 if (idx
< names
.size()) {
1183 return names
[idx
].AsCString();
1189 SBThreadCollection
SBProcess::GetHistoryThreads(addr_t addr
) {
1190 LLDB_INSTRUMENT_VA(this, addr
);
1192 ProcessSP
process_sp(GetSP());
1193 SBThreadCollection threads
;
1195 threads
= SBThreadCollection(process_sp
->GetHistoryThreads(addr
));
1200 bool SBProcess::IsInstrumentationRuntimePresent(
1201 InstrumentationRuntimeType type
) {
1202 LLDB_INSTRUMENT_VA(this, type
);
1204 ProcessSP
process_sp(GetSP());
1208 std::lock_guard
<std::recursive_mutex
> guard(
1209 process_sp
->GetTarget().GetAPIMutex());
1211 InstrumentationRuntimeSP runtime_sp
=
1212 process_sp
->GetInstrumentationRuntime(type
);
1214 if (!runtime_sp
.get())
1217 return runtime_sp
->IsActive();
1220 lldb::SBError
SBProcess::SaveCore(const char *file_name
) {
1221 LLDB_INSTRUMENT_VA(this, file_name
);
1222 SBSaveCoreOptions options
;
1223 options
.SetOutputFile(SBFileSpec(file_name
));
1224 options
.SetStyle(SaveCoreStyle::eSaveCoreFull
);
1225 return SaveCore(options
);
1228 lldb::SBError
SBProcess::SaveCore(const char *file_name
,
1230 SaveCoreStyle core_style
) {
1231 LLDB_INSTRUMENT_VA(this, file_name
, flavor
, core_style
);
1232 SBSaveCoreOptions options
;
1233 options
.SetOutputFile(SBFileSpec(file_name
));
1234 options
.SetStyle(core_style
);
1235 SBError error
= options
.SetPluginName(flavor
);
1238 return SaveCore(options
);
1241 lldb::SBError
SBProcess::SaveCore(SBSaveCoreOptions
&options
) {
1243 LLDB_INSTRUMENT_VA(this, options
);
1245 lldb::SBError error
;
1246 ProcessSP
process_sp(GetSP());
1248 error
= Status::FromErrorString("SBProcess is invalid");
1252 std::lock_guard
<std::recursive_mutex
> guard(
1253 process_sp
->GetTarget().GetAPIMutex());
1255 if (process_sp
->GetState() != eStateStopped
) {
1256 error
= Status::FromErrorString("the process is not stopped");
1260 error
.ref() = PluginManager::SaveCore(process_sp
, options
.ref());
1266 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr
,
1267 SBMemoryRegionInfo
&sb_region_info
) {
1268 LLDB_INSTRUMENT_VA(this, load_addr
, sb_region_info
);
1270 lldb::SBError sb_error
;
1271 ProcessSP
process_sp(GetSP());
1273 Process::StopLocker stop_locker
;
1274 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1275 std::lock_guard
<std::recursive_mutex
> guard(
1276 process_sp
->GetTarget().GetAPIMutex());
1279 process_sp
->GetMemoryRegionInfo(load_addr
, sb_region_info
.ref());
1281 sb_error
= Status::FromErrorString("process is running");
1284 sb_error
= Status::FromErrorString("SBProcess is invalid");
1289 lldb::SBMemoryRegionInfoList
SBProcess::GetMemoryRegions() {
1290 LLDB_INSTRUMENT_VA(this);
1292 lldb::SBMemoryRegionInfoList sb_region_list
;
1294 ProcessSP
process_sp(GetSP());
1295 Process::StopLocker stop_locker
;
1296 if (process_sp
&& stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1297 std::lock_guard
<std::recursive_mutex
> guard(
1298 process_sp
->GetTarget().GetAPIMutex());
1300 process_sp
->GetMemoryRegions(sb_region_list
.ref());
1303 return sb_region_list
;
1306 lldb::SBProcessInfo
SBProcess::GetProcessInfo() {
1307 LLDB_INSTRUMENT_VA(this);
1309 lldb::SBProcessInfo sb_proc_info
;
1310 ProcessSP
process_sp(GetSP());
1311 ProcessInstanceInfo proc_info
;
1312 if (process_sp
&& process_sp
->GetProcessInfo(proc_info
)) {
1313 sb_proc_info
.SetProcessInfo(proc_info
);
1315 return sb_proc_info
;
1318 lldb::SBFileSpec
SBProcess::GetCoreFile() {
1319 LLDB_INSTRUMENT_VA(this);
1321 ProcessSP
process_sp(GetSP());
1324 core_file
= process_sp
->GetCoreFile();
1326 return SBFileSpec(core_file
);
1329 addr_t
SBProcess::GetAddressMask(AddressMaskType type
,
1330 AddressMaskRange addr_range
) {
1331 LLDB_INSTRUMENT_VA(this, type
, addr_range
);
1333 if (ProcessSP process_sp
= GetSP()) {
1335 case eAddressMaskTypeCode
:
1336 if (addr_range
== eAddressMaskRangeHigh
)
1337 return process_sp
->GetHighmemCodeAddressMask();
1339 return process_sp
->GetCodeAddressMask();
1340 case eAddressMaskTypeData
:
1341 if (addr_range
== eAddressMaskRangeHigh
)
1342 return process_sp
->GetHighmemDataAddressMask();
1344 return process_sp
->GetDataAddressMask();
1345 case eAddressMaskTypeAny
:
1346 if (addr_range
== eAddressMaskRangeHigh
)
1347 return process_sp
->GetHighmemDataAddressMask();
1349 return process_sp
->GetDataAddressMask();
1352 return LLDB_INVALID_ADDRESS_MASK
;
1355 void SBProcess::SetAddressMask(AddressMaskType type
, addr_t mask
,
1356 AddressMaskRange addr_range
) {
1357 LLDB_INSTRUMENT_VA(this, type
, mask
, addr_range
);
1359 if (ProcessSP process_sp
= GetSP()) {
1361 case eAddressMaskTypeCode
:
1362 if (addr_range
== eAddressMaskRangeAll
) {
1363 process_sp
->SetCodeAddressMask(mask
);
1364 process_sp
->SetHighmemCodeAddressMask(mask
);
1365 } else if (addr_range
== eAddressMaskRangeHigh
) {
1366 process_sp
->SetHighmemCodeAddressMask(mask
);
1368 process_sp
->SetCodeAddressMask(mask
);
1371 case eAddressMaskTypeData
:
1372 if (addr_range
== eAddressMaskRangeAll
) {
1373 process_sp
->SetDataAddressMask(mask
);
1374 process_sp
->SetHighmemDataAddressMask(mask
);
1375 } else if (addr_range
== eAddressMaskRangeHigh
) {
1376 process_sp
->SetHighmemDataAddressMask(mask
);
1378 process_sp
->SetDataAddressMask(mask
);
1381 case eAddressMaskTypeAll
:
1382 if (addr_range
== eAddressMaskRangeAll
) {
1383 process_sp
->SetCodeAddressMask(mask
);
1384 process_sp
->SetDataAddressMask(mask
);
1385 process_sp
->SetHighmemCodeAddressMask(mask
);
1386 process_sp
->SetHighmemDataAddressMask(mask
);
1387 } else if (addr_range
== eAddressMaskRangeHigh
) {
1388 process_sp
->SetHighmemCodeAddressMask(mask
);
1389 process_sp
->SetHighmemDataAddressMask(mask
);
1391 process_sp
->SetCodeAddressMask(mask
);
1392 process_sp
->SetDataAddressMask(mask
);
1399 void SBProcess::SetAddressableBits(AddressMaskType type
, uint32_t num_bits
,
1400 AddressMaskRange addr_range
) {
1401 LLDB_INSTRUMENT_VA(this, type
, num_bits
, addr_range
);
1403 SetAddressMask(type
, AddressableBits::AddressableBitToMask(num_bits
),
1407 addr_t
SBProcess::FixAddress(addr_t addr
, AddressMaskType type
) {
1408 LLDB_INSTRUMENT_VA(this, addr
, type
);
1410 if (ProcessSP process_sp
= GetSP()) {
1411 if (type
== eAddressMaskTypeAny
)
1412 return process_sp
->FixAnyAddress(addr
);
1413 else if (type
== eAddressMaskTypeData
)
1414 return process_sp
->FixDataAddress(addr
);
1415 else if (type
== eAddressMaskTypeCode
)
1416 return process_sp
->FixCodeAddress(addr
);
1421 lldb::addr_t
SBProcess::AllocateMemory(size_t size
, uint32_t permissions
,
1422 lldb::SBError
&sb_error
) {
1423 LLDB_INSTRUMENT_VA(this, size
, permissions
, sb_error
);
1425 lldb::addr_t addr
= LLDB_INVALID_ADDRESS
;
1426 ProcessSP
process_sp(GetSP());
1428 Process::StopLocker stop_locker
;
1429 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1430 std::lock_guard
<std::recursive_mutex
> guard(
1431 process_sp
->GetTarget().GetAPIMutex());
1432 addr
= process_sp
->AllocateMemory(size
, permissions
, sb_error
.ref());
1434 sb_error
= Status::FromErrorString("process is running");
1437 sb_error
= Status::FromErrorString("SBProcess is invalid");
1442 lldb::SBError
SBProcess::DeallocateMemory(lldb::addr_t ptr
) {
1443 LLDB_INSTRUMENT_VA(this, ptr
);
1445 lldb::SBError sb_error
;
1446 ProcessSP
process_sp(GetSP());
1448 Process::StopLocker stop_locker
;
1449 if (stop_locker
.TryLock(&process_sp
->GetRunLock())) {
1450 std::lock_guard
<std::recursive_mutex
> guard(
1451 process_sp
->GetTarget().GetAPIMutex());
1452 Status error
= process_sp
->DeallocateMemory(ptr
);
1453 sb_error
.SetError(std::move(error
));
1455 sb_error
= Status::FromErrorString("process is running");
1458 sb_error
= Status::FromErrorString("SBProcess is invalid");
1463 lldb::SBScriptObject
SBProcess::GetScriptedImplementation() {
1464 LLDB_INSTRUMENT_VA(this);
1465 ProcessSP
process_sp(GetSP());
1466 return lldb::SBScriptObject((process_sp
) ? process_sp
->GetImplementation()
1468 eScriptLanguageDefault
);