3

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.

ank
  • 79
  • 7
  • 1
    None of these bits will help you filter out your buffer specifically. They can filter out *all* private memory, for instance, but then you're probably just as well off not dumping the core to begin with. – Dolda2000 Mar 17 '15 at 01:06
  • So do you mean there is no way to filter a buffer to be dumped into a core file. – ank Mar 17 '15 at 21:06
  • Not using `coredump_filter`, at least, but that's not to say that it is impossible. I have a lead that I'll write an answer about. – Dolda2000 Mar 18 '15 at 01:32
  • Would you consider the option of editing the core file after it has been created? – Dolda2000 Mar 18 '15 at 04:35

1 Answers1

4

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.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • Completely agree with you that madvise can be used with flag MADV_DONTDUMP. However, this option is available since Linux 3.4. I am using version 2.6.32. So will not be able to use this solution as well. – ank Mar 18 '15 at 04:33
  • 1
    @ank: I don't think there are any other options, though. Except patching your 2.6.32 kernel with the `MADV_DONTDUMP` code, I guess. :) – Dolda2000 Mar 18 '15 at 04:34