Kdump 调试:kdump原理及其使用方法,kdump

简介

Kdump是在系统崩溃、死锁或死机时用来转储内存运行参数的一个工具和服务,是一种新的crash dump捕获机制,用来捕获kernel crash(内核崩溃)的时候产生的crash dump。

Kdump 使用两个内核:生产内核和捕获内核。生产内核是一个普通内核,它使用特殊的 kdump 特定标志启动。我们需要告诉生产内核保留一些物理内存,用于加载捕获内核。我们需要提前加载捕获内核,因为在崩溃发生的那一刻,由于内核损坏,无法从磁盘读取任何数据。

生产内核是捕获内核服务的对像。捕获内核会在生产内核崩溃时启动起来,与相应的ramdisk一起组建一个微环境,用以对生产内核下的内存进行收集和转存。

第一个内核保留了内存的一部分给第二内核启动用。由于kdump利用kexec启动捕获内核,绕过了 BIOS,所以第一个内核的内存得以保留。这是内核崩溃转储的本质。

dump原理

为了在生产内核崩溃时能顺利启动捕获内核,捕获内核以及它的ramdisk是事先放到生产内核的内存中的

生产内核的内存是通过/proc/vmcore这个文件交给捕获内核的。为了生成它,用户工具在生产内核中分析出内存的使用和分布等情况,然后把这些信息综合起来生成一个ELF头文件保存起来。

捕获内核被引导时会被同时传递这个ELF文件头的地址,通过分析它,捕获内核就可以生成出/proc/vmcore。有了/proc/vmcore这个文件,捕获内核的ramdisk中的脚本就可以通过通常的文件读写和网络来实现各种策略了。

注意,在启动时,kdump保留了一定数量的重要的内存,为了计算系统需要的真正最小内存,加上kdump使用的内存数量,以决定真正的最小内存的需求。

支持架构

x86,x86_64,arm,arm64,ppc,s390,sh

kexec机制

kexec简介

Kexec是基于kexec机制工作的,因此先了解一下Kexec。

kexec是一个快速启动机制,允许通过已经运行的内核的上下文启动一个Linux内核,不需要经过BIOS。(BIOS可能会消耗很多时间,特别是带有众多数量的外设的大型服务器。这种办法可以为经常启动机器的开发者节省很多时间。)

Kexec的实现包括2个组成部分:

** 一是内核空间的系统调用:kexec_load() **,负责在生产内核(production kernel 或 first kernel)启动时将捕获内核(capture kernel或sencond kernel)加载到指定地址。

** 二是用户空间的工具kexec-tools **,他将捕获内核的地址传递给生产内核,从而在系统崩溃的时候能够找到捕获内核的地址并运行。没有kexec就没有kdump。先有kexec实现了在一个内核中可以启动另一个内核,才让kdump有了用武之地。

kexec_load()

kexec 在 kernel 里以一个系统调用 kexec_load()的形式提供给用户。这个系统调用主要用来把另一个内核和其 ramdisk 加载到当前内核中。在 kdump中,捕获内核只能使用事先预留的一小段内存。

生产内核的内存镜像会被以 /proc/vmcore 的形式提供给用户。这是一个 ELF格式的方件,它的头是由用户空间工具 kexec 生成并传递来的。在系统崩溃时,系统最后会调用machine_kexec()。这通常是一个硬件相关的函数。它会引导捕获内核,从而完成 kdump 的过程。

kexec-tools

kdump 的很大一部分工作都是在用户空间内完成的。与 kexec相关的集中在一个叫kexec-tools的工具中的kexec程序中。

该程序主要是为调用 kexec_load()收集各种信息,然后调用之。这些信息主要包括 purgatory 的入口地址,还有一组由 struct kexec_segment描述的信息。

最后,附上一张图,看下kdump和kexec整个的工作流程。

kdump使用

内核配置

修改内核中以下的配置宏,可在.config文件中修改,或者通过make menuconfig修改

CONFIG_KEXEC=yCONFIG_CRASH_DUMP=yCONFIG_PROC_VMCORE=yCONFIG_PROC_KCORE=yCONFIG_SYSFS=yCONFIG_DEBUG_INFO=y

确认修改成功

root@firefly:/sys/kernel# ls /sys/kernel/ | grep kexeckexec_crash_loadedkexec_crash_sizekexec_loadedroot@firefly:~# ls /proc/ | grep kcorekcore

如果出现proc/kcore,kexec相关节点说明配置生效了。

配置预留内存

预留内存的4种形式

预留内存的设置一般有4种形式:

第一种是最常用的,直接通过size指定预留的大小,offset指定预留内存地址的起始位置。不过,offset一般不指定,对于一般用户来讲,很难确定预留内存恶起始位置。

crashkernel=size[KMG][@offset[KMG]]

第二种方式会根据系统的内存大小自动选择预留内存的大小,比较灵活。

crashkernel=range1:size1[,range2:size,...][@offset]

举例

crashkernel=512M-2G:64M,2G-6G:256M,6G-8G:512M,8G-:768M

参数含义如下:

如果RAM大小小于512M,则不预留内存。

如果RAM大小为512M - 2G,则预留 64M。

如果RAM大小为2 - 6G,则预留 256M。

如果RAM大小大于8G,则预留768 M。

一般我们会在0~4G范围内预留内存。如果系统内存大于4G,则支持在4G以上预留内存,比如X86_64架构。当指定high参数时,系统会在0~4G和4G以上预留两段内存。默认情况下,x86_64会在4G以下预留256M内存。

crashkernel=size[KMG],hign

low参数主要是配合high来使用的。如果觉得4G以下默认预留的256M太多了,可以手动指定预留内存。

crashkernel=size[KMG],low

在ARM上配置预留内存

在X86-64主机上一般是修改/etc/default/grup中的参数来配置及检查, 但是在嵌入式设备上因为是裁剪的系统,并没有grup这个文件。

但我们可以知道,配置grup文件的目的就是更改cmdline中的内容,那我们如何去更改cmdline的内容呢?提供以下几个思路:

在dts中中添加:修改chosen

在BoardConfig中添加

在uboot中添加:在源码中添加或者通过setenv配置bootargs变量

在android的Makefile中添加

这里我们选择在dts中修改。

vim kernel/arch/arm64/boot/dts/rockchip/rk3399-linux.dtsi 

当前使用的设备RAM已经是4G,所以预留的是256M

root@firefly:~# free -m total used free shared buff/cache availableMem: 3583 194 3154 8 234 3351Swap: 0 0 0

 

 

重新编译烧写内核,看到设备启动时,已经加入了启动参数。

查看启动参数是否生效

root@firefly:~# cat /proc/iomem | grep Crashe5e00000-f5dfffff : Crash kernel

确认分配内存大小

root@firefly:~# cat /sys/kernel/kexec_crash_size268435456

预留内存大小评估

在某些情况下,我们需要正确评估预留内存的大小,主要从以下2个方面考虑。

系统内kernel,initrd,romfs,devices driver的大小。

捕获内核启动cpu的个数。启动cpu越多,需要的内存越大。一般情况下,捕获内核一般启动一个CPU核即可。

/proc/iomem表示的是系统的物理内存布局, System RAM entry表示当前系统可用的预留内存。例如,我当前设备的内存为3.8G,预留800M内存也是足够的。

root@firefly:~# cat /proc/iomem | grep System00200000-083fffff : System RAM0a200000-f7ffffff : System RAM 

编译kexec工具

解压源码包。

tar xvpzf kexec-tools-2.0.26.tar.gz 

进入到kexec-tools中,并进行配置。

LDFLAGS=-static ./configure ARCH=arm64 --build=x86_64-linux-gnu --host=aarch64-linux-gnu --target=aarch64-linux-gnu --without-xen

这里使用静态编译。

然后使用make进行编译。

make

将build目录下sbin/kexec 拷贝至rootfs /usr/sbin/中。

root@firefly:~/kexec/sbin# kexec -vkexec-tools 2.0.26

查看kexec参数。

root@firefly:~# kexec -hkexec-tools 2.0.26Usage: kexec [OPTION]... [kernel]Directly reboot into a new kernel -h, Print this help. -v, --version Print the version of kexec. -f, --force Force an immediate kexec, don't call shutdown. -i, --no-checks Fast reboot, no memory integrity checks. -x, --no-ifdown Don't bring down network interfaces. -y, --no-sync Don't sync filesystems before kexec. -l, --load Load the new kernel into the current kernel. -p, --load-panic Load the new kernel for use on panic. -u, --unload Unload the current kexec target kernel. If capture kernel is being unloaded specify -p with -u. -e, --exec Execute a currently loaded kernel. --exec-live-update Execute a currently loaded xen image afterstoring the state required to live update. -t, --type=TYPE Specify the new kernel is of this type. --mem-min= Specify the lowest memory address to load code into. --mem-max= Specify the highest memory address to load code into. --reuseinitrd Reuse initrd from first boot. --print-ckr-size Print crash kernel region size. --load-preserve-context Load the new kernel and preserve context of current kernel during kexec. --load-jump-back-helper Load a helper image to jump back to original kernel. --load-live-update Load the new kernel to overwrite the running kernel. --entry= Specify jump back address. (0 means it's not jump back or preserve context) to original kernel. -s, --kexec-file-syscall Use file based syscall for kexec operation -c, --kexec-syscall Use the kexec_load syscall for for compatibility with systems that don't support -s (default) -a, --kexec-syscall-auto Use file based syscall for kexec and fall back to the compatibility syscall when file based syscall is not supported or the kernel did not understand the image -d, --debug Enable debugging to help spot a failure. -S, --status Return 1 if the type (by default crash) is loaded, 0 if not.Supported kernel file types and options: vmlinux An ARM64 ELF image, big or little endian. Typically vmlinux or a stripped version of vmlinux.Image An ARM64 binary image, uncompressed, big or little endian. Typically an Image file.uImage An ARM64 U-boot uImage file, compressed or not, big or little endian.zImage An ARM64 zImage, compressed, big or little endian. Typically an Image.gz or Image.lzma file.Architecture options: --append=STRING Set the kernel command line to STRING. --command-line=STRING Set the kernel command line to STRING. --dtb=FILE Use FILE as the device tree blob. --initrd=FILE Use FILE as the kernel initial ramdisk. --serial=STRING Name of console used for purgatory printing. (e.g. ttyAMA0) --ramdisk=FILE Use FILE as the kernel initial ramdisk. --reuse-cmdline Use kernel command line from running system.

注意以下几个参数

-d: 执行kexec指令时会打印调试信息 -p: 将内核加载到预留内存中,panic时自动启动capture内核。-l: 将内核加载到预留内存中--append : capture内核的command line的内容--t: 内核的类型,比如vmlinux,Image,uImage,zImage--intrd:指定initrd--reuseinitrd:复用第一个内核的initrd--dtb:指定设备树

vmlinux,Image,uImage,zImage区别参考:secure boot (一)FIT Image

测试

配置kexec

尝试手动配置kexec

kexec --t vmlinux -p /root/var/vmlinux --ramdisk /root/var/ramdisk.img --append="storagemedia=emmc androidboot.storagemedia=emmc androidboot.mode=normal storagenode=sdhci@fe330000 androidboot.slot_suffix= androidboot.serialno=3fdce35e50641399 ro rootwait earlycon=uart8250,mmio32,0xff1a0000 swiotlb=1 console=ttyFIQ0 root=PARTLABEL=rootfs rootfstype=ext4 overlayroot=device:dev=PARTLABEL=userdata,fstype=ext4,mkfs=1 coherent_pool=1m systemd.gpt_auto=0 cgroup_enable=memory swapaccount=1 crashkernel=256M"

command line 可以通过 cat /proc/cmdline 查看。

ramdisk.img 也可以叫做initrd.img, 它是一个小文件系统,麻雀虽小五脏俱全,它介于kernel 和 文件系统之间。kernel 启动后会先执行ramdisk.img 里面的init, 挂载这里的小型文件系统,接着开始完成一些必要的操作,最后在交给文件系统/sbin/init 进行执行。

查看捕获内核的加载状态 0:未加载,1:已加载

root@firefly:~# cat /sys/kernel/kexec_crash_loaded 

1查看捕获内核的大小

root@firefly:~# cat /sys/kernel/kexec_crash_size

268435456

确认 kexec_load_disabled 的状态

root@firefly:~# cat /proc/sys/kernel/kexec_load_disabled0

kexec_load_disabled:表示kexec_load系统调用是否被禁止,此系统调用用于kdump。当发生了一次kexec_load后,此值会自动设置为1。

测试启动捕获内核

在前面的准备工作完成后,如果触发系统崩溃,系统将重新引导到转储-捕获内核,触发点位于panic()、die()、die_nmi()和sysrq处理程序中。接下来我将通过 魔术键来触发系统panic。

开启sysrq

echo 1 > /proc/sys/kernel/sysrq 

触发sysrq

echo c > /proc/sysrq-trigger

触发sysrq后,系统重启,串口打印出标志性 log:Bye!Starting crashdump kernel...。

[0637]root@firefly:~# echo c > /proc/sysrq-trigger[0637][ 28.817657] sysrq: SysRq : Trigger a crash[0637][ 28.818172] Unable to handle kernel NULL pointer dereference at virtual address 00000000[0637][ 28.818894] pgd = ffffffc0deb9d000[0637][ 28.819326] [00000000] *pgd=0000000000000000, *pud=0000000000000000....................[0637][ 28.950698] [] sysrq_handle_crash+0x24/0x30[0637][ 28.951218] [] __handle_sysrq+0xa0/0x14c[0637][ 28.951713] [] write_sysrq_trigger+0x5c/0x74[0637][ 28.952246] [] proc_reg_write+0xa8/0xcc[0637][ 28.952744] [] __vfs_write+0x48/0xe8[0637][ 28.953214] [] vfs_write+0xa8/0x15c[0637][ 28.953674] [] SyS_write+0x5c/0xb0[0637][ 28.954123] [] el0_svc_naked+0x24/0x28[0637][ 28.954609] Code: 52800020 b90a1c20 d5033e9f d2800001 (39000020) [0637][ 28.955167] SMP: stopping secondary CPUs[0637][ 28.955899] Starting crashdump kernel...[0637][ 28.956264] Bye![0651][ 0.000000] Booting Linux on physical CPU 0x101[0651][ 0.000000] Initializing cgroup subsys cp 0.000000] Initializing cgrouys cpu[0651][ 0.000000] Initializys cpuacct[0651][ 0.000000] Linux version 4.4.194+ (zhongyi@ubunty: b1730021dd51a88c333473088af3a402491b4c23) (gcc version 6.3.1 20170404 (Linaro GCC 6.3-2017.05SMP Fri Mar 3 0700 CST 2023[0651][ 0.000000] Boot CPU: AArch64 Processor [410fd082][0651][ 0.000000] earlycon: Early serial console at MMIO32 0xff1a0000 (opti '')[0651][ 0.000000] bootconsole [uart0] enabled[0651][ 0.000000] cannot allocate crashkernel (size:0x10000000)[0651][ 0.000000] Reserving 1KB of memory at 0xf5dff000 for elfcorehdr[0651][ 0.000000] psci: probing for conduit method from DT.[0651][ 0.000000] psci: PSCIv1.0 detected in firmware.[0651][ 0.000000] psci: Using standard PSCI v0.2 function IDs[0652][ 0.000000] psci: Trusted OS migration not required[0652][ 0.000000] PERCPU: Embedded 21 pages/cpu @ffffffc035cf1000 s46440 r8192 d31384 u86016[0652][ 0.000000] Detected PIPT I-cache on CPU0[0652][ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 64512[0652][ 0.000000] Kernel command line: storagemedia=emmc androidboot.storagemedia=emmc androidboot.mode=normal storagenode=sdhci@fe330000 androidboot.slot_suffix= androidboot.serialno=3fdce35e50641399 ro rootwait earlycon=uart8250,mmio32,0xff1a0000 swiotlb=1 console=ttyFIQ0 root=PARTLABEL=rootfs rootfstype=ext4 overlayroot=device:dev=PARTLABEL=userdata,fstype=ext4,mkfs=1 coherent_pool=1m systemd.gpt_auto=0 cgroup_enable=memory swapaccount=1 crashkernel=256M[0652][ 0.000000] PID hash table entries: 1024 (order: 1, 8192 bytes)[0652][ 0.000000] Dentry cache hash table entries: 32768 (order: 6, 262144 bytes)[0652][ 0.000000] Inode-cache hash table entries: 16384 (order: 5, 131072 bytes)[0652][ 0.000000] software IO TLB: mapped [mem 0xf5c51000-0xf5c91000] (0MB)[0652][ 0.000000] Memory: 208908K/262144K available (14782K kernel code, 2146K rwdata, 6988K rodata, 1216K init, 780K bss, 53236K reserved, 0K cma-reserved)[0652][ 0.000000] Virtual kernel memory layout:[0652][ 0.000000] modules : 0xffffff8000000000 - 0xffffff8008000000 ( 128 MB)[0652][ 0.000000] vmalloc : 0xffffff8008000000 - 0xffffffbdbfff0000 ( 246 GB)[0652][ 0.000000] .init : 0xffffff80095d0000 - 0xffffff8009700000 ( 1216 KB)[0652][ 0.000000] .text : 0xffffff8008080000 - 0xffffff8008ef0000 ( 14784 KB)[0652][ 0.000000] .rodata : 0xffffff8008ef0000 - 0xffffff80095d0000 ( 7040 KB)[0652][ 0.000000] .data : 0xffffff8009700000 - 0xffffff8009918808 ( 2147 KB)[0652][ 0.000000] vmemmap : 0xffffffbdc0000000 - 0xffffffbfc0000000 ( 8 GB maximum)[0652][ 0.000000] 0xffffffbdc0978000 - 0xffffffbdc0d78000 ( 4 MB actual)[0652][ 0.000000] fixed : 0xffffffbffe7fb000 - 0xffffffbffec00000 ( 4116 KB)[0652][ 0.000000] PCI I/O : 0xffffffbffee00000 - 0xffffffbfffe00000 ( 16 MB)[0652][ 0.000000] memory : 0xffffffc025e00000 - 0xffffffc035e00000 ( 256 MB)[0652][ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=6, Nodes=1.........................................[0654][ 2.313003] rockchip-drm display-subsystem: bound ff940000.hdmi (ops dw_hdmi_rockchip_ops)[0654][ 2.314207] i2c i2c-10: of_i2c: modalias failure on /dp@fec00000/ports[0654][ 2.315077] rockchip-drm display-subsystem: bound fec00000.dp (ops cdn_dp_component_ops)[0654][ 2.315815] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).[0654][ 2.316404] [drm] No driver support for vblank timestamp query.[0654][ 2.317133] rockchip-drm display-subsystem: connector[HDMI-A-1] can't found any modes.................................................[0658][ 6.180434] [dhd] dhd_conf_set_path_params : Final conf_path=/vendor/etc/firmware/config.txt[0658][ 6.313492] [dhd] dhd_conf_set_txglom_params : txglom_mode=multi-desc[0658][ 6.314159] [dhd] dhd_conf_set_txglom_params : txglomsize=36, deferred_tx_len=0[0658][ 6.314868] [dhd] dhd_conf_set_txglom_params : txinrx_thres=128d_txminmax=-1[0658][ 6.315529] [ddhd_conf_set_txglom_params : tx__offset=0, txctl_tmo_fix=300[0658][ 6.316245] [dhd] dhd_conf_get_disable_proptx : fw_proptx=1, disable_proptx=-1[0658][ 6.380768] [dhd] dhd_conf_map_country_list : CN/38[0658][ 6.381222] [dhd] dhd_conf_set_country : set country CN, revision 38[0658][ 6.385992] [dhd] dhd_conf_set_country : Country code: CN (CN/38)[0658][ OK ] Started Network Manager.[0658][ OK ] Reached target Network.[0658] Starting Permit User Sessions...[0658] Starting OpenBSD Secure Shell server...[0658][ OK ] Started Permit User Sessions.[0658] Starting Hold until boot process finishes up...[0658][ OK ] Started Hold until boot process finishes up.[0658][ OK ] Started Serial Getty on ttyFIQ0.[0658] Starting Set console scheme...[0658][ OK ] Started Set console scheme.[0658][ OK ] Created slice system-getty.slice.[0658][ OK ] Started Getty on tty1.[0658][ OK ] Reached target Login Prompts.[0658][ OK ] Started OpenBSD Secure Shell server.[0658][ OK ] Started Adbd for linux.[0658][ OK ] Started Setup rockchip platform environment.[0658] Starting Light Display Manager...[0658][ OK ] Reached target Multi-User System.[0659][ OK ] Started Light Display Manager.[0659][ OK ] Reached target Graphical Interface.[0659] Starting Update UTMP about System Runlevel Changes...[0659][ OK ] Started Update UTMP about System Runlevel Changes.[0659][0659]Ubuntu 18.04.6 LTS firefly ttyFIQ0 [0639]root@firefly:~# ls -al /proc/vmcore [0641]-r-------- 1 root root 3885387776 Mar 5 22:49 /proc/vmcore[0624]root@firefly:~# ls -al --block-size=m /proc/vmcore 

系统正常启动后,就可以将/proc/vmcore文件拷贝出来在ubuntu上用crash工具分析。

常见问题及解决办法

在ARM平台上,系统崩溃后卡死,未启动第二内核

不清楚是宿主机的原因还是代码原因,目前主线的 Linux kernel 代码在执行命令使第一个内核崩溃之后,跳转到第二个内核的过程中卡死,在社区上也有其他人遇到了类似的情况并给出了补丁,但是并没有合并到主线,不过目前为了演示暂时不考虑为何原因导致这个问题的出现,如果你的 arm64 不存在这个问题,那么就不需要打这个补丁了。奉上补丁如下:

diff --git a/arch/arm64/kernel/machine_kexec.c b/arch/arm64/kernel/machine_kexec.cindex aa9c94113700..3b0350d20e31 100644--- a/arch/arm64/kernel/machine_kexec.c+++ b/arch/arm64/kernel/machine_kexec.c@@ -234,19 +234,12 @@ static void machine_kexec_mask_interrupts(void) for_each_irq_desc(i, desc) { struct irq_chip *chip;- int ret; chip = irq_desc_get_chip(desc); if (!chip) continue;- /*- * First try to remove the active state. If this- * fails, try to EOI the interrupt.- */- ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);-- if (ret && irqd_irq_inprogress(&desc->irq_data) &&+ if (irqd_irq_inprogress(&desc->irq_data) && chip->irq_eoi) chip->irq_eoi(&desc->irq_data);

还有一点需要说明的就是在 Ubuntu 默认仓库的 crash 不支持最新版本的 Linux 内核,需要更新到 7.2.5 版本才可以。

没有生产vmcore

按照kdump执行流程,确定问题来自那个阶段。

预留内存失败

预留内存过大,设备没有足够的可用内存。默认会在0~4G预留内存。比如预留512M的空间,而在0~4G并没有可用的512M空间,就会导致预留失败。

加载内核失败

是否预留内存,crashkernel是否配置?

预留内存失败。

预留内存成功:尝试使用kexec -d -p 查看失败的具体原因。

第二内核启动失败

打印出bye后没有任何信息输出,可能是第二内核可能未配置串口,earlycon/console

oom后卡死,可能是预留内存太小。

驱动初始化失败。有些驱动,比如dma32,可能只能使用0~4G内存,在4G以上预留内存会导致驱动加载失败。

makedumpfile失败

加上-D,打印出debug选项,查看失败原因。

用户态工具问题

kernel,kexec,makedumpfile,crash匹配问题,更新到最新的工具。

编辑:黄飞

相关推荐

相关文章