Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- SBCommunication.cpp -----------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBCommunication.h" |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 10 | #include "SBReproducerPrivate.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBBroadcaster.h" |
| 12 | #include "lldb/Core/Communication.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 13 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Pavel Labath | 4ccd995 | 2017-06-27 10:33:14 +0000 | [diff] [blame] | 14 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace lldb; |
| 17 | using namespace lldb_private; |
| 18 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 19 | SBCommunication::SBCommunication() : m_opaque(nullptr), m_opaque_owned(false) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 20 | LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommunication); |
| 21 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | SBCommunication::SBCommunication(const char *broadcaster_name) |
| 24 | : m_opaque(new Communication(broadcaster_name)), m_opaque_owned(true) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 25 | LLDB_RECORD_CONSTRUCTOR(SBCommunication, (const char *), broadcaster_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | SBCommunication::~SBCommunication() { |
| 29 | if (m_opaque && m_opaque_owned) |
| 30 | delete m_opaque; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 31 | m_opaque = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | m_opaque_owned = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 35 | bool SBCommunication::IsValid() const { |
| 36 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsValid); |
Pavel Labath | 7f5237b | 2019-03-11 13:58:46 +0000 | [diff] [blame] | 37 | return this->operator bool(); |
| 38 | } |
| 39 | SBCommunication::operator bool() const { |
| 40 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, operator bool); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 41 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 42 | return m_opaque != nullptr; |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 43 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | |
| 45 | bool SBCommunication::GetCloseOnEOF() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 46 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, GetCloseOnEOF); |
| 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | if (m_opaque) |
| 49 | return m_opaque->GetCloseOnEOF(); |
| 50 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | void SBCommunication::SetCloseOnEOF(bool b) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 54 | LLDB_RECORD_METHOD(void, SBCommunication, SetCloseOnEOF, (bool), b); |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | if (m_opaque) |
| 57 | m_opaque->SetCloseOnEOF(b); |
Johnny Chen | dd68ab8 | 2011-06-20 22:30:48 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | ConnectionStatus SBCommunication::Connect(const char *url) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 61 | LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication, Connect, |
| 62 | (const char *), url); |
| 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | if (m_opaque) { |
| 65 | if (!m_opaque->HasConnection()) |
Pavel Labath | 451741a | 2020-04-02 14:40:59 +0200 | [diff] [blame] | 66 | m_opaque->SetConnection(Host::CreateDefaultConnection(url)); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 67 | return m_opaque->Connect(url, nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | } |
| 69 | return eConnectionStatusNoConnection; |
Greg Clayton | d46c87a | 2010-12-04 02:39:47 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | ConnectionStatus SBCommunication::AdoptFileDesriptor(int fd, bool owns_fd) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 73 | LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication, |
| 74 | AdoptFileDesriptor, (int, bool), fd, owns_fd); |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | ConnectionStatus status = eConnectionStatusNoConnection; |
| 77 | if (m_opaque) { |
| 78 | if (m_opaque->HasConnection()) { |
| 79 | if (m_opaque->IsConnected()) |
| 80 | m_opaque->Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | } |
Pavel Labath | 451741a | 2020-04-02 14:40:59 +0200 | [diff] [blame] | 82 | m_opaque->SetConnection( |
| 83 | std::make_unique<ConnectionFileDescriptor>(fd, owns_fd)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | if (m_opaque->IsConnected()) |
| 85 | status = eConnectionStatusSuccess; |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 86 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | status = eConnectionStatusLostConnection; |
| 88 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | ConnectionStatus SBCommunication::Disconnect() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 93 | LLDB_RECORD_METHOD_NO_ARGS(lldb::ConnectionStatus, SBCommunication, |
| 94 | Disconnect); |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | ConnectionStatus status = eConnectionStatusNoConnection; |
| 97 | if (m_opaque) |
| 98 | status = m_opaque->Disconnect(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | bool SBCommunication::IsConnected() const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 103 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsConnected); |
| 104 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 105 | return m_opaque ? m_opaque->IsConnected() : false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec, |
| 109 | ConnectionStatus &status) { |
Jonas Devlieghere | 0d7b0c9 | 2019-03-08 19:09:27 +0000 | [diff] [blame] | 110 | LLDB_RECORD_DUMMY(size_t, SBCommunication, Read, |
| 111 | (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst, |
| 112 | dst_len, timeout_usec, status); |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | size_t bytes_read = 0; |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 115 | Timeout<std::micro> timeout = timeout_usec == UINT32_MAX |
| 116 | ? Timeout<std::micro>(llvm::None) |
| 117 | : std::chrono::microseconds(timeout_usec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | if (m_opaque) |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 119 | bytes_read = m_opaque->Read(dst, dst_len, timeout, status, nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | else |
| 121 | status = eConnectionStatusNoConnection; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | return bytes_read; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | size_t SBCommunication::Write(const void *src, size_t src_len, |
| 127 | ConnectionStatus &status) { |
Jonas Devlieghere | 0d7b0c9 | 2019-03-08 19:09:27 +0000 | [diff] [blame] | 128 | LLDB_RECORD_DUMMY(size_t, SBCommunication, Write, |
| 129 | (const void *, size_t, lldb::ConnectionStatus &), src, |
| 130 | src_len, status); |
| 131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | size_t bytes_written = 0; |
| 133 | if (m_opaque) |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 134 | bytes_written = m_opaque->Write(src, src_len, status, nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | else |
| 136 | status = eConnectionStatusNoConnection; |
| 137 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 138 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | bool SBCommunication::ReadThreadStart() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 142 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStart); |
| 143 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 144 | return m_opaque ? m_opaque->StartReadThread() : false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | bool SBCommunication::ReadThreadStop() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 148 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStop); |
| 149 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 150 | return m_opaque ? m_opaque->StopReadThread() : false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool SBCommunication::ReadThreadIsRunning() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 154 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadIsRunning); |
| 155 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 156 | return m_opaque ? m_opaque->ReadThreadIsRunning() : false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool SBCommunication::SetReadThreadBytesReceivedCallback( |
| 160 | ReadThreadBytesReceived callback, void *callback_baton) { |
Jonas Devlieghere | 7bc8356 | 2019-03-11 20:31:21 +0000 | [diff] [blame] | 161 | LLDB_RECORD_DUMMY(bool, SBCommunication, SetReadThreadBytesReceivedCallback, |
Jonas Devlieghere | 0d7b0c9 | 2019-03-08 19:09:27 +0000 | [diff] [blame] | 162 | (lldb::SBCommunication::ReadThreadBytesReceived, void *), |
| 163 | callback, callback_baton); |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | bool result = false; |
| 166 | if (m_opaque) { |
| 167 | m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton); |
| 168 | result = true; |
| 169 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | return result; |
| 171 | } |
| 172 | |
| 173 | SBBroadcaster SBCommunication::GetBroadcaster() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 174 | LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommunication, |
| 175 | GetBroadcaster); |
| 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | SBBroadcaster broadcaster(m_opaque, false); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 178 | return LLDB_RECORD_RESULT(broadcaster); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | const char *SBCommunication::GetBroadcasterClass() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 182 | LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommunication, |
| 183 | GetBroadcasterClass); |
| 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | return Communication::GetStaticBroadcasterClass().AsCString(); |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 186 | } |
Michal Gorny | ae211ec | 2019-03-19 17:13:13 +0000 | [diff] [blame] | 187 | |
| 188 | namespace lldb_private { |
| 189 | namespace repro { |
| 190 | |
| 191 | template <> |
| 192 | void 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 | } |