2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
10 #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;
101 char serr
[STRERR_BUFSIZE
];
103 file
= popen(cmd
, "r");
105 pr_err("ERROR: unable to popen cmd: %s\n",
106 strerror_r(errno
, serr
, sizeof(serr
)));
110 while (!feof(file
) && !ferror(file
)) {
112 * Make buf_sz always have obe byte extra space so we
113 * can put '\0' there.
115 if (buf_sz
- read_sz
< READ_SIZE
+ 1) {
118 buf_sz
= read_sz
+ READ_SIZE
+ 1;
119 new_buf
= realloc(buf
, buf_sz
);
122 pr_err("ERROR: failed to realloc memory\n");
129 read_sz
+= fread(buf
+ read_sz
, 1, READ_SIZE
, file
);
132 if (buf_sz
- read_sz
< 1) {
133 pr_err("ERROR: internal error\n");
139 pr_err("ERROR: error occurred when reading from pipe: %s\n",
140 strerror_r(errno
, serr
, sizeof(serr
)));
145 err
= WEXITSTATUS(pclose(file
));
153 * If buf is string, give it terminal '\0' to make our life
154 * easier. If buf is not string, that '\0' is out of space
155 * indicated by read_sz so caller won't even notice it.
157 ((char *)buf
)[read_sz
] = '\0';
165 *p_read_sz
= read_sz
;
180 force_set_env(const char *var
, const char *value
)
183 setenv(var
, value
, 1);
184 pr_debug("set env: %s=%s\n", var
, value
);
187 pr_debug("unset env: %s\n", var
);
195 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
196 " \tYou may want to try git trunk:\n"
197 " \t\tgit clone http://llvm.org/git/llvm.git\n"
199 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
200 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
201 " \tdebian/ubuntu:\n"
202 " \t\thttp://llvm.org/apt\n\n"
203 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
204 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
205 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
206 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
207 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
208 " \t(Replace /path/to/llc with path to your llc)\n\n"
212 static int detect_kbuild_dir(char **kbuild_dir
)
214 const char *test_dir
= llvm_param
.kbuild_dir
;
215 const char *prefix_dir
= "";
216 const char *suffix_dir
= "";
223 /* _UTSNAME_LENGTH is 65 */
226 err
= fetch_kernel_version(NULL
, release
,
232 prefix_dir
= "/lib/modules/";
233 suffix_dir
= "/build";
236 err
= asprintf(&autoconf_path
, "%s%s%s/include/generated/autoconf.h",
237 prefix_dir
, test_dir
, suffix_dir
);
241 if (access(autoconf_path
, R_OK
) == 0) {
244 err
= asprintf(kbuild_dir
, "%s%s%s", prefix_dir
, test_dir
,
254 static const char *kinc_fetch_script
=
255 "#!/usr/bin/env sh\n"
256 "if ! test -d \"$KBUILD_DIR\"\n"
260 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
264 "TMPDIR=`mktemp -d`\n"
265 "if test -z \"$TMPDIR\"\n"
269 "cat << EOF > $TMPDIR/Makefile\n"
271 "\\$(obj)/%.o: \\$(src)/%.c\n"
272 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
274 "touch $TMPDIR/dummy.c\n"
275 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
281 get_kbuild_opts(char **kbuild_dir
, char **kbuild_include_opts
)
285 if (!kbuild_dir
|| !kbuild_include_opts
)
289 *kbuild_include_opts
= NULL
;
291 if (llvm_param
.kbuild_dir
&& !llvm_param
.kbuild_dir
[0]) {
292 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
293 pr_debug("Skip kbuild options detection.\n");
297 err
= detect_kbuild_dir(kbuild_dir
);
300 "WARNING:\tunable to get correct kernel building directory.\n"
301 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
302 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
303 " \tdetection.\n\n");
307 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir
);
308 force_set_env("KBUILD_DIR", *kbuild_dir
);
309 force_set_env("KBUILD_OPTS", llvm_param
.kbuild_opts
);
310 err
= read_from_pipe(kinc_fetch_script
,
311 (void **)kbuild_include_opts
,
315 "WARNING:\tunable to get kernel include directories from '%s'\n"
316 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
317 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
318 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
326 pr_debug("include option is set to %s\n", *kbuild_include_opts
);
329 int llvm__compile_bpf(const char *path
, void **p_obj_buf
,
330 size_t *p_obj_buf_sz
)
333 void *obj_buf
= NULL
;
334 int err
, nr_cpus_avail
;
335 unsigned int kernel_version
;
336 char linux_version_code_str
[64];
337 const char *clang_opt
= llvm_param
.clang_opt
;
338 char clang_path
[PATH_MAX
], abspath
[PATH_MAX
], nr_cpus_avail_str
[64];
339 char serr
[STRERR_BUFSIZE
];
340 char *kbuild_dir
= NULL
, *kbuild_include_opts
= NULL
;
341 const char *template = llvm_param
.clang_bpf_cmd_template
;
343 if (path
[0] != '-' && realpath(path
, abspath
) == NULL
) {
345 pr_err("ERROR: problems with path %s: %s\n",
346 path
, strerror_r(err
, serr
, sizeof(serr
)));
351 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE
;
353 err
= search_program(llvm_param
.clang_path
,
354 "clang", clang_path
);
357 "ERROR:\tunable to find clang.\n"
358 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
359 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
365 * This is an optional work. Even it fail we can continue our
366 * work. Needn't to check error return.
368 get_kbuild_opts(&kbuild_dir
, &kbuild_include_opts
);
370 nr_cpus_avail
= sysconf(_SC_NPROCESSORS_CONF
);
371 if (nr_cpus_avail
<= 0) {
373 "WARNING:\tunable to get available CPUs in this system: %s\n"
374 " \tUse 128 instead.\n", strerror_r(errno
, serr
, sizeof(serr
)));
377 snprintf(nr_cpus_avail_str
, sizeof(nr_cpus_avail_str
), "%d",
380 if (fetch_kernel_version(&kernel_version
, NULL
, 0))
383 snprintf(linux_version_code_str
, sizeof(linux_version_code_str
),
384 "0x%x", kernel_version
);
386 force_set_env("NR_CPUS", nr_cpus_avail_str
);
387 force_set_env("LINUX_VERSION_CODE", linux_version_code_str
);
388 force_set_env("CLANG_EXEC", clang_path
);
389 force_set_env("CLANG_OPTIONS", clang_opt
);
390 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts
);
391 force_set_env("WORKING_DIR", kbuild_dir
? : ".");
394 * Since we may reset clang's working dir, path of source file
395 * should be transferred into absolute path, except we want
396 * stdin to be source file (testing).
398 force_set_env("CLANG_SOURCE",
399 (path
[0] == '-') ? path
: abspath
);
401 pr_debug("llvm compiling command template: %s\n", template);
402 err
= read_from_pipe(template, &obj_buf
, &obj_buf_sz
);
404 pr_err("ERROR:\tunable to compile %s\n", path
);
405 pr_err("Hint:\tCheck error message shown above.\n");
406 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
407 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path
);
408 pr_err(" \twith proper -I and -D options.\n");
413 free(kbuild_include_opts
);
417 *p_obj_buf
= obj_buf
;
420 *p_obj_buf_sz
= obj_buf_sz
;
424 free(kbuild_include_opts
);
433 int llvm__search_clang(void)
435 char clang_path
[PATH_MAX
];
437 return search_program(llvm_param
.clang_path
, "clang", clang_path
);