工控網首頁
>

應用設計

>

嵌入式Linux上使用Ramoops

嵌入式Linux上使用Ramoops

對于嵌入式設備,盡管在部署前會經歷大量的測試和驗證,但在使用現場有時候仍不可避免會出現意外情況,如 Kernel opps、panic。當出現類似情況時,系統日志往往無法及時寫入 flash,重啟后不能獲得用于分析問題的關鍵信息。Ramoops 可以應對此類問題。當發發生 Kernel opps、panic時,它能夠將相關日志保存到特定的內存區域,并在軟重啟后仍可以讀取。文章將使用安裝 Linux BSP v3.0  Apalis iMX6 計算機模塊進行說明。

 

首先使用 Toradex Easy Installer 安裝 Linux BSP v3.0。然后下載對應的 Linux 源碼,分支為 toradex_4.14-2.3.x-imx。交叉編譯工具是 gcc-arm-8.2-2019.01-x86_64-arm-linux-gnueabihf。

 

應用  Apalis iMX6 默認內核配置。

-----------------------------------------

make apalis_imx6_defconfig

-----------------------------------------

 

開啟 ramoops 功能。

-----------------------------------------

make menuconfig

File systems → Miscellaneous filesystems

<*>   Persistent store support                                                                                               

    Choose compression algorithm (ZLIB)  --->                                                                          

[*]     Log kernel console messages                                                     

[*]     Log user space messages      

<*>     Log panic/oops to a RAM buffer

-----------------------------------------

 

嵌入式Linux上使用Ramoops1445.png 

 

 

為了便于觸發 kernel panic 開啟 sysrq 功能。

-----------------------------------------

Kernel hacking

[*] Magic SysRq key

0x1) Enable magic SysRq key functions by default

[*]   Enable magic SysRq key over serial  

-----------------------------------------

 

嵌入式Linux上使用Ramoops1697.png 

 

 

最后重新編譯內核以及內核模塊。

-----------------------------------------

$ make zImage LOADADDR=10008000

$ make modules

-----------------------------------------

 

 

使用新的內核和模塊重新啟動 Apalis iMX6。Ramoops 在內核配置里又稱為 PSTORE,使用下面命令查看之前的內核內置是否生效。

-----------------------------------------

root@apalis-imx6:~# zcat /proc/config.gz |grep PSTORE

CONFIG_PSTORE=y

CONFIG_PSTORE_ZLIB_COMPRESS=y

# CONFIG_PSTORE_LZO_COMPRESS is not set

# CONFIG_PSTORE_LZ4_COMPRESS is not set

CONFIG_PSTORE_CONSOLE=y

CONFIG_PSTORE_PMSG=y

CONFIG_PSTORE_RAM=y

root@apalis-imx6:~# zcat /proc/config.gz |grep SYSRQ

CONFIG_MAGIC_SYSRQ=y

CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x1

CONFIG_MAGIC_SYSRQ_SERIAL=y

-----------------------------------------

 

此時,ramoops 還沒有配置完成,需要在 device tree里創建對應的節點。在這之前先確定在內存中可以為 ramoops 預留的地址空間。在 Linux 運行下面命令。

-----------------------------------------

root@apalis-imx6:~# cat /proc/iomem

00100000-00103fff : /soc/caam-sm@00100000

00120000-00128fff : 120000.hdmi_core

00130000-00133fff : galcore register region

……

02204000-02207fff : galcore register region

02400000-027fffff : 2400000.ipu

02800000-02bfffff : 2800000.ipu

10000000-4fffffff : System RAM

  10008000-10cfffff : Kernel code

  10e00000-10eeb3cf : Kernel data

-----------------------------------------

 

RAM 的物理地址空間為 0x10000000-0x4fffffff,選擇在 Kernel code  Kernel data 之外的 0x30000000 作為 ramoops 的起始地址,大小為 1MB。

 

 arch/arm/boot/dts/imx6q.dtsi  reserved-memory  節點里添加  ramoops。

嵌入式Linux上使用Ramoops3109.png 

 

重新編譯 device tree。

-----------------------------------------

make imx6q-apalis-eval.dtb

 

-----------------------------------------

 

使用新的 device tree 啟動后,可以看到以下信息。

-----------------------------------------

root@apalis-imx6:~# dmesg|grep ramoops

[    0.071682] pstore: Registered ramoops as persistent store backend

[    0.071707] ramoops: attached 0x100000@0x30000000, ecc: 0/0

-----------------------------------------

 

 

下面命令分別設置系統在發生 kernel panic 秒后自動重啟,以及觸發 kernel panic。

-----------------------------------------

root@apalis-imx6:~# echo 1 > /proc/sys/kernel/panic

root@apalis-imx6:~# echo c > /proc/sysrq-trigger

-----------------------------------------

 

在自動重啟后,將 ramoops 掛載到 /home/root/pstore 目錄,可以看到上次發生 kerne panic 時的日志。

-----------------------------------------

root@apalis-imx6:~# mkdir -p /home/root/pstore

root@apalis-imx6:~# mount -t pstore psotre /home/root/pstore

root@apalis-imx6:~# ls pstore/

console-ramoops-0  dmesg-ramoops-0    dmesg-ramoops-1

 

root@apalis-imx6:~/pstore# tail -n 5 console-ramoops-0

[  856.337055] ffa0:                                     00be5898 00000000 00000020 76ed4bb4

[  856.345259] ffc0: 00be5898 00000020 00000002 00000001 76ed71c0 00be6828 00000001 7ed702e0

[  856.353460] ffe0: 00000000 7ed70138 76dd382d 76d88cd0 000f0010 ffffffff

[  856.360101]  r9:00be6828 r8:10c5387d r7:10c5387d r6:ffffffff r5:000f0010 r4:76d88cd0

[  856.385215] Rebooting in 1 seconds..

 

root@apalis-imx6:~/pstore# tail -n 5 dmesg-ramoops-0

<4>[  856.200454]  r9:00000000 r8:00000000 r7:00000002 r6:00d00440 r5:a909bf00 r4:a909bf00

<4>[  856.208233] [<80227be8>] (SyS_write) from [<80107d20>] (ret_fast_syscall+0x0/0x54)

<4>[  856.215828]  r9:a975a000 r8:80107f24 r7:00000004 r6:76f5bda0 r5:00d00440 r4:0000006c

<0>[  856.223594] Code: e5834000 f57ff04e ebf07aaa e3a03000 (e5c34000)

<4>[  856.229847] ---[ end trace 583cc693cbfd2cb1 ]---

-----------------------------------------

 

 

由于 ramoops 是將日志保存在內存里,如果模塊是冷啟動,即電源復位,那么相關的內容也不再保留。

-----------------------------------------

root@apalis-imx6:~# mount -t pstore psotre /home/root/pstore

root@apalis-imx6:~# ls pstore/

root@apalis-imx6:~#

-----------------------------------------

 

參考

https://git.toradex.cn/cgit/linux-toradex.git/tree/Documentation/admin-guide/ramoops.rst?h=toradex_4.14-2.3.x-imx

https://lwn.net/Articles/501748/

 

 

總結

ramoops 可以使用較小的開銷記錄系統日志一般難以保存的 kernel panic 錯誤。由于存儲于內存里面,因此在掉電后這些信息就不復存在。在 Linux 中還有許多其他的調試方法,如kdb, kdump, tracing 等,它們的使用特點和復雜程度也各有不同,用戶可以根據需求加以選擇。

審核編輯(
王靜
)
投訴建議

提交

查看更多評論
其他資訊

查看更多

在 Linux 系統上 Docker 容器的性能影響

上游優先 - Toradex 采用主線內核支持

NXP iMX8M Plus M7核心FreeRTOS開發

使用容器編譯Yocto鏡像

基于NXP iMX8測試Secure Boot功能部署