What should I do if the Linux C program exits abnormally?-The core file helps you, linuxcore

Source: Internet
Author: User

What should I do if the Linux C program exits abnormally?-The core file helps you, linuxcore

You must have encountered a program exit exception in Linux. The exit exception seems to have a magical magic, which will make you nervous suddenly. Don't be afraid. Linux is very powerful. When the program exits, it will generate a core file in the directory where the executable file is located, it well records some mysterious and necessary things to help you locate the root cause of errors. I can't answer the core files I heard during previous interviews, if you say what the interviewer thinks, let's talk about the core file.

The core file is actually a memory image with some debugging information.

First, you have to turn on the system's core file generation switch. Enter the ulimit-c command in the terminal. If it is displayed as 0, the current switch is disabled. Modify the current user's. bash_profile file (By the way, do not use the root user to modify the/etc/profile file without moving, because this is the case at work. Don't joke, the program won't have the root permission for a lifetime, what you need to do is to get rid of the root habit, just as after you move to the society, you should note that you are not the Emperor), open it and add ulimit-c unlimited at the end, save and quit. Then input source. bash_profile on the terminal and our settings will take effect. Then, enter ulimit-c to display unlimited. The size of the core file generated by ulimited is unlimited. You can also set a limit on the size, for example, ulimit-c 10, it indicates that the size of the generated core file is limited to 10 KB, But we generally do not do this. This may cause the generated core file to be incomplete. Another reason is the disk space and willfulness.

Let's look at a simple example.

The Code is as follows:

#include <stdio.h>#include <stdlib.h>void main(){char *p ;free(p);}

The code is so simple. I just want to explain the problem. compile it into an executable file and add the-g parameter during compilation to execute this program.

We can see that the core. xxx file is generated in the current directory. How can we check where the error occurs.

Input gdb test core. xxx in the current directory (Description: bin indicates the name of the current program)

Enter the where command to view the root cause of the error, for example:


In the source file test. if there is a problem with the code in the sixth line in c, let's look at the code corresponding to the source file, free (p). Obviously char * p is not allocated any space, and of course it cannot be released.

The core file is that simple. Do you mean no?

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.