HID: wacom: Use correct report to query pen ID from INTUOSHT2 devices
[cascardo/linux.git] / tools / perf / tests / llvm.c
1 #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
8 static 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  */
18 static 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
25 static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
26 {
27         struct bpf_object *obj;
28
29         obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
30         if (!obj)
31                 return -1;
32         bpf_object__close(obj);
33         return 0;
34 }
35 #else
36 static int test__bpf_parsing(void *obj_buf __maybe_unused,
37                              size_t obj_buf_sz __maybe_unused)
38 {
39         fprintf(stderr, " (skip bpf parsing)");
40         return 0;
41 }
42 #endif
43
44 int 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()) {
58                 fprintf(stderr, " (no clang, try 'perf test -v LLVM')");
59                 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;
89         if (err) {
90                 if (!verbose)
91                         fprintf(stderr, " (use -v to see error message)");
92                 return -1;
93         }
94
95         err = test__bpf_parsing(obj_buf, obj_buf_sz);
96         free(obj_buf);
97         return err;
98 }