How to generate a core dump in Linux on a segmentation fault?

前端 未结 12 1146
余生分开走
余生分开走 2020-11-22 17:06

I have a process in Linux that\'s getting a segmentation fault. How can I tell it to generate a core dump when it fails?

相关标签:
12条回答
  • 2020-11-22 17:21

    As explained above the real question being asked here is how to enable core dumps on a system where they are not enabled. That question is answered here.

    If you've come here hoping to learn how to generate a core dump for a hung process, the answer is

    gcore <pid>
    

    if gcore is not available on your system then

    kill -ABRT <pid>
    

    Don't use kill -SEGV as that will often invoke a signal handler making it harder to diagnose the stuck process

    0 讨论(0)
  • 2020-11-22 17:26

    There are more things that may influence the generation of a core dump. I encountered these:

    • the directory for the dump must be writable. By default this is the current directory of the process, but that may be changed by setting /proc/sys/kernel/core_pattern.
    • in some conditions, the kernel value in /proc/sys/fs/suid_dumpable may prevent the core to be generated.

    There are more situations which may prevent the generation that are described in the man page - try man core.

    0 讨论(0)
  • 2020-11-22 17:30

    By default you will get a core file. Check to see that the current directory of the process is writable, or no core file will be created.

    0 讨论(0)
  • 2020-11-22 17:34

    Ubuntu 19.04

    All other answers themselves didn't help me. But the following sum up did the job

    Create ~/.config/apport/settings with the following content:

    [main]
    unpackaged=true
    

    (This tells apport to also write core dumps for custom apps)

    check: ulimit -c. If it outputs 0, fix it with

    ulimit -c unlimited
    

    Just for in case restart apport:

    sudo systemctl restart apport
    

    Crash files are now written in /var/crash/. But you cannot use them with gdb. To use them with gdb, use

    apport-unpack <location_of_report> <target_directory>
    

    Further information:

    • Some answers suggest changing core_pattern. Be aware, that that file might get overwritten by the apport service on restarting.
    • Simply stopping apport did not do the job
    • The ulimit -c value might get changed automatically while you're trying other answers of the web. Be sure to check it regularly during setting up your core dump creation.

    References:

    • https://stackoverflow.com/a/47481884/6702598
    0 讨论(0)
  • 2020-11-22 17:38

    What I did at the end was attach gdb to the process before it crashed, and then when it got the segfault I executed the generate-core-file command. That forced generation of a core dump.

    0 讨论(0)
  • 2020-11-22 17:39

    It's worth mentioning that if you have a systemd set up, then things are a little bit different. The set up typically would have the core files be piped, by means of core_pattern sysctl value, through systemd-coredump(8). The core file size rlimit would typically be configured as "unlimited" already.

    It is then possible to retrieve the core dumps using coredumpctl(1).

    The storage of core dumps, etc. is configured by coredump.conf(5). There are examples of how to get the core files in the coredumpctl man page, but in short, it would look like this:

    Find the core file:

    [vps@phoenix]~$ coredumpctl list test_me | tail -1
    Sun 2019-01-20 11:17:33 CET   16163  1224  1224  11 present /home/vps/test_me
    

    Get the core file:

    [vps@phoenix]~$ coredumpctl -o test_me.core dump 16163
    
    0 讨论(0)
提交回复
热议问题