[monitor] 5. Linux core dump(进程临终快照)

1、Core dump概念

linux下一个用户态程序出错通常会报出“Segmentation fault”并退出。如果系统使能了“core dump”功能,系统会给崩溃的进程拍一个临终快照存储到一个core文件当中,快照中包括进程所有的内存、cpu寄存器、os状态标志等等。程序出故障以后,可以使用gdb来调试快照文件,定位程序当时出现了什么故障。

Core dump的说明:

A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. The name comes from the once-standard memory technology core memory. Core dumps are often used to diagnose or debug errors in computer programs.

On many operating systems, a fatal error in a program automatically triggers a core dump, and by extension the phrase “to dump core” has come to mean, in many cases, any fatal error, regardless of whether a record of the program memory is created.

快照文件是一个core类型的elf文件,elf专门为这种类型的文件定义了一种类型:

1
2

2、Core dump的使能

使用“ulimit -a”查看本系统coredump的配置情况:

3

“Core file size”这一行指定了core dump文件的最大大小。使用“ulimit –c xxx”指定core dump文件的大小限制,“ulimit –c unlimited”配置为无限大。

Core dump文件生成的路径和格式在“/proc/sys/kernel/core_pattern”文件中指定:

4

3、Core dump的使用

  • 第一步、编写一个会出错的用户程序,并编译出带调试信息的可执行文件。

    5

  • 第二步、运行会出错的可执行文件,生成core dump文件。

6

运行出错提示“Segmentation fault (core dumped)”,在“/proc/sys/kernel/core_pattern”定义的路径“/usr/local/cdp/core-%e-%p-%t”下面可以看到新生成的core文件“core-test_coredump-22614-1353319306”

  • 第三步、使用gdb调试core文件。

把源代码c文件、带调试信息的可执行文件、core文件拷贝到同一目录中,使用“gdb xxxexe xxxcore”命令,就可以调试出错当时的场景。

7

posted @ 2017-10-13 14:07  pwl999  阅读(64)  评论(0)    收藏  举报