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.

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