现在的位置: 首页 > 综合 > 正文

linux printk函数

2018年04月17日 ⁄ 综合 ⁄ 共 2178字 ⁄ 字号 评论关闭

1、printk
l
evel的打印级别

在printk.h中定义了8个级别的log level,

#define KERN_EMERG "<0>"
#define KERN_ALERT "<1>"
#define KERN_CRIT "<2>"
#define KERN_ERR "<3>"
#define KERN_WARNING "<4>"
#define KERN_NOTICE "<5>"

#define KERN_INFO "<6>"

#define KERN_DEBUG "<7>"


内核中关于printk level的定义:在kernel/printk文件

/* 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 */

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 */默认终端级别

};

查看printk打印级别的方法:

     sh-# cat /proc/sys/kernel/printk
     7       4       1       7

设定printk level的两种办法:
     (1)使用dmesg命令进行设定,
     sh-# cat /proc/sys/kernel/printk
     0       4       1       7
     sh-# dmesg -n 7
     sh-# cat /proc/sys/kernel/printk
     7       4       1       7
     (2)直接更改printk配置文件, 
     sh-# echo 1 > /proc/sys/kernel/printk
     sh-# cat /proc/sys/kernel/printk
     1       4       1       7

2、在写驱动时,需要打印出驱动相关的调试信息:

内核在IIC驱动(
./drivers/i2c/ )的makefile有如下代码:
ifeq ($(CONFIG_I2C_DEBUG_CORE),y)
EXTRA_CFLAGS += -DDEBUG
endif

Kconfig里面当然也有对应的:
config I2C_DEBUG_CORE bool "I2C Core debugging messages"
    help Say Y here if you want the I2C core to produce a bunch of debug
      messages to the system log.  Select this if you are having a
      problem with I2C support and want to see more of what is going on.

在配置内核时, 设置IIC对应的debug项目:
Device Drivers --->
    <*> I2C support -->
        <*> I2C Core debugging messages
        <*> I2C Algorithm debugging messages
        
<*> I2C Bus debugging messages
        
<*> I2C Chip debugging messages

重新编译内核然. 烧写启动后更该printk的缺省值:
#echo 8 > /proc/sys/kernel/printk
后运行 ./i2c_test, 会看见如下的调试信息:
注: IIC测试程序代码:http://blog.csdn.net/hongtao_liu/article/details/4964244

i2c-adapter i2c-0: ioctl, cmd=0x702, arg=0x01

i2c-adapter i2c-0: ioctl, cmd=0x701, arg=0x02

i2c-adapter i2c-0: ioctl, cmd=0x707, arg=0xbeb95d28

i2c-adapter i2c-0: master_xfer[0] W, addr=0x50, len=2

s3c2440-i2c s3c2440-i2c: START: 000000d0 to IICSTAT, a0 to DS

s3c2440-i2c s3c2440-i2c: iiccon, 000000e0

s3c2440-i2c s3c2440-i2c: STOP

抱歉!评论已关闭.