Fundamentals 9 min read

Master Linux Kernel Debugging with dev_dbg and Dynamic Debug

This guide explains why kernel developers should replace printk with dev_info, dev_dbg, and dev_err, shows how to enable and use CONFIG_DYNAMIC_DEBUG and debugfs to control dev_dbg output at runtime, and provides code examples and a detailed analysis of the underlying implementation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Kernel Debugging with dev_dbg and Dynamic Debug

In modern kernel driver code, direct use of printk is discouraged; developers should use the dev_info, dev_dbg, and dev_err helpers, which still rely on printk but add module and device context and support dynamic debugging.

They can print module and device information.

They integrate with the kernel's dynamic debug facility.

The typical usage patterns are: dev_info() – one‑time informational messages such as during probe. dev_dbg() – debugging messages for ordinary errors (e.g., -EINVAL, -ENOMEM). dev_err() – serious errors where the user cannot obtain an errno or the cause is hard to guess.

Dynamic Debug Usage

Enable the kernel options CONFIG_DYNAMIC_DEBUG and CONFIG_DEBUG_FS (e.g., via make menuconfig).

After boot, mount the debugfs filesystem:

mkdir /mnt/dbg
mount -t debugfs none /mnt/dbg

Control output of specific dev_dbg() calls by writing to /mnt/dbg/dynamic_debug/control:

Enable all dev_dbg() in a file: echo -n "file xxx.c +p" > /mnt/dbg/dynamic_debug/control Enable all dev_dbg() in a function: echo -n "func foo +p" > /mnt/dbg/dynamic_debug/control Disable the same with -p instead of +p.

Run the program and view messages with dmesg.

Example Commands

echo -n "file ca_dsc_core.c +p" > /mnt/dbg/dynamic_debug/control   # enable all dev_dbg in ca_dsc_core.c
echo -n "func ca_dsc_read +p" > /mnt/dbg/dynamic_debug/control   # enable dev_dbg in ca_dsc_read()

How Dynamic Debug Works

When CONFIG_DYNAMIC_DEBUG is enabled, the compiler records every dev_dbg() call in a table. The table can be inspected via /mnt/dbg/dynamic_debug/control:

# cat /mnt/dbg/dynamic_debug/control
... (output truncated) ...
drivers/alidrivers/modules/alidsc/ca_dsc_core.c:800 [alidsc]ca_dsc_probe_dt =_ "get dev-index error12"
...

Lines ending with =_ are disabled, while those with =p are enabled and will produce output.

dev_dbg() is especially useful for tracing kernel subsystems; for example, enabling dynamic debug for net/ipv4/ping.c lets you observe the ping implementation.

Code Analysis

The relevant source files are include/linux/device.h, include/linux/dynamic_debug.h, and lib/dynamic_debug.c. The macro definitions are:

#if defined(CONFIG_DYNAMIC_DEBUG)
#define dev_dbg(dev, format, ...) do { 
    dynamic_dev_dbg(dev, format, ##__VA_ARGS__); 
} while (0)
#elif defined(DEBUG)
#define dev_dbg(dev, format, arg...) dev_printk(KERN_DEBUG, dev, format, ##arg)
#else
#define dev_dbg(dev, format, arg...) ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
#endif

The dynamic_dev_dbg() macro expands to:

#define dynamic_dev_dbg(dev, fmt, ...) do { 
    DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); 
    if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT)) 
        __dynamic_dev_dbg(&descriptor, dev, fmt, ##__VA_ARGS__); 
} while (0)

It creates a descriptor for each call; the _DPRINTK_FLAGS_PRINT flag is toggled via the debugfs control file, allowing runtime enabling/disabling of individual debug statements.

Benefits

Development builds: Enable CONFIG_DYNAMIC_DEBUG and CONFIG_DEBUG_FS to dynamically observe and debug kernel code without recompiling.

Production builds: Disable those options; all debugfs entries and dev_dbg calls are stripped at compile time, eliminating overhead.

dev_dbgdynamic debugdriver-development
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.