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.

193 lines
4.6KB

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