Dynamic debug is a powerful debugging feature in the Linux kernel that allows you to enable and disable debugging messages at runtime without the need to recompile the kernel or reboot the system.
You can use dynamic debugging in several situations, such as:
Troubleshooting kernel issues
Developing drivers for new hardware
Tracing and auditing security events
Certain benefits of dynamic debugging are listed below:
Dynamic debugging enables debugging messages without requiring a system reboot. This real-time capability is crucial for diagnosing issues in production environments.
You can enable debugging messages for specific parts of the kernel or even individual modules, allowing you to focus on relevant information.
Use dynamic debugging to monitor and optimize kernel performance by selectively enabling or disabling debugging messages based on the current analysis requirements.
For supported kernel versions that are installed by default, dynamic debug is already built-in. To check the status of dynamic debug, run the following command as the root user:
#
zcat /proc/config.gz | grep CONFIG_DYNAMIC_DEBUG
If dynamic debug is compiled into the kernel, you should see an output similar to the following:
CONFIG_DYNAMIC_DEBUG=y CONFIG_DYNAMIC_DEBUG_CORE=y
To enable specific debug messages or logs within the running kernel, you
can use the echo
command and write to the
/sys/kernel/debug/dynamic_debug/control
file.
The following examples illustrate certain simple uses of dynamic debug:
Dynamic debug relies on specific debugging macros, such as
pr_debug
, embedded in the kernel code. These
macros are used by kernel developers to insert debugging messages into
the code.
The examples in this section assume that the
pr_debug
macro works correctly because dynamic
debug is allowed for the running kernel.
To enable the debug messages for a specific kernel source code file, use the following example:
#
echo "file FILE_NAME.c +p" > /sys/kernel/debug/dynamic_debug/control
To enable debug messages for a specific kernel module, use the following example:
#
echo "module MODULE_NAME +p" > /sys/kernel/debug/dynamic_debug/control
To disable previously enabled debugging messages for a specific
kernel source code file or a kernel module, run the
echo
command with the -p
option. For example:
#
echo "file FILE_NAME.c -p" > /sys/kernel/debug/dynamic_debug/control
#
echo "module MODULE_NAME -p" > /sys/kernel/debug/dynamic_debug/control
For detailed information about dynamic debug and its use cases, refer to its official documentation.
You can view the dynamic debug messages that were generated based on the
configurations you enabled, by running dmesg
and
filtering the output with grep
. For example:
#
dmesg | grep -i "FILE_NAME.c"
Optionally, to continuously monitor the system messages as they are
generated, you can use the tail
command with the
-f
option:
#
tail -f /var/log/messages