LLDB mainline
SBCommunication.cpp
Go to the documentation of this file.
1//===-- SBCommunication.cpp -----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
13#include "lldb/Host/Host.h"
15
16using namespace lldb;
17using namespace lldb_private;
18
20
21SBCommunication::SBCommunication(const char *broadcaster_name)
22 : m_opaque(new ThreadedCommunication(broadcaster_name)),
23 m_opaque_owned(true) {
24 LLDB_INSTRUMENT_VA(this, broadcaster_name);
25}
26
29 delete m_opaque;
30 m_opaque = nullptr;
31 m_opaque_owned = false;
32}
33
36 return this->operator bool();
37}
38SBCommunication::operator bool() const {
40
41 return m_opaque != nullptr;
42}
43
46
47 if (m_opaque)
48 return m_opaque->GetCloseOnEOF();
49 return false;
50}
51
53 LLDB_INSTRUMENT_VA(this, b);
54
55 if (m_opaque)
57}
58
60 LLDB_INSTRUMENT_VA(this, url);
61
62 if (m_opaque) {
63 if (!m_opaque->HasConnection())
65 return m_opaque->Connect(url, nullptr);
66 }
68}
69
71 LLDB_INSTRUMENT_VA(this, fd, owns_fd);
72
74 if (m_opaque) {
75 if (m_opaque->HasConnection()) {
76 if (m_opaque->IsConnected())
78 }
80 std::make_unique<ConnectionFileDescriptor>(fd, owns_fd));
81 if (m_opaque->IsConnected())
83 else
85 }
86 return status;
87}
88
91
93 if (m_opaque)
94 status = m_opaque->Disconnect();
95 return status;
96}
97
100
101 return m_opaque ? m_opaque->IsConnected() : false;
102}
103
104size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
105 ConnectionStatus &status) {
106 LLDB_INSTRUMENT_VA(this, dst, dst_len, timeout_usec, status);
107
108 size_t bytes_read = 0;
109 Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
110 ? Timeout<std::micro>(std::nullopt)
111 : std::chrono::microseconds(timeout_usec);
112 if (m_opaque)
113 bytes_read = m_opaque->Read(dst, dst_len, timeout, status, nullptr);
114 else
116
117 return bytes_read;
118}
119
120size_t SBCommunication::Write(const void *src, size_t src_len,
121 ConnectionStatus &status) {
122 LLDB_INSTRUMENT_VA(this, src, src_len, status);
123
124 size_t bytes_written = 0;
125 if (m_opaque)
126 bytes_written = m_opaque->Write(src, src_len, status, nullptr);
127 else
129
130 return bytes_written;
131}
132
134 LLDB_INSTRUMENT_VA(this);
135
136 return m_opaque ? m_opaque->StartReadThread() : false;
137}
138
140 LLDB_INSTRUMENT_VA(this);
141
142 return m_opaque ? m_opaque->StopReadThread() : false;
143}
144
146 LLDB_INSTRUMENT_VA(this);
147
148 return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
149}
150
152 ReadThreadBytesReceived callback, void *callback_baton) {
153 LLDB_INSTRUMENT_VA(this, callback, callback_baton);
154
155 bool result = false;
156 if (m_opaque) {
157 m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
158 result = true;
159 }
160 return result;
161}
162
164 LLDB_INSTRUMENT_VA(this);
165
166 SBBroadcaster broadcaster(m_opaque, false);
167 return broadcaster;
168}
169
172
174}
#define LLDB_INSTRUMENT()
#define LLDB_INSTRUMENT_VA(...)
static void ReadThreadBytesReceived(void *baton, const void *src, size_t src_len)
lldb_private::ThreadedCommunication * m_opaque
static const char * GetBroadcasterClass()
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status)
size_t Read(void *dst, size_t dst_len, uint32_t timeout_usec, lldb::ConnectionStatus &status)
lldb::ConnectionStatus Disconnect()
lldb::ConnectionStatus AdoptFileDesriptor(int fd, bool owns_fd)
lldb::ConnectionStatus Connect(const char *url)
lldb::SBBroadcaster GetBroadcaster()
bool SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
lldb::ConnectionStatus Connect(const char *url, Status *error_ptr)
Connect using the current connection by passing url to its connect function.
bool IsConnected() const
Check if the connection is valid.
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr)
The actual write function that attempts to write to the communications protocol.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:192
static std::unique_ptr< Connection > CreateDefaultConnection(llvm::StringRef url)
"lldb/Core/ThreadedCommunication.h" Variation of Communication that supports threaded reads.
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr) override
Read bytes from the current connection.
static ConstString & GetStaticBroadcasterClass()
void SetConnection(std::unique_ptr< Connection > connection) override
Sets the connection that it to be used by this class.
virtual bool StopReadThread(Status *error_ptr=nullptr)
Stops the read thread by cancelling it.
lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr) override
Disconnect the communications connection if one is currently connected.
virtual bool StartReadThread(Status *error_ptr=nullptr)
Starts a read thread whose sole purpose it to read bytes from the current connection.
bool ReadThreadIsRunning()
Checks if there is a currently running read thread.
void SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
ConnectionStatus
Connection Status Types.
@ eConnectionStatusSuccess
Success.
@ eConnectionStatusLostConnection
Lost connection while connected to a valid connection.
@ eConnectionStatusNoConnection
No connection.