blob: 5ca63c4426badbe4710087cbf737b3a1f0dcbafc [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);
37
38 return m_opaque != NULL;
39}
Kate Stoneb9c1b512016-09-06 20:57:50 +000040
41bool SBCommunication::GetCloseOnEOF() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000042 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, GetCloseOnEOF);
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 if (m_opaque)
45 return m_opaque->GetCloseOnEOF();
46 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void SBCommunication::SetCloseOnEOF(bool b) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000050 LLDB_RECORD_METHOD(void, SBCommunication, SetCloseOnEOF, (bool), b);
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 if (m_opaque)
53 m_opaque->SetCloseOnEOF(b);
Johnny Chendd68ab82011-06-20 22:30:48 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056ConnectionStatus SBCommunication::Connect(const char *url) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000057 LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication, Connect,
58 (const char *), url);
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 if (m_opaque) {
61 if (!m_opaque->HasConnection())
Pavel Labath4ccd9952017-06-27 10:33:14 +000062 m_opaque->SetConnection(Host::CreateDefaultConnection(url).release());
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 return m_opaque->Connect(url, NULL);
64 }
65 return eConnectionStatusNoConnection;
Greg Claytond46c87a2010-12-04 02:39:47 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068ConnectionStatus SBCommunication::AdoptFileDesriptor(int fd, bool owns_fd) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000069 LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication,
70 AdoptFileDesriptor, (int, bool), fd, owns_fd);
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 ConnectionStatus status = eConnectionStatusNoConnection;
73 if (m_opaque) {
74 if (m_opaque->HasConnection()) {
75 if (m_opaque->IsConnected())
76 m_opaque->Disconnect();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 m_opaque->SetConnection(new ConnectionFileDescriptor(fd, owns_fd));
79 if (m_opaque->IsConnected())
80 status = eConnectionStatusSuccess;
Greg Clayton48381312010-10-30 04:51:46 +000081 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 status = eConnectionStatusLostConnection;
83 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087ConnectionStatus SBCommunication::Disconnect() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000088 LLDB_RECORD_METHOD_NO_ARGS(lldb::ConnectionStatus, SBCommunication,
89 Disconnect);
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 ConnectionStatus status = eConnectionStatusNoConnection;
92 if (m_opaque)
93 status = m_opaque->Disconnect();
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097bool SBCommunication::IsConnected() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000098 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsConnected);
99
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000100 return m_opaque ? m_opaque->IsConnected() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
104 ConnectionStatus &status) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000105 LLDB_RECORD_DUMMY(size_t, SBCommunication, Read,
106 (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst,
107 dst_len, timeout_usec, status);
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 size_t bytes_read = 0;
Pavel Labathc4063ee2016-11-25 11:58:44 +0000110 Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
111 ? Timeout<std::micro>(llvm::None)
112 : std::chrono::microseconds(timeout_usec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 if (m_opaque)
Pavel Labathc4063ee2016-11-25 11:58:44 +0000114 bytes_read = m_opaque->Read(dst, dst_len, timeout, status, NULL);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 else
116 status = eConnectionStatusNoConnection;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 return bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119}
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121size_t SBCommunication::Write(const void *src, size_t src_len,
122 ConnectionStatus &status) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000123 LLDB_RECORD_DUMMY(size_t, SBCommunication, Write,
124 (const void *, size_t, lldb::ConnectionStatus &), src,
125 src_len, status);
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 size_t bytes_written = 0;
128 if (m_opaque)
129 bytes_written = m_opaque->Write(src, src_len, status, NULL);
130 else
131 status = eConnectionStatusNoConnection;
132
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000133 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136bool SBCommunication::ReadThreadStart() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000137 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStart);
138
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000139 return m_opaque ? m_opaque->StartReadThread() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142bool SBCommunication::ReadThreadStop() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000143 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStop);
144
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000145 return m_opaque ? m_opaque->StopReadThread() : false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146}
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148bool SBCommunication::ReadThreadIsRunning() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000149 LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadIsRunning);
150
Jonas Devlieghere581af8b2019-03-07 22:47:13 +0000151 return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152}
153
154bool SBCommunication::SetReadThreadBytesReceivedCallback(
155 ReadThreadBytesReceived callback, void *callback_baton) {
Jonas Devlieghere0d7b0c92019-03-08 19:09:27 +0000156 LLDB_RECORD_DUMMY(bool, SBCommunication, SetReadThreadBytesReceivedCallback,
157 (lldb::SBCommunication::ReadThreadBytesReceived, void *),
158 callback, callback_baton);
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 bool result = false;
161 if (m_opaque) {
162 m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
163 result = true;
164 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 return result;
166}
167
168SBBroadcaster SBCommunication::GetBroadcaster() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000169 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommunication,
170 GetBroadcaster);
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 SBBroadcaster broadcaster(m_opaque, false);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000173 return LLDB_RECORD_RESULT(broadcaster);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174}
175
176const char *SBCommunication::GetBroadcasterClass() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000177 LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommunication,
178 GetBroadcasterClass);
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 return Communication::GetStaticBroadcasterClass().AsCString();
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000181}