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 m_read_pending = false;
122
123 // Close the file handle if we owned it, but don't close the event handles.
124 // We could always reconnect with the same Connection instance.
125 if (m_owns_file)
126 ::CloseHandle(old_file);
127
128 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
129 m_owns_file = false;
130 m_uri.clear();
132}
133
134size_t ConnectionGenericFile::Read(void *dst, size_t dst_len,
135 const Timeout<std::micro> &timeout,
137 Status *error_ptr) {
138 if (error_ptr)
139 error_ptr->Clear();
140
141 auto finish = [&](size_t bytes, ConnectionStatus s, DWORD error_code) {
144 status = s;
145 if (error_ptr)
146 *error_ptr = Status(error_code, eErrorTypeWin32);
147
148 // kBytesAvailableEvent is a manual reset event. Make sure it gets reset
149 // here so that any subsequent operations don't immediately see bytes
150 // available.
154 LLDB_LOGF(log,
155 "%p ConnectionGenericFile::Read() handle = %p, dst = %p, "
156 "dst_len = %zu) => %zu, error = %s",
157 static_cast<void *>(this), m_file, dst, dst_len, bytes,
158 error_code ? Status(error_code, eErrorTypeWin32).AsCString()
159 : "");
160 return bytes;
161 };
162
163 if (!IsConnected())
164 return finish(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
165
166 BOOL read_result = FALSE;
167 DWORD read_error = ERROR_SUCCESS;
168 if (!m_read_pending) {
170 read_result = ::ReadFile(m_file, dst, dst_len, nullptr, &m_overlapped);
171 read_error = ::GetLastError();
172 }
173
174 if (!m_read_pending && !read_result && read_error != ERROR_IO_PENDING) {
175 if (read_error == ERROR_BROKEN_PIPE) {
176 // The write end of a pipe was closed. This is equivalent to EOF.
177 return finish(0, eConnectionStatusEndOfFile, 0);
178 }
179 // An unknown error occurred. Fail out.
180 return finish(0, eConnectionStatusError, read_error);
181 }
182
183 if ((!m_read_pending && !read_result) ||
184 (m_read_pending && !HasOverlappedIoCompleted(&m_overlapped))) {
185 // The expected return path. The operation is pending. Wait for the
186 // operation to complete or be interrupted. A pending read that already
187 // completed skips the wait: finish() may have reset its event.
188 DWORD milliseconds =
189 timeout
190 ? std::chrono::duration_cast<std::chrono::milliseconds>(*timeout)
191 .count()
192 : INFINITE;
193 DWORD wait_result = ::WaitForMultipleObjects(
194 std::size(m_event_handles), m_event_handles, FALSE, milliseconds);
195 // All of the events are manual reset events, so make sure we reset them
196 // to non-signalled.
197 switch (wait_result) {
198 case WAIT_OBJECT_0 + kBytesAvailableEvent:
199 break;
200 case WAIT_OBJECT_0 + kInterruptEvent:
201 return finish(0, eConnectionStatusInterrupted, 0);
202 case WAIT_TIMEOUT:
203 return finish(0, eConnectionStatusTimedOut, 0);
204 case WAIT_FAILED:
205 return finish(0, eConnectionStatusError, ::GetLastError());
206 }
207 }
208
209 // The data is ready. Figure out how much was read and return;
210 DWORD bytes_read = 0;
211 if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_read, FALSE)) {
212 DWORD result_error = ::GetLastError();
213 // ERROR_OPERATION_ABORTED occurs when someone calls Disconnect() during
214 // a blocking read. This triggers a call to CancelIoEx, which causes the
215 // operation to complete and the result to be ERROR_OPERATION_ABORTED.
216 if (result_error == ERROR_HANDLE_EOF ||
217 result_error == ERROR_OPERATION_ABORTED ||
218 result_error == ERROR_BROKEN_PIPE)
219 return finish(bytes_read, eConnectionStatusEndOfFile, 0);
220 return finish(bytes_read, eConnectionStatusError, result_error);
221 }
222
223 if (bytes_read == 0)
224 return finish(0, eConnectionStatusEndOfFile, 0);
225 return finish(bytes_read, eConnectionStatusSuccess, 0);
226}
227
228size_t ConnectionGenericFile::Write(const void *src, size_t src_len,
230 Status *error_ptr) {
231 if (error_ptr)
232 error_ptr->Clear();
233
234 auto finish = [&](size_t bytes, ConnectionStatus s, DWORD error_code) {
235 status = s;
236 if (error_ptr)
237 *error_ptr = Status(error_code, eErrorTypeWin32);
240 LLDB_LOGF(log,
241 "%p ConnectionGenericFile::Write() handle = %p, src = %p, "
242 "src_len = %zu) => %zu, error = %s",
243 static_cast<void *>(this), m_file, src, src_len, bytes,
244 Status(error_code, eErrorTypeWin32).AsCString());
245 return bytes;
246 };
247
248 if (!IsConnected())
249 return finish(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
250
251 m_overlapped.hEvent = nullptr;
252
253 DWORD bytes_written = 0;
254 BOOL result = ::WriteFile(m_file, src, src_len, nullptr, &m_overlapped);
255 if (!result && ::GetLastError() != ERROR_IO_PENDING)
256 return finish(0, eConnectionStatusError, ::GetLastError());
257
258 if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_written, TRUE))
259 return finish(bytes_written, eConnectionStatusError, ::GetLastError());
260
261 return finish(bytes_written, eConnectionStatusSuccess, 0);
262}
263
264std::string ConnectionGenericFile::GetURI() { return m_uri; }
265
269
271 LARGE_INTEGER old_pos;
272 old_pos.HighPart = m_overlapped.OffsetHigh;
273 old_pos.LowPart = m_overlapped.Offset;
274 old_pos.QuadPart += amount;
275 m_overlapped.Offset = old_pos.LowPart;
276 m_overlapped.OffsetHigh = old_pos.HighPart;
277}
#define LLDB_LOGF(log,...)
Definition Log.h:389
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:338
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.