Avoid dumping information in a core file

霸气de小男生 提交于 2021-01-28 02:05:25

问题


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.


回答1:


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 mallocing it, so that it is alone in a page.



来源:https://stackoverflow.com/questions/29089633/avoid-dumping-information-in-a-core-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!