2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
10 #include "llvm-utils.h"
13 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
14 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
15 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
16 "$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 -"
21 struct llvm_param llvm_param
= {
22 .clang_path
= "clang",
23 .clang_bpf_cmd_template
= CLANG_BPF_CMD_DEFAULT_TEMPLATE
,
27 .user_set_param
= false,
30 int perf_llvm_config(const char *var
, const char *value
)
32 if (prefixcmp(var
, "llvm."))
34 var
+= sizeof("llvm.") - 1;
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
);
46 else if (!strcmp(var
, "dump-obj"))
47 llvm_param
.dump_obj
= !!perf_config_bool(var
, value
);
50 llvm_param
.user_set_param
= true;
55 search_program(const char *def
, const char *name
,
58 char *env
, *path
, *tmp
= NULL
;
63 if (def
&& def
[0] != '\0') {
65 if (access(def
, F_OK
) == 0) {
66 strlcpy(output
, def
, PATH_MAX
);
69 } else if (def
[0] != '\0')
81 path
= strtok_r(env
, ":", &tmp
);
83 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
84 if (access(buf
, F_OK
) == 0) {
85 strlcpy(output
, buf
, PATH_MAX
);
89 path
= strtok_r(NULL
, ":", &tmp
);
96 #define READ_SIZE 4096
98 read_from_pipe(const char *cmd
, void **p_buf
, size_t *p_read_sz
)
103 size_t read_sz
= 0, buf_sz
= 0;
104 char serr
[STRERR_BUFSIZE
];
106 file
= popen(cmd
, "r");
108 pr_err("ERROR: unable to popen cmd: %s\n",
109 strerror_r(errno
, serr
, sizeof(serr
)));
113 while (!feof(file
) && !ferror(file
)) {
115 * Make buf_sz always have obe byte extra space so we
116 * can put '\0' there.
118 if (buf_sz
- read_sz
< READ_SIZE
+ 1) {
121 buf_sz
= read_sz
+ READ_SIZE
+ 1;
122 new_buf
= realloc(buf
, buf_sz
);
125 pr_err("ERROR: failed to realloc memory\n");
132 read_sz
+= fread(buf
+ read_sz
, 1, READ_SIZE
, file
);
135 if (buf_sz
- read_sz
< 1) {
136 pr_err("ERROR: internal error\n");
142 pr_err("ERROR: error occurred when reading from pipe: %s\n",
143 strerror_r(errno
, serr
, sizeof(serr
)));
148 err
= WEXITSTATUS(pclose(file
));
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.
160 ((char *)buf
)[read_sz
] = '\0';
168 *p_read_sz
= read_sz
;
183 force_set_env(const char *var
, const char *value
)
186 setenv(var
, value
, 1);
187 pr_debug("set env: %s=%s\n", var
, value
);
190 pr_debug("unset env: %s\n", var
);
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"
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"
215 static int detect_kbuild_dir(char **kbuild_dir
)
217 const char *test_dir
= llvm_param
.kbuild_dir
;
218 const char *prefix_dir
= "";
219 const char *suffix_dir
= "";
226 /* _UTSNAME_LENGTH is 65 */
229 err
= fetch_kernel_version(NULL
, release
,
235 prefix_dir
= "/lib/modules/";
236 suffix_dir
= "/build";
239 err
= asprintf(&autoconf_path
, "%s%s%s/include/generated/autoconf.h",
240 prefix_dir
, test_dir
, suffix_dir
);
244 if (access(autoconf_path
, R_OK
) == 0) {
247 err
= asprintf(kbuild_dir
, "%s%s%s", prefix_dir
, test_dir
,
257 static const char *kinc_fetch_script
=
258 "#!/usr/bin/env sh\n"
259 "if ! test -d \"$KBUILD_DIR\"\n"
263 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
267 "TMPDIR=`mktemp -d`\n"
268 "if test -z \"$TMPDIR\"\n"
272 "cat << EOF > $TMPDIR/Makefile\n"
274 "\\$(obj)/%.o: \\$(src)/%.c\n"
275 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
277 "touch $TMPDIR/dummy.c\n"
278 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
284 get_kbuild_opts(char **kbuild_dir
, char **kbuild_include_opts
)
288 if (!kbuild_dir
|| !kbuild_include_opts
)
292 *kbuild_include_opts
= NULL
;
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");
300 err
= detect_kbuild_dir(kbuild_dir
);
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");
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
,
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",
329 pr_debug("include option is set to %s\n", *kbuild_include_opts
);
333 dump_obj(const char *path
, void *obj_buf
, size_t size
)
335 char *obj_path
= strdup(path
);
340 pr_warning("WARNING: No enough memory, skip object dumping\n");
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",
352 fp
= fopen(obj_path
, "wb");
354 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
355 obj_path
, strerror(errno
));
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
));
368 int llvm__compile_bpf(const char *path
, void **p_obj_buf
,
369 size_t *p_obj_buf_sz
)
372 void *obj_buf
= NULL
;
373 int err
, nr_cpus_avail
;
374 unsigned int kernel_version
;
375 char linux_version_code_str
[64];
376 const char *clang_opt
= llvm_param
.clang_opt
;
377 char clang_path
[PATH_MAX
], abspath
[PATH_MAX
], nr_cpus_avail_str
[64];
378 char serr
[STRERR_BUFSIZE
];
379 char *kbuild_dir
= NULL
, *kbuild_include_opts
= NULL
;
380 const char *template = llvm_param
.clang_bpf_cmd_template
;
382 if (path
[0] != '-' && realpath(path
, abspath
) == NULL
) {
384 pr_err("ERROR: problems with path %s: %s\n",
385 path
, strerror_r(err
, serr
, sizeof(serr
)));
390 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE
;
392 err
= search_program(llvm_param
.clang_path
,
393 "clang", clang_path
);
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");
404 * This is an optional work. Even it fail we can continue our
405 * work. Needn't to check error return.
407 get_kbuild_opts(&kbuild_dir
, &kbuild_include_opts
);
409 nr_cpus_avail
= sysconf(_SC_NPROCESSORS_CONF
);
410 if (nr_cpus_avail
<= 0) {
412 "WARNING:\tunable to get available CPUs in this system: %s\n"
413 " \tUse 128 instead.\n", strerror_r(errno
, serr
, sizeof(serr
)));
416 snprintf(nr_cpus_avail_str
, sizeof(nr_cpus_avail_str
), "%d",
419 if (fetch_kernel_version(&kernel_version
, NULL
, 0))
422 snprintf(linux_version_code_str
, sizeof(linux_version_code_str
),
423 "0x%x", kernel_version
);
425 force_set_env("NR_CPUS", nr_cpus_avail_str
);
426 force_set_env("LINUX_VERSION_CODE", linux_version_code_str
);
427 force_set_env("CLANG_EXEC", clang_path
);
428 force_set_env("CLANG_OPTIONS", clang_opt
);
429 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts
);
430 force_set_env("WORKING_DIR", kbuild_dir
? : ".");
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).
437 force_set_env("CLANG_SOURCE",
438 (path
[0] == '-') ? path
: abspath
);
440 pr_debug("llvm compiling command template: %s\n", template);
441 err
= read_from_pipe(template, &obj_buf
, &obj_buf_sz
);
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");
452 free(kbuild_include_opts
);
454 if (llvm_param
.dump_obj
)
455 dump_obj(path
, obj_buf
, obj_buf_sz
);
460 *p_obj_buf
= obj_buf
;
463 *p_obj_buf_sz
= obj_buf_sz
;
467 free(kbuild_include_opts
);
476 int llvm__search_clang(void)
478 char clang_path
[PATH_MAX
];
480 return search_program(llvm_param
.clang_path
, "clang", clang_path
);