代码改变世界

gdb强制生成core文件

2018-04-14 18:33  youxin  阅读(7972)  评论(0编辑  收藏  举报

如何为自己的进程产生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文件进行调试了。