Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing the core files during core generation

Tags:

linux

kernel

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 core dump generation with immediate compression?

Ideally the method would work on older versions of linux such as 2.6.x.

like image 385
user626146 Avatar asked Feb 21 '11 07:02

user626146


3 Answers

For an embedded Linux systems, following script change perfectly works to generate compressed core files in 2 steps

step 1: create a script

touch /bin/gen_compress_core.sh
chmod +x /bin/gen_compress_core.sh

cat > /bin/gen_compress_core.sh #!/bin/sh exec /bin/gzip -f - >"/var/core/core-$1.$2.gz"
ctrl +d

step 2: update the core pattern file

cat > /proc/sys/kernel/core_pattern |/bin/gen_compress_core.sh %e %p ctrl+d

like image 188
lakumalla Avatar answered Oct 06 '22 00:10

lakumalla


The Linux kernel /proc/sys/kernel/core_pattern file will do what you want: http://www.mjmwired.net/kernel/Documentation/sysctl/kernel.txt#141

Set the filename to something like |/bin/gzip -1 > /var/crash/core-%t-%p-%u.gz and your core files should be saved compressed for you.

like image 21
sarnold Avatar answered Oct 06 '22 00:10

sarnold


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/<username>/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/<username>/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.

like image 22
cerkiewny Avatar answered Oct 05 '22 23:10

cerkiewny