1.简介
pstore(persistent storage)
主要用于存储内核异常时的log信息。实现方式是,管理一块“非易失性的存储空间”,如不断电的RAM或外部存储,当系统异常时,将log信息写到pstore管理的存储空间,直到下一次系统正常时,在将log读出来,以文件形式提供给用户使用。
ramoops指的是采用ram保存oops信息的一个功能,在内核开关中用4个开关控制:PSTORE_CONSOLE控制是否保存控制台内核输出,PSTORE_PMSG控制是否保存控制台用户输出,PSTORE_FTRACE控制是否保存函数调用序列,PSTORE_RAM控制是否保存panic/oops信息。
pstore简单来说就是一个小文件系统。主要是读取Android设备内核日志时会用到该模块。
2.整体框架
代码路径:fs/pstore
很简单,一共就6个文件,不做过多分析
框架如下
3.配置
3.1.dts
32bit操作系统如下配置
reserved-memory {
#address-cells = <2>;
#size-cells = <2>;
ranges;
ramoops_mem@90008000 {
compatible = "ramoops_mem";
reg = <0x90008000 0x100000>;
record-size = <0x0 0x20000>;
console-size = <0x0 0x20000>;
ftrace-size = <0x0 0x20000>;
pmsg-size = <0x0 0x20000>;
};
};
ramoops {
memory-region = <&reserved-memory>;
status = "okay";
};
由于dts指定的是物理地址,如果内核地址为0x80008000那么可以把pstore地址配置为0x90008000,留出256M空间给内核使用。reg这么配置是32位。每个日志是128KB,总大小是1M,最多可有8个。
3.2.Kconfig
linux/fs/pstore/Kconfig
部分内容如下
config PSTORE
tristate "Persistent store support"
select CRYPTO if PSTORE_COMPRESS
default n
help
This option enables generic access to platform level
persistent storage via "pstore" filesystem that can
be mounted as /dev/pstore. Only useful if you have
a platform level driver that registers with pstore to
provide the data, so you probably should just go say "Y"
(or "M") to a platform specific persistent store driver
(e.g. ACPI_APEI on X86) which will select this for you.
If you don't have a platform persistent store driver,
say N.
config PSTORE_CONSOLE
bool "Log kernel console messages"
depends on PSTORE
help
When the option is enabled, pstore will log all kernel
messages, even if no oops or panic happened.
config PSTORE_PMSG
bool "Log user space messages"
depends on PSTORE
help
When the option is enabled, pstore will export a character
interface /dev/pmsg0 to log user space messages. On reboot
data can be retrieved from /sys/fs/pstore/pmsg-ramoops-[ID].
If unsure, say N.
config PSTORE_FTRACE
bool "Persistent function tracer"
depends on PSTORE
depends on FUNCTION_TRACER
depends on DEBUG_FS
help
With this option kernel traces function calls into a persistent
ram buffer that can be decoded and dumped after reboot through
pstore filesystem. It can be used to determine what function
was last called before a reset or panic.
If unsure, say N.
config PSTORE_RAM
tristate "Log panic/oops to a RAM buffer"
depends on PSTORE
depends on HAS_IOMEM
depends on HAVE_MEMBLOCK
select REED_SOLOMON
select REED_SOLOMON_ENC8
select REED_SOLOMON_DEC8
help
This enables panic and oops messages to be logged to a circular
buffer in RAM where it can be read back at some later point.
Note that for historical reasons, the module will be named
"ramoops.ko".
For more information, see Documentation/admin-guide/ramoops.rst.
根据Kconfig文件,对应的配置选项为
选项 | Kconfig选项 | 含义 |
PSTORE_CONSOLE | Log kernel console messages | 内核日志信息 |
PSTORE_PMSG | Log user space messages | 用户日志信息 |
PSTORE_FTRACE | Persistent function tracer | 函数调用关系 |
PSTORE_RAM | Log panic/oops to a RAM buffer" | 内核崩溃或错误日志 |
3.3.makefile
linux/fs/pstore/makefile
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the linux pstorefs routines.
#
obj-$(CONFIG_PSTORE) += pstore.o
pstore-objs += inode.o platform.o
pstore-$(CONFIG_PSTORE_FTRACE) += ftrace.o
pstore-$(CONFIG_PSTORE_PMSG) += pmsg.o
ramoops-objs += ram.o ram_core.o
obj-$(CONFIG_PSTORE_RAM) += ramoops.o
默认就已经配好了。
3.4.menuconfig配置
如下所示
3.5.编译
make kernel -j8
4.测试
查看模pstore模块开起来没有
dmesg | grep -i pstore
dmesg | grep -i ramoops
日志生成目录
cd /sys/fs/pstore
生成一个内核日志和用户日志
reboot
产生一个崩溃日志
echo c > /proc/sysrq-trigger
产生函数调用日志
echo 1 > /sys/kernel/debug/pstore/record_ftrace
5.参考资料
Documentation/ramoops.txt
https://blog.csdn.net/zgp2917/article/details/107711813
https://blog.csdn.net/qq_27125121/article/details/78666907