From c559c2ff433a8a6bef10a4276a6deadff6cc84a8 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 29 May 2025 08:28:39 +0200 Subject: [PATCH] Support stopping emulation while all threads are sleeping This fixes #242 --- src/windows-emulator/windows_emulator.cpp | 10 +++++++++- src/windows-emulator/windows_emulator.hpp | 2 ++ src/windows-gdb-stub/win_x64_gdb_stub_handler.hpp | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 4e333ee9..76e6cc79 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -606,11 +606,13 @@ void windows_emulator::setup_hooks() void windows_emulator::start(size_t count) { + this->should_stop = false; + const auto use_count = count > 0; const auto start_instructions = this->executed_instructions_; const auto target_instructions = start_instructions + count; - while (true) + while (!this->should_stop) { if (this->switch_thread_ || !this->current_thread().is_thread_ready(this->process, this->clock())) { @@ -638,6 +640,12 @@ void windows_emulator::start(size_t count) } } +void windows_emulator::stop() +{ + this->should_stop = true; + this->emu().stop(); +} + void windows_emulator::register_factories(utils::buffer_deserializer& buffer) { buffer.register_factory([this] { diff --git a/src/windows-emulator/windows_emulator.hpp b/src/windows-emulator/windows_emulator.hpp index da02e78c..520e1c80 100644 --- a/src/windows-emulator/windows_emulator.hpp +++ b/src/windows-emulator/windows_emulator.hpp @@ -132,6 +132,7 @@ class windows_emulator } void start(size_t count = 0); + void stop(); void serialize(utils::buffer_serializer& buffer) const; void deserialize(utils::buffer_deserializer& buffer); @@ -191,6 +192,7 @@ class windows_emulator bool switch_thread_{false}; bool use_relative_time_{false}; bool silent_until_main_{false}; + std::atomic_bool should_stop{false}; std::unordered_map port_mappings_{}; diff --git a/src/windows-gdb-stub/win_x64_gdb_stub_handler.hpp b/src/windows-gdb-stub/win_x64_gdb_stub_handler.hpp index 1755e09a..fb15e878 100644 --- a/src/windows-gdb-stub/win_x64_gdb_stub_handler.hpp +++ b/src/windows-gdb-stub/win_x64_gdb_stub_handler.hpp @@ -14,6 +14,11 @@ class win_x64_gdb_stub_handler : public x64_gdb_stub_handler { } + void on_interrupt() override + { + this->win_emu_->stop(); + } + bool should_stop() override { return this->should_stop_();