Bug Summary

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