so2学习日志2-内核api


加载/卸载模块

在虚拟机中可以在kernel_modules下的1-2-test文件夹下尝试

1
2
insmod module.ko
rmmod module.ko

内核oops

内核级错误报告,遇到无法安全处理的操作(空指针引用,非法访问内存,堆栈溢出)时触发,但不会导致内核崩溃,oops记录了错误发生时的系统状态
oops实例分析

内核日志打印
dmesg -c 可以显示并清除当前日志信息
dmesg | tail -10 显示最后10行

kernel_api

控制台日志
0-7对应严重等级

1
2
3
#include<linux/kernel.h>
pr_debug("Hello!\n");
printk(KERN_INFO "Hello2!\n");
1
2
cat /proc/sys/kernel/printk
echo 6 > /proc/sys/kernel/printk

内存分配

1
2
3
4
5
6
#include <linux/slab.h>
string = kmalloc(string_len+1,GFP_KERNEL);
if(!string){
//errer
}
kfree(string);