1 //===-- ConnectionGenericFileWindows.cpp ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Error.h"
11 #include "lldb/Core/Log.h"
12 #include "lldb/Host/TimeValue.h"
13 #include "lldb/Host/windows/ConnectionGenericFileWindows.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
19 using namespace lldb_private;
23 // This is a simple helper class to package up the information needed to return from a Read/Write
24 // operation function. Since there is a lot of code to be run before exit regardless of whether the
25 // operation succeeded or failed, combined with many possible return paths, this is the cleanest
26 // way to represent it.
31 Set(size_t bytes, ConnectionStatus status, DWORD error_code)
33 m_error.SetError(error_code, eErrorTypeWin32);
39 Set(size_t bytes, ConnectionStatus status, llvm::StringRef error_msg)
41 m_error.SetErrorString(error_msg.data());
65 ConnectionStatus m_status;
69 ConnectionGenericFile::ConnectionGenericFile()
70 : m_file(INVALID_HANDLE_VALUE)
73 ::ZeroMemory(&m_overlapped, sizeof(m_overlapped));
74 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
75 InitializeEventHandles();
78 ConnectionGenericFile::ConnectionGenericFile(lldb::file_t file, bool owns_file)
80 , m_owns_file(owns_file)
82 ::ZeroMemory(&m_overlapped, sizeof(m_overlapped));
83 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
84 InitializeEventHandles();
87 ConnectionGenericFile::~ConnectionGenericFile()
89 if (m_owns_file && IsConnected())
90 ::CloseHandle(m_file);
92 ::CloseHandle(m_event_handles[kBytesAvailableEvent]);
93 ::CloseHandle(m_event_handles[kInterruptEvent]);
97 ConnectionGenericFile::InitializeEventHandles()
99 m_event_handles[kInterruptEvent] = CreateEvent(NULL, FALSE, FALSE, NULL);
101 // Note, we should use a manual reset event for the hEvent argument of the OVERLAPPED. This
102 // is because both WaitForMultipleObjects and GetOverlappedResult (if you set the bWait
103 // argument to TRUE) will wait for the event to be signalled. If we use an auto-reset event,
104 // WaitForMultipleObjects will reset the event, return successfully, and then
105 // GetOverlappedResult will block since the event is no longer signalled.
106 m_event_handles[kBytesAvailableEvent] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
110 ConnectionGenericFile::IsConnected() const
112 return m_file && (m_file != INVALID_HANDLE_VALUE);
115 lldb::ConnectionStatus
116 ConnectionGenericFile::Connect(const char *s, Error *error_ptr)
118 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
120 log->Printf("%p ConnectionGenericFile::Connect (url = '%s')", static_cast<void *>(this), s);
122 if (strstr(s, "file://") != s)
125 error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'", s);
126 return eConnectionStatusError;
131 ConnectionStatus status = Disconnect(error_ptr);
132 if (status != eConnectionStatusSuccess)
137 const char *path = s + strlen("file://");
138 // Open the file for overlapped access. If it does not exist, create it. We open it overlapped
139 // so that we can issue asynchronous reads and then use WaitForMultipleObjects to allow the read
140 // to be interrupted by an event object.
141 m_file = ::CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
142 if (m_file == INVALID_HANDLE_VALUE)
145 error_ptr->SetError(::GetLastError(), eErrorTypeWin32);
146 return eConnectionStatusError;
151 return eConnectionStatusSuccess;
154 lldb::ConnectionStatus
155 ConnectionGenericFile::Disconnect(Error *error_ptr)
157 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
159 log->Printf("%p ConnectionGenericFile::Disconnect ()", static_cast<void *>(this));
162 return eConnectionStatusSuccess;
164 // Reset the handle so that after we unblock any pending reads, subsequent calls to Read() will
165 // see a disconnected state.
166 HANDLE old_file = m_file;
167 m_file = INVALID_HANDLE_VALUE;
169 // Set the disconnect event so that any blocking reads unblock, then cancel any pending IO operations.
170 ::CancelIoEx(old_file, &m_overlapped);
172 // Close the file handle if we owned it, but don't close the event handles. We could always
173 // reconnect with the same Connection instance.
175 ::CloseHandle(old_file);
177 ::ZeroMemory(&m_file_position, sizeof(m_file_position));
180 return eConnectionStatusSuccess;
184 ConnectionGenericFile::Read(void *dst, size_t dst_len, uint32_t timeout_usec, lldb::ConnectionStatus &status, Error *error_ptr)
186 ReturnInfo return_info;
188 DWORD bytes_read = 0;
195 return_info.Set(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
199 m_overlapped.hEvent = m_event_handles[kBytesAvailableEvent];
201 result = ::ReadFile(m_file, dst, dst_len, NULL, &m_overlapped);
202 if (result || ::GetLastError() == ERROR_IO_PENDING)
206 // The expected return path. The operation is pending. Wait for the operation to complete
207 // or be interrupted.
208 TimeValue time_value;
209 time_value.OffsetWithMicroSeconds(timeout_usec);
210 DWORD milliseconds = time_value.milliseconds();
211 DWORD wait_result = ::WaitForMultipleObjects(llvm::array_lengthof(m_event_handles), m_event_handles, FALSE, milliseconds);
212 // All of the events are manual reset events, so make sure we reset them to non-signalled.
215 case WAIT_OBJECT_0 + kBytesAvailableEvent:
217 case WAIT_OBJECT_0 + kInterruptEvent:
218 return_info.Set(0, eConnectionStatusInterrupted, 0);
221 return_info.Set(0, eConnectionStatusTimedOut, 0);
224 return_info.Set(0, eConnectionStatusError, ::GetLastError());
228 // The data is ready. Figure out how much was read and return;
229 if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_read, FALSE))
231 DWORD result_error = ::GetLastError();
232 // ERROR_OPERATION_ABORTED occurs when someone calls Disconnect() during a blocking read.
233 // This triggers a call to CancelIoEx, which causes the operation to complete and the
234 // result to be ERROR_OPERATION_ABORTED.
235 if (result_error == ERROR_HANDLE_EOF || result_error == ERROR_OPERATION_ABORTED || result_error == ERROR_BROKEN_PIPE)
236 return_info.Set(bytes_read, eConnectionStatusEndOfFile, 0);
238 return_info.Set(bytes_read, eConnectionStatusError, result_error);
240 else if (bytes_read == 0)
241 return_info.Set(bytes_read, eConnectionStatusEndOfFile, 0);
243 return_info.Set(bytes_read, eConnectionStatusSuccess, 0);
247 else if (::GetLastError() == ERROR_BROKEN_PIPE)
249 // The write end of a pipe was closed. This is equivalent to EOF.
250 return_info.Set(0, eConnectionStatusEndOfFile, 0);
254 // An unknown error occurred. Fail out.
255 return_info.Set(0, eConnectionStatusError, ::GetLastError());
260 status = return_info.GetStatus();
262 *error_ptr = return_info.GetError();
264 // kBytesAvailableEvent is a manual reset event. Make sure it gets reset here so that any
265 // subsequent operations don't immediately see bytes available.
266 ResetEvent(m_event_handles[kBytesAvailableEvent]);
268 IncrementFilePointer(return_info.GetBytes());
269 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
272 log->Printf("%" PRIxPTR " ConnectionGenericFile::Read() handle = %" PRIxPTR ", dst = %" PRIxPTR ", dst_len = %" PRIu64
273 ") => %" PRIu64 ", error = %s",
274 this, m_file, dst, static_cast<uint64_t>(dst_len), static_cast<uint64_t>(return_info.GetBytes()),
275 return_info.GetError().AsCString());
278 return return_info.GetBytes();
282 ConnectionGenericFile::Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Error *error_ptr)
284 ReturnInfo return_info;
285 DWORD bytes_written = 0;
293 return_info.Set(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
297 m_overlapped.hEvent = NULL;
299 // Writes are not interruptible like reads are, so just block until it's done.
300 result = ::WriteFile(m_file, src, src_len, NULL, &m_overlapped);
301 if (!result && ::GetLastError() != ERROR_IO_PENDING)
303 return_info.Set(0, eConnectionStatusError, ::GetLastError());
307 if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_written, TRUE))
309 return_info.Set(bytes_written, eConnectionStatusError, ::GetLastError());
313 return_info.Set(bytes_written, eConnectionStatusSuccess, 0);
317 status = return_info.GetStatus();
319 *error_ptr = return_info.GetError();
321 IncrementFilePointer(return_info.GetBytes());
322 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
325 log->Printf("%" PRIxPTR " ConnectionGenericFile::Write() handle = %" PRIxPTR ", src = %" PRIxPTR ", src_len = %" PRIu64
326 ") => %" PRIu64 ", error = %s",
327 this, m_file, src, static_cast<uint64_t>(src_len), static_cast<uint64_t>(return_info.GetBytes()),
328 return_info.GetError().AsCString());
330 return return_info.GetBytes();
334 ConnectionGenericFile::GetURI()
340 ConnectionGenericFile::InterruptRead()
342 return ::SetEvent(m_event_handles[kInterruptEvent]);
346 ConnectionGenericFile::IncrementFilePointer(DWORD amount)
348 LARGE_INTEGER old_pos;
349 old_pos.HighPart = m_overlapped.OffsetHigh;
350 old_pos.LowPart = m_overlapped.Offset;
351 old_pos.QuadPart += amount;
352 m_overlapped.Offset = old_pos.LowPart;
353 m_overlapped.OffsetHigh = old_pos.HighPart;