1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
4 * Copyright (C) 2015, Huawei Inc.
12 #include <linux/err.h>
13 #include <linux/string.h>
14 #include <linux/zalloc.h>
16 #include "llvm-utils.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",
33 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
38 .user_set_param = false,
41 static void version_notice(void);
43 int perf_llvm_config(const char *var, const char *value)
45 if (!strstarts(var, "llvm."))
47 var += sizeof("llvm.") - 1;
49 if (!strcmp(var, "clang-path"))
50 llvm_param.clang_path = strdup(value);
51 else if (!strcmp(var, "clang-bpf-cmd-template"))
52 llvm_param.clang_bpf_cmd_template = strdup(value);
53 else if (!strcmp(var, "clang-opt"))
54 llvm_param.clang_opt = strdup(value);
55 else if (!strcmp(var, "kbuild-dir"))
56 llvm_param.kbuild_dir = strdup(value);
57 else if (!strcmp(var, "kbuild-opts"))
58 llvm_param.kbuild_opts = strdup(value);
59 else if (!strcmp(var, "dump-obj"))
60 llvm_param.dump_obj = !!perf_config_bool(var, value);
61 else if (!strcmp(var, "opts"))
62 llvm_param.opts = strdup(value);
64 pr_debug("Invalid LLVM config option: %s\n", value);
67 llvm_param.user_set_param = true;
72 search_program(const char *def, const char *name,
75 char *env, *path, *tmp = NULL;
80 if (def && def[0] != '\0') {
82 if (access(def, F_OK) == 0) {
83 strlcpy(output, def, PATH_MAX);
86 } else if (def[0] != '\0')
98 path = strtok_r(env, ":", &tmp);
100 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
101 if (access(buf, F_OK) == 0) {
102 strlcpy(output, buf, PATH_MAX);
106 path = strtok_r(NULL, ":", &tmp);
113 static int search_program_and_warn(const char *def, const char *name,
116 int ret = search_program(def, name, output);
119 pr_err("ERROR:\tunable to find %s.\n"
120 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
121 " \tand '%s-path' option in [llvm] section of ~/.perfconfig.\n",
128 #define READ_SIZE 4096
130 read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
135 size_t read_sz = 0, buf_sz = 0;
136 char serr[STRERR_BUFSIZE];
138 file = popen(cmd, "r");
140 pr_err("ERROR: unable to popen cmd: %s\n",
141 str_error_r(errno, serr, sizeof(serr)));
145 while (!feof(file) && !ferror(file)) {
147 * Make buf_sz always have obe byte extra space so we
148 * can put '\0' there.
150 if (buf_sz - read_sz < READ_SIZE + 1) {
153 buf_sz = read_sz + READ_SIZE + 1;
154 new_buf = realloc(buf, buf_sz);
157 pr_err("ERROR: failed to realloc memory\n");
164 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
167 if (buf_sz - read_sz < 1) {
168 pr_err("ERROR: internal error\n");
174 pr_err("ERROR: error occurred when reading from pipe: %s\n",
175 str_error_r(errno, serr, sizeof(serr)));
180 err = WEXITSTATUS(pclose(file));
188 * If buf is string, give it terminal '\0' to make our life
189 * easier. If buf is not string, that '\0' is out of space
190 * indicated by read_sz so caller won't even notice it.
192 ((char *)buf)[read_sz] = '\0';
200 *p_read_sz = read_sz;
215 force_set_env(const char *var, const char *value)
218 setenv(var, value, 1);
219 pr_debug("set env: %s=%s\n", var, value);
222 pr_debug("unset env: %s\n", var);
230 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
231 " \tYou may want to try git trunk:\n"
232 " \t\tgit clone http://llvm.org/git/llvm.git\n"
234 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
235 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
236 " \tdebian/ubuntu:\n"
237 " \t\thttps://apt.llvm.org/\n\n"
238 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
239 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
240 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS \\\n"
241 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
242 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
243 " \t(Replace /path/to/llc with path to your llc)\n\n"
247 static int detect_kbuild_dir(char **kbuild_dir)
249 const char *test_dir = llvm_param.kbuild_dir;
250 const char *prefix_dir = "";
251 const char *suffix_dir = "";
253 /* _UTSNAME_LENGTH is 65 */
261 err = fetch_kernel_version(NULL, release,
267 prefix_dir = "/lib/modules/";
268 suffix_dir = "/build";
271 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
272 prefix_dir, test_dir, suffix_dir);
276 if (access(autoconf_path, R_OK) == 0) {
279 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
285 pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n",
286 __func__, autoconf_path);
291 static const char *kinc_fetch_script =
292 "#!/usr/bin/env sh\n"
293 "if ! test -d \"$KBUILD_DIR\"\n"
297 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
301 "TMPDIR=`mktemp -d`\n"
302 "if test -z \"$TMPDIR\"\n"
306 "cat << EOF > $TMPDIR/Makefile\n"
308 "\\$(obj)/%.o: \\$(src)/%.c\n"
309 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
310 "\t\\$(CC) -c -o \\$@ \\$<\n"
312 "touch $TMPDIR/dummy.c\n"
313 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
318 void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
320 static char *saved_kbuild_dir;
321 static char *saved_kbuild_include_opts;
324 if (!kbuild_dir || !kbuild_include_opts)
328 *kbuild_include_opts = NULL;
330 if (saved_kbuild_dir && saved_kbuild_include_opts &&
331 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
332 *kbuild_dir = strdup(saved_kbuild_dir);
333 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
335 if (*kbuild_dir && *kbuild_include_opts)
339 zfree(kbuild_include_opts);
341 * Don't fall through: it may breaks saved_kbuild_dir and
342 * saved_kbuild_include_opts if detect them again when
348 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
349 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
350 pr_debug("Skip kbuild options detection.\n");
354 err = detect_kbuild_dir(kbuild_dir);
357 "WARNING:\tunable to get correct kernel building directory.\n"
358 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
359 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
360 " \tdetection.\n\n");
364 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
365 force_set_env("KBUILD_DIR", *kbuild_dir);
366 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
367 err = read_from_pipe(kinc_fetch_script,
368 (void **)kbuild_include_opts,
372 "WARNING:\tunable to get kernel include directories from '%s'\n"
373 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
374 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
375 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
382 pr_debug("include option is set to %s\n", *kbuild_include_opts);
384 saved_kbuild_dir = strdup(*kbuild_dir);
385 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
387 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
388 zfree(&saved_kbuild_dir);
389 zfree(&saved_kbuild_include_opts);
393 saved_kbuild_dir = ERR_PTR(-EINVAL);
394 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
397 int llvm__get_nr_cpus(void)
399 static int nr_cpus_avail = 0;
400 char serr[STRERR_BUFSIZE];
402 if (nr_cpus_avail > 0)
403 return nr_cpus_avail;
405 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
406 if (nr_cpus_avail <= 0) {
408 "WARNING:\tunable to get available CPUs in this system: %s\n"
409 " \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
412 return nr_cpus_avail;
415 void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
417 char *obj_path = strdup(path);
422 pr_warning("WARNING: Not enough memory, skip object dumping\n");
426 p = strrchr(obj_path, '.');
427 if (!p || (strcmp(p, ".c") != 0)) {
428 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
434 fp = fopen(obj_path, "wb");
436 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
437 obj_path, strerror(errno));
441 pr_debug("LLVM: dumping %s\n", obj_path);
442 if (fwrite(obj_buf, size, 1, fp) != 1)
443 pr_debug("WARNING: failed to write to file '%s': %s, skip object dumping\n", obj_path, strerror(errno));
449 int llvm__compile_bpf(const char *path, void **p_obj_buf,
450 size_t *p_obj_buf_sz)
453 void *obj_buf = NULL;
454 int err, nr_cpus_avail;
455 unsigned int kernel_version;
456 char linux_version_code_str[64];
457 const char *clang_opt = llvm_param.clang_opt;
458 char clang_path[PATH_MAX], llc_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
459 char serr[STRERR_BUFSIZE];
460 char *kbuild_dir = NULL, *kbuild_include_opts = NULL,
461 *perf_bpf_include_opts = NULL;
462 const char *template = llvm_param.clang_bpf_cmd_template;
463 char *pipe_template = NULL;
464 const char *opts = llvm_param.opts;
465 char *command_echo = NULL, *command_out;
466 char *perf_include_dir = system_path(PERF_INCLUDE_DIR);
468 if (path[0] != '-' && realpath(path, abspath) == NULL) {
470 pr_err("ERROR: problems with path %s: %s\n",
471 path, str_error_r(err, serr, sizeof(serr)));
476 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
478 err = search_program_and_warn(llvm_param.clang_path,
479 "clang", clang_path);
484 * This is an optional work. Even it fail we can continue our
485 * work. Needn't check error return.
487 llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
489 nr_cpus_avail = llvm__get_nr_cpus();
490 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
493 if (fetch_kernel_version(&kernel_version, NULL, 0))
496 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
497 "0x%x", kernel_version);
498 if (asprintf(&perf_bpf_include_opts, "-I%s/bpf", perf_include_dir) < 0)
500 force_set_env("NR_CPUS", nr_cpus_avail_str);
501 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
502 force_set_env("CLANG_EXEC", clang_path);
503 force_set_env("CLANG_OPTIONS", clang_opt);
504 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
505 force_set_env("PERF_BPF_INC_OPTIONS", perf_bpf_include_opts);
506 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
509 err = search_program_and_warn(llvm_param.llc_path, "llc", llc_path);
514 if (asprintf(&pipe_template, "%s -emit-llvm | %s -march=bpf %s -filetype=obj -o -",
515 template, llc_path, opts) < 0) {
516 pr_err("ERROR:\tnot enough memory to setup command line\n");
520 template = pipe_template;
525 * Since we may reset clang's working dir, path of source file
526 * should be transferred into absolute path, except we want
527 * stdin to be source file (testing).
529 force_set_env("CLANG_SOURCE",
530 (path[0] == '-') ? path : abspath);
532 pr_debug("llvm compiling command template: %s\n", template);
535 if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
538 err = read_from_pipe(command_echo, (void **) &command_out, NULL);
542 pr_debug("llvm compiling command : %s\n", command_out);
544 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
546 pr_err("ERROR:\tunable to compile %s\n", path);
547 pr_err("Hint:\tCheck error message shown above.\n");
548 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
549 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
550 pr_err(" \twith proper -I and -D options.\n");
557 free(kbuild_include_opts);
558 free(perf_bpf_include_opts);
559 free(perf_include_dir);
564 *p_obj_buf = obj_buf;
567 *p_obj_buf_sz = obj_buf_sz;
572 free(kbuild_include_opts);
574 free(perf_bpf_include_opts);
575 free(perf_include_dir);
584 int llvm__search_clang(void)
586 char clang_path[PATH_MAX];
588 return search_program_and_warn(llvm_param.clang_path, "clang", clang_path);