2 * tools/testing/selftests/kvm/lib/assert.c
4 * Copyright (C) 2018, Google LLC.
6 * This work is licensed under the terms of the GNU GPL, version 2.
9 #define _GNU_SOURCE /* for getline(3) and strchrnul(3)*/
11 #include "test_util.h"
14 #include <sys/syscall.h>
16 #include "../../kselftest.h"
18 /* Dumps the current stack trace to stderr. */
19 static void __attribute__((noinline
)) test_dump_stack(void);
20 static void test_dump_stack(void)
23 * Build and run this command:
25 * addr2line -s -e /proc/$PPID/exe -fpai {backtrace addresses} | \
26 * grep -v test_dump_stack | cat -n 1>&2
28 * Note that the spacing is different and there's no newline.
33 const char *addr2line
= "addr2line -s -e /proc/$PPID/exe -fpai";
34 const char *pipeline
= "|cat -n 1>&2";
35 char cmd
[strlen(addr2line
) + strlen(pipeline
) +
36 /* N bytes per addr * 2 digits per byte + 1 space per addr: */
37 n
* (((sizeof(void *)) * 2) + 1) +
38 /* Null terminator: */
42 n
= backtrace(stack
, n
);
44 c
+= sprintf(c
, "%s", addr2line
);
46 * Skip the first 3 frames: backtrace, test_dump_stack, and
47 * test_assert. We hope that backtrace isn't inlined and the other two
48 * we've declared noinline.
50 for (i
= 2; i
< n
; i
++)
51 c
+= sprintf(c
, " %lx", ((unsigned long) stack
[i
]) - 1);
52 c
+= sprintf(c
, "%s", pipeline
);
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wunused-result"
56 #pragma GCC diagnostic pop
59 static pid_t
_gettid(void)
61 return syscall(SYS_gettid
);
64 void __attribute__((noinline
))
65 test_assert(bool exp
, const char *exp_str
,
66 const char *file
, unsigned int line
, const char *fmt
, ...)
73 fprintf(stderr
, "==== Test Assertion Failure ====\n"
75 " pid=%d tid=%d - %s\n",
76 file
, line
, exp_str
, getpid(), _gettid(),
81 vfprintf(stderr
, fmt
, ap
);
87 ksft_exit_skip("Access denied - Exiting.\n");