一、打开core文件限制
a.sudo vi /etc/profile
b.文件末尾添加ulimit -c unlimited
source /etc/profile
把文件重新加载到内存
c.root@ubuntu:~/code# ulimit -c
unlimited
说明core文件限制已经去处。
二、让core文件生成在进程当前目录
echo "core-%e-%p-%t" > /proc/sys/kernel/core_pattern
三、写一个同一块内存释放两次引起coredump的例子定位并解决
a.编写err.cpp代码如下,同一块内存释放了两次。
root@ubuntu:~/code# cat err.cpp
#include<cstdlib> using namespace std; void repeatFree(char *p) { if(NULL != p) { free(p); } } int main() { char* pstr =(char*) malloc(1024); free(pstr); repeatFree(pstr); }
b.g++ -o err err.cpp
编译生成err可执行文件。
c. ./err
root@ubuntu:~/code# ./err *** Error in `./err': double free or corruption (top): 0x0000000001911010 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7fbe4039f725] /lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7fbe403a7f4a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fbe403ababc] ./err[0x400585] ./err[0x4005b6] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fbe40348830] ./err[0x400499] ======= Memory map: ======== 00400000-00401000 r-xp 00000000 08:01 398325 /root/code/err 00600000-00601000 r--p 00000000 08:01 398325 /root/code/err 00601000-00602000 rw-p 00001000 08:01 398325 /root/code/err 01911000-01932000 rw-p 00000000 00:00 0 [heap] 7fbe3c000000-7fbe3c021000 rw-p 00000000 00:00 0 7fbe3c021000-7fbe40000000 ---p 00000000 00:00 0 7fbe40112000-7fbe40128000 r-xp 00000000 08:01 791701 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fbe40128000-7fbe40327000 ---p 00016000 08:01 791701 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fbe40327000-7fbe40328000 rw-p 00015000 08:01 791701 /lib/x86_64-linux-gnu/libgcc_s.so.1 7fbe40328000-7fbe404e8000 r-xp 00000000 08:01 791663 /lib/x86_64-linux-gnu/libc-2.23.so 7fbe404e8000-7fbe406e7000 ---p 001c0000 08:01 791663 /lib/x86_64-linux-gnu/libc-2.23.so 7fbe406e7000-7fbe406eb000 r--p 001bf000 08:01 791663 /lib/x86_64-linux-gnu/libc-2.23.so 7fbe406eb000-7fbe406ed000 rw-p 001c3000 08:01 791663 /lib/x86_64-linux-gnu/libc-2.23.so 7fbe406ed000-7fbe406f1000 rw-p 00000000 00:00 0 7fbe406f1000-7fbe40717000 r-xp 00000000 08:01 791635 /lib/x86_64-linux-gnu/ld-2.23.so 7fbe408fb000-7fbe408fe000 rw-p 00000000 00:00 0 7fbe40913000-7fbe40916000 rw-p 00000000 00:00 0 7fbe40916000-7fbe40917000 r--p 00025000 08:01 791635 /lib/x86_64-linux-gnu/ld-2.23.so 7fbe40917000-7fbe40918000 rw-p 00026000 08:01 791635 /lib/x86_64-linux-gnu/ld-2.23.so 7fbe40918000-7fbe40919000 rw-p 00000000 00:00 0 7ffe51f1b000-7ffe51f3c000 rw-p 00000000 00:00 0 [stack] 7ffe51ff4000-7ffe51ff6000 r--p 00000000 00:00 0 [vvar] 7ffe51ff6000-7ffe51ff8000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted (core dumped)