2010/06/29

SIGSEGV

Gdyby ktoś potrzebował kodu obsługującego SIGSEGV w aplikacji:

gcc -rdynamic

#define _GNU_SOURCE
#include
#include
#include
#include

----------- SIGSEG!!! -----------
Fault addr [0x00000001] in function [0x00416d94] FKT_BL_Creat+0x32
-------- Backtrace start --------
Obtained 8 stack frames.
0 : [0x00408018] pthread_create+0x2d2
1 : [0x2956e420] __kernel_rt_sigreturn+0x0
2 : [0x00416d94] FKT_BL_Creat+0x32
3 : [0x00415fcc] Fkt_Tuning_Init+0x90
4 : [0x00408018] pthread_create+0x8d8
5 : [0x00408018] pthread_create+0x654
6 : [0x0043123c] unresolved
7 : [0x2958559e] unresolved
Aborted

static void sigseg_hndlr(int unused(signum), siginfo_t* siginfo, void* ucont)
{
   void * array[50];
   size_t size;
   size_t i;
   ucontext_t *ucontext = (ucontext_t*)ucont;
   void* pc = (void*)(ucontext->uc_mcontext.gregs[REG_EIP]); /* for x86 */
   Dl_info dlinfo;

   printf("----------- SIGSEG!!! -----------\n");
   if( dladdr(pc, &dlinfo) && (NULL != dlinfo.dli_sname) )
      printf("Fault addr [0x%08x] in function [0x%08x] %s+0x%x\n", (unsigned int)(siginfo->si_addr), (unsigned int)(dlinfo.dli_saddr), dlinfo.dli_sname, pc - dlinfo.dli_saddr );
   else
      printf("Fault addr [0x%08x] in function [0x%08x] unresolved\n", (unsigned int)(siginfo->si_addr), (unsigned int)(pc));

   printf("-------- Backtrace start --------\n");
   size = backtrace(array, 50);
   printf ("Obtained %zd stack frames.\n", size);
   for (i = 0; i < size; i++)
   {
      if( dladdr(array[i], &dlinfo) && (NULL != dlinfo.dli_sname) )
         printf("%d : [0x%08x] %s+0x%x\n", i, (unsigned int)(dlinfo.dli_saddr), dlinfo.dli_sname, array[i] - dlinfo.dli_saddr );
      else
         printf("%d : [0x%08x] unresolved\n", i, (unsigned int)(array[i]));
   }

   fflush(stdout);
   abort();
}


int main(int argc, char* argv[])
{
   SAL_Config_T sal_config;
   SAL_Thread_Attr_T thread_attr;
   int retv = -1;

   sigaction(
      SIGSEGV, /* int signum, */
      &sigsegact, /* const struct sigaction *act, */
      NULL /* struct sigaction *oldact */ );
}

Brak komentarzy:

Prześlij komentarz