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.

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