《OpenShift / RHEL / DevSecOps 汇总目录》
说明:本文针对 Linux 的版本为 RHEL 8。
配置 core dump
- 执行命令,将 core 文件的大小设为不受限。
$ ulimit -c unlimited
$ ulimit -c
- 设置 core dump 文件名的 pattern 和存放位置。
$ echo "/tmp/core.%e.%p" > /proc/sys/kernel/core_pattern
应用生成 core dump 示例
- 创建名为 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;
}
- 编译 crash_example.c 文件,生成可执行程序。
$ gcc -g -o crash_example crash_example.c
- 执行 crash_example 程序,确认出现 core dump 提示。
$ ./crash_example
Starting the program...
Segmentation fault (core dumped)
- 确认在指定目录中出现和 crash_example 程序相关的 core dump 文件。
$ ll /tmp/core*
-rw-------. 1 root root 385024 Feb 7 19:54 /tmp/core.crash_example.2601
分析 core dump 文件
- 安装 gdb 工具。说明:gdb(GNU Debugger)是 core dump 的分析工具。
$ yum install gdb
- 另外还要安装和 glibc 对应的调试信息软件包。
$ yum debuginfo-install glibc-2.28-151.el8.x86_64
- 执行命令,基于可执行程序 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)
- 执行 bt 命令查看程序崩溃时的调用栈,可以看到调用栈和代码行号。
(gdb) bt
#0 0x0000000000400596 in crash () at crash_example.c:6
#1 0x00000000004005b4 in main () at crash_example.c:11
- 执行 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