1 //===-- SBCommunication.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/SBCommunication.h"
10 #include "lldb/API/SBBroadcaster.h"
11 #include "lldb/Core/ThreadedCommunication.h"
12 #include "lldb/Host/ConnectionFileDescriptor.h"
13 #include "lldb/Host/Host.h"
14 #include "lldb/Utility/Instrumentation.h"
17 using namespace lldb_private
;
19 SBCommunication::SBCommunication() { LLDB_INSTRUMENT_VA(this); }
21 SBCommunication::SBCommunication(const char *broadcaster_name
)
22 : m_opaque(new ThreadedCommunication(broadcaster_name
)),
23 m_opaque_owned(true) {
24 LLDB_INSTRUMENT_VA(this, broadcaster_name
);
27 SBCommunication::~SBCommunication() {
28 if (m_opaque
&& m_opaque_owned
)
31 m_opaque_owned
= false;
34 bool SBCommunication::IsValid() const {
35 LLDB_INSTRUMENT_VA(this);
36 return this->operator bool();
38 SBCommunication::operator bool() const {
39 LLDB_INSTRUMENT_VA(this);
41 return m_opaque
!= nullptr;
44 bool SBCommunication::GetCloseOnEOF() {
45 LLDB_INSTRUMENT_VA(this);
48 return m_opaque
->GetCloseOnEOF();
52 void SBCommunication::SetCloseOnEOF(bool b
) {
53 LLDB_INSTRUMENT_VA(this, b
);
56 m_opaque
->SetCloseOnEOF(b
);
59 ConnectionStatus
SBCommunication::Connect(const char *url
) {
60 LLDB_INSTRUMENT_VA(this, url
);
63 if (!m_opaque
->HasConnection())
64 m_opaque
->SetConnection(Host::CreateDefaultConnection(url
));
65 return m_opaque
->Connect(url
, nullptr);
67 return eConnectionStatusNoConnection
;
70 ConnectionStatus
SBCommunication::AdoptFileDesriptor(int fd
, bool owns_fd
) {
71 LLDB_INSTRUMENT_VA(this, fd
, owns_fd
);
73 ConnectionStatus status
= eConnectionStatusNoConnection
;
75 if (m_opaque
->HasConnection()) {
76 if (m_opaque
->IsConnected())
77 m_opaque
->Disconnect();
79 m_opaque
->SetConnection(
80 std::make_unique
<ConnectionFileDescriptor
>(fd
, owns_fd
));
81 if (m_opaque
->IsConnected())
82 status
= eConnectionStatusSuccess
;
84 status
= eConnectionStatusLostConnection
;
89 ConnectionStatus
SBCommunication::Disconnect() {
90 LLDB_INSTRUMENT_VA(this);
92 ConnectionStatus status
= eConnectionStatusNoConnection
;
94 status
= m_opaque
->Disconnect();
98 bool SBCommunication::IsConnected() const {
99 LLDB_INSTRUMENT_VA(this);
101 return m_opaque
? m_opaque
->IsConnected() : false;
104 size_t SBCommunication::Read(void *dst
, size_t dst_len
, uint32_t timeout_usec
,
105 ConnectionStatus
&status
) {
106 LLDB_INSTRUMENT_VA(this, dst
, dst_len
, timeout_usec
, status
);
108 size_t bytes_read
= 0;
109 Timeout
<std::micro
> timeout
= timeout_usec
== UINT32_MAX
110 ? Timeout
<std::micro
>(std::nullopt
)
111 : std::chrono::microseconds(timeout_usec
);
113 bytes_read
= m_opaque
->Read(dst
, dst_len
, timeout
, status
, nullptr);
115 status
= eConnectionStatusNoConnection
;
120 size_t SBCommunication::Write(const void *src
, size_t src_len
,
121 ConnectionStatus
&status
) {
122 LLDB_INSTRUMENT_VA(this, src
, src_len
, status
);
124 size_t bytes_written
= 0;
126 bytes_written
= m_opaque
->Write(src
, src_len
, status
, nullptr);
128 status
= eConnectionStatusNoConnection
;
130 return bytes_written
;
133 bool SBCommunication::ReadThreadStart() {
134 LLDB_INSTRUMENT_VA(this);
136 return m_opaque
? m_opaque
->StartReadThread() : false;
139 bool SBCommunication::ReadThreadStop() {
140 LLDB_INSTRUMENT_VA(this);
142 return m_opaque
? m_opaque
->StopReadThread() : false;
145 bool SBCommunication::ReadThreadIsRunning() {
146 LLDB_INSTRUMENT_VA(this);
148 return m_opaque
? m_opaque
->ReadThreadIsRunning() : false;
151 bool SBCommunication::SetReadThreadBytesReceivedCallback(
152 ReadThreadBytesReceived callback
, void *callback_baton
) {
153 LLDB_INSTRUMENT_VA(this, callback
, callback_baton
);
157 m_opaque
->SetReadThreadBytesReceivedCallback(callback
, callback_baton
);
163 SBBroadcaster
SBCommunication::GetBroadcaster() {
164 LLDB_INSTRUMENT_VA(this);
166 SBBroadcaster
broadcaster(m_opaque
, false);
170 const char *SBCommunication::GetBroadcasterClass() {
173 return ConstString(ThreadedCommunication::GetStaticBroadcasterClass())