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