Bug Summary

File:build/source/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
Warning:line 555, column 5
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ConnectionFileDescriptorPosix.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-17/lib/clang/17 -isystem /usr/include/libxml2 -D HAVE_ROUND -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/lldb/source/Host -I /build/source/lldb/source/Host -I /build/source/lldb/include -I tools/lldb/include -I include -I /build/source/llvm/include -I /usr/include/python3.9 -I /build/source/clang/include -I tools/lldb/../clang/include -I /build/source/lldb/source -I tools/lldb/source -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-17/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1683717183 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-stringop-truncation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2023-05-10-133810-16478-1 -x c++ /build/source/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
1//===-- ConnectionFileDescriptorPosix.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
9#if defined(__APPLE__)
10// Enable this special support for Apple builds where we can have unlimited
11// select bounds. We tried switching to poll() and kqueue and we were panicing
12// the kernel, so we have to stick with select for now.
13#define _DARWIN_UNLIMITED_SELECT
14#endif
15
16#include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
17#include "lldb/Host/Config.h"
18#include "lldb/Host/Socket.h"
19#include "lldb/Host/SocketAddress.h"
20#include "lldb/Utility/LLDBLog.h"
21#include "lldb/Utility/SelectHelper.h"
22#include "lldb/Utility/Timeout.h"
23
24#include <cerrno>
25#include <cstdlib>
26#include <cstring>
27#include <fcntl.h>
28#include <sys/types.h>
29
30#if LLDB_ENABLE_POSIX1
31#include <termios.h>
32#include <unistd.h>
33#endif
34
35#include <memory>
36#include <sstream>
37
38#include "llvm/Support/Errno.h"
39#include "llvm/Support/ErrorHandling.h"
40#if defined(__APPLE__)
41#include "llvm/ADT/SmallVector.h"
42#endif
43#include "lldb/Host/Host.h"
44#include "lldb/Host/Socket.h"
45#include "lldb/Host/common/TCPSocket.h"
46#include "lldb/Host/common/UDPSocket.h"
47#include "lldb/Utility/Log.h"
48#include "lldb/Utility/StreamString.h"
49#include "lldb/Utility/Timer.h"
50
51using namespace lldb;
52using namespace lldb_private;
53
54ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
55 : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
56
57 m_child_processes_inherit(child_processes_inherit) {
58 Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
59 LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()"
, static_cast<void *>(this)); } while (0)
60 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()"
, static_cast<void *>(this)); } while (0)
;
61}
62
63ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
64 : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
65 m_child_processes_inherit(false) {
66 m_io_sp =
67 std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, owns_fd);
68
69 Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
70 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
"%i, owns_fd = %i)", static_cast<void *>(this), fd, owns_fd
); } while (0)
71 "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
"%i, owns_fd = %i)", static_cast<void *>(this), fd, owns_fd
); } while (0)
72 "%i, owns_fd = %i)",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
"%i, owns_fd = %i)", static_cast<void *>(this), fd, owns_fd
); } while (0)
73 static_cast<void *>(this), fd, owns_fd)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
"%i, owns_fd = %i)", static_cast<void *>(this), fd, owns_fd
); } while (0)
;
74 OpenCommandPipe();
75}
76
77ConnectionFileDescriptor::ConnectionFileDescriptor(Socket *socket)
78 : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
79 m_child_processes_inherit(false) {
80 InitializeSocket(socket);
81}
82
83ConnectionFileDescriptor::~ConnectionFileDescriptor() {
84 Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
85 LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()"
, static_cast<void *>(this)); } while (0)
86 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()"
, static_cast<void *>(this)); } while (0)
;
87 Disconnect(nullptr);
88 CloseCommandPipe();
89}
90
91void ConnectionFileDescriptor::OpenCommandPipe() {
92 CloseCommandPipe();
93
94 Log *log = GetLog(LLDBLog::Connection);
95 // Make the command file descriptor here:
96 Status result = m_pipe.CreateNew(m_child_processes_inherit);
97 if (!result.Success()) {
98 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
"make pipe: %s", static_cast<void *>(this), result.AsCString
()); } while (0)
99 "%p ConnectionFileDescriptor::OpenCommandPipe () - could not "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
"make pipe: %s", static_cast<void *>(this), result.AsCString
()); } while (0)
100 "make pipe: %s",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
"make pipe: %s", static_cast<void *>(this), result.AsCString
()); } while (0)
101 static_cast<void *>(this), result.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
"make pipe: %s", static_cast<void *>(this), result.AsCString
()); } while (0)
;
102 } else {
103 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success "
"readfd=%d writefd=%d", static_cast<void *>(this), m_pipe
.GetReadFileDescriptor(), m_pipe.GetWriteFileDescriptor()); }
while (0)
104 "%p ConnectionFileDescriptor::OpenCommandPipe() - success "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success "
"readfd=%d writefd=%d", static_cast<void *>(this), m_pipe
.GetReadFileDescriptor(), m_pipe.GetWriteFileDescriptor()); }
while (0)
105 "readfd=%d writefd=%d",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success "
"readfd=%d writefd=%d", static_cast<void *>(this), m_pipe
.GetReadFileDescriptor(), m_pipe.GetWriteFileDescriptor()); }
while (0)
106 static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success "
"readfd=%d writefd=%d", static_cast<void *>(this), m_pipe
.GetReadFileDescriptor(), m_pipe.GetWriteFileDescriptor()); }
while (0)
107 m_pipe.GetWriteFileDescriptor())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success "
"readfd=%d writefd=%d", static_cast<void *>(this), m_pipe
.GetReadFileDescriptor(), m_pipe.GetWriteFileDescriptor()); }
while (0)
;
108 }
109}
110
111void ConnectionFileDescriptor::CloseCommandPipe() {
112 Log *log = GetLog(LLDBLog::Connection);
113 LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::CloseCommandPipe()"
, static_cast<void *>(this)); } while (0)
114 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::CloseCommandPipe()"
, static_cast<void *>(this)); } while (0)
;
115
116 m_pipe.Close();
117}
118
119bool ConnectionFileDescriptor::IsConnected() const {
120 return m_io_sp && m_io_sp->IsValid();
121}
122
123ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
124 Status *error_ptr) {
125 return Connect(
126 path, [](llvm::StringRef) {}, error_ptr);
127}
128
129ConnectionStatus
130ConnectionFileDescriptor::Connect(llvm::StringRef path,
131 socket_id_callback_type socket_id_callback,
132 Status *error_ptr) {
133 std::lock_guard<std::recursive_mutex> guard(m_mutex);
134 Log *log = GetLog(LLDBLog::Connection);
135 LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Connect (url = '%s')"
, static_cast<void *>(this), path.str().c_str()); } while
(0)
136 static_cast<void *>(this), path.str().c_str())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Connect (url = '%s')"
, static_cast<void *>(this), path.str().c_str()); } while
(0)
;
137
138 OpenCommandPipe();
139
140 if (path.empty()) {
141 if (error_ptr)
142 error_ptr->SetErrorString("invalid connect arguments");
143 return eConnectionStatusError;
144 }
145
146 llvm::StringRef scheme;
147 std::tie(scheme, path) = path.split("://");
148
149 if (!path.empty()) {
150 auto method =
151 llvm::StringSwitch<ConnectionStatus (ConnectionFileDescriptor::*)(
152 llvm::StringRef, socket_id_callback_type, Status *)>(scheme)
153 .Case("listen", &ConnectionFileDescriptor::AcceptTCP)
154 .Cases("accept", "unix-accept",
155 &ConnectionFileDescriptor::AcceptNamedSocket)
156 .Case("unix-abstract-accept",
157 &ConnectionFileDescriptor::AcceptAbstractSocket)
158 .Cases("connect", "tcp-connect",
159 &ConnectionFileDescriptor::ConnectTCP)
160 .Case("udp", &ConnectionFileDescriptor::ConnectUDP)
161 .Case("unix-connect", &ConnectionFileDescriptor::ConnectNamedSocket)
162 .Case("unix-abstract-connect",
163 &ConnectionFileDescriptor::ConnectAbstractSocket)
164#if LLDB_ENABLE_POSIX1
165 .Case("fd", &ConnectionFileDescriptor::ConnectFD)
166 .Case("file", &ConnectionFileDescriptor::ConnectFile)
167 .Case("serial", &ConnectionFileDescriptor::ConnectSerialPort)
168#endif
169 .Default(nullptr);
170
171 if (method) {
172 if (error_ptr)
173 *error_ptr = Status();
174 return (this->*method)(path, socket_id_callback, error_ptr);
175 }
176 }
177
178 if (error_ptr)
179 error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
180 path.str().c_str());
181 return eConnectionStatusError;
182}
183
184bool ConnectionFileDescriptor::InterruptRead() {
185 size_t bytes_written = 0;
186 Status result = m_pipe.Write("i", 1, bytes_written);
187 return result.Success();
188}
189
190ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) {
191 Log *log = GetLog(LLDBLog::Connection);
192 LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect ()"
, static_cast<void *>(this)); } while (0)
193 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect ()"
, static_cast<void *>(this)); } while (0)
;
194
195 ConnectionStatus status = eConnectionStatusSuccess;
196
197 if (!IsConnected()) {
198 LLDB_LOGF(do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect"
, static_cast<void *>(this)); } while (0)
199 log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect"
, static_cast<void *>(this)); } while (0)
200 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect"
, static_cast<void *>(this)); } while (0)
;
201 return eConnectionStatusSuccess;
202 }
203
204 if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
205 static_cast<Socket &>(*m_io_sp).PreDisconnect();
206
207 // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is
208 // quite likely because somebody is doing a blocking read on our file
209 // descriptor. If that's the case, then send the "q" char to the command
210 // file channel so the read will wake up and the connection will then know to
211 // shut down.
212 std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
213 if (!locker.try_lock()) {
214 if (m_pipe.CanWrite()) {
215 size_t bytes_written = 0;
216 Status result = m_pipe.Write("q", 1, bytes_written);
217 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
"the lock, sent 'q' to %d, error = '%s'.", static_cast<void
*>(this), m_pipe.GetWriteFileDescriptor(), result.AsCString
()); } while (0)
218 "%p ConnectionFileDescriptor::Disconnect(): Couldn't get "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
"the lock, sent 'q' to %d, error = '%s'.", static_cast<void
*>(this), m_pipe.GetWriteFileDescriptor(), result.AsCString
()); } while (0)
219 "the lock, sent 'q' to %d, error = '%s'.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
"the lock, sent 'q' to %d, error = '%s'.", static_cast<void
*>(this), m_pipe.GetWriteFileDescriptor(), result.AsCString
()); } while (0)
220 static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
"the lock, sent 'q' to %d, error = '%s'.", static_cast<void
*>(this), m_pipe.GetWriteFileDescriptor(), result.AsCString
()); } while (0)
221 result.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
"the lock, sent 'q' to %d, error = '%s'.", static_cast<void
*>(this), m_pipe.GetWriteFileDescriptor(), result.AsCString
()); } while (0)
;
222 } else if (log) {
223 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
"lock, but no command pipe is available.", static_cast<void
*>(this)); } while (0)
224 "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
"lock, but no command pipe is available.", static_cast<void
*>(this)); } while (0)
225 "lock, but no command pipe is available.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
"lock, but no command pipe is available.", static_cast<void
*>(this)); } while (0)
226 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
"lock, but no command pipe is available.", static_cast<void
*>(this)); } while (0)
;
227 }
228 locker.lock();
229 }
230
231 // Prevents reads and writes during shutdown.
232 m_shutting_down = true;
233
234 Status error = m_io_sp->Close();
235 if (error.Fail())
236 status = eConnectionStatusError;
237 if (error_ptr)
238 *error_ptr = error;
239
240 // Close any pipes we were using for async interrupts
241 m_pipe.Close();
242
243 m_uri.clear();
244 m_shutting_down = false;
245 return status;
246}
247
248size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
249 const Timeout<std::micro> &timeout,
250 ConnectionStatus &status,
251 Status *error_ptr) {
252 Log *log = GetLog(LLDBLog::Connection);
253
254 std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
255 if (!locker.try_lock()) {
256 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read () failed to get the "
"connection lock.", static_cast<void *>(this)); } while
(0)
257 "%p ConnectionFileDescriptor::Read () failed to get the "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read () failed to get the "
"connection lock.", static_cast<void *>(this)); } while
(0)
258 "connection lock.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read () failed to get the "
"connection lock.", static_cast<void *>(this)); } while
(0)
259 static_cast<void *>(this))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read () failed to get the "
"connection lock.", static_cast<void *>(this)); } while
(0)
;
260 if (error_ptr)
261 error_ptr->SetErrorString("failed to get the connection lock for read.");
262
263 status = eConnectionStatusTimedOut;
264 return 0;
265 }
266
267 if (m_shutting_down) {
268 if (error_ptr)
269 error_ptr->SetErrorString("shutting down");
270 status = eConnectionStatusError;
271 return 0;
272 }
273
274 status = BytesAvailable(timeout, error_ptr);
275 if (status != eConnectionStatusSuccess)
276 return 0;
277
278 Status error;
279 size_t bytes_read = dst_len;
280 error = m_io_sp->Read(dst, bytes_read);
281
282 if (log) {
283 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
284 "%p ConnectionFileDescriptor::Read() fd = %" PRIu64do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
285 ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
286 static_cast<void *>(this),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
287 static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
288 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
289 static_cast<uint64_t>(bytes_read), error.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Read() fd = %"
"l" "u" ", dst = %p, dst_len = %" "l" "u" ") => %" "l" "u"
", error = %s", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<void *>(dst), static_cast<uint64_t>(dst_len), static_cast
<uint64_t>(bytes_read), error.AsCString()); } while (0)
;
290 }
291
292 if (bytes_read == 0) {
293 error.Clear(); // End-of-file. Do not automatically close; pass along for
294 // the end-of-file handlers.
295 status = eConnectionStatusEndOfFile;
296 }
297
298 if (error_ptr)
299 *error_ptr = error;
300
301 if (error.Fail()) {
302 uint32_t error_value = error.GetError();
303 switch (error_value) {
304 case EAGAIN11: // The file was marked for non-blocking I/O, and no data were
305 // ready to be read.
306 if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
307 status = eConnectionStatusTimedOut;
308 else
309 status = eConnectionStatusSuccess;
310 return 0;
311
312 case EFAULT14: // Buf points outside the allocated address space.
313 case EINTR4: // A read from a slow device was interrupted before any data
314 // arrived by the delivery of a signal.
315 case EINVAL22: // The pointer associated with fildes was negative.
316 case EIO5: // An I/O error occurred while reading from the file system.
317 // The process group is orphaned.
318 // The file is a regular file, nbyte is greater than 0, the
319 // starting position is before the end-of-file, and the
320 // starting position is greater than or equal to the offset
321 // maximum established for the open file descriptor
322 // associated with fildes.
323 case EISDIR21: // An attempt is made to read a directory.
324 case ENOBUFS105: // An attempt to allocate a memory buffer fails.
325 case ENOMEM12: // Insufficient memory is available.
326 status = eConnectionStatusError;
327 break; // Break to close....
328
329 case ENOENT2: // no such file or directory
330 case EBADF9: // fildes is not a valid file or socket descriptor open for
331 // reading.
332 case ENXIO6: // An action is requested of a device that does not exist..
333 // A requested action cannot be performed by the device.
334 case ECONNRESET104: // The connection is closed by the peer during a read
335 // attempt on a socket.
336 case ENOTCONN107: // A read is attempted on an unconnected socket.
337 status = eConnectionStatusLostConnection;
338 break; // Break to close....
339
340 case ETIMEDOUT110: // A transmission timeout occurs during a read attempt on a
341 // socket.
342 status = eConnectionStatusTimedOut;
343 return 0;
344
345 default:
346 LLDB_LOG(log, "this = {0}, unexpected error: {1}", this,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp"
, __func__, "this = {0}, unexpected error: {1}", this, llvm::
sys::StrError(error_value)); } while (0)
347 llvm::sys::StrError(error_value))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp"
, __func__, "this = {0}, unexpected error: {1}", this, llvm::
sys::StrError(error_value)); } while (0)
;
348 status = eConnectionStatusError;
349 break; // Break to close....
350 }
351
352 return 0;
353 }
354 return bytes_read;
355}
356
357size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
358 ConnectionStatus &status,
359 Status *error_ptr) {
360 Log *log = GetLog(LLDBLog::Connection);
361 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write (src = %p, src_len = %"
"l" "u" ")", static_cast<void *>(this), static_cast<
const void *>(src), static_cast<uint64_t>(src_len));
} while (0)
362 "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write (src = %p, src_len = %"
"l" "u" ")", static_cast<void *>(this), static_cast<
const void *>(src), static_cast<uint64_t>(src_len));
} while (0)
363 ")",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write (src = %p, src_len = %"
"l" "u" ")", static_cast<void *>(this), static_cast<
const void *>(src), static_cast<uint64_t>(src_len));
} while (0)
364 static_cast<void *>(this), static_cast<const void *>(src),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write (src = %p, src_len = %"
"l" "u" ")", static_cast<void *>(this), static_cast<
const void *>(src), static_cast<uint64_t>(src_len));
} while (0)
365 static_cast<uint64_t>(src_len))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write (src = %p, src_len = %"
"l" "u" ")", static_cast<void *>(this), static_cast<
const void *>(src), static_cast<uint64_t>(src_len));
} while (0)
;
366
367 if (!IsConnected()) {
368 if (error_ptr)
369 error_ptr->SetErrorString("not connected");
370 status = eConnectionStatusNoConnection;
371 return 0;
372 }
373
374 if (m_shutting_down) {
375 if (error_ptr)
376 error_ptr->SetErrorString("shutting down");
377 status = eConnectionStatusError;
378 return 0;
379 }
380
381 Status error;
382
383 size_t bytes_sent = src_len;
384 error = m_io_sp->Write(src, bytes_sent);
385
386 if (log) {
387 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
388 "%p ConnectionFileDescriptor::Write(fd = %" PRIu64do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
389 ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
390 static_cast<void *>(this),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
391 static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
392 static_cast<const void *>(src), static_cast<uint64_t>(src_len),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
393 static_cast<uint64_t>(bytes_sent), error.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::Write(fd = %"
"l" "u" ", src = %p, src_len = %" "l" "u" ") => %" "l" "u"
" (error = %s)", static_cast<void *>(this), static_cast
<uint64_t>(m_io_sp->GetWaitableHandle()), static_cast
<const void *>(src), static_cast<uint64_t>(src_len
), static_cast<uint64_t>(bytes_sent), error.AsCString()
); } while (0)
;
394 }
395
396 if (error_ptr)
397 *error_ptr = error;
398
399 if (error.Fail()) {
400 switch (error.GetError()) {
401 case EAGAIN11:
402 case EINTR4:
403 status = eConnectionStatusSuccess;
404 return 0;
405
406 case ECONNRESET104: // The connection is closed by the peer during a read
407 // attempt on a socket.
408 case ENOTCONN107: // A read is attempted on an unconnected socket.
409 status = eConnectionStatusLostConnection;
410 break; // Break to close....
411
412 default:
413 status = eConnectionStatusError;
414 break; // Break to close....
415 }
416
417 return 0;
418 }
419
420 status = eConnectionStatusSuccess;
421 return bytes_sent;
422}
423
424std::string ConnectionFileDescriptor::GetURI() { return m_uri; }
425
426// This ConnectionFileDescriptor::BytesAvailable() uses select() via
427// SelectHelper
428//
429// PROS:
430// - select is consistent across most unix platforms
431// - The Apple specific version allows for unlimited fds in the fd_sets by
432// setting the _DARWIN_UNLIMITED_SELECT define prior to including the
433// required header files.
434// CONS:
435// - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
436// This implementation will assert if it runs into that hard limit to let
437// users know that another ConnectionFileDescriptor::BytesAvailable() should
438// be used or a new version of ConnectionFileDescriptor::BytesAvailable()
439// should be written for the system that is running into the limitations.
440
441ConnectionStatus
442ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
443 Status *error_ptr) {
444 // Don't need to take the mutex here separately since we are only called from
445 // Read. If we ever get used more generally we will need to lock here as
446 // well.
447
448 Log *log = GetLog(LLDBLog::Connection);
449 LLDB_LOG(log, "this = {0}, timeout = {1}", this, timeout)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp"
, __func__, "this = {0}, timeout = {1}", this, timeout); } while
(0)
;
450
451 // Make a copy of the file descriptors to make sure we don't have another
452 // thread change these values out from under us and cause problems in the
453 // loop below where like in FS_SET()
454 const IOObject::WaitableHandle handle = m_io_sp->GetWaitableHandle();
455 const int pipe_fd = m_pipe.GetReadFileDescriptor();
456
457 if (handle != IOObject::kInvalidHandleValue) {
458 SelectHelper select_helper;
459 if (timeout)
460 select_helper.SetTimeout(*timeout);
461
462 select_helper.FDSetRead(handle);
463#if defined(_WIN32)
464 // select() won't accept pipes on Windows. The entire Windows codepath
465 // needs to be converted over to using WaitForMultipleObjects and event
466 // HANDLEs, but for now at least this will allow ::select() to not return
467 // an error.
468 const bool have_pipe_fd = false;
469#else
470 const bool have_pipe_fd = pipe_fd >= 0;
471#endif
472 if (have_pipe_fd)
473 select_helper.FDSetRead(pipe_fd);
474
475 while (handle == m_io_sp->GetWaitableHandle()) {
476
477 Status error = select_helper.Select();
478
479 if (error_ptr)
480 *error_ptr = error;
481
482 if (error.Fail()) {
483 switch (error.GetError()) {
484 case EBADF9: // One of the descriptor sets specified an invalid
485 // descriptor.
486 return eConnectionStatusLostConnection;
487
488 case EINVAL22: // The specified time limit is invalid. One of its
489 // components is negative or too large.
490 default: // Other unknown error
491 return eConnectionStatusError;
492
493 case ETIMEDOUT110:
494 return eConnectionStatusTimedOut;
495
496 case EAGAIN11: // The kernel was (perhaps temporarily) unable to
497 // allocate the requested number of file descriptors, or
498 // we have non-blocking IO
499 case EINTR4: // A signal was delivered before the time limit
500 // expired and before any of the selected events occurred.
501 break; // Lets keep reading to until we timeout
502 }
503 } else {
504 if (select_helper.FDIsSetRead(handle))
505 return eConnectionStatusSuccess;
506
507 if (select_helper.FDIsSetRead(pipe_fd)) {
508 // There is an interrupt or exit command in the command pipe Read the
509 // data from that pipe:
510 char c;
511
512 ssize_t bytes_read =
513 llvm::sys::RetryAfterSignal(-1, ::read, pipe_fd, &c, 1);
514 assert(bytes_read == 1)(static_cast <bool> (bytes_read == 1) ? void (0) : __assert_fail
("bytes_read == 1", "lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp"
, 514, __extension__ __PRETTY_FUNCTION__))
;
515 (void)bytes_read;
516 switch (c) {
517 case 'q':
518 LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::BytesAvailable() "
"got data: %c from the command channel.", static_cast<void
*>(this), c); } while (0)
519 "%p ConnectionFileDescriptor::BytesAvailable() "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::BytesAvailable() "
"got data: %c from the command channel.", static_cast<void
*>(this), c); } while (0)
520 "got data: %c from the command channel.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::BytesAvailable() "
"got data: %c from the command channel.", static_cast<void
*>(this), c); } while (0)
521 static_cast<void *>(this), c)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Printf("%p ConnectionFileDescriptor::BytesAvailable() "
"got data: %c from the command channel.", static_cast<void
*>(this), c); } while (0)
;
522 return eConnectionStatusEndOfFile;
523 case 'i':
524 // Interrupt the current read
525 return eConnectionStatusInterrupted;
526 }
527 }
528 }
529 }
530 }
531
532 if (error_ptr)
533 error_ptr->SetErrorString("not connected");
534 return eConnectionStatusLostConnection;
535}
536
537lldb::ConnectionStatus ConnectionFileDescriptor::AcceptSocket(
538 Socket::SocketProtocol socket_protocol, llvm::StringRef socket_name,
539 llvm::function_ref<void(Socket &)> post_listen_callback,
540 Status *error_ptr) {
541 Status error;
542 std::unique_ptr<Socket> listening_socket =
543 Socket::Create(socket_protocol, m_child_processes_inherit, error);
544 Socket *accepted_socket;
2
'accepted_socket' declared without an initial value
545
546 if (!error.Fail())
3
Assuming the condition is false
4
Taking false branch
547 error = listening_socket->Listen(socket_name, 5);
548
549 if (!error.Fail()) {
5
Assuming the condition is false
6
Taking false branch
550 post_listen_callback(*listening_socket);
551 error = listening_socket->Accept(accepted_socket);
552 }
553
554 if (!error.Fail()) {
7
Assuming the condition is true
8
Taking true branch
555 m_io_sp.reset(accepted_socket);
9
1st function call argument is an uninitialized value
556 m_uri.assign(socket_name.str());
557 return eConnectionStatusSuccess;
558 }
559
560 if (error_ptr)
561 *error_ptr = error;
562 return eConnectionStatusError;
563}
564
565lldb::ConnectionStatus
566ConnectionFileDescriptor::ConnectSocket(Socket::SocketProtocol socket_protocol,
567 llvm::StringRef socket_name,
568 Status *error_ptr) {
569 Status error;
570 std::unique_ptr<Socket> socket =
571 Socket::Create(socket_protocol, m_child_processes_inherit, error);
572
573 if (!error.Fail())
574 error = socket->Connect(socket_name);
575
576 if (!error.Fail()) {
577 m_io_sp = std::move(socket);
578 m_uri.assign(socket_name.str());
579 return eConnectionStatusSuccess;
580 }
581
582 if (error_ptr)
583 *error_ptr = error;
584 return eConnectionStatusError;
585}
586
587ConnectionStatus ConnectionFileDescriptor::AcceptNamedSocket(
588 llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
589 Status *error_ptr) {
590 return AcceptSocket(
591 Socket::ProtocolUnixDomain, socket_name,
592 [socket_id_callback, socket_name](Socket &listening_socket) {
593 socket_id_callback(socket_name);
594 },
595 error_ptr);
596}
597
598ConnectionStatus ConnectionFileDescriptor::ConnectNamedSocket(
599 llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
600 Status *error_ptr) {
601 return ConnectSocket(Socket::ProtocolUnixDomain, socket_name, error_ptr);
602}
603
604ConnectionStatus ConnectionFileDescriptor::AcceptAbstractSocket(
605 llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
606 Status *error_ptr) {
607 return AcceptSocket(
608 Socket::ProtocolUnixAbstract, socket_name,
609 [socket_id_callback, socket_name](Socket &listening_socket) {
610 socket_id_callback(socket_name);
611 },
612 error_ptr);
613}
614
615lldb::ConnectionStatus ConnectionFileDescriptor::ConnectAbstractSocket(
616 llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
617 Status *error_ptr) {
618 return ConnectSocket(Socket::ProtocolUnixAbstract, socket_name, error_ptr);
619}
620
621ConnectionStatus
622ConnectionFileDescriptor::AcceptTCP(llvm::StringRef socket_name,
623 socket_id_callback_type socket_id_callback,
624 Status *error_ptr) {
625 ConnectionStatus ret = AcceptSocket(
1
Calling 'ConnectionFileDescriptor::AcceptSocket'
626 Socket::ProtocolTcp, socket_name,
627 [socket_id_callback](Socket &listening_socket) {
628 uint16_t port =
629 static_cast<TCPSocket &>(listening_socket).GetLocalPortNumber();
630 socket_id_callback(std::to_string(port));
631 },
632 error_ptr);
633 if (ret == eConnectionStatusSuccess)
634 m_uri.assign(
635 static_cast<TCPSocket *>(m_io_sp.get())->GetRemoteConnectionURI());
636 return ret;
637}
638
639ConnectionStatus
640ConnectionFileDescriptor::ConnectTCP(llvm::StringRef socket_name,
641 socket_id_callback_type socket_id_callback,
642 Status *error_ptr) {
643 return ConnectSocket(Socket::ProtocolTcp, socket_name, error_ptr);
644}
645
646ConnectionStatus
647ConnectionFileDescriptor::ConnectUDP(llvm::StringRef s,
648 socket_id_callback_type socket_id_callback,
649 Status *error_ptr) {
650 if (error_ptr)
651 *error_ptr = Status();
652 llvm::Expected<std::unique_ptr<UDPSocket>> socket =
653 Socket::UdpConnect(s, m_child_processes_inherit);
654 if (!socket) {
655 if (error_ptr)
656 *error_ptr = socket.takeError();
657 else
658 LLDB_LOG_ERROR(GetLog(LLDBLog::Connection), socket.takeError(),do { ::lldb_private::Log *log_private = (GetLog(LLDBLog::Connection
)); ::llvm::Error error_private = (socket.takeError()); if (log_private
&& error_private) { log_private->FormatError(::std
::move(error_private), "lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp"
, __func__, "tcp connect failed: {0}"); } else ::llvm::consumeError
(::std::move(error_private)); } while (0)
659 "tcp connect failed: {0}")do { ::lldb_private::Log *log_private = (GetLog(LLDBLog::Connection
)); ::llvm::Error error_private = (socket.takeError()); if (log_private
&& error_private) { log_private->FormatError(::std
::move(error_private), "lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp"
, __func__, "tcp connect failed: {0}"); } else ::llvm::consumeError
(::std::move(error_private)); } while (0)
;
660 return eConnectionStatusError;
661 }
662 m_io_sp = std::move(*socket);
663 m_uri.assign(std::string(s));
664 return eConnectionStatusSuccess;
665}
666
667ConnectionStatus
668ConnectionFileDescriptor::ConnectFD(llvm::StringRef s,
669 socket_id_callback_type socket_id_callback,
670 Status *error_ptr) {
671#if LLDB_ENABLE_POSIX1
672 // Just passing a native file descriptor within this current process that
673 // is already opened (possibly from a service or other source).
674 int fd = -1;
675
676 if (!s.getAsInteger(0, fd)) {
677 // We have what looks to be a valid file descriptor, but we should make
678 // sure it is. We currently are doing this by trying to get the flags
679 // from the file descriptor and making sure it isn't a bad fd.
680 errno(*__errno_location ()) = 0;
681 int flags = ::fcntl(fd, F_GETFL3, 0);
682 if (flags == -1 || errno(*__errno_location ()) == EBADF9) {
683 if (error_ptr)
684 error_ptr->SetErrorStringWithFormat("stale file descriptor: %s",
685 s.str().c_str());
686 m_io_sp.reset();
687 return eConnectionStatusError;
688 } else {
689 // Don't take ownership of a file descriptor that gets passed to us
690 // since someone else opened the file descriptor and handed it to us.
691 // TODO: Since are using a URL to open connection we should
692 // eventually parse options using the web standard where we have
693 // "fd://123?opt1=value;opt2=value" and we can have an option be
694 // "owns=1" or "owns=0" or something like this to allow us to specify
695 // this. For now, we assume we must assume we don't own it.
696
697 std::unique_ptr<TCPSocket> tcp_socket;
698 tcp_socket = std::make_unique<TCPSocket>(fd, false, false);
699 // Try and get a socket option from this file descriptor to see if
700 // this is a socket and set m_is_socket accordingly.
701 int resuse;
702 bool is_socket =
703 !!tcp_socket->GetOption(SOL_SOCKET1, SO_REUSEADDR2, resuse);
704 if (is_socket)
705 m_io_sp = std::move(tcp_socket);
706 else
707 m_io_sp =
708 std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, false);
709 m_uri = s.str();
710 return eConnectionStatusSuccess;
711 }
712 }
713
714 if (error_ptr)
715 error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"%s\"",
716 s.str().c_str());
717 m_io_sp.reset();
718 return eConnectionStatusError;
719#endif // LLDB_ENABLE_POSIX
720 llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX")::llvm::llvm_unreachable_internal("this function should be only called w/ LLDB_ENABLE_POSIX"
, "lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp",
720)
;
721}
722
723ConnectionStatus ConnectionFileDescriptor::ConnectFile(
724 llvm::StringRef s, socket_id_callback_type socket_id_callback,
725 Status *error_ptr) {
726#if LLDB_ENABLE_POSIX1
727 std::string addr_str = s.str();
728 // file:///PATH
729 int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR02);
730 if (fd == -1) {
731 if (error_ptr)
732 error_ptr->SetErrorToErrno();
733 return eConnectionStatusError;
734 }
735
736 if (::isatty(fd)) {
737 // Set up serial terminal emulation
738 struct termios options;
739 ::tcgetattr(fd, &options);
740
741 // Set port speed to maximum
742 ::cfsetospeed(&options, B1152000010002);
743 ::cfsetispeed(&options, B1152000010002);
744
745 // Raw input, disable echo and signals
746 options.c_lflag &= ~(ICANON0000002 | ECHO0000010 | ECHOE0000020 | ISIG0000001);
747
748 // Make sure only one character is needed to return from a read
749 options.c_cc[VMIN6] = 1;
750 options.c_cc[VTIME5] = 0;
751
752 llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW0, &options);
753 }
754
755 m_io_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, true);
756 return eConnectionStatusSuccess;
757#endif // LLDB_ENABLE_POSIX
758 llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX")::llvm::llvm_unreachable_internal("this function should be only called w/ LLDB_ENABLE_POSIX"
, "lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp",
758)
;
759}
760
761ConnectionStatus ConnectionFileDescriptor::ConnectSerialPort(
762 llvm::StringRef s, socket_id_callback_type socket_id_callback,
763 Status *error_ptr) {
764#if LLDB_ENABLE_POSIX1
765 llvm::StringRef path, qs;
766 // serial:///PATH?k1=v1&k2=v2...
767 std::tie(path, qs) = s.split('?');
768
769 llvm::Expected<SerialPort::Options> serial_options =
770 SerialPort::OptionsFromURL(qs);
771 if (!serial_options) {
772 if (error_ptr)
773 *error_ptr = serial_options.takeError();
774 else
775 llvm::consumeError(serial_options.takeError());
776 return eConnectionStatusError;
777 }
778
779 int fd = llvm::sys::RetryAfterSignal(-1, ::open, path.str().c_str(), O_RDWR02);
780 if (fd == -1) {
781 if (error_ptr)
782 error_ptr->SetErrorToErrno();
783 return eConnectionStatusError;
784 }
785
786 llvm::Expected<std::unique_ptr<SerialPort>> serial_sp = SerialPort::Create(
787 fd, File::eOpenOptionReadWrite, serial_options.get(), true);
788 if (!serial_sp) {
789 if (error_ptr)
790 *error_ptr = serial_sp.takeError();
791 else
792 llvm::consumeError(serial_sp.takeError());
793 return eConnectionStatusError;
794 }
795 m_io_sp = std::move(serial_sp.get());
796
797 return eConnectionStatusSuccess;
798#endif // LLDB_ENABLE_POSIX
799 llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX")::llvm::llvm_unreachable_internal("this function should be only called w/ LLDB_ENABLE_POSIX"
, "lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp",
799)
;
800}
801
802bool ConnectionFileDescriptor::GetChildProcessesInherit() const {
803 return m_child_processes_inherit;
804}
805
806void ConnectionFileDescriptor::SetChildProcessesInherit(
807 bool child_processes_inherit) {
808 m_child_processes_inherit = child_processes_inherit;
809}
810
811void ConnectionFileDescriptor::InitializeSocket(Socket *socket) {
812 m_io_sp.reset(socket);
813 m_uri = socket->GetRemoteConnectionURI();
814}