1. The linux kernel print function printk
- One, the function has a header file
- Two, the key macro definition
- Three, function prototype
- Fourth, the use of functions (written by the upper position)
- Five, compile and download (host computer operation)
- Sixth, modify the Linux printing kernel information environment of the lower computer
- Seven, load module / unload module test
One, the function has a header file
路径:/opt/kernel/include/linux/printk.h
头文件包含:#include <linux/printk>
*注:一般其它头文件已包含了当前头文件,编写代码时可以不用写*
Two, the key macro definition
#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>" //debug消息。
#define KERN_DEFAULT "<d>" //系统内核默认日志级别
Three, function prototype
int printk(const char *fmt, ...);
//功能:内核打印函数,使用方式和printf类似,
// 都是采用可变长参数列表
Fourth, the use of functions (written by the upper position)
New source file: printk_test.c
//vim printk_test.c
include <linux/init.h>
#include <linux/module.h>
static int printk_init(void){
printk("装载模块:%s\n",__func__);
printk(KERN_EMERG "系统崩溃\n");
printk(KERN_ALERT "事件需要处理\n");
printk(KERN_CRIT "严重\n");
printk(KERN_ERR "错误\n");
printk(KERN_WARNING "警告\n");
printk(KERN_NOTICE "正常事件\n");
printk(KERN_INFO "提示信息\n");
printk(KERN_DEBUG "调试\n");
return 0;
}
static void printk_exit(void){
printk("卸载模块:%s\n",__func__);
printk("<0>" "系统崩溃\n");
printk("<1>" "事件需要处理\n");
printk("<2>" "严重\n");
printk("<3>" "错误\n");
printk("<4>" "警告\n");
printk("<5>" "正常事件\n");
printk("<6>" "提示信息\n");
printk("<7>" "调试\n");
}
module_init(printk_init);
module_exit(printk_exit);
MODULE_LICENSE("GPL");
New Makefile
# vim Makefile
obj-m += p_test.o
all:
make -C /opt/kernel SUBDIRS=$(PWD) modules
clean:
make -C /opt/kernel SUBDIRS=$(PWD) clean
# 目录 /opt/kernel 为开发板对应内核源码
Five, compile and download (host computer operation)
make //生成printk_test.ko文件
cp /printk_test.ko /nfs_server/drivers/test //nfs_server为根文件系统目录
Sixth, modify the Linux printing kernel information environment of the lower computer
echo 8 > /proc/sys/kernel/printk //表示打印内核消息级别小于8即:0~7。
It can also be modified in the kernel source code:
// cd /opt/kernrl
// vim kernel/printk.c
#define DEFAULT_CONSOLE_LOGLEVEL 8 /* anything MORE serious than KERN_DEBUG */
// :wq 修改后保存退出
// make uImage 重新编译内核 ,编译完成后,生成文件在:arch/arm/boot/uImage
//烧写uImage到SD卡 / EMMC / 网络挂载点 / ......
Seven, load module / unload module test
/drivers/test # echo 8 > /proc/sys/kernel/printk //以下打印级别为0~7
/drivers/test # insmod printk_test.ko
[ 2284.699000] 装载模块:printk_init
[ 2284.699000] 打印级别:<7>
[ 2284.700000] 系统崩溃
[ 2284.701000] 事件需要处理
[ 2284.703000] 严重
[ 2284.704000] 错误
[ 2284.705000] 警告
[ 2284.706000] 正常事件
[ 2284.707000] 提示信息
[ 2284.711000] 调试
/drivers/test # rmmod p_test.ko
[ 2315.846000] 卸载模块:printk_exit
[ 2315.846000] 系统崩溃
[ 2315.847000] 事件需要处理
[ 2315.848000] 严重
[ 2315.849000] 错误
[ 2315.850000] 警告
[ 2315.851000] 正常事件
[ 2315.852000] 提示信息
[ 2315.853000] 调试
/drivers/test # echo 3 >/proc/sys/kernel/printk //以下打印级别为0~3
/drivers/test # insmod p_test.ko
[ 2336.840000] 系统崩溃
[ 2336.840000] 事件需要处理
[ 2336.840000] 严重
/drivers/test # rmmod p_test.ko
[ 2339.333000] 系统崩溃
[ 2339.333000] 事件需要处理
[ 2339.333000] 严重