Home » Linux Kernel » Core Kernel » How to use Dynamic Debugging using pr_debug in Linux Kernel ?

How to use Dynamic Debugging using pr_debug in Linux Kernel ?

pr_debug is a macro used in the Linux kernel for debugging purposes. It is defined in the header file and is part of the kernel’s logging infrastructure.

The pr_debug macro is used to print debug messages to the kernel log when the kernel is compiled with the CONFIG_DYNAMIC_DEBUG configuration option enabled. This option allows developers to dynamically enable and disable debug messages at runtime without recompiling the kernel.

The syntax for using pr_debug is similar to printk, which is a function used to print messages to the kernel log. Here’s an example:

#include <linux/kernel.h>
#include <linux/module.h>

static int __init my_module_init(void)
{
    pr_debug("Initializing my_module\n");
    /* Other initialization code */

    return 0;
}

static void __exit my_module_exit(void)
{
    pr_debug("Exiting my_module\n");
    /* Other cleanup code */
}

module_init(my_module_init);
module_exit(my_module_exit);

MODULE_LICENSE("GPL");

In the above example, the pr_debug macro is used to print debug messages during the initialization and exit of a kernel module. These messages will only be printed if the CONFIG_DYNAMIC_DEBUG option is enabled and the corresponding debug statement is enabled at runtime.

It’s worth noting that pr_debug is intended for temporary debugging purposes and should be removed or disabled in the final code. Debug statements left enabled in the production kernel can impact performance and consume system resources unnecessarily.

Reference : https://docs.kernel.org/admin-guide/dynamic-debug-howto.html


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment