ubuntu18/20 下如何生成core文件

34 篇文章 0 订阅

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 

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

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

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

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

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

使用命令查看相关信息

unlimt -a 

参考: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

重启!!!!!

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


二、应用

示例代码:

#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;
}



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

gcc coretest.cpp -o coretest -g
./coretest
ctrl + C

产生一个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) 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值