core dump

一.core文件的生成开关和大小限制
---------------------------------
1)使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。
2)使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。

二.core文件的名称和生成路径
----------------------------
core文件生成路径:
输入可执行文件运行命令的同一路径下。
若系统生成的core文件不带其他任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。

1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
可通过以下命令修改此文件:
echo "1" > /proc/sys/kernel/core_uses_pid

2)proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件:
echo "/corefile/core-%e-%p-%t" > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
%p - insert pid into filename 添加pid
%u - insert current uid into filename 添加当前uid
%g - insert current gid into filename 添加当前gid
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加命令名

三.core文件的调试
core文件需要使用gdb来查看。

使用bt命令即可看到程序出错的地方。
gdb ./a.out  core.xxxx
bt   %查看堆栈

info threads %查看线程

thread num  %进入对应的线程号后,可以通过 bt 查看对应线程的调用栈

f num  %进入第一级调用栈查看所有的局部变量

i locals   %查看所有变量值

接下来可以继续用gdb进行调试,进一步分析。

gdb ./a.out 

b num

run

记住几个常用的gdb命令:

l(list) ,显示源代码,并且可以看到对应的行号;

b(break)x, x是行号,表示在对应的行号位置设置断点; 

       b file:linenum    %某个文件对应行数断点

       b 函数名

       b 行号 if 条件

p(print)x, x是变量名,表示打印变量x的值

r(run), 表示继续执行到断点的位置

n(next),表示执行下一步

c(continue),表示继续执行

q(quit),表示退出gdb

四、出现core设置错误

问题:[taoge@localhost ~]$ ulimit -c 999
        -bash: ulimit: core file size: cannot modify limit: Operation not permitted

解决:

         1. 执行su命令, 以root用户登录, 然后打开如下文件:

             [root@localhost taoge]# vim /etc/ssh/sshd_config 

             把UseLogin后面的no改为yes, 且把UseLogin前面的#号去掉

          2. 重启sshd服务, 如下:

              [root@localhost taoge]# /etc/init.d/sshd restart

          3. 执行exit命令, 退出到普通用户状态。 然后再次执行exit命令退出SecureCRT, 然后重新连上SecureCRT.

          4. 普通用户可以执行ulimit -c 999这样的命令了, 如下:

               [taoge@localhost ~]$ ulimit -c 999
               [taoge@localhost ~]$

          于是, OK了。 

五、 几种不会产生core文件的情况说明

The core file will not be generated if

(a)    the process was set-user-ID and the current user is not the owner of the program file, or

(b)     the process was set-group-ID and the current user is not the group owner of the file,

(c)     the user does not have permission to write in the current working directory, 

(d)     the file already exists and the user does not have permission to write to it, or 

(e)     the file is too big (recall the RLIMIT_CORE limit in Section 7.11). The permissions of the core file (assuming that the file doesn’t already exist) are usually user-read and user-write, although Mac OS X sets only user-read.

六、coredump产生的几种可能情况

造成程序coredump的原因有很多,这里总结一些比较常用的经验吧:

 1,内存访问越界

  a) 由于使用错误的下标,导致数组访问越界。

  b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串没有正常的使用结束符。

  c) 使用strcpy, strcat, sprintf, strcmp,strcasecmp等字符串操作函数,将目标字符串读/写爆。应该使用strncpy, strlcpy, strncat, strlcat, snprintf, strncmp, strncasecmp等函数防止读写越界。

 2,多线程程序使用了线程不安全的函数。

 3,多线程读写的数据未加锁保护。

       对于会被多个线程同时访问的全局数据,应该注意加锁保护,否则很容易造成coredump

 4,非法指针

  a) 使用空指针

  b) 随意使用指针转换。一个指向一段内存的指针,除非确定这段内存原先就分配为某种结构或类型,或者这种结构或类型的数组,否则不要将它转换为这种结构或类型的指针,而应该将这段内存拷贝到一个这种结构或类型中,再访问这个结构或类型。这是因为如果这段内存的开始地址不是按照这种结构或类型对齐的,那么访问它时就很容易因为bus error而core dump。

 5,堆栈溢出

不要使用大的局部变量(因为局部变量都分配在栈上),这样容易造成堆栈溢出,破坏系统的栈和堆结构,导致出现莫名其妙的错误。  

七、如何判断core文件

可以通过简单的file命令进行快速判断:     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值