Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 608 of 9368. Advanced Debugging and Hardware VerificationFree lesson

Detecting Deadlock States via Hardware Watchpoints

Configure multi-stage hardware watchpoints to trigger a system halt when circular resource dependencies occur between competing execution threads or interrupts.

In this lesson, you will learn how to configure multi-stage hardware watchpoints to trigger a system halt the moment a circular resource dependency creates a deadlock.

The Mechanism of Deadlock Detection

In multi-threaded or interrupt-driven embedded systems, a deadlock typically occurs when Thread A holds Resource 1 and waits for Resource 2, while Thread B holds Resource 2 and waits for Resource 1. Unlike software-based heartbeats, which may be too slow to catch a stall, hardware watchpoints allow us to monitor the memory locations acting as "locks" (mutexes or semaphores) at wire speed.

We implement this by placing watchpoints on the specific memory addresses that track lock states. When a thread attempts to acquire a lock, the hardware monitors the transaction. If we observe a pattern where Thread A writes to a lock variable for Resource 1 while its execution context is already flagged as waiting for Resource 2, we have detected a circular dependency.

Configuring Multi-Stage Watchpoints

To catch this, we configure the Data Watchpoint and Trace (DWT) unit to trigger on specific data patterns. We treat the watchpoints as a multi-stage logic gate:

// Conceptually monitoring lock ownership and request state
DWT->COMP0 = (uint32_t)&lock_resource_1; // Address of resource 1
DWT->MASK0 = 0;                          // Watch all bits
DWT->FUNCTION0 = 0x03;                   // Trigger on Read/Write access

// When hit, the debugger logic checks if the current thread 
// context is already blocking on Resource 2.
if (current_thread_state == WAITING_FOR_R2) {
    HALT_SYSTEM(); // Immediate trap to debugger
}

By chaining these watchpoints, we can enforce a "resource acquisition ordering" rule. If the hardware observes a write operation to a lock that violates the defined hierarchy—for example, attempting to lock a low-level resource while holding a high-level one—the watchpoint unit triggers an immediate halt before the CPU executes the instruction that seals the deadlock. This stops the processor in the exact state where the collision occurred, preserving the register file for inspection of the circular wait condition.

Key Takeaway: By using hardware watchpoints to monitor lock acquisition patterns against the current thread’s wait-state, you can trap circular resource dependencies the instant they materialize, enabling deterministic debugging of complex deadlocks.