在核心生成期间压缩核心文件

发布于 2024-10-18 11:50:14 字数 115 浏览 1 评论 0 原文

有没有办法在核心转储生成期间压缩核心文件?

如果系统中的存储空间有限,是否有办法在需要立即压缩生成核心转储时节省空间?

理想情况下,该方法适用于旧版本的 Linux,例如 2.6.x。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-10-25 11:50:14

Linux 内核 /proc/sys/kernel/core_pattern 文件将执行您想要的操作:http://www.mjmwired.net/kernel/Documentation/sysctl/kernel.txt#191

将文件名设置为类似 |/bin/gzip -1 > /var/crash/core-%t-%p-%u.gz 并且您的核心文件应该为您压缩保存。

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

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.

美羊羊 2024-10-25 11:50:14

对于嵌入式 Linux 系统,以下脚本更改完美地可以通过 2 个步骤生成压缩核心文件

第 1 步:创建脚本

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

步骤2:更新核心模式文件

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

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

情绪失控 2024-10-25 11:50:14

正如其他答案所建议的,Linux内核 /proc/sys/kernel/core_pattern 文件是一个很好的起点: http://www.mjmwired.net/kernel/Documentation/sysctl/kernel.txt#141

正如文档所说,您可以指定特殊字符“|”这将告诉内核将文件输出到脚本。根据建议,您可以使用 |/bin/gzip -1 > /var/crash/core-%t-%p-%u.gz 作为名称,但它似乎对我不起作用。我认为原因是在我的系统内核上不处理 >字符作为输出,而是可能将其作为参数传递给 gzip。

为了避免这个问题,像其他建议一样,您可以在我使用的某个位置创建文件 /home//crash/core.sh,使用以下命令创建它,并替换为您的用户。或者,您显然也可以更改整个路径。

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

现在这个脚本将接受 5 个输入参数并将它们连接起来并添加到 core-path。必须在 ~/crashes/core.sh 中指定完整路径。还可以指定该脚本的位置。现在让我们告诉内核在生成文件时使用带参数的tour可执行文件:

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

再次应该替换(或匹配 core.sh 脚本的位置和名称的整个路径)。下一步是崩溃一些程序,让我们创建崩溃 cpp 文件的示例:

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

编译并运行后有 2 个选项,我们将看到:

分段错误(核心转储)

分段错误

如果我们看到后者,可能的原因很少。

  1. ulimit 未设置,ulimit -c 应指定核心 apport 的限制
  2. ,或者您的发行版核心转储收集器未运行,应进一步调查
  3. 我们编写的脚本中有错误,我建议检查一些基本转储检查其他原因是否不是下面应该创建 /tmp/core.dump 的路径:

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

我知道有已经是这个问题的答案,但对我来说并不清楚为什么它不能“开箱即用”,所以我想总结一下我的发现,希望它对某人有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文