转载地址: http://hi.baidu.com/widebright/item/e94ea43535711f4a3075a1d0

本文包含如下部分内容

1. Linux内核里面生成的core file文件相关的代码

2. core dump file 相关的设置

3. 如何在程序中调用代码生成 core dump file,程序又不用退出。

4. 使用gdb分析 core dump file 文件

5.  用gdb 生成core文件

1. Linux内核里面生成的core file文件相关的代码

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

get_signal_to_deliver    这里没判断是不是信号是不是要触发core dump,然后调用

do_coredump

最后会调用elf_core_dump

里面有一段这样变量vma的循环,应该是保存所有内存的? 不过应该还有其他信息的

1980   1981  for ( vma  =  first_vma ( current ,  gate_vma );  vma  !=  NULL ; 1982 vma  =  next_vma ( vma ,  gate_vma )) { 1983  struct  elf_phdr   phdr ; 1984 1985   phdr . p_type  =  PT_LOAD ; 1986   phdr . p_offset  = offset ; 1987   phdr . p_vaddr  =  vma -> vm_start ; 1988   phdr . p_paddr  = 0; 1989   phdr . p_filesz  =  vma_dump_size ( vma ,  cprm -> mm_flags ); 1990   phdr . p_memsz  =  vma -> vm_end  -  vma -> vm_start ; 1991   offset  +=  phdr . p_filesz ; 1992   phdr . p_flags  =  vma -> vm_flags  &  VM_READ  ?  PF_R  : 0; 1993  if ( vma -> vm_flags  &  VM_WRITE ) 1994   phdr . p_flags  |=  PF_W ; 1995  if ( vma -> vm_flags  &  VM_EXEC ) 1996   phdr . p_flags  |=  PF_X ; 1997   phdr . p_align  =  ELF_EXEC_PAGESIZE ; 1998 1999   size  += sizeof( phdr ); 2000  if ( size  >  cprm -> limit

2001 || !dump_write(cprm->file, &phdr, sizeof(phdr)))2002 goto end_coredump;2003 }

core dump file 相关的设置:
---------------------------------------
默认 linux 是没有dump core文件的,需要设置一下
widebright@:~/桌面$ ulimit -a
core file size          (blocks, -c) 0            >>>>>>>>>>>>>>>>>这个设为0了,不会有core dump了
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

ulimit -c 1000  最大允许core文件有1000块 ?
ulimit -c unlimited  不做限制

另有/proc/sys/kernel/core_pattern   core_pipe_limit  core_uses_pid 等文件可以控制core 文件的名字,具体可以看上面说的函数的实现。
或者在内核里面查找core_pattern变量。

----------------------------------------------
如何为自己的进程产生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.'.

widebright@:~/桌面$ file /usr/bin/gcore
/usr/bin/gcore: POSIX shell script text executable

=============================

cat /usr/bin/gcore
#!/bin/sh

#   Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
#   Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .

#
# gcore.sh
# Script to generate a core file of a running program.
# It starts up gdb, attaches to the given PID and invokes the gcore command.
#

if [ "$#" -eq "0" ]
then
    echo "usage:  gcore [-o filename] pid"
    exit 2
fi

# Need to check for -o option, but set default basename to "core".
name=core

if [ "$1" = "-o" ]
then
    if [ "$#" -lt "3" ]
    then
    # Not enough arguments.
    echo "usage:  gcore [-o filename] pid"
    exit 2
    fi
    name=$2

# Shift over to start of pid list
    shift; shift
fi

# Create a temporary file.  Use mktemp if available, but cope if it is not.
tmpfile=`mktemp ${name}.XXXXXX 2>/dev/null` || {
  tmpfile=${name}.$$
  if test -e $tmpfile; then
    echo "Could not create temporary file $tmpfile"
    exit 1
  fi
  touch $tmpfile
}
trap "rm -f $tmpfile" EXIT

# Initialise return code.
rc=0

# Loop through pids
for pid in $*
do
    # Write gdb script for pid $pid.  
    cat >>$tmpfile <<EOF
attach $pid
gcore $name.$pid
detach
quit
EOF

gdb -x $tmpfile -batch

if [ -r $name.$pid ] ; then 
        rc=0
    else
        echo gcore: failed to create $name.$pid
        rc=1
        break
    fi

done

exit $rc
========================================

gcore 获取程序core dump file 但程序不用退出,gdb 分析core相关推荐

  1. 通过gdb core dump方法查看程序异常时的堆栈信息

    在Linux下可通过core文件来获取当程序异常退出(如异常信号SIGSEGV, SIGABRT等)时的堆栈信息.core dump叫做核心转储,当程序运行过程中发生异常的那一刻的一个内存快照,操作系 ...

  2. 嵌入式linux应用程序崩溃,嵌入式Linux gdb core dump快速定位程序crash问题

    指定生成 core dump 文件: echo "/tmp/core-%e-%p-%t" > /proc/sys/kernel/core_pattern ulimit -c ...

  3. 调试技术: Linux core dump file

    1. 前言: 有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的. 但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的 ...

  4. linux core文件调试,Linux程序调试助手–core,解决段错误!

    出现问题,不知道怎么解决,出现段错误,解决不了.那试一下core文件信息吧! 采用core文件的方法 1.core文件在什么位置创建? 在进程当前工作目录的下创建.通常与程序在相同的路径下.但如果程序 ...

  5. PostgreSQL的学习心得和知识总结(二十四)|CentOS环境 配置生成coredump程序崩溃内存转储文件及gdb调试core文件

    目录结构 注:提前言明 本文借鉴了以下博主.书籍或网站的内容,其列表如下: 1.使用GDB分析core dump文件,点击前往 2.详解coredump,点击前往 3.PostgreSQL数据库仓库链 ...

  6. Core Dump核心转储

    核心转储(core dump),在汉语中有时戏称为吐核,是操作系统在进程收到某些信号而终止运行时,将此时进程地址空间的内容以及有关进程状态的其他信息写出的一个磁盘文件.这种信息往往用于调试. 概述 编 ...

  7. core dump 崩溃分析

    linux core dump 分析无调试符号 core dump 文件 linux core dump 一般称为核心转储.内核转储,统称为转储文件. 代表某个时刻.某个进程的内存信息映射.包含了生成 ...

  8. oracle dump 源码,AIX 下的 core dump 分析入门

    AIX 下的 core dump 分析入门 (), 工程师, IBM 作者毕业于中国科学技术大学,目前任职于 IBM,主要方向为 AIX 平台移植.性能优化.问题定位等. 简介: 本文简要介绍了 AI ...

  9. Core Dump流程分析

    闲话 最近分析问题时,发现我的环境中,经常有用户态进程异常退出,但是却没有core文件生成,简单看了一下相关的内核流程,mark一下. Core Dump基本原理 当应用程序在用户态发生异常时,比如常 ...

最新文章

  1. 整数划分问题(路径输出)【递归求解方式】
  2. Linux——自定义服务命令
  3. vim如何开启256色
  4. Oracle 4dae,如何处理Python-CXOracle中未知词的错误报告问题,pythoncxOracle,查询,到,生僻字,报错...
  5. php 不申明构造函数,PHP的构造函数和同类名函数同时申明时调用的情况
  6. Java计算1除以3结果为0_关于java:Int division:为什么1/3 == 0的结果?
  7. 街霸5 android,MD街头霸王5免安装版
  8. 使用DxVcl为Python的飞信库写一个简单的GUI
  9. opencv 4.5.5 imread 失败(报错)的处理方式
  10. android解析包时出现问题怎么解决方法,手机安装APP提示解析错误怎么办?解析包时出现问题如何解决?...
  11. PC端网页布局——世纪佳缘(二)页面初搭建
  12. MFC与Windows编程
  13. idea 编译通过,无法调试 Frames are not available
  14. 广东小学几年级有计算机课,广州小学开设网络班:小学生人手一台手提电脑
  15. 微型计算机原理与接口技术——8086指令系统之数据传送指令
  16. 使用matlab绘画曲线图,6.利用Matlab绘制趋势面图形.doc
  17. Linux Nginx安装配置及HTTPS配置
  18. Hex Editor实现Notepad++16进制编辑功能
  19. Hadoop基础【HDFS、Yarn、MapReduce框架概述、框架的搭建】
  20. 社会即将分层,你将会在第几层?

热门文章

  1. sqlserverSMSS登录 18456 错误
  2. python如何询问用户是否继续游戏_Python猜猜游戏如何再现
  3. Python面向对象编程(类编程)中self的含义详解(简单明了直击本质的解释)
  4. C++编程技巧:内码的转换技术
  5. Fisher精确检验.医学统计实例详解-医学统计助手★卡方检验,t检验,F检验,秩和检验,方差分析
  6. 处理 Code:516. Authentication failed: password is incorrect or there is no user with such name.
  7. npm/yarn 安装和命令
  8. 散转程序c语言,单片机 散转表程序的使用
  9. 数据库两表联查、多表联查,多重联查
  10. .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法