1cfca06d7SDimitry Andric //===-- SBCommunication.cpp -----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBCommunication.h"
10f034231aSEd Maste #include "lldb/API/SBBroadcaster.h"
11e3b55780SDimitry Andric #include "lldb/Core/ThreadedCommunication.h"
12205afe67SEd Maste #include "lldb/Host/ConnectionFileDescriptor.h"
131b306c26SDimitry Andric #include "lldb/Host/Host.h"
146f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
15f034231aSEd Maste
16f034231aSEd Maste using namespace lldb;
17f034231aSEd Maste using namespace lldb_private;
18f034231aSEd Maste
SBCommunication()196f8fc217SDimitry Andric SBCommunication::SBCommunication() { LLDB_INSTRUMENT_VA(this); }
20f034231aSEd Maste
SBCommunication(const char * broadcaster_name)2114f1b3e8SDimitry Andric SBCommunication::SBCommunication(const char *broadcaster_name)
22e3b55780SDimitry Andric : m_opaque(new ThreadedCommunication(broadcaster_name)),
23e3b55780SDimitry Andric m_opaque_owned(true) {
246f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, broadcaster_name);
25f034231aSEd Maste }
26f034231aSEd Maste
~SBCommunication()2714f1b3e8SDimitry Andric SBCommunication::~SBCommunication() {
28f034231aSEd Maste if (m_opaque && m_opaque_owned)
29f034231aSEd Maste delete m_opaque;
305f29bb8aSDimitry Andric m_opaque = nullptr;
31f034231aSEd Maste m_opaque_owned = false;
32f034231aSEd Maste }
33f034231aSEd Maste
IsValid() const345f29bb8aSDimitry Andric bool SBCommunication::IsValid() const {
356f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
365f29bb8aSDimitry Andric return this->operator bool();
375f29bb8aSDimitry Andric }
operator bool() const385f29bb8aSDimitry Andric SBCommunication::operator bool() const {
396f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
405f29bb8aSDimitry Andric
415f29bb8aSDimitry Andric return m_opaque != nullptr;
425f29bb8aSDimitry Andric }
43f034231aSEd Maste
GetCloseOnEOF()4414f1b3e8SDimitry Andric bool SBCommunication::GetCloseOnEOF() {
456f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
465f29bb8aSDimitry Andric
47f034231aSEd Maste if (m_opaque)
48f034231aSEd Maste return m_opaque->GetCloseOnEOF();
49f034231aSEd Maste return false;
50f034231aSEd Maste }
51f034231aSEd Maste
SetCloseOnEOF(bool b)5214f1b3e8SDimitry Andric void SBCommunication::SetCloseOnEOF(bool b) {
536f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, b);
545f29bb8aSDimitry Andric
55f034231aSEd Maste if (m_opaque)
56f034231aSEd Maste m_opaque->SetCloseOnEOF(b);
57f034231aSEd Maste }
58f034231aSEd Maste
Connect(const char * url)5914f1b3e8SDimitry Andric ConnectionStatus SBCommunication::Connect(const char *url) {
606f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, url);
615f29bb8aSDimitry Andric
6214f1b3e8SDimitry Andric if (m_opaque) {
63f034231aSEd Maste if (!m_opaque->HasConnection())
64cfca06d7SDimitry Andric m_opaque->SetConnection(Host::CreateDefaultConnection(url));
655f29bb8aSDimitry Andric return m_opaque->Connect(url, nullptr);
66f034231aSEd Maste }
67f034231aSEd Maste return eConnectionStatusNoConnection;
68f034231aSEd Maste }
69f034231aSEd Maste
AdoptFileDesriptor(int fd,bool owns_fd)7014f1b3e8SDimitry Andric ConnectionStatus SBCommunication::AdoptFileDesriptor(int fd, bool owns_fd) {
716f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, fd, owns_fd);
72f034231aSEd Maste
73f034231aSEd Maste ConnectionStatus status = eConnectionStatusNoConnection;
7414f1b3e8SDimitry Andric if (m_opaque) {
7514f1b3e8SDimitry Andric if (m_opaque->HasConnection()) {
76f034231aSEd Maste if (m_opaque->IsConnected())
77f034231aSEd Maste m_opaque->Disconnect();
78f034231aSEd Maste }
79cfca06d7SDimitry Andric m_opaque->SetConnection(
80cfca06d7SDimitry Andric std::make_unique<ConnectionFileDescriptor>(fd, owns_fd));
81f034231aSEd Maste if (m_opaque->IsConnected())
82f034231aSEd Maste status = eConnectionStatusSuccess;
83f034231aSEd Maste else
84f034231aSEd Maste status = eConnectionStatusLostConnection;
85f034231aSEd Maste }
86f034231aSEd Maste return status;
87f034231aSEd Maste }
88f034231aSEd Maste
Disconnect()8914f1b3e8SDimitry Andric ConnectionStatus SBCommunication::Disconnect() {
906f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
91f034231aSEd Maste
92f034231aSEd Maste ConnectionStatus status = eConnectionStatusNoConnection;
93f034231aSEd Maste if (m_opaque)
94f034231aSEd Maste status = m_opaque->Disconnect();
95f034231aSEd Maste return status;
96f034231aSEd Maste }
97f034231aSEd Maste
IsConnected() const9814f1b3e8SDimitry Andric bool SBCommunication::IsConnected() const {
996f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
100f034231aSEd Maste
1015f29bb8aSDimitry Andric return m_opaque ? m_opaque->IsConnected() : false;
102f034231aSEd Maste }
103f034231aSEd Maste
Read(void * dst,size_t dst_len,uint32_t timeout_usec,ConnectionStatus & status)10414f1b3e8SDimitry Andric size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
10514f1b3e8SDimitry Andric ConnectionStatus &status) {
1066f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, dst, dst_len, timeout_usec, status);
1075f29bb8aSDimitry Andric
108f034231aSEd Maste size_t bytes_read = 0;
10914f1b3e8SDimitry Andric Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
110e3b55780SDimitry Andric ? Timeout<std::micro>(std::nullopt)
11114f1b3e8SDimitry Andric : std::chrono::microseconds(timeout_usec);
112f034231aSEd Maste if (m_opaque)
1135f29bb8aSDimitry Andric bytes_read = m_opaque->Read(dst, dst_len, timeout, status, nullptr);
114f034231aSEd Maste else
115f034231aSEd Maste status = eConnectionStatusNoConnection;
116f034231aSEd Maste
117f034231aSEd Maste return bytes_read;
118f034231aSEd Maste }
119f034231aSEd Maste
Write(const void * src,size_t src_len,ConnectionStatus & status)12014f1b3e8SDimitry Andric size_t SBCommunication::Write(const void *src, size_t src_len,
12114f1b3e8SDimitry Andric ConnectionStatus &status) {
1226f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, src, src_len, status);
1235f29bb8aSDimitry Andric
124f034231aSEd Maste size_t bytes_written = 0;
125f034231aSEd Maste if (m_opaque)
1265f29bb8aSDimitry Andric bytes_written = m_opaque->Write(src, src_len, status, nullptr);
127f034231aSEd Maste else
128f034231aSEd Maste status = eConnectionStatusNoConnection;
129f034231aSEd Maste
1305f29bb8aSDimitry Andric return bytes_written;
131f034231aSEd Maste }
132f034231aSEd Maste
ReadThreadStart()13314f1b3e8SDimitry Andric bool SBCommunication::ReadThreadStart() {
1346f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
135f034231aSEd Maste
1365f29bb8aSDimitry Andric return m_opaque ? m_opaque->StartReadThread() : false;
137f034231aSEd Maste }
138f034231aSEd Maste
ReadThreadStop()13914f1b3e8SDimitry Andric bool SBCommunication::ReadThreadStop() {
1406f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
141f034231aSEd Maste
1425f29bb8aSDimitry Andric return m_opaque ? m_opaque->StopReadThread() : false;
143f034231aSEd Maste }
144f034231aSEd Maste
ReadThreadIsRunning()14514f1b3e8SDimitry Andric bool SBCommunication::ReadThreadIsRunning() {
1466f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1475f29bb8aSDimitry Andric
1485f29bb8aSDimitry Andric return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
149f034231aSEd Maste }
150f034231aSEd Maste
SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback,void * callback_baton)15114f1b3e8SDimitry Andric bool SBCommunication::SetReadThreadBytesReceivedCallback(
15214f1b3e8SDimitry Andric ReadThreadBytesReceived callback, void *callback_baton) {
1536f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, callback, callback_baton);
154f034231aSEd Maste
155f034231aSEd Maste bool result = false;
15614f1b3e8SDimitry Andric if (m_opaque) {
157f034231aSEd Maste m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
158f034231aSEd Maste result = true;
159f034231aSEd Maste }
160f034231aSEd Maste return result;
161f034231aSEd Maste }
162f034231aSEd Maste
GetBroadcaster()16314f1b3e8SDimitry Andric SBBroadcaster SBCommunication::GetBroadcaster() {
1646f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1655f29bb8aSDimitry Andric
166f034231aSEd Maste SBBroadcaster broadcaster(m_opaque, false);
1676f8fc217SDimitry Andric return broadcaster;
168f034231aSEd Maste }
169f034231aSEd Maste
GetBroadcasterClass()17014f1b3e8SDimitry Andric const char *SBCommunication::GetBroadcasterClass() {
1716f8fc217SDimitry Andric LLDB_INSTRUMENT();
1725f29bb8aSDimitry Andric
173ac9a064cSDimitry Andric return ConstString(ThreadedCommunication::GetStaticBroadcasterClass())
174ac9a064cSDimitry Andric .AsCString();
175f034231aSEd Maste }
176