Skip to main content

Overview

The emulator_callbacks struct provides a comprehensive set of optional callback functions for monitoring and controlling Windows emulator behavior. Callbacks enable instrumentation, debugging, and custom behavior during emulation.

Definition

Inherits callbacks from:
  • module_manager::callbacks - Module loading/unloading events
  • process_context::callbacks - Process and thread events
Source: windows_emulator.hpp:21

Callback Types

All callbacks use opt_func (alias for utils::optional_function) which allows them to be unset (null) by default.

Exception Callbacks

on_exception

Called when an exception occurs during emulation. Parameters: None Source: windows_emulator.hpp:25 Example:

Memory Callbacks

on_memory_protect

Called when memory protection is changed.
uint64_t
Starting address of the memory region
uint64_t
Size of the memory region in bytes
memory_permission
New memory protection flags (read/write/exec)
Source: windows_emulator.hpp:27 Example:

on_memory_allocate

Called when memory is allocated.
uint64_t
Starting address of the allocated region
uint64_t
Size of the allocation in bytes
memory_permission
Memory protection flags for the allocation
bool
Whether the memory is committed (true) or just reserved (false)
Source: windows_emulator.hpp:28 Example:

on_memory_violate

Called when a memory access violation occurs.
uint64_t
Address where the violation occurred
uint64_t
Size of the attempted access
memory_operation
Type of operation (read/write/exec)
memory_violation_type
Violation type (unmapped or protection violation)
Source: windows_emulator.hpp:29 Example:

CPU Instruction Callbacks

on_rdtsc

Called when the RDTSC (Read Time-Stamp Counter) instruction is executed. Parameters: None Source: windows_emulator.hpp:31 Example:

on_rdtscp

Called when the RDTSCP (Read Time-Stamp Counter and Processor ID) instruction is executed. Parameters: None Source: windows_emulator.hpp:32 Example:

on_instruction

Called before each instruction is executed.
uint64_t
Virtual address of the instruction being executed
Source: windows_emulator.hpp:39 Example:
Note: This callback can significantly slow down emulation if enabled for all instructions.

System Call Callbacks

on_syscall

Called when a system call is about to be executed.
uint32_t
The system call number/ID
std::string_view
Name of the system call (e.g., “NtCreateFile”)
Returns: instruction_hook_continuation indicating whether to execute or skip the syscall
  • instruction_hook_continuation::run_instruction - Execute the syscall
  • instruction_hook_continuation::skip_instruction - Skip the syscall
Source: windows_emulator.hpp:33 Example:

I/O and Activity Callbacks

on_stdout

Called when the emulated process writes to stdout.
std::string_view
The data written to stdout
Source: windows_emulator.hpp:34 Example:

on_debug_string

Called when the emulated process outputs a debug string (via OutputDebugString).
std::string_view
The debug message
Source: windows_emulator.hpp:38 Example:

on_ioctrl

Called when an IOCTL (I/O Control) operation is performed on a device.
io_device&
Reference to the I/O device
std::u16string_view
Name of the device (UTF-16)
ULONG
IOCTL control code
Source: windows_emulator.hpp:40 Example:

Activity Tracking Callbacks

on_generic_access

Called when the emulator accesses a generic resource (file, registry key, etc.).
std::string_view
Type of resource being accessed (e.g., “file”, “registry”)
std::u16string_view
Name/path of the resource (UTF-16)
Source: windows_emulator.hpp:35 Example:

on_generic_activity

Called when generic activity occurs in the emulator.
std::string_view
Description of the activity
Source: windows_emulator.hpp:36 Example:

on_suspicious_activity

Called when potentially suspicious or malicious activity is detected.
std::string_view
Description of the suspicious activity
Source: windows_emulator.hpp:37 Example:

instruction_hook_continuation

Return value for instruction hooks indicating whether to execute or skip.

memory_operation

Alias for memory_permission when used to describe the type of memory access.

memory_violation_type

Type of memory violation:
  • unmapped - Accessing unmapped memory
  • protection - Permission violation (e.g., writing to read-only memory)

Usage Example

Module Management Callbacks

These callbacks are inherited from module_manager::callbacks and track DLL loading/unloading.

on_module_load

Called when a module (DLL or EXE) is loaded into memory.
mapped_module&
Reference to the loaded module
Example:

on_module_unload

Called when a module is unloaded from memory.
mapped_module&
Reference to the module being unloaded

Thread Management Callbacks

These callbacks are inherited from process_context::callbacks and track thread lifecycle.

on_thread_create

Called when a new thread is created.
handle
Handle to the new thread
emulator_thread&
Reference to the new thread object
Example:

on_thread_terminated

Called when a thread terminates.
handle
Handle to the terminated thread
emulator_thread&
Reference to the terminated thread object

on_thread_switch

Called when the emulator switches between threads (context switch).
emulator_thread&
Thread being switched away from
emulator_thread&
Thread being switched to
Example:

on_thread_set_name

Called when a thread’s name is set (via SetThreadDescription or similar).
emulator_thread&
Thread whose name was set

Performance Considerations

  • Callbacks like on_instruction are called very frequently and can significantly impact performance
  • Keep callback implementations lightweight
  • Avoid heavy I/O operations in hot-path callbacks
  • Use filtering in callbacks to minimize overhead
  • Consider batching output instead of printing on every callback
  • Thread callbacks (on_thread_switch) can be called frequently in multi-threaded programs

Notes

  • All callbacks are optional (can be left unset/null)
  • Callbacks are synchronous and block emulation
  • Return values (like in on_syscall) control emulator behavior
  • emulator_callbacks inherits from both module_manager::callbacks and process_context::callbacks
  • UTF-16 strings (std::u16string_view) are used for Windows path/name parameters
  • Module and thread callbacks use callback_list or optional_function for multiple subscribers