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.

203 lines
5.0KB

  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 "JackError.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. ucontext_t *ucontext = (ucontext_t*)ptr;
  52. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  53. int f = 0;
  54. Dl_info dlinfo;
  55. void **bp = 0;
  56. void *ip = 0;
  57. #else
  58. void *bt[20];
  59. char **strings;
  60. size_t sz;
  61. #endif
  62. if (signum == SIGSEGV)
  63. {
  64. jack_error("Segmentation Fault!");
  65. }
  66. else if (signum == SIGABRT)
  67. {
  68. jack_error("Abort!");
  69. }
  70. else if (signum == SIGILL)
  71. {
  72. jack_error("Illegal instruction!");
  73. }
  74. else if (signum == SIGFPE)
  75. {
  76. jack_error("Floating point exception!");
  77. }
  78. else
  79. {
  80. jack_error("Unknown bad signal catched!");
  81. }
  82. jack_error("info.si_signo = %d", signum);
  83. jack_error("info.si_errno = %d", info->si_errno);
  84. jack_error("info.si_code = %d (%s)", info->si_code, si_codes[info->si_code]);
  85. jack_error("info.si_addr = %p", info->si_addr);
  86. #if !defined(__alpha__) && !defined(__ia64__) && !defined(__FreeBSD_kernel__) && !defined(__arm__) && !defined(__hppa__) && !defined(__sh__)
  87. for(i = 0; i < NGREG; i++)
  88. jack_error("reg[%02d] = 0x" REGFORMAT, i,
  89. #if defined(__powerpc__)
  90. ucontext->uc_mcontext.uc_regs[i]
  91. #elif defined(__sparc__) && defined(__arch64__)
  92. ucontext->uc_mcontext.mc_gregs[i]
  93. #else
  94. ucontext->uc_mcontext.gregs[i]
  95. #endif
  96. );
  97. #endif /* alpha, ia64, kFreeBSD, arm, hppa */
  98. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  99. # if defined(SIGSEGV_STACK_IA64)
  100. ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
  101. bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
  102. # elif defined(SIGSEGV_STACK_X86)
  103. ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
  104. bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
  105. # endif
  106. jack_error("Stack trace:");
  107. while(bp && ip) {
  108. if(!dladdr(ip, &dlinfo))
  109. break;
  110. const char *symname = dlinfo.dli_sname;
  111. #ifndef NO_CPP_DEMANGLE
  112. int status;
  113. char *tmp = __cxa_demangle(symname, NULL, 0, &status);
  114. if(status == 0 && tmp)
  115. symname = tmp;
  116. #endif
  117. jack_error("% 2d: %p <%s+%u> (%s)",
  118. ++f,
  119. ip,
  120. symname,
  121. (unsigned)(ip - dlinfo.dli_saddr),
  122. dlinfo.dli_fname);
  123. #ifndef NO_CPP_DEMANGLE
  124. if(tmp)
  125. free(tmp);
  126. #endif
  127. if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
  128. break;
  129. ip = bp[1];
  130. bp = (void**)bp[0];
  131. }
  132. #else
  133. jack_error("Stack trace (non-dedicated):");
  134. sz = backtrace(bt, 20);
  135. strings = backtrace_symbols(bt, sz);
  136. for(i = 0; i < sz; ++i)
  137. jack_error("%s", strings[i]);
  138. #endif
  139. jack_error("End of stack trace");
  140. exit (-1);
  141. }
  142. #endif
  143. int setup_sigsegv() {
  144. struct sigaction action;
  145. memset(&action, 0, sizeof(action));
  146. action.sa_sigaction = signal_segv;
  147. action.sa_flags = SA_SIGINFO;
  148. if(sigaction(SIGSEGV, &action, NULL) < 0) {
  149. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  150. return 0;
  151. }
  152. if(sigaction(SIGILL, &action, NULL) < 0) {
  153. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  154. return 0;
  155. }
  156. if(sigaction(SIGABRT, &action, NULL) < 0) {
  157. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  158. return 0;
  159. }
  160. if(sigaction(SIGFPE, &action, NULL) < 0) {
  161. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. #ifndef SIGSEGV_NO_AUTO_INIT
  167. static void __attribute((constructor)) init(void) {
  168. setup_sigsegv();
  169. }
  170. #endif