linux pstore驱动分析和调试

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

https://www.cnblogs.com/ZanyRain/p/10076645.html

http://iyounix.com/2018/03/03/Android_Driver/Driver/[Android7.1][RK3399]%20%E4%BD%BF%E7%94%A8%20ramoops%20%E6%9C%BA%E5%88%B6%E6%9F%A5%E7%9C%8B%E5%BC%80%E6%9C%BA%20Kernel%20Log/

http://huaqianlee.github.io/2020/11/13/Android/pstore/

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pstore是一个内核机制,用于在系统崩溃或意外关机等情况下保存内核日志和转储信息。它可以将这些信息保存在非易失性存储设备(如闪存)中,以便在下次启动时进行检索和分析。 Pstore代码的主要部分包括以下内容: 1. pstore_init()函数:这是Pstore的初始化函数,它在系统启动时被调用。它设置Pstore的参数并创建一个名为pstore_thread的内核线程,该线程在系统运行期间监视内核日志和转储信息,并将其保存到Pstore中。 2. pstore_register()函数:该函数用于向Pstore注册一个后端存储设备,以便在系统崩溃或意外关机时将信息保存到该设备中。该函数需要指定存储设备的类型、名称和其他参数。 3. pstore_ftrace_init()函数:该函数用于初始化Ftrace机制,它可以在系统崩溃时记录内核函数调用堆栈信息。 4. pstore_decompress()函数:该函数用于解压缩Pstore中的压缩数据块。它支持多种压缩算法,包括gzip、bzip2和LZ4等。 5. pstore_write()函数:该函数用于将内核日志和转储信息保存到Pstore中。它接受一个内存缓冲区和其长度作为参数,并将其写入到Pstore中。 6. pstore_sync()函数:该函数用于将Pstore中的缓冲区数据刷回到存储设备中。它可以在系统崩溃或意外关机时确保数据的完整性。 7. pstore_dump()函数:该函数用于在系统崩溃时生成内核转储信息。它会将内核的物理内存映像保存到Pstore中,以便在下次启动时进行分析。 总体来说,Pstore的代码实现比较复杂,包含了很多细节和特殊情况的处理。但是,理解Pstore的工作原理和代码实现对于内核开发人员来说是非常有益的,可以帮助他们更好地理解系统崩溃和日志记录机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值