Where does dev_dbg writes log to?

linux-device-driver

Solution

`dev_dbg()` expands to `dynamic_dev_dbg()`, `dev_printk()`, or no-op depending on the compilation flags.

#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);  \
})
#endif

`dynamic_dev_dbg()` and `dev_printk()` call `dev_printk_emit()` which calls `vprintk_emit()`.

This very same function is called in a normal mode when you just do a `printk()`. Just note here, that the rest functions like `dev_err()` will end up in the same function.

Thus, obviously, the buffer is all the same, i.e. kernel intrenal buffer.

The logged message at the end is printed to

- Current console if kernel loglevel value (can be changed via kernel command line or via procfs) is high enough for certain message, here KERN_DEBUG.

- Internal buffer which can be read by running `dmesg` command.

Note, data in 2 is kept as long as there still room in the buffer. Since it's limited and circular, newer data preempts old one.

Additional information how to enable Dynamic Debug.

First of all, be sure you have `CONFIG_DYNAMIC_DEBUG=y` in the kernel configuration.

Assume we would like to enable all debug prints in the built-in module with name 8250. To achieve that we simple add to the kernel command line the following `8250.dyndbg=+p`.

If the same driver is compiled as loadable module we may either add `options 8250 dyndbg` to the modprobe configuration or to the shell command line when do it manually, like `modprobe 8250 dyndbg`.

More details are described in the Dynamic Debug documentation.

The "How certain debug prints are automatically enabled in linux kernel?" raises the question why some debug prints are automatically enabled and how `DEBUG` affects that when `CONFIG_DYNAMIC_DEBUG=y`. The answer is lying in the dynamic_debug.h and since it's used during compilation the `_DPRINTK_FLAGS_DEFAULT` defines the certain message appearence.

#if defined DEBUG
#define _DPRINTK_FLAGS_DEFAULT _DPRINTK_FLAGS_PRINT
#else
#define _DPRINTK_FLAGS_DEFAULT 0
#endif

Problem

In a device driver source in the Linux tree, I saw `dev_dbg(...)` and `dev_err(...)`, where do I find the logged message? One reference suggest to add `#define DEBUG` . The other reference involves dynamic debug and debugfs, and I got lost.

Original source