一、信号core 与 Trem动作类型缩写含义典型信号终止 核心转储Core进程终止并生成 core dump 文件SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV仅终止Term进程直接终止不生成 core 文件SIGHUP, SIGINT, SIGKILL, SIGTERM, SIGPIPE二、Core Dump2.1 什么是 Core DumpCore Dump 核心转储 进程内存的现场快照2.2 为什么没见过 Core 文件云服务器上core dump 功能是被禁止掉的2.3 如何开启 Core Dump# 1. 查看当前限制 $ ulimit -a | grep core core file size (blocks, -c) 0 # 2. 临时开启当前 shell 有效 $ ulimit -c unlimited # 不限制大小 $ ulimit -c 40960 # 限制 40960 blocks约 20MB # 3. 验证设置 $ ulimit -a | grep core core file size (blocks, -c) 40960 # 4. 永久开启写入配置文件 $ echo ulimit -c unlimited ~/.bashrc生成并分析 Core 文件# 测试程序触发除零错误 $ cat test_sig.c EOF #include stdio.h int main() { int a 10; int b 0; int c a / b; // SIGFPE printf(%d\n, c); return 0; } EOF $ gcc -o test_sig test_sig.c -g # -g 保留调试信息 # 运行生成 core 文件 $ ./test_sig Floating point exception (core dumped) # 查看生成的 core 文件 $ ls -lh core* -rw------- 1 user user 2.3M Apr 6 10:30 core.1234 # 使用 gdb 分析 $ gdb ./test_sig core.1234 (gdb) bt # 查看调用栈 (gdb) info locals # 查看局部变量 (gdb) list # 查看源代码三、为什么会核心转储支持debug !开启 core dump 直接运行崩溃 gdb core - file core 直接帮助我们定位到出错行#include iostream #include unistd.h #include signal.h #include sys/types.h #include functional #include vector #include cstdio #include sys/wait.h int main() { pid_t id fork(); if (id 0) { sleep(2); printf(hello world!\n); printf(hello world!\n); printf(hello world!\n); printf(hello world!\n); printf(hello world!\n); int a 0; a / 0; printf(go home!\n); exit(1); } int status 0; waitpid(id, status, 0); printf(signal: %d, exit code: %d, core dump: %d\n, (status 0x7F), (status 8) 0xFF, (status 7) 0x1); return 0; }总结要点一句话总结Core 是调试工具程序崩溃时的内存快照用于事后分析Term 是干净退出外部干预时的正常终止不保留现场云服务器默认禁 Core通过ulimit -c 0限制防止磁盘爆满Core 文件可能很大与进程 RSS 相当大进程可能产生 GB 级 core必须用 -g 编译没有调试信息gdb 无法定位源代码行信号编号看低7位status 0x7F得到终止信号Core 标志看第8位(status 7) 1表示是否产生 core