2

Tried to create a small core dump file without all the memory data dump. This question seems to have a good solution. But when I set the mask to be 0 (to exclude all the memory data), there is no core file. If I set the mask to be 0x33, then core file is produced. Any idea why?

#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.
}

UPDATE 1

To clarify, I need the core to have stack and symbols only so the core file will be small enough for me. The mask of 0 was suggested by the question quoted above.

Community
  • 1
  • 1
packetie
  • 4,839
  • 8
  • 37
  • 72

1 Answers1

0

Well, if you set all bits to zero, nothing have to be dumped, thus no core file is produced. Am I missing something?

man 5 core:

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.)

YSC
  • 38,212
  • 9
  • 96
  • 149
  • Thanks @YSC for the quote. I just need the core file to have stack and symbols only so the core file is small enough. Updated the question. Any ideas? – packetie Nov 21 '15 at 01:20
  • Then yes, I was missing something. Hope you'll find your answer. – YSC Nov 21 '15 at 01:24