Coredump练习

Coredump练习

  • 系统环境

    [root@localhost crash]# cat /proc/version
    Linux version 4.18.0-147.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)) #1 SMP Wed Dec 4 21:51:45 UTC 2019
    [root@localhost crash]# uname -a
    Linux localhost.localdomain 4.18.0-147.el8.x86_64 #1 SMP Wed Dec 4 21:51:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
    [root@localhost crash]# uname -r
    4.18.0-147.el8.x86_64
    

设定ulimit -c

可设定core文件的大小为unlimited或者指定的最大Size

[root@localhost crash]# ulimit -c
unlimited
[root@localhost crash]# ulimit -c 1024
[root@localhost crash]# ulimit -c
1024
[root@localhost crash]# ulimit -c 4096
[root@localhost crash]# ulimit -c
4096

ulimit -a 可以列出所有的资源限制

[root@localhost crash]# ulimit -a
core file size          (blocks, -c) 4096
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7083
max locked memory       (kbytes, -l) 16384
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) 7083
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

设定core文件的保存路径

proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。

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 添加命令名

[root@localhost kernel]# ll |grep core
-rw-r--r--. 1 root root 0 Oct 19 22:34 core_pattern
-rw-r--r--. 1 root root 0 Oct 20 00:24 core_pipe_limit
-rw-r--r--. 1 root root 0 Oct 20 00:24 core_uses_pid
[root@localhost kernel]# cat core_pattern
|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %e
[root@localhost kernel]# echo "/corefile/core-%e-%p-%t" > core_pattern
[root@localhost kernel]# cat core_pattern
/corefile/core-%e-%p-%t

生菜core的代码

#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#define CORE_SIZE   1024 * 1024 * 500
int main()
{
    struct rlimit rlmt;
    if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
        return -1;
    }  
    printf("Before set rlimit CORE dump current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);
 
    rlmt.rlim_cur = (rlim_t)CORE_SIZE;
    rlmt.rlim_max  = (rlim_t)CORE_SIZE;
 
    if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
        return -1;
    }  
 
    if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
        return -1;
    }  
    printf("After set rlimit CORE dump current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);
 
    /*测试非法内存,产生core文件*/
    int *ptr = NULL;
    *ptr = 10;
 
    return 0;
}

执行后得到

[root@localhost test_Core]# ls
a.out  main.cpp  test.cpp
[root@localhost test_Core]# ./a.out
Before set rlimit CORE dump current is:4194304, max is:4194304
After set rlimit CORE dump current is:524288000, max is:524288000
Segmentation fault (core dumped)
[root@localhost test_Core]# ls
a.out  main.cpp  test.cpp
[root@localhost test_Core]# cd /corefile/
[root@localhost corefile]# ls
core-a.out-4191-1603124814

查看core文件

[root@localhost test_Core]# readelf -h core-a.out-4191-1603124814
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              CORE (Core file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          0 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         19
  Size of section headers:           0 (bytes)
  Number of section headers:         0
  Section header string table index: 0
 
[root@localhost test_Core]# file core-a.out-4191-1603124814
core-a.out-4191-1603124814: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from './a.out', real uid: 0, effective uid: 0, real gid: 0, effective gid: 0, execfn: './a.out', platform: 'x86_64'

GDB调试core文件

[root@localhost test_Core]# mv /corefile/core-a.out-4191-1603124814  core-a.out-4191-1603124814
[root@localhost test_Core]# ls
a.out  core-a.out-4191-1603124814  main.cpp  test.cpp
[root@localhost test_Core]# gdb ./a.out core-a.out-4191-1603124814
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
 
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
[New LWP 4191]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004006e9 in main () at main.cpp:28
28          *ptr = 10;
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-72.el8_1.1.x86_64
(gdb) bt
#0  0x00000000004006e9 in main () at main.cpp:28
(gdb)
posted @ 2022-03-13 15:04  Coputing  阅读(36)  评论(0)    收藏  举报