• ubuntu18/20 下如何生成core文件


    ubuntu18/20 下如何生成core文件

    一、设置

    原理:https://blog.csdn.net/Sunnyside_/article/details/118439302

    原来在ubuntu14,ubuntu16上只需要一步就能生成core文件.
    $ ulimit -c unlimited

    但是ubuntu18, ubuntu20 启用了systemd 系统管理, 想要开启生成core文件,
    需要3步, 前面2步是铺垫.

    1. 设置core 文件的格式
      gedit /etc/sysctl.conf 
    
    • 1

    在底部加上如下2行, 设置生成的core文件格式, 你也可以设置成简单格式

    #%e-execute name, %p-process %s-siganl that cause coredump
    kernel.core_pattern=core-%e-%p-%t-%s
    
    • 1
    • 2

    或者临时更改,可以直接设置

    echo "core-%e-%p-%t" > /proc/sys/kernel/core_pattern
    
    • 1
    1. 禁用apport 服务, 有这个服务运行还是不能生成core 文件
      $ sudo systemctl disable apport.service

    2. 然后就可以像以前一样打开或关闭core 文件了.
      用 $ ulimit -c unlimited 来打开core dump 文件

    使用命令查看相关信息

    unlimt -a 
    
    • 1

    参考:https://blog.csdn.net/hejinjing_tom_com/article/details/121908482

    参考1:http://t.csdn.cn/MIfw0

    更改/etc/security/limits.conf 并不起作用!!!更改profile也不起作用,真实崩溃!

    Ubuntu下图形登录后非图形登录的配置文件是不的,/etc/security/limits.conf只是在非图形登录情况下有效。

    有人说需要更改:

    图形登录情况下需要修改/etc/systemd/user.conf/etc/systemd/system.conf中如下面这行的配置项(这将处理图形登录)

    但是我发现不能使用*,需要设置为具体的用户名,ubuntu可能做的不细致,

    root               soft    core            4194304
    root               hard    core            4194304
    
    • 1
    • 2

    重启!!!!!

    ulimit -a

    core file size          (blocks, -c) 4194304
    data seg size           (kbytes, -d) unlimited
    scheduling priority             (-e) 0
    file size               (blocks, -f) unlimited
    pending signals                 (-i) 31382
    max locked memory       (kbytes, -l) 65536
    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) 31382
    virtual memory          (kbytes, -v) unlimited
    file locks                      (-x) unlimited
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    二、应用

    示例代码:

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main()
    {
        pid_t pid = fork();
        if(pid < 0)
        {
            perror("创建失败\n");
            return 0;
        }
        else if(pid == 0)
        {
            //子进程
            int * p = NULL;
            *p = 20; //解引用空指针,这里会出错
            while(1)
            {
                printf("i am child pid=[%d] ppid=[%d]\n",getpid(),getppid());
                sleep(1);
            }       
        }
        else
        {
            //父进程pid > 0
            printf("begin ---> i am father pid=[%d] ppid=[%d]\n",getpid(),getppid());
            int status;
            wait(& status); //父进程在等待子进程的退出 
    
            printf("sig_code : %d\n",status & 0x7F); //获取进程终止信号
            printf("coredump_code:%d\n",(status >> 7) & 0x1); //获取 coredump 标志位
    
            //父进程应该在等待,等待子进程退出,退出后才会进入下面这个循环 
            while(1)
            {
                printf("end ---> i am father pid=[%d] ppid=[%d]\n",getpid(),getppid());
                sleep(1);
            }
        }
        return 0;
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    编译时候需要添加调试信息

    gcc coretest.cpp -o coretest -g
    ./coretest
    ctrl + C
    
    • 1
    • 2
    • 3

    产生一个core 文件 core-coretest-2352-1655862503-11,

    需要使用gdb定位并调试:

    root@ubuntu:/robin2/tracer# gdb ./coretest core-coretest-2352-1655862503-11
    GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
    Copyright (C) 2020 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-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 ./coretest...
    [New LWP 2352]
    Core was generated by `./coretest'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x000056178e89d28e in main () at coretest.cpp:19
    19	        *p = 20; //解引用空指针,这里会出错
    (gdb) 
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    帆软的数知鸟是一个什么东西
    【Jmeter】安装配置:Jmeter 安装插件管理器 Plugins Manager
    Android 12 DreamCamera2 默认关闭HDR
    c语言小项目(排雷游戏实现)
    LLM - SFT workflow 微调工作流程
    Spring源码(十一)reflush方法的registerBeanPostProcessors方法
    更换双cut的via
    Newman+Jenkins实现接口自动化测试
    A_A03_002 51(STC)单片机程序串口烧录
    【算法与数据结构】17、LeetCode电话号码的字母组合
  • 原文地址:https://blog.csdn.net/robinfoxnan/article/details/125425218