Turn on core/crash dumps programmatically

I found a working solution. The core files are now being created.

struct rlimit core_limit;
core_limit.rlim_cur = RLIM_INFINITY;
core_limit.rlim_max = RLIM_INFINITY;

if (setrlimit(RLIMIT_CORE, &core_limit) < 0)
    fprintf(stderr, "setrlimit: %s\nWarning: core dumps may be truncated or non-existant\n", strerror(errno));

Credit goes here: http://adamrosenfield.com/blog/2010/04/23/dumping-core/


if you want to check your current limit for your process than

struct rlimit  v;   //you can decelare any variable

getrlimit(RLIMIT_CORE, &v);

printf("softlimit=%d   hardlimit=%d  \n",v.rlim_cur,v.rlim_max);

if you want to set new limit than use below code

///////////////////// set limit //////////////////////////////

lets make it simple

struct rlimit v;
v.rlim_cur = 0 ;  //if you do not want the core dump file

/*  v.rlim_cur=RLIM_INFINITY;    //set maximum soft limit of the file(unlimited) */

v.rlim_max = RLIM_INFINITY;    //for reference to the soft limit(unlimited)

setrlimit(RLIMIT_CORE, &v);

A value of rlim_cur is between 0 and infinity means that core dumps will be generated and truncated to the specified size. This risks creating an incomplete core dump

Tags:

Linux

C