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.

183 lines
4.5KB

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