之前大部分时间都是在windows下进行开发,一般都是用集成环境,所以当出现程序崩溃的时候,直接使用编译出来的pdb文件即可找到有用的堆栈信息,并且解决问题
1.centos下默认是不生成coredump,先打开限制,直接上代码吧
#!/bin/bash
### Filename: coredumpshell.sh
### Description: enable coredump and format the name of core file on centos system
# enable coredump whith unlimited file-size for all users
echo -e "\n# enable coredump whith unlimited file-size for all users\n* soft core unlimited" >> /etc/security/limits.conf
# set the path of core file with permission 777
mkdir mydata
cd /mydata && mkdir corefile && chmod 777 corefile
# format the name of core file.
# %% – 符号%
# %p – 进程号
# %u – 进程用户id
# %g – 进程用户组id
# %s – 生成core文件时收到的信号
# %t – 生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970)
# %h – 主机名
# %e – 程序文件名
echo -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /proc/sys/kernel/core_pattern
# for centos7 system(update 2017.2.3 21:44)
echo -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /etc/sysctl.conf
# suffix of the core file name
echo -e "1" > /proc/sys/kernel/core_uses_pid
2.安装gdb调试工具
yum install gdb -y
3.编译生成代码,需要做下处理
编译的时候需要加上 -g
如果要查看变量 需要把 -O优化项改成 -O0
4.常用gdb调试使用命令
gdb 可执行文件路径 coredump文件
进入命令行后使用
where 查找到对应崩溃堆栈
print 打印变量
参考文章:https://blog.csdn.net/ProgramVAE/article/details/105921381