基于pstore和 ramoops实现在非易失性内存中保存panic日志
1. kernel配置
在menuconfig
中选择内核pstore
模块,build-in进内核
$ make menuconfig
|-> File systems
|-> Miscellaneous filesystems
|-> Persistent store support
|-> Log kernel console messages # console 前端
|-> Log user space messages # pmsg 前端
|-> Persistent function tracer # ftrace 前端
|-> Log panic/oops to a RAM buffer # pstore/ram 后端
|-> Log panic/oops to a block device # pstore/blk 后端
2. 参数设定--确定地址范围
设定用来保存panic日志的非易失性内存的起始地址和长度。
比如笔者用到的那部分内存的起始地址是0x9C000000,长度是16M。
有三种参数设定方式,这里我采用的是设备数传参的方式:
详细的三种参数设定方式说明可以看源码上的文档
- Documentation/admin-guide/ramoops.rst
- Documentation/admin-guide/pstore-blk.rst
- Documentation/devicetree/bindings/reserved-memory/ramoops.txt(解释record-size含义)
For example::
reserved-memory {
#address-cells = <2>;
#size-cells = <2>;
ranges;
ramoops@9C000000 {
compatible = "ramoops";
reg = <0 0x9C000000 0 0x100000>;
record-size = <0x4000>;
console-size = <0x4000>;
};
};
3. 挂载
在正确配置设备后,启动的时候应该会有这样的日志:
[ 0.225867] pstore: using zlib compression
[ 0.225882] console [pstore-1] enabled
[ 0.225905] pstore: Registered ramoops as persistent store backend
[ 0.225909] ramoops: attached 0x100000@0x9c000000, ecc: 0/0
这代表pstore
找到了设备且正常注册。
接下来,我们还需要通过挂载的形式触发pstore
从设备读取数据。
常见的挂载是这样的:
mount -t pstore pstore /sys/fs/pstore
挂载后,通过mount命令可以看到类似这样的信息:
console:/sys/fs/pstore # mount
pstore on /sys/fs/pstore type pstore (rw,seclabel,relatime)
如果曾经触发过崩溃日志,在挂载点应该有类似这样的文件:
console:/sys/fs/pstore # ls -al
total 0
drwxr-x--- 2 root root 0 2020-06-01 00:06 .
drwxr-xr-x 7 root root 0 2016-09-26 23:23 ..
-r--r--r-- 1 root root 8674 2020-06-01 00:06 console-ramoops-0
-r--r--r-- 1 root root 27302 2020-06-01 00:01 dmesg-ramoops-0
-r--r--r-- 1 root root 27298 2020-06-01 00:01 dmesg-ramoops-1
4. 验证
可以通过以下命令,手动触发系统奔溃重启
# echo c > /proc/sysrq-trigger
参考文章:
https://blog.csdn.net/21cnbao/article/details/106678646
https://blog.51cto.com/xiamachao/1872790