Linux code generation Core file

Source: Internet
Author: User

We all know that in a Linux environment, the ulimit-c Size command makes it easy to turn the Coredump function on or off, creating a core file for easy debugging. But many people may not know how to build a core file with code control. It's really easy for us to look at it together.

The first thing you need to know about two functions:

       int getrlimit (int resource, struct rlimit *rlim);       int setrlimit (int resource, const struct RLIMIT *rlim);

These two functions can set some of the system's resources, such as the number of open processes, the number of files, the size of the resulting core file, and so on.

For the build core file that we care about, we need to set the first parameter, int resource, to Rlimit_core. This makes it easy to control the resulting core size by setting it in the second parameter.

The sample code is as follows:

Test.h file:

#include <sys/time.h> #include <sys/resource.h>int set_core (int core_size) {struct Rlimit rlim;   Rlim.rlim_cur = core_size;  Rlim.rlim_max = core_size;  int ret = Setrlimit (Rlimit_core, &rlim); if (ret! = 0) {printf ("Setrlimit | Set core size failed, ret=%d\n ", ret); return ret;} printf ("Setrlimit | Set core size successfully, ret=%d, core_size=%d\n ", ret, core_size); return ret;}  int Get_core (int* limit_cur, int* limit_max) {struct Rlimit rlim;  int ret = Getrlimit (Rlimit_core, &rlim); if (ret! = 0) {printf ("Getrlimit | Get core size failed, ret=%d\n ", ret); return ret;} printf ("Getrlimit | Get core size successfully, ret=%d, Limit_cur:%lu, Limit_max:%lu \ n ", ret, Rlim.rlim_cur, Rlim.rlim_max); *limit_cur = (int) rlim.rlim_cur; *limit_max = (int) rlim.rlim_max;return ret;}  int Test_core () {int set_core_size = 1024;  int get_cur = 0;  int Get_max = 0;  Get_core (&get_cur, &get_max); Set_core (set_core_size);   Get_core (&get_cur, &get_max); Return 0;} 
Test.cpp file:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include "test.h" int main () {     test_ Core ();     int* p = NULL;     *p = 123;      return 0;}


Output:

[Email protected]:/home/demo_test#./test
Getrlimit | Get core size successfully, Ret=0, limit_cur:20480, limit_max:20480
Setrlimit | Set core size successfully, Ret=0, core_size=1024
Getrlimit | Get core size successfully, Ret=0, limit_cur:1024, limit_max:1024
Segment Error (Core has been dumped)

Generated files:

[Email protected]:/home/demo_test# ll
Ls:3????? 10496
-rw-r-----1 root root 10723328 April 19:02 Core
-rwxr--r--1 root root 22 April 15:27 make.sh
-rwxr-xr-x 1 root root 8748 April 19:24 test
-rw-r--r--1 root root 276 April 19:31 test.cpp
-rw-r--r--1 root root 1389 April 16:06 test.h


As we can see, the core file is generated in the current directory, the size is 10723328 bytes, and subsequent debugging with this core file is possible. Such a method is especially useful for online service programs, especially when the odds of a crash are low, saving a lot of time to crash the process.


But there is also a problem, I do not understand, that is, when I set the Set_core function inside the core file size is 1024 bytes, the resulting core file is still 10723328, as if the size of the settings did not take effect, do not know why. I can communicate with prawns who know the reason.



Linux code generation Core file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.