C and core dumps.

In the U.S.S. Enterprise, warp cores tend to “dump” a lot (alright… it gets ejected – but dump, eject, jettison, all mean roughly the same thing!). But those are warp cores, and vastly different to the cores you find on Unix systems. When I first learned to program C on Unix, core dumps happened quite often when there was some sort of program crash. The term “core” is attributed to the magnetic core memory found in computing systems of the 1950s to 1970s. Core dumps are triggered by a fatal error of some sort, and effectively are a dump of the programs memory “space” into a file. On early systems, we compiled code in /tmp to avoid running out of quota space, because /tmp was *huge*.

Here is a piece of code which creates a core dump:

</pre>
#include <stdio.h>

int main(void)
{
    char *seg_flt = "winter";
    *seg_flt = 'X';

    return 0;
}

If your system does not create a core dump (and by default most don’t these days), you’ll have to activate it. In Bash Shell on a Raspberry Pi, this involves enabling “ulimit -c unlimited“. If the above code is then compiled and executed, a “Segmentation fault (core dumped)” message is indicated to the user. The string literal “winter” is stored in read-only memory, and the variable set_flt is set to point to the string. When an attempt is made to write the character X to the variable, a segmentation fault occurs. Looking at the file core:

-rw------- 1 pi pi 217088 Nov 25 00:01 core

What can be done with core dumps? They can be analyzed using gdb, but it is not for the faint-hearted.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s