From 8ed5e8f975f2bfd3120a8ea39ea46ea2d452225f Mon Sep 17 00:00:00 2001 From: Andrew Rogers Date: Wed, 2 Oct 2024 13:30:19 -0700 Subject: [PATCH 1/2] implementations for `vFile:mode` and `vFile:size` --- .../GDBRemote/DummySessionDelegateImpl.h | 2 ++ .../GDBRemote/Mixins/FileOperationsMixin.h | 4 ++++ Headers/DebugServer2/GDBRemote/SessionDelegate.h | 2 ++ Headers/DebugServer2/Host/File.h | 1 + Sources/GDBRemote/DummySessionDelegateImpl.cpp | 2 ++ Sources/GDBRemote/Mixins/FileOperationsMixin.hpp | 13 +++++++++++++ Sources/GDBRemote/Session.cpp | 11 +++++++++++ Sources/Host/POSIX/File.cpp | 9 +++++++++ Sources/Host/Windows/File.cpp | 5 +++++ 9 files changed, 49 insertions(+) diff --git a/Headers/DebugServer2/GDBRemote/DummySessionDelegateImpl.h b/Headers/DebugServer2/GDBRemote/DummySessionDelegateImpl.h index eae20161..3c4fa5fd 100644 --- a/Headers/DebugServer2/GDBRemote/DummySessionDelegateImpl.h +++ b/Headers/DebugServer2/GDBRemote/DummySessionDelegateImpl.h @@ -245,6 +245,8 @@ class DummySessionDelegateImpl : public SessionDelegate { uint8_t digest[16]) override; ErrorCode onFileGetSize(Session &session, std::string const &path, uint64_t &size) override; + ErrorCode onFileGetMode(Session &session, std::string const &path, + uint32_t &mode) const override; ErrorCode onQueryProcessList(Session &session, ProcessInfoMatch const &match, bool first, ProcessInfo &info) const override; diff --git a/Headers/DebugServer2/GDBRemote/Mixins/FileOperationsMixin.h b/Headers/DebugServer2/GDBRemote/Mixins/FileOperationsMixin.h index b463c89e..a733bc90 100644 --- a/Headers/DebugServer2/GDBRemote/Mixins/FileOperationsMixin.h +++ b/Headers/DebugServer2/GDBRemote/Mixins/FileOperationsMixin.h @@ -45,6 +45,10 @@ template class FileOperationsMixin : public T { protected: ErrorCode onFileExists(Session &session, std::string const &path) override; + ErrorCode onFileGetSize(Session &session, std::string const &path, + uint64_t &size) override; + ErrorCode onFileGetMode(Session &session, std::string const &path, + uint32_t &mode) const override; ErrorCode onFileRemove(Session &session, std::string const &path) override; protected: diff --git a/Headers/DebugServer2/GDBRemote/SessionDelegate.h b/Headers/DebugServer2/GDBRemote/SessionDelegate.h index 94f9f8d2..13a13987 100644 --- a/Headers/DebugServer2/GDBRemote/SessionDelegate.h +++ b/Headers/DebugServer2/GDBRemote/SessionDelegate.h @@ -272,6 +272,8 @@ class SessionDelegate { uint8_t digest[16]) = 0; virtual ErrorCode onFileGetSize(Session &session, std::string const &path, uint64_t &size) = 0; + virtual ErrorCode onFileGetMode(Session &session, std::string const &path, + uint32_t &mode) const = 0; virtual ErrorCode onQueryProcessList(Session &session, ProcessInfoMatch const &match, diff --git a/Headers/DebugServer2/Host/File.h b/Headers/DebugServer2/Host/File.h index 79518df8..bd7c2524 100644 --- a/Headers/DebugServer2/Host/File.h +++ b/Headers/DebugServer2/Host/File.h @@ -63,6 +63,7 @@ class File { public: static ErrorCode fileSize(std::string const &path, uint64_t &size); + static ErrorCode fileMode(std::string const &path, uint32_t &mode); protected: int _fd; diff --git a/Sources/GDBRemote/DummySessionDelegateImpl.cpp b/Sources/GDBRemote/DummySessionDelegateImpl.cpp index fd71953b..02f83e63 100644 --- a/Sources/GDBRemote/DummySessionDelegateImpl.cpp +++ b/Sources/GDBRemote/DummySessionDelegateImpl.cpp @@ -293,6 +293,8 @@ DUMMY_IMPL_EMPTY(onFileComputeMD5, Session &, std::string const &, uint8_t[16]) DUMMY_IMPL_EMPTY(onFileGetSize, Session &, std::string const &, uint64_t &) +DUMMY_IMPL_EMPTY_CONST(onFileGetMode, Session &, std::string const &, uint32_t&) + DUMMY_IMPL_EMPTY_CONST(onQueryProcessList, Session &, ProcessInfoMatch const &, bool, ProcessInfo &) diff --git a/Sources/GDBRemote/Mixins/FileOperationsMixin.hpp b/Sources/GDBRemote/Mixins/FileOperationsMixin.hpp index fe318e08..28589b8f 100644 --- a/Sources/GDBRemote/Mixins/FileOperationsMixin.hpp +++ b/Sources/GDBRemote/Mixins/FileOperationsMixin.hpp @@ -88,6 +88,19 @@ ErrorCode FileOperationsMixin::onFileExists(Session &, return Host::Platform::IsFilePresent(path) ? kSuccess : kErrorNotFound; } +template +ErrorCode FileOperationsMixin::onFileGetSize(Session &session, std::string const &path, + uint64_t &size){ + return Host::File::fileSize(path, size); +} + +template +ErrorCode FileOperationsMixin::onFileGetMode(Session &session, + std::string const &path, + uint32_t &mode) const { + return Host::File::fileMode(path, mode); +} + template ErrorCode FileOperationsMixin::onFileRemove(Session &session, std::string const &path) { diff --git a/Sources/GDBRemote/Session.cpp b/Sources/GDBRemote/Session.cpp index 611c08b5..f2a9090a 100644 --- a/Sources/GDBRemote/Session.cpp +++ b/Sources/GDBRemote/Session.cpp @@ -3099,6 +3099,17 @@ void Session::Handle_vFile(ProtocolInterpreter::Handler const &, } else { ss << 'F' << std::hex << size; } + } else if (op == "mode") { + uint32_t mode; + ErrorCode error = + _delegate->onFileGetMode(*this, HexToString(&args[op_end]), mode); + // Response is F followed by the mode bits in base 16 or + // F-1,errno with the errno if an error occurs, base 16 + if (error != kSuccess) { + ss << 'F' << -1 << ',' << std::hex << error; + } else { + ss << 'F' << std::hex << mode; + } } else { sendError(kErrorUnsupported); return; diff --git a/Sources/Host/POSIX/File.cpp b/Sources/Host/POSIX/File.cpp index 4c0e6448..85d16352 100644 --- a/Sources/Host/POSIX/File.cpp +++ b/Sources/Host/POSIX/File.cpp @@ -163,5 +163,14 @@ ErrorCode File::fileSize(std::string const &path, uint64_t &size) { size = static_cast(stbuf.st_size); return kSuccess; } + +ErrorCode File::fileMode(std::string const &path, uint32_t &mode) { + struct stat stbuf; + if (stat(path.c_str(), &stbuf) < 0) + return Platform::TranslateError(); + + mode = static_cast(ALLPERMS & stbuf.st_mode); + return kSuccess; +} } // namespace Host } // namespace ds2 diff --git a/Sources/Host/Windows/File.cpp b/Sources/Host/Windows/File.cpp index 7a459a45..df65fad7 100644 --- a/Sources/Host/Windows/File.cpp +++ b/Sources/Host/Windows/File.cpp @@ -42,5 +42,10 @@ ErrorCode File::createDirectory(std::string const &path, uint32_t flags) { ErrorCode File::fileSize(std::string const &path, uint64_t &size) { return kErrorUnsupported; } + +ErrorCode File::fileMode(std::string const &path, uint32_t &mode) { + return kErrorUnsupported; +} + } // namespace Host } // namespace ds2 From 3aaed7894ab837266b4ec99d088f48761eec0b52 Mon Sep 17 00:00:00 2001 From: Andrew Rogers Date: Thu, 3 Oct 2024 16:26:41 -0700 Subject: [PATCH 2/2] simplify vFile:mode reply and fix vFile:size reply to match lldb documentation --- Sources/GDBRemote/Session.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/GDBRemote/Session.cpp b/Sources/GDBRemote/Session.cpp index f2a9090a..fd1faea2 100644 --- a/Sources/GDBRemote/Session.cpp +++ b/Sources/GDBRemote/Session.cpp @@ -2964,6 +2964,7 @@ void Session::Handle_vFile(ProtocolInterpreter::Handler const &, // // LLDB: vFile:exists:path // vFile:size:path + // vFile:mode:path // vFile:MD5:path // bool escaped = false; @@ -3093,11 +3094,13 @@ void Session::Handle_vFile(ProtocolInterpreter::Handler const &, uint64_t size; ErrorCode error = _delegate->onFileGetSize(*this, HexToString(&args[op_end]), size); - // Fsize or Exx if error. + // Response is F followed by the file size in base 16 or + // F-1,errno with the errno if an error occurs, base 16. + ss << 'F'; if (error != kSuccess) { - ss << 'E' << std::hex << error; + ss << -1 << std::hex << error; } else { - ss << 'F' << std::hex << size; + ss << std::hex << size; } } else if (op == "mode") { uint32_t mode; @@ -3105,10 +3108,11 @@ void Session::Handle_vFile(ProtocolInterpreter::Handler const &, _delegate->onFileGetMode(*this, HexToString(&args[op_end]), mode); // Response is F followed by the mode bits in base 16 or // F-1,errno with the errno if an error occurs, base 16 + ss << 'F'; if (error != kSuccess) { - ss << 'F' << -1 << ',' << std::hex << error; + ss << -1 << ',' << std::hex << error; } else { - ss << 'F' << std::hex << mode; + ss << std::hex << mode; } } else { sendError(kErrorUnsupported);