Use the Linux signal mechanism to debug segment errors (segment fault)

Source: Internet
Author: User
Tags sigint signal

In the actual development process, you may encounter a segment error. Although it is an old problem, it brings a great risk. Once it appears, the program will crash and stop immediately. If the program runs in the PC, segment fault debugging is relatively convenient, because you can view messages through the serial port and display. As long as the program runs, you can use the gdb debugging tool to capture the specific causes of segment fault. But I wonder if you have any idea. When the program runs on an embedded device, you are faced with a lack of resources. You do not have serial port printing information, no display to view, and you do not know the running status, if a bug such as segment falut occurs only three or four times in a year, and the time is uncertain, how do you know the specific location of a program error? And so on.

There are several ways to solve this problem:

1. Before the product is released, try to identify the cause of all segment fault and eliminate all (ideally)

2. Add printing information to the key position of the program, set the printing level, and narrow down the bug check scope by using the mail information. If it is an embedded device, you need to write the information into the file, save it in flash.

3. Use the watchdog to feed the dog. If the program is suspended or is stuck in an endless loop for a long time, the program will be reloaded.

4. Use the Linux signal mechanism to solve segment errors (a bit similar to the Soft Watchdog)

 

 

Next I will focus on the solution to the 4th key points:

First, let me describe my overall idea. If my program calls the dummy_func () function somewhere, this function has a segment fault segment error, if your program does not process it, you do not have to worry about it. Your program will immediately crash. If it is a program in an embedded device, you may not know the specific cause and location of the segment fault, you can only view logs for analysis. We know that in the signal mechanism of Linux, when a segment fault error occurs, the program will generate a SIGSEGV signal, so we can imagine that if we can capture this signal in time in the program, then re-load the application in this signal processing function, you can implement a function similar to the dog. For now, this method is called the software dog. However, this method is only a matter of expediency. It can make your product feel good in front of the customer (because if the program crashes and is re-loaded, the customer does not know ), instead of going to the site frequently for a long time to solve this problem, this method is applicable to the cycle of generating segment errors with uncertainty. In fact, we all know that we have to solve the specific cause of this problem when we encounter segment fault, this is the correct solution.

Below I will list the complete program source code, although simple, but very useful:

 

/******************************Program start


**********************************/

 

# Include <stdio. h>
# Include <signal. h>
# Include <string. h>
# Include <stdlib. h>

Char main_status = 0;

/***********************
* This function produces a segment error.
**********************/
Void dummy_func (void)
{
Printf ("Hello World/N ");
Char * P = NULL; // 0 address
* P = 0x1a; // write data to the 0 address, with a segment Error
Return;
}

/************************
* This function is used to reload the program.
************************/
Void deal (void)
{
Char buffer [255];
Memset (buffer, 0, sizeof (buffer ));
Sprintf (buffer, "CD ~ /Test ");
System (buffer );

/*
* This latency is very important. If no latency is added, the CTRL + C signal cannot be processed in time (that is, CTRL + C is invalid), and the program will load cyclically
* If no latency is added, the program re-executes the following./test1 statement to re-load the application.
*/
Sleep (5 );

Memset (buffer, 0, sizeof (buffer ));
Sprintf (buffer, "./test1 ");
System (buffer );
Printf ("xxxxxxxxxxxxxxx/N ");

If (1 = main_status)
Exit (0 );
}

/**************************
* The processing function that captures the CTRL + C Signal
**************************/
Void ctrl_c_func (INT signo)
{
Printf ("stop the demo/N ");
Main_status = 1; // set the flag bit
Exit (0 );
}

Int ctrl_c_func_init (void)
{
Int ret = 0;
Struct sigaction Act;

Act. sa_handler = ctrl_c_func;
Sigemptyset (& act. sa_mask );
Act. sa_flags = 0;

Ret = sigaction (SIGINT, & act, null );
}

/****************************
* Main program
****************************/
Int main (INT argc, char ** argv)
{
Signal (SIGSEGV, & deal); // capture SIGSEGV Signals
Signal (SIGINT, & ctrl_c_func); // capture the SIGINT Signal
// Ctrl_c_func_init (); // call this function if you do not need to capture it.

While (1)
{
If (1 = main_status) // exit if the flag is received
{
Exit (0 );
}
Dummy_func (); // call the segment fault Function
}
Return 0;
}
/******************************Program ended


**********************************/

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.