Core Dump
what is it
In computing, a core dump, memory dump, crash dump, storage dump, system dump, or ABEND dump consists of the recorded state of the working memory of a computer program at a specific time, generally when the program has crashed or otherwise terminated abnormally.
简而言之,core dump/crash dump就是一个由操作系统记录的进程死亡现场
causes of a core dump
A process dumps core when the operating system terminates it due to an error in the program. The most usual reason for this is that the program accessed an invalid pointer value.
core dump file
core dump 是一个在程序崩溃时由操作系统自动生成的文件,它的存储路径为/data/corefile/
,文件名通常带有.{pid}
后缀
它包含了程序崩溃时的内存快照、CPU寄存器状态以及其他与程序执行相关的信息。然而,core dump文件可能会包含敏感信息(如密码、密钥等),因此在处理core dump文件时应注意保护数据的安全和隐私。此外,core dump文件通常非常大,可能会占用大量磁盘空间,因此在生产环境中通常会限制core dump文件的大小或完全禁用core dump功能
设置 core dump file 大小
临时配置系统级 core dump
ulimit
命令用于设置shell会话中进程的资源限制。要通过ulimit
命令设置core dump的大小,执行ulimit -c
查看当前会话允许的core dump文件大小限制,执行ulimit -c int
设置当前会话允许的core dump文件大小
注意,使用ulimit
命令设置的core dump大小限制仅适用于当前shell会话。当您关闭终端或启动新的shell会话时,这些设置将不再生效
持久配置系统级 core dump
在大多数Linux发行版中,可以在/etc/security/limits.conf
文件中设置资源限制。例如,要将core dump文件大小限制设置为1 GB,请在文件末尾添加以下行:
# - core - limits the core file size (KB)
* soft core 1048576
* hard core 1048576
这将为所有用户设置core dump文件的软限制和硬限制。保存文件并重新启动系统以使更改生效
对特定进程配置core dump
-
查看进程的core dump配置
prlimit --pid your_pid --core
orgrep "core" /proc/$pid/limits
[root@VM-241-167-centos /data/corefile]# prlimit --pid 30433 --core RESOURCE DESCRIPTION SOFT HARD UNITS CORE max core file size 1024 unlimited blocks [root@VM-241-167-centos /data/corefile]# grep "core" /proc/30433/limits Max core file size 1024 unlimited bytes
1024 bytes is soft limit, unlimited is hard limit
通常,Max core file size 的 soft limit < 4096 则无法产生core文件
-
修改进程的core dump配置
prlimit --pid your_pid --core=softlimit:hardlimit
使用 gdb 分析core dump
- 使用gdb打开core dump文件和对应的可执行文件:
gdb your_executable your_core_dump_file
这将启动gdb并加载core dump文件
如果你不清楚core dump文件是哪个可执行文件产生的,可以使用gdb -c your_core_dump_file
让gdb [尝试] 从core dump文件的元数据中获取可执行文件的信息。如果成功,gdb将自动加载对应的可执行文件
- 使用gdb中常用的分析命令进行分析调试:
-
run(或r):启动程序并运行,直到遇到断点或崩溃。
-
break(或b):设置断点。您可以在函数名、源代码文件名及行号或地址处设置断点。例如:
break main break file.c:42 break *0x123456
-
continue(或c):从当前位置继续执行程序,直到遇到下一个断点或崩溃。
-
next(或n):逐行执行程序,但不进入函数调用。
-
step(或s):逐行执行程序,进入函数调用。
-
finish:继续执行程序,直到当前函数返回。
-
print(或p):打印变量或表达式的值。例如:
print variable_name print *pointer print array[42]
-
bt(backtrace):显示函数调用堆栈。
-
frame(或f):选择堆栈帧。例如:
frame 3
这将切换到调用堆栈中的第3帧。
-
info locals:显示当前堆栈帧的局部变量及其值。
-
info args:显示当前堆栈帧的函数参数及其值。
-
info registers:显示CPU寄存器的值。
-
list(或l):显示源代码。您可以指定要显示的源代码文件名及行号。例如:
list file.c:42
-
watch:设置观察点,当变量或表达式的值发生变化时,程序将暂停执行。例如:
watch variable_name
-
delete:删除断点或观察点。您可以指定要删除的断点或观察点的编号。例如:
delete 1
-
quit(或q):退出gdb。
许多编程IDE(集成开发环境)的调试器底层实际上是基于类似gdb这样的命令行调试工具。IDE将这些底层调试工具的功能封装成了图形界面,使得开发人员可以更方便地设置断点、查看变量值、单步执行代码等。
例如,在C和C++开发领域,许多流行的IDE(如Eclipse、Visual Studio Code、CLion等)都支持使用gdb作为底层调试器
- 样例
[root@VM-241-167-centos /data/devops/os/corefilecheck]# gdb proccore5 /data/corefile/core_proccore5_1690614962.30432
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.tl2
Copyright (C) 2013 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".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/devops/os/corefilecheck/proccore5...(no debugging symbols found)...done.
[New LWP 30432]
Core was generated by `./proccore5'. ### Attention: mark core's generator here
Program terminated with signal 11, Segmentation fault.
#0 0x00007f685a89f9e0 in __nanosleep_nocancel () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install bash-4.2.46-34.tl2.3.x86_64 glibc-2.17-326.tl2.x86_64 libgcc-4.8.5-44.tl2.1.x86_64 libstdc++-4.8.5-44.tl2.1.x86_64
(gdb)
(gdb) bt
#0 0x00007f685a89f9e0 in __nanosleep_nocancel () from /lib64/libc.so.6
#1 0x00007f685a89f894 in sleep () from /lib64/libc.so.6
#2 0x000000000040056b in main ()
(gdb)
(gdb) info registers
rax 0xfffffffffffffdfc -516
rbx 0x7ffdafe16390 140727554237328
rcx 0x7f685a89f9e0 140086172318176
rdx 0x0 0
rsi 0x7ffdafe16380 140727554237312
rdi 0x7ffdafe16380 140727554237312
rbp 0xffffffff 0xffffffff
rsp 0x7ffdafe16378 0x7ffdafe16378
r8 0x7ffdafe16490 140727554237584
r9 0x7ffdafe162d0 140727554237136
r10 0x8 8
r11 0x246 582
r12 0x7ffdafe16410 140727554237456
r13 0x7ffdafe16640 140727554238016
r14 0x0 0
r15 0x0 0
rip 0x7f685a89f9e0 0x7f685a89f9e0 <__nanosleep_nocancel+7>
eflags 0x246 [ PF ZF IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) list
No symbol table is loaded. Use the "file" command.
(gdb) file
No executable file now.
No symbol file now.
(gdb) quit
网友评论