blob: 06f45c1d42561df030e35d55078d70d910e79998 [file] [log] [blame]
Wang Nan9bc898c2015-07-08 10:04:02 +00001#include <stdio.h>
2#include <bpf/libbpf.h>
3#include <util/llvm-utils.h>
4#include <util/cache.h>
Wang Nanb31de012015-11-06 13:49:41 +00005#include "llvm.h"
Wang Nan9bc898c2015-07-08 10:04:02 +00006#include "tests.h"
7#include "debug.h"
8
9static int perf_config_cb(const char *var, const char *val,
10 void *arg __maybe_unused)
11{
12 return perf_default_config(var, val, arg);
13}
14
Wang Nan9bc898c2015-07-08 10:04:02 +000015#ifdef HAVE_LIBBPF_SUPPORT
16static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
17{
18 struct bpf_object *obj;
19
Wang Nanacf860a2015-08-27 02:30:55 +000020 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
Wang Nan6371ca32015-11-06 13:49:37 +000021 if (IS_ERR(obj))
Wang Nanb31de012015-11-06 13:49:41 +000022 return TEST_FAIL;
Wang Nan9bc898c2015-07-08 10:04:02 +000023 bpf_object__close(obj);
Wang Nanb31de012015-11-06 13:49:41 +000024 return TEST_OK;
Wang Nan9bc898c2015-07-08 10:04:02 +000025}
26#else
27static int test__bpf_parsing(void *obj_buf __maybe_unused,
28 size_t obj_buf_sz __maybe_unused)
29{
Wang Nan597bdeb2015-11-03 10:44:42 +000030 pr_debug("Skip bpf parsing\n");
Wang Nanb31de012015-11-06 13:49:41 +000031 return TEST_OK;
Wang Nan9bc898c2015-07-08 10:04:02 +000032}
33#endif
34
Wang Nanb31de012015-11-06 13:49:41 +000035static struct {
36 const char *source;
37 const char *desc;
38} bpf_source_table[__LLVM_TESTCASE_MAX] = {
39 [LLVM_TESTCASE_BASE] = {
40 .source = test_llvm__bpf_base_prog,
41 .desc = "Basic BPF llvm compiling test",
42 },
Wang Nan7af3f3d2015-11-06 13:49:42 +000043 [LLVM_TESTCASE_KBUILD] = {
44 .source = test_llvm__bpf_test_kbuild_prog,
45 .desc = "Test kbuild searching",
46 },
Wang Nanbbb7d492015-11-16 12:10:14 +000047 [LLVM_TESTCASE_BPF_PROLOGUE] = {
48 .source = test_llvm__bpf_test_prologue_prog,
Wang Nane8c6d502015-11-17 08:32:48 +000049 .desc = "Compile source for BPF prologue generation test",
Wang Nanbbb7d492015-11-16 12:10:14 +000050 },
Wang Nanb31de012015-11-06 13:49:41 +000051};
52
53
54int
55test_llvm__fetch_bpf_obj(void **p_obj_buf,
56 size_t *p_obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -030057 enum test_llvm__testcase idx,
Wang Nanb31de012015-11-06 13:49:41 +000058 bool force)
Wang Nan9bc898c2015-07-08 10:04:02 +000059{
Wang Nanb31de012015-11-06 13:49:41 +000060 const char *source;
61 const char *desc;
62 const char *tmpl_old, *clang_opt_old;
63 char *tmpl_new = NULL, *clang_opt_new = NULL;
64 int err, old_verbose, ret = TEST_FAIL;
65
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -030066 if (idx >= __LLVM_TESTCASE_MAX)
Wang Nanb31de012015-11-06 13:49:41 +000067 return TEST_FAIL;
68
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -030069 source = bpf_source_table[idx].source;
70 desc = bpf_source_table[idx].desc;
Wang Nan9bc898c2015-07-08 10:04:02 +000071
72 perf_config(perf_config_cb, NULL);
73
74 /*
75 * Skip this test if user's .perfconfig doesn't set [llvm] section
76 * and clang is not found in $PATH, and this is not perf test -v
77 */
Wang Nanb31de012015-11-06 13:49:41 +000078 if (!force && (verbose == 0 &&
79 !llvm_param.user_set_param &&
80 llvm__search_clang())) {
Wang Nan597bdeb2015-11-03 10:44:42 +000081 pr_debug("No clang and no verbosive, skip this test\n");
Wang Nan9bc898c2015-07-08 10:04:02 +000082 return TEST_SKIP;
83 }
84
Wang Nan9bc898c2015-07-08 10:04:02 +000085 /*
86 * llvm is verbosity when error. Suppress all error output if
87 * not 'perf test -v'.
88 */
Wang Nanb31de012015-11-06 13:49:41 +000089 old_verbose = verbose;
Wang Nan9bc898c2015-07-08 10:04:02 +000090 if (verbose == 0)
91 verbose = -1;
92
Wang Nanb31de012015-11-06 13:49:41 +000093 *p_obj_buf = NULL;
94 *p_obj_buf_sz = 0;
95
Wang Nan9bc898c2015-07-08 10:04:02 +000096 if (!llvm_param.clang_bpf_cmd_template)
Wang Nanb31de012015-11-06 13:49:41 +000097 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +000098
99 if (!llvm_param.clang_opt)
100 llvm_param.clang_opt = strdup("");
101
Wang Nanb31de012015-11-06 13:49:41 +0000102 err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
103 llvm_param.clang_bpf_cmd_template,
104 old_verbose ? "" : " 2>/dev/null");
Wang Nan9bc898c2015-07-08 10:04:02 +0000105 if (err < 0)
Wang Nanb31de012015-11-06 13:49:41 +0000106 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000107 err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
108 if (err < 0)
Wang Nanb31de012015-11-06 13:49:41 +0000109 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000110
Wang Nanb31de012015-11-06 13:49:41 +0000111 tmpl_old = llvm_param.clang_bpf_cmd_template;
Wang Nan9bc898c2015-07-08 10:04:02 +0000112 llvm_param.clang_bpf_cmd_template = tmpl_new;
Wang Nanb31de012015-11-06 13:49:41 +0000113 clang_opt_old = llvm_param.clang_opt;
Wang Nan9bc898c2015-07-08 10:04:02 +0000114 llvm_param.clang_opt = clang_opt_new;
Wang Nanb31de012015-11-06 13:49:41 +0000115
116 err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);
117
118 llvm_param.clang_bpf_cmd_template = tmpl_old;
119 llvm_param.clang_opt = clang_opt_old;
Wang Nan9bc898c2015-07-08 10:04:02 +0000120
121 verbose = old_verbose;
Wang Nan597bdeb2015-11-03 10:44:42 +0000122 if (err)
Wang Nanb31de012015-11-06 13:49:41 +0000123 goto out;
Wang Nan9bc898c2015-07-08 10:04:02 +0000124
Wang Nanb31de012015-11-06 13:49:41 +0000125 ret = TEST_OK;
126out:
127 free(tmpl_new);
128 free(clang_opt_new);
129 if (ret != TEST_OK)
130 pr_debug("Failed to compile test case: '%s'\n", desc);
131 return ret;
132}
133
Wang Nane8c6d502015-11-17 08:32:48 +0000134int test__llvm(int subtest)
Wang Nanb31de012015-11-06 13:49:41 +0000135{
Wang Nane8c6d502015-11-17 08:32:48 +0000136 int ret;
137 void *obj_buf = NULL;
138 size_t obj_buf_sz = 0;
Wang Nanb31de012015-11-06 13:49:41 +0000139
Wang Nane8c6d502015-11-17 08:32:48 +0000140 if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
141 return TEST_FAIL;
Wang Nanb31de012015-11-06 13:49:41 +0000142
Wang Nane8c6d502015-11-17 08:32:48 +0000143 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
144 subtest, false);
Wang Nanb31de012015-11-06 13:49:41 +0000145
Wang Nane8c6d502015-11-17 08:32:48 +0000146 if (ret == TEST_OK) {
147 ret = test__bpf_parsing(obj_buf, obj_buf_sz);
148 if (ret != TEST_OK) {
149 pr_debug("Failed to parse test case '%s'\n",
150 bpf_source_table[subtest].desc);
Wang Nanb31de012015-11-06 13:49:41 +0000151 }
152 }
Wang Nane8c6d502015-11-17 08:32:48 +0000153 free(obj_buf);
154
155 return ret;
156}
157
158int test__llvm_subtest_get_nr(void)
159{
160 return __LLVM_TESTCASE_MAX;
161}
162
163const char *test__llvm_subtest_get_desc(int subtest)
164{
165 if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
166 return NULL;
167
168 return bpf_source_table[subtest].desc;
Wang Nan9bc898c2015-07-08 10:04:02 +0000169}