如何为自己的进程产生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. 质量团队在VUCA时代如何走?

    如今,VUCA时代已到来.在VUCA时代(易变性volatility.不确定性uncertainty.复杂性complexity.模糊性ambiguity),面对外部环境的复杂和不确定性,测试圈是否已 ...

  2. 深度学习模型融合stacking

    当你的深度学习模型变得很多时,选一个确定的模型也是一个头痛的问题.或者你可以把他们都用起来,就进行模型融合.我主要使用stacking和blend方法.先把代码贴出来,大家可以看一下. import ...

  3. xcode工程编译错误:一般错误总结

    1.Apple LLVM 8.0 Error Group /’all-product-headers.yaml’ not found 最近升级了xcode打包后出现了个BUG,记录解决的方法. 现象: ...

  4. 使用UMDH查找内存泄露

    参考文献: 1.http://blog.csdn.net/wcjy07220114/article/details/6962140 2.http://blog.csdn.net/chenyujing1 ...

  5. AndroidStudio_ListView

    在这里梳理一下ListView的用法: 1.建立一个activity,例如建立一个ListViewActivity,这时将生成两个文件:ListViewActivity.java和activity_l ...

  6. LeetCode 976 Largest Perimeter Triangle 解题报告

    题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  7. winform窗体启动过程

    窗体启动执行顺序:FormShowFormPaintFormActivateFormResize 关闭窗体过程FormCloseFormDestroy 最小化再最大化:FormPaintFormRes ...

  8. mysql windows开启客户端连接权限

    use mysql;  select 'host' from user where user='root';  update user set host = '%' where user ='root ...

  9. Orchard Core 版本冲突 The type 'FormTagHelper' exists in both 'Microsoft.AspNetCore.Mvc.TagHelpers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and...

    最近老大让我看Orchard Core,这是一个CMS系统.可以先参考大佬的文章:https://www.cnblogs.com/shanyou/archive/2018/09/25/9700422. ...

  10. JavaScript substr() 字符串截取函数使用详解

    substr 定义和用法 substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符. 语法 stringObject.substr(start,length) 如果 length ...