assert.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tools/testing/selftests/kvm/lib/assert.c
  4. *
  5. * Copyright (C) 2018, Google LLC.
  6. */
  7. #define _GNU_SOURCE /* for getline(3) and strchrnul(3)*/
  8. #include "test_util.h"
  9. #include <execinfo.h>
  10. #include <sys/syscall.h>
  11. #include "kselftest.h"
  12. /* Dumps the current stack trace to stderr. */
  13. static void __attribute__((noinline)) test_dump_stack(void);
  14. static void test_dump_stack(void)
  15. {
  16. /*
  17. * Build and run this command:
  18. *
  19. * addr2line -s -e /proc/$PPID/exe -fpai {backtrace addresses} | \
  20. * cat -n 1>&2
  21. *
  22. * Note that the spacing is different and there's no newline.
  23. */
  24. size_t i;
  25. size_t n = 20;
  26. void *stack[n];
  27. const char *addr2line = "addr2line -s -e /proc/$PPID/exe -fpai";
  28. const char *pipeline = "|cat -n 1>&2";
  29. char cmd[strlen(addr2line) + strlen(pipeline) +
  30. /* N bytes per addr * 2 digits per byte + 1 space per addr: */
  31. n * (((sizeof(void *)) * 2) + 1) +
  32. /* Null terminator: */
  33. 1];
  34. char *c = cmd;
  35. n = backtrace(stack, n);
  36. /*
  37. * Skip the first 2 frames, which should be test_dump_stack() and
  38. * test_assert(); both of which are declared noinline. Bail if the
  39. * resulting stack trace would be empty. Otherwise, addr2line will block
  40. * waiting for addresses to be passed in via stdin.
  41. */
  42. if (n <= 2) {
  43. fputs(" (stack trace empty)\n", stderr);
  44. return;
  45. }
  46. c += sprintf(c, "%s", addr2line);
  47. for (i = 2; i < n; i++)
  48. c += sprintf(c, " %lx", ((unsigned long) stack[i]) - 1);
  49. c += sprintf(c, "%s", pipeline);
  50. #pragma GCC diagnostic push
  51. #pragma GCC diagnostic ignored "-Wunused-result"
  52. system(cmd);
  53. #pragma GCC diagnostic pop
  54. }
  55. static pid_t _gettid(void)
  56. {
  57. return syscall(SYS_gettid);
  58. }
  59. void __attribute__((noinline))
  60. test_assert(bool exp, const char *exp_str,
  61. const char *file, unsigned int line, const char *fmt, ...)
  62. {
  63. va_list ap;
  64. if (!(exp)) {
  65. va_start(ap, fmt);
  66. fprintf(stderr, "==== Test Assertion Failure ====\n"
  67. " %s:%u: %s\n"
  68. " pid=%d tid=%d errno=%d - %s\n",
  69. file, line, exp_str, getpid(), _gettid(),
  70. errno, strerror(errno));
  71. test_dump_stack();
  72. if (fmt) {
  73. fputs(" ", stderr);
  74. vfprintf(stderr, fmt, ap);
  75. fputs("\n", stderr);
  76. }
  77. va_end(ap);
  78. if (errno == EACCES) {
  79. print_skip("Access denied - Exiting");
  80. exit(KSFT_SKIP);
  81. }
  82. exit(254);
  83. }
  84. return;
  85. }