summaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/llvm.c
blob: 512d3620e9f9439b9bf81bbc076c0720e1d128e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <bpf/libbpf.h>
#include <util/llvm-utils.h>
#include <util/cache.h>
#include "tests.h"
#include "debug.h"

static int perf_config_cb(const char *var, const char *val,
			  void *arg __maybe_unused)
{
	return perf_default_config(var, val, arg);
}

/*
 * Randomly give it a "version" section since we don't really load it
 * into kernel
 */
static const char test_bpf_prog[] =
	"__attribute__((section(\"do_fork\"), used)) "
	"int fork(void *ctx) {return 0;} "
	"char _license[] __attribute__((section(\"license\"), used)) = \"GPL\";"
	"int _version __attribute__((section(\"version\"), used)) = 0x40100;";

#ifdef HAVE_LIBBPF_SUPPORT
static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
{
	struct bpf_object *obj;

	obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
	if (!obj)
		return -1;
	bpf_object__close(obj);
	return 0;
}
#else
static int test__bpf_parsing(void *obj_buf __maybe_unused,
			     size_t obj_buf_sz __maybe_unused)
{
	pr_debug("Skip bpf parsing\n");
	return 0;
}
#endif

int test__llvm(void)
{
	char *tmpl_new, *clang_opt_new;
	void *obj_buf;
	size_t obj_buf_sz;
	int err, old_verbose;

	perf_config(perf_config_cb, NULL);

	/*
	 * Skip this test if user's .perfconfig doesn't set [llvm] section
	 * and clang is not found in $PATH, and this is not perf test -v
	 */
	if (verbose == 0 && !llvm_param.user_set_param && llvm__search_clang()) {
		pr_debug("No clang and no verbosive, skip this test\n");
		return TEST_SKIP;
	}

	old_verbose = verbose;
	/*
	 * llvm is verbosity when error. Suppress all error output if
	 * not 'perf test -v'.
	 */
	if (verbose == 0)
		verbose = -1;

	if (!llvm_param.clang_bpf_cmd_template)
		return -1;

	if (!llvm_param.clang_opt)
		llvm_param.clang_opt = strdup("");

	err = asprintf(&tmpl_new, "echo '%s' | %s", test_bpf_prog,
		       llvm_param.clang_bpf_cmd_template);
	if (err < 0)
		return -1;
	err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
	if (err < 0)
		return -1;

	llvm_param.clang_bpf_cmd_template = tmpl_new;
	llvm_param.clang_opt = clang_opt_new;
	err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz);

	verbose = old_verbose;
	if (err)
		return TEST_FAIL;

	err = test__bpf_parsing(obj_buf, obj_buf_sz);
	free(obj_buf);
	return err;
}
OpenPOWER on IntegriCloud