Re: Signals (SIGVTALARM) and the current function... (fwd)

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 05/29/98


[ Ah, I'm too lazy resubscribe with this new address :) ]


On Fri, 29 May 1998, George wrote:


> >Is there a way to find out what function the program was in when a signal
> >is trapped, and then log the name (using Linux) ?  I want to find out

> If you have glibc 2 (libc5 doesn't seem to have it), there is a structure
> called 'sigcontext' that will save the 'eip' that the program was currently
> at. Then in gdb, to see the code at that address:
>
> list *0x[address]
>
> I don't know of a way to automate that, and actually, don't see any
> functions in the header to actually use sigcontext. But it's there.  If you
> find out more, let me know.

This is how to make it work on libc5 (note, I have not tested this
myself):


Subject:      Re: Saved Context Access in exception handler
From:         seyko@p5.f434.n5020.z2.fidonet.org (Sergey Korshunoff)
Date:         1998/05/25
Message-ID:   <6kbj60$2kr@p5.f434.n5020.z2.fidonet.org>
Newsgroups:   comp.os.linux.development.apps
 [More Headers]
[Subscribe to comp.os.linux.development.apps]
[LINK]

nextsoft@ist.cerist.dz wrote:

:  Well, I would like to know how can I get the saved context (registers, ip,
:  stack pointer, ...) in the SIGSEGV exception handler when the exception is
:  generated ( for the Intel-386 based Linux). Example:

:  void sig_handler ( int s )
:  {
:    /* I modify here the Instruction Pointer Register */
:    /* so I can continue the program execution*/
:  }

:  int main()
:  {
:    signal(SIGSEGV, sig_handler);
:    int* p=NULL;

:    *p=1;
:  }

:  I thank in advance for your suggestions and help.

Look at the example. It works for libc v5.x.x and for glibc (libc v6.0.x)
Regards,
        Sergey Korshunoff


== begin  ===================================================================
#include <stdio.h>      /* fprintf() */
#include <unistd.h>     /* _exit() */
#include <string.h>     /* strsignal() */
#include <signal.h>     /* signal(), sigcontext */

# if defined( _LINUX_C_LIB_VERSION_MAJOR ) && _LINUX_C_LIB_VERSION_MAJOR==5
#   include <asm/sigcontext.h>    /* sigcontext (for libc 5.x.x) */
#   define sigcontext sigcontext_struct
# endif


void sig_handler ( int signum, struct sigcontext info )
{
    if( signum == SIGSEGV )
    {
        fprintf( stderr,
                 "Fatal error: segmentation violation accessing %p\n"
                 " caused by code at %p\n",
                        (void *)info.cr2, (void *)info.eip );
    }

    fprintf( stderr,
             "Program terminated by signal `%d' (%s)\n",
                        signum, strsignal(signum));

    _exit( -1 );
}

int main()
{
  signal(SIGSEGV, (void (*)(int))sig_handler ); /* sigaction() is better */
  int* p= (int *)0x10;

  *p=1;

  return 0;
}



 =============================================================================
<erwin@andreasen.com>      Herlev, Denmark              UNIX System Programmer
<URL:http://www.abandoned.org/drylock/>     <*>         (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST