LLDB  mainline
Communication.cpp
Go to the documentation of this file.
1 //===-- Communication.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 
12 #include "lldb/Utility/LLDBLog.h"
13 #include "lldb/Utility/Log.h"
14 #include "lldb/Utility/Status.h"
15 
16 #include "llvm/Support/Compiler.h"
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <memory>
21 
22 #include <cerrno>
23 #include <cinttypes>
24 #include <cstdio>
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 Communication::Communication()
30  : m_connection_sp(), m_write_mutex(), m_close_on_eof(true) {
31 }
32 
34  Clear();
35 }
36 
38  Disconnect(nullptr);
39 }
40 
41 ConnectionStatus Communication::Connect(const char *url, Status *error_ptr) {
42  Clear();
43 
45  "{0} Communication::Connect (url = {1})", this, url);
46 
47  lldb::ConnectionSP connection_sp(m_connection_sp);
48  if (connection_sp)
49  return connection_sp->Connect(url, error_ptr);
50  if (error_ptr)
51  error_ptr->SetErrorString("Invalid connection.");
53 }
54 
56  LLDB_LOG(GetLog(LLDBLog::Communication), "{0} Communication::Disconnect ()",
57  this);
58 
59  lldb::ConnectionSP connection_sp(m_connection_sp);
60  if (connection_sp) {
61  ConnectionStatus status = connection_sp->Disconnect(error_ptr);
62  // We currently don't protect connection_sp with any mutex for multi-
63  // threaded environments. So lets not nuke our connection class without
64  // putting some multi-threaded protections in. We also probably don't want
65  // to pay for the overhead it might cause if every time we access the
66  // connection we have to take a lock.
67  //
68  // This unique pointer will cleanup after itself when this object goes
69  // away, so there is no need to currently have it destroy itself
70  // immediately upon disconnect.
71  // connection_sp.reset();
72  return status;
73  }
75 }
76 
78  lldb::ConnectionSP connection_sp(m_connection_sp);
79  return (connection_sp ? connection_sp->IsConnected() : false);
80 }
81 
83  return m_connection_sp.get() != nullptr;
84 }
85 
86 size_t Communication::Read(void *dst, size_t dst_len,
87  const Timeout<std::micro> &timeout,
88  ConnectionStatus &status, Status *error_ptr) {
90  LLDB_LOG(
91  log,
92  "this = {0}, dst = {1}, dst_len = {2}, timeout = {3}, connection = {4}",
93  this, dst, dst_len, timeout, m_connection_sp.get());
94 
95  return ReadFromConnection(dst, dst_len, timeout, status, error_ptr);
96 }
97 
98 size_t Communication::Write(const void *src, size_t src_len,
99  ConnectionStatus &status, Status *error_ptr) {
100  lldb::ConnectionSP connection_sp(m_connection_sp);
101 
102  std::lock_guard<std::mutex> guard(m_write_mutex);
104  "{0} Communication::Write (src = {1}, src_len = {2}"
105  ") connection = {3}",
106  this, src, (uint64_t)src_len, connection_sp.get());
107 
108  if (connection_sp)
109  return connection_sp->Write(src, src_len, status, error_ptr);
110 
111  if (error_ptr)
112  error_ptr->SetErrorString("Invalid connection.");
114  return 0;
115 }
116 
117 size_t Communication::WriteAll(const void *src, size_t src_len,
118  ConnectionStatus &status, Status *error_ptr) {
119  size_t total_written = 0;
120  do
121  total_written += Write(static_cast<const char *>(src) + total_written,
122  src_len - total_written, status, error_ptr);
123  while (status == eConnectionStatusSuccess && total_written < src_len);
124  return total_written;
125 }
126 
127 size_t Communication::ReadFromConnection(void *dst, size_t dst_len,
128  const Timeout<std::micro> &timeout,
129  ConnectionStatus &status,
130  Status *error_ptr) {
131  lldb::ConnectionSP connection_sp(m_connection_sp);
132  if (connection_sp)
133  return connection_sp->Read(dst, dst_len, timeout, status, error_ptr);
134 
135  if (error_ptr)
136  error_ptr->SetErrorString("Invalid connection.");
138  return 0;
139 }
140 
141 void Communication::SetConnection(std::unique_ptr<Connection> connection) {
142  Disconnect(nullptr);
143  m_connection_sp = std::move(connection);
144 }
145 
148  switch (status) {
150  return "success";
152  return "error";
154  return "timed out";
156  return "no connection";
158  return "lost connection";
160  return "end of file";
162  return "interrupted";
163  }
164 
165  return "@" + std::to_string(status);
166 }
lldb_private::Communication::ConnectionStatusAsString
static std::string ConnectionStatusAsString(lldb::ConnectionStatus status)
Definition: Communication.cpp:147
lldb_private::Communication::m_write_mutex
std::mutex m_write_mutex
Don't let multiple threads write at the same time...
Definition: Communication.h:171
lldb_private::Communication::m_connection_sp
lldb::ConnectionSP m_connection_sp
The connection that is current in use by this communications class.
Definition: Communication.h:168
lldb::eConnectionStatusNoConnection
@ eConnectionStatusNoConnection
No connection.
Definition: lldb-enumerations.h:301
lldb::eConnectionStatusEndOfFile
@ eConnectionStatusEndOfFile
End-of-file encountered.
Definition: lldb-enumerations.h:298
lldb_private::Communication::SetConnection
virtual void SetConnection(std::unique_ptr< Connection > connection)
Sets the connection that it to be used by this class.
Definition: Communication.cpp:141
lldb_private::Communication::WriteAll
size_t WriteAll(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr)
Repeatedly attempt writing until either src_len bytes are written or a permanent failure occurs.
Definition: Communication.cpp:117
lldb_private::Communication::ReadFromConnection
size_t ReadFromConnection(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr)
Definition: Communication.cpp:127
Log.h
lldb_private::Communication::Clear
virtual void Clear()
Definition: Communication.cpp:37
lldb::eConnectionStatusTimedOut
@ eConnectionStatusTimedOut
Request timed out.
Definition: lldb-enumerations.h:300
lldb::ConnectionStatus
ConnectionStatus
Connection Status Types.
Definition: lldb-enumerations.h:296
lldb::eConnectionStatusInterrupted
@ eConnectionStatusInterrupted
Interrupted read.
Definition: lldb-enumerations.h:304
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
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:98
lldb_private::Communication::~Communication
virtual ~Communication()
Destructor.
Definition: Communication.cpp:33
Connection.h
lldb_private::LLDBLog::Communication
@ Communication
lldb_private::Communication::Disconnect
virtual lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr)
Disconnect the communications connection if one is currently connected.
Definition: Communication.cpp:55
lldb_private::Communication::HasConnection
bool HasConnection() const
Definition: Communication.cpp:82
lldb_private::Status
Definition: Status.h:44
lldb_private::Timeout< std::micro >
lldb_private::Communication::Read
virtual 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:86
lldb::eConnectionStatusError
@ eConnectionStatusError
Check GetError() for details.
Definition: lldb-enumerations.h:299
lldb_private::Status::SetErrorString
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
lldb_private::Communication::IsConnected
bool IsConnected() const
Check if the connection is valid.
Definition: Communication.cpp:77
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
lldb::eConnectionStatusSuccess
@ eConnectionStatusSuccess
Success.
Definition: lldb-enumerations.h:297
Status.h
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:302
lldb_private::Log
Definition: Log.h:115
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
lldb
Definition: SBAddress.h:15
LLDBLog.h
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:41