Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 638
Sabya Avatar asked Feb 12 '10 11:02

Sabya


People also ask

Where is core dump in Linux?

By default, all core dumps are stored in /var/lib/systemd/coredump (due to Storage=external ) and they are compressed with zstd (due to Compress=yes ). Additionally, various size limits for the storage can be configured. Note: The default value for kernel. core_pattern is set in /usr/lib/sysctl.

What is the use of core dump?

A core dump is the printing or the copying to a more permanent medium (such as a hard disk ) the contents of random access memory ( RAM ) at one moment in time. One can think of it as a full-length "snapshot" of RAM. A core dump is taken mainly for the purpose of debugging a program.

What causes core dump in Linux?

Core dumps are generated when the process receives certain signals, such as SIGSEGV, which the kernels sends it when it accesses memory outside its address space. Typically that happens because of errors in how pointers are used. That means there's a bug in the program. The core dump is useful for finding the bug.


1 Answers

  • Regarding the core limit, you can do it yourself in C by calling setrlimit.
  • On a GNU (glibc) or BSD system, you can get a backtrace by calling backtrace and related system calls. You will then have to translate the function addresses into function names by running addr2line (or duplicating its functionality).
  • Just don't use -g, you can still get a backtrace (except that inlined functions will not appear).
like image 181
F'x Avatar answered Oct 07 '22 18:10

F'x