Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Headers/DebugServer2/GDBRemote/DummySessionDelegateImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ class DummySessionDelegateImpl : public SessionDelegate {
uint64_t &size) override;
ErrorCode onFileGetMode(Session &session, std::string const &path,
uint32_t &mode) const override;
ErrorCode onFileFstat(Session &session, int fd,
ByteVector &buffer) const override;

ErrorCode onQueryProcessList(Session &session, ProcessInfoMatch const &match,
bool first, ProcessInfo &info) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ template <typename T> class FileOperationsMixin : public T {
uint64_t &size) override;
ErrorCode onFileGetMode(Session &session, std::string const &path,
uint32_t &mode) const override;
ErrorCode onFileFstat(Session &session, int fd, ByteVector &buffer) const override;
ErrorCode onFileRemove(Session &session, std::string const &path) override;

protected:
Expand Down
2 changes: 2 additions & 0 deletions Headers/DebugServer2/GDBRemote/SessionDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ class SessionDelegate {
uint64_t &size) = 0;
virtual ErrorCode onFileGetMode(Session &session, std::string const &path,
uint32_t &mode) const = 0;
virtual ErrorCode onFileFstat(Session &session, int fd,
ByteVector &buffer) const = 0;

virtual ErrorCode onQueryProcessList(Session &session,
ProcessInfoMatch const &match,
Expand Down
1 change: 1 addition & 0 deletions Headers/DebugServer2/Host/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class File {
public:
ErrorCode pread(ByteVector &buf, uint64_t &count, uint64_t offset);
ErrorCode pwrite(ByteVector const &buf, uint64_t &count, uint64_t offset);
ErrorCode fstat(ByteVector &buffer) const;

public:
bool valid() const { return (_fd >= 0); }
Expand Down
2 changes: 2 additions & 0 deletions Sources/GDBRemote/DummySessionDelegateImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ 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(onFileFstat, Session &, int, ByteVector &)

DUMMY_IMPL_EMPTY_CONST(onQueryProcessList, Session &, ProcessInfoMatch const &,
bool, ProcessInfo &)

Expand Down
10 changes: 10 additions & 0 deletions Sources/GDBRemote/Mixins/FileOperationsMixin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ ErrorCode FileOperationsMixin<T>::onFileGetMode(Session &session,
return Host::File::fileMode(path, mode);
}

template <typename T>
ErrorCode FileOperationsMixin<T>::onFileFstat(Session &session, int fd,
ByteVector &buffer) const {
auto it = _openFiles.find(fd);
if (it == _openFiles.end())
return kErrorInvalidHandle;

return it->second.fstat(buffer);
}

template <typename T>
ErrorCode FileOperationsMixin<T>::onFileRemove(Session &session,
std::string const &path) {
Expand Down
14 changes: 14 additions & 0 deletions Sources/GDBRemote/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,20 @@ void Session::Handle_vFile(ProtocolInterpreter::Handler const &,
} else {
ss << std::hex << mode;
}
} else if (op == "fstat") {
ByteVector buffer;
int fd = std::strtol(&args[op_end], nullptr, 16);
ErrorCode error = _delegate->onFileFstat(*this, fd, buffer);
// Response is F, followed by the the size of the stat data (base 16),
// a semicolon, followed by the stat result in the binary-escaped-data
// encoding or F-1,errno with the errno if an error occurs, base 16
ss << 'F';
if (error != kSuccess) {
ss << -1 << ',' << std::hex << error;
} else {
ss << std::hex << buffer.size() << ';' << Escape(buffer);
escaped = true;
}
} else {
sendError(kErrorUnsupported);
return;
Expand Down
44 changes: 44 additions & 0 deletions Sources/Host/POSIX/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#include <sys/stat.h>
#include <unistd.h>

#if defined(OS_LINUX)
#include <endian.h>
#elif defined(OS_FREEBSD)
#include <sys/_endian.h>
#elif defined(OS_DARWIN)
#include <libkern/OSByteOrder.h>
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#endif

namespace ds2 {
namespace Host {

Expand Down Expand Up @@ -118,6 +128,40 @@ ErrorCode File::pwrite(ByteVector const &buf, uint64_t &count,
return _lastError = kSuccess;
}

// lldb expects stat data is returned as a packed buffer with total size of 64
// bytes. The field order is the same as the POSIX defined stat struct. All
// fields are encoded as 4-byte, big-endian unsigned integers except for
// st_size, st_blksize, and st_blocks which are all 8-byte, big-endian unsigned
// integers.
ErrorCode File::fstat(ByteVector &buffer) const {
struct stat s;
if (::fstat(_fd, &s) < 0)
return Platform::TranslateError();

const auto appendInteger = [&buffer](auto value) -> void {
buffer.insert(buffer.end(),
reinterpret_cast<uint8_t*>(&value),
reinterpret_cast<uint8_t*>(&value) + sizeof(value));
};

appendInteger(htobe32(static_cast<uint32_t>(s.st_dev)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_ino)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_mode)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_nlink)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_uid)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_gid)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_rdev)));
appendInteger(htobe64(static_cast<uint64_t>(s.st_size)));
appendInteger(htobe64(static_cast<uint64_t>(s.st_blksize)));
appendInteger(htobe64(static_cast<uint64_t>(s.st_blocks)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_atime)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_mtime)));
appendInteger(htobe32(static_cast<uint32_t>(s.st_ctime)));
DS2ASSERT(buffer.size() == 64);

return kSuccess;
}

ErrorCode File::chmod(std::string const &path, uint32_t mode) {
if (::chmod(path.c_str(), mode) < 0) {
return Platform::TranslateError();
Expand Down
4 changes: 4 additions & 0 deletions Sources/Host/Windows/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ ErrorCode File::pwrite(ByteVector const &buf, uint64_t &count,
return kErrorUnsupported;
}

ErrorCode File::fstat(ByteVector &buffer) const {
return kErrorUnsupported;
}

ErrorCode File::chmod(std::string const &path, uint32_t mode) {
return kErrorUnsupported;
}
Expand Down
Loading