CORE DUMP 配置
2019年4月30日 · 426 字 · 1 分钟
A core dump is basically a snapshot of the memory when the program crashed.
CORE DUMP
linux 下开发 c/c++
程序往往都是放到服务器上运行。程序再完美,难免 crash
。程序 crash
的时候,我们需要生成一个 core dump(核心转储?)
文件。我们可以使用这个 dump
文件来诊断和调试我们的程序,方法时将这个文件与可执行文件一起加载到调试器中。
预配置
sudo bash -c 'echo core.%e.%p > /proc/sys/kernel/core_pattern'
cat /proc/sys/kernel/core_pattern
#########################
### core.%e.%p
#########################
ulimit -c unlimited ## ubuntu 默认不开启
Demo
/* t.c */
#include <stdio.h>
#include <sys/resource.h>
void foo()
{
int *ptr = 0;
*ptr = 7;
}
int main()
{
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
foo();
return 0;
}
gcc -g -o t.app t.c
./t.app
###################################
# [1] 11630 segmentation fault (core dumped) ./t.app
###################################
gdb ./t.app core.t.app.11630
###################################
# GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
# 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-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 ./t.app...done.
# [New LWP 11630]
# Core was generated by `./t.app'.
# Program terminated with signal SIGSEGV, Segmentation fault.
# #0 0x00005643e43466ca in foo () at t.c:7
# 7 *ptr = 7;
# (gdb) []
###################################
其他
注意:
- gcc 编译的时候要带 -g 参数,否则
gdb
只显示函数名,无法定位到行数 ulimit -c unlimited
命令只对当前命令行生效,所以还是在代码里设置更合适