gpt4 book ai didi

c++ - 掩码设置为 0 时不生成核心文件

转载 作者:太空狗 更新时间:2023-10-29 12:43:31 25 4
gpt4 key购买 nike

试图创建一个没有所有内存数据转储的小型核心转储文件。这question似乎有一个很好的解决方案。但是当我设置掩码为0(排除所有内存数据)时,没有核心文件。如果我将掩码设置为 0x33,则会生成核心文件。知道为什么吗?

#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>


void baz() {
int *foo = (int*)-1; // make a bad pointer
printf("%d\n", *foo); // causes segfault
}

void bar() { baz(); }
void foo() { bar(); }


int main(int argc, char **argv) {
FILE *fp = fopen("/proc/self/coredump_filter", "wb");
if (!fp) {
printf("Failed to open /proc/self/coredump_filter\n"); exit(0);
}
int mask = 0;
fprintf(fp, "%08x\n", mask);
fclose(fp);
foo(); // this will call foo, bar, and baz. baz segfaults.
}

更新 1

澄清一下,我只需要核心文件有堆栈和符号,这样核心文件对我来说就足够小了。上面引用的问题建议使用 0 掩码。

最佳答案

好吧,如果您将所有位都设置为零,则无需转储任何内容,因此不会生成核心文件。我错过了什么吗?

man 5 核心:

Controlling which mappings are written to the core dump

Since kernel 2.6.23, the Linux-specific /proc/PID/coredump_filter file can be used to control which memory segments are written to the core dump file in the event that a core dump is performed for the process with the corresponding process ID.

The value in the file is a bit mask of memory mapping types (see mmap(2)). If a bit is set in the mask, then memory mappings of the corresponding type are dumped; otherwise they are not dumped. The bits in this file have the following meanings:

bit 0  Dump anonymous private mappings.
bit 1 Dump anonymous shared mappings.
bit 2 Dump file-backed private mappings.
bit 3 Dump file-backed shared mappings.
bit 4 (since Linux 2.6.24)
Dump ELF headers.
bit 5 (since Linux 2.6.28)
Dump private huge pages.
bit 6 (since Linux 2.6.28)
Dump shared huge pages.

By default, the following bits are set: 0, 1, 4 (if the CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS kernel configuration option is enabled), and 5. This default can be modified at boot time using the coredump_filter boot option.

The value of this file is displayed in hexadecimal. (The default value is thus displayed as 33.)

关于c++ - 掩码设置为 0 时不生成核心文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33838346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com