ank
ank

Reputation: 89

Avoid dumping information in a core file

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*) in memory. I do not want it to be dumped into a core file in case of any crash. Is there any specific flag I have to use for mmap?

Upvotes: 3

Views: 2056

Answers (1)

Dolda2000
Dolda2000

Reputation: 25855

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.

Upvotes: 5

Related Questions