blob: 21bcde12cb3de3a1e7a99ad81445bf27e2d754c1 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBCommunication.cpp -------------------------------------*- C++ -*-===//
2//
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
Jonas Devliegherebaf56642019-03-06 00:06:00 +000019SBCommunication::SBCommunication() : m_opaque(NULL), m_opaque_owned(false) {
20 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;
31 m_opaque = NULL;
32 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
42 return m_opaque != NULL;
43}
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 Labath4ccd9952017-06-27 10:33:14 +000066 m_opaque->SetConnection(Host::CreateDefaultConnection(url).release());
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 return m_opaque->Connect(url, NULL);
68 }
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 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 m_opaque->SetConnection(new ConnectionFileDescriptor(fd, owns_fd));
83 if (m_opaque->IsConnected())
84 status = eConnectionStatusSuccess;
Greg Clayton48381312010-10-30 04:51:46 +000085 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 status = eConnectionStatusLostConnection;
87 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091ConnectionStatus SBCommunication::Disconnect() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000092 LLDB_RECORD_METHOD_NO_ARGS(lldb::ConnectionStatus, SBCommunication,
93 Disconnect);
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 ConnectionStatus status = eConnectionStatusNoConnection;
96 if (m_opaque)
97 status = m_opaque->Disconnect();
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101bool SBCommunication::IsConnected() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000102 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsConnected);
103
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000104 return m_opaque ? m_opaque->IsConnected() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
108 ConnectionStatus &status) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000109 LLDB_RECORD_DUMMY(size_t, SBCommunication, Read,
110 (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst,
111 dst_len, timeout_usec, status);
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 size_t bytes_read = 0;
Pavel Labathc4063ee2016-11-25 11:58:44 +0000114 Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
115 ? Timeout<std::micro>(llvm::None)
116 : std::chrono::microseconds(timeout_usec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 if (m_opaque)
Pavel Labathc4063ee2016-11-25 11:58:44 +0000118 bytes_read = m_opaque->Read(dst, dst_len, timeout, status, NULL);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 else
120 status = eConnectionStatusNoConnection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 return bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125size_t SBCommunication::Write(const void *src, size_t src_len,
126 ConnectionStatus &status) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000127 LLDB_RECORD_DUMMY(size_t, SBCommunication, Write,
128 (const void *, size_t, lldb::ConnectionStatus &), src,
129 src_len, status);
130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 size_t bytes_written = 0;
132 if (m_opaque)
133 bytes_written = m_opaque->Write(src, src_len, status, NULL);
134 else
135 status = eConnectionStatusNoConnection;
136
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000137 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140bool SBCommunication::ReadThreadStart() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000141 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStart);
142
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000143 return m_opaque ? m_opaque->StartReadThread() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146bool SBCommunication::ReadThreadStop() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000147 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStop);
148
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000149 return m_opaque ? m_opaque->StopReadThread() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150}
151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152bool SBCommunication::ReadThreadIsRunning() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000153 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadIsRunning);
154
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000155 return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156}
157
158bool SBCommunication::SetReadThreadBytesReceivedCallback(
159 ReadThreadBytesReceived callback, void *callback_baton) {
Jonas Devlieghere7bc83562019-03-11 20:31:21 +0000160 LLDB_RECORD_DUMMY(bool, SBCommunication, SetReadThreadBytesReceivedCallback,
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000161 (lldb::SBCommunication::ReadThreadBytesReceived, void *),
162 callback, callback_baton);
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 bool result = false;
165 if (m_opaque) {
166 m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
167 result = true;
168 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 return result;
170}
171
172SBBroadcaster SBCommunication::GetBroadcaster() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000173 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommunication,
174 GetBroadcaster);
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 SBBroadcaster broadcaster(m_opaque, false);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000177 return LLDB_RECORD_RESULT(broadcaster);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178}
179
180const char *SBCommunication::GetBroadcasterClass() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000181 LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommunication,
182 GetBroadcasterClass);
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 return Communication::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000185}
Michal Gornyae211ec2019-03-19 17:13:13 +0000186
187namespace lldb_private {
188namespace repro {
189
190template <>
191void RegisterMethods<SBCommunication>(Registry &R) {
192 LLDB_REGISTER_CONSTRUCTOR(SBCommunication, ());
193 LLDB_REGISTER_CONSTRUCTOR(SBCommunication, (const char *));
194 LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, IsValid, ());
195 LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, operator bool, ());
196 LLDB_REGISTER_METHOD(bool, SBCommunication, GetCloseOnEOF, ());
197 LLDB_REGISTER_METHOD(void, SBCommunication, SetCloseOnEOF, (bool));
198 LLDB_REGISTER_METHOD(lldb::ConnectionStatus, SBCommunication, Connect,
199 (const char *));
200 LLDB_REGISTER_METHOD(lldb::ConnectionStatus, SBCommunication,
201 AdoptFileDesriptor, (int, bool));
202 LLDB_REGISTER_METHOD(lldb::ConnectionStatus, SBCommunication, Disconnect,
203 ());
204 LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, IsConnected, ());
205 LLDB_REGISTER_METHOD(bool, SBCommunication, ReadThreadStart, ());
206 LLDB_REGISTER_METHOD(bool, SBCommunication, ReadThreadStop, ());
207 LLDB_REGISTER_METHOD(bool, SBCommunication, ReadThreadIsRunning, ());
208 LLDB_REGISTER_METHOD(lldb::SBBroadcaster, SBCommunication, GetBroadcaster,
209 ());
210 LLDB_REGISTER_STATIC_METHOD(const char *, SBCommunication,
211 GetBroadcasterClass, ());
212}
213
214}
215}