enable linux kernel driver dev_dbg debug messages

48,113

Solution 1

the simplest way to receive dev_dbg messages without installing/configuring syslog/etc, appeared necessary to do following steps:

  1. provide debug key into bootargs kernel parameters

  2. append #define DEBUG at the first line of the driver file - if the driver is a single file and is using a common Makefile, or append -DDEBUG inside the CC build options if the driver contains of multiple source files and as usually has it's own Makefile

  3. upon the kernel booted and the prompt appear to enable debug level messages by executing either dmesg -n 8 or echo 8 > /proc/sys/kernel/printk

  4. load the driver if the module with the command either insmod <driver name> or modprobe <driver name> or if the driver is integrated into the kernel the insertion commands may vary.

example on how to assign the kernel integrated driver for the i2c bus subsystem:

echo <driver name> <i2c bus address> > /sys/bus/i2c/devices/i2c-0/new_device

side notes:

if the DTS will have a driver record assignment, manually repeated driver assignment will cause the error - in case of i2c subsystem - error EBUSY (-16), the driver will be assigned way before the command prompt and the dmesg messages will be limited to the default level (usually dev_info only)

in case if the driver has been already assigned by DTS and there is no way to exclude it temporary from the tree source - it's useful to detach and reattach it once again after the debug (trace) level messages activated

for the i2c subsystem it would require to execute a command:

echo <driver name> > /sys/bus/i2c/drivers/<drivername>/unbind

then

echo <driver name> > /sys/bus/i2c/drivers/<drivername>/bind

warning:

the kernel drivers trace mechanism will not help on debugging internal driver improper configured or missing service structures. i.e. if the driver is loaded but remain silent with no trace messages means the probe has never been executed because of some kernel expected service structures information were missing or faulty

Solution 2

You need to follow below three steps.

1. Make sure that your kernel is complied with CONFIG_DYNAMIC_DEBUG=y

cat /proc/config.gz | gunzip | grep CONFIG_DYNAMIC_DEBUG

If not then recompile your kernel with CONFIG_DYNAMIC_DEBUG=y

2) After boot up check that debugfs is mounted somewhere or not.

mount | grep debugfs

mostly it get mounted in /sys/kernel/debug if not then you can manually mount it anywhere like below

mount -t debugfs none /sys/kernel/debug

3) Now enable the file name for which you need dev_dbg() logs.

echo 'file <driverfilename.c> +p'>/sys/kernel/debug/dynamic_debug/control

some more commands to play with dynamic_debug/control are at https://www.kernel.org/doc/html/v4.11/admin-guide/dynamic-debug-howto.html

now you should get your debug.

If you still do not see your message then enable prink level

echo "8    4    1    7" > /proc/sys/kernel/printk

If the CONFIG_DYNAMIC_DEBUG option is not set, then dev_dbg/pr_debug can be turned into normal printk() statements with KERN_DEBUG level.

But for that you need to add #define DEBUG at beginning of file. or add -DDEBUG while compiling or Enable CONFIG_[SUBSYSTEM]_DEBUG=y

Solution 3

I used below command to turn on the Kernel log for Android. I think for Linux to it should work:

echo 'file <driverfilename.c> +p'>/sys/kernel/debug/dynamic_debug/control

You can see the driver logs in "dmesg"

Thanks MJ

Solution 4

Adding "debug" to the kernel parameters will lift the kernel events log level to the KERN_DEBUG (level 7) that will pollute the kmsg buffer with debug messages for the entire kernel code and it's not usually what we want while debugging our kernel module. The same is right when we change the current kernel log level via /proc/sys/kernel/printk.

To gain fine-grained control of all log levels above the KERN_ERR for your kernel module only without messing with global kernel log level configuration just add the following lines at the beginning of your module code:

#undef dev_dbg
#undef dev_info
#undef dev_warn
#undef dev_notice

#define dev_dbg dev_err
#define dev_info dev_err
#define dev_warn dev_err
#define dev_notice dev_err

Solution 5

You can mount the debug filesystem manually by executing the following command:

mount -t debugfs none /sys/kernel/debug

Once done, do:

ls /sys/kernel/debug

All debug information and message would be stored there without having to re-compile.

Also, you can add the this under your /etc/fstab to make the mount go automatic when you reboot.

... If you are re-compiling your kernel anyway, then you could enable " [*] Debug File System" under "Kernel Hacking"

Good luck, I hope all goes well.

Share:
48,113

Related videos on Youtube

Oleg Kokorin
Author by

Oleg Kokorin

Role: Embedded/mobile/IoT systems hardware and software design architect, experienced in debugging/porting/integration, cross-environment distributed team management. Technology: Languages: C/C++, ARM/NXP/TI DSP Assemblies, gas, csh, ksh, zsh, tcl/tk, python3, XML, JSON, MongoDB, UML, SysML, MISRA, REXX, Forth, VHDL. Tools: CLion, sublime, XpressoIDE, OpenOCD, Xilinx ISE/Vivado, iMpact, xc3sprog, XCode, avrdude, Eclipse, GIT, GDB, STL, Trace32, SystemView, Philips NMDK, RealView, TI Code Composer, Rational Rose, ClearCase, Qt/Qtopia, EMP SDK, LWIP, CURL, Zlib, Gerrit, Mercurial, CHIRP, bitbake, repo, git, jira, vim, emacs, gdb. OS: Ubuntu/Fedora, ThreadX TrustZone, MacOSX, homebrew, Parallels, Android, Symbian S60(3rd/2nd ed)/UIQ, Embedded Linux, V4L2, Nucleus, VxWorks, Obigo Framework, EMP, Palm OS, OpenWRT, Yocto, Buildroot, LEDE, Angstrom, ConfD RPC, DPDK, MQTT. HW: Xilinx Zynq/Artix/Kintex/Spartan/Virtex/CPLD, STMicro Nomadik 88xx, Silabs SC47xx/SC49xx, NXP iMX/iMXRT/JN516x, TI OMAP, Intel PXA25x-26x, Samsung SGH, Arduino, Raspberry PI, BeagleBone Black, Parallella, Enclustra, XDS510PP plus, Lauterbach LA-7705 JTAGs, logical analyzers, simulators, USB, UART, Bluetooth, accelerometer, gyroscope, thermometer, Hayes, edma, adma, sai, spi, 1 wire, i2s, i2c, MDIO, SDIO, GPIO, PWM, RTC, DSI, CSI, LVDS, SATA, SCSI, 1394/FireWire, PCMCIA, ExpressCard, USB, JTAG, CAN, AUTOSAR, ECU, 10G NIC.

Updated on January 30, 2022

Comments

  • Oleg Kokorin
    Oleg Kokorin over 2 years

    is there a simplest possible way to enable linux kernel driver dev_dbg debug messages (actually it's a trace style messages) hopefully without messing up with the kernel patching/recompiling or the driver implementing something extra like debugfs? perhaps there is a way to enable something SIMPLE in the kernel (like one flag?) triggering particular driver or all drivers dev_dbg (it can be filtered with the `dmesg|grep "driverName") output?

    the kernel version is 4.14. there is NO syslog/daemonlog/system log running at all. there is NO network interface and only single serial port is available. the target system is very slow and is very compact so there is NO WAY to add syslog/etc, there is nothing but dmesg where exactly would be good to see the output of the lines like:

    dev_dbg(&client->dev, "bla bla bla\n");

    some posts already suggested to add debug keyword for the bootargs kernel parameters unfortunately wasn't enough.

    the outputs like dev_info are getting into the dmesg with no issue so it's definitely something close. thanks

  • Oleg Kokorin
    Oleg Kokorin over 5 years
    "without messing up with the kernel patching/recompiling or the driver implementing something extra like debugfs" was the original requirement
  • Étienne
    Étienne over 4 years
    Just for reference, because this answer is one of the first Google hits for "enable dev_dbg", the messages then have to be enabled in debug/dynamic/control (kernel.org/doc/html/v4.15/admin-guide/dynamic-debug-howto.h‌​tml).
  • Carlo Wood
    Carlo Wood about 4 years
  • Oleg Kokorin
    Oleg Kokorin over 2 years
    "without messing up with the kernel patching/recompiling" detail was not for you, I suppose?
  • Filip Kubicz
    Filip Kubicz over 2 years
    Hi, thanks for the comment. Some other answers also require rebuilding the kernel, or modifying code of a driver and rebuilding it. I just shared what worked for me, and is likely to be easy to use for other Buildroot/Yocto users.
  • Filip Kubicz
    Filip Kubicz over 2 years
    I added the answer not because your answer was wrong; rather to give another option for the people who come here from the search engine.