coredump路径查看及设置
- coredump路径查看
命令1: cat /proc/sys/kernel/core_pattern
命令2: /sbin/sysctl kernel.core_pattern
- coredump路径修改
临时修改:echo ‘/var/log/%e.core.%p’ > /proc/sys/kernel/core_pattern
永久修改: /sbin/sysctl -w kernel.core_pattern=/var/log/%e.core.%p
如果是ubuntu系统,上述设置方法不生效,因为ubuntu中默认开启了系统错误诊断,这项配置会阻止"echo /home/core-%e-%p >/proc/sys/kernel/core_pattern "的操作。需要做如下设置:
-
- 关闭系统诊断
sudo vim /etc/default/apport
将文件中enabled设置为0
enabled=0
-
- 修改/etc/sysctl.conf文件
vim /etc/sysctl.conf
kernel.core_pattern = /corefile/core-%e-%p-%t
重启后查看coredump路径,设置成功。
coredump size设置
查看coredump size
ulimit -c 返回0则不会生成coredump
ulimit -c unlimited 设置coredump长度,不限长
强制生成coredump
kill -6 pid会强制生成coredump
如dead_lock_demo.cc
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>static int sequence1 = 0;
static int sequence2 = 0;pthread_mutex_t lock1;
pthread_mutex_t lock2;int func1(){pthread_mutex_lock(&lock1); ++sequence1; sleep(1); pthread_mutex_lock(&lock2); ++sequence2; pthread_mutex_unlock(&lock2); pthread_mutex_unlock(&lock1); return sequence1;
}int func2(){pthread_mutex_lock(&lock2); ++sequence2; sleep(1); pthread_mutex_lock(&lock1); ++sequence2; pthread_mutex_unlock(&lock1); pthread_mutex_unlock(&lock2); return sequence1;
}void* thread1(void *arg)
{int rev = 0;while(1){rev = func1(); if (rev == 100000){pthread_exit(NULL);}}
}void* thread2(void *arg)
{int rev = 0;while(1){rev = func2(); if (rev == 100000){pthread_exit(NULL);}}
}void* thread3(void *arg){int count = 0;while(1){sleep(1);if ( count++ > 10000){pthread_exit(NULL);}}
}void* thread4(void *arg){int count = 0;while(1){sleep(1);if ( count++ > 10000){pthread_exit(NULL);}}
}int main(){float a = 0.0334;float b = -0.0321;printf("abs(a)=%.4f,abs(b)=%.4f\n",fabs(a),fabs(b));pthread_t tid[4]; pthread_mutex_init(&lock1, NULL);pthread_mutex_init(&lock2, NULL);if(pthread_create(&tid[0], NULL, &thread1, NULL) != 0){_exit(1);}if(pthread_create(&tid[1], NULL, &thread2, NULL) != 0){_exit(1);}if(pthread_create(&tid[2], NULL, &thread3, NULL) != 0){_exit(1);}if(pthread_create(&tid[3], NULL, &thread4, NULL) != 0){_exit(1);}sleep(5);pthread_join(tid[0], NULL);pthread_join(tid[1], NULL);pthread_join(tid[2], NULL);pthread_join(tid[3], NULL);pthread_mutex_destroy( &lock1 );pthread_mutex_destroy( &lock2 );return 0;
}
执行g++ -Wall -std=c++11 dead_lock_demo.cc -o dead_lock_demo -g -pthread
运行
./dead_lock_demo
找到pid,kill -6 pid会生成coredump
调试coredump gdb dead_lock_demo core-dead_lock_demo
Thread 5 (Thread 0x7f883fb92700 (LWP 6840)):
#0 0x00007f884147a7a0 in __GI___nanosleep (requested_time=requested_time@entry=0x7f883fb91e90, remaining=remaining@entry=0x7f883fb91e90)at ../sysdeps/unix/sysv/linux/nanosleep.c:28
#1 0x00007f884147a67a in __sleep (seconds=0) at ../sysdeps/posix/sleep.c:55
#2 0x0000562321db4b37 in thread4 (arg=0x0) at dead_lock_demo.cc:73
#3 0x00007f884178e6db in start_thread (arg=0x7f883fb92700) at pthread_create.c:463
#4 0x00007f88414b771f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 4 (Thread 0x7f8840393700 (LWP 6839)):
#0 0x00007f884147a7a0 in __GI___nanosleep (requested_time=requested_time@entry=0x7f8840392e90, remaining=remaining@entry=0x7f8840392e90)at ../sysdeps/unix/sysv/linux/nanosleep.c:28
#1 0x00007f884147a67a in __sleep (seconds=0) at ../sysdeps/posix/sleep.c:55
#2 0x0000562321db4afb in thread3 (arg=0x0) at dead_lock_demo.cc:63
#3 0x00007f884178e6db in start_thread (arg=0x7f8840393700) at pthread_create.c:463
#4 0x00007f88414b771f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 3 (Thread 0x7f8840b94700 (LWP 6838)):
#0 __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1 0x00007f8841791025 in __GI___pthread_mutex_lock (mutex=0x562321fb6040 <lock1>) at ../nptl/pthread_mutex_lock.c:80
#2 0x0000562321db4a53 in func2 () at dead_lock_demo.cc:29
#3 0x0000562321db4ac8 in thread2 (arg=0x0) at dead_lock_demo.cc:53
#4 0x00007f884178e6db in start_thread (arg=0x7f8840b94700) at pthread_create.c:463
#5 0x00007f88414b771f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 2 (Thread 0x7f8841395700 (LWP 6837)):
#0 __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1 0x00007f8841791025 in __GI___pthread_mutex_lock (mutex=0x562321fb6080 <lock2>) at ../nptl/pthread_mutex_lock.c:80
#2 0x0000562321db49ef in func1 () at dead_lock_demo.cc:17
#3 0x0000562321db4a9a in thread1 (arg=0x0) at dead_lock_demo.cc:42
#4 0x00007f884178e6db in start_thread (arg=0x7f8841395700) at pthread_create.c:463
#5 0x00007f88414b771f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 1 (Thread 0x7f8841bb6740 (LWP 6836)):
#0 0x00007f884178fd2d in __GI___pthread_timedjoin_ex (threadid=140223186556672, thread_return=0x0, abstime=0x0, block=<optimized out>) at pthread_join_common.c:89
#1 0x0000562321db4cd3 in main () at dead_lock_demo.cc:106
从thread 堆栈信息看出,line17 ,line29发生死锁。
也可以不生成coredump调试,gdb attach pid (or sudo gdb attach pid)
thread apply all bt
和coredump一样的堆栈信息
(gdb) thread apply all btThread 5 (Thread 0x7f648932e700 (LWP 6949)):
#0 0x00007f648ac167a0 in __GI___nanosleep (requested_time=requested_time@entry=0x7f648932de90, remaining=remaining@entry=0x7f648932de90)at ../sysdeps/unix/sysv/linux/nanosleep.c:28
#1 0x00007f648ac1667a in __sleep (seconds=0) at ../sysdeps/posix/sleep.c:55
#2 0x000056036b638b37 in thread4 (arg=0x0) at dead_lock_demo.cc:73
#3 0x00007f648af2a6db in start_thread (arg=0x7f648932e700) at pthread_create.c:463
#4 0x00007f648ac5371f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 4 (Thread 0x7f6489b2f700 (LWP 6948)):
#0 0x00007f648ac167a0 in __GI___nanosleep (requested_time=requested_time@entry=0x7f6489b2ee90, remaining=remaining@entry=0x7f6489b2ee90)at ../sysdeps/unix/sysv/linux/nanosleep.c:28
#1 0x00007f648ac1667a in __sleep (seconds=0) at ../sysdeps/posix/sleep.c:55
#2 0x000056036b638afb in thread3 (arg=0x0) at dead_lock_demo.cc:63
#3 0x00007f648af2a6db in start_thread (arg=0x7f6489b2f700) at pthread_create.c:463
#4 0x00007f648ac5371f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 3 (Thread 0x7f648a330700 (LWP 6947)):
#0 __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1 0x00007f648af2d025 in __GI___pthread_mutex_lock (mutex=0x56036b83a040 <lock1>) at ../nptl/pthread_mutex_lock.c:80
#2 0x000056036b638a53 in func2 () at dead_lock_demo.cc:29
#3 0x000056036b638ac8 in thread2 (arg=0x0) at dead_lock_demo.cc:53
#4 0x00007f648af2a6db in start_thread (arg=0x7f648a330700) at pthread_create.c:463
#5 0x00007f648ac5371f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 2 (Thread 0x7f648ab31700 (LWP 6946)):
#0 __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1 0x00007f648af2d025 in __GI___pthread_mutex_lock (mutex=0x56036b83a080 <lock2>) at ../nptl/pthread_mutex_lock.c:80
#2 0x000056036b6389ef in func1 () at dead_lock_demo.cc:17
#3 0x000056036b638a9a in thread1 (arg=0x0) at dead_lock_demo.cc:42
#4 0x00007f648af2a6db in start_thread (arg=0x7f648ab31700) at pthread_create.c:463
#5 0x00007f648ac5371f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95Thread 1 (Thread 0x7f648b352740 (LWP 6945)):
#0 0x00007f648af2bd2d in __GI___pthread_timedjoin_ex (threadid=140069800449792, thread_return=0x0, abstime=0x0, block=<optimized out>) at pthread_join_common.c:89
#1 0x000056036b638cd3 in main () at dead_lock_demo.cc:106
gdb attach生成coredump
先gdb attach pid
然后generate-core-file/gcore coredump_file_name即可生成coredump file。
参考
设置coredump存储路径_coredump 路径-CSDN博客 [1]