Change the SIGSEGV signal handler, any problems?

Change the SIGSEGV signal handler, any problems?

Post by Haibing Qi » Wed, 06 Aug 1997 04:00:00



In the program at the end of the message,  I change the the signal handler
for SIGSEGV and SIGBUS

I do this in a hope that the program will not core dump when there is a
memory failure. If the program is a very complex server which has a array
of services, which also incorporates many thousands of methods. If one
method in one service has a memory problem, instead core dump the whole
server, there will only be an exception throwing and one request failure,
the server will still be running with most of the services available. On
the other hand, software maintainence programmers can debug offline.

I know this is a bold approach, I am wondering if anyone out there thinked
about this or even using this approach. I notice some times Netscape will
disappear due to "bus error", I believe this approach might help to
prevent that.

Anyway, I still need to make sure that this approach be possible
technically, I believe it's OK but still need opinions from net
friends.

-haibing qiao
*****************************
#include <iostream.h>
#include <signal.h>

class CoreDumpErr
{
  public:
    CoreDumpErr( const char* em, int sig ):errMsg(em),sigType(sig) {};
    const char* getErrMsg()
    {
        return errMsg;
    }
    int  getSigType()
    {
        return sigType;
    }

  protected:
    const char* errMsg;
    int         sigType;

Quote:}

;

void sigSegHandler(int error)
{
    char c;

    cout << " enter 'y' to dump core, or any other to continur--->";
    cin >> c;

    if( c !='y')
    {
        throw CoreDumpErr("Core err", error);
    }

Quote:}

// a dummy class to test the scope, and create a memory violation
class scope
{
public:
    scope()
    {
        cout << "CTOR of Scope" << endl;
    }
    void print()
    {
        cout << ppp[100000] << " this is" << endl;

    }

    ~scope()
    {
        cout << "DTOR of scpe" << endl;
    }
    char* ppp;

Quote:}

;

main()
{
    scope *sptrLeak, *sptrCore;

// catch SEGV
    signal(SIGSEGV,sigSegHandler);  
    signal(SIGBUS,sigSegHandler);  

    try
    {
        scope sval;             // this will be automatically destructed  
        sptrLeak = new scope;   // this will be deleted in catch block

        sptrCore->print();      // this line will cause memory violation
        delete sptrLeak;
    }
    catch(CoreDumpErr& err)
    {
        cout << " the error is catched -->" <<err.getErrMsg() << endl;
        cout << "signal type is " << err.getSigType() << endl;
        // this delete is for the possible memory leak might occur
        delete sptrLeak;

    }
    cout << " now we get out" << endl;
 }

// compile on HP :  CC +eh xxxx.C
// compile on SGI:  CC -exceptions xxxx.C
// compile on SUN:  CC xxxx.C

--

 
 
 

Change the SIGSEGV signal handler, any problems?

Post by John A. Krol » Wed, 06 Aug 1997 04:00:00



> In the program at the end of the message,  I change the the signal
> handler
> for SIGSEGV and SIGBUS

> I do this in a hope that the program will not core dump when there is
> a
> memory failure. If the program is a very complex server which has a
> array
> of services, which also incorporates many thousands of methods. If one

> method in one service has a memory problem, instead core dump the
> whole
> server, there will only be an exception throwing and one request
> failure,
> the server will still be running with most of the services available.
> On
> the other hand, software maintainence programmers can debug offline.

> I know this is a bold approach, I am wondering if anyone out there
> thinked
> about this or even using this approach. I notice some times Netscape
> will
> disappear due to "bus error", I believe this approach might help to
> prevent that.

> Anyway, I still need to make sure that this approach be possible
> technically, I believe it's OK but still need opinions from net
> friends.

> -haibing qiao
> *****************************
> #include <iostream.h>
> #include <signal.h>

> class CoreDumpErr
> {
>   public:
>     CoreDumpErr( const char* em, int sig ):errMsg(em),sigType(sig) {};

>     const char* getErrMsg()
>     {
>         return errMsg;
>     }
>     int  getSigType()
>     {
>         return sigType;
>     }

>   protected:
>     const char* errMsg;
>     int         sigType;

> }
> ;

> void sigSegHandler(int error)
> {
>     char c;

>     cout << " enter 'y' to dump core, or any other to continur--->";
>     cin >> c;

>     if( c !='y')
>     {
>         throw CoreDumpErr("Core err", error);
>     }

> }

> // a dummy class to test the scope, and create a memory violation
> class scope
> {
> public:
>     scope()
>     {
>         cout << "CTOR of Scope" << endl;
>     }
>     void print()
>     {
>         cout << ppp[100000] << " this is" << endl;

>     }

>     ~scope()
>     {
>         cout << "DTOR of scpe" << endl;
>     }
>     char* ppp;
> }
> ;

> main()
> {
>     scope *sptrLeak, *sptrCore;

> // catch SEGV
>     signal(SIGSEGV,sigSegHandler);
>     signal(SIGBUS,sigSegHandler);

>     try
>     {
>         scope sval;             // this will be automatically
> destructed
>         sptrLeak = new scope;   // this will be deleted in catch block

>         sptrCore->print();      // this line will cause memory
> violation
>         delete sptrLeak;
>     }
>     catch(CoreDumpErr& err)
>     {
>         cout << " the error is catched -->" <<err.getErrMsg() << endl;

>         cout << "signal type is " << err.getSigType() << endl;
>         // this delete is for the possible memory leak might occur
>         delete sptrLeak;

>     }
>     cout << " now we get out" << endl;
>  }

> // compile on HP :  CC +eh xxxx.C
> // compile on SGI:  CC -exceptions xxxx.C
> // compile on SUN:  CC xxxx.C

> --

  A couple of points.  First use sigaction instead of signal.  In the
above example, a
you would need to reinstall your signal handlers with another call to
signal or
the process reinstates the default action (terminate with core).  Use of

sigaction prevents this.
     Second, don't try to use stdio or iostreams within signal handlers
(bad
habit to get into even for examples).  Finally since you don't know the
source of the SIGSEGV or SIGBUS errors, there is a significant
chance that critical library data structures (e.g. malloc's
allocation / free lists) have been corrupted.  You are probably better
off catching the signal, fork / exec a new server (to handle any future
requests), then let the original server execute the default action
for the signal (i.e. generate a core file).

Just my 0.02 worth.

John Kroll

 
 
 

1. Signal handlers inside signal handlers

Greetings Netters,

Unfortunately, the project I'm working on requires I mess with nested signal
handlers, and I've checked out obvious manuals and the POSIX std for clues,
but I'm having no luck.

What I'm trying to do is within a signal handler, plant another handler.
For example

#include        blah blah blah

foo2( int signo )
{
        printf("Caught the second sigalrm\n");

foo1( int signo )
{
        printf("Caught the first sigalrm\n");
        signal( SIGARLM, foo2 );
        alarm(1);

        for (;;)
                ;

main()
{
        signal( SIGALRM, foo1 );
        alarm(3);

        /* Wait for the first alarm */
        sleep(10);

The above program when run, prints the message from function foo1
but never reaches foo2.  Note that my project dictates that I can't
exit foo2, until foo1 has run.
I've tried messing with posix signals (sigaction etc), but have the
same problem.

Has anyone tried to do this sort of thing before??

Thanks in advance,
Scott Wallace

2. DIP Question

3. SIGSEGV signal handler

4. server/raid/solaris upgrading - need help (raid manager)

5. Data acquisition boards

6. SIGSEGV signal handler and pthread

7. cli_sti in drivers_net_hamradio_bpqether.c

8. Threads performance - allow signal handler to not call handler

9. tooltalk tt_open changes signal handler?

10. change signal handler of child thread?

11. SIGSEGV exception handler problem

12. SIGSEGV handler exception problem