Last edited one month ago

How to use the kernel dynamic debug

Template:ArticleMainWriter

Template:ArticleApprovedVersion


1. Introduction[edit | edit source]

As prerequisite to reading this article, please refer to the Dmesg and Linux kernel log page.

"Dynamic debug is designed to allow you to dynamically enable/disable kernel code to obtain additional kernel information. Currently, if CONFIG_DYNAMIC_DEBUG is set, all Template:Highlight calls can be dynamically enabled per-callsite." extracted from the Linux kernel documentation[1].

The related debugfs entry is usually:

/sys/kernel/debug/Template:Highlight

Note that the verbose Template:Highlight calls cannot be dynamically activated.

When the dynamic debug traces are activated, the trace results are printed in dmesg (or /proc/kmsg), and in the console if console loglevel is set to 8.

2. More technical information[edit | edit source]

The dynamic debug trace configuration is done through a control file in the debugfs filesystem: Template:OrangeTemplate:Highlight

The command includes keywords and flag elements (for details see the Linux kernel documentation[1]).

  • Keywords

Possible keywords are:

func : function name
Template:Green : source filename
Template:Blue : module name
format : format string
Template:Orange : line number (including ranges of line numbers)
The colored keywords above are illustrated by examples in the next chapter.
  • Flags

The flag specification comprises a change operation followed by one or more flag characters. The change operation is one of the characters:

- : remove the given flags
+ : add the given flags
= : set the flags to the given flags

Possible flags are:

f : Include the function name in the printed message
l : Include line number in the printed message
m : Include module name in the printed message
p : Causes a printk() message to be emitted to dmesg
t : Include thread ID in messages not generated from interrupt context

3. Examples[edit | edit source]

  • Track all dev_*dbg/pr_debug() in a Template:Green (you can add several files if necessary):
Template:Board$ mount -t debugfs none /sys/kernel/debug
Template:Board$ echo "Template:Green stm32-adc.c +p" > /sys/kernel/debug/dynamic_debug/control

Note that just the file name or full file path can be given, here stm32-adc.c or drivers/iio/adc/stm32-adc.c

  • Track only one Template:Orange with dev_dbg() in a Template:Green (you can add several files and several lines if necessary, please use the last line number of the function call):
Template:Board$ echo "Template:Green stm32-adc.c Template:Orange 1438 +p" > /sys/kernel/debug/dynamic_debug/control
  • For an entire "Template:Blue (module means ~.ko, so not applicable for a statically linked driver)":
Template:Board$ echo "Template:Blue cfg80211 +p" > /sys/kernel/debug/dynamic_debug/control
Template:Board$ cat /sys/kernel/debug/dynamic_debug/control Template:Grey
Template:Board$ cat /sys/kernel/debug/dynamic_debug/control | grep adc
Template:Green:Template:Orange [stm32_adc]stm32_adc_conf_scan_seq =p "%s chan %d to %s%d\012"
Template:Green:Template:Orange [stm32_adc]stm32_adc_awd_set =p "%s chan%d htr:%d ltr:%d\012"
Template:Green:Template:Orange [stm32_adc]stm32_adc_dma_start =p "%s size=%d watermark=%d\012"
Template:Green:Template:Orange [stm32_adc]stm32_adc_trigger_handler =p "%s bufi=%d\012"
Template:Green:Template:Orange [stm32_adc]stm32_adc_chan_of_init =p "Configured to use injected\012"
Template:Green:Template:Orange [stm32_adc]stm32_adc_of_get_resolution =p "Using %u bits resolution\012"
  • Multiple commands can be written together, separated by ';' or '\n'.
Template:Board$ echo "Template:Green stm32-adc.c +p; Template:Green stm32-adc-core.c +p" > /sys/kernel/debug/dynamic_debug/control
  • A another method is to use a wildcard. The match rule supports * (matches zero or more characters) and ? (matches exactly one character). For example, you can match all USB drivers:
Template:Board$ echo "Template:Green drivers/usb/* +p" > /sys/kernel/debug/dynamic_debug/control

4. Synchronous tracing on the console[edit | edit source]

In the case of a crash, or impossibility to call dmesg, it is sometimes useful to have traces synchronously emitted on the console.
Only error, warning and informational traces are emitted synchronously on the console (that is, loglevel=5), so if you need to see the lower level traces too, you need to change the console loglevel to "8".

<enable the conditional traces>
Template:Board$ echo 8 > /proc/sys/kernel/printk
or
Template:Board$ dmesg -n 8
or
Template:Board$ dmesg -n debug

Please follow this article to get a serial console for the target: How to get Terminal Template:Warning

5. Debug messages during boot process[edit | edit source]

In order to activate debug messages during the boot process, even before userspace and debugfs exist, use the kernel's command-line parameter: Template:Highlight

For instance, the kernel bootargs can be modified in the following ways:

  • Mount a boot partition from the Linux kernel console, and then update the extlinux.conf file using the vi editor (see man page[2], or introduction page[3] ). For example:
Template:Board$ mount /dev/mmcblk0p4 /boot
Template:Board$ vi /boot/mmc0_stm32mp157c-ev1_extlinux/extlinux.conf

or

To mount partitions (mmc 0:microSD card / mmc 1: eMMC):

- Press any key to stop at U-Boot execution when booting the board.
Template:Board$ ...
Template:Board$ Hit any key to stop autoboot:  0
Template:Board$ STM32MP>
- Then
STM32MP> ums 0 mmc 0
- Check for the boot partition mounted on your host PC (/media/$USER/bootfs)
- Edit the extlinux file corresponding to your setup (/media/$USER/bootfs/mmc0_stm32mp157c-ev2_extlinux/extlinux.conf
  • Update the kernel command line, adding the dyndbg parameter:
root=/dev/mmcblk0p5 rootwait rw console=ttyS3,115200 dyndbg="Template:Green drivers/usb/core/hub.c +p"

Save and quit file update, and then reboot the board.

Note: to display these debug messages in the console, in addition to the dmesg, add loglevel=8 in the kernel command line.

  • Reboot the board and check for a kernel command-line, and that debug messages are present in the dmesg output

6. References[edit | edit source]


  • Useful external links
Document link Document Type Description
The dynamic debugging interface (lwn.net) User guide http://lwn.net
Documentation/dynamic-debug-howto.txt (lwn.txt) User guide http://lwn.net
Dynamic debug howto (kernel.org) Standard http://www.kernel.org