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.

221 lines
5.4KB

  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. #ifdef HAVE_EXECINFO_H
  27. # include <execinfo.h>
  28. #endif
  29. #include <errno.h>
  30. #ifndef NO_CPP_DEMANGLE
  31. char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
  32. #endif
  33. #include "jack/control.h"
  34. #if defined(REG_RIP)
  35. # define SIGSEGV_STACK_IA64
  36. # define REGFORMAT "%016lx"
  37. #elif defined(REG_EIP)
  38. # define SIGSEGV_STACK_X86
  39. # define REGFORMAT "%08x"
  40. #else
  41. # define SIGSEGV_STACK_GENERIC
  42. # define REGFORMAT "%x"
  43. #endif
  44. #ifdef __APPLE__
  45. // TODO : does not compile yet on OSX
  46. static void signal_segv(int signum, siginfo_t* info, void*ptr)
  47. {}
  48. #else
  49. #include <ucontext.h>
  50. static void signal_segv(int signum, siginfo_t* info, void*ptr) {
  51. static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
  52. const char *si_code_str;
  53. ucontext_t *ucontext = (ucontext_t*)ptr;
  54. #if (defined(HAVE_UCONTEXT) && defined(HAVE_NGREG)) || defined(HAVE_EXECINFO_H)
  55. size_t i;
  56. #endif
  57. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  58. int f = 0;
  59. Dl_info dlinfo;
  60. void **bp = 0;
  61. void *ip = 0;
  62. #else
  63. void *bt[20];
  64. char **strings;
  65. size_t sz;
  66. #endif
  67. if (signum == SIGSEGV)
  68. {
  69. jack_error("Segmentation Fault!");
  70. }
  71. else if (signum == SIGABRT)
  72. {
  73. jack_error("Abort!");
  74. }
  75. else if (signum == SIGILL)
  76. {
  77. jack_error("Illegal instruction!");
  78. }
  79. else if (signum == SIGFPE)
  80. {
  81. jack_error("Floating point exception!");
  82. }
  83. else
  84. {
  85. jack_error("Unknown bad signal caught!");
  86. }
  87. if (info->si_code >= 0 && info->si_code < 3)
  88. si_code_str = si_codes[info->si_code];
  89. else
  90. si_code_str = "unknown";
  91. jack_error("info.si_signo = %d", signum);
  92. jack_error("info.si_errno = %d", info->si_errno);
  93. jack_error("info.si_code = %d (%s)", info->si_code, si_code_str);
  94. jack_error("info.si_addr = %p", info->si_addr);
  95. #if defined(HAVE_UCONTEXT) && defined(HAVE_NGREG)
  96. for(i = 0; i < NGREG; i++)
  97. jack_error("reg[%02d] = 0x" REGFORMAT, i,
  98. #if defined(HAVE_UCONTEXT_GP_REGS)
  99. ucontext->uc_mcontext.gp_regs[i]
  100. #elif defined(HAVE_UCONTEXT_UC_REGS)
  101. ucontext->uc_mcontext.uc_regs[i]
  102. #elif defined(HAVE_UCONTEXT_MC_GREGS)
  103. ucontext->uc_mcontext.mc_gregs[i]
  104. #elif defined(HAVE_UCONTEXT_GREGS)
  105. ucontext->uc_mcontext.gregs[i]
  106. #endif
  107. );
  108. #endif /* defined(HAVE_UCONTEXT) && defined(HAVE_NGREG) */
  109. #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
  110. # if defined(SIGSEGV_STACK_IA64)
  111. ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
  112. bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
  113. # elif defined(SIGSEGV_STACK_X86)
  114. ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
  115. bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
  116. # endif
  117. jack_error("Stack trace:");
  118. while(bp && ip) {
  119. if(!dladdr(ip, &dlinfo))
  120. break;
  121. const char *symname = dlinfo.dli_sname;
  122. #ifndef NO_CPP_DEMANGLE
  123. int status;
  124. char *tmp = __cxa_demangle(symname, NULL, 0, &status);
  125. if(status == 0 && tmp)
  126. symname = tmp;
  127. #endif
  128. jack_error("% 2d: %p <%s+%u> (%s)",
  129. ++f,
  130. ip,
  131. symname,
  132. (unsigned)(ip - dlinfo.dli_saddr),
  133. dlinfo.dli_fname);
  134. #ifndef NO_CPP_DEMANGLE
  135. if(tmp)
  136. free(tmp);
  137. #endif
  138. if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
  139. break;
  140. ip = bp[1];
  141. bp = (void**)bp[0];
  142. }
  143. #else
  144. # ifdef HAVE_EXECINFO_H
  145. jack_error("Stack trace (non-dedicated):");
  146. sz = backtrace(bt, 20);
  147. strings = backtrace_symbols(bt, sz);
  148. for(i = 0; i < sz; ++i)
  149. jack_error("%s", strings[i]);
  150. # else
  151. jack_error("Stack trace not available");
  152. # endif
  153. #endif
  154. jack_error("End of stack trace");
  155. exit (-1);
  156. }
  157. #endif
  158. int setup_sigsegv() {
  159. struct sigaction action;
  160. memset(&action, 0, sizeof(action));
  161. action.sa_sigaction = signal_segv;
  162. #ifdef SA_SIGINFO
  163. action.sa_flags = SA_SIGINFO;
  164. #endif
  165. if(sigaction(SIGSEGV, &action, NULL) < 0) {
  166. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  167. return 0;
  168. }
  169. if(sigaction(SIGILL, &action, NULL) < 0) {
  170. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  171. return 0;
  172. }
  173. if(sigaction(SIGABRT, &action, NULL) < 0) {
  174. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  175. return 0;
  176. }
  177. if(sigaction(SIGFPE, &action, NULL) < 0) {
  178. jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
  179. return 0;
  180. }
  181. return 1;
  182. }
  183. #ifndef SIGSEGV_NO_AUTO_INIT
  184. static void __attribute((constructor)) init(void) {
  185. setup_sigsegv();
  186. }
  187. #endif