blob: d55ecd35b55709cc5a46d7863d932531cf9e7662 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- SBCommunication.cpp -----------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBCommunication.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/API/SBBroadcaster.h"
12#include "lldb/Core/Communication.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000013#include "lldb/Host/ConnectionFileDescriptor.h"
Pavel Labath4ccd9952017-06-27 10:33:14 +000014#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
16using namespace lldb;
17using namespace lldb_private;
18
Konrad Kleine248a1302019-05-23 11:14:47 +000019SBCommunication::SBCommunication() : m_opaque(nullptr), m_opaque_owned(false) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000020 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommunication);
21}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023SBCommunication::SBCommunication(const char *broadcaster_name)
24 : m_opaque(new Communication(broadcaster_name)), m_opaque_owned(true) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000025 LLDB_RECORD_CONSTRUCTOR(SBCommunication, (const char *), broadcaster_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026}
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028SBCommunication::~SBCommunication() {
29 if (m_opaque && m_opaque_owned)
30 delete m_opaque;
Konrad Kleine248a1302019-05-23 11:14:47 +000031 m_opaque = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 m_opaque_owned = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033}
34
Jonas Devliegherebaf56642019-03-06 00:06:00 +000035bool SBCommunication::IsValid() const {
36 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsValid);
Pavel Labath7f5237b2019-03-11 13:58:46 +000037 return this->operator bool();
38}
39SBCommunication::operator bool() const {
40 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, operator bool);
Jonas Devliegherebaf56642019-03-06 00:06:00 +000041
Konrad Kleine248a1302019-05-23 11:14:47 +000042 return m_opaque != nullptr;
Jonas Devliegherebaf56642019-03-06 00:06:00 +000043}
Kate Stoneb9c1b512016-09-06 20:57:50 +000044
45bool SBCommunication::GetCloseOnEOF() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000046 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, GetCloseOnEOF);
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 if (m_opaque)
49 return m_opaque->GetCloseOnEOF();
50 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053void SBCommunication::SetCloseOnEOF(bool b) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000054 LLDB_RECORD_METHOD(void, SBCommunication, SetCloseOnEOF, (bool), b);
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 if (m_opaque)
57 m_opaque->SetCloseOnEOF(b);
Johnny Chendd68ab82011-06-20 22:30:48 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060ConnectionStatus SBCommunication::Connect(const char *url) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000061 LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication, Connect,
62 (const char *), url);
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 if (m_opaque) {
65 if (!m_opaque->HasConnection())
Pavel Labath451741a2020-04-02 14:40:59 +020066 m_opaque->SetConnection(Host::CreateDefaultConnection(url));
Konrad Kleine248a1302019-05-23 11:14:47 +000067 return m_opaque->Connect(url, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 }
69 return eConnectionStatusNoConnection;
Greg Claytond46c87a2010-12-04 02:39:47 +000070}
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072ConnectionStatus SBCommunication::AdoptFileDesriptor(int fd, bool owns_fd) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000073 LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication,
74 AdoptFileDesriptor, (int, bool), fd, owns_fd);
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 ConnectionStatus status = eConnectionStatusNoConnection;
77 if (m_opaque) {
78 if (m_opaque->HasConnection()) {
79 if (m_opaque->IsConnected())
80 m_opaque->Disconnect();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 }
Pavel Labath451741a2020-04-02 14:40:59 +020082 m_opaque->SetConnection(
83 std::make_unique<ConnectionFileDescriptor>(fd, owns_fd));
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 if (m_opaque->IsConnected())
85 status = eConnectionStatusSuccess;
Greg Clayton48381312010-10-30 04:51:46 +000086 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 status = eConnectionStatusLostConnection;
88 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092ConnectionStatus SBCommunication::Disconnect() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000093 LLDB_RECORD_METHOD_NO_ARGS(lldb::ConnectionStatus, SBCommunication,
94 Disconnect);
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 ConnectionStatus status = eConnectionStatusNoConnection;
97 if (m_opaque)
98 status = m_opaque->Disconnect();
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102bool SBCommunication::IsConnected() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000103 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsConnected);
104
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000105 return m_opaque ? m_opaque->IsConnected() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106}
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
109 ConnectionStatus &status) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000110 LLDB_RECORD_DUMMY(size_t, SBCommunication, Read,
111 (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst,
112 dst_len, timeout_usec, status);
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 size_t bytes_read = 0;
Pavel Labathc4063ee2016-11-25 11:58:44 +0000115 Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
116 ? Timeout<std::micro>(llvm::None)
117 : std::chrono::microseconds(timeout_usec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (m_opaque)
Konrad Kleine248a1302019-05-23 11:14:47 +0000119 bytes_read = m_opaque->Read(dst, dst_len, timeout, status, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 else
121 status = eConnectionStatusNoConnection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 return bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124}
125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126size_t SBCommunication::Write(const void *src, size_t src_len,
127 ConnectionStatus &status) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000128 LLDB_RECORD_DUMMY(size_t, SBCommunication, Write,
129 (const void *, size_t, lldb::ConnectionStatus &), src,
130 src_len, status);
131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 size_t bytes_written = 0;
133 if (m_opaque)
Konrad Kleine248a1302019-05-23 11:14:47 +0000134 bytes_written = m_opaque->Write(src, src_len, status, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 else
136 status = eConnectionStatusNoConnection;
137
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000138 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141bool SBCommunication::ReadThreadStart() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000142 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStart);
143
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000144 return m_opaque ? m_opaque->StartReadThread() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147bool SBCommunication::ReadThreadStop() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000148 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStop);
149
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000150 return m_opaque ? m_opaque->StopReadThread() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153bool SBCommunication::ReadThreadIsRunning() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000154 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadIsRunning);
155
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000156 return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157}
158
159bool SBCommunication::SetReadThreadBytesReceivedCallback(
160 ReadThreadBytesReceived callback, void *callback_baton) {
Jonas Devlieghere7bc83562019-03-11 20:31:21 +0000161 LLDB_RECORD_DUMMY(bool, SBCommunication, SetReadThreadBytesReceivedCallback,
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000162 (lldb::SBCommunication::ReadThreadBytesReceived, void *),
163 callback, callback_baton);
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 bool result = false;
166 if (m_opaque) {
167 m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
168 result = true;
169 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 return result;
171}
172
173SBBroadcaster SBCommunication::GetBroadcaster() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000174 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommunication,
175 GetBroadcaster);
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 SBBroadcaster broadcaster(m_opaque, false);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000178 return LLDB_RECORD_RESULT(broadcaster);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179}
180
181const char *SBCommunication::GetBroadcasterClass() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000182 LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommunication,
183 GetBroadcasterClass);
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 return Communication::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000186}
Michal Gornyae211ec2019-03-19 17:13:13 +0000187
188namespace lldb_private {
189namespace repro {
190
191template <>
192void RegisterMethods<SBCommunication>(Registry &R) {
193 LLDB_REGISTER_CONSTRUCTOR(SBCommunication, ());
194 LLDB_REGISTER_CONSTRUCTOR(SBCommunication, (const char *));
195 LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, IsValid, ());
196 LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, operator bool, ());
197 LLDB_REGISTER_METHOD(bool, SBCommunication, GetCloseOnEOF, ());
198 LLDB_REGISTER_METHOD(void, SBCommunication, SetCloseOnEOF, (bool));
199 LLDB_REGISTER_METHOD(lldb::ConnectionStatus, SBCommunication, Connect,
200 (const char *));
201 LLDB_REGISTER_METHOD(lldb::ConnectionStatus, SBCommunication,
202 AdoptFileDesriptor, (int, bool));
203 LLDB_REGISTER_METHOD(lldb::ConnectionStatus, SBCommunication, Disconnect,
204 ());
205 LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, IsConnected, ());
206 LLDB_REGISTER_METHOD(bool, SBCommunication, ReadThreadStart, ());
207 LLDB_REGISTER_METHOD(bool, SBCommunication, ReadThreadStop, ());
208 LLDB_REGISTER_METHOD(bool, SBCommunication, ReadThreadIsRunning, ());
209 LLDB_REGISTER_METHOD(lldb::SBBroadcaster, SBCommunication, GetBroadcaster,
210 ());
211 LLDB_REGISTER_STATIC_METHOD(const char *, SBCommunication,
212 GetBroadcasterClass, ());
213}
214
215}
216}