2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
10 #include <linux/err.h>
12 #include "llvm-utils.h"
16 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
17 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
18 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
19 "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
20 "-Wno-unused-value -Wno-pointer-sign " \
21 "-working-directory $WORKING_DIR " \
22 "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
24 struct llvm_param llvm_param
= {
25 .clang_path
= "clang",
26 .clang_bpf_cmd_template
= CLANG_BPF_CMD_DEFAULT_TEMPLATE
,
30 .user_set_param
= false,
33 int perf_llvm_config(const char *var
, const char *value
)
35 if (prefixcmp(var
, "llvm."))
37 var
+= sizeof("llvm.") - 1;
39 if (!strcmp(var
, "clang-path"))
40 llvm_param
.clang_path
= strdup(value
);
41 else if (!strcmp(var
, "clang-bpf-cmd-template"))
42 llvm_param
.clang_bpf_cmd_template
= strdup(value
);
43 else if (!strcmp(var
, "clang-opt"))
44 llvm_param
.clang_opt
= strdup(value
);
45 else if (!strcmp(var
, "kbuild-dir"))
46 llvm_param
.kbuild_dir
= strdup(value
);
47 else if (!strcmp(var
, "kbuild-opts"))
48 llvm_param
.kbuild_opts
= strdup(value
);
49 else if (!strcmp(var
, "dump-obj"))
50 llvm_param
.dump_obj
= !!perf_config_bool(var
, value
);
52 pr_debug("Invalid LLVM config option: %s\n", value
);
55 llvm_param
.user_set_param
= true;
60 search_program(const char *def
, const char *name
,
63 char *env
, *path
, *tmp
= NULL
;
68 if (def
&& def
[0] != '\0') {
70 if (access(def
, F_OK
) == 0) {
71 strlcpy(output
, def
, PATH_MAX
);
74 } else if (def
[0] != '\0')
86 path
= strtok_r(env
, ":", &tmp
);
88 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
89 if (access(buf
, F_OK
) == 0) {
90 strlcpy(output
, buf
, PATH_MAX
);
94 path
= strtok_r(NULL
, ":", &tmp
);
101 #define READ_SIZE 4096
103 read_from_pipe(const char *cmd
, void **p_buf
, size_t *p_read_sz
)
108 size_t read_sz
= 0, buf_sz
= 0;
109 char serr
[STRERR_BUFSIZE
];
111 file
= popen(cmd
, "r");
113 pr_err("ERROR: unable to popen cmd: %s\n",
114 str_error_r(errno
, serr
, sizeof(serr
)));
118 while (!feof(file
) && !ferror(file
)) {
120 * Make buf_sz always have obe byte extra space so we
121 * can put '\0' there.
123 if (buf_sz
- read_sz
< READ_SIZE
+ 1) {
126 buf_sz
= read_sz
+ READ_SIZE
+ 1;
127 new_buf
= realloc(buf
, buf_sz
);
130 pr_err("ERROR: failed to realloc memory\n");
137 read_sz
+= fread(buf
+ read_sz
, 1, READ_SIZE
, file
);
140 if (buf_sz
- read_sz
< 1) {
141 pr_err("ERROR: internal error\n");
147 pr_err("ERROR: error occurred when reading from pipe: %s\n",
148 str_error_r(errno
, serr
, sizeof(serr
)));
153 err
= WEXITSTATUS(pclose(file
));
161 * If buf is string, give it terminal '\0' to make our life
162 * easier. If buf is not string, that '\0' is out of space
163 * indicated by read_sz so caller won't even notice it.
165 ((char *)buf
)[read_sz
] = '\0';
173 *p_read_sz
= read_sz
;
188 force_set_env(const char *var
, const char *value
)
191 setenv(var
, value
, 1);
192 pr_debug("set env: %s=%s\n", var
, value
);
195 pr_debug("unset env: %s\n", var
);
203 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
204 " \tYou may want to try git trunk:\n"
205 " \t\tgit clone http://llvm.org/git/llvm.git\n"
207 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
208 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
209 " \tdebian/ubuntu:\n"
210 " \t\thttp://llvm.org/apt\n\n"
211 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
212 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
213 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
214 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
215 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
216 " \t(Replace /path/to/llc with path to your llc)\n\n"
220 static int detect_kbuild_dir(char **kbuild_dir
)
222 const char *test_dir
= llvm_param
.kbuild_dir
;
223 const char *prefix_dir
= "";
224 const char *suffix_dir
= "";
231 /* _UTSNAME_LENGTH is 65 */
234 err
= fetch_kernel_version(NULL
, release
,
240 prefix_dir
= "/lib/modules/";
241 suffix_dir
= "/build";
244 err
= asprintf(&autoconf_path
, "%s%s%s/include/generated/autoconf.h",
245 prefix_dir
, test_dir
, suffix_dir
);
249 if (access(autoconf_path
, R_OK
) == 0) {
252 err
= asprintf(kbuild_dir
, "%s%s%s", prefix_dir
, test_dir
,
262 static const char *kinc_fetch_script
=
263 "#!/usr/bin/env sh\n"
264 "if ! test -d \"$KBUILD_DIR\"\n"
268 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
272 "TMPDIR=`mktemp -d`\n"
273 "if test -z \"$TMPDIR\"\n"
277 "cat << EOF > $TMPDIR/Makefile\n"
279 "\\$(obj)/%.o: \\$(src)/%.c\n"
280 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
282 "touch $TMPDIR/dummy.c\n"
283 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
288 void llvm__get_kbuild_opts(char **kbuild_dir
, char **kbuild_include_opts
)
290 static char *saved_kbuild_dir
;
291 static char *saved_kbuild_include_opts
;
294 if (!kbuild_dir
|| !kbuild_include_opts
)
298 *kbuild_include_opts
= NULL
;
300 if (saved_kbuild_dir
&& saved_kbuild_include_opts
&&
301 !IS_ERR(saved_kbuild_dir
) && !IS_ERR(saved_kbuild_include_opts
)) {
302 *kbuild_dir
= strdup(saved_kbuild_dir
);
303 *kbuild_include_opts
= strdup(saved_kbuild_include_opts
);
305 if (*kbuild_dir
&& *kbuild_include_opts
)
309 zfree(kbuild_include_opts
);
311 * Don't fall through: it may breaks saved_kbuild_dir and
312 * saved_kbuild_include_opts if detect them again when
318 if (llvm_param
.kbuild_dir
&& !llvm_param
.kbuild_dir
[0]) {
319 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
320 pr_debug("Skip kbuild options detection.\n");
324 err
= detect_kbuild_dir(kbuild_dir
);
327 "WARNING:\tunable to get correct kernel building directory.\n"
328 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
329 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
330 " \tdetection.\n\n");
334 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir
);
335 force_set_env("KBUILD_DIR", *kbuild_dir
);
336 force_set_env("KBUILD_OPTS", llvm_param
.kbuild_opts
);
337 err
= read_from_pipe(kinc_fetch_script
,
338 (void **)kbuild_include_opts
,
342 "WARNING:\tunable to get kernel include directories from '%s'\n"
343 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
344 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
345 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
353 pr_debug("include option is set to %s\n", *kbuild_include_opts
);
355 saved_kbuild_dir
= strdup(*kbuild_dir
);
356 saved_kbuild_include_opts
= strdup(*kbuild_include_opts
);
358 if (!saved_kbuild_dir
|| !saved_kbuild_include_opts
) {
359 zfree(&saved_kbuild_dir
);
360 zfree(&saved_kbuild_include_opts
);
364 saved_kbuild_dir
= ERR_PTR(-EINVAL
);
365 saved_kbuild_include_opts
= ERR_PTR(-EINVAL
);
368 int llvm__get_nr_cpus(void)
370 static int nr_cpus_avail
= 0;
371 char serr
[STRERR_BUFSIZE
];
373 if (nr_cpus_avail
> 0)
374 return nr_cpus_avail
;
376 nr_cpus_avail
= sysconf(_SC_NPROCESSORS_CONF
);
377 if (nr_cpus_avail
<= 0) {
379 "WARNING:\tunable to get available CPUs in this system: %s\n"
380 " \tUse 128 instead.\n", str_error_r(errno
, serr
, sizeof(serr
)));
383 return nr_cpus_avail
;
386 void llvm__dump_obj(const char *path
, void *obj_buf
, size_t size
)
388 char *obj_path
= strdup(path
);
393 pr_warning("WARNING: Not enough memory, skip object dumping\n");
397 p
= strrchr(obj_path
, '.');
398 if (!p
|| (strcmp(p
, ".c") != 0)) {
399 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
405 fp
= fopen(obj_path
, "wb");
407 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
408 obj_path
, strerror(errno
));
412 pr_info("LLVM: dumping %s\n", obj_path
);
413 if (fwrite(obj_buf
, size
, 1, fp
) != 1)
414 pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n",
415 obj_path
, strerror(errno
));
421 int llvm__compile_bpf(const char *path
, void **p_obj_buf
,
422 size_t *p_obj_buf_sz
)
425 void *obj_buf
= NULL
;
426 int err
, nr_cpus_avail
;
427 unsigned int kernel_version
;
428 char linux_version_code_str
[64];
429 const char *clang_opt
= llvm_param
.clang_opt
;
430 char clang_path
[PATH_MAX
], abspath
[PATH_MAX
], nr_cpus_avail_str
[64];
431 char serr
[STRERR_BUFSIZE
];
432 char *kbuild_dir
= NULL
, *kbuild_include_opts
= NULL
;
433 const char *template = llvm_param
.clang_bpf_cmd_template
;
435 if (path
[0] != '-' && realpath(path
, abspath
) == NULL
) {
437 pr_err("ERROR: problems with path %s: %s\n",
438 path
, str_error_r(err
, serr
, sizeof(serr
)));
443 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE
;
445 err
= search_program(llvm_param
.clang_path
,
446 "clang", clang_path
);
449 "ERROR:\tunable to find clang.\n"
450 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
451 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
457 * This is an optional work. Even it fail we can continue our
458 * work. Needn't to check error return.
460 llvm__get_kbuild_opts(&kbuild_dir
, &kbuild_include_opts
);
462 nr_cpus_avail
= llvm__get_nr_cpus();
463 snprintf(nr_cpus_avail_str
, sizeof(nr_cpus_avail_str
), "%d",
466 if (fetch_kernel_version(&kernel_version
, NULL
, 0))
469 snprintf(linux_version_code_str
, sizeof(linux_version_code_str
),
470 "0x%x", kernel_version
);
472 force_set_env("NR_CPUS", nr_cpus_avail_str
);
473 force_set_env("LINUX_VERSION_CODE", linux_version_code_str
);
474 force_set_env("CLANG_EXEC", clang_path
);
475 force_set_env("CLANG_OPTIONS", clang_opt
);
476 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts
);
477 force_set_env("WORKING_DIR", kbuild_dir
? : ".");
480 * Since we may reset clang's working dir, path of source file
481 * should be transferred into absolute path, except we want
482 * stdin to be source file (testing).
484 force_set_env("CLANG_SOURCE",
485 (path
[0] == '-') ? path
: abspath
);
487 pr_debug("llvm compiling command template: %s\n", template);
488 err
= read_from_pipe(template, &obj_buf
, &obj_buf_sz
);
490 pr_err("ERROR:\tunable to compile %s\n", path
);
491 pr_err("Hint:\tCheck error message shown above.\n");
492 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
493 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path
);
494 pr_err(" \twith proper -I and -D options.\n");
499 free(kbuild_include_opts
);
504 *p_obj_buf
= obj_buf
;
507 *p_obj_buf_sz
= obj_buf_sz
;
511 free(kbuild_include_opts
);
520 int llvm__search_clang(void)
522 char clang_path
[PATH_MAX
];
524 return search_program(llvm_param
.clang_path
, "clang", clang_path
);