gcore 获取程序core dump file 但程序不用退出,gdb 分析core
本文包含如下部分内容
1. Linux内核里面生成的core file文件相关的代码
2. core dump file 相关的设置
3. 如何在程序中调用代码生成 core dump file,程序又不用退出。
4. 使用gdb分析 core dump file 文件
5.
1. Linux内核里面生成的core file文件相关的代码
------------------------------------------------------------------
get_signal_to_deliver
最后会调用elf_core_dump
里面有一段这样变量vma的循环,应该是保存所有内存的? 不过应该还有其他信息的
1980 2001
core dump file 相关的设置:
---------------------------------------
默认 linux 是没有dump core文件的,需要设置一下
widebright@:~/桌面$ ulimit -a
core file size
data seg size
scheduling priority
file size
pending signals
max locked memory
max memory size
open files
pipe size
POSIX message queues
real-time priority
stack size
cpu time
max user processes
virtual memory
file locks
ulimit -c 1000
ulimit -c unlimited
另有/proc/sys/kernel/core_pattern
或者在内核里面查找core_pattern变量。
----------------------------------------------
如何为自己的进程产生core 文件,又不想退出这个进程?
系统只在程序崩溃退出时自动产生core file。 有的人像自己处理异常信号,然后自己产生一个core file,然后继续运行。那该怎么办呢? 如果自己在想产生core file的时候,调用abort 函数来生成文件,core文件是生成了,但自己的进程也退出了。为了进程退出,在网上找到两个办法:
=============================================
方法一:
内存空间了,然后在子进程触发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 main(int argc,char **argv)
{
}
-------------------------------------------------------
使用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.
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
(gdb) file ./a.out
Reading symbols from /home/widebright/桌面/a.out...done.
(gdb) bt
#0
#1
#2
#3
#4
#5
#6
#7
(gdb) frame 7
#7
31
(gdb) print i
$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
1000
1000
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.
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
(gdb) file a.out
Reading symbols from /home/widebright/桌面/a.out...done.
(gdb) bt
#0
#1
#2
#3
#4
(gdb) p i
No symbol "i" in current context.
(gdb) frame 4
#4
27
(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.
(gdb) help gcore
Save a core file with the current state of the debugged process.
Argument is optional filename.
widebright@:~/桌面$ file /usr/bin/gcore
/usr/bin/gcore: POSIX shell script text executable
=============================
cat /usr/bin/gcore
#!/bin/sh
#
#
# 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.
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.
#
# 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
fi
# Need to check for -o option, but set default basename to "core".
name=core
if [ "$1" = "-o" ]
then
fi
# Create a temporary file.
tmpfile=`mktemp ${name}.XXXXXX 2>/dev/null` || {
}
trap "rm -f $tmpfile" EXIT
# Initialise return code.
rc=0
# Loop through pids
for pid in $*
do
attach $pid
gcore $name.$pid
detach
quit
EOF
done
exit $rc
========================================
更多推荐
所有评论(0)