《coredump问题原理探究》Linux x86版第二章coredump捕获的环境配置 转

来源:互联网 发布:libevent windows 编译 编辑:程序博客网 时间:2024/05/16 10:12

《coredump问题原理探究》Linux x86版第二章coredump捕获的环境配置

分类: coredump问题原理探究(Linux版) 3605人阅读 评论(0) 收藏 举报
c++C++linuxLinuxLINUXx86X86程序崩溃调试技术

在Linux下捕获coredump的方法,按照作用范围,分为:作用于当前shell的方法,作用于单个用户的方法,作用于系统所有用户的方法。

 

1.      作用于当前shell的方法

在当前shell设置coredump的捕获,一般是用ulimit –c 命令。这条命令的含义,可以通过man ulimit得到。

如果用ulimit –c的结果不是0,那么coredump是会捕获的。例子如下:

[plain] view plaincopy
  1. [buckxu@xuzhina 1]$ ulimit -c  
  2. unlimited  
  3. [buckxu@xuzhina 1]$ ls        
  4. xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  5. [buckxu@xuzhina 1]$ ./xuzhina_dump_c1   
  6. Segmentation fault (core dumped)  
  7. [buckxu@xuzhina 1]$ ls  
  8. core_xuzhina_dump_c1_7124  xuzhina_dump_c1  xuzhina_dump_c1.cpp  

如果当ulimit –c的结果为0,则coredump是不会捕获的。例子如下:

[plain] view plaincopy
  1.  [buckxu@xuzhina 1]$ ls  
  2. core_xuzhina_dump_c1_7124  xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  3. [buckxu@xuzhina 1]$ rm core_xuzhina_dump_c1_7124   
  4. [buckxu@xuzhina 1]$ ulimit -c 0  
  5. [buckxu@xuzhina 1]$ ulimit -c  
  6. 0  
  7. [buckxu@xuzhina 1]$ ls  
  8. xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  9. [buckxu@xuzhina 1]$ ./xuzhina_dump_c1   
  10. Segmentation fault  
  11. [buckxu@xuzhina 1]$ ls  
  12. xuzhina_dump_c1  xuzhina_dump_c1.cpp  

但当ulimit –c的结果为0,要重新设置,则有可能会遇到权限的问题

[plain] view plaincopy
  1. [buckxu@xuzhina 1]$ ulimit -c   
  2. 0  
  3. [buckxu@xuzhina 1]$ ulimit -c 10  
  4. -bash: ulimit: core file size: cannot modify limit: Operation not permitted  
  5. [buckxu@xuzhina 1]$ ulimit -c 100  
  6. -bash: ulimit: core file size: cannot modify limit: Operation not permitted  
  7. [buckxu@xuzhina 1]$ ulimit -c 1000  
  8. -bash: ulimit: core file size: cannot modify limit: Operation not permitted  

1.      作用于单个用户的方法

最简单的方法就是把ulimit–c放在用户的配置文件。不同的shell有不同的配置文件。如bash的话,用户配置文件可以是~/.bash_profile,~/.bash_login,~/.profile。可以通过man bash这个命令找到依据:

[plain] view plaincopy
  1. When  bash is invoked as an interactive login shell, or as a non-inter-  
  2.        active shell with the --login option, it first reads and executes  com-  
  3.        mands  from  the file /etc/profile, if that file exists.  After reading  
  4.        that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,  
  5.        in  that order, and reads and executes commands from the first one that  
  6.        exists and is readable.    

像本人用的是bash,那么,就在~/.bash_profile加了一行:

[plain] view plaincopy
  1. ulimit -c 10000  

然后重新登录后,都变成这样:

[plain] view plaincopy
  1. [buckxu@xuzhina ~]$ ulimit -c  
  2. 10000  

验证一下:

[plain] view plaincopy
  1. buckxu@xuzhina 1]$ ls  
  2. xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  3. [buckxu@xuzhina 1]$ ./xuzhina_dump_c1   
  4. Segmentation fault (core dumped)  
  5. [buckxu@xuzhina 1]$ ls  
  6. core_xuzhina_dump_c1_7497  xuzhina_dump_c1  xuzhina_dump_c1.cpp  

还有另外一种方法,但需要重启。例子如下:

a)用root用户登录,在/etc/security/limits.conf,增加一行:

[plain] view plaincopy
  1. buckxu           soft    core           2000  

这个文件的含义,可以通过man  limits.conf来查询。

b)重启

c)用buckxu用户登录,

[plain] view plaincopy
  1. [buckxu@xuzhina ~]$ ulimit -c  
  2. 2000  

d)用其它用户登录,

[plain] view plaincopy
  1. [allyluo@xuzhina ~]$ ulimit -c  
  2. 0  

1.      作用于系统所有用户的方法

作用于系统所有用户的方法有两种。

一种是在/etc/profile里加一行

[plain] view plaincopy
  1. ulimit –c <corefile size>  

其中corefilesize可以是任何数值或者unlimited。

 

 

 

而另外一种呢,是在/etc/security/limits.conf加上一行:

[plain] view plaincopy
  1. *               soft    core          unlimited  #indicates unlimit on corefile size  

然后重启。

可以看到所有用户的corefilesize都为unlimited了。

[plain] view plaincopy
  1. [allyluo@xuzhina ~]$ ulimit -c  
  2. unlimited  
  3.   
  4. [buckxu@xuzhina ~]$ ulimit -c  
  5. unlimited  

通过上面的方法,当程序崩溃时,就会产生coredump文件,但是文件名却是core。如果使用命令man  core就会发现corefile有一些命名规则:

[plain] view plaincopy
  1. Naming of core dump files  
  2.        By default, a core dump file is  named  core,  but  the  /proc/sys/ker-  
  3.        nel/core_pattern file (since Linux 2.6 and 2.4.21) can be set to define  
  4.        a template that is used to name core dump files.  The template can con-  
  5.        tain  % specifiers which are substituted by the following values when a  
  6.        core file is created:  
  7.   
  8.            %%  a single % character  
  9.            %p  PID of dumped process  
  10.            %u  (numeric) real UID of dumped process  
  11.            %g  (numeric) real GID of dumped process  
  12.            %s  number of signal causing dump  
  13.            %t  time of dump, expressed as seconds since the Epoch,  1970-01-01  
  14.                00:00:00 +0000 (UTC)  
  15.            %h  hostname (same as nodename returned by uname(2))  
  16.            %e  executable filename (without path prefix)  
  17.            %c  core  file  size soft resource limit of crashing process (since  
  18.                Linux 2.6.24)  

如果要马上设置命名规则,可以用root用户执行下面命令

[plain] view plaincopy
  1. echo "core-%e-%p-%u" >/proc/sys/kernel/core_pattern  

在本人机器的执行结果:

[plain] view plaincopy
  1. [buckxu@xuzhina 1]$ ls  
  2. xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  3. [buckxu@xuzhina 1]$ ./xuzhina_dump_c1   
  4. Segmentation fault (core dumped)  
  5. [buckxu@xuzhina 1]$ ls  
  6. core-xuzhina_dump_c1-1246-1000  xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  7. [buckxu@xuzhina 1]$ echo $UID  
  8. 1000  

也可以用root用户在/etc/sysctl.conf加入一行

[plain] view plaincopy
  1. kernel.core_pattern= core-%e-%p-%u  

然后执行

[plain] view plaincopy
  1. sysctl –p  

在fedora系统下,只要启动了abrtd服务。它也会设置core文件的命名。

[plain] view plaincopy
  1. [buckxu@xuzhina ~]$ ps aux|grep abrtd  
  2. root       504  0.0  0.1   5816  1136 ?        Ss   21:55   0:00 /usr/sbin/abrtd -d –s  
  3. [buckxu@xuzhina ~]$ cat /proc/sys/kernel/core_pattern   
  4. |/usr/libexec/abrt-hook-ccpp %s %c %p %u %g %t e  

产生的core文件如下:

[plain] view plaincopy
  1. [buckxu@xuzhina 1]$ ls  
  2. xuzhina_dump_c1  xuzhina_dump_c1.cpp  
  3. [buckxu@xuzhina 1]$ ./xuzhina_dump_c1   
  4. Segmentation fault (core dumped)  
  5. [buckxu@xuzhina 1]$ ls  
  6. core.961  xuzhina_dump_c1  xuzhina_dump_c1.cpp  


实际上,core文件产生得不理想。如果多个进程要产生core dump,那么,就不知道是哪一个程序产生的,往往用gdb没办法调试。调试前还得用file命令来确定一下是哪个程序产生的。

[plain] view plaincopy
  1. [buckxu@xuzhina 1]$ file core.961   
  2. core.961: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from './xuzhina_dump_c1'  

所以,在fedora系统,最好是把abrtd服务停止掉,在/etc/sysctl.conf里加上kernel.core_pattern的设置。

0 0
原创粉丝点击