2021-12-07 22:47:57 +01:00
|
|
|
#include "content/providers/gdb_provider.hpp"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
|
|
|
|
2021-12-07 22:47:57 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
#include <hex/helpers/crypto.hpp>
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.hpp>
|
2021-12-07 22:47:57 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2022-11-07 00:04:47 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2021-12-07 22:47:57 +01:00
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
namespace gdb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
u8 calculateChecksum(const std::string &data) {
|
|
|
|
u64 checksum = 0;
|
|
|
|
|
|
|
|
for (const auto &c : data)
|
|
|
|
checksum += c;
|
|
|
|
|
|
|
|
return checksum & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string createPacket(const std::string &data) {
|
|
|
|
return hex::format("${}#{:02x}", data, calculateChecksum(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> parsePacket(const std::string &packet) {
|
|
|
|
if (packet.length() < 4)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
if (!packet.starts_with('$'))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
if (packet[packet.length() - 3] != '#')
|
|
|
|
return std::nullopt;
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
std::string data = packet.substr(1, packet.length() - 4);
|
2021-12-07 22:47:57 +01:00
|
|
|
std::string checksum = packet.substr(packet.length() - 2, 2);
|
|
|
|
|
2023-03-07 16:03:34 +01:00
|
|
|
auto decodedChecksum = crypt::decode16(checksum);
|
|
|
|
if (checksum.length() != 2 || decodedChecksum.empty() || decodedChecksum[0] != calculateChecksum(data))
|
2021-12-07 22:47:57 +01:00
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
void sendAck(const wolv::net::SocketClient &socket) {
|
2021-12-07 22:47:57 +01:00
|
|
|
socket.writeString("+");
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
void continueExecution(const wolv::net::SocketClient &socket) {
|
2023-03-12 18:27:29 +01:00
|
|
|
socket.writeString(createPacket("vCont;c"));
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::vector<u8> readMemory(const wolv::net::SocketClient &socket, u64 address, size_t size) {
|
2021-12-07 22:47:57 +01:00
|
|
|
std::string packet = createPacket(hex::format("m{:X},{:X}", address, size));
|
|
|
|
|
|
|
|
socket.writeString(packet);
|
|
|
|
|
|
|
|
auto receivedPacket = socket.readString(size * 2 + 4);
|
|
|
|
|
|
|
|
if (receivedPacket.empty())
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-12-07 22:47:57 +01:00
|
|
|
|
|
|
|
auto receivedData = parsePacket(receivedPacket);
|
|
|
|
if (!receivedData.has_value())
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-12-07 22:47:57 +01:00
|
|
|
|
|
|
|
if (receivedData->size() == 3 && receivedData->starts_with("E"))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-12-07 22:47:57 +01:00
|
|
|
|
|
|
|
auto data = crypt::decode16(receivedData.value());
|
|
|
|
|
|
|
|
data.resize(size);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
void writeMemory(const wolv::net::SocketClient &socket, u64 address, const void *buffer, size_t size) {
|
2021-12-12 00:41:44 +01:00
|
|
|
std::vector<u8> bytes(size);
|
|
|
|
std::memcpy(bytes.data(), buffer, size);
|
|
|
|
|
|
|
|
std::string byteString = crypt::encode16(bytes);
|
|
|
|
|
|
|
|
std::string packet = createPacket(hex::format("M{:X},{:X}:{}", address, size, byteString));
|
|
|
|
|
|
|
|
socket.writeString(packet);
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
auto receivedPacket = socket.readString(6);
|
2021-12-12 00:41:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
bool enableNoAckMode(const wolv::net::SocketClient &socket) {
|
2021-12-07 22:47:57 +01:00
|
|
|
socket.writeString(createPacket("QStartNoAckMode"));
|
|
|
|
|
|
|
|
auto ack = socket.readString(1);
|
|
|
|
|
|
|
|
if (ack.empty() || ack[0] != '+')
|
2022-01-30 12:43:43 +01:00
|
|
|
return false;
|
2021-12-07 22:47:57 +01:00
|
|
|
|
|
|
|
auto receivedPacket = socket.readString(6);
|
|
|
|
|
|
|
|
auto receivedData = parsePacket(receivedPacket);
|
|
|
|
|
|
|
|
if (receivedData && *receivedData == "OK") {
|
|
|
|
sendAck(socket);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
GDBProvider::GDBProvider() : m_size(0xFFFF'FFFF) {
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GDBProvider::isAvailable() const {
|
2021-12-12 11:55:38 +01:00
|
|
|
return this->m_socket.isConnected();
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GDBProvider::isReadable() const {
|
|
|
|
return this->m_socket.isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDBProvider::isWritable() const {
|
2023-03-12 18:27:29 +01:00
|
|
|
return true;
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GDBProvider::isResizable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDBProvider::isSavable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-02 18:42:57 +01:00
|
|
|
void GDBProvider::readRaw(u64 offset, void *buffer, size_t size) {
|
2021-12-09 21:10:24 +01:00
|
|
|
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
|
2021-12-07 22:47:57 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-09 21:10:24 +01:00
|
|
|
offset -= this->getBaseAddress();
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
u64 alignedOffset = offset - (offset % CacheLineSize);
|
2021-12-07 22:47:57 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
if (size <= CacheLineSize) {
|
2021-12-07 22:47:57 +01:00
|
|
|
std::scoped_lock lock(this->m_cacheLock);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const auto &cacheLine = std::find_if(this->m_cache.begin(), this->m_cache.end(), [&](auto &line) {
|
2021-12-07 22:47:57 +01:00
|
|
|
return line.address == alignedOffset;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cacheLine != this->m_cache.end()) {
|
|
|
|
// Cache hit
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Cache miss
|
|
|
|
|
|
|
|
this->m_cache.push_back({ alignedOffset, { 0 } });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheLine != this->m_cache.end())
|
2023-03-12 18:27:29 +01:00
|
|
|
std::memcpy(buffer, &cacheLine->data[0] + (offset % CacheLineSize), std::min(size, cacheLine->data.size()));
|
2021-12-12 00:41:44 +01:00
|
|
|
} else {
|
|
|
|
while (size > 0) {
|
|
|
|
size_t readSize = std::min(size, CacheLineSize);
|
2023-12-02 18:42:57 +01:00
|
|
|
|
|
|
|
auto data = gdb::readMemory(this->m_socket, offset, size);
|
|
|
|
if (!data.empty())
|
|
|
|
std::memcpy(buffer, &data[0], data.size());
|
2021-12-12 00:41:44 +01:00
|
|
|
|
|
|
|
size -= readSize;
|
|
|
|
offset += readSize;
|
|
|
|
}
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 18:42:57 +01:00
|
|
|
void GDBProvider::writeRaw(u64 offset, const void *buffer, size_t size) {
|
2021-12-12 00:41:44 +01:00
|
|
|
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
|
|
|
|
return;
|
2021-12-07 22:47:57 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
offset -= this->getBaseAddress();
|
|
|
|
|
|
|
|
gdb::writeMemory(this->m_socket, offset, buffer, size);
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDBProvider::save() {
|
2023-04-06 17:36:28 +02:00
|
|
|
Provider::save();
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GDBProvider::getActualSize() const {
|
2022-01-20 23:24:26 +01:00
|
|
|
return this->m_size;
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GDBProvider::getName() const {
|
2022-05-17 20:46:42 +02:00
|
|
|
std::string address = "-";
|
|
|
|
std::string port = "-";
|
2021-12-08 22:18:59 +01:00
|
|
|
|
2022-05-17 20:46:42 +02:00
|
|
|
if (this->isConnected()) {
|
2021-12-08 22:18:59 +01:00
|
|
|
address = this->m_ipAddress;
|
2022-02-01 22:09:44 +01:00
|
|
|
port = std::to_string(this->m_port);
|
2021-12-08 22:18:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return hex::format("hex.builtin.provider.gdb.name"_lang, address, port);
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 10:42:11 +02:00
|
|
|
std::vector<GDBProvider::Description> GDBProvider::getDataDescription() const {
|
2021-12-12 00:41:44 +01:00
|
|
|
return {
|
2022-01-24 20:53:17 +01:00
|
|
|
{"hex.builtin.provider.gdb.server"_lang, hex::format("{}:{}", this->m_ipAddress, this->m_port)},
|
2021-12-12 00:41:44 +01:00
|
|
|
};
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
bool GDBProvider::open() {
|
2023-04-19 21:56:34 +02:00
|
|
|
this->m_socket = wolv::net::SocketClient(wolv::net::SocketClient::Type::TCP);
|
2021-12-12 00:41:44 +01:00
|
|
|
this->m_socket.connect(this->m_ipAddress, this->m_port);
|
2021-12-07 22:47:57 +01:00
|
|
|
if (!gdb::enableNoAckMode(this->m_socket)) {
|
|
|
|
this->m_socket.disconnect();
|
2021-12-12 00:41:44 +01:00
|
|
|
return false;
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->m_socket.isConnected()) {
|
2023-03-12 18:27:29 +01:00
|
|
|
gdb::continueExecution(this->m_socket);
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
this->m_cacheUpdateThread = std::thread([this] {
|
2021-12-07 22:47:57 +01:00
|
|
|
auto cacheLine = this->m_cache.begin();
|
2021-12-07 23:36:28 +01:00
|
|
|
while (this->isConnected()) {
|
2021-12-07 22:47:57 +01:00
|
|
|
{
|
|
|
|
std::scoped_lock lock(this->m_cacheLock);
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
if (this->m_resetCache) {
|
|
|
|
this->m_cache.clear();
|
|
|
|
this->m_resetCache = false;
|
|
|
|
cacheLine = this->m_cache.begin();
|
|
|
|
}
|
2021-12-07 22:47:57 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
if (cacheLine != this->m_cache.end()) {
|
|
|
|
std::vector<u8> data = gdb::readMemory(this->m_socket, cacheLine->address, CacheLineSize);
|
2023-03-07 16:03:34 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
while (std::count_if(this->m_cache.begin(), this->m_cache.end(), [&](auto &line) { return !line.data.empty(); }) > 100) {
|
2021-12-07 22:47:57 +01:00
|
|
|
this->m_cache.pop_front();
|
|
|
|
cacheLine = this->m_cache.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(cacheLine->data.data(), data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheLine == this->m_cache.end())
|
|
|
|
cacheLine = this->m_cache.begin();
|
2023-03-07 16:03:34 +01:00
|
|
|
else
|
2023-11-10 20:47:08 +01:00
|
|
|
++cacheLine;
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
2023-03-12 18:27:29 +01:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
});
|
2021-12-12 00:41:44 +01:00
|
|
|
|
2022-09-04 11:16:20 +02:00
|
|
|
return true;
|
2021-12-12 00:41:44 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
void GDBProvider::close() {
|
2021-12-07 22:47:57 +01:00
|
|
|
this->m_socket.disconnect();
|
|
|
|
|
|
|
|
if (this->m_cacheUpdateThread.joinable()) {
|
|
|
|
this->m_cacheUpdateThread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
bool GDBProvider::isConnected() const {
|
2021-12-07 22:47:57 +01:00
|
|
|
return this->m_socket.isConnected();
|
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
|
2023-03-16 16:48:15 +01:00
|
|
|
bool GDBProvider::drawLoadInterface() {
|
2022-03-03 09:24:09 +01:00
|
|
|
ImGui::InputText("hex.builtin.provider.gdb.ip"_lang, this->m_ipAddress);
|
2021-12-12 11:55:38 +01:00
|
|
|
ImGui::InputInt("hex.builtin.provider.gdb.port"_lang, &this->m_port, 0, 0);
|
2021-12-12 00:41:44 +01:00
|
|
|
|
2022-01-20 23:24:26 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InputHexadecimal("hex.builtin.common.size"_lang, &this->m_size, ImGuiInputTextFlags_CharsHexadecimal);
|
2022-01-20 23:24:26 +01:00
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
if (this->m_port < 0)
|
|
|
|
this->m_port = 0;
|
|
|
|
else if (this->m_port > 0xFFFF)
|
|
|
|
this->m_port = 0xFFFF;
|
2023-03-16 16:48:15 +01:00
|
|
|
|
|
|
|
return !this->m_ipAddress.empty() && this->m_port != 0;
|
2021-12-12 00:41:44 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
void GDBProvider::loadSettings(const nlohmann::json &settings) {
|
|
|
|
Provider::loadSettings(settings);
|
|
|
|
|
2023-05-16 11:33:00 +02:00
|
|
|
this->m_ipAddress = settings.at("ip").get<std::string>();
|
|
|
|
this->m_port = settings.at("port").get<int>();
|
|
|
|
this->m_size = settings.at("size").get<size_t>();
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json GDBProvider::storeSettings(nlohmann::json settings) const {
|
|
|
|
settings["ip"] = this->m_ipAddress;
|
|
|
|
settings["port"] = this->m_port;
|
|
|
|
settings["size"] = this->m_size;
|
|
|
|
|
|
|
|
return Provider::storeSettings(settings);
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:45:18 +02:00
|
|
|
std::pair<Region, bool> GDBProvider::getRegionValidity(u64 address) const {
|
2022-08-17 16:28:44 +02:00
|
|
|
address -= this->getBaseAddress();
|
|
|
|
|
2022-08-14 14:45:18 +02:00
|
|
|
if (address < this->getActualSize())
|
2022-08-17 16:28:44 +02:00
|
|
|
return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true };
|
2022-08-14 14:45:18 +02:00
|
|
|
else
|
|
|
|
return { Region::Invalid(), false };
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
std::variant<std::string, i128> GDBProvider::queryInformation(const std::string &category, const std::string &argument) {
|
|
|
|
if (category == "ip")
|
|
|
|
return this->m_ipAddress;
|
|
|
|
else if (category == "port")
|
|
|
|
return this->m_port;
|
|
|
|
else
|
|
|
|
return Provider::queryInformation(category, argument);
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|