LLDB mainline
ConnectionGenericFileWindows.cpp
Go to the documentation of this file.
1//===-- ConnectionGenericFileWindows.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
11#include "lldb/Utility/Log.h"
12#include "lldb/Utility/Status.h"
14
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/ConvertUTF.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
23 : m_file(INVALID_HANDLE_VALUE), m_owns_file(false) {
24 ::ZeroMemory(&m_overlapped, sizeof(m_overlapped));
25 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
27}
28
30 : m_file(file), m_owns_file(owns_file) {
31 ::ZeroMemory(&m_overlapped, sizeof(m_overlapped));
32 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
34}
35
43
46 CreateEvent(nullptr, FALSE, FALSE, nullptr);
47
48 // Note, we should use a manual reset event for the hEvent argument of the
49 // OVERLAPPED. This is because both WaitForMultipleObjects and
50 // GetOverlappedResult (if you set the bWait argument to TRUE) will wait for
51 // the event to be signalled. If we use an auto-reset event,
52 // WaitForMultipleObjects will reset the event, return successfully, and then
53 // GetOverlappedResult will block since the event is no longer signalled.
55 ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
56}
57
59 return m_file && (m_file != INVALID_HANDLE_VALUE);
60}
61
63 Status *error_ptr) {
65 LLDB_LOGF(log, "%p ConnectionGenericFile::Connect (url = '%s')",
66 static_cast<void *>(this), path.str().c_str());
67
68 if (!path.consume_front("file://")) {
69 if (error_ptr)
71 "unsupported connection URL: '%s'", path.str().c_str());
73 }
74
75 if (IsConnected()) {
76 ConnectionStatus status = Disconnect(error_ptr);
77 if (status != eConnectionStatusSuccess)
78 return status;
79 }
80
81 // Open the file for overlapped access. If it does not exist, create it. We
82 // open it overlapped so that we can issue asynchronous reads and then use
83 // WaitForMultipleObjects to allow the read to be interrupted by an event
84 // object.
85 std::wstring wpath;
86 if (!llvm::ConvertUTF8toWide(path, wpath)) {
87 if (error_ptr)
88 *error_ptr = Status(1, eErrorTypeGeneric);
90 }
91 m_file = ::CreateFileW(wpath.c_str(), GENERIC_READ | GENERIC_WRITE,
92 FILE_SHARE_READ, nullptr, OPEN_ALWAYS,
93 FILE_FLAG_OVERLAPPED, nullptr);
94 if (m_file == INVALID_HANDLE_VALUE) {
95 if (error_ptr)
96 *error_ptr = Status(::GetLastError(), eErrorTypeWin32);
98 }
99
100 m_owns_file = true;
101 m_uri = path.str();
103}
104
107 LLDB_LOGF(log, "%p ConnectionGenericFile::Disconnect ()",
108 static_cast<void *>(this));
109
110 if (!IsConnected())
112
113 // Reset the handle so that after we unblock any pending reads, subsequent
114 // calls to Read() will see a disconnected state.
115 HANDLE old_file = m_file;
116 m_file = INVALID_HANDLE_VALUE;
117
118 // Set the disconnect event so that any blocking reads unblock, then cancel
119 // any pending IO operations.
120 ::CancelIoEx(old_file, &m_overlapped);
121
122 // Close the file handle if we owned it, but don't close the event handles.
123 // We could always reconnect with the same Connection instance.
124 if (m_owns_file)
125 ::CloseHandle(old_file);
126
127 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
128 m_owns_file = false;
129 m_uri.clear();
131}
132
133size_t ConnectionGenericFile::Read(void *dst, size_t dst_len,
134 const Timeout<std::micro> &timeout,
136 Status *error_ptr) {
137 if (error_ptr)
138 error_ptr->Clear();
139
140 auto finish = [&](size_t bytes, ConnectionStatus s, DWORD error_code) {
142 status = s;
143 if (error_ptr)
144 *error_ptr = Status(error_code, eErrorTypeWin32);
145
146 // kBytesAvailableEvent is a manual reset event. Make sure it gets reset
147 // here so that any subsequent operations don't immediately see bytes
148 // available.
152 LLDB_LOGF(log,
153 "%p ConnectionGenericFile::Read() handle = %p, dst = %p, "
154 "dst_len = %zu) => %zu, error = %s",
155 static_cast<void *>(this), m_file, dst, dst_len, bytes,
156 error_code ? Status(error_code, eErrorTypeWin32).AsCString()
157 : "");
158 return bytes;
159 };
160
161 if (!IsConnected())
162 return finish(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
163
164 BOOL read_result = FALSE;
165 DWORD read_error = ERROR_SUCCESS;
166 if (!m_read_pending) {
168 read_result = ::ReadFile(m_file, dst, dst_len, nullptr, &m_overlapped);
169 read_error = ::GetLastError();
170 }
171
172 if (!m_read_pending && !read_result && read_error != ERROR_IO_PENDING) {
173 if (read_error == ERROR_BROKEN_PIPE) {
174 // The write end of a pipe was closed. This is equivalent to EOF.
175 return finish(0, eConnectionStatusEndOfFile, 0);
176 }
177 // An unknown error occurred. Fail out.
178 return finish(0, eConnectionStatusError, read_error);
179 }
180
181 if (!read_result || m_read_pending) {
182 // The expected return path. The operation is pending. Wait for the
183 // operation to complete or be interrupted.
184 DWORD milliseconds =
185 timeout
186 ? std::chrono::duration_cast<std::chrono::milliseconds>(*timeout)
187 .count()
188 : INFINITE;
189 DWORD wait_result = ::WaitForMultipleObjects(
190 std::size(m_event_handles), m_event_handles, FALSE, milliseconds);
191 // All of the events are manual reset events, so make sure we reset them
192 // to non-signalled.
193 switch (wait_result) {
194 case WAIT_OBJECT_0 + kBytesAvailableEvent:
195 break;
196 case WAIT_OBJECT_0 + kInterruptEvent:
197 return finish(0, eConnectionStatusInterrupted, 0);
198 case WAIT_TIMEOUT:
199 return finish(0, eConnectionStatusTimedOut, 0);
200 case WAIT_FAILED:
201 return finish(0, eConnectionStatusError, ::GetLastError());
202 }
203 }
204
205 // The data is ready. Figure out how much was read and return;
206 DWORD bytes_read = 0;
207 if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_read, FALSE)) {
208 DWORD result_error = ::GetLastError();
209 // ERROR_OPERATION_ABORTED occurs when someone calls Disconnect() during
210 // a blocking read. This triggers a call to CancelIoEx, which causes the
211 // operation to complete and the result to be ERROR_OPERATION_ABORTED.
212 if (result_error == ERROR_HANDLE_EOF ||
213 result_error == ERROR_OPERATION_ABORTED ||
214 result_error == ERROR_BROKEN_PIPE)
215 return finish(bytes_read, eConnectionStatusEndOfFile, 0);
216 return finish(bytes_read, eConnectionStatusError, result_error);
217 }
218
219 if (bytes_read == 0)
220 return finish(0, eConnectionStatusEndOfFile, 0);
221 return finish(bytes_read, eConnectionStatusSuccess, 0);
222}
223
224size_t ConnectionGenericFile::Write(const void *src, size_t src_len,
226 Status *error_ptr) {
227 if (error_ptr)
228 error_ptr->Clear();
229
230 auto finish = [&](size_t bytes, ConnectionStatus s, DWORD error_code) {
231 status = s;
232 if (error_ptr)
233 *error_ptr = Status(error_code, eErrorTypeWin32);
236 LLDB_LOGF(log,
237 "%p ConnectionGenericFile::Write() handle = %p, src = %p, "
238 "src_len = %zu) => %zu, error = %s",
239 static_cast<void *>(this), m_file, src, src_len, bytes,
240 Status(error_code, eErrorTypeWin32).AsCString());
241 return bytes;
242 };
243
244 if (!IsConnected())
245 return finish(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
246
247 m_overlapped.hEvent = nullptr;
248
249 DWORD bytes_written = 0;
250 BOOL result = ::WriteFile(m_file, src, src_len, nullptr, &m_overlapped);
251 if (!result && ::GetLastError() != ERROR_IO_PENDING)
252 return finish(0, eConnectionStatusError, ::GetLastError());
253
254 if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_written, TRUE))
255 return finish(bytes_written, eConnectionStatusError, ::GetLastError());
256
257 return finish(bytes_written, eConnectionStatusSuccess, 0);
258}
259
260std::string ConnectionGenericFile::GetURI() { return m_uri; }
261
265
267 LARGE_INTEGER old_pos;
268 old_pos.HighPart = m_overlapped.OffsetHigh;
269 old_pos.LowPart = m_overlapped.Offset;
270 old_pos.QuadPart += amount;
271 m_overlapped.Offset = old_pos.LowPart;
272 m_overlapped.OffsetHigh = old_pos.HighPart;
273}
#define LLDB_LOGF(log,...)
Definition Log.h:378
void * HANDLE
lldb::ConnectionStatus Disconnect(Status *error_ptr) override
Disconnect the communications connection if one is currently connected.
lldb::ConnectionStatus Connect(llvm::StringRef s, Status *error_ptr) override
Connect using the connect string url.
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr) override
The actual write function that attempts to write to the communications protocol.
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr) override
The read function that attempts to read from the connection.
bool IsConnected() const override
Check if the connection is valid.
bool InterruptRead() override
Interrupts an ongoing Read() operation.
std::string GetURI() override
Returns a URI that describes this connection object.
An error handling class.
Definition Status.h:118
void Clear()
Clear the object state.
Definition Status.cpp:214
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
int file_t
Definition lldb-types.h:59
ConnectionStatus
Connection Status Types.
@ eConnectionStatusError
Check GetError() for details.
@ eConnectionStatusInterrupted
Interrupted read.
@ eConnectionStatusTimedOut
Request timed out.
@ eConnectionStatusEndOfFile
End-of-file encountered.
@ eConnectionStatusSuccess
Success.
@ eConnectionStatusNoConnection
No connection.
@ eErrorTypeGeneric
Generic errors that can be any value.
@ eErrorTypeWin32
Standard Win32 error codes.