Allow MemoryWatcher to follow pointers

This commit is contained in:
spxtr 2015-12-30 19:10:31 -08:00
parent afde6ae72c
commit 525fc4fe8a
2 changed files with 52 additions and 16 deletions

View File

@ -11,7 +11,7 @@
#include "Core/HW/Memmap.h" #include "Core/HW/Memmap.h"
// We don't want to kill the cpu, so sleep for this long after polling. // We don't want to kill the cpu, so sleep for this long after polling.
static const int SLEEP_DURATION = 10; // ms static const int SLEEP_DURATION = 2; // ms
MemoryWatcher::MemoryWatcher() MemoryWatcher::MemoryWatcher()
{ {
@ -39,14 +39,25 @@ bool MemoryWatcher::LoadAddresses(const std::string& path)
if (!locations) if (!locations)
return false; return false;
u32 data; std::string line;
locations >> std::hex; while (std::getline(locations, line))
while (locations >> data) ParseLine(line);
m_values[data] = 0;
return m_values.size() > 0; return m_values.size() > 0;
} }
void MemoryWatcher::ParseLine(const std::string& line)
{
m_values[line] = 0;
m_addresses[line] = std::vector<u32>();
std::stringstream offsets(line);
offsets >> std::hex;
u32 offset;
while (offsets >> offset)
m_addresses[line].push_back(offset);
}
bool MemoryWatcher::OpenSocket(const std::string& path) bool MemoryWatcher::OpenSocket(const std::string& path)
{ {
memset(&m_addr, 0, sizeof(m_addr)); memset(&m_addr, 0, sizeof(m_addr));
@ -57,24 +68,40 @@ bool MemoryWatcher::OpenSocket(const std::string& path)
return m_fd >= 0; return m_fd >= 0;
} }
u32 MemoryWatcher::ChasePointer(const std::string& line)
{
u32 value = 0;
for (u32 offset : m_addresses[line])
value = Memory::Read_U32(value + offset);
return value;
}
std::string MemoryWatcher::ComposeMessage(const std::string& line, u32 value)
{
std::stringstream message_stream;
message_stream << line << '\n' << std::hex << value;
return message_stream.str();
}
void MemoryWatcher::WatcherThread() void MemoryWatcher::WatcherThread()
{ {
while (m_running) while (m_running)
{ {
for (auto& entry : m_values) for (auto& entry : m_values)
{ {
u32 address = entry.first; std::string address = entry.first;
u32& current_value = entry.second; u32& current_value = entry.second;
u32 new_value = Memory::Read_U32(address); u32 new_value = ChasePointer(address);
if (new_value != current_value) if (new_value != current_value)
{ {
// Update the value
current_value = new_value; current_value = new_value;
u32 buf[2] = {address, current_value}; std::string message = ComposeMessage(address, new_value);
sendto( sendto(
m_fd, m_fd,
static_cast<void*>(buf), message.c_str(),
sizeof(buf), message.size() + 1,
0, 0,
reinterpret_cast<sockaddr*>(&m_addr), reinterpret_cast<sockaddr*>(&m_addr),
sizeof(m_addr)); sizeof(m_addr));

View File

@ -7,16 +7,18 @@
#include <atomic> #include <atomic>
#include <map> #include <map>
#include <thread> #include <thread>
#include <vector>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
// MemoryWatcher reads a file containing in-game memory addresses and outputs // MemoryWatcher reads a file containing in-game memory addresses and outputs
// changes to those memory addresses to a unix domain socket as the game runs. // changes to those memory addresses to a unix domain socket as the game runs.
// //
// The input file is a newline-separated list of hex memory addresses // The input file is a newline-separated list of hex memory addresses, without
// (without the "0x"). The output to the socket is two words long, the first // the "0x". To follow pointers, separate addresses with a space. For example,
// containing the address, and the second containing the data as stored in // "ABCD EF" will watch the address at (*0xABCD) + 0xEF.
// game memory. // The output to the socket is two lines. The first is the address from the
// input file, and the second is the new value in hex.
class MemoryWatcher final class MemoryWatcher final
{ {
public: public:
@ -26,6 +28,11 @@ public:
private: private:
bool LoadAddresses(const std::string& path); bool LoadAddresses(const std::string& path);
bool OpenSocket(const std::string& path); bool OpenSocket(const std::string& path);
void ParseLine(const std::string& line);
u32 ChasePointer(const std::string& line);
std::string ComposeMessage(const std::string& line, u32 value);
void WatcherThread(); void WatcherThread();
std::thread m_watcher_thread; std::thread m_watcher_thread;
@ -34,6 +41,8 @@ private:
int m_fd; int m_fd;
sockaddr_un m_addr; sockaddr_un m_addr;
// Address -> last value // Address as stored in the file -> list of offsets to follow
std::map<u32, u32> m_values; std::map<std::string, std::vector<u32>> m_addresses;
// Address as stored in the file -> current value
std::map<std::string, u32> m_values;
}; };