gdb强制生成core文件
如何为自己的进程产生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文件的更多相关文章
- 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 ...
- Linux生成core文件、core文件路径设置
在Linux下产生并调试core文件 先看看我用的是个什么机器: $ uname -aLinux dev 2.4.21-9.30AXsmp #1 SMP Wed May 26 23:37:09 EDT ...
- GDB调试之core文件(如何定位到Segment fault)
core dump又叫核心转储,当程序运行过程中发生异常,程序异常退出时,由操作系统把程序当前的内存状况存储在一个core文件中,叫core dump.(内部实现是:linux系统中内存越界会收到SI ...
- Linux环境崩溃生成core文件以及调试
Linux环境崩环境溃生成core文件以及调试 gdb结合coredump定位崩溃进程 Linux 使用core file文件快速定位程序崩溃代码行 http://www.cnblogs.com/ha ...
- Linux下无法生成core文件的解决办法
1.检查ulimit [root ~]# ulimit -c 0 0:表示禁止生成core文件,此时需要执行ulimit -c unlimited(临时生效),或者在.bashrc中添加“ulimit ...
- Linux环境下如何生成core文件
Linux环境下进程发生异常而挂掉,通常很难查找原因,但是一般Linux内核给我们提供的核心文件,记录了进程在崩溃时候的信息.但是生成core文件需要设置开关,具体步骤如下: 1.查看生成core文件 ...
- 让linux中的程序崩溃时生成core文件
当我们的linux程序崩溃的时候,常常会有这样的提示: Segmentation fault (core dumped) 段错误 (核心已转储) 提示说生成了core文件,但是此功能 ...
- gdb简单调试~core文件
1.打开终端,进入项目目录,输入ulimit -a ,可以看core文件大小设置(第一行),若为0, 则没有打开core dump设置. 2.ulimit -c unlimited ,core文件大小 ...
- Linux中ulimit -c生成core文件()
理解这六个shell脚本语言的功能 echo "kernel.core_pattern = /tmp/core-%e-%p-%t" >> /etc/sysctl.con ...
随机推荐
- bytes和str的区别与转换
bytes和str的区别 1.英文 b'alex'的表现形式与str没什么两样 2.中文 b'\xe4\xb8\xad'这是一个汉字在utf-8的bytes表现形式 3.中文 b'\xce\xd2'这 ...
- take a cpu core offline
[root@vrouter1 ~]# cat /sys/devices/system/cpu/online -,,- [root@vrouter1 ~]# cat /sys/devices/syste ...
- [development][vim] vim显示空白字符
1. 作为一个严谨的程序员,你必须关心你敲下过的没一个字符.其中包括空白字符. 2. 有时候你需要review别人的代码,对于哪些肆意使用tab,space,enter的人.你怎么发现那些被他们留下的 ...
- python多线程相关知识点
1. 信号量 信号机维护着一个计数器,指定可同时访问资源或者进入临界区的线程数.每次有一个线程获得信号机时,计数器-1.若计数器为0,其他线程就停止访问信号机 Semphore,是一种带计数的线程同步 ...
- 主备归档不一致导致的RMAN-08137无法清理归档解决方案
值班夜里接到归档目录满的告警,执行删除脚本发现报错 RMAN-08137: WARNING: archived log not deleted, needed for standby or upstr ...
- LeetCode 892 Surface Area of 3D Shapes 解题报告
题目要求 On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of ...
- eclipse几种常见问题的解决
build项目时出现卡死现象的解决方案 场景:在使用使用Eclipse编辑文件保存时或者build项目时,经常出现卡死现象,此时即便杀死eclipse进程重启还是依然出现这种现象. 原因:eclips ...
- 通过pytty工具代理连接数据库mysql(绕开数据库白名单限制)
1.下载putty在本机,不用安装. 2.数据库地址及端口,输入服务器账户.密码登录. 输入数据库地址.端口及本机映射端口 输入服务器账户.密码登录 然后本地新建数据库连接就可以了
- java JDBC (五) properties配置文件
1.在src目录下创建文件 database.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://192.168.0.207:33 ...
- 陌生的 metaclass(转)
add by zhj:这是我见过的对metaclass解释最清楚的文章了,例子很好,真是一例胜千言 原文:http://wiki.jikexueyuan.com/project/explore-pyt ...