GdbServer: Convert fstream to fextl

This commit is contained in:
Ryan Houdek 2023-03-25 05:07:02 -07:00
parent 001a086d85
commit 7180bb1496
7 changed files with 46 additions and 31 deletions

View File

@ -1,11 +1,11 @@
#include "Common/StringConv.h"
#include "Common/StringUtils.h"
#include "Common/Paths.h"
#include "Utils/FileLoading.h"
#include <FEXCore/Config/Config.h>
#include <FEXCore/Utils/Allocator.h>
#include <FEXCore/Utils/CPUInfo.h>
#include <FEXCore/Utils/FileLoading.h>
#include <FEXCore/Utils/LogManager.h>
#include <FEXCore/fextl/list.h>
#include <FEXCore/fextl/map.h>

View File

@ -8,12 +8,12 @@ $end_info$
#include "Common/StringConv.h"
#include "Interface/Context/Context.h"
#include "Interface/Core/CPUID.h"
#include "Utils/FileLoading.h"
#include <FEXCore/Config/Config.h>
#include <FEXCore/Core/CPUID.h>
#include <FEXCore/Core/HostFeatures.h>
#include <FEXCore/Utils/CPUInfo.h>
#include <FEXCore/Utils/FileLoading.h>
#include <FEXCore/fextl/string.h>
#include <FEXHeaderUtils/Syscalls.h>

View File

@ -24,6 +24,7 @@ $end_info$
#include <FEXCore/HLE/Linux/ThreadManagement.h>
#include <FEXCore/HLE/SyscallHandler.h>
#include <FEXCore/Utils/CompilerDefs.h>
#include <FEXCore/Utils/FileLoading.h>
#include <FEXCore/Utils/NetStream.h>
#include <FEXCore/Utils/LogManager.h>
#include <FEXCore/Utils/Threads.h>
@ -137,16 +138,9 @@ static fextl::string encodeHex(const unsigned char *data, size_t length) {
static fextl::string getThreadName(uint32_t ThreadID) {
const auto ThreadFile = fextl::fmt::format("/proc/{}/task/{}/comm", getpid(), ThreadID);
std::fstream fs(ThreadFile.c_str(), std::fstream::in | std::fstream::binary);
if (fs.is_open()) {
fextl::string ThreadName;
fs >> ThreadName;
fs.close();
return ThreadName;
}
return "<No Name>";
fextl::string ThreadName {"<No Name>"};
FEXCore::FileLoading::LoadFile(ThreadName, ThreadFile);
return ThreadName;
}
// Packet parser
@ -544,7 +538,10 @@ void GdbServer::buildLibraryMap() {
fextl::ostringstream xml;
std::fstream fs("/proc/self/maps", std::fstream::in | std::fstream::binary);
fextl::string MapsFile;
FEXCore::FileLoading::LoadFile(MapsFile, "/proc/self/maps");
fextl::istringstream MapsStream(MapsFile);
fextl::string Line;
struct FileData {
@ -555,7 +552,7 @@ void GdbServer::buildLibraryMap() {
// 7ff5dd6d2000-7ff5dd6d3000 rw-p 0000a000 103:0b 1881447 /usr/lib/x86_64-linux-gnu/libnss_compat.so.2
fextl::string const &RuntimeExecutable = Filename();
while (std::getline(fs, Line)) {
while (std::getline(MapsStream, Line)) {
auto ss = fextl::istringstream(Line);
fextl::string Tmp;
fextl::string Begin;
@ -737,11 +734,14 @@ GdbServer::HandledPacketType GdbServer::handleXfer(const fextl::string &packet)
static size_t CheckMemMapping(uint64_t Address, size_t Size) {
uint64_t AddressEnd = Address + Size;
std::fstream fs("/proc/self/maps", std::fstream::in | std::fstream::binary);
fextl::string MapsFile;
FEXCore::FileLoading::LoadFile(MapsFile, "/proc/self/maps");
fextl::istringstream MapsStream(MapsFile);
fextl::string Line;
while (std::getline(fs, Line)) {
if (fs.eof()) break;
while (std::getline(MapsStream, Line)) {
if (MapsStream.eof()) break;
uint64_t Begin, End;
char r,w,x,p;
if (sscanf(Line.c_str(), "%lx-%lx %c%c%c%c", &Begin, &End, &r, &w, &x, &p) == 6) {
@ -1280,7 +1280,7 @@ void GdbServer::StartThread() {
}
void GdbServer::OpenListenSocket() {
// open socket
FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator glibc;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
@ -1309,9 +1309,11 @@ void GdbServer::OpenListenSocket() {
}
listen(ListenSocket, 1);
freeaddrinfo(res);
}
std::unique_ptr<std::iostream> GdbServer::OpenSocket() {
fextl::unique_ptr<std::iostream> GdbServer::OpenSocket() {
// Block until a connection arrives
struct sockaddr_storage their_addr{};
socklen_t addr_size{};
@ -1319,7 +1321,7 @@ std::unique_ptr<std::iostream> GdbServer::OpenSocket() {
LogMan::Msg::IFmt("GdbServer, waiting for connection on localhost:8086");
int new_fd = accept(ListenSocket, (struct sockaddr *)&their_addr, &addr_size);
return std::make_unique<FEXCore::Utils::NetStream>(new_fd);
return fextl::make_unique<FEXCore::Utils::NetStream>(new_fd);
}
} // namespace FEXCore

View File

@ -8,6 +8,7 @@ $end_info$
#include <FEXCore/Config/Config.h>
#include <FEXCore/Utils/Event.h>
#include <FEXCore/Utils/Threads.h>
#include <FEXCore/fextl/memory.h>
#include <FEXCore/fextl/string.h>
#include <atomic>
@ -37,7 +38,7 @@ private:
void Break(int signal);
void OpenListenSocket();
std::unique_ptr<std::iostream> OpenSocket();
fextl::unique_ptr<std::iostream> OpenSocket();
void StartThread();
fextl::string ReadPacket(std::iostream &stream);
void SendPacket(std::ostream &stream, const fextl::string& packet);
@ -76,8 +77,8 @@ private:
HandledPacketType readReg(const fextl::string& packet);
FEXCore::Context::ContextImpl *CTX;
std::unique_ptr<FEXCore::Threads::Thread> gdbServerThread;
std::unique_ptr<std::iostream> CommsStream;
fextl::unique_ptr<FEXCore::Threads::Thread> gdbServerThread;
fextl::unique_ptr<std::iostream> CommsStream;
std::mutex sendMutex;
bool SettingNoAckMode{false};
bool NoAckMode{false};

View File

@ -9,7 +9,8 @@
namespace FEXCore::FileLoading {
bool LoadFile(fextl::vector<char> &Data, const fextl::string &Filepath, size_t FixedSize) {
template<typename T>
bool LoadFileImpl(T &Data, const fextl::string &Filepath, size_t FixedSize) {
int FD = open(Filepath.c_str(), O_RDONLY);
if (FD == -1) {
@ -39,6 +40,14 @@ bool LoadFile(fextl::vector<char> &Data, const fextl::string &Filepath, size_t F
return Read == FileSize;
}
bool LoadFile(fextl::vector<char> &Data, const fextl::string &Filepath, size_t FixedSize) {
return LoadFileImpl(Data, Filepath, FixedSize);
}
bool LoadFile(fextl::string &Data, const fextl::string &Filepath, size_t FixedSize) {
return LoadFileImpl(Data, Filepath, FixedSize);
}
ssize_t LoadFileToBuffer(const fextl::string &Filepath, std::span<char> Buffer) {
int FD = open(Filepath.c_str(), O_RDONLY);

View File

@ -1,6 +1,7 @@
#pragma once
#include <FEXCore/fextl/string.h>
#include <FEXCore/fextl/vector.h>
#include <FEXCore/Utils/CompilerDefs.h>
#include <filesystem>
#include <fstream>
@ -15,7 +16,8 @@ namespace FEXCore::FileLoading {
*
* @return true on file loaded, false on failure
*/
bool LoadFile(fextl::vector<char> &Data, const fextl::string &Filepath, size_t FixedSize = 0);
FEX_DEFAULT_VISIBILITY bool LoadFile(fextl::vector<char> &Data, const fextl::string &Filepath, size_t FixedSize = 0);
FEX_DEFAULT_VISIBILITY bool LoadFile(fextl::string &Data, const fextl::string &Filepath, size_t FixedSize = 0);
/**
* @brief Loads a filepath in to a buffer of data with a fixed size
@ -25,6 +27,6 @@ namespace FEXCore::FileLoading {
*
* @return The amount of data read or -1 on error.
*/
ssize_t LoadFileToBuffer(const fextl::string &Filepath, std::span<char> Buffer);
FEX_DEFAULT_VISIBILITY ssize_t LoadFileToBuffer(const fextl::string &Filepath, std::span<char> Buffer);
}

View File

@ -10,7 +10,6 @@
#include <cstring>
#include <filesystem>
#include <fstream>
#include <linux/limits.h>
#include <list>
#include <utility>
@ -37,10 +36,12 @@ namespace FEX::Config {
Dest = json_objClose(Dest);
json_end(Dest);
std::ofstream Output (fextl::string_from_string(Filename), std::ios::out | std::ios::binary);
if (Output.is_open()) {
Output.write(Buffer, strlen(Buffer));
Output.close();
constexpr int USER_PERMS = S_IRWXU | S_IRWXG | S_IRWXO;
int FD = open(Filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, USER_PERMS);
if (FD != -1) {
write(FD, Buffer, strlen(Buffer));
close(FD);
}
}