blob: 282c30f6a51d93e03d7e32962735dc3f4c2026c2 [file] [log] [blame]
Wang Nanaa61fd02015-07-21 11:13:34 +00001/*
2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
4 */
5
Arnaldo Carvalho de Melo78478262016-03-23 17:34:29 -03006#include <limits.h>
Wang Nanaa61fd02015-07-21 11:13:34 +00007#include <stdio.h>
Arnaldo Carvalho de Melo78478262016-03-23 17:34:29 -03008#include <stdlib.h>
Wang Nanaa61fd02015-07-21 11:13:34 +00009#include "debug.h"
10#include "llvm-utils.h"
Taeung Song41840d22016-06-23 17:55:17 +090011#include "config.h"
Wang Nanaa61fd02015-07-21 11:13:34 +000012
13#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
Wang Nan59f41af2015-11-04 11:20:04 +000014 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
Wang Nan4a4f66a2015-11-04 11:20:05 +000015 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
Wang Nan59f41af2015-11-04 11:20:04 +000016 "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
17 "-Wno-unused-value -Wno-pointer-sign " \
18 "-working-directory $WORKING_DIR " \
19 "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
Wang Nanaa61fd02015-07-21 11:13:34 +000020
21struct llvm_param llvm_param = {
22 .clang_path = "clang",
23 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
24 .clang_opt = NULL,
25 .kbuild_dir = NULL,
26 .kbuild_opts = NULL,
Wang Nan9bc898c2015-07-08 10:04:02 +000027 .user_set_param = false,
Wang Nanaa61fd02015-07-21 11:13:34 +000028};
29
30int perf_llvm_config(const char *var, const char *value)
31{
32 if (prefixcmp(var, "llvm."))
33 return 0;
34 var += sizeof("llvm.") - 1;
35
36 if (!strcmp(var, "clang-path"))
37 llvm_param.clang_path = strdup(value);
38 else if (!strcmp(var, "clang-bpf-cmd-template"))
39 llvm_param.clang_bpf_cmd_template = strdup(value);
40 else if (!strcmp(var, "clang-opt"))
41 llvm_param.clang_opt = strdup(value);
42 else if (!strcmp(var, "kbuild-dir"))
43 llvm_param.kbuild_dir = strdup(value);
44 else if (!strcmp(var, "kbuild-opts"))
45 llvm_param.kbuild_opts = strdup(value);
Wang Nanf0784642016-06-16 08:02:40 +000046 else if (!strcmp(var, "dump-obj"))
47 llvm_param.dump_obj = !!perf_config_bool(var, value);
Wang Nanaa61fd02015-07-21 11:13:34 +000048 else
49 return -1;
Wang Nan9bc898c2015-07-08 10:04:02 +000050 llvm_param.user_set_param = true;
Wang Nanaa61fd02015-07-21 11:13:34 +000051 return 0;
52}
Wang Nan4cea3a92015-06-11 10:31:09 +000053
54static int
55search_program(const char *def, const char *name,
56 char *output)
57{
58 char *env, *path, *tmp = NULL;
59 char buf[PATH_MAX];
60 int ret;
61
62 output[0] = '\0';
63 if (def && def[0] != '\0') {
64 if (def[0] == '/') {
65 if (access(def, F_OK) == 0) {
66 strlcpy(output, def, PATH_MAX);
67 return 0;
68 }
69 } else if (def[0] != '\0')
70 name = def;
71 }
72
73 env = getenv("PATH");
74 if (!env)
75 return -1;
76 env = strdup(env);
77 if (!env)
78 return -1;
79
80 ret = -ENOENT;
81 path = strtok_r(env, ":", &tmp);
82 while (path) {
83 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
84 if (access(buf, F_OK) == 0) {
85 strlcpy(output, buf, PATH_MAX);
86 ret = 0;
87 break;
88 }
89 path = strtok_r(NULL, ":", &tmp);
90 }
91
92 free(env);
93 return ret;
94}
95
96#define READ_SIZE 4096
97static int
98read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
99{
100 int err = 0;
101 void *buf = NULL;
102 FILE *file = NULL;
103 size_t read_sz = 0, buf_sz = 0;
Arnaldo Carvalho de Melo76267142016-03-23 17:42:21 -0300104 char serr[STRERR_BUFSIZE];
Wang Nan4cea3a92015-06-11 10:31:09 +0000105
106 file = popen(cmd, "r");
107 if (!file) {
108 pr_err("ERROR: unable to popen cmd: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300109 str_error_r(errno, serr, sizeof(serr)));
Wang Nan4cea3a92015-06-11 10:31:09 +0000110 return -EINVAL;
111 }
112
113 while (!feof(file) && !ferror(file)) {
114 /*
115 * Make buf_sz always have obe byte extra space so we
116 * can put '\0' there.
117 */
118 if (buf_sz - read_sz < READ_SIZE + 1) {
119 void *new_buf;
120
121 buf_sz = read_sz + READ_SIZE + 1;
122 new_buf = realloc(buf, buf_sz);
123
124 if (!new_buf) {
125 pr_err("ERROR: failed to realloc memory\n");
126 err = -ENOMEM;
127 goto errout;
128 }
129
130 buf = new_buf;
131 }
132 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
133 }
134
135 if (buf_sz - read_sz < 1) {
136 pr_err("ERROR: internal error\n");
137 err = -EINVAL;
138 goto errout;
139 }
140
141 if (ferror(file)) {
142 pr_err("ERROR: error occurred when reading from pipe: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300143 str_error_r(errno, serr, sizeof(serr)));
Wang Nan4cea3a92015-06-11 10:31:09 +0000144 err = -EIO;
145 goto errout;
146 }
147
148 err = WEXITSTATUS(pclose(file));
149 file = NULL;
150 if (err) {
151 err = -EINVAL;
152 goto errout;
153 }
154
155 /*
156 * If buf is string, give it terminal '\0' to make our life
157 * easier. If buf is not string, that '\0' is out of space
158 * indicated by read_sz so caller won't even notice it.
159 */
160 ((char *)buf)[read_sz] = '\0';
161
162 if (!p_buf)
163 free(buf);
164 else
165 *p_buf = buf;
166
167 if (p_read_sz)
168 *p_read_sz = read_sz;
169 return 0;
170
171errout:
172 if (file)
173 pclose(file);
174 free(buf);
175 if (p_buf)
176 *p_buf = NULL;
177 if (p_read_sz)
178 *p_read_sz = 0;
179 return err;
180}
181
182static inline void
183force_set_env(const char *var, const char *value)
184{
185 if (value) {
186 setenv(var, value, 1);
187 pr_debug("set env: %s=%s\n", var, value);
188 } else {
189 unsetenv(var);
190 pr_debug("unset env: %s\n", var);
191 }
192}
193
194static void
195version_notice(void)
196{
197 pr_err(
198" \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
199" \tYou may want to try git trunk:\n"
200" \t\tgit clone http://llvm.org/git/llvm.git\n"
201" \t\t and\n"
202" \t\tgit clone http://llvm.org/git/clang.git\n\n"
203" \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
204" \tdebian/ubuntu:\n"
205" \t\thttp://llvm.org/apt\n\n"
206" \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
207" \toption in [llvm] section of ~/.perfconfig to:\n\n"
208" \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
209" \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
210" \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
211" \t(Replace /path/to/llc with path to your llc)\n\n"
212);
213}
214
Wang Nand325d782015-07-14 06:40:02 +0000215static int detect_kbuild_dir(char **kbuild_dir)
216{
217 const char *test_dir = llvm_param.kbuild_dir;
218 const char *prefix_dir = "";
219 const char *suffix_dir = "";
220
221 char *autoconf_path;
Wang Nand325d782015-07-14 06:40:02 +0000222
223 int err;
224
225 if (!test_dir) {
Wang Nan07bc5c62015-11-06 13:55:35 +0000226 /* _UTSNAME_LENGTH is 65 */
227 char release[128];
Wang Nand325d782015-07-14 06:40:02 +0000228
Wang Nan07bc5c62015-11-06 13:55:35 +0000229 err = fetch_kernel_version(NULL, release,
230 sizeof(release));
231 if (err)
232 return -EINVAL;
233
234 test_dir = release;
Wang Nand325d782015-07-14 06:40:02 +0000235 prefix_dir = "/lib/modules/";
236 suffix_dir = "/build";
237 }
238
239 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
240 prefix_dir, test_dir, suffix_dir);
241 if (err < 0)
242 return -ENOMEM;
243
244 if (access(autoconf_path, R_OK) == 0) {
245 free(autoconf_path);
246
247 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
248 suffix_dir);
249 if (err < 0)
250 return -ENOMEM;
251 return 0;
252 }
253 free(autoconf_path);
254 return -ENOENT;
255}
256
Wang Nan0c6d18b2015-06-11 11:25:49 +0000257static const char *kinc_fetch_script =
258"#!/usr/bin/env sh\n"
259"if ! test -d \"$KBUILD_DIR\"\n"
260"then\n"
261" exit -1\n"
262"fi\n"
263"if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
264"then\n"
265" exit -1\n"
266"fi\n"
267"TMPDIR=`mktemp -d`\n"
268"if test -z \"$TMPDIR\"\n"
269"then\n"
270" exit -1\n"
271"fi\n"
272"cat << EOF > $TMPDIR/Makefile\n"
273"obj-y := dummy.o\n"
274"\\$(obj)/%.o: \\$(src)/%.c\n"
275"\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
276"EOF\n"
277"touch $TMPDIR/dummy.c\n"
278"make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
279"RET=$?\n"
280"rm -rf $TMPDIR\n"
281"exit $RET\n";
282
Wang Nand325d782015-07-14 06:40:02 +0000283static inline void
Wang Nan0c6d18b2015-06-11 11:25:49 +0000284get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
Wang Nand325d782015-07-14 06:40:02 +0000285{
286 int err;
287
Wang Nan0c6d18b2015-06-11 11:25:49 +0000288 if (!kbuild_dir || !kbuild_include_opts)
Wang Nand325d782015-07-14 06:40:02 +0000289 return;
290
291 *kbuild_dir = NULL;
Wang Nan0c6d18b2015-06-11 11:25:49 +0000292 *kbuild_include_opts = NULL;
Wang Nand325d782015-07-14 06:40:02 +0000293
294 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
295 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
296 pr_debug("Skip kbuild options detection.\n");
297 return;
298 }
299
300 err = detect_kbuild_dir(kbuild_dir);
301 if (err) {
302 pr_warning(
303"WARNING:\tunable to get correct kernel building directory.\n"
304"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
305" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
306" \tdetection.\n\n");
307 return;
308 }
Wang Nan0c6d18b2015-06-11 11:25:49 +0000309
310 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
311 force_set_env("KBUILD_DIR", *kbuild_dir);
312 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
313 err = read_from_pipe(kinc_fetch_script,
314 (void **)kbuild_include_opts,
315 NULL);
316 if (err) {
317 pr_warning(
318"WARNING:\tunable to get kernel include directories from '%s'\n"
319"Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
320" \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
321" \toption in [llvm] to \"\" to suppress this detection.\n\n",
322 *kbuild_dir);
323
324 free(*kbuild_dir);
325 *kbuild_dir = NULL;
326 return;
327 }
328
329 pr_debug("include option is set to %s\n", *kbuild_include_opts);
Wang Nand325d782015-07-14 06:40:02 +0000330}
331
Wang Nanf0784642016-06-16 08:02:40 +0000332static void
333dump_obj(const char *path, void *obj_buf, size_t size)
334{
335 char *obj_path = strdup(path);
336 FILE *fp;
337 char *p;
338
339 if (!obj_path) {
340 pr_warning("WARNING: No enough memory, skip object dumping\n");
341 return;
342 }
343
344 p = strrchr(obj_path, '.');
345 if (!p || (strcmp(p, ".c") != 0)) {
346 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
347 obj_path);
348 goto out;
349 }
350
351 p[1] = 'o';
352 fp = fopen(obj_path, "wb");
353 if (!fp) {
354 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
355 obj_path, strerror(errno));
356 goto out;
357 }
358
359 pr_info("LLVM: dumping %s\n", obj_path);
360 if (fwrite(obj_buf, size, 1, fp) != 1)
361 pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n",
362 obj_path, strerror(errno));
363 fclose(fp);
364out:
365 free(obj_path);
366}
367
Wang Nan4cea3a92015-06-11 10:31:09 +0000368int llvm__compile_bpf(const char *path, void **p_obj_buf,
369 size_t *p_obj_buf_sz)
370{
Wang Nan07bc5c62015-11-06 13:55:35 +0000371 size_t obj_buf_sz;
372 void *obj_buf = NULL;
Wang Nan59f41af2015-11-04 11:20:04 +0000373 int err, nr_cpus_avail;
Wang Nan07bc5c62015-11-06 13:55:35 +0000374 unsigned int kernel_version;
Wang Nan4a4f66a2015-11-04 11:20:05 +0000375 char linux_version_code_str[64];
Wang Nan4cea3a92015-06-11 10:31:09 +0000376 const char *clang_opt = llvm_param.clang_opt;
Arnaldo Carvalho de Melo78478262016-03-23 17:34:29 -0300377 char clang_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
378 char serr[STRERR_BUFSIZE];
Wang Nan0c6d18b2015-06-11 11:25:49 +0000379 char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
Wang Nan07bc5c62015-11-06 13:55:35 +0000380 const char *template = llvm_param.clang_bpf_cmd_template;
Wang Nan4cea3a92015-06-11 10:31:09 +0000381
Arnaldo Carvalho de Melo78478262016-03-23 17:34:29 -0300382 if (path[0] != '-' && realpath(path, abspath) == NULL) {
383 err = errno;
384 pr_err("ERROR: problems with path %s: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300385 path, str_error_r(err, serr, sizeof(serr)));
Arnaldo Carvalho de Melo78478262016-03-23 17:34:29 -0300386 return -err;
387 }
388
Wang Nan4cea3a92015-06-11 10:31:09 +0000389 if (!template)
390 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
391
392 err = search_program(llvm_param.clang_path,
393 "clang", clang_path);
394 if (err) {
395 pr_err(
396"ERROR:\tunable to find clang.\n"
397"Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
398" \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
399 version_notice();
400 return -ENOENT;
401 }
402
Wang Nand325d782015-07-14 06:40:02 +0000403 /*
404 * This is an optional work. Even it fail we can continue our
405 * work. Needn't to check error return.
406 */
Wang Nan0c6d18b2015-06-11 11:25:49 +0000407 get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
Wang Nand325d782015-07-14 06:40:02 +0000408
Wang Nan59f41af2015-11-04 11:20:04 +0000409 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
410 if (nr_cpus_avail <= 0) {
411 pr_err(
412"WARNING:\tunable to get available CPUs in this system: %s\n"
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300413" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
Wang Nan59f41af2015-11-04 11:20:04 +0000414 nr_cpus_avail = 128;
415 }
416 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
417 nr_cpus_avail);
418
Wang Nan07bc5c62015-11-06 13:55:35 +0000419 if (fetch_kernel_version(&kernel_version, NULL, 0))
420 kernel_version = 0;
421
Wang Nan4a4f66a2015-11-04 11:20:05 +0000422 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
Wang Nan07bc5c62015-11-06 13:55:35 +0000423 "0x%x", kernel_version);
Wang Nan4a4f66a2015-11-04 11:20:05 +0000424
Wang Nan59f41af2015-11-04 11:20:04 +0000425 force_set_env("NR_CPUS", nr_cpus_avail_str);
Wang Nan4a4f66a2015-11-04 11:20:05 +0000426 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
Wang Nan4cea3a92015-06-11 10:31:09 +0000427 force_set_env("CLANG_EXEC", clang_path);
428 force_set_env("CLANG_OPTIONS", clang_opt);
Wang Nan0c6d18b2015-06-11 11:25:49 +0000429 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
Wang Nand325d782015-07-14 06:40:02 +0000430 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
Wang Nan4cea3a92015-06-11 10:31:09 +0000431
432 /*
433 * Since we may reset clang's working dir, path of source file
434 * should be transferred into absolute path, except we want
435 * stdin to be source file (testing).
436 */
437 force_set_env("CLANG_SOURCE",
Arnaldo Carvalho de Melo78478262016-03-23 17:34:29 -0300438 (path[0] == '-') ? path : abspath);
Wang Nan4cea3a92015-06-11 10:31:09 +0000439
440 pr_debug("llvm compiling command template: %s\n", template);
441 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
442 if (err) {
443 pr_err("ERROR:\tunable to compile %s\n", path);
444 pr_err("Hint:\tCheck error message shown above.\n");
445 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
446 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
447 pr_err(" \twith proper -I and -D options.\n");
448 goto errout;
449 }
450
Wang Nand325d782015-07-14 06:40:02 +0000451 free(kbuild_dir);
Wang Nan0c6d18b2015-06-11 11:25:49 +0000452 free(kbuild_include_opts);
Wang Nanf0784642016-06-16 08:02:40 +0000453
454 if (llvm_param.dump_obj)
455 dump_obj(path, obj_buf, obj_buf_sz);
456
Wang Nan4cea3a92015-06-11 10:31:09 +0000457 if (!p_obj_buf)
458 free(obj_buf);
459 else
460 *p_obj_buf = obj_buf;
461
462 if (p_obj_buf_sz)
463 *p_obj_buf_sz = obj_buf_sz;
464 return 0;
465errout:
Wang Nand325d782015-07-14 06:40:02 +0000466 free(kbuild_dir);
Wang Nan0c6d18b2015-06-11 11:25:49 +0000467 free(kbuild_include_opts);
Wang Nan4cea3a92015-06-11 10:31:09 +0000468 free(obj_buf);
469 if (p_obj_buf)
470 *p_obj_buf = NULL;
471 if (p_obj_buf_sz)
472 *p_obj_buf_sz = 0;
473 return err;
474}
Wang Nan9bc898c2015-07-08 10:04:02 +0000475
476int llvm__search_clang(void)
477{
478 char clang_path[PATH_MAX];
479
480 return search_program(llvm_param.clang_path, "clang", clang_path);
481}