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 
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBBroadcaster.h"
14 #include "lldb/Host/Host.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 SBCommunication::SBCommunication() : m_opaque(nullptr), m_opaque_owned(false) {
21 }
22 
23 SBCommunication::SBCommunication(const char *broadcaster_name)
24  : m_opaque(new Communication(broadcaster_name)), m_opaque_owned(true) {
25  LLDB_RECORD_CONSTRUCTOR(SBCommunication, (const char *), broadcaster_name);
26 }
27 
29  if (m_opaque && m_opaque_owned)
30  delete m_opaque;
31  m_opaque = nullptr;
32  m_opaque_owned = false;
33 }
34 
37  return this->operator bool();
38 }
39 SBCommunication::operator bool() const {
41 
42  return m_opaque != nullptr;
43 }
44 
47 
48  if (m_opaque)
49  return m_opaque->GetCloseOnEOF();
50  return false;
51 }
52 
55 
56  if (m_opaque)
58 }
59 
62  (const char *), url);
63 
64  if (m_opaque) {
65  if (!m_opaque->HasConnection())
66  m_opaque->SetConnection(Host::CreateDefaultConnection(url));
67  return m_opaque->Connect(url, nullptr);
68  }
70 }
71 
74  AdoptFileDesriptor, (int, bool), fd, owns_fd);
75 
77  if (m_opaque) {
78  if (m_opaque->HasConnection()) {
79  if (m_opaque->IsConnected())
81  }
83  std::make_unique<ConnectionFileDescriptor>(fd, owns_fd));
84  if (m_opaque->IsConnected())
85  status = eConnectionStatusSuccess;
86  else
88  }
89  return status;
90 }
91 
94  Disconnect);
95 
97  if (m_opaque)
98  status = m_opaque->Disconnect();
99  return status;
100 }
101 
104 
105  return m_opaque ? m_opaque->IsConnected() : false;
106 }
107 
108 size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
109  ConnectionStatus &status) {
111  (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst,
112  dst_len, timeout_usec, status);
113 
114  size_t bytes_read = 0;
115  Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
116  ? Timeout<std::micro>(llvm::None)
117  : std::chrono::microseconds(timeout_usec);
118  if (m_opaque)
119  bytes_read = m_opaque->Read(dst, dst_len, timeout, status, nullptr);
120  else
122 
123  return bytes_read;
124 }
125 
126 size_t SBCommunication::Write(const void *src, size_t src_len,
127  ConnectionStatus &status) {
129  (const void *, size_t, lldb::ConnectionStatus &), src,
130  src_len, status);
131 
132  size_t bytes_written = 0;
133  if (m_opaque)
134  bytes_written = m_opaque->Write(src, src_len, status, nullptr);
135  else
137 
138  return bytes_written;
139 }
140 
143 
144  return m_opaque ? m_opaque->StartReadThread() : false;
145 }
146 
149 
150  return m_opaque ? m_opaque->StopReadThread() : false;
151 }
152 
155 
156  return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
157 }
158 
160  ReadThreadBytesReceived callback, void *callback_baton) {
163  callback, callback_baton);
164 
165  bool result = false;
166  if (m_opaque) {
167  m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
168  result = true;
169  }
170  return result;
171 }
172 
176 
177  SBBroadcaster broadcaster(m_opaque, false);
178  return LLDB_RECORD_RESULT(broadcaster);
179 }
180 
184 
185  return Communication::GetStaticBroadcasterClass().AsCString();
186 }
187 
188 namespace lldb_private {
189 namespace repro {
190 
191 template <>
196  LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, operator bool, ());
200  (const char *));
202  AdoptFileDesriptor, (int, bool));
204  ());
210  ());
212  GetBroadcasterClass, ());
213 }
214 
215 }
216 }
The registry contains a unique mapping between functions and their ID.
A class that represents a running process on the host machine.
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
virtual bool StopReadThread(Status *error_ptr=nullptr)
Stops the read thread by cancelling it.
lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr)
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.
#define LLDB_RECORD_DUMMY(Result, Class, Method, Signature,...)
The LLDB_RECORD_DUMMY macro is special because it doesn&#39;t actually record anything.
#define LLDB_RECORD_STATIC_METHOD_NO_ARGS(Result, Class, Method)
void SetConnection(std::unique_ptr< Connection > connection)
Sets the connection that it to be used by this class.
bool SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
size_t Read(void *dst, size_t dst_len, uint32_t timeout_usec, lldb::ConnectionStatus &status)
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb::ConnectionStatus AdoptFileDesriptor(int fd, bool owns_fd)
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
lldb::ConnectionStatus Connect(const char *url)
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
void(* ReadThreadBytesReceived)(void *baton, const void *src, size_t src_len)
static const char * GetBroadcasterClass()
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
void SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
bool IsConnected() const
Check if the connection is valid.
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr)
Read bytes from the current connection.
lldb_private::Communication * m_opaque
An abstract communications class.
Definition: Communication.h:84
lldb::SBBroadcaster GetBroadcaster()
lldb::ConnectionStatus Disconnect()
Definition: SBAddress.h:15
Lost connection while connected to a valid connection.
bool ReadThreadIsRunning()
Checks if there is a currently running read thread.
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
ConnectionStatus
Connection Status Types.
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status)
#define LLDB_RECORD_RESULT(Result)
lldb::ConnectionStatus Connect(const char *url, Status *error_ptr)
Connect using the current connection by passing url to its connect function.
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
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.
void RegisterMethods< SBCommunication >(Registry &R)