Assert.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Assert.h"
  16. #include "util/Gcc.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #ifdef __GLIBC__
  21. # include <execinfo.h>
  22. #endif
  23. Gcc_PRINTF(1, 2)
  24. void Assert_failure(const char* format, ...)
  25. {
  26. printf("\n\n");
  27. fflush(stdout);
  28. fflush(stderr);
  29. va_list args;
  30. va_start(args, format);
  31. vfprintf(stderr, format, args);
  32. fflush(stderr);
  33. #ifdef __GLIBC__
  34. void *array[20];
  35. size_t size = backtrace(array, 20);
  36. char **strings = backtrace_symbols(array, size);
  37. fprintf(stderr, "Backtrace (%zd frames):\n", size);
  38. for (size_t i = 0; i < size; i++)
  39. {
  40. fprintf(stderr, " %s\n", strings[i]);
  41. }
  42. fflush(stderr);
  43. free(strings);
  44. #endif
  45. abort();
  46. va_end(args);
  47. }