Wang Nan | 9bc898c | 2015-07-08 10:04:02 +0000 | [diff] [blame] | 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 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 29 | obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL); |
Wang Nan | 9bc898c | 2015-07-08 10:04:02 +0000 | [diff] [blame] | 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 | { |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame^] | 39 | pr_debug("Skip bpf parsing\n"); |
Wang Nan | 9bc898c | 2015-07-08 10:04:02 +0000 | [diff] [blame] | 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()) { |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame^] | 58 | pr_debug("No clang and no verbosive, skip this test\n"); |
Wang Nan | 9bc898c | 2015-07-08 10:04:02 +0000 | [diff] [blame] | 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; |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame^] | 89 | if (err) |
| 90 | return TEST_FAIL; |
Wang Nan | 9bc898c | 2015-07-08 10:04:02 +0000 | [diff] [blame] | 91 | |
| 92 | err = test__bpf_parsing(obj_buf, obj_buf_sz); |
| 93 | free(obj_buf); |
| 94 | return err; |
| 95 | } |