Error.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #if defined __GNUG__ && defined(LINUX)
  27. #include <cxxabi.h>
  28. #include <execinfo.h>
  29. #endif
  30. #include "Config.h"
  31. #pragma once
  32. namespace crown
  33. {
  34. namespace error
  35. {
  36. inline void log_backtrace()
  37. {
  38. #if defined __GNUG__ && defined(LINUX)
  39. void* array[50];
  40. int size = backtrace(array, 50);
  41. char** messages = backtrace_symbols(array, size);
  42. // skip first stack frame (points here)
  43. for (int i = 1; i < size && messages != NULL; ++i)
  44. {
  45. char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
  46. // find parantheses and +address offset surrounding mangled name
  47. for (char *p = messages[i]; *p; ++p)
  48. {
  49. if (*p == '(')
  50. {
  51. mangled_name = p;
  52. }
  53. else if (*p == '+')
  54. {
  55. offset_begin = p;
  56. }
  57. else if (*p == ')')
  58. {
  59. offset_end = p;
  60. break;
  61. }
  62. }
  63. // if the line could be processed, attempt to demangle the symbol
  64. if (mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
  65. {
  66. *mangled_name++ = '\0';
  67. *offset_begin++ = '\0';
  68. *offset_end++ = '\0';
  69. int status;
  70. char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
  71. printf("\t[%d] %s: (%s)+%s %s\n", i, messages[i], (status == 0 ? real_name : mangled_name), offset_begin, offset_end);
  72. free(real_name);
  73. }
  74. // otherwise, print the whole line
  75. else
  76. {
  77. printf("\t[%d] %s\n", i, messages[i]);
  78. }
  79. }
  80. free(messages);
  81. #endif
  82. }
  83. /// Aborts the program execution logging an error message and the stacktrace if
  84. /// the platform supports it.
  85. inline void abort(const char* file, int line, const char* message, ...)
  86. {
  87. va_list ap;
  88. va_start(ap, message);
  89. vprintf(message, ap);
  90. va_end(ap);
  91. printf("\tIn: %s:%d\n\n", file, line);
  92. printf("Backtrace:\n\n");
  93. fflush(0);
  94. log_backtrace();
  95. exit(EXIT_FAILURE);
  96. }
  97. } // namespace error
  98. } // namespace crown