Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBCommunication.cpp -------------------------------------*- C++ -*-===// |
| 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 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 19 | SBCommunication::SBCommunication() : m_opaque(NULL), m_opaque_owned(false) { |
| 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; |
| 31 | m_opaque = NULL; |
| 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); |
| 37 | |
| 38 | return m_opaque != NULL; |
| 39 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | |
| 41 | bool SBCommunication::GetCloseOnEOF() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 42 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, GetCloseOnEOF); |
| 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | if (m_opaque) |
| 45 | return m_opaque->GetCloseOnEOF(); |
| 46 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | void SBCommunication::SetCloseOnEOF(bool b) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 50 | LLDB_RECORD_METHOD(void, SBCommunication, SetCloseOnEOF, (bool), b); |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | if (m_opaque) |
| 53 | m_opaque->SetCloseOnEOF(b); |
Johnny Chen | dd68ab8 | 2011-06-20 22:30:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | ConnectionStatus SBCommunication::Connect(const char *url) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 57 | LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication, Connect, |
| 58 | (const char *), url); |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | if (m_opaque) { |
| 61 | if (!m_opaque->HasConnection()) |
Pavel Labath | 4ccd995 | 2017-06-27 10:33:14 +0000 | [diff] [blame] | 62 | m_opaque->SetConnection(Host::CreateDefaultConnection(url).release()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | return m_opaque->Connect(url, NULL); |
| 64 | } |
| 65 | return eConnectionStatusNoConnection; |
Greg Clayton | d46c87a | 2010-12-04 02:39:47 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | ConnectionStatus SBCommunication::AdoptFileDesriptor(int fd, bool owns_fd) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 69 | LLDB_RECORD_METHOD(lldb::ConnectionStatus, SBCommunication, |
| 70 | AdoptFileDesriptor, (int, bool), fd, owns_fd); |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | ConnectionStatus status = eConnectionStatusNoConnection; |
| 73 | if (m_opaque) { |
| 74 | if (m_opaque->HasConnection()) { |
| 75 | if (m_opaque->IsConnected()) |
| 76 | m_opaque->Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | m_opaque->SetConnection(new ConnectionFileDescriptor(fd, owns_fd)); |
| 79 | if (m_opaque->IsConnected()) |
| 80 | status = eConnectionStatusSuccess; |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 81 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | status = eConnectionStatusLostConnection; |
| 83 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | ConnectionStatus SBCommunication::Disconnect() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 88 | LLDB_RECORD_METHOD_NO_ARGS(lldb::ConnectionStatus, SBCommunication, |
| 89 | Disconnect); |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | ConnectionStatus status = eConnectionStatusNoConnection; |
| 92 | if (m_opaque) |
| 93 | status = m_opaque->Disconnect(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | bool SBCommunication::IsConnected() const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 98 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommunication, IsConnected); |
| 99 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 100 | return m_opaque ? m_opaque->IsConnected() : false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec, |
| 104 | ConnectionStatus &status) { |
Jonas Devlieghere | 0d7b0c9 | 2019-03-08 19:09:27 +0000 | [diff] [blame^] | 105 | LLDB_RECORD_DUMMY(size_t, SBCommunication, Read, |
| 106 | (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst, |
| 107 | dst_len, timeout_usec, status); |
| 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | size_t bytes_read = 0; |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 110 | Timeout<std::micro> timeout = timeout_usec == UINT32_MAX |
| 111 | ? Timeout<std::micro>(llvm::None) |
| 112 | : std::chrono::microseconds(timeout_usec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | if (m_opaque) |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 114 | bytes_read = m_opaque->Read(dst, dst_len, timeout, status, NULL); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | else |
| 116 | status = eConnectionStatusNoConnection; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | return bytes_read; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | size_t SBCommunication::Write(const void *src, size_t src_len, |
| 122 | ConnectionStatus &status) { |
Jonas Devlieghere | 0d7b0c9 | 2019-03-08 19:09:27 +0000 | [diff] [blame^] | 123 | LLDB_RECORD_DUMMY(size_t, SBCommunication, Write, |
| 124 | (const void *, size_t, lldb::ConnectionStatus &), src, |
| 125 | src_len, status); |
| 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | 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 Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 133 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | bool SBCommunication::ReadThreadStart() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 137 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStart); |
| 138 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 139 | return m_opaque ? m_opaque->StartReadThread() : false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | bool SBCommunication::ReadThreadStop() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 143 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadStop); |
| 144 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 145 | return m_opaque ? m_opaque->StopReadThread() : false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | bool SBCommunication::ReadThreadIsRunning() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 149 | LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommunication, ReadThreadIsRunning); |
| 150 | |
Jonas Devlieghere | 581af8b | 2019-03-07 22:47:13 +0000 | [diff] [blame] | 151 | return m_opaque ? m_opaque->ReadThreadIsRunning() : false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool SBCommunication::SetReadThreadBytesReceivedCallback( |
| 155 | ReadThreadBytesReceived callback, void *callback_baton) { |
Jonas Devlieghere | 0d7b0c9 | 2019-03-08 19:09:27 +0000 | [diff] [blame^] | 156 | LLDB_RECORD_DUMMY(bool, SBCommunication, SetReadThreadBytesReceivedCallback, |
| 157 | (lldb::SBCommunication::ReadThreadBytesReceived, void *), |
| 158 | callback, callback_baton); |
| 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | bool result = false; |
| 161 | if (m_opaque) { |
| 162 | m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton); |
| 163 | result = true; |
| 164 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | return result; |
| 166 | } |
| 167 | |
| 168 | SBBroadcaster SBCommunication::GetBroadcaster() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 169 | LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommunication, |
| 170 | GetBroadcaster); |
| 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | SBBroadcaster broadcaster(m_opaque, false); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 173 | return LLDB_RECORD_RESULT(broadcaster); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | const char *SBCommunication::GetBroadcasterClass() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 177 | LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommunication, |
| 178 | GetBroadcasterClass); |
| 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | return Communication::GetStaticBroadcasterClass().AsCString(); |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 181 | } |