Search code examples
linuxgcccrashcrash-dumpscoredump

Core dump in Linux


I want to create a core dump whenever my process crashes. Currently I am following this approach:

  1. Build a special "debug" version of the program using "-g" of gcc/g++.
  2. Execute "ulimit -c unlimited"
  3. Now we get the core dump whenever the program crashes.

But I want to minimize the number of steps so that:

  • Core dump should always get created. Even if it is "release" build. The user should not be asked to execute the command "ulimit -c unlimited" manually.
  • That core dump's backtrace should be able to give the file, function, line number of the calls. That is stack trace in a human readable form.
  • I don't want to build the program as a debug build with "-g". Or at least it shouldn't contain any other debugging information which is not required to produce the human readable stack trace. Because this would be a release build of the program.

So I have two questions:

  1. How to create a core dump in the "release" build of a program?
  2. Always. Without manually executing the "ulimit -c unlimited"

Solution

  • The usual solution is to build with -g and to strip off the debug information before releasing the file. Look for the 'strip' command. You keep the file with debug information and use it to debug core dumps you get from customers.

    If you want to print the human readable backtrace on the users machine you'll need to distribute binaries with (some) debug information. Look for the 'backtrace()' function in glibc.

    Note that core dumps will be created (if ulimit is set appropriately) even if your binary doesn't contain debug information.

    The best way to ensure the creation of a core dump is probably to execute your binary from a script which sets ulimit before running the binary.