coredump

摘要:
核心文件只是一个内存映像,主要用于调试。

1. core文件的简单介绍

在一个程序崩溃时,它一般会在指定目录下生成一个core文件。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。

2. 开启或关闭core文件的生成

用以下命令来阻止系统生成core文件:ulimit -c 0下面的命令可以检查生成core文件的选项是否打开:ulimit -a该命令将显示所有的用户定制,其中选项-a代表“all”。也可以修改系统文件来调整core选项在/etc/profile通常会有这样一句话来禁止产生core文件,通常这种设置是合理的:# No core files by defaultulimit -S -c 0 > /dev/null 2>&1但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开core文件产生的设置在用户的~/.bash_profile里加上ulimit -c unlimited来让特定的用户可以产生core文件如果ulimit -c 0则也是禁止产生core文件,而ulimit -c 1024则限制产生的core文件的大小不能超过1024kb

3. 设置Core Dump的核心转储文件目录和命名规则

/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名,比如原来文件内容是core-%e可以这样修改:echo "/corefile/core-%e-%p-%t" > core_pattern将会控制所产生的core文件会存放到/corefile目录下,产生的文件名为core-命令名-pid-时间戳以下是参数列表:%p - insert pid into filename 添加pid%u - insert current uid into filename 添加当前uid%g - insert current gid into filename 添加当前gid%s - insert signal that caused the coredump into the filename 添加导致产生core的信号%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间%h - insert hostname where the coredump happened into filename 添加主机名%e - insert coredumping executable name into filename 添加命令名

4. 使用core文件

在core文件所在目录下键入:gdb -c core它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等如果你已经知道是由什么程序生成此core文件的,比如MyServer崩溃了生成core.12345,那么用此指令调试:gdb -c core MyServer以下怎么办就该去学习gdb的使用了

5. 一个小方法来测试产生core文件

[root@ys hello]# ulimit -c 0

[root@ys hello]# ulimit -a

core file size        (blocks, -c) 0

data seg size         (kbytes, -d) unlimited

file size             (blocks, -f) unlimited

max locked memory     (kbytes, -l) unlimited

max memory size       (kbytes, -m) unlimited

open files                    (-n) 1024

pipe size          (512 bytes, -p) 8

stack size            (kbytes, -s) 8192

cpu time             (seconds, -t) unlimited

max user processes            (-u) 4096

virtual memory        (kbytes, -v) unlimited

[root@ys hello]# ulimit -c 1024

[root@ys hello]# ulimit -a

core file size        (blocks, -c) 1024

data seg size         (kbytes, -d) unlimited

file size             (blocks, -f) unlimited

max locked memory     (kbytes, -l) unlimited

max memory size       (kbytes, -m) unlimited

open files                    (-n) 1024

pipe size          (512 bytes, -p) 8

stack size            (kbytes, -s) 8192

cpu time             (seconds, -t) unlimited

max user processes            (-u) 4096

virtual memory        (kbytes, -v) unlimited

[root@ys hello]# cat test.c

#include <stdio.h>

const char *str = "test";

void core_test(){

    str[1] = 'T';

}

int main(){

    core_test();

    return 0;

}

[root@ys hello]# gcc -g test.c -o test

test.c: In function `core_test':

test.c:4: warning: assignment of read-only location

[root@ys hello]# ./test

段错误 (core dumped)

[root@ys hello]# ls

core.2795  test  test.c

[root@ys hello]# gdb test core.2795

GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)

Copyright 2003 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB.  Type "show warranty" for details.

This GDB was configured as "i386-redhat-linux-gnu"...

Core was generated by `./test'.

Program terminated with signal 11, Segmentation fault.

Readingsymbols from /lib/tls/libc.so.6...done.

Loaded symbols for /lib/tls/libc.so.6

Readingsymbols from /lib/ld-linux.so.2...done.

Loaded symbols for /lib/ld-linux.so.2

#0  0x080482fd in core_test () at test.c:4

4           str[1] = 'T';

(gdb) where

#0  0x080482fd in core_test () at test.c:4

#1  0x08048317 in main () at test.c:8

#2  0x42015574 in __libc_start_main () from /lib/tls/libc.so.6

(gdb) q

免责声明:文章转载自《coredump》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇智选物流-API接口平台(淘宝菜鸟、快递鸟)解决element-ui 中upload组件使用多个时无法绑定对应的元素下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

由pthread库版本不一致导致的段错误

前几天工作中遇到一个奇怪的问题,程序编译好之后一运行,就发生 segmentation fault. 另一个奇怪的问题是,删掉部分无用的代码(至少在程序启动时不会被调用),编译出来的程序稍微小了一点,就可以运行了。 发生 Segmentation fault 的程序,写在 main() 函数内的 log 都没有打印出来,因此断定是库的问题,但要跟踪确定问题...

制作C/C++动态链接库(dll)若干注意事项

一、CC++ 运行时库编译选项简单说明 问题:我的dll别人没法用 运行时库是个很复杂的东西,作为开发过程中dll制作需要了解的一部分,这里主要简单介绍一下如何选择编译选项。 在我们的开发过程中时常会遇到这样的问题: 1. 我的VS版本比较高(比如:VS2012),我想制作一个dll,封装了几个函数给别人用。 2. 打包后发现我的dll引用了msvcr1...

VS2015配置OpenCV,使用mfc摄像头程序测试

转自:https://blog.csdn.net/Lee_Dk/article/details/80466523 这只是介绍了如何加入OpenCV,怎么查找OpenCV请看出处。  新建一个项目。找到属性管理器,debug-win32。后4个是原本就有的。Micross.Cpp.Win32.user是vs的默认属性表,如果在这个表中配置了,以后的项目都不用...

Linux-019-Centos Shell 安装 Nginx 后启动时提示找不到Lua模块的libluajit-5.1.so.2文件,具体提示信息:./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

Centos Shell 安装后启动时提示找不到 lua 模块的 libluajit-5.1.so.2 文件,具体提示信息:./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or di...

ASP.NET Core MVC中Hangfire及EF Core的简单使用

项目中可能要实现定时读写数据,所以了解了一下Hangfire,并简单尝试使用,同时又实践了一波EF Core的DB First模式 Hangfire 1.新建ASP.NET Core项目 2.Nuget安装Hangfire的包,因为我Hangfire配置用数据库使用的PostgreSql,所以添加的pg相关的引用,Hangfire官方支持SQL Serve...

QT 添加 lib库

QT 添加 lib库扒自网友文章: 一.添加第三方的头文件 首先,添加头文件  #include "ControlCAN.h" 然后,再将这个头文件放到工程的目录下  二.添加.lib文件 首先,将.lib文件放到对应的工程目录下(当然,放到其他路径也可以)。 但是,仅仅做这一步是不行的,工程不会主动去包含这个.lib文件,你必须告诉他,lib文件在哪里,...