mirror of
https://github.com/NohamR/Reclass.git
synced 2026-05-10 19:59:21 +00:00
WinDbg plugin, ProcessMemoryWindows, dialog cleanup, and misc fixes
- Add WinDbgMemory plugin with debug server connection support - Replace ProcessMemory plugin with Windows-specific ProcessMemoryWindows - Simplify WinDbg dialog: single panel, no tabs, palette-based theming - Fix example text visibility on dark themes (QPalette::Dark -> Disabled WindowText) - Fix "file" -> "File" capitalization in source menu - Add windbg_provider and com_security tests
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(ProcessMemoryPlugin LANGUAGES CXX)
|
||||
project(ProcessMemoryWindowsPlugin LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@@ -12,36 +12,36 @@ set(CMAKE_AUTOUIC ON)
|
||||
|
||||
# Plugin sources
|
||||
set(PLUGIN_SOURCES
|
||||
ProcessMemoryPlugin.h
|
||||
ProcessMemoryPlugin.cpp
|
||||
ProcessMemoryWindowsPlugin.h
|
||||
ProcessMemoryWindowsPlugin.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src/processpicker.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src/processpicker.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src/processpicker.ui
|
||||
)
|
||||
|
||||
# Create shared library (DLL)
|
||||
add_library(ProcessMemoryPlugin SHARED ${PLUGIN_SOURCES})
|
||||
add_library(ProcessMemoryWindowsPlugin SHARED ${PLUGIN_SOURCES})
|
||||
|
||||
# Link Qt
|
||||
target_link_libraries(ProcessMemoryPlugin PRIVATE ${QT}::Widgets ${_QT_WINEXTRAS})
|
||||
target_link_libraries(ProcessMemoryWindowsPlugin PRIVATE ${QT}::Widgets ${_QT_WINEXTRAS})
|
||||
|
||||
# Platform-specific linking
|
||||
if(WIN32)
|
||||
target_link_libraries(ProcessMemoryPlugin PRIVATE psapi shell32)
|
||||
target_link_libraries(ProcessMemoryWindowsPlugin PRIVATE psapi shell32)
|
||||
endif()
|
||||
|
||||
# On Linux, hide all symbols by default so only RCX_PLUGIN_EXPORT-marked ones are exported
|
||||
if(UNIX AND NOT APPLE)
|
||||
target_compile_options(ProcessMemoryPlugin PRIVATE -fvisibility=hidden)
|
||||
target_compile_options(ProcessMemoryWindowsPlugin PRIVATE -fvisibility=hidden)
|
||||
endif()
|
||||
|
||||
# Include directories
|
||||
target_include_directories(ProcessMemoryPlugin PRIVATE
|
||||
target_include_directories(ProcessMemoryWindowsPlugin PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src
|
||||
)
|
||||
|
||||
# Output to Plugins folder
|
||||
set_target_properties(ProcessMemoryPlugin PROPERTIES
|
||||
set_target_properties(ProcessMemoryWindowsPlugin PROPERTIES
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Plugins"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Plugins"
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ProcessMemoryPlugin.h"
|
||||
#include "ProcessMemoryWindowsPlugin.h"
|
||||
|
||||
#include "../../src/processpicker.h"
|
||||
|
||||
@@ -32,12 +32,12 @@
|
||||
#endif
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// ProcessMemoryProvider implementation
|
||||
// ProcessMemoryWindowsProvider implementation
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
ProcessMemoryProvider::ProcessMemoryProvider(uint32_t pid, const QString& processName)
|
||||
ProcessMemoryWindowsProvider::ProcessMemoryWindowsProvider(uint32_t pid, const QString& processName)
|
||||
: m_handle(nullptr)
|
||||
, m_pid(pid)
|
||||
, m_processName(processName)
|
||||
@@ -60,7 +60,7 @@ ProcessMemoryProvider::ProcessMemoryProvider(uint32_t pid, const QString& proces
|
||||
cacheModules();
|
||||
}
|
||||
|
||||
bool ProcessMemoryProvider::read(uint64_t addr, void* buf, int len) const
|
||||
bool ProcessMemoryWindowsProvider::read(uint64_t addr, void* buf, int len) const
|
||||
{
|
||||
if (!m_handle || len <= 0) return false;
|
||||
|
||||
@@ -71,7 +71,7 @@ bool ProcessMemoryProvider::read(uint64_t addr, void* buf, int len) const
|
||||
return bytesRead > 0;
|
||||
}
|
||||
|
||||
bool ProcessMemoryProvider::write(uint64_t addr, const void* buf, int len)
|
||||
bool ProcessMemoryWindowsProvider::write(uint64_t addr, const void* buf, int len)
|
||||
{
|
||||
if (!m_handle || !m_writable || len <= 0) return false;
|
||||
|
||||
@@ -81,7 +81,7 @@ bool ProcessMemoryProvider::write(uint64_t addr, const void* buf, int len)
|
||||
return false;
|
||||
}
|
||||
|
||||
QString ProcessMemoryProvider::getSymbol(uint64_t addr) const
|
||||
QString ProcessMemoryWindowsProvider::getSymbol(uint64_t addr) const
|
||||
{
|
||||
for (const auto& mod : m_modules)
|
||||
{
|
||||
@@ -96,7 +96,7 @@ QString ProcessMemoryProvider::getSymbol(uint64_t addr) const
|
||||
return {};
|
||||
}
|
||||
|
||||
void ProcessMemoryProvider::cacheModules()
|
||||
void ProcessMemoryWindowsProvider::cacheModules()
|
||||
{
|
||||
HMODULE mods[1024];
|
||||
DWORD needed = 0;
|
||||
@@ -126,7 +126,7 @@ void ProcessMemoryProvider::cacheModules()
|
||||
|
||||
#elif defined(__linux__)
|
||||
|
||||
ProcessMemoryProvider::ProcessMemoryProvider(uint32_t pid, const QString& processName)
|
||||
ProcessMemoryWindowsProvider::ProcessMemoryWindowsProvider(uint32_t pid, const QString& processName)
|
||||
: m_fd(-1)
|
||||
, m_pid(pid)
|
||||
, m_processName(processName)
|
||||
@@ -152,7 +152,7 @@ ProcessMemoryProvider::ProcessMemoryProvider(uint32_t pid, const QString& proces
|
||||
|
||||
}
|
||||
|
||||
bool ProcessMemoryProvider::read(uint64_t addr, void* buf, int len) const
|
||||
bool ProcessMemoryWindowsProvider::read(uint64_t addr, void* buf, int len) const
|
||||
{
|
||||
if (m_fd < 0 || len <= 0) return false;
|
||||
|
||||
@@ -176,7 +176,7 @@ bool ProcessMemoryProvider::read(uint64_t addr, void* buf, int len) const
|
||||
return nread == static_cast<ssize_t>(len);
|
||||
}
|
||||
|
||||
bool ProcessMemoryProvider::write(uint64_t addr, const void* buf, int len)
|
||||
bool ProcessMemoryWindowsProvider::write(uint64_t addr, const void* buf, int len)
|
||||
{
|
||||
if (m_fd < 0 || !m_writable || len <= 0) return false;
|
||||
|
||||
@@ -200,7 +200,7 @@ bool ProcessMemoryProvider::write(uint64_t addr, const void* buf, int len)
|
||||
return nwritten == static_cast<ssize_t>(len);
|
||||
}
|
||||
|
||||
QString ProcessMemoryProvider::getSymbol(uint64_t addr) const
|
||||
QString ProcessMemoryWindowsProvider::getSymbol(uint64_t addr) const
|
||||
{
|
||||
for (const auto& mod : m_modules)
|
||||
{
|
||||
@@ -215,7 +215,7 @@ QString ProcessMemoryProvider::getSymbol(uint64_t addr) const
|
||||
return {};
|
||||
}
|
||||
|
||||
void ProcessMemoryProvider::cacheModules()
|
||||
void ProcessMemoryWindowsProvider::cacheModules()
|
||||
{
|
||||
// Parse /proc/<pid>/maps to discover loaded modules
|
||||
QString mapsPath = QStringLiteral("/proc/%1/maps").arg(m_pid);
|
||||
@@ -288,7 +288,7 @@ void ProcessMemoryProvider::cacheModules()
|
||||
|
||||
#endif // platform
|
||||
|
||||
ProcessMemoryProvider::~ProcessMemoryProvider()
|
||||
ProcessMemoryWindowsProvider::~ProcessMemoryWindowsProvider()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (m_handle)
|
||||
@@ -299,7 +299,7 @@ ProcessMemoryProvider::~ProcessMemoryProvider()
|
||||
#endif
|
||||
}
|
||||
|
||||
int ProcessMemoryProvider::size() const
|
||||
int ProcessMemoryWindowsProvider::size() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return m_handle ? 0x10000 : 0;
|
||||
@@ -309,22 +309,22 @@ int ProcessMemoryProvider::size() const
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// ProcessMemoryPlugin implementation
|
||||
// ProcessMemoryWindowsPlugin implementation
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
|
||||
QIcon ProcessMemoryPlugin::Icon() const
|
||||
QIcon ProcessMemoryWindowsPlugin::Icon() const
|
||||
{
|
||||
return qApp->style()->standardIcon(QStyle::SP_ComputerIcon);
|
||||
}
|
||||
|
||||
bool ProcessMemoryPlugin::canHandle(const QString& target) const
|
||||
bool ProcessMemoryWindowsPlugin::canHandle(const QString& target) const
|
||||
{
|
||||
// Target format: "pid:name" or just "pid"
|
||||
QRegularExpression re("^\\d+");
|
||||
return re.match(target).hasMatch();
|
||||
}
|
||||
|
||||
std::unique_ptr<rcx::Provider> ProcessMemoryPlugin::createProvider(const QString& target, QString* errorMsg)
|
||||
std::unique_ptr<rcx::Provider> ProcessMemoryWindowsPlugin::createProvider(const QString& target, QString* errorMsg)
|
||||
{
|
||||
// Parse target: "pid:name" or just "pid"
|
||||
QStringList parts = target.split(':');
|
||||
@@ -339,7 +339,7 @@ std::unique_ptr<rcx::Provider> ProcessMemoryPlugin::createProvider(const QString
|
||||
|
||||
QString name = parts.size() > 1 ? parts[1] : QString("PID %1").arg(pid);
|
||||
|
||||
auto provider = std::make_unique<ProcessMemoryProvider>(pid, name);
|
||||
auto provider = std::make_unique<ProcessMemoryWindowsProvider>(pid, name);
|
||||
if (!provider->isValid())
|
||||
{
|
||||
if (errorMsg)
|
||||
@@ -352,7 +352,7 @@ std::unique_ptr<rcx::Provider> ProcessMemoryPlugin::createProvider(const QString
|
||||
return provider;
|
||||
}
|
||||
|
||||
uint64_t ProcessMemoryPlugin::getInitialBaseAddress(const QString& target) const
|
||||
uint64_t ProcessMemoryWindowsPlugin::getInitialBaseAddress(const QString& target) const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// Parse PID from target
|
||||
@@ -409,7 +409,7 @@ uint64_t ProcessMemoryPlugin::getInitialBaseAddress(const QString& target) const
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ProcessMemoryPlugin::selectTarget(QWidget* parent, QString* target)
|
||||
bool ProcessMemoryWindowsPlugin::selectTarget(QWidget* parent, QString* target)
|
||||
{
|
||||
// Use custom process enumeration from plugin
|
||||
QVector<PluginProcessInfo> pluginProcesses = enumerateProcesses();
|
||||
@@ -440,7 +440,7 @@ bool ProcessMemoryPlugin::selectTarget(QWidget* parent, QString* target)
|
||||
return false;
|
||||
}
|
||||
|
||||
QVector<PluginProcessInfo> ProcessMemoryPlugin::enumerateProcesses()
|
||||
QVector<PluginProcessInfo> ProcessMemoryWindowsPlugin::enumerateProcesses()
|
||||
{
|
||||
QVector<PluginProcessInfo> processes;
|
||||
|
||||
@@ -543,5 +543,5 @@ QVector<PluginProcessInfo> ProcessMemoryPlugin::enumerateProcesses()
|
||||
|
||||
extern "C" RCX_PLUGIN_EXPORT IPlugin* CreatePlugin()
|
||||
{
|
||||
return new ProcessMemoryPlugin();
|
||||
return new ProcessMemoryWindowsPlugin();
|
||||
}
|
||||
@@ -5,14 +5,14 @@
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* Process memory provider
|
||||
* Reads/writes memory from a live process using platform APIs
|
||||
* Process memory provider (Windows)
|
||||
* Reads/writes memory from a live process using Windows platform APIs
|
||||
*/
|
||||
class ProcessMemoryProvider : public rcx::Provider
|
||||
class ProcessMemoryWindowsProvider : public rcx::Provider
|
||||
{
|
||||
public:
|
||||
ProcessMemoryProvider(uint32_t pid, const QString& processName);
|
||||
~ProcessMemoryProvider() override;
|
||||
ProcessMemoryWindowsProvider(uint32_t pid, const QString& processName);
|
||||
~ProcessMemoryWindowsProvider() override;
|
||||
|
||||
// Required overrides
|
||||
bool read(uint64_t addr, void* buf, int len) const override;
|
||||
@@ -57,15 +57,15 @@ private:
|
||||
};
|
||||
|
||||
/**
|
||||
* Plugin that provides ProcessMemoryProvider
|
||||
* Plugin that provides ProcessMemoryWindowsProvider
|
||||
*/
|
||||
class ProcessMemoryPlugin : public IProviderPlugin
|
||||
class ProcessMemoryWindowsPlugin : public IProviderPlugin
|
||||
{
|
||||
public:
|
||||
std::string Name() const override { return "Process Memory"; }
|
||||
std::string Name() const override { return "Process Memory Windows"; }
|
||||
std::string Version() const override { return "1.0.0"; }
|
||||
std::string Author() const override { return "Reclass"; }
|
||||
std::string Description() const override { return "Read and write memory from local running processes"; }
|
||||
std::string Description() const override { return "Read and write memory from local running processes (Windows)"; }
|
||||
k_ELoadType LoadType() const override { return k_ELoadTypeAuto; }
|
||||
QIcon Icon() const override;
|
||||
|
||||
34
plugins/WinDbgMemory/CMakeLists.txt
Normal file
34
plugins/WinDbgMemory/CMakeLists.txt
Normal file
@@ -0,0 +1,34 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(WinDbgMemoryPlugin LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Qt is found by the parent project; QT variable (Qt5 or Qt6) is inherited
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
|
||||
# Plugin sources
|
||||
set(PLUGIN_SOURCES
|
||||
WinDbgMemoryPlugin.h
|
||||
WinDbgMemoryPlugin.cpp
|
||||
)
|
||||
|
||||
# Create shared library (DLL)
|
||||
add_library(WinDbgMemoryPlugin SHARED ${PLUGIN_SOURCES})
|
||||
|
||||
# Link Qt + DbgEng
|
||||
target_link_libraries(WinDbgMemoryPlugin PRIVATE ${QT}::Widgets dbgeng ole32)
|
||||
|
||||
# Include directories
|
||||
target_include_directories(WinDbgMemoryPlugin PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../src
|
||||
)
|
||||
|
||||
# Output to Plugins folder
|
||||
set_target_properties(WinDbgMemoryPlugin PROPERTIES
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Plugins"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Plugins"
|
||||
)
|
||||
510
plugins/WinDbgMemory/WinDbgMemoryPlugin.cpp
Normal file
510
plugins/WinDbgMemory/WinDbgMemoryPlugin.cpp
Normal file
@@ -0,0 +1,510 @@
|
||||
#include "WinDbgMemoryPlugin.h"
|
||||
|
||||
#include <QStyle>
|
||||
#include <QApplication>
|
||||
#include <QMessageBox>
|
||||
#include <QDialog>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QLabel>
|
||||
#include <QDebug>
|
||||
#include <QClipboard>
|
||||
#include <QGuiApplication>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <initguid.h>
|
||||
#include <dbgeng.h>
|
||||
#pragma comment(lib, "dbgeng.lib")
|
||||
#endif
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// Thread dispatch helper
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
|
||||
template<typename Fn>
|
||||
void WinDbgMemoryProvider::dispatchToOwner(Fn&& fn) const
|
||||
{
|
||||
if (!m_dispatcher) { fn(); return; }
|
||||
|
||||
if (QThread::currentThread() == m_dispatcher->thread()) {
|
||||
// Already on the owning thread — call directly
|
||||
fn();
|
||||
} else {
|
||||
// Marshal to the owning thread and block until done
|
||||
QMetaObject::invokeMethod(m_dispatcher, std::forward<Fn>(fn),
|
||||
Qt::BlockingQueuedConnection);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// WinDbgMemoryProvider implementation
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
|
||||
WinDbgMemoryProvider::WinDbgMemoryProvider(const QString& target)
|
||||
{
|
||||
// Create a dedicated thread for all DbgEng COM operations.
|
||||
// DbgEng's remote transport (TCP/named-pipe) is thread-affine — all
|
||||
// calls must happen on the thread that called DebugConnect/DebugCreate.
|
||||
// A private thread with its own event loop guarantees:
|
||||
// 1. dispatchToOwner() works from any calling thread (main, thread-pool, etc.)
|
||||
// 2. No deadlock — the DbgEng thread is never blocked by the caller
|
||||
m_dbgThread = new QThread();
|
||||
m_dbgThread->setObjectName(QStringLiteral("DbgEngThread"));
|
||||
m_dbgThread->start();
|
||||
|
||||
m_dispatcher = new DbgEngDispatcher();
|
||||
m_dispatcher->moveToThread(m_dbgThread);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Run all DbgEng initialization on the dedicated thread.
|
||||
// BlockingQueuedConnection blocks us until the lambda finishes,
|
||||
// so member variables written inside are visible after the call.
|
||||
dispatchToOwner([this, &target]() {
|
||||
HRESULT hr;
|
||||
|
||||
qDebug() << "[WinDbg] Opening target:" << target
|
||||
<< "on DbgEng thread" << QThread::currentThread();
|
||||
|
||||
if (target.startsWith("tcp:", Qt::CaseInsensitive)
|
||||
|| target.startsWith("npipe:", Qt::CaseInsensitive))
|
||||
{
|
||||
// ── Remote: connect to existing WinDbg debug server ──
|
||||
QByteArray connUtf8 = target.toUtf8();
|
||||
qDebug() << "[WinDbg] DebugConnect:" << target;
|
||||
hr = DebugConnect(connUtf8.constData(), IID_IDebugClient, (void**)&m_client);
|
||||
qDebug() << "[WinDbg] DebugConnect hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "client=" << (void*)m_client;
|
||||
if (FAILED(hr) || !m_client) {
|
||||
qWarning() << "[WinDbg] DebugConnect FAILED hr=0x" << Qt::hex << (unsigned long)hr;
|
||||
return;
|
||||
}
|
||||
m_isRemote = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// ── Local: create debug client for pid/dump ──
|
||||
hr = DebugCreate(IID_IDebugClient, (void**)&m_client);
|
||||
qDebug() << "[WinDbg] DebugCreate hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "client=" << (void*)m_client;
|
||||
if (FAILED(hr) || !m_client) {
|
||||
qWarning() << "[WinDbg] DebugCreate FAILED hr=0x" << Qt::hex << (unsigned long)hr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (target.startsWith("pid:", Qt::CaseInsensitive))
|
||||
{
|
||||
bool ok = false;
|
||||
ULONG pid = target.mid(4).trimmed().toULong(&ok);
|
||||
if (!ok || pid == 0) {
|
||||
qWarning() << "[WinDbg] Invalid PID in target:" << target;
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug() << "[WinDbg] Attaching to PID" << pid << "(non-invasive)";
|
||||
hr = m_client->AttachProcess(
|
||||
0, pid,
|
||||
DEBUG_ATTACH_NONINVASIVE | DEBUG_ATTACH_NONINVASIVE_NO_SUSPEND);
|
||||
qDebug() << "[WinDbg] AttachProcess hr=" << Qt::hex << (unsigned long)hr;
|
||||
if (FAILED(hr)) {
|
||||
qWarning() << "[WinDbg] AttachProcess FAILED";
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (target.startsWith("dump:", Qt::CaseInsensitive))
|
||||
{
|
||||
QString path = target.mid(5).trimmed();
|
||||
QByteArray pathUtf8 = path.toUtf8();
|
||||
|
||||
qDebug() << "[WinDbg] Opening dump file:" << path;
|
||||
hr = m_client->OpenDumpFile(pathUtf8.constData());
|
||||
qDebug() << "[WinDbg] OpenDumpFile hr=" << Qt::hex << (unsigned long)hr;
|
||||
if (FAILED(hr)) {
|
||||
qWarning() << "[WinDbg] OpenDumpFile FAILED";
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning() << "[WinDbg] Unknown target format:" << target;
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
initInterfaces();
|
||||
|
||||
// WaitForEvent to finalize the attach/dump load.
|
||||
// For remote connections the server session is already active — skip.
|
||||
if (m_control && !m_isRemote) {
|
||||
qDebug() << "[WinDbg] WaitForEvent...";
|
||||
hr = m_control->WaitForEvent(0, 10000);
|
||||
qDebug() << "[WinDbg] WaitForEvent hr=" << Qt::hex << (unsigned long)hr;
|
||||
}
|
||||
|
||||
querySessionInfo();
|
||||
});
|
||||
|
||||
#else
|
||||
Q_UNUSED(target);
|
||||
#endif
|
||||
}
|
||||
|
||||
void WinDbgMemoryProvider::initInterfaces()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!m_client) return;
|
||||
|
||||
HRESULT hr;
|
||||
hr = m_client->QueryInterface(IID_IDebugDataSpaces, (void**)&m_dataSpaces);
|
||||
qDebug() << "[WinDbg] IDebugDataSpaces hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "ptr=" << (void*)m_dataSpaces;
|
||||
|
||||
hr = m_client->QueryInterface(IID_IDebugControl, (void**)&m_control);
|
||||
qDebug() << "[WinDbg] IDebugControl hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "ptr=" << (void*)m_control;
|
||||
|
||||
hr = m_client->QueryInterface(IID_IDebugSymbols, (void**)&m_symbols);
|
||||
qDebug() << "[WinDbg] IDebugSymbols hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "ptr=" << (void*)m_symbols;
|
||||
|
||||
if (!m_dataSpaces) {
|
||||
qWarning() << "[WinDbg] No IDebugDataSpaces — cleaning up";
|
||||
cleanup();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void WinDbgMemoryProvider::querySessionInfo()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!m_client) return;
|
||||
HRESULT hr;
|
||||
|
||||
if (m_control) {
|
||||
ULONG debugClass = 0, debugQualifier = 0;
|
||||
hr = m_control->GetDebuggeeType(&debugClass, &debugQualifier);
|
||||
qDebug() << "[WinDbg] GetDebuggeeType hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "class=" << debugClass << "qualifier=" << debugQualifier;
|
||||
if (SUCCEEDED(hr)) {
|
||||
m_isLive = (debugQualifier < DEBUG_DUMP_SMALL);
|
||||
m_writable = m_isLive;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_symbols) {
|
||||
ULONG numModules = 0, numUnloaded = 0;
|
||||
hr = m_symbols->GetNumberModules(&numModules, &numUnloaded);
|
||||
qDebug() << "[WinDbg] GetNumberModules hr=" << Qt::hex << (unsigned long)hr
|
||||
<< "loaded=" << numModules << "unloaded=" << numUnloaded;
|
||||
if (SUCCEEDED(hr) && numModules > 0) {
|
||||
char modName[256] = {};
|
||||
ULONG modSize = 0;
|
||||
hr = m_symbols->GetModuleNames(0, 0, nullptr, 0, nullptr,
|
||||
modName, sizeof(modName), &modSize,
|
||||
nullptr, 0, nullptr);
|
||||
if (SUCCEEDED(hr) && modSize > 0)
|
||||
m_name = QString::fromUtf8(modName);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_name.isEmpty())
|
||||
m_name = m_isLive ? QStringLiteral("DbgEng (Live)") : QStringLiteral("DbgEng (Dump)");
|
||||
|
||||
if (m_symbols) {
|
||||
ULONG numModules = 0, numUnloaded = 0;
|
||||
hr = m_symbols->GetNumberModules(&numModules, &numUnloaded);
|
||||
if (SUCCEEDED(hr) && numModules > 0) {
|
||||
ULONG64 moduleBase = 0;
|
||||
hr = m_symbols->GetModuleByIndex(0, &moduleBase);
|
||||
qDebug() << "[WinDbg] Module 0 base=" << Qt::hex << moduleBase;
|
||||
if (SUCCEEDED(hr))
|
||||
m_base = moduleBase;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_base && m_dataSpaces) {
|
||||
uint8_t probe[2] = {};
|
||||
ULONG got = 0;
|
||||
hr = m_dataSpaces->ReadVirtual(m_base, probe, 2, &got);
|
||||
qDebug() << "[WinDbg] Probe read at" << Qt::hex << m_base
|
||||
<< "hr=" << (unsigned long)hr << "got=" << got
|
||||
<< "bytes:" << (int)probe[0] << (int)probe[1];
|
||||
if (FAILED(hr) || got == 0) {
|
||||
qWarning() << "[WinDbg] Probe read FAILED — cleaning up";
|
||||
cleanup();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << "[WinDbg] Ready. name=" << m_name
|
||||
<< "base=" << Qt::hex << m_base << "isLive=" << m_isLive;
|
||||
#endif
|
||||
}
|
||||
|
||||
WinDbgMemoryProvider::~WinDbgMemoryProvider()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// Dispatch COM cleanup to the DbgEng thread (thread-affine release)
|
||||
if (m_dbgThread && m_dbgThread->isRunning() && m_dispatcher) {
|
||||
dispatchToOwner([this]() {
|
||||
if (m_client) {
|
||||
if (m_isRemote)
|
||||
m_client->EndSession(DEBUG_END_DISCONNECT);
|
||||
else
|
||||
m_client->DetachProcesses();
|
||||
}
|
||||
cleanup();
|
||||
});
|
||||
} else {
|
||||
// Thread not running — clean up directly (best-effort)
|
||||
if (m_client) {
|
||||
if (m_isRemote)
|
||||
m_client->EndSession(DEBUG_END_DISCONNECT);
|
||||
else
|
||||
m_client->DetachProcesses();
|
||||
}
|
||||
cleanup();
|
||||
}
|
||||
#else
|
||||
cleanup();
|
||||
#endif
|
||||
|
||||
// Stop the dedicated thread
|
||||
if (m_dbgThread) {
|
||||
m_dbgThread->quit();
|
||||
m_dbgThread->wait(3000);
|
||||
delete m_dbgThread;
|
||||
m_dbgThread = nullptr;
|
||||
}
|
||||
delete m_dispatcher;
|
||||
m_dispatcher = nullptr;
|
||||
}
|
||||
|
||||
void WinDbgMemoryProvider::cleanup()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (m_symbols) { m_symbols->Release(); m_symbols = nullptr; }
|
||||
if (m_control) { m_control->Release(); m_control = nullptr; }
|
||||
if (m_dataSpaces) { m_dataSpaces->Release(); m_dataSpaces = nullptr; }
|
||||
if (m_client) { m_client->Release(); m_client = nullptr; }
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WinDbgMemoryProvider::read(uint64_t addr, void* buf, int len) const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!m_dataSpaces || len <= 0) return false;
|
||||
|
||||
bool result = false;
|
||||
dispatchToOwner([&]() {
|
||||
ULONG bytesRead = 0;
|
||||
HRESULT hr = m_dataSpaces->ReadVirtual(m_base + addr, buf, (ULONG)len, &bytesRead);
|
||||
if (FAILED(hr) || (int)bytesRead < len)
|
||||
memset((char*)buf + bytesRead, 0, len - bytesRead);
|
||||
result = bytesRead > 0;
|
||||
});
|
||||
return result;
|
||||
#else
|
||||
Q_UNUSED(addr); Q_UNUSED(buf); Q_UNUSED(len);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WinDbgMemoryProvider::write(uint64_t addr, const void* buf, int len)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!m_dataSpaces || !m_writable || len <= 0) return false;
|
||||
|
||||
bool result = false;
|
||||
dispatchToOwner([&]() {
|
||||
ULONG bytesWritten = 0;
|
||||
HRESULT hr = m_dataSpaces->WriteVirtual(m_base + addr, const_cast<void*>(buf),
|
||||
(ULONG)len, &bytesWritten);
|
||||
result = SUCCEEDED(hr) && bytesWritten == (ULONG)len;
|
||||
});
|
||||
return result;
|
||||
#else
|
||||
Q_UNUSED(addr); Q_UNUSED(buf); Q_UNUSED(len);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
int WinDbgMemoryProvider::size() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return m_dataSpaces ? 0x10000 : 0;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WinDbgMemoryProvider::isReadable(uint64_t /*addr*/, int len) const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// DbgEng's ReadVirtual can read any mapped virtual address.
|
||||
return m_dataSpaces != nullptr && len >= 0;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
QString WinDbgMemoryProvider::getSymbol(uint64_t addr) const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!m_symbols) return {};
|
||||
|
||||
QString result;
|
||||
dispatchToOwner([&]() {
|
||||
char nameBuf[512] = {};
|
||||
ULONG nameSize = 0;
|
||||
ULONG64 displacement = 0;
|
||||
HRESULT hr = m_symbols->GetNameByOffset(m_base + addr, nameBuf, sizeof(nameBuf),
|
||||
&nameSize, &displacement);
|
||||
if (SUCCEEDED(hr) && nameSize > 0) {
|
||||
result = QString::fromUtf8(nameBuf);
|
||||
if (displacement > 0)
|
||||
result += QStringLiteral("+0x%1").arg(displacement, 0, 16);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
#else
|
||||
Q_UNUSED(addr);
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// WinDbgMemoryPlugin implementation
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
|
||||
QIcon WinDbgMemoryPlugin::Icon() const
|
||||
{
|
||||
return qApp->style()->standardIcon(QStyle::SP_DriveNetIcon);
|
||||
}
|
||||
|
||||
bool WinDbgMemoryPlugin::canHandle(const QString& target) const
|
||||
{
|
||||
return target.startsWith("tcp:", Qt::CaseInsensitive)
|
||||
|| target.startsWith("npipe:", Qt::CaseInsensitive)
|
||||
|| target.startsWith("pid:", Qt::CaseInsensitive)
|
||||
|| target.startsWith("dump:", Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
std::unique_ptr<rcx::Provider> WinDbgMemoryPlugin::createProvider(const QString& target, QString* errorMsg)
|
||||
{
|
||||
auto provider = std::make_unique<WinDbgMemoryProvider>(target);
|
||||
if (!provider->isValid())
|
||||
{
|
||||
if (errorMsg) {
|
||||
if (target.startsWith("tcp:", Qt::CaseInsensitive)
|
||||
|| target.startsWith("npipe:", Qt::CaseInsensitive))
|
||||
*errorMsg = QString("Failed to connect to debug server.\n\n"
|
||||
"Target: %1\n\n"
|
||||
"Make sure WinDbg is running with a matching .server command\n"
|
||||
"(e.g. .server tcp:port=5055) and the port/pipe is reachable.")
|
||||
.arg(target);
|
||||
else if (target.startsWith("pid:", Qt::CaseInsensitive))
|
||||
*errorMsg = QString("Failed to attach to process.\n\n"
|
||||
"Target: %1\n\n"
|
||||
"Make sure the process is running and you have "
|
||||
"sufficient privileges (try Run as Administrator).")
|
||||
.arg(target);
|
||||
else
|
||||
*errorMsg = QString("Failed to open dump file.\n\n"
|
||||
"Target: %1\n\n"
|
||||
"Make sure the file exists and is a valid dump.")
|
||||
.arg(target);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
uint64_t WinDbgMemoryPlugin::getInitialBaseAddress(const QString& target) const
|
||||
{
|
||||
Q_UNUSED(target);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool WinDbgMemoryPlugin::selectTarget(QWidget* parent, QString* target)
|
||||
{
|
||||
QDialog dlg(parent);
|
||||
dlg.setWindowTitle("WinDbg Settings");
|
||||
dlg.resize(460, 260);
|
||||
|
||||
QPalette dlgPal = qApp->palette();
|
||||
dlg.setPalette(dlgPal);
|
||||
dlg.setAutoFillBackground(true);
|
||||
|
||||
auto* layout = new QVBoxLayout(&dlg);
|
||||
|
||||
layout->addWidget(new QLabel(
|
||||
"Connect to a running WinDbg debug server.\n"
|
||||
"In WinDbg, run: .server tcp:port=5055"));
|
||||
|
||||
layout->addSpacing(8);
|
||||
layout->addWidget(new QLabel("Connection string:"));
|
||||
auto* connEdit = new QLineEdit;
|
||||
connEdit->setPlaceholderText("tcp:Port=5055,Server=localhost");
|
||||
connEdit->setText("tcp:Port=5055,Server=localhost");
|
||||
layout->addWidget(connEdit);
|
||||
|
||||
layout->addSpacing(4);
|
||||
layout->addWidget(new QLabel("Run one of these in WinDbg first:"));
|
||||
|
||||
auto addExample = [&](const QString& text) {
|
||||
auto* row = new QHBoxLayout;
|
||||
auto* label = new QLabel(text);
|
||||
QPalette lp = dlgPal;
|
||||
lp.setColor(QPalette::WindowText, dlgPal.color(QPalette::Disabled, QPalette::WindowText));
|
||||
label->setPalette(lp);
|
||||
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||||
row->addWidget(label, 1);
|
||||
auto* copyBtn = new QPushButton("Copy");
|
||||
copyBtn->setFixedWidth(50);
|
||||
copyBtn->setToolTip("Copy to clipboard");
|
||||
QObject::connect(copyBtn, &QPushButton::clicked, [text]() {
|
||||
QGuiApplication::clipboard()->setText(text);
|
||||
});
|
||||
row->addWidget(copyBtn);
|
||||
layout->addLayout(row);
|
||||
};
|
||||
|
||||
addExample(".server tcp:port=5055");
|
||||
addExample(".server npipe:pipe=reclass");
|
||||
layout->addStretch();
|
||||
|
||||
auto* btnLayout = new QHBoxLayout;
|
||||
btnLayout->addStretch();
|
||||
auto* okBtn = new QPushButton("OK");
|
||||
auto* cancelBtn = new QPushButton("Cancel");
|
||||
btnLayout->addWidget(okBtn);
|
||||
btnLayout->addWidget(cancelBtn);
|
||||
layout->addLayout(btnLayout);
|
||||
|
||||
QObject::connect(okBtn, &QPushButton::clicked, &dlg, &QDialog::accept);
|
||||
QObject::connect(cancelBtn, &QPushButton::clicked, &dlg, &QDialog::reject);
|
||||
|
||||
if (dlg.exec() != QDialog::Accepted)
|
||||
return false;
|
||||
|
||||
QString conn = connEdit->text().trimmed();
|
||||
if (conn.isEmpty()) return false;
|
||||
*target = conn;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// Plugin factory
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
|
||||
extern "C" RCX_PLUGIN_EXPORT IPlugin* CreatePlugin()
|
||||
{
|
||||
return new WinDbgMemoryPlugin();
|
||||
}
|
||||
122
plugins/WinDbgMemory/WinDbgMemoryPlugin.h
Normal file
122
plugins/WinDbgMemory/WinDbgMemoryPlugin.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
#include "../../src/iplugin.h"
|
||||
#include "../../src/core.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <QObject>
|
||||
#include <QThread>
|
||||
|
||||
// Forward declarations for DbgEng COM interfaces
|
||||
struct IDebugClient;
|
||||
struct IDebugDataSpaces;
|
||||
struct IDebugControl;
|
||||
struct IDebugSymbols;
|
||||
|
||||
/**
|
||||
* WinDbg memory provider
|
||||
*
|
||||
* Uses DbgEng to read memory from:
|
||||
* - An existing WinDbg debug server via DebugConnect (tcp/npipe)
|
||||
* - A live process by PID via DebugCreate (non-invasive attach)
|
||||
* - A crash dump (.dmp) file via DebugCreate
|
||||
*
|
||||
* Target string format:
|
||||
* "tcp:Port=5055,Server=localhost" - connect to WinDbg debug server (TCP)
|
||||
* "npipe:Pipe=name,Server=localhost" - connect to WinDbg debug server (named pipe)
|
||||
* "pid:1234" - attach to process 1234
|
||||
* "dump:C:/path/to/file.dmp" - open dump file
|
||||
*
|
||||
* Threading: All DbgEng COM calls are dispatched to the thread that created
|
||||
* the connection (DebugConnect/DebugCreate). This is required because the
|
||||
* remote transport (TCP/named-pipe) binds to the creating thread. The
|
||||
* controller's background refresh threads call read() which transparently
|
||||
* marshals to the owning thread via BlockingQueuedConnection.
|
||||
*/
|
||||
|
||||
// Helper QObject that lives on the DbgEng-owning thread.
|
||||
// Used as a target for QMetaObject::invokeMethod to marshal calls.
|
||||
class DbgEngDispatcher : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using QObject::QObject;
|
||||
};
|
||||
|
||||
class WinDbgMemoryProvider : public rcx::Provider
|
||||
{
|
||||
public:
|
||||
/// Create a provider from a target string
|
||||
WinDbgMemoryProvider(const QString& target);
|
||||
~WinDbgMemoryProvider() override;
|
||||
|
||||
// Required overrides
|
||||
bool read(uint64_t addr, void* buf, int len) const override;
|
||||
int size() const override;
|
||||
|
||||
// Optional overrides
|
||||
bool isReadable(uint64_t addr, int len) const override;
|
||||
bool write(uint64_t addr, const void* buf, int len) override;
|
||||
bool isWritable() const override { return m_writable; }
|
||||
QString name() const override { return m_name; }
|
||||
QString kind() const override { return QStringLiteral("WinDbg"); }
|
||||
QString getSymbol(uint64_t addr) const override;
|
||||
|
||||
bool isLive() const override { return m_isLive; }
|
||||
uint64_t base() const override { return m_base; }
|
||||
void setBase(uint64_t b) override { m_base = b; }
|
||||
|
||||
private:
|
||||
void initInterfaces(); // get IDebugDataSpaces/Control/Symbols from client
|
||||
void querySessionInfo(); // determine live/dump, writable, name, base
|
||||
void cleanup();
|
||||
|
||||
// Marshal a lambda to the DbgEng-owning thread. If already on that
|
||||
// thread, calls directly. Otherwise blocks via QueuedConnection.
|
||||
template<typename Fn>
|
||||
void dispatchToOwner(Fn&& fn) const;
|
||||
|
||||
IDebugClient* m_client = nullptr;
|
||||
IDebugDataSpaces* m_dataSpaces = nullptr;
|
||||
IDebugControl* m_control = nullptr;
|
||||
IDebugSymbols* m_symbols = nullptr;
|
||||
|
||||
QString m_name;
|
||||
uint64_t m_base = 0;
|
||||
bool m_isLive = false;
|
||||
bool m_writable = false;
|
||||
bool m_isRemote = false; // true when connected via DebugConnect (tcp/npipe)
|
||||
|
||||
// Dedicated thread for DbgEng COM operations. The remote TCP/pipe
|
||||
// transport is thread-affine — all calls must happen on the thread
|
||||
// that called DebugConnect. A private thread with its own event loop
|
||||
// ensures dispatchToOwner() works from any calling thread (including
|
||||
// QtConcurrent workers and the main/GUI thread) without deadlock.
|
||||
QThread* m_dbgThread = nullptr;
|
||||
DbgEngDispatcher* m_dispatcher = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* Plugin that provides WinDbgMemoryProvider
|
||||
*
|
||||
* Uses DbgEng to read memory via:
|
||||
* - Remote connection to an existing WinDbg debug server (tcp/npipe)
|
||||
* - Local non-invasive attach to a live process (pid)
|
||||
* - Local crash dump file (dump)
|
||||
*/
|
||||
class WinDbgMemoryPlugin : public IProviderPlugin
|
||||
{
|
||||
public:
|
||||
std::string Name() const override { return "WinDbg Memory"; }
|
||||
std::string Version() const override { return "2.0.0"; }
|
||||
std::string Author() const override { return "Reclass"; }
|
||||
std::string Description() const override { return "Read memory via DbgEng (live process attach or crash dump)"; }
|
||||
k_ELoadType LoadType() const override { return k_ELoadTypeAuto; }
|
||||
QIcon Icon() const override;
|
||||
|
||||
bool canHandle(const QString& target) const override;
|
||||
std::unique_ptr<rcx::Provider> createProvider(const QString& target, QString* errorMsg) override;
|
||||
uint64_t getInitialBaseAddress(const QString& target) const override;
|
||||
bool selectTarget(QWidget* parent, QString* target) override;
|
||||
};
|
||||
|
||||
// Plugin export
|
||||
extern "C" RCX_PLUGIN_EXPORT IPlugin* CreatePlugin();
|
||||
Reference in New Issue
Block a user