为了确认内核打印等级以及prink 参数对打印的分级,在led驱动初始化代码【以及exit出口】加入如下代码。
每次insmod 、rmmod led模块时,根据打印等级的设置,得到不同的打印结果:
-
static int __init s3c24xx_leds_init()
-
{
-
int ret ;
-
int minor = 0 ;
-
-
printk(KERN_EMERG " 1111111 KERN_EMERG ");
-
printk(KERN_ALERT " 2222 KERN_ALERT ");
-
printk(KERN_CRIT " 3333333 KERN_CRIT ");
-
printk(KERN_ERR " 44444444 KERN_ERR ");
-
printk(KERN_WARNING " 55555 KERN_WARNING ");
-
printk(KERN_NOTICE " 66666 KERN_NOTICE ");
-
printk(KERN_INFO " 77777 KERN_INFO ");
-
printk(KERN_DEBUG " 888888 KERN_DEBUG ");
-
printk(" 9999 no_fix ");
-
…
-
}
操作:
一次性地设置编译内核源码时默认的打印级数:【它们在kernel/printk.c中定义】
echo 8 4 1 7 > /proc/sys/kernel/printk
8. 为KERN_DEBUG
# echo 8 4 1 7 > /proc/sys/kernel/printk
# insmod myleds_new.ko
1111111 KERN_EMERG
2222 KERN_ALERT
3333333 KERN_CRIT
44444444 KERN_ERR
55555 KERN_WARNING
66666 KERN_NOTICE
77777 KERN_INFO
888888 KERN_DEBUG
9999 no_fix
7. 为默认
#cat /proc/sys/kernel/printk
#7 4 1 7
打印结果:
111111 KERN_EMERG
2222 KERN_ALERT
3333333 KERN_CRIT
44444444 KERN_ERR
55555 KERN_WARNING
66666 KERN_NOTICE
77777 KERN_INFO
9999 no_fix
6.
# echo 6 4 1 7 > /proc/sys/kernel/printk
# insmod myleds_new.ko
1111111 KERN_EMERG
2222 KERN_ALERT
3333333 KERN_CRIT
44444444 KERN_ERR
55555 KERN_WARNING
66666 KERN_NOTICE
9999 no_fix
5.
# echo 5 1 4 7 > /proc/sys/kernel/printk
# rmmod myleds_new.ko
1111111 KERN_EMERG
2222 KERN_ALERT
3333333 KERN_CRIT
44444444 KERN_ERR
55555 KERN_WARNING
9999 no_fix
4.
# echo 4 4 1 7 > /proc/sys/kernel/printk
# insmod myleds_new.ko
1111111 KERN_EMERG
2222 KERN_ALERT
3333333 KERN_CRIT
44444444 KERN_ERR
3.
# echo 3 4 1 7 > /proc/sys/kernel/printk
# rmmod myleds_new.ko
1111111 KERN_EMERG
2222 KERN_ALERT
3333333 KERN_CRIT
2.
# echo 2 4 1 7 > /proc/sys/kernel/printk
# insmod myleds_new.ko
1111111 KERN_EMERG
2222 KERN_ALERT
1.
# echo 1 4 1 7 > /proc/sys/kernel/printk
# insmod myleds_new.ko
1111111 KERN_EMERG
分析:
手动设置打印等级为5 1 4 7 及其以上时(重启会失效),会打印KERN_WARNING 以及没有加格式参数的printk打印信息。
KERN_DEBUG优先级最低。
KERN_EMERG优先级最高。
其它的,按级数递推。
假设要默认打印无fix的printk,
kernel/printk.c
#define DEFAULT_CONSOLE_LOGLEVEL (XX); xx >=5
假设要默认打印KERN_DEBUG信息
#define DEFAULT_CONSOLE_LOGLEVEL (XX); xx=8
//上述操作,一次性的可以使用echo xx 4 1 7 > /proc/sys/kernel/printk
源代码参考:
kernel/printk.c
-
/* printk's without a loglevel use this.. */
-
#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
-
-
/* We show everything that is MORE important than this.. */
-
#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
-
#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
-
-
DECLARE_WAIT_QUEUE_HEAD(log_wait);
-
-
int console_printk[4] = {
-
DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
-
DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
-
MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
-
DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
-
};
-
…
Include/linux/kernel.h
-
#define KERN_EMERG "<0>" /* system is unusable */
-
#define KERN_ALERT "<1>" /* action must be taken immediately */
-
#define KERN_CRIT "<2>" /* critical conditions */
-
#define KERN_ERR "<3>" /* error conditions */
-
#define KERN_WARNING "<4>" /* warning conditions */
-
#define KERN_NOTICE "<5>" /* normal but significant condition */
-
#define KERN_INFO "<6>" /* informational */
-
#define KERN_DEBUG "<7>" /* debug-level messages */