zoukankan      html  css  js  c++  java
  • 内核printk打印等级

    为了确认内核打印等级以及prink 参数对打印的分级,在led驱动初始化代码【以及exit出口】加入如下代码。

    每次insmod 、rmmod led模块时,根据打印等级的设置,得到不同的打印结果:

    1. static int __init s3c24xx_leds_init()
    2. {
    3.     int ret ;
    4.     int minor = 0 ;
    5.  
    6.    printk(KERN_EMERG " 1111111 KERN_EMERG ");
    7.    printk(KERN_ALERT " 2222 KERN_ALERT ");
    8.    printk(KERN_CRIT " 3333333 KERN_CRIT ");
    9.    printk(KERN_ERR " 44444444 KERN_ERR ");
    10.    printk(KERN_WARNING " 55555 KERN_WARNING ");
    11.    printk(KERN_NOTICE " 66666 KERN_NOTICE ");
    12.    printk(KERN_INFO " 77777 KERN_INFO ");
    13.    printk(KERN_DEBUG " 888888 KERN_DEBUG ");
    14.    printk(" 9999 no_fix ");
    15. }

    操作:

    一次性地设置编译内核源码时默认的打印级数:【它们在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

    1. /* printk's without a loglevel use this.. */
    2. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
    3.  
    4. /* We show everything that is MORE important than this.. */
    5. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
    6. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
    7.  
    8. DECLARE_WAIT_QUEUE_HEAD(log_wait);
    9.  
    10. int console_printk[4] = {
    11.    DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
    12.    DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
    13.    MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
    14.    DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
    15. };

    Include/linux/kernel.h

    1. #define KERN_EMERG "<0>" /* system is unusable */
    2. #define KERN_ALERT "<1>" /* action must be taken immediately */
    3. #define KERN_CRIT "<2>" /* critical conditions */
    4. #define KERN_ERR "<3>" /* error conditions */
    5. #define KERN_WARNING "<4>" /* warning conditions */
    6. #define KERN_NOTICE "<5>" /* normal but significant condition */
    7. #define KERN_INFO "<6>" /* informational */
    8. #define KERN_DEBUG "<7>" /* debug-level messages */
  • 相关阅读:
    软件公司项目经理岗位职责
    指针和链表
    数据结构
    五子棋
    AtCoder Grand Contest 031 B
    两道dp
    博客搬迁
    [Codeforces Round #526 (Div. 2)]
    [Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition]
    [codeforces Mail.Ru Cup 2018 Round 3][B Divide Candies ][思维+数学]
  • 原文地址:https://www.cnblogs.com/mylinux/p/4028787.html
Copyright © 2011-2022 走看看