如何为自己的进程产生core 文件,又不想退出这个进程?

系统只在程序崩溃退出时自动产生core file。 有的人像自己处理异常信号,然后自己产生一个core file,然后继续运行。那该怎么办呢? 如果自己在想产生core file的时候,调用abort 函数来生成文件,core文件是生成了,但自己的进程也退出了。为了进程退出,在网上找到两个办法:

=============================================
方法一: 先fork创建一个子进程,子进程拥有和父进程一样的
内存空间了,然后在子进程触发abort信号,让子进程进行core 
dump。 这个fork看来还比较有意思的,子进程拥有父进程的一样
的内存空间,上次才看到有人想定时存档备份进程数据时,也是想fork一个
子进程出来,说是这样父进程在备份时也不用同步等待了。子进程可以
访问父进程的内容吧。
=============================================
方法来自
http://stackoverflow.com/questions/131439/how-can-a-c-program-produce-a-core-dump-of-itself-without-terminating

#include
#include
#include
#include
#include
#include
#include
#include

#define mcrosec 1000000

void create_dump(void)
{
        int * invalid = NULL;
        if(!fork()) {
                // Crash the app in your favorite way here
                abort();  //和 kill(getpid(), SIGABRT);应该一样的
                *invalid = 42;    //应该不会到这里来了吧。
        }
}

int main(int argc,char **argv)
{
   int i =0;
   while(1){
      usleep( 2*mcrosec);
      i++;
      printf("ddd\n");
      if( i==5)    
        create_dump();
      
   }
   return 0;
}

-------------------------------------------------------

使用gdb分析一下这个core文件哈

widebright@:~/桌面$ gdb -core core 
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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 "i686-linux-gnu".
For bug reporting instructions, please see:
.
[New Thread 3320]
Core was generated by `./a.out'.
Program terminated with signal 6, Aborted.
#0  0x00982416 in __kernel_vsyscall ()
(gdb) file ./a.out 
Reading symbols from /home/widebright/桌面/a.out...done.
(gdb) bt                                         ///查出错时候的堆栈
#0  0x00982416 in __kernel_vsyscall ()
#1  0x00ec6e71 in ?? ()
#2  0x00ff8ff4 in ?? ()
#3  0x00eca34e in ?? ()
#4  0x00000006 in ?? ()
#5  0xbfa5fd80 in ?? ()
#6  0x0804845f in create_dump () at main.c:18
#7  0x0804849c in main (argc=1, argv=0xbfa5ff04) at main.c:31
(gdb) frame 7                                             //切换的调用main的调用堆栈环境上去
#7  0x0804849c in main (argc=1, argv=0xbfa5ff04) at main.c:31
31            create_dump();

(gdb) print i                        //i 等于5的时候调用的dump 呵呵
$1 = 5

=========================================
方法二:调用gcore命令为指定的进程生成core 文件
=========================================
http://forums.freebsd.org/archive/index.php/t-8268.html

char cmd[50];
sprintf(cmd, "gcore %u", getpid());
system(cmd);

-----------------------------------
widebright@:~/桌面$ ps -ef |grep a.out 
1000      3665  3546  0 10:53 pts/0    00:00:00 ./a.out
1000      3669  3665  0 10:53 pts/0    00:00:00 [a.out]
1000      3686  2937  0 10:53 pts/2    00:00:00 grep --color=auto a.out

widebright@:~/桌面$ sudo gcore 3665
[sudo] password for widebright: 
0x00c5b416 in __kernel_vsyscall ()
Saved corefile core.3665

---------------------------
widebright@:~/桌面$ gdb -core core.3665 
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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 "i686-linux-gnu".
For bug reporting instructions, please see:
.
[New Thread 3665]
Core was generated by `/home/widebright/桌面/a.out'.
#0  0x00c5b416 in __kernel_vsyscall ()
(gdb) file a.out 
Reading symbols from /home/widebright/桌面/a.out...done.
(gdb) bt
#0  0x00c5b416 in __kernel_vsyscall ()
#1  0x00d2afc0 in ?? ()
#2  0x00d5c1ac in ?? ()
#3  0xbfed8b90 in ?? ()
#4  0x08048481 in main (argc=1, argv=0xbfed8c74) at main.c:27
(gdb) p i
No symbol "i" in current context.
(gdb) frame 4
#4  0x08048481 in main (argc=1, argv=0xbfed8c74) at main.c:27
27          usleep( 2*mcrosec);
(gdb) p i
$1 = 48

=============================
在我机器上gcore命令就个shell脚本,自动生成一个gdb的脚本,attach 指定的进程,然后调用gcore这个gdb 命令生成core文件,然后detach让进程继续进行。

(gdb) help generate-core-file
Save a core file with the current state of the debugged process.
Argument is optional filename.  Default filename is 'core.'.

(gdb) help gcore
Save a core file with the current state of the debugged process.
Argument is optional filename.  Default filename is 'core.'.

如果在测试过程中遇到某个进程的CPU利用率过高或者卡死而需要去调试该进程时,可以利用gdb命令生成coredump文件,然后再去调试coredump文件来定位问题。

那么如何使用gdb生成coredump文件呢?其实步骤很简单:

1. 安装好gdb,然后使用命令 'gdb'。(假设需要调试的进程号为 21509)

2. 使用 ‘attach 21590’命令将gdb附加到进程21509上。

3. 使用‘gcore core_name’命令生成coredump文件core_name。

4. 使用‘detach’命令断开连接。

5.使用‘q’命令退出gdb。

此时,在当前目录下就会产生一个名为core_name的coredump文件。下面就可以利用gdb工具来对该coredump文件进行调试了。

gdb强制生成core文件的更多相关文章

  1. linux包之gdb之gdb命令与core文件产生

    gdb-7.2-64.el6_5.2.x86_64/usr/bin/gcore/usr/bin/gdb/usr/bin/gdb-add-index/usr/bin/gdbtui/usr/bin/gst ...

  2. Linux生成core文件、core文件路径设置

    在Linux下产生并调试core文件 先看看我用的是个什么机器: $ uname -aLinux dev 2.4.21-9.30AXsmp #1 SMP Wed May 26 23:37:09 EDT ...

  3. GDB调试之core文件(如何定位到Segment fault)

    core dump又叫核心转储,当程序运行过程中发生异常,程序异常退出时,由操作系统把程序当前的内存状况存储在一个core文件中,叫core dump.(内部实现是:linux系统中内存越界会收到SI ...

  4. Linux环境崩溃生成core文件以及调试

    Linux环境崩环境溃生成core文件以及调试 gdb结合coredump定位崩溃进程 Linux 使用core file文件快速定位程序崩溃代码行 http://www.cnblogs.com/ha ...

  5. Linux下无法生成core文件的解决办法

    1.检查ulimit [root ~]# ulimit -c 0 0:表示禁止生成core文件,此时需要执行ulimit -c unlimited(临时生效),或者在.bashrc中添加“ulimit ...

  6. Linux环境下如何生成core文件

    Linux环境下进程发生异常而挂掉,通常很难查找原因,但是一般Linux内核给我们提供的核心文件,记录了进程在崩溃时候的信息.但是生成core文件需要设置开关,具体步骤如下: 1.查看生成core文件 ...

  7. 让linux中的程序崩溃时生成core文件

    当我们的linux程序崩溃的时候,常常会有这样的提示:    Segmentation fault (core dumped)    段错误 (核心已转储)    提示说生成了core文件,但是此功能 ...

  8. gdb简单调试~core文件

    1.打开终端,进入项目目录,输入ulimit -a ,可以看core文件大小设置(第一行),若为0, 则没有打开core dump设置. 2.ulimit -c unlimited ,core文件大小 ...

  9. Linux中ulimit -c生成core文件()

    理解这六个shell脚本语言的功能 echo "kernel.core_pattern = /tmp/core-%e-%p-%t" >> /etc/sysctl.con ...

随机推荐

  1. Saltstack设置安装源为阿里源

    Saltstack设置安装源为官方源有时候在国内网络不好安装较慢或者安装不上,可设置为阿里源 比如对于 Centos 7 系统,在 saltstack 的官网提供的配置初始化手册是: sudo yum ...

  2. css学习_css盒模型及应用

    1.看透网页布局的本质 2.盒模型 margin.border.padding.width.height a. border: 1px solid red                (solid/ ...

  3. ubuntu16.04下安装opencv3.4.1及其扩展模块

    1.源文件下载 opencv-3.4.1.tar.gz(https://github.com/opencv/opencv/releases) opencv_contrib-3.4.1.tar.gz(h ...

  4. [No0000EC]C# 字符串(String)

    在 C# 中,可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量.string 关键字是 System.String 类的别名. 创建 String 对象 ...

  5. mysql帮助命令

    HELP contents 查看MySQL命令的使用. eg: HELP 'Data Type' 查看所有的数据类型的使用方法.

  6. [DPDK] 转发 DPDK分析

    前半部分,对于背景和需求的分析,写的相当好啊! 原文地址:https://www.jianshu.com/p/0ff8cb4deaef 背景分析 前10年中,网络程序性能优化的目标主要是为了解决C10 ...

  7. [daily][centos][sudo] sudo 报错

    有时候, 比如在CentOS 6上. sudo 会报如下错误: sudo: must be setuid root 这是因为, sudo 命令, 没有SUID, [root@T209 ~]# ll / ...

  8. visual stodio 编译前后动作定制总结

    copy "$(TargetDir)$(TargetName).lib" ..\lib\deploy\$(TargetName).lib 编译完成后将一个.lib 文件拷贝到指定目 ...

  9. 用Pyinstaller 实现py.转化为exe可执行文件----二维码实例

    1,安装 Pyinstaller 命令提示符窗口:pip install pyinstaller 2,制作二维码脚本 d5_code.py from MyQR import myqr #生成二维码 w ...

  10. Java如何对List集合的操作方法(一)

    目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...