Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on core/crash dumps programmatically

Tags:

c

linux

I know I can run "ulimit -c unlimited"

In the shell to turn on core dumps for the current user. What I am wondering is how can I do this programmatically from C?

I see there is a ulimit call, but it's been deprecated in favour of get/setrlimit. What I want to know is what is the equivalent call to setrlimit that would allow crash dumps to be generated?

like image 683
hookenz Avatar asked Nov 15 '11 22:11

hookenz


2 Answers

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/

like image 165
hookenz Avatar answered Oct 04 '22 00:10

hookenz


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

like image 20
Jigar Patel Avatar answered Oct 04 '22 00:10

Jigar Patel