linux kernel printk log(以及dev_dbg)

 

The smaller the num value, the higher the log priority.

 

 

1. view loglevel

# cat /proc/sys/kernel/printk

9    4    1    7

 

9: console_loglevel, messages with a priority higher than this value are printed to the console

4: default_message_loglevel, default priority if printk without priority, for example: printk("hello")

1: minimum_console_loglevel, The minimum value at which the console log level can be set 

7: default_console_loglevel, default console log level

 

linux/kernel/printk.c

int console_printk[4] = {

        CONSOLE_LOGLEVEL_DEFAULT,       /* console_loglevel */

        MESSAGE_LOGLEVEL_DEFAULT,       /* default_message_loglevel */

        CONSOLE_LOGLEVEL_MIN,           /* minimum_console_loglevel */

        CONSOLE_LOGLEVEL_DEFAULT,       /* default_console_loglevel */

};

 

all kernel log will be printed to console since  console_loglevel is 9(>7).

Note, kernel will print all log which loglevel highter than console_loglevel, except dev_dbg. if we want to enable dev_dbg, make sure to definite DEBUG micro in corresponding driver module. so, every driver module can enable dev_dbg respectively.

 

 

2. modify loglevel

2.1 dts bootargs

arch/arm64/boot/dts/megvii/mc40_haps.dts

loglevel = 9

set console_loglevel(console_printk[0]) as 9

 

console_loglevel(console_printk[0]) will be equivalent to CONSOLE_LOGLEVEL_DEFAULT(default_console_loglevel) if there is no loglevel in bootargs.

like this:

# cat /proc/sys/kernel/printk

7    4    1    7

 

2.2 echo x > /proc/sys/kernel/printk

disable dev_dbg:

# echo 7 > /proc/sys/kernel/printk

# cat /proc/sys/kernel/printk

7    4    1    7

 

enable all log(include dev_dbg): 

# echo 8 > /proc/sys/kernel/printk

# cat /proc/sys/kernel/printk

8    4    1    7

 

support this:

# echo 7 3 2 6 > /proc/sys/kernel/printk

# cat /proc/sys/kernel/printk

7    3    2    6

 

2.3 kernel config

 

1) Default console loglevel (1-15)

CONSOLE_LOGLEVEL_DEFAULT

Default loglevel to determine what will be printed on the console.                                                                        

Setting a default here is equivalent to passing in loglevel=<x> in                                           

the kernel bootargs. loglevel=<x> continues to override whatever                                          

value is specified here as well.

 

2) Default message log level (1-7)

MESSAGE_LOGLEVEL_DEFAULT

Default log level for printk statements with no specified priority.                                           

This was hard-coded to KERN_WARNING since at least 2.6.10 but folks                                            

that are auditing their logs closely may want to set it to a lower priority.

 

2.4 a bad method

modify  DEFAULT_CONSOLE_LOGLEVEL in kernel/printk/printk.c

 

 

3. dev_dbg

3.1 definition

include/linux/device.h

#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

 

1) if CONFIG_DYNAMIC_DEBUG

see chapter 4.

2) if DEBUG

enable dev_dbg

3) else

disable dev_dbg

 

3.2 enable dev_dbg info

1) add micro DEBUG in driver module

add EXTRA_CFLAGS += -DDEBUG in Makefile of driver module

for example: enable dev_dbg in usb host driver

add add EXTRA_CFLAGS += -DDEBUG in drivers/usb/host/Makefile

 

Note, if we need to enable dev_dbg for one or several special file, we can add  #define DEBUG 1 at  start of file.

 

2) modify loglevel to print debug info

if we want print debug info on kernel booting phase: modify dts bootargs or kernel config

  • loglevel = 8 in bootargs

  • set "Default console loglevel" to 8 in kernel config

if not, modify /proc/sys/kernel/printk

  • echo 8 > /proc/sys/kernel/printk

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello-linux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值