Skip to content

sim: runtime debug flags toggle via SIGRTMIN+1#3181

Open
polpetras wants to merge 1 commit into
gem5:developfrom
polpetras:misc/runtime-debug-flag-ctl
Open

sim: runtime debug flags toggle via SIGRTMIN+1#3181
polpetras wants to merge 1 commit into
gem5:developfrom
polpetras:misc/runtime-debug-flag-ctl

Conversation

@polpetras
Copy link
Copy Markdown

@polpetras polpetras commented May 19, 2026

Follows up on discussion #1598.

Summary

This patch adds the ability to enable and disable gem5 debug flags in a
running simulation without restarting it.

gem5 installs a handler on SIGRTMIN+1 at simulation start. A companion
shell script (util/gem5_debug_ctl.sh) writes commands to a temporary file
and sends the signal. Commands are processed on the main event loop at the
next sim tick after delivery — no mid-instruction races.

Supported commands:

Command Effect
+FLAG Enable flag
+FLAG:Nt Enable flag, auto-disable after N ticks
-FLAG Disable flag
trace:on Global output enable (individual flags unchanged)
trace:off Global output silence (individual flags unchanged)

Example usage:

# Enable Rename for 100000 ticks on a running sim (PID e.g. 12345)
util/gem5_debug_ctl.sh <PID> +Rename:100000t

# Enable multiple flags at once for a bounded window, with output enabled
util/gem5_debug_ctl.sh <PID> +Fetch:100000t +Rename:100000t +IEW:100000t trace:on

# Disable Fetch and silence all output
util/gem5_debug_ctl.sh <PID> -Fetch trace:off

Confirmation lines (gem5_debug_ctl: ...) appear in the sim's stdout/log.

Design notes

  • SIGRTMIN+1 is used to avoid SIGRTMIN which is reserved for KVM
    (KVM_KICK_SIGNAL).
  • The signal handler follows the existing gem5 async pattern: sets
    async_event + async_debug_cmd, wakes the primary event queue, returns
    immediately. All file I/O and flag changes happen on the main thread.
  • Flag::_tracing is changed from bool to std::atomic<bool> (relaxed
    ordering) to eliminate the data race that arises when changeFlag() is
    called while other host threads are executing DPRINTF.
  • Auto-disable is implemented as a self-deleting Event (AutoDelete)
    scheduled at curTick() + duration.

This implementation was developed with AI assistance.

Add support for toggling gem5 debug flags in a running simulation
without restarting. A SIGRTMIN+1 handler (debugCmdHandler) sets
async_debug_cmd; doSimLoop() dispatches processDebugCmd() which
reads /tmp/gem5_debug_<pid>.cmd and applies the commands.

Command format (one per line, # comments supported):
  +FLAG         enable flag (stays on until disabled)
  +FLAG:Nt      enable flag, auto-disable after N ticks
  -FLAG         disable flag immediately
  trace:on      global trace master enable
  trace:off     global trace master disable

SIGRTMIN is reserved for KVM (KVM_KICK_SIGNAL in cpu/kvm/base.hh),
so SIGRTMIN+1 is used to avoid conflicts.

Auto-disable is implemented via DebugFlagOffEvent, a self-deleting
Event scheduled on the main event queue. Unknown flag names produce
a warning but do not abort processing of remaining commands.

util/gem5_debug_ctl.sh is a companion shell script that writes the
command file and sends SIGRTMIN+1 (kill -RTMIN+1) to the target pid.

base/debug.hh: Flag::_tracing is made std::atomic<bool> with
memory_order_relaxed to eliminate the data race between the main
thread calling changeFlag() and subordinate threads reading _tracing
in DPRINTFs. On x86/ARM64 relaxed atomics compile to plain
loads/stores so there is no performance overhead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sim General gem5 Simulation Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants