blob: 512d3620e9f9439b9bf81bbc076c0720e1d128e7 [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>
5#include "tests.h"
6#include "debug.h"
7
8static int perf_config_cb(const char *var, const char *val,
9 void *arg __maybe_unused)
10{
11 return perf_default_config(var, val, arg);
12}
13
14/*
15 * Randomly give it a "version" section since we don't really load it
16 * into kernel
17 */
18static const char test_bpf_prog[] =
19 "__attribute__((section(\"do_fork\"), used)) "
20 "int fork(void *ctx) {return 0;} "
21 "char _license[] __attribute__((section(\"license\"), used)) = \"GPL\";"
22 "int _version __attribute__((section(\"version\"), used)) = 0x40100;";
23
24#ifdef HAVE_LIBBPF_SUPPORT
25static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
26{
27 struct bpf_object *obj;
28
Wang Nanacf860a2015-08-27 02:30:55 +000029 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
Wang Nan9bc898c2015-07-08 10:04:02 +000030 if (!obj)
31 return -1;
32 bpf_object__close(obj);
33 return 0;
34}
35#else
36static int test__bpf_parsing(void *obj_buf __maybe_unused,
37 size_t obj_buf_sz __maybe_unused)
38{
Wang Nan597bdeb2015-11-03 10:44:42 +000039 pr_debug("Skip bpf parsing\n");
Wang Nan9bc898c2015-07-08 10:04:02 +000040 return 0;
41}
42#endif
43
44int test__llvm(void)
45{
46 char *tmpl_new, *clang_opt_new;
47 void *obj_buf;
48 size_t obj_buf_sz;
49 int err, old_verbose;
50
51 perf_config(perf_config_cb, NULL);
52
53 /*
54 * Skip this test if user's .perfconfig doesn't set [llvm] section
55 * and clang is not found in $PATH, and this is not perf test -v
56 */
57 if (verbose == 0 && !llvm_param.user_set_param && llvm__search_clang()) {
Wang Nan597bdeb2015-11-03 10:44:42 +000058 pr_debug("No clang and no verbosive, skip this test\n");
Wang Nan9bc898c2015-07-08 10:04:02 +000059 return TEST_SKIP;
60 }
61
62 old_verbose = verbose;
63 /*
64 * llvm is verbosity when error. Suppress all error output if
65 * not 'perf test -v'.
66 */
67 if (verbose == 0)
68 verbose = -1;
69
70 if (!llvm_param.clang_bpf_cmd_template)
71 return -1;
72
73 if (!llvm_param.clang_opt)
74 llvm_param.clang_opt = strdup("");
75
76 err = asprintf(&tmpl_new, "echo '%s' | %s", test_bpf_prog,
77 llvm_param.clang_bpf_cmd_template);
78 if (err < 0)
79 return -1;
80 err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
81 if (err < 0)
82 return -1;
83
84 llvm_param.clang_bpf_cmd_template = tmpl_new;
85 llvm_param.clang_opt = clang_opt_new;
86 err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz);
87
88 verbose = old_verbose;
Wang Nan597bdeb2015-11-03 10:44:42 +000089 if (err)
90 return TEST_FAIL;
Wang Nan9bc898c2015-07-08 10:04:02 +000091
92 err = test__bpf_parsing(obj_buf, obj_buf_sz);
93 free(obj_buf);
94 return err;
95}