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.

213 lines
5.3KB

  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(__powerpc64__)
  95. ucontext->uc_mcontext.gp_regs[i]
  96. #elif defined(__powerpc__)
  97. ucontext->uc_mcontext.uc_regs[i]
  98. #elif defined(__sparc__) && defined(__arch64__)
  99. ucontext->uc_mcontext.mc_gregs[i]
  100. #else
  101. ucontext->uc_mcontext.gregs[i]
  102. #endif
  103. );
  104. #endif /* alpha, ia64, kFreeBSD, arm, hppa */
  105. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  106. # if defined(SIGSEGV_STACK_IA64)
  107. ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
  108. bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
  109. # elif defined(SIGSEGV_STACK_X86)
  110. ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
  111. bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
  112. # endif
  113. jack_error("Stack trace:");
  114. while(bp && ip) {
  115. if(!dladdr(ip, &dlinfo))
  116. break;
  117. const char *symname = dlinfo.dli_sname;
  118. #ifndef NO_CPP_DEMANGLE
  119. int status;
  120. char *tmp = __cxa_demangle(symname, NULL, 0, &status);
  121. if(status == 0 && tmp)
  122. symname = tmp;
  123. #endif
  124. jack_error("% 2d: %p <%s+%u> (%s)",
  125. ++f,
  126. ip,
  127. symname,
  128. (unsigned)(ip - dlinfo.dli_saddr),
  129. dlinfo.dli_fname);
  130. #ifndef NO_CPP_DEMANGLE
  131. if(tmp)
  132. free(tmp);
  133. #endif
  134. if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
  135. break;
  136. ip = bp[1];
  137. bp = (void**)bp[0];
  138. }
  139. #else
  140. jack_error("Stack trace (non-dedicated):");
  141. sz = backtrace(bt, 20);
  142. strings = backtrace_symbols(bt, sz);
  143. for(i = 0; i < sz; ++i)
  144. jack_error("%s", strings[i]);
  145. #endif
  146. jack_error("End of stack trace");
  147. exit (-1);
  148. }
  149. #endif
  150. int setup_sigsegv() {
  151. struct sigaction action;
  152. memset(&action, 0, sizeof(action));
  153. action.sa_sigaction = signal_segv;
  154. #ifdef SA_SIGINFO
  155. action.sa_flags = SA_SIGINFO;
  156. #endif
  157. if(sigaction(SIGSEGV, &action, NULL) < 0) {
  158. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  159. return 0;
  160. }
  161. if(sigaction(SIGILL, &action, NULL) < 0) {
  162. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  163. return 0;
  164. }
  165. if(sigaction(SIGABRT, &action, NULL) < 0) {
  166. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  167. return 0;
  168. }
  169. if(sigaction(SIGFPE, &action, NULL) < 0) {
  170. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. #ifndef SIGSEGV_NO_AUTO_INIT
  176. static void __attribute((constructor)) init(void) {
  177. setup_sigsegv();
  178. }
  179. #endif