linux pstore驱动分析和调试

1.简介

pstore(persistent storage)

  主要用于存储内核异常时的log信息。实现方式是,管理一块“非易失性的存储空间”,如不断电的RAM或外部存储,当系统异常时,将log信息写到Pstore管理的存储空间,直到下一次系统正常时,在将log读出来,以文件形式提供给用户使用。

  ramoops指的是采用ram保存oops信息的一个功能,在内核开关中用3个开关控制:PSTORE_CONSOLE控制是否保存控制台输出,PSTORE_FTRACE控制是否保存函数调用序列,PSTORE_RAM控制是否保存panic/oops信息。

  pstore简单来说就是一个小文件系统。主要是读取Android设备内核日志时会用到该模块。

 

2.整体框架

 

3. 配置

3.1. dts

32位操作系统如下配置

reserved-memory {

  #address-cells = <2>;

  #size-cells = <2>;

  ranges;

  reg = <0x50008000 0x100000>;

  reg-names = "reserved-memory ";

};

 

ramoops {

  compatible = "ramoops ";

  record-size = <0x0 0x20000>;

  console-size = <0x0 0x20000>;

  ftrace-size = <0x0 0x20000>;

  pmsg-size = <0x0 0x20000>;

  memory-region = <&reserved-memory>;

  status = "okay";

};

 

  由于dts指定的是物理地址,如果内核地址为0x40008000那么可以把pstore地址配置为0x50008000,留出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

文件路径: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 > /proc/sys/kernel/sysrq

 

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/

 

posted @ 2021-03-03 23:03  waterzhm  阅读(4207)  评论(0编辑  收藏  举报