Advertisement
Guest User

stacktrace

a guest
Jul 19th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. int getFileAndLine (unw_word_t addr, char *file, size_t flen, int *line)
  2. {
  3.         static char buf[256];
  4.         char *p;
  5.        
  6.         sprintf (buf, "/usr/bin/addr2line -C -e %s -f -i %lx", getExecutableName(), (unsigned long) addr);
  7.         FILE* f = popen (buf, "r");
  8.  
  9.         if (f == NULL)
  10.         {
  11.                 perror (buf);
  12.                 return 0;
  13.         }
  14.  
  15.         // get function name
  16.         fgets (buf, 256, f);
  17.  
  18.         // get file and line
  19.         fgets (buf, 256, f);
  20.  
  21.         if (buf[0] != '?')
  22.         {
  23.                 int l;
  24.                 char *p = buf;
  25.  
  26.                 // file name is until ':'
  27.                 while (*p != ':')
  28.                 {
  29.                         p++;
  30.                 }
  31.  
  32.                 *p++ = 0;
  33.                 // after file name follows line number
  34.                 strcpy (file , buf);
  35.                 sscanf (p,"%d", line);
  36.         }
  37.         else
  38.         {
  39.                 strcpy (file,"unkown");
  40.                 *line = 0;
  41.         }
  42.  
  43.         pclose(f);
  44. }
  45.  
  46. void show_backtrace (void)
  47. {
  48.         char name[256];
  49.         unw_cursor_t cursor; unw_context_t uc;
  50.         unw_word_t ip, sp, offp;
  51.  
  52.         unw_getcontext(&uc);
  53.         unw_init_local(&cursor, &uc);
  54.  
  55.         while (unw_step(&cursor) > 0)
  56.         {
  57.                 char file[256];
  58.                 int line = 0;
  59.  
  60.                 name[0] = '\0';
  61.                 unw_get_proc_name(&cursor, name, 256, &offp);
  62.                 unw_get_reg(&cursor, UNW_REG_IP, &ip);
  63.                 unw_get_reg(&cursor, UNW_REG_SP, &sp);
  64.                
  65.                 getFileAndLine((long)ip, file, 256, &line);
  66.                 printf("%s in file %s line %d\n", name, file, line);
  67.         }
  68. }
  69.  
  70. volatile sig_atomic_t fatal_error_in_progress = 0;
  71.  
  72. void fatal_error_signal (int sig)
  73. {
  74.         /* Since this handler is established for more than one kind of signal,
  75.           it might still get invoked recursively by delivery of some other kind
  76.           of signal.  Use a static variable to keep track of that. */
  77.        if (fatal_error_in_progress)
  78.            raise (sig);
  79.        fatal_error_in_progress = 1;
  80.  
  81.         show_backtrace();
  82.  
  83.        signal (sig, SIG_DFL);
  84.        raise (sig);
  85. }
  86.  
  87. void setupStacktrace()
  88. {
  89.     signal(SIGSEGV, &fatal_error_signal);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement