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 
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 <>
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));
200  (const char *));
202  AdoptFileDesriptor, (int, bool));
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, ());
210  ());
212  GetBroadcasterClass, ());
213 }
214 
215 }
216 }
lldb::SBCommunication::ReadThreadBytesReceived
void(* ReadThreadBytesReceived)(void *baton, const void *src, size_t src_len)
Definition: SBCommunication.h:33
LLDB_RECORD_STATIC_METHOD_NO_ARGS
#define LLDB_RECORD_STATIC_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:174
lldb::SBCommunication
Definition: SBCommunication.h:17
lldb_private::Communication::ReadThreadIsRunning
bool ReadThreadIsRunning()
Checks if there is a currently running read thread.
Definition: Communication.cpp:315
lldb_private::Communication::SetCloseOnEOF
void SetCloseOnEOF(bool b)
Definition: Communication.h:308
lldb::SBCommunication::Disconnect
lldb::ConnectionStatus Disconnect()
Definition: SBCommunication.cpp:92
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
Host.h
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
ReadThreadBytesReceived
static void ReadThreadBytesReceived(void *baton, const void *src, size_t src_len)
Definition: ScriptInterpreter.cpp:139
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
SBReproducerPrivate.h
SBCommunication.h
lldb::SBCommunication::Write
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status)
Definition: SBCommunication.cpp:126
lldb::eConnectionStatusNoConnection
@ eConnectionStatusNoConnection
No connection.
Definition: lldb-enumerations.h:300
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBCommunication::ReadThreadIsRunning
bool ReadThreadIsRunning()
Definition: SBCommunication.cpp:153
lldb_private::Communication::SetConnection
void SetConnection(std::unique_ptr< Connection > connection)
Sets the connection that it to be used by this class.
Definition: Communication.cpp:423
lldb_private::Communication
Definition: Communication.h:84
lldb::SBCommunication::~SBCommunication
~SBCommunication()
Definition: SBCommunication.cpp:28
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb_private::repro::RegisterMethods< SBCommunication >
void RegisterMethods< SBCommunication >(Registry &R)
Definition: SBCommunication.cpp:192
lldb::SBCommunication::m_opaque_owned
bool m_opaque_owned
Definition: SBCommunication.h:79
lldb::SBCommunication::m_opaque
lldb_private::Communication * m_opaque
Definition: SBCommunication.h:78
lldb::SBCommunication::GetCloseOnEOF
bool GetCloseOnEOF()
Definition: SBCommunication.cpp:45
lldb::SBCommunication::SBCommunication
SBCommunication()
Definition: SBCommunication.cpp:19
SBBroadcaster.h
lldb_private::Communication::StartReadThread
virtual bool StartReadThread(Status *error_ptr=nullptr)
Starts a read thread whose sole purpose it to read bytes from the current connection.
Definition: Communication.cpp:202
lldb::SBCommunication::IsConnected
bool IsConnected() const
Definition: SBCommunication.cpp:102
lldb::ConnectionStatus
ConnectionStatus
Connection Status Types.
Definition: lldb-enumerations.h:295
lldb_private::Communication::SetReadThreadBytesReceivedCallback
void SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
Definition: Communication.cpp:396
ConnectionFileDescriptor.h
lldb_private::Communication::StopReadThread
virtual bool StopReadThread(Status *error_ptr=nullptr)
Stops the read thread by cancelling it.
Definition: Communication.cpp:237
LLDB_REGISTER_STATIC_METHOD
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:103
Communication.h
lldb_private::Communication::Write
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.
Definition: Communication.cpp:173
lldb::SBCommunication::Connect
lldb::ConnectionStatus Connect(const char *url)
Definition: SBCommunication.cpp:60
lldb::SBCommunication::ReadThreadStop
bool ReadThreadStop()
Definition: SBCommunication.cpp:147
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb_private::Communication::Disconnect
lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr)
Disconnect the communications connection if one is currently connected.
Definition: Communication.cpp:92
lldb_private::Communication::HasConnection
bool HasConnection() const
Definition: Communication.cpp:121
lldb_private::Timeout< std::micro >
lldb_private::Communication::Read
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.
Definition: Communication.cpp:125
uint32_t
lldb::SBCommunication::Read
size_t Read(void *dst, size_t dst_len, uint32_t timeout_usec, lldb::ConnectionStatus &status)
Definition: SBCommunication.cpp:108
lldb::SBCommunication::SetCloseOnEOF
void SetCloseOnEOF(bool b)
Definition: SBCommunication.cpp:53
lldb::SBCommunication::GetBroadcasterClass
static const char * GetBroadcasterClass()
Definition: SBCommunication.cpp:181
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Communication::IsConnected
bool IsConnected() const
Check if the connection is valid.
Definition: Communication.cpp:116
lldb_private::Communication::GetCloseOnEOF
bool GetCloseOnEOF() const
Definition: Communication.h:306
lldb::eConnectionStatusSuccess
@ eConnectionStatusSuccess
Success.
Definition: lldb-enumerations.h:296
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eConnectionStatusLostConnection
@ eConnectionStatusLostConnection
Lost connection while connected to a valid connection.
Definition: lldb-enumerations.h:301
lldb::SBBroadcaster
Definition: SBBroadcaster.h:16
LLDB_RECORD_DUMMY
#define LLDB_RECORD_DUMMY(Result, Class, Method, Signature,...)
The LLDB_RECORD_DUMMY macro is special because it doesn't actually record anything.
Definition: ReproducerInstrumentation.h:216
lldb::SBCommunication::ReadThreadStart
bool ReadThreadStart()
Definition: SBCommunication.cpp:141
lldb
Definition: SBAddress.h:15
lldb::SBCommunication::AdoptFileDesriptor
lldb::ConnectionStatus AdoptFileDesriptor(int fd, bool owns_fd)
Definition: SBCommunication.cpp:72
lldb::SBCommunication::SetReadThreadBytesReceivedCallback
bool SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
Definition: SBCommunication.cpp:159
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93
lldb::SBCommunication::GetBroadcaster
lldb::SBBroadcaster GetBroadcaster()
Definition: SBCommunication.cpp:173
lldb::SBCommunication::IsValid
bool IsValid() const
Definition: SBCommunication.cpp:35
lldb_private::Communication::Connect
lldb::ConnectionStatus Connect(const char *url, Status *error_ptr)
Connect using the current connection by passing url to its connect function.
Definition: Communication.cpp:78