本文介绍了避免将信息转储到核心文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在发生任何崩溃时,我想避免将某些信息从程序中转储到核心文件中.
为此,我可以使用coredump_filter( http://man7.org/linux/man-pages/man5/core.5.html )
手册页提供了以下说明
文件中的值是内存映射类型的位掩码(请参见mmap(2)).如果在掩码中设置了一位,则相应的类型被转储;否则,它们不会被丢弃.这该文件中的位具有以下含义:
bit 0转储匿名专用映射.bit 1转储匿名共享映射.bit 2转储文件支持的专用映射.bit 3转储文件支持的共享映射.位4(从Linux 2.6.24开始)转储ELF标头.bit 5(自Linux 2.6.28起)转储私人大页面.位6(从Linux 2.6.28开始)转储共享大页面.
我想知道在我的情况下要设置和重置的位.我不清楚这些字段是专用还是共享的.
我在内存中有一个缓冲区(无符号char *).如果发生任何崩溃,我不想将其转储到核心文件中.我必须为mmap使用任何特定的标志吗?请帮忙.预先感谢.
解决方案
coredump_filter
仅会设置进程全局设置,因此基本上只允许您转储全部内存或不保存任何内存.
但是, madvise
有一个标记可能会更接近您想要的标记: MADV_DONTDUMP
.它将特定的内存页面标记为不出现在coredump中.但是,您的程序将需要运行 madvise
本身.您不能从进程外部进行设置(我想除了使用 gdb
之外).
请注意, madvise
仅可在整个页面上运行.您不能将标志设置为这193个字节"或不被转储的标志.如果标记缓冲区所在的页面,则该页面的其余部分也不会被转储.如果这对您来说是个问题,我认为您只需要在缓冲区中 mmap
而不是 malloc
即可,这样它就可以单独出现在页面中了./p>
I want to avoid dumping certain information from my program into a core file in case of any crash.
For that, I can use coredump_filter (http://man7.org/linux/man-pages/man5/core.5.html)
The man page provides following description
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.
I am looking to know which bit to set and reset in my case. I am not clear with these fields specially private and shared.
I have a buffer (unsigned char*) into memory. I do not want to dumped this into a core file in case of any crash. Is there any specific flag I have to use for mmap? Please help. Thanks in advance.
解决方案
coredump_filter
will only set process-global settings, so it will only allow you to dump all memory or none, basically.
However, there is a flag to madvise
which probably does something closer to what you want: MADV_DONTDUMP
. It will flag specific memory pages for not appearing in the coredump. Your program will need to run madvise
itself, though; you can't set it from outside the process (except by using gdb
, I guess).
Do note that madvise
only operates on entire pages, however. You can't set flag "these 193 bytes" or somesuch to not be dumped. If you flag the page your buffer is in, then the rest of that same page won't be dumped either. If this is a problem for you, I think you'll just have to mmap
in your buffer instead of malloc
ing it, so that it is alone in a page.
这篇关于避免将信息转储到核心文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!