In this lesson, you will learn how to surgically enable and disable kernel debug messages at runtime for specific drivers or modules without needing to recompile your kernel.
The Problem with Static Debugging
In high-density containerized environments, traditional kernel logging (printk) is often either too noisy—flooding your buffers—or insufficient because the specific events you care about are compiled out of the kernel binary. Dynamic Debugging (dyndbg) solves this by providing a mechanism to toggle pr_debug() and dev_dbg() calls on or off while the system is actively running.
Using the dynamic_debug Control File
The kernel exposes the dyndbg interface through /sys/kernel/debug/dynamic_debug/control. This file acts as a gatekeeper for every debug instrumentation point built into your kernel.
To use it, you first need to ensure debugfs is mounted. You can then query and update these points using simple filter strings. For example, if you are debugging a network driver (like virtio_net) and want to see debug messages triggered specifically within virtio_net.c, you can issue the following command:
# Enable all debug messages in a specific file
echo 'file virtio_net.c +p' > /sys/kernel/debug/dynamic_debug/control
# Disable them once you've captured your logs
echo 'file virtio_net.c -p' > /sys/kernel/debug/dynamic_debug/control
Granular Filtering
Beyond filtering by filename, you can filter by function or even specific line numbers. If you suspect a race condition in a specific function within a namespace-heavy module, you can narrow your output significantly:
# Enable debug logs only for a specific function
echo 'func veth_xmit +p' > /sys/kernel/debug/dynamic_debug/control
# Enable logs for a specific module
echo 'module br_netfilter +p' > /sys/kernel/debug/dynamic_debug/control
The +p flag tells the kernel to print these messages to the dmesg buffer. By toggling these flags dynamically, you minimize the performance overhead on your containers, ensuring that logging only consumes CPU cycles and I/O bandwidth during the exact window of your investigation.
Key Takeaway: Dynamic Debugging allows you to selectively activate or silence kernel trace points at runtime via the dynamic_debug/control interface, providing a lightweight alternative to global logging that is ideal for troubleshooting isolated container networking or driver issues.