LLDB  mainline
SBCommunication.cpp
Go to the documentation of this file.
1 //===-- SBCommunication.cpp -------------------------------------*- C++ -*-===//
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(NULL), 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 = NULL;
32  m_opaque_owned = false;
33 }
34 
37  return this->operator bool();
38 }
39 SBCommunication::operator bool() const {
41 
42  return m_opaque != NULL;
43 }
44 
47 
48  if (m_opaque)
49  return m_opaque->GetCloseOnEOF();
50  return false;
51 }
52 
55 
56  if (m_opaque)
57  m_opaque->SetCloseOnEOF(b);
58 }
59 
62  (const char *), url);
63 
64  if (m_opaque) {
65  if (!m_opaque->HasConnection())
66  m_opaque->SetConnection(Host::CreateDefaultConnection(url).release());
67  return m_opaque->Connect(url, NULL);
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())
80  m_opaque->Disconnect();
81  }
82  m_opaque->SetConnection(new ConnectionFileDescriptor(fd, owns_fd));
83  if (m_opaque->IsConnected())
84  status = eConnectionStatusSuccess;
85  else
87  }
88  return status;
89 }
90 
93  Disconnect);
94 
96  if (m_opaque)
97  status = m_opaque->Disconnect();
98  return status;
99 }
100 
103 
104  return m_opaque ? m_opaque->IsConnected() : false;
105 }
106 
107 size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
108  ConnectionStatus &status) {
110  (void *, size_t, uint32_t, lldb::ConnectionStatus &), dst,
111  dst_len, timeout_usec, status);
112 
113  size_t bytes_read = 0;
114  Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
115  ? Timeout<std::micro>(llvm::None)
116  : std::chrono::microseconds(timeout_usec);
117  if (m_opaque)
118  bytes_read = m_opaque->Read(dst, dst_len, timeout, status, NULL);
119  else
121 
122  return bytes_read;
123 }
124 
125 size_t SBCommunication::Write(const void *src, size_t src_len,
126  ConnectionStatus &status) {
128  (const void *, size_t, lldb::ConnectionStatus &), src,
129  src_len, status);
130 
131  size_t bytes_written = 0;
132  if (m_opaque)
133  bytes_written = m_opaque->Write(src, src_len, status, NULL);
134  else
136 
137  return bytes_written;
138 }
139 
142 
143  return m_opaque ? m_opaque->StartReadThread() : false;
144 }
145 
148 
149  return m_opaque ? m_opaque->StopReadThread() : false;
150 }
151 
154 
155  return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
156 }
157 
159  ReadThreadBytesReceived callback, void *callback_baton) {
162  callback, callback_baton);
163 
164  bool result = false;
165  if (m_opaque) {
166  m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
167  result = true;
168  }
169  return result;
170 }
171 
175 
176  SBBroadcaster broadcaster(m_opaque, false);
177  return LLDB_RECORD_RESULT(broadcaster);
178 }
179 
183 
184  return Communication::GetStaticBroadcasterClass().AsCString();
185 }
186 
187 namespace lldb_private {
188 namespace repro {
189 
190 template <>
195  LLDB_REGISTER_METHOD_CONST(bool, SBCommunication, operator bool, ());
199  (const char *));
201  AdoptFileDesriptor, (int, bool));
203  ());
209  ());
211  GetBroadcasterClass, ());
212 }
213 
214 }
215 }
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#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.
void SetConnection(Connection *connection)
Sets the connection that it to be used by this class.
#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)
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.
An abstract communications class.
Definition: Communication.h:91
lldb::SBBroadcaster GetBroadcaster()
lldb::ConnectionStatus Disconnect()
Definition: SBAddress.h:15
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)
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)