2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
9 #include "llvm-utils.h"
12 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
13 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
14 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
15 "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
16 "-Wno-unused-value -Wno-pointer-sign " \
17 "-working-directory $WORKING_DIR " \
18 "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
20 struct llvm_param llvm_param
= {
21 .clang_path
= "clang",
22 .clang_bpf_cmd_template
= CLANG_BPF_CMD_DEFAULT_TEMPLATE
,
26 .user_set_param
= false,
29 int perf_llvm_config(const char *var
, const char *value
)
31 if (prefixcmp(var
, "llvm."))
33 var
+= sizeof("llvm.") - 1;
35 if (!strcmp(var
, "clang-path"))
36 llvm_param
.clang_path
= strdup(value
);
37 else if (!strcmp(var
, "clang-bpf-cmd-template"))
38 llvm_param
.clang_bpf_cmd_template
= strdup(value
);
39 else if (!strcmp(var
, "clang-opt"))
40 llvm_param
.clang_opt
= strdup(value
);
41 else if (!strcmp(var
, "kbuild-dir"))
42 llvm_param
.kbuild_dir
= strdup(value
);
43 else if (!strcmp(var
, "kbuild-opts"))
44 llvm_param
.kbuild_opts
= strdup(value
);
47 llvm_param
.user_set_param
= true;
52 search_program(const char *def
, const char *name
,
55 char *env
, *path
, *tmp
= NULL
;
60 if (def
&& def
[0] != '\0') {
62 if (access(def
, F_OK
) == 0) {
63 strlcpy(output
, def
, PATH_MAX
);
66 } else if (def
[0] != '\0')
78 path
= strtok_r(env
, ":", &tmp
);
80 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
81 if (access(buf
, F_OK
) == 0) {
82 strlcpy(output
, buf
, PATH_MAX
);
86 path
= strtok_r(NULL
, ":", &tmp
);
93 #define READ_SIZE 4096
95 read_from_pipe(const char *cmd
, void **p_buf
, size_t *p_read_sz
)
100 size_t read_sz
= 0, buf_sz
= 0;
102 file
= popen(cmd
, "r");
104 pr_err("ERROR: unable to popen cmd: %s\n",
109 while (!feof(file
) && !ferror(file
)) {
111 * Make buf_sz always have obe byte extra space so we
112 * can put '\0' there.
114 if (buf_sz
- read_sz
< READ_SIZE
+ 1) {
117 buf_sz
= read_sz
+ READ_SIZE
+ 1;
118 new_buf
= realloc(buf
, buf_sz
);
121 pr_err("ERROR: failed to realloc memory\n");
128 read_sz
+= fread(buf
+ read_sz
, 1, READ_SIZE
, file
);
131 if (buf_sz
- read_sz
< 1) {
132 pr_err("ERROR: internal error\n");
138 pr_err("ERROR: error occurred when reading from pipe: %s\n",
144 err
= WEXITSTATUS(pclose(file
));
152 * If buf is string, give it terminal '\0' to make our life
153 * easier. If buf is not string, that '\0' is out of space
154 * indicated by read_sz so caller won't even notice it.
156 ((char *)buf
)[read_sz
] = '\0';
164 *p_read_sz
= read_sz
;
179 force_set_env(const char *var
, const char *value
)
182 setenv(var
, value
, 1);
183 pr_debug("set env: %s=%s\n", var
, value
);
186 pr_debug("unset env: %s\n", var
);
194 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
195 " \tYou may want to try git trunk:\n"
196 " \t\tgit clone http://llvm.org/git/llvm.git\n"
198 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
199 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
200 " \tdebian/ubuntu:\n"
201 " \t\thttp://llvm.org/apt\n\n"
202 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
203 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
204 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
205 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
206 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
207 " \t(Replace /path/to/llc with path to your llc)\n\n"
211 static int detect_kbuild_dir(char **kbuild_dir
)
213 const char *test_dir
= llvm_param
.kbuild_dir
;
214 const char *prefix_dir
= "";
215 const char *suffix_dir
= "";
222 /* _UTSNAME_LENGTH is 65 */
225 err
= fetch_kernel_version(NULL
, release
,
231 prefix_dir
= "/lib/modules/";
232 suffix_dir
= "/build";
235 err
= asprintf(&autoconf_path
, "%s%s%s/include/generated/autoconf.h",
236 prefix_dir
, test_dir
, suffix_dir
);
240 if (access(autoconf_path
, R_OK
) == 0) {
243 err
= asprintf(kbuild_dir
, "%s%s%s", prefix_dir
, test_dir
,
253 static const char *kinc_fetch_script
=
254 "#!/usr/bin/env sh\n"
255 "if ! test -d \"$KBUILD_DIR\"\n"
259 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
263 "TMPDIR=`mktemp -d`\n"
264 "if test -z \"$TMPDIR\"\n"
268 "cat << EOF > $TMPDIR/Makefile\n"
270 "\\$(obj)/%.o: \\$(src)/%.c\n"
271 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
273 "touch $TMPDIR/dummy.c\n"
274 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
280 get_kbuild_opts(char **kbuild_dir
, char **kbuild_include_opts
)
284 if (!kbuild_dir
|| !kbuild_include_opts
)
288 *kbuild_include_opts
= NULL
;
290 if (llvm_param
.kbuild_dir
&& !llvm_param
.kbuild_dir
[0]) {
291 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
292 pr_debug("Skip kbuild options detection.\n");
296 err
= detect_kbuild_dir(kbuild_dir
);
299 "WARNING:\tunable to get correct kernel building directory.\n"
300 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
301 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
302 " \tdetection.\n\n");
306 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir
);
307 force_set_env("KBUILD_DIR", *kbuild_dir
);
308 force_set_env("KBUILD_OPTS", llvm_param
.kbuild_opts
);
309 err
= read_from_pipe(kinc_fetch_script
,
310 (void **)kbuild_include_opts
,
314 "WARNING:\tunable to get kernel include directories from '%s'\n"
315 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
316 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
317 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
325 pr_debug("include option is set to %s\n", *kbuild_include_opts
);
328 int llvm__compile_bpf(const char *path
, void **p_obj_buf
,
329 size_t *p_obj_buf_sz
)
332 void *obj_buf
= NULL
;
333 int err
, nr_cpus_avail
;
334 unsigned int kernel_version
;
335 char linux_version_code_str
[64];
336 const char *clang_opt
= llvm_param
.clang_opt
;
337 char clang_path
[PATH_MAX
], nr_cpus_avail_str
[64];
338 char *kbuild_dir
= NULL
, *kbuild_include_opts
= NULL
;
339 const char *template = llvm_param
.clang_bpf_cmd_template
;
342 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE
;
344 err
= search_program(llvm_param
.clang_path
,
345 "clang", clang_path
);
348 "ERROR:\tunable to find clang.\n"
349 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
350 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
356 * This is an optional work. Even it fail we can continue our
357 * work. Needn't to check error return.
359 get_kbuild_opts(&kbuild_dir
, &kbuild_include_opts
);
361 nr_cpus_avail
= sysconf(_SC_NPROCESSORS_CONF
);
362 if (nr_cpus_avail
<= 0) {
364 "WARNING:\tunable to get available CPUs in this system: %s\n"
365 " \tUse 128 instead.\n", strerror(errno
));
368 snprintf(nr_cpus_avail_str
, sizeof(nr_cpus_avail_str
), "%d",
371 if (fetch_kernel_version(&kernel_version
, NULL
, 0))
374 snprintf(linux_version_code_str
, sizeof(linux_version_code_str
),
375 "0x%x", kernel_version
);
377 force_set_env("NR_CPUS", nr_cpus_avail_str
);
378 force_set_env("LINUX_VERSION_CODE", linux_version_code_str
);
379 force_set_env("CLANG_EXEC", clang_path
);
380 force_set_env("CLANG_OPTIONS", clang_opt
);
381 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts
);
382 force_set_env("WORKING_DIR", kbuild_dir
? : ".");
385 * Since we may reset clang's working dir, path of source file
386 * should be transferred into absolute path, except we want
387 * stdin to be source file (testing).
389 force_set_env("CLANG_SOURCE",
390 (path
[0] == '-') ? path
:
391 make_nonrelative_path(path
));
393 pr_debug("llvm compiling command template: %s\n", template);
394 err
= read_from_pipe(template, &obj_buf
, &obj_buf_sz
);
396 pr_err("ERROR:\tunable to compile %s\n", path
);
397 pr_err("Hint:\tCheck error message shown above.\n");
398 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
399 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path
);
400 pr_err(" \twith proper -I and -D options.\n");
405 free(kbuild_include_opts
);
409 *p_obj_buf
= obj_buf
;
412 *p_obj_buf_sz
= obj_buf_sz
;
416 free(kbuild_include_opts
);
425 int llvm__search_clang(void)
427 char clang_path
[PATH_MAX
];
429 return search_program(llvm_param
.clang_path
, "clang", clang_path
);