Compressing the core files during core generation

前端 未结 3 1732
野性不改
野性不改 2021-02-07 22:01

Is there way to compress the core files during core dump generation?

If the storage space is limited in the system, is there a way of conserving it in case of need for c

3条回答
  •  悲&欢浪女
    2021-02-07 23:06

    As suggested by other answer, the Linux kernel /proc/sys/kernel/core_pattern file is good place to start: http://www.mjmwired.net/kernel/Documentation/sysctl/kernel.txt#141

    As documentation says you can specify the special character "|" which will tell kernel to output the file to script. As suggested you could use |/bin/gzip -1 > /var/crash/core-%t-%p-%u.gz as name, however it doesn't seem to work for me. I expect that the reason is that on my system kernel doesn't treat the > character as a output, rather it probably passes it as a parameter to gzip.

    In order to avoid this problem, like other suggested you can create your file in some location I am using /home//crash/core.sh, create it using the following command, replacing with your user. Alternatively you can also obviously change the entire path.

    echo -e '#!/bin/bash\nexec /bin/gzip -f - >"/home//crashes/core-$1-$2-$3-$4-$5.gz"' > ~/crashes/core.sh
    

    Now this script will take 5 input parameters and concatenate them and add to core-path. The full paths must be specified in the ~/crashes/core.sh. Also the location of this script can be specified. Now lets tell kernel to use tour executable with parameters when generating file:

    sudo sysctl -w kernel.core_pattern="|/home//crashes/core.sh %e %p %h %t"
    

    Again should be replaced (or entire path to match location and name of core.sh script). Next step is to crash some program, lets create example crashing cpp file:

    int main (){
        int * a = nullptr;
        int b = *a;
    }
    

    After compiling and running there are 2 options, either we will see:

    Segmentation fault (core dumped)

    Or

    Segmentation fault

    In case we see the latter, there are few possible reasons.

    1. ulimit is not set, ulimit -c should specify what is limit for cores
    2. apport or your distro core dump collector is not running, this should be investigated further
    3. there is an error in script we wrote, I suggest than checking some basic dump path to check if the other things aren't reason the below should create /tmp/core.dump:

      sudo sysctl -w kernel.core_pattern="/tmp/core.dump"
      

    I know there is already an answer for this question however it wasn't obvious for me why it isn't working "out of the box" so I wanted to summarize my findings, hope it helps someone.

提交回复
热议问题