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 "lldb/API/SBBroadcaster.h"
13 #include "lldb/Host/Host.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
20 
21 SBCommunication::SBCommunication(const char *broadcaster_name)
22  : m_opaque(new Communication(broadcaster_name)), m_opaque_owned(true) {
23  LLDB_INSTRUMENT_VA(this, broadcaster_name);
24 }
25 
27  if (m_opaque && m_opaque_owned)
28  delete m_opaque;
29  m_opaque = nullptr;
30  m_opaque_owned = false;
31 }
32 
34  LLDB_INSTRUMENT_VA(this);
35  return this->operator bool();
36 }
37 SBCommunication::operator bool() const {
38  LLDB_INSTRUMENT_VA(this);
39 
40  return m_opaque != nullptr;
41 }
42 
44  LLDB_INSTRUMENT_VA(this);
45 
46  if (m_opaque)
47  return m_opaque->GetCloseOnEOF();
48  return false;
49 }
50 
52  LLDB_INSTRUMENT_VA(this, b);
53 
54  if (m_opaque)
56 }
57 
59  LLDB_INSTRUMENT_VA(this, url);
60 
61  if (m_opaque) {
62  if (!m_opaque->HasConnection())
63  m_opaque->SetConnection(Host::CreateDefaultConnection(url));
64  return m_opaque->Connect(url, nullptr);
65  }
67 }
68 
70  LLDB_INSTRUMENT_VA(this, fd, owns_fd);
71 
73  if (m_opaque) {
74  if (m_opaque->HasConnection()) {
75  if (m_opaque->IsConnected())
77  }
79  std::make_unique<ConnectionFileDescriptor>(fd, owns_fd));
80  if (m_opaque->IsConnected())
81  status = eConnectionStatusSuccess;
82  else
84  }
85  return status;
86 }
87 
89  LLDB_INSTRUMENT_VA(this);
90 
92  if (m_opaque)
93  status = m_opaque->Disconnect();
94  return status;
95 }
96 
98  LLDB_INSTRUMENT_VA(this);
99 
100  return m_opaque ? m_opaque->IsConnected() : false;
101 }
102 
103 size_t SBCommunication::Read(void *dst, size_t dst_len, uint32_t timeout_usec,
104  ConnectionStatus &status) {
105  LLDB_INSTRUMENT_VA(this, dst, dst_len, timeout_usec, status);
106 
107  size_t bytes_read = 0;
108  Timeout<std::micro> timeout = timeout_usec == UINT32_MAX
109  ? Timeout<std::micro>(llvm::None)
110  : std::chrono::microseconds(timeout_usec);
111  if (m_opaque)
112  bytes_read = m_opaque->Read(dst, dst_len, timeout, status, nullptr);
113  else
115 
116  return bytes_read;
117 }
118 
119 size_t SBCommunication::Write(const void *src, size_t src_len,
120  ConnectionStatus &status) {
121  LLDB_INSTRUMENT_VA(this, src, src_len, status);
122 
123  size_t bytes_written = 0;
124  if (m_opaque)
125  bytes_written = m_opaque->Write(src, src_len, status, nullptr);
126  else
128 
129  return bytes_written;
130 }
131 
133  LLDB_INSTRUMENT_VA(this);
134 
135  return m_opaque ? m_opaque->StartReadThread() : false;
136 }
137 
139  LLDB_INSTRUMENT_VA(this);
140 
141  return m_opaque ? m_opaque->StopReadThread() : false;
142 }
143 
145  LLDB_INSTRUMENT_VA(this);
146 
147  return m_opaque ? m_opaque->ReadThreadIsRunning() : false;
148 }
149 
151  ReadThreadBytesReceived callback, void *callback_baton) {
152  LLDB_INSTRUMENT_VA(this, callback, callback_baton);
153 
154  bool result = false;
155  if (m_opaque) {
156  m_opaque->SetReadThreadBytesReceivedCallback(callback, callback_baton);
157  result = true;
158  }
159  return result;
160 }
161 
163  LLDB_INSTRUMENT_VA(this);
164 
165  SBBroadcaster broadcaster(m_opaque, false);
166  return broadcaster;
167 }
168 
170  LLDB_INSTRUMENT();
171 
172  return Communication::GetStaticBroadcasterClass().AsCString();
173 }
Instrumentation.h
lldb_private::Communication::ReadThreadIsRunning
bool ReadThreadIsRunning()
Checks if there is a currently running read thread.
Definition: Communication.cpp:312
lldb_private::Communication::SetCloseOnEOF
void SetCloseOnEOF(bool b)
Definition: Communication.h:308
lldb::SBCommunication::Disconnect
lldb::ConnectionStatus Disconnect()
Definition: SBCommunication.cpp:88
Host.h
ReadThreadBytesReceived
static void ReadThreadBytesReceived(void *baton, const void *src, size_t src_len)
Definition: ScriptInterpreter.cpp:139
LLDB_INSTRUMENT
#define LLDB_INSTRUMENT()
Definition: Instrumentation.h:95
SBCommunication.h
lldb::SBCommunication::Write
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status)
Definition: SBCommunication.cpp:119
lldb::eConnectionStatusNoConnection
@ eConnectionStatusNoConnection
No connection.
Definition: lldb-enumerations.h:300
lldb::SBCommunication::ReadThreadIsRunning
bool ReadThreadIsRunning()
Definition: SBCommunication.cpp:144
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:417
lldb_private::Communication
Definition: Communication.h:84
lldb::SBCommunication::~SBCommunication
~SBCommunication()
Definition: SBCommunication.cpp:26
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:43
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:200
lldb::SBCommunication::IsConnected
bool IsConnected() const
Definition: SBCommunication.cpp:97
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:390
ConnectionFileDescriptor.h
lldb_private::Communication::StopReadThread
virtual bool StopReadThread(Status *error_ptr=nullptr)
Stops the read thread by cancelling it.
Definition: Communication.cpp:234
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:171
lldb::SBCommunication::Connect
lldb::ConnectionStatus Connect(const char *url)
Definition: SBCommunication.cpp:58
lldb::SBCommunication::ReadThreadStop
bool ReadThreadStop()
Definition: SBCommunication.cpp:138
lldb_private::Communication::Disconnect
lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr)
Disconnect the communications connection if one is currently connected.
Definition: Communication.cpp:90
lldb_private::Communication::HasConnection
bool HasConnection() const
Definition: Communication.cpp:119
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:123
uint32_t
lldb::SBCommunication::Read
size_t Read(void *dst, size_t dst_len, uint32_t timeout_usec, lldb::ConnectionStatus &status)
Definition: SBCommunication.cpp:103
lldb::SBCommunication::SetCloseOnEOF
void SetCloseOnEOF(bool b)
Definition: SBCommunication.cpp:51
lldb::SBCommunication::GetBroadcasterClass
static const char * GetBroadcasterClass()
Definition: SBCommunication.cpp:169
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb_private::Communication::IsConnected
bool IsConnected() const
Check if the connection is valid.
Definition: Communication.cpp:114
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_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb::eConnectionStatusLostConnection
@ eConnectionStatusLostConnection
Lost connection while connected to a valid connection.
Definition: lldb-enumerations.h:301
lldb::SBBroadcaster
Definition: SBBroadcaster.h:16
lldb::SBCommunication::ReadThreadStart
bool ReadThreadStart()
Definition: SBCommunication.cpp:132
lldb
Definition: SBAddress.h:15
lldb::SBCommunication::AdoptFileDesriptor
lldb::ConnectionStatus AdoptFileDesriptor(int fd, bool owns_fd)
Definition: SBCommunication.cpp:69
lldb::SBCommunication::SetReadThreadBytesReceivedCallback
bool SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
Definition: SBCommunication.cpp:150
lldb::SBCommunication::GetBroadcaster
lldb::SBBroadcaster GetBroadcaster()
Definition: SBCommunication.cpp:162
lldb::SBCommunication::IsValid
bool IsValid() const
Definition: SBCommunication.cpp:33
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:76