RHEL故障诊断2 - 配置 core dump,分析应用故障

OpenShift / RHEL / DevSecOps 汇总目录
说明:本文针对 Linux 的版本为 RHEL 8。

配置 core dump

  1. 执行命令,将 core 文件的大小设为不受限。
$ ulimit -c unlimited
$ ulimit -c
  1. 设置 core dump 文件名的 pattern 和存放位置。
$ echo "/tmp/core.%e.%p" > /proc/sys/kernel/core_pattern

应用生成 core dump 示例

  1. 创建名为 crash_example.c 的文件,内容如下:
#include <stdio.h>
#include <stdlib.h>
 
void crash() {
    char *ptr = NULL;
    *ptr = 'a';  // 故意引发段错误
}
 
int main() {
    printf("Starting the program...\n");
    crash();
    printf("This line will not be reached.\n");
    return 0;
}
  1. 编译 crash_example.c 文件,生成可执行程序。
$ gcc -g -o crash_example crash_example.c
  1. 执行 crash_example 程序,确认出现 core dump 提示。
$ ./crash_example
Starting the program...
Segmentation fault (core dumped)
  1. 确认在指定目录中出现和 crash_example 程序相关的 core dump 文件。
$ ll /tmp/core*
-rw-------. 1 root root 385024 Feb  7 19:54 /tmp/core.crash_example.2601

分析 core dump 文件

  1. 安装 gdb 工具。说明:gdb(GNU Debugger)是 core dump 的分析工具。
$ yum install gdb
  1. 另外还要安装和 glibc 对应的调试信息软件包。
$ yum debuginfo-install glibc-2.28-151.el8.x86_64
  1. 执行命令,基于可执行程序 crash_example 和 core dump 文件 /tmp/core.crash_example.2601 进行跟踪调试。可以看到提示:崩溃是代码中第 6 行造成的。
$ gdb ./crash_example /tmp/core.crash_example.2601
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-20.el8
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
 
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./crash_example...done.
[New LWP 2601]
Core was generated by `./crash_example'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400596 in crash () at crash_example.c:6
6     *ptr = 'a';  // 故意引发段错误
(gdb)
  1. 执行 bt 命令查看程序崩溃时的调用栈,可以看到调用栈和代码行号。
(gdb) bt
#0  0x0000000000400596 in crash () at crash_example.c:6
#1  0x00000000004005b4 in main () at crash_example.c:11
  1. 执行 list 命令查,显示引起程序崩溃附近的代码。
(gdb) list
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void crash() {
5     char *ptr = NULL;
6     *ptr = 'a';  // 故意引发段错误
7 }
8
9 int main() {
10          printf("Starting the program...\n");

参考

https://access.redhat.com/solutions/649193
https://access.redhat.com/solutions/56021
https://access.redhat.com/solutions/4896

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值