Debugging segment errors with the Linux signaling mechanism (Segment fault)

Source: Internet
Author: User
Tags log log sprintf

In the actual development process, you may encounter the problem of paragraph errors, although it is an old problem, but the hidden danger is enormous, as long as the occurrence of the program immediately crash stop. If the program runs in the PC, the segment fault debugging is relatively convenient, because the serial port, the display can view the message, as long as the program runs, through the GDB debugging tool can capture the specific reasons for the segment fault generated. But I do not know if you have any idea, when the program runs on embedded devices, you are faced with a lack of resources, you do not have a serial port printing information, no monitor can be viewed, you do not know the state of the program running, if the program produced segment Falut This bug occurred within 1 years only occurred three or four times, Time is uncertain, and how do you debug the exact location where you know the program is going wrong? And so on, the problem is really confusing.

There are several ways to resolve this problem:

1 before the product release, as far as possible all segment fault cause to find out, all eliminate (ideally)

2 in the key location of the program to increase the printing information, set the print level, through the letter to reduce the information to check the scope of the bug, if it is embedded device, you need to write this information to the file, saved in Flash.

3 using the watchdog to feed the dog, if the program is aborted or long-term into a dead loop, the program will reload

4 using the Linux signaling mechanism to solve the problem of segment errors (somewhat similar to soft watchdog)

Here I focus on the solution of the 4th point:

First of all, I first describe my general thinking, assuming that my program somewhere called a Function dummy_func (), this function has a segment fault segment error, if you do not handle it, do not doubt, your program immediately hangs off, if it is embedded device program, You may not know the specific cause and location of the segment fault, only look at the log log to analyze slowly. We know that in the signal mechanism of Linux, when a segment fault error occurs, the program generates a SIGSEGV signal, so we just imagine that if we can catch this signal in time in the program, and then reload the application in this signal processing function, You can implement a watchdog-like function and call it a software watchdog. But this method is only expedient, it can make your product in front of the customer to maintain a good impression (because if the program is hung and reloaded, and the customer does not know), rather than long-term to go to the scene to solve the problem, this method is applicable to the period of generating a segment error uncertainty, In fact, we all know that encounter segment fault we have to solve the specific reasons for this problem, this is the correct way to solve.

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

/****************************** program starts **********************************/

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

char main_status = 0;

/***********************
* This function generates a segment error
* *********************/
void Dummy_func (void)
{
printf ("Hello world/n");
char *p = NULL; 0 Address
*p = 0x1a; A segment error occurred while writing data to 0 address
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 delay is very important, if no delay, CTRL + C signal can not be processed in time (that is, CTRL + C failure), the program will be cyclic loading
* If no delay is added, the program is re-executed later. The/TEST1 statement reloads the application.
*/
Sleep (5);

memset (buffer, 0, sizeof (buffer));
sprintf (Buffer, "./test1");
System (buffer);
printf ("xxxxxxxxxxxxxxx/n");

if (1 = = Main_status)
Exit (0);
}


/**************************
* Processing function to capture the CTRL + C signal
**************************/
void Ctrl_c_func (int signo)
{
printf ("Stop the demo/n");
Main_status = 1; Position flag
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 Master Program
****************************/
int main (int argc, char **argv)
{
Signal (SIGSEGV, &deal); Capturing SIGSEGV Signals
Signal (SIGINT, &ctrl_c_func); Capturing SIGINT signals
Ctrl_c_func_init (); If you don't use the above snap, call this function.

while (1)
{
if (1 = = main_status)//If a flag bit is received, exit
{
Exit (0);
}
Dummy_func (); Call to generate segment fault function
}
return 0;
}
/******************************End of program**********************************/

Debugging segment errors using the Linux signaling mechanism (Segment fault)

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.