jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.2KB

  1. /**
  2. * This source file is used to print out a stack-trace when your program
  3. * segfaults. It is relatively reliable and spot-on accurate.
  4. *
  5. * This code is in the public domain. Use it as you see fit, some credit
  6. * would be appreciated, but is not a prerequisite for usage. Feedback
  7. * on it's use would encourage further development and maintenance.
  8. *
  9. * Author: Jaco Kroon <jaco@kroon.co.za>
  10. *
  11. * Copyright (C) 2005 - 2008 Jaco Kroon
  12. */
  13. #if defined(HAVE_CONFIG_H)
  14. #include "config.h"
  15. #endif
  16. //#define NO_CPP_DEMANGLE
  17. #define SIGSEGV_NO_AUTO_INIT
  18. #ifndef _GNU_SOURCE
  19. # define _GNU_SOURCE
  20. #endif
  21. #include <memory.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <signal.h>
  25. #include <dlfcn.h>
  26. #include <execinfo.h>
  27. #include <errno.h>
  28. #ifndef NO_CPP_DEMANGLE
  29. char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
  30. #endif
  31. #include "jack/control.h"
  32. #if defined(REG_RIP)
  33. # define SIGSEGV_STACK_IA64
  34. # define REGFORMAT "%016lx"
  35. #elif defined(REG_EIP)
  36. # define SIGSEGV_STACK_X86
  37. # define REGFORMAT "%08x"
  38. #else
  39. # define SIGSEGV_STACK_GENERIC
  40. # define REGFORMAT "%x"
  41. #endif
  42. #ifdef __APPLE__
  43. // TODO : does not compile yet on OSX
  44. static void signal_segv(int signum, siginfo_t* info, void*ptr)
  45. {}
  46. #else
  47. #include <ucontext.h>
  48. static void signal_segv(int signum, siginfo_t* info, void*ptr) {
  49. static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
  50. size_t i;
  51. const char *si_code_str;
  52. ucontext_t *ucontext = (ucontext_t*)ptr;
  53. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  54. int f = 0;
  55. Dl_info dlinfo;
  56. void **bp = 0;
  57. void *ip = 0;
  58. #else
  59. void *bt[20];
  60. char **strings;
  61. size_t sz;
  62. #endif
  63. if (signum == SIGSEGV)
  64. {
  65. jack_error("Segmentation Fault!");
  66. }
  67. else if (signum == SIGABRT)
  68. {
  69. jack_error("Abort!");
  70. }
  71. else if (signum == SIGILL)
  72. {
  73. jack_error("Illegal instruction!");
  74. }
  75. else if (signum == SIGFPE)
  76. {
  77. jack_error("Floating point exception!");
  78. }
  79. else
  80. {
  81. jack_error("Unknown bad signal catched!");
  82. }
  83. if (info->si_code >= 0 && info->si_code < 3)
  84. si_code_str = si_codes[info->si_code];
  85. else
  86. si_code_str = "unknown";
  87. jack_error("info.si_signo = %d", signum);
  88. jack_error("info.si_errno = %d", info->si_errno);
  89. jack_error("info.si_code = %d (%s)", info->si_code, si_code_str);
  90. jack_error("info.si_addr = %p", info->si_addr);
  91. #if !defined(__alpha__) && !defined(__ia64__) && !defined(__FreeBSD_kernel__) && !defined(__arm__) && !defined(__hppa__) && !defined(__sh__)
  92. for(i = 0; i < NGREG; i++)
  93. jack_error("reg[%02d] = 0x" REGFORMAT, i,
  94. #if defined(__powerpc__)
  95. ucontext->uc_mcontext.uc_regs[i]
  96. #elif defined(__sparc__) && defined(__arch64__)
  97. ucontext->uc_mcontext.mc_gregs[i]
  98. #else
  99. ucontext->uc_mcontext.gregs[i]
  100. #endif
  101. );
  102. #endif /* alpha, ia64, kFreeBSD, arm, hppa */
  103. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  104. # if defined(SIGSEGV_STACK_IA64)
  105. ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
  106. bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
  107. # elif defined(SIGSEGV_STACK_X86)
  108. ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
  109. bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
  110. # endif
  111. jack_error("Stack trace:");
  112. while(bp && ip) {
  113. if(!dladdr(ip, &dlinfo))
  114. break;
  115. const char *symname = dlinfo.dli_sname;
  116. #ifndef NO_CPP_DEMANGLE
  117. int status;
  118. char *tmp = __cxa_demangle(symname, NULL, 0, &status);
  119. if(status == 0 && tmp)
  120. symname = tmp;
  121. #endif
  122. jack_error("% 2d: %p <%s+%u> (%s)",
  123. ++f,
  124. ip,
  125. symname,
  126. (unsigned)(ip - dlinfo.dli_saddr),
  127. dlinfo.dli_fname);
  128. #ifndef NO_CPP_DEMANGLE
  129. if(tmp)
  130. free(tmp);
  131. #endif
  132. if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
  133. break;
  134. ip = bp[1];
  135. bp = (void**)bp[0];
  136. }
  137. #else
  138. jack_error("Stack trace (non-dedicated):");
  139. sz = backtrace(bt, 20);
  140. strings = backtrace_symbols(bt, sz);
  141. for(i = 0; i < sz; ++i)
  142. jack_error("%s", strings[i]);
  143. #endif
  144. jack_error("End of stack trace");
  145. exit (-1);
  146. }
  147. #endif
  148. int setup_sigsegv() {
  149. struct sigaction action;
  150. memset(&action, 0, sizeof(action));
  151. action.sa_sigaction = signal_segv;
  152. #ifdef SA_SIGINFO
  153. action.sa_flags = SA_SIGINFO;
  154. #endif
  155. if(sigaction(SIGSEGV, &action, NULL) < 0) {
  156. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  157. return 0;
  158. }
  159. if(sigaction(SIGILL, &action, NULL) < 0) {
  160. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  161. return 0;
  162. }
  163. if(sigaction(SIGABRT, &action, NULL) < 0) {
  164. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  165. return 0;
  166. }
  167. if(sigaction(SIGFPE, &action, NULL) < 0) {
  168. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. #ifndef SIGSEGV_NO_AUTO_INIT
  174. static void __attribute((constructor)) init(void) {
  175. setup_sigsegv();
  176. }
  177. #endif