Sign inorRegister Site is for SALE!
C++
Raiting:
126

Segmentation Fault Handling in C++


Introduction


C ++ is unmanaged language, because the programs can escape without saving the user’s data and give the error messages, etc. For example, it takes only to get into an uninitialized memory. For example:

void fall()
{
char * s = "short_text";
sprintf(s,"This is very long text");
}

or

void fall()
{
int * pointer = NULL;
*pointer = 13;
}
It would be better if we could "catch" a program crash just like in java we are catching exceptions, as well we could do anything before the program will crash (save the user’s document, display a dialog with the error message, etc.)

A task does not have a general solution, as C ++ does not have its own model of exception handling that is work-related with the memory. Nevertheless, we will consider two methods that are using the features of the operating system that caused an exception.


Method 1: SEH

If you use OS Windows as the target and Visual C ++ as a compiler, then you can use Structured Exception Handling – an extension of C ++ from Microsoft, which enables you to have complete control over the handling of exceptions that occur in the program.

The general syntax of exception handling is as follows:

__try
{
segfault1();
}
__except( condition1 )
{
/ / exception handling if condition1 == EXCEPTION_EXECUTE_HANDLER.
/ / in condition1 can (should) be a call method that checks
/ / type of exception, and it returns EXCEPTION_EXECUTE_HANDLER
/ / if the type of exception corresponds to what we want to handle
}
__except( condition2 )
{
/ / another handler
}
__finally
{
/ / that will be executed
}
Here is the "working example", "Copy and paste into Visual Studio»

#include <stdio.h>
#include <windows.h>
#include <excpt.h>

int memento() // Segfault handlling
{
MessageBoxA(NULL,"Memento Mori","Exception catched!",NULL);
return 0;
}

void fall() // segfault generation
{
int* p = 0x00000000;
*p = 13;
}

int main(int argc, char *argv[])
{
__try
{
fall();
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
memento();
}
}
This technique has several disadvantages:

• One compiler. One operating system. "C ++ is not pure". You cannot use this technique If you want to work without MS tools.
• One stream - one table. If you write a structure of __try ... __except within __try, another stream will be started that will cause segfault without exiting __try, then ... nothing happens, the program will crash "as usual", because a separate handler SEH must be written for each stream.

There were so many disadvantages that we have to seek a second solution.

Method 2: POSIX - signals

The method relies on the program that receives a POSIX-message SIGSEGV in the time of the crash. This is certainly for all UNIX-based systems, as well as Windows.

The method is simple, we have to write a message handler SIGSEGV, where the program will make its final actions and finally, it will crash:

void posix_death_signal(int signum)
{
memento(); // final actions
signal(signum, SIG_DFL); // signal resending

exit(3); / / exiting the program. If you will not do it, then the handler will be called endlessly.
}

following which we need to register this handler:

signal(SIGSEGV, posix_death_signal);
Here is a ready-made example:

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

int memento()
{
int a=0;
MessageBoxA(NULL,"Memento mori","POSIX Signal",NULL);
return 0;
}
void fall()
{
int* p = 0x00000000;
*p = 13;
}
void posix_death_signal(int signum)
{
memento();
signal(signum, SIG_DFL);
exit(3);
}


int main(int argc, char *argv[])
{
signal(SIGSEGV, posix_death_signal);
fall();
}
It always works unlike SEH: the solution is multithreaded (you can drop the program in any stream, the handler will run in any case) and a cross-platform will work with any compiler and any POSIX that is compatible with OS.
+2
Skull 25 december 2011, 13:26
Vote for this post
Bring it to the Main Page
 

Comments

Leave a Reply

B
I
U
S
Help
Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute