Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / llvm-utils.c
blobdbdffb6673feb63b63fc6b7aa0ec0ed167032881
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
4 * Copyright (C) 2015, Huawei Inc.
5 */
7 #include <errno.h>
8 #include <limits.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <linux/err.h>
13 #include <linux/string.h>
14 #include <linux/zalloc.h>
15 #include "debug.h"
16 #include "llvm-utils.h"
17 #include "config.h"
18 #include "util.h"
19 #include <sys/wait.h>
20 #include <subcmd/exec-cmd.h>
22 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
23 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
24 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
25 "$CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS " \
26 "-Wno-unused-value -Wno-pointer-sign " \
27 "-working-directory $WORKING_DIR " \
28 "-c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE"
30 struct llvm_param llvm_param = {
31 .clang_path = "clang",
32 .llc_path = "llc",
33 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
34 .clang_opt = NULL,
35 .opts = NULL,
36 .kbuild_dir = NULL,
37 .kbuild_opts = NULL,
38 .user_set_param = false,
41 int perf_llvm_config(const char *var, const char *value)
43 if (!strstarts(var, "llvm."))
44 return 0;
45 var += sizeof("llvm.") - 1;
47 if (!strcmp(var, "clang-path"))
48 llvm_param.clang_path = strdup(value);
49 else if (!strcmp(var, "clang-bpf-cmd-template"))
50 llvm_param.clang_bpf_cmd_template = strdup(value);
51 else if (!strcmp(var, "clang-opt"))
52 llvm_param.clang_opt = strdup(value);
53 else if (!strcmp(var, "kbuild-dir"))
54 llvm_param.kbuild_dir = strdup(value);
55 else if (!strcmp(var, "kbuild-opts"))
56 llvm_param.kbuild_opts = strdup(value);
57 else if (!strcmp(var, "dump-obj"))
58 llvm_param.dump_obj = !!perf_config_bool(var, value);
59 else if (!strcmp(var, "opts"))
60 llvm_param.opts = strdup(value);
61 else {
62 pr_debug("Invalid LLVM config option: %s\n", value);
63 return -1;
65 llvm_param.user_set_param = true;
66 return 0;
69 static int
70 search_program(const char *def, const char *name,
71 char *output)
73 char *env, *path, *tmp = NULL;
74 char buf[PATH_MAX];
75 int ret;
77 output[0] = '\0';
78 if (def && def[0] != '\0') {
79 if (def[0] == '/') {
80 if (access(def, F_OK) == 0) {
81 strlcpy(output, def, PATH_MAX);
82 return 0;
84 } else if (def[0] != '\0')
85 name = def;
88 env = getenv("PATH");
89 if (!env)
90 return -1;
91 env = strdup(env);
92 if (!env)
93 return -1;
95 ret = -ENOENT;
96 path = strtok_r(env, ":", &tmp);
97 while (path) {
98 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
99 if (access(buf, F_OK) == 0) {
100 strlcpy(output, buf, PATH_MAX);
101 ret = 0;
102 break;
104 path = strtok_r(NULL, ":", &tmp);
107 free(env);
108 return ret;
111 #define READ_SIZE 4096
112 static int
113 read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
115 int err = 0;
116 void *buf = NULL;
117 FILE *file = NULL;
118 size_t read_sz = 0, buf_sz = 0;
119 char serr[STRERR_BUFSIZE];
121 file = popen(cmd, "r");
122 if (!file) {
123 pr_err("ERROR: unable to popen cmd: %s\n",
124 str_error_r(errno, serr, sizeof(serr)));
125 return -EINVAL;
128 while (!feof(file) && !ferror(file)) {
130 * Make buf_sz always have obe byte extra space so we
131 * can put '\0' there.
133 if (buf_sz - read_sz < READ_SIZE + 1) {
134 void *new_buf;
136 buf_sz = read_sz + READ_SIZE + 1;
137 new_buf = realloc(buf, buf_sz);
139 if (!new_buf) {
140 pr_err("ERROR: failed to realloc memory\n");
141 err = -ENOMEM;
142 goto errout;
145 buf = new_buf;
147 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
150 if (buf_sz - read_sz < 1) {
151 pr_err("ERROR: internal error\n");
152 err = -EINVAL;
153 goto errout;
156 if (ferror(file)) {
157 pr_err("ERROR: error occurred when reading from pipe: %s\n",
158 str_error_r(errno, serr, sizeof(serr)));
159 err = -EIO;
160 goto errout;
163 err = WEXITSTATUS(pclose(file));
164 file = NULL;
165 if (err) {
166 err = -EINVAL;
167 goto errout;
171 * If buf is string, give it terminal '\0' to make our life
172 * easier. If buf is not string, that '\0' is out of space
173 * indicated by read_sz so caller won't even notice it.
175 ((char *)buf)[read_sz] = '\0';
177 if (!p_buf)
178 free(buf);
179 else
180 *p_buf = buf;
182 if (p_read_sz)
183 *p_read_sz = read_sz;
184 return 0;
186 errout:
187 if (file)
188 pclose(file);
189 free(buf);
190 if (p_buf)
191 *p_buf = NULL;
192 if (p_read_sz)
193 *p_read_sz = 0;
194 return err;
197 static inline void
198 force_set_env(const char *var, const char *value)
200 if (value) {
201 setenv(var, value, 1);
202 pr_debug("set env: %s=%s\n", var, value);
203 } else {
204 unsetenv(var);
205 pr_debug("unset env: %s\n", var);
209 static void
210 version_notice(void)
212 pr_err(
213 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
214 " \tYou may want to try git trunk:\n"
215 " \t\tgit clone http://llvm.org/git/llvm.git\n"
216 " \t\t and\n"
217 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
218 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
219 " \tdebian/ubuntu:\n"
220 " \t\thttp://llvm.org/apt\n\n"
221 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
222 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
223 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS \\\n"
224 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
225 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
226 " \t(Replace /path/to/llc with path to your llc)\n\n"
230 static int detect_kbuild_dir(char **kbuild_dir)
232 const char *test_dir = llvm_param.kbuild_dir;
233 const char *prefix_dir = "";
234 const char *suffix_dir = "";
236 /* _UTSNAME_LENGTH is 65 */
237 char release[128];
239 char *autoconf_path;
241 int err;
243 if (!test_dir) {
244 err = fetch_kernel_version(NULL, release,
245 sizeof(release));
246 if (err)
247 return -EINVAL;
249 test_dir = release;
250 prefix_dir = "/lib/modules/";
251 suffix_dir = "/build";
254 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
255 prefix_dir, test_dir, suffix_dir);
256 if (err < 0)
257 return -ENOMEM;
259 if (access(autoconf_path, R_OK) == 0) {
260 free(autoconf_path);
262 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
263 suffix_dir);
264 if (err < 0)
265 return -ENOMEM;
266 return 0;
268 pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n",
269 __func__, autoconf_path);
270 free(autoconf_path);
271 return -ENOENT;
274 static const char *kinc_fetch_script =
275 "#!/usr/bin/env sh\n"
276 "if ! test -d \"$KBUILD_DIR\"\n"
277 "then\n"
278 " exit 1\n"
279 "fi\n"
280 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
281 "then\n"
282 " exit 1\n"
283 "fi\n"
284 "TMPDIR=`mktemp -d`\n"
285 "if test -z \"$TMPDIR\"\n"
286 "then\n"
287 " exit 1\n"
288 "fi\n"
289 "cat << EOF > $TMPDIR/Makefile\n"
290 "obj-y := dummy.o\n"
291 "\\$(obj)/%.o: \\$(src)/%.c\n"
292 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
293 "\t\\$(CC) -c -o \\$@ \\$<\n"
294 "EOF\n"
295 "touch $TMPDIR/dummy.c\n"
296 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
297 "RET=$?\n"
298 "rm -rf $TMPDIR\n"
299 "exit $RET\n";
301 void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
303 static char *saved_kbuild_dir;
304 static char *saved_kbuild_include_opts;
305 int err;
307 if (!kbuild_dir || !kbuild_include_opts)
308 return;
310 *kbuild_dir = NULL;
311 *kbuild_include_opts = NULL;
313 if (saved_kbuild_dir && saved_kbuild_include_opts &&
314 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
315 *kbuild_dir = strdup(saved_kbuild_dir);
316 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
318 if (*kbuild_dir && *kbuild_include_opts)
319 return;
321 zfree(kbuild_dir);
322 zfree(kbuild_include_opts);
324 * Don't fall through: it may breaks saved_kbuild_dir and
325 * saved_kbuild_include_opts if detect them again when
326 * memory is low.
328 return;
331 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
332 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
333 pr_debug("Skip kbuild options detection.\n");
334 goto errout;
337 err = detect_kbuild_dir(kbuild_dir);
338 if (err) {
339 pr_warning(
340 "WARNING:\tunable to get correct kernel building directory.\n"
341 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
342 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
343 " \tdetection.\n\n");
344 goto errout;
347 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
348 force_set_env("KBUILD_DIR", *kbuild_dir);
349 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
350 err = read_from_pipe(kinc_fetch_script,
351 (void **)kbuild_include_opts,
352 NULL);
353 if (err) {
354 pr_warning(
355 "WARNING:\tunable to get kernel include directories from '%s'\n"
356 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
357 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
358 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
359 *kbuild_dir);
361 zfree(kbuild_dir);
362 goto errout;
365 pr_debug("include option is set to %s\n", *kbuild_include_opts);
367 saved_kbuild_dir = strdup(*kbuild_dir);
368 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
370 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
371 zfree(&saved_kbuild_dir);
372 zfree(&saved_kbuild_include_opts);
374 return;
375 errout:
376 saved_kbuild_dir = ERR_PTR(-EINVAL);
377 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
380 int llvm__get_nr_cpus(void)
382 static int nr_cpus_avail = 0;
383 char serr[STRERR_BUFSIZE];
385 if (nr_cpus_avail > 0)
386 return nr_cpus_avail;
388 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
389 if (nr_cpus_avail <= 0) {
390 pr_err(
391 "WARNING:\tunable to get available CPUs in this system: %s\n"
392 " \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
393 nr_cpus_avail = 128;
395 return nr_cpus_avail;
398 void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
400 char *obj_path = strdup(path);
401 FILE *fp;
402 char *p;
404 if (!obj_path) {
405 pr_warning("WARNING: Not enough memory, skip object dumping\n");
406 return;
409 p = strrchr(obj_path, '.');
410 if (!p || (strcmp(p, ".c") != 0)) {
411 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
412 obj_path);
413 goto out;
416 p[1] = 'o';
417 fp = fopen(obj_path, "wb");
418 if (!fp) {
419 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
420 obj_path, strerror(errno));
421 goto out;
424 pr_debug("LLVM: dumping %s\n", obj_path);
425 if (fwrite(obj_buf, size, 1, fp) != 1)
426 pr_debug("WARNING: failed to write to file '%s': %s, skip object dumping\n", obj_path, strerror(errno));
427 fclose(fp);
428 out:
429 free(obj_path);
432 int llvm__compile_bpf(const char *path, void **p_obj_buf,
433 size_t *p_obj_buf_sz)
435 size_t obj_buf_sz;
436 void *obj_buf = NULL;
437 int err, nr_cpus_avail;
438 unsigned int kernel_version;
439 char linux_version_code_str[64];
440 const char *clang_opt = llvm_param.clang_opt;
441 char clang_path[PATH_MAX], llc_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
442 char serr[STRERR_BUFSIZE];
443 char *kbuild_dir = NULL, *kbuild_include_opts = NULL,
444 *perf_bpf_include_opts = NULL;
445 const char *template = llvm_param.clang_bpf_cmd_template;
446 char *pipe_template = NULL;
447 const char *opts = llvm_param.opts;
448 char *command_echo = NULL, *command_out;
449 char *perf_include_dir = system_path(PERF_INCLUDE_DIR);
451 if (path[0] != '-' && realpath(path, abspath) == NULL) {
452 err = errno;
453 pr_err("ERROR: problems with path %s: %s\n",
454 path, str_error_r(err, serr, sizeof(serr)));
455 return -err;
458 if (!template)
459 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
461 err = search_program(llvm_param.clang_path,
462 "clang", clang_path);
463 if (err) {
464 pr_err(
465 "ERROR:\tunable to find clang.\n"
466 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
467 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
468 version_notice();
469 return -ENOENT;
473 * This is an optional work. Even it fail we can continue our
474 * work. Needn't to check error return.
476 llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
478 nr_cpus_avail = llvm__get_nr_cpus();
479 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
480 nr_cpus_avail);
482 if (fetch_kernel_version(&kernel_version, NULL, 0))
483 kernel_version = 0;
485 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
486 "0x%x", kernel_version);
487 if (asprintf(&perf_bpf_include_opts, "-I%s/bpf", perf_include_dir) < 0)
488 goto errout;
489 force_set_env("NR_CPUS", nr_cpus_avail_str);
490 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
491 force_set_env("CLANG_EXEC", clang_path);
492 force_set_env("CLANG_OPTIONS", clang_opt);
493 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
494 force_set_env("PERF_BPF_INC_OPTIONS", perf_bpf_include_opts);
495 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
497 if (opts) {
498 err = search_program(llvm_param.llc_path, "llc", llc_path);
499 if (err) {
500 pr_err("ERROR:\tunable to find llc.\n"
501 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
502 " \tand 'llc-path' option in [llvm] section of ~/.perfconfig.\n");
503 version_notice();
504 goto errout;
507 if (asprintf(&pipe_template, "%s -emit-llvm | %s -march=bpf %s -filetype=obj -o -",
508 template, llc_path, opts) < 0) {
509 pr_err("ERROR:\tnot enough memory to setup command line\n");
510 goto errout;
513 template = pipe_template;
518 * Since we may reset clang's working dir, path of source file
519 * should be transferred into absolute path, except we want
520 * stdin to be source file (testing).
522 force_set_env("CLANG_SOURCE",
523 (path[0] == '-') ? path : abspath);
525 pr_debug("llvm compiling command template: %s\n", template);
527 if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
528 goto errout;
530 err = read_from_pipe(command_echo, (void **) &command_out, NULL);
531 if (err)
532 goto errout;
534 pr_debug("llvm compiling command : %s\n", command_out);
536 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
537 if (err) {
538 pr_err("ERROR:\tunable to compile %s\n", path);
539 pr_err("Hint:\tCheck error message shown above.\n");
540 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
541 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
542 pr_err(" \twith proper -I and -D options.\n");
543 goto errout;
546 free(command_echo);
547 free(command_out);
548 free(kbuild_dir);
549 free(kbuild_include_opts);
550 free(perf_bpf_include_opts);
551 free(perf_include_dir);
553 if (!p_obj_buf)
554 free(obj_buf);
555 else
556 *p_obj_buf = obj_buf;
558 if (p_obj_buf_sz)
559 *p_obj_buf_sz = obj_buf_sz;
560 return 0;
561 errout:
562 free(command_echo);
563 free(kbuild_dir);
564 free(kbuild_include_opts);
565 free(obj_buf);
566 free(perf_bpf_include_opts);
567 free(perf_include_dir);
568 free(pipe_template);
569 if (p_obj_buf)
570 *p_obj_buf = NULL;
571 if (p_obj_buf_sz)
572 *p_obj_buf_sz = 0;
573 return err;
576 int llvm__search_clang(void)
578 char clang_path[PATH_MAX];
580 return search_program(llvm_param.clang_path, "clang", clang_path);