• 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
  • 相关阅读:
    信息学奥赛一本通 1189:Pell数列
    DES加密算法是怎么实现的?
    打造更懂投资人的发展模式 锦江酒店(中国区)属地化深耕赋能
    Android入门第42天-Android中的Service(IntentService)
    Linux用户与用户组管理
    机器学习笔记 - 使用3D卷积神经网络进行视频分类
    mysql++库connected与ping方法的区别
    【CS324】LLM(大模型的能力、数据、架构、分布式训练、微调等)
    现货黄金职业交易员怎么使用技术分析?
    [附源码]java毕业设计小区宠物管理系统
  • 原文地址:https://blog.csdn.net/robinfoxnan/article/details/125425218