[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- ConnectionGenericFileWindows.cpp ----------------------------------===//
|
2014-10-07 05:23:09 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-10-07 05:23:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Host/windows/ConnectionGenericFileWindows.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-04-07 05:28:29 +08:00
|
|
|
#include "lldb/Utility/Timeout.h"
|
2014-10-07 05:23:09 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-03-23 01:58:09 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2014-10-07 05:23:09 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// This is a simple helper class to package up the information needed to return
|
2018-05-01 00:49:04 +08:00
|
|
|
// from a Read/Write operation function. Since there is a lot of code to be
|
|
|
|
// run before exit regardless of whether the operation succeeded or failed,
|
|
|
|
// combined with many possible return paths, this is the cleanest way to
|
|
|
|
// represent it.
|
2014-10-07 05:23:09 +08:00
|
|
|
class ReturnInfo {
|
|
|
|
public:
|
|
|
|
void Set(size_t bytes, ConnectionStatus status, DWORD error_code) {
|
|
|
|
m_error.SetError(error_code, eErrorTypeWin32);
|
|
|
|
m_bytes = bytes;
|
|
|
|
m_status = status;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
void Set(size_t bytes, ConnectionStatus status, llvm::StringRef error_msg) {
|
|
|
|
m_error.SetErrorString(error_msg.data());
|
|
|
|
m_bytes = bytes;
|
|
|
|
m_status = status;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
size_t GetBytes() const { return m_bytes; }
|
|
|
|
ConnectionStatus GetStatus() const { return m_status; }
|
2017-05-12 12:51:55 +08:00
|
|
|
const Status &GetError() const { return m_error; }
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
private:
|
2017-05-12 12:51:55 +08:00
|
|
|
Status m_error;
|
2014-10-07 05:23:09 +08:00
|
|
|
size_t m_bytes;
|
|
|
|
ConnectionStatus m_status;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionGenericFile::ConnectionGenericFile()
|
|
|
|
: m_file(INVALID_HANDLE_VALUE), m_owns_file(false) {
|
|
|
|
::ZeroMemory(&m_overlapped, sizeof(m_overlapped));
|
|
|
|
::ZeroMemory(&m_file_position, sizeof(m_file_position));
|
|
|
|
InitializeEventHandles();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionGenericFile::ConnectionGenericFile(lldb::file_t file, bool owns_file)
|
|
|
|
: m_file(file), m_owns_file(owns_file) {
|
|
|
|
::ZeroMemory(&m_overlapped, sizeof(m_overlapped));
|
|
|
|
::ZeroMemory(&m_file_position, sizeof(m_file_position));
|
|
|
|
InitializeEventHandles();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionGenericFile::~ConnectionGenericFile() {
|
|
|
|
if (m_owns_file && IsConnected())
|
|
|
|
::CloseHandle(m_file);
|
|
|
|
|
|
|
|
::CloseHandle(m_event_handles[kBytesAvailableEvent]);
|
|
|
|
::CloseHandle(m_event_handles[kInterruptEvent]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionGenericFile::InitializeEventHandles() {
|
|
|
|
m_event_handles[kInterruptEvent] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
// Note, we should use a manual reset event for the hEvent argument of the
|
2018-05-01 00:49:04 +08:00
|
|
|
// OVERLAPPED. This is because both WaitForMultipleObjects and
|
|
|
|
// GetOverlappedResult (if you set the bWait argument to TRUE) will wait for
|
|
|
|
// the event to be signalled. If we use an auto-reset event,
|
2014-10-07 05:23:09 +08:00
|
|
|
// WaitForMultipleObjects will reset the event, return successfully, and then
|
|
|
|
// GetOverlappedResult will block since the event is no longer signalled.
|
|
|
|
m_event_handles[kBytesAvailableEvent] =
|
|
|
|
::CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConnectionGenericFile::IsConnected() const {
|
|
|
|
return m_file && (m_file != INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:15:14 +08:00
|
|
|
lldb::ConnectionStatus ConnectionGenericFile::Connect(llvm::StringRef path,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2014-10-07 05:23:09 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionGenericFile::Connect (url = '%s')",
|
|
|
|
static_cast<void *>(this), path.str().c_str());
|
2014-10-07 05:23:09 +08:00
|
|
|
|
2016-11-18 05:15:14 +08:00
|
|
|
if (!path.consume_front("file://")) {
|
2014-10-07 05:23:09 +08:00
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
|
2016-11-18 05:15:14 +08:00
|
|
|
path.str().c_str());
|
2014-10-07 05:23:09 +08:00
|
|
|
return eConnectionStatusError;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
if (IsConnected()) {
|
|
|
|
ConnectionStatus status = Disconnect(error_ptr);
|
|
|
|
if (status != eConnectionStatusSuccess)
|
|
|
|
return status;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
// Open the file for overlapped access. If it does not exist, create it. We
|
2016-11-18 05:15:14 +08:00
|
|
|
// open it overlapped so that we can issue asynchronous reads and then use
|
|
|
|
// WaitForMultipleObjects to allow the read to be interrupted by an event
|
|
|
|
// object.
|
2016-03-23 01:58:09 +08:00
|
|
|
std::wstring wpath;
|
|
|
|
if (!llvm::ConvertUTF8toWide(path, wpath)) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetError(1, eErrorTypeGeneric);
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
m_file = ::CreateFileW(wpath.c_str(), GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ, NULL, OPEN_ALWAYS,
|
|
|
|
FILE_FLAG_OVERLAPPED, NULL);
|
2014-10-07 05:23:09 +08:00
|
|
|
if (m_file == INVALID_HANDLE_VALUE) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetError(::GetLastError(), eErrorTypeWin32);
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_owns_file = true;
|
2020-01-29 18:15:20 +08:00
|
|
|
m_uri = path.str();
|
2014-10-07 05:23:09 +08:00
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
lldb::ConnectionStatus ConnectionGenericFile::Disconnect(Status *error_ptr) {
|
2014-10-07 05:23:09 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionGenericFile::Disconnect ()",
|
|
|
|
static_cast<void *>(this));
|
2014-10-07 05:23:09 +08:00
|
|
|
|
|
|
|
if (!IsConnected())
|
|
|
|
return eConnectionStatusSuccess;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
// Reset the handle so that after we unblock any pending reads, subsequent
|
2018-05-01 00:49:04 +08:00
|
|
|
// calls to Read() will see a disconnected state.
|
2014-10-07 05:23:09 +08:00
|
|
|
HANDLE old_file = m_file;
|
|
|
|
m_file = INVALID_HANDLE_VALUE;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
// Set the disconnect event so that any blocking reads unblock, then cancel
|
|
|
|
// any pending IO operations.
|
|
|
|
::CancelIoEx(old_file, &m_overlapped);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
// Close the file handle if we owned it, but don't close the event handles.
|
2018-05-01 00:49:04 +08:00
|
|
|
// We could always reconnect with the same Connection instance.
|
2014-10-07 05:23:09 +08:00
|
|
|
if (m_owns_file)
|
|
|
|
::CloseHandle(old_file);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
::ZeroMemory(&m_file_position, sizeof(m_file_position));
|
|
|
|
m_owns_file = false;
|
2015-01-17 10:20:29 +08:00
|
|
|
m_uri.clear();
|
2014-10-07 05:23:09 +08:00
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ConnectionGenericFile::Read(void *dst, size_t dst_len,
|
2016-11-25 22:43:37 +08:00
|
|
|
const Timeout<std::micro> &timeout,
|
2014-10-07 05:23:09 +08:00
|
|
|
lldb::ConnectionStatus &status,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2014-10-07 05:23:09 +08:00
|
|
|
ReturnInfo return_info;
|
2014-10-23 18:36:53 +08:00
|
|
|
BOOL result = 0;
|
|
|
|
DWORD bytes_read = 0;
|
2014-10-07 05:23:09 +08:00
|
|
|
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->Clear();
|
|
|
|
|
|
|
|
if (!IsConnected()) {
|
|
|
|
return_info.Set(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
|
|
|
|
goto finish;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
m_overlapped.hEvent = m_event_handles[kBytesAvailableEvent];
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-23 18:36:53 +08:00
|
|
|
result = ::ReadFile(m_file, dst, dst_len, NULL, &m_overlapped);
|
2014-10-07 05:23:09 +08:00
|
|
|
if (result || ::GetLastError() == ERROR_IO_PENDING) {
|
|
|
|
if (!result) {
|
|
|
|
// The expected return path. The operation is pending. Wait for the
|
2018-05-01 00:49:04 +08:00
|
|
|
// operation to complete or be interrupted.
|
2016-11-25 22:43:37 +08:00
|
|
|
DWORD milliseconds =
|
|
|
|
timeout
|
|
|
|
? std::chrono::duration_cast<std::chrono::milliseconds>(*timeout)
|
|
|
|
.count()
|
|
|
|
: INFINITE;
|
2015-04-03 04:57:38 +08:00
|
|
|
DWORD wait_result =
|
|
|
|
::WaitForMultipleObjects(llvm::array_lengthof(m_event_handles),
|
|
|
|
m_event_handles, FALSE, milliseconds);
|
2014-10-07 05:23:09 +08:00
|
|
|
// All of the events are manual reset events, so make sure we reset them
|
|
|
|
// to non-signalled.
|
2015-04-03 04:57:38 +08:00
|
|
|
switch (wait_result) {
|
2014-10-07 05:23:09 +08:00
|
|
|
case WAIT_OBJECT_0 + kBytesAvailableEvent:
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2014-10-07 05:23:09 +08:00
|
|
|
case WAIT_OBJECT_0 + kInterruptEvent:
|
|
|
|
return_info.Set(0, eConnectionStatusInterrupted, 0);
|
|
|
|
goto finish;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
return_info.Set(0, eConnectionStatusTimedOut, 0);
|
|
|
|
goto finish;
|
2014-10-09 04:38:41 +08:00
|
|
|
case WAIT_FAILED:
|
|
|
|
return_info.Set(0, eConnectionStatusError, ::GetLastError());
|
2014-10-07 05:23:09 +08:00
|
|
|
goto finish;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-10-09 04:38:41 +08:00
|
|
|
}
|
2014-10-07 05:23:09 +08:00
|
|
|
// The data is ready. Figure out how much was read and return;
|
|
|
|
if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_read, FALSE)) {
|
|
|
|
DWORD result_error = ::GetLastError();
|
2018-05-01 00:49:04 +08:00
|
|
|
// ERROR_OPERATION_ABORTED occurs when someone calls Disconnect() during
|
|
|
|
// a blocking read. This triggers a call to CancelIoEx, which causes the
|
|
|
|
// operation to complete and the result to be ERROR_OPERATION_ABORTED.
|
2014-10-10 04:17:53 +08:00
|
|
|
if (result_error == ERROR_HANDLE_EOF ||
|
|
|
|
result_error == ERROR_OPERATION_ABORTED ||
|
|
|
|
result_error == ERROR_BROKEN_PIPE)
|
2014-10-07 05:23:09 +08:00
|
|
|
return_info.Set(bytes_read, eConnectionStatusEndOfFile, 0);
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
2014-10-07 05:23:09 +08:00
|
|
|
return_info.Set(bytes_read, eConnectionStatusError, result_error);
|
|
|
|
} else if (bytes_read == 0)
|
|
|
|
return_info.Set(bytes_read, eConnectionStatusEndOfFile, 0);
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
2014-10-07 05:23:09 +08:00
|
|
|
return_info.Set(bytes_read, eConnectionStatusSuccess, 0);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
goto finish;
|
2014-10-09 04:38:41 +08:00
|
|
|
} else if (::GetLastError() == ERROR_BROKEN_PIPE) {
|
|
|
|
// The write end of a pipe was closed. This is equivalent to EOF.
|
|
|
|
return_info.Set(0, eConnectionStatusEndOfFile, 0);
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2015-07-22 08:16:02 +08:00
|
|
|
// An unknown error occurred. Fail out.
|
2014-10-07 05:23:09 +08:00
|
|
|
return_info.Set(0, eConnectionStatusError, ::GetLastError());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-10-07 05:23:09 +08:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
status = return_info.GetStatus();
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = return_info.GetError();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// kBytesAvailableEvent is a manual reset event. Make sure it gets reset
|
|
|
|
// here so that any subsequent operations don't immediately see bytes
|
|
|
|
// available.
|
2014-10-07 05:23:09 +08:00
|
|
|
ResetEvent(m_event_handles[kBytesAvailableEvent]);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
IncrementFilePointer(return_info.GetBytes());
|
|
|
|
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
|
2019-09-23 20:03:28 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionGenericFile::Read() handle = %p, dst = %p, "
|
|
|
|
"dst_len = %zu) => %zu, error = %s",
|
|
|
|
static_cast<void *>(this), m_file, dst, dst_len,
|
|
|
|
return_info.GetBytes(), return_info.GetError().AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
return return_info.GetBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ConnectionGenericFile::Write(const void *src, size_t src_len,
|
|
|
|
lldb::ConnectionStatus &status,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2014-10-07 05:23:09 +08:00
|
|
|
ReturnInfo return_info;
|
2014-10-23 18:36:53 +08:00
|
|
|
DWORD bytes_written = 0;
|
|
|
|
BOOL result = 0;
|
2014-10-07 05:23:09 +08:00
|
|
|
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->Clear();
|
|
|
|
|
|
|
|
if (!IsConnected()) {
|
|
|
|
return_info.Set(0, eConnectionStatusNoConnection, ERROR_INVALID_HANDLE);
|
|
|
|
goto finish;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-10-07 05:23:09 +08:00
|
|
|
|
2014-10-23 18:36:53 +08:00
|
|
|
m_overlapped.hEvent = NULL;
|
2014-10-07 05:23:09 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Writes are not interruptible like reads are, so just block until it's
|
|
|
|
// done.
|
2014-10-07 05:23:09 +08:00
|
|
|
result = ::WriteFile(m_file, src, src_len, NULL, &m_overlapped);
|
|
|
|
if (!result && ::GetLastError() != ERROR_IO_PENDING) {
|
|
|
|
return_info.Set(0, eConnectionStatusError, ::GetLastError());
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!::GetOverlappedResult(m_file, &m_overlapped, &bytes_written, TRUE)) {
|
|
|
|
return_info.Set(bytes_written, eConnectionStatusError, ::GetLastError());
|
|
|
|
goto finish;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
return_info.Set(bytes_written, eConnectionStatusSuccess, 0);
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
status = return_info.GetStatus();
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = return_info.GetError();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
IncrementFilePointer(return_info.GetBytes());
|
|
|
|
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
|
2019-09-23 20:03:28 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionGenericFile::Write() handle = %p, src = %p, "
|
|
|
|
"src_len = %zu) => %zu, error = %s",
|
|
|
|
static_cast<void *>(this), m_file, src, src_len,
|
|
|
|
return_info.GetBytes(), return_info.GetError().AsCString());
|
2014-10-07 05:23:09 +08:00
|
|
|
return return_info.GetBytes();
|
|
|
|
}
|
|
|
|
|
2015-01-17 10:20:29 +08:00
|
|
|
std::string ConnectionGenericFile::GetURI() { return m_uri; }
|
|
|
|
|
2014-10-07 05:23:09 +08:00
|
|
|
bool ConnectionGenericFile::InterruptRead() {
|
|
|
|
return ::SetEvent(m_event_handles[kInterruptEvent]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionGenericFile::IncrementFilePointer(DWORD amount) {
|
|
|
|
LARGE_INTEGER old_pos;
|
|
|
|
old_pos.HighPart = m_overlapped.OffsetHigh;
|
|
|
|
old_pos.LowPart = m_overlapped.Offset;
|
|
|
|
old_pos.QuadPart += amount;
|
|
|
|
m_overlapped.Offset = old_pos.LowPart;
|
|
|
|
m_overlapped.OffsetHigh = old_pos.HighPart;
|
2014-10-23 18:36:53 +08:00
|
|
|
}
|