Coredump 调试
coredump是什么?
gdb调试很多都用在编码调试阶段,当在生产环境下的时候,用户正在访问我们的系统不可以用gdb调试,coredump可以在事后查看错误.
程序异常退出时,会产生一个core文件,该文件记录了程序运行时的内存,寄存器状态,堆栈指针,内存管理信息还有各种函数调用堆栈信息等,我们可以理解为是程序工作当前状态存储生成的一个文件,通过工具分析这个文件,我们可以定位到程序异常退出的时候对应的堆栈调用等信息,找出问题所在并进行及时解决。
前期设置
- 先创建所需要产生core文件的目录
- 设置core文件生成的目录,其中%e表示程序文件名,%p表示进程ID,否则会在程序的当前目录生成dore文件;
echo /data/coredump/core.%e.%p >/proc/sys/kernel/core_pattern
-
当前执行程序的用户对core目录有写权限且有足够的空间存储core文件;
------------------这里我们设置文件权限666 所有用户都可以对文件进行读写 -
生成不受限制的core文件; 设置无限
ulimit -c unlimited
root@cuirui-virtual-machine:/# ls
bin dev lib libx32 mnt root snap sys var
boot etc lib32 lost+found opt run srv tmp
cdrom home lib64 media proc sbin swapfile usr
root@cuirui-virtual-machine:/# mkdir data
root@cuirui-virtual-machine:/# cd data/
root@cuirui-virtual-machine:/data# mkdir coredump
root@cuirui-virtual-machine:/data# ls
coredump
root@cuirui-virtual-machine:/data# ll
总用量 12
drwxr-xr-x 3 root root 4096 3月 19 15:22 ./
drwxr-xr-x 21 root root 4096 3月 19 15:22 ../
drwxr-xr-x 2 root root 4096 3月 19 15:22 coredump/
root@cuirui-virtual-machine:/data# chmod 666 coredump/
root@cuirui-virtual-machine:/data# ll
总用量 12
drwxr-xr-x 3 root root 4096 3月 19 15:22 ./
drwxr-xr-x 21 root root 4096 3月 19 15:22 ../
drw-rw-rw- 2 root root 4096 3月 19 15:22 coredump/
root@cuirui-virtual-machine:/data# echo /data/coredump/core.%e.%p >/proc/sys/kernel/core_pattern #指定core文件生成的目录
root@cuirui-virtual-machine:/data# cat /proc/sys/kernel/core_pattern # 查看生成core文件的生成目录
/data/coredump/core.%e.%p
# 设置无限
root@cuirui-virtual-machine:/data# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15243
max locked memory (kbytes, -l) 65536
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 15243
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
root@cuirui-virtual-machine:/data# ulimit -c unlimited #设置无限
root@cuirui-virtual-machine:/data# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15243
max locked memory (kbytes, -l) 65536
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 15243
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
什么情况下会导致程序异常退出
非法指针的访问,堆栈溢出 ----- 一下子就可以定位
如何调试
- 编译的时候添加-g选项,增加调试信息
- gdb program core_file
- bt或者where查看调用栈信息
如果你要查看某一层的信息,你需要切换当前的栈,一般来说,程序停止时,最顶层的栈就是当前栈,如果你要查看栈下面层的详细信息,首先要做的是切换当前栈。
一个小栗子: 定位指针非法访问的行号
text.c
#include <stdio.h>
int func(int *p){
int y = *p;
return y;
}
int main(){
int *p = NULL;
return func(p);
}
root@cuirui-virtual-machine:/home/cuirui/text_demo/coredumpText# ls
text.c text.exe
root@cuirui-virtual-machine:/home/cuirui/text_demo/coredumpText# ./text.exe
段错误 (核心已转储)
root@cuirui-virtual-machine:/home/cuirui/text_demo/coredumpText# ls -la /data/coredump/
总用量 116
drw-rw-rw- 2 root root 4096 3月 19 16:04 .
drwxr-xr-x 3 root root 4096 3月 19 15:22 ..
-rw------- 1 root root 253952 3月 19 16:04 core.text.exe.31259
root@cuirui-virtual-machine:/home/cuirui/text_demo/coredumpText# gdb text.exe /data/coredump/core.text.exe.31259
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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 text.exe...
[New LWP 31259]
Core was generated by `./text.exe'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000558137130139 in func (p=0x0) at text.c:4 #定位第四行出现非法指针访问
4 int y = *p;
(gdb) bt
#0 0x0000558137130139 in func (p=0x0) at text.c:4
#1 0x0000558137130163 in main () at text.c:11
(gdb) quit
root@cuirui-virtual-machine:/home/cuirui/text_demo/coredumpText#