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.

199 lines
4.7KB

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