zoukankan      html  css  js  c++  java
  • Linux下如何生成core dump文件

    转载http://blog.csdn.net/li_yang98/article/details/3261211

    使用C/C++语言开发程序时,当程序crash的时候产生core dump文件对于调试程序是很有帮助的。在Redhat Linux系统中默认是不生成core dump文件的,这是因为在/etc/profile文件中有这样一行

                     ulimit -S -c 0 > /dev/null 2>&1


    如何打开core dump呢?最简单的方法是用户在自己的~/.bash_profile中加入ulimit -S -c unlimited > /dev/null 2>&1,这样设置后允许当前用户生成没有大小限制的core dump文件。此外还有两种系统级修改生成core dump的方法。

    第一种方法是修改/etc/profile,把ulimit那一行改为

    #vim /etc/profile

    #ulimit -S -c unlimited > /dev/null 2>&1

    这样设置后系统允许所有用户生成没有大小限制的core dump文件。这样做的优点是不需要重起系统,缺点是无法控制只让某些用户生成core dump文件。

    第二种方法是修改/etc/security/limits.conf文件。很多系统上限都可以通过修改这个文件改变,如最大子进程个数,最大打开文件数等等。这个文件有详细的注释,对如何修改这个文件做了说明。如果想对所有用户打开core dump,可以加入一行

    * soft core 0

    如果只想对某些用户或用户组打开core dump,可以加入

    user soft core 0或@group soft core 0

    注意如果通过修改/etc/security/limits.conf文件打开core dump,还需要注释掉/etc/profile中的ulmit那一行

    #ulimit -S -c 0 > /dev/null 2>&1
    这样修改的优点是可以针对特定用户或特定组打开core dump文件,缺点是需要重起系统。

    最后说一下生成core dump文件的位置,默认位置与可执行程序在同一目录下,文件名是core.***,其中***是一个数字。core dump文件名的模式保存在/proc/sys/kernel/core_pattern中,缺省值是core。通过以下命令可以更改core dump文件的位置(如希望生成到/tmp/cores目录下)
    echo "/tmp/cores/core" > /proc/sys/kernel/core_pattern
     
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <sys/types.h>
     5 #include <fcntl.h>
     6 #include <dirent.h>
     7 #include <unistd.h>
     8 
     9 int main(int argc, char** argv)
    10 {
    11     int* p;
    12     int pid;
    13 
    14     pid = getpid();
    15     printf("%d\n", pid);
    16 
    17     scanf("%d", p);
    18 
    19     return 0;
    20 }

    #gcc main.c -o app
    # gcc main.c -o app
    # ./app
    7907
    5557
    Segmentation fault(core dumped)
    # ls core.7907
    core.7907
    #

     core.xxx中的xxx表示的是进程号。
  • 相关阅读:
    SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)
    麦咖啡导致电脑不能上网
    SharePoint 2013 Central Admin 不能打开
    SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)
    SharePoint 2013 APP 开发示例 系列
    synthesize(合成) keyword in IOS
    Git Cmd
    简单的正则匹配
    Dropbox
    SQL Server Replication
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3038990.html
Copyright © 2011-2022 走看看