11 #include <linux/err.h> 17 #include <subcmd/exec-cmd.h> 19 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ 20 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ 21 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \ 22 "$CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS " \ 23 "-Wno-unused-value -Wno-pointer-sign " \ 24 "-working-directory $WORKING_DIR " \ 25 "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -" 33 .user_set_param =
false,
38 if (!strstarts(var,
"llvm."))
40 var +=
sizeof(
"llvm.") - 1;
42 if (!strcmp(var,
"clang-path"))
44 else if (!strcmp(var,
"clang-bpf-cmd-template"))
46 else if (!strcmp(var,
"clang-opt"))
48 else if (!strcmp(var,
"kbuild-dir"))
50 else if (!strcmp(var,
"kbuild-opts"))
52 else if (!strcmp(var,
"dump-obj"))
55 pr_debug(
"Invalid LLVM config option: %s\n", value);
66 char *env, *path, *tmp = NULL;
71 if (def && def[0] !=
'\0') {
73 if (access(def, F_OK) == 0) {
77 }
else if (def[0] !=
'\0')
89 path = strtok_r(env,
":", &tmp);
91 scnprintf(buf,
sizeof(buf),
"%s/%s", path, name);
92 if (access(buf, F_OK) == 0) {
97 path = strtok_r(NULL,
":", &tmp);
104 #define READ_SIZE 4096 111 size_t read_sz = 0, buf_sz = 0;
114 file = popen(cmd,
"r");
116 pr_err(
"ERROR: unable to popen cmd: %s\n",
117 str_error_r(errno, serr,
sizeof(serr)));
121 while (!feof(file) && !ferror(file)) {
130 new_buf = realloc(buf, buf_sz);
133 pr_err(
"ERROR: failed to realloc memory\n");
140 read_sz += fread(buf + read_sz, 1,
READ_SIZE, file);
143 if (buf_sz - read_sz < 1) {
144 pr_err(
"ERROR: internal error\n");
150 pr_err(
"ERROR: error occurred when reading from pipe: %s\n",
151 str_error_r(errno, serr,
sizeof(serr)));
156 err = WEXITSTATUS(pclose(file));
168 ((
char *)buf)[read_sz] =
'\0';
176 *p_read_sz = read_sz;
194 setenv(var, value, 1);
195 pr_debug(
"set env: %s=%s\n", var, value);
206 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n" 207 " \tYou may want to try git trunk:\n" 208 " \t\tgit clone http://llvm.org/git/llvm.git\n" 210 " \t\tgit clone http://llvm.org/git/clang.git\n\n" 211 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n" 212 " \tdebian/ubuntu:\n" 213 " \t\thttp://llvm.org/apt\n\n" 214 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n" 215 " \toption in [llvm] section of ~/.perfconfig to:\n\n" 216 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS \\\n" 217 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n" 218 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n" 219 " \t(Replace /path/to/llc with path to your llc)\n\n" 226 const char *prefix_dir =
"";
227 const char *suffix_dir =
"";
243 prefix_dir =
"/lib/modules/";
244 suffix_dir =
"/build";
247 err = asprintf(&autoconf_path,
"%s%s%s/include/generated/autoconf.h",
248 prefix_dir, test_dir, suffix_dir);
252 if (access(autoconf_path, R_OK) == 0) {
255 err = asprintf(kbuild_dir,
"%s%s%s", prefix_dir, test_dir,
266 "#!/usr/bin/env sh\n" 267 "if ! test -d \"$KBUILD_DIR\"\n" 271 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n" 275 "TMPDIR=`mktemp -d`\n" 276 "if test -z \"$TMPDIR\"\n" 280 "cat << EOF > $TMPDIR/Makefile\n" 282 "\\$(obj)/%.o: \\$(src)/%.c\n" 283 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n" 285 "touch $TMPDIR/dummy.c\n" 286 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n" 293 static char *saved_kbuild_dir;
294 static char *saved_kbuild_include_opts;
297 if (!kbuild_dir || !kbuild_include_opts)
301 *kbuild_include_opts = NULL;
303 if (saved_kbuild_dir && saved_kbuild_include_opts &&
304 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
305 *kbuild_dir = strdup(saved_kbuild_dir);
306 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
308 if (*kbuild_dir && *kbuild_include_opts)
312 zfree(kbuild_include_opts);
322 pr_debug(
"[llvm.kbuild-dir] is set to \"\" deliberately.\n");
323 pr_debug(
"Skip kbuild options detection.\n");
330 "WARNING:\tunable to get correct kernel building directory.\n" 331 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n" 332 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n" 333 " \tdetection.\n\n");
337 pr_debug(
"Kernel build dir is set to %s\n", *kbuild_dir);
341 (
void **)kbuild_include_opts,
345 "WARNING:\tunable to get kernel include directories from '%s'\n" 346 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n" 347 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n" 348 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
356 pr_debug(
"include option is set to %s\n", *kbuild_include_opts);
358 saved_kbuild_dir = strdup(*kbuild_dir);
359 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
361 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
362 zfree(&saved_kbuild_dir);
363 zfree(&saved_kbuild_include_opts);
367 saved_kbuild_dir = ERR_PTR(-EINVAL);
368 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
373 static int nr_cpus_avail = 0;
376 if (nr_cpus_avail > 0)
377 return nr_cpus_avail;
379 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
380 if (nr_cpus_avail <= 0) {
382 "WARNING:\tunable to get available CPUs in this system: %s\n" 383 " \tUse 128 instead.\n", str_error_r(errno, serr,
sizeof(serr)));
386 return nr_cpus_avail;
391 char *obj_path = strdup(path);
396 pr_warning(
"WARNING: Not enough memory, skip object dumping\n");
400 p = strrchr(obj_path,
'.');
401 if (!p || (strcmp(p,
".c") != 0)) {
402 pr_warning(
"WARNING: invalid llvm source path: '%s', skip object dumping\n",
408 fp = fopen(obj_path,
"wb");
410 pr_warning(
"WARNING: failed to open '%s': %s, skip object dumping\n",
415 pr_info(
"LLVM: dumping %s\n", obj_path);
416 if (fwrite(obj_buf, size, 1, fp) != 1)
417 pr_warning(
"WARNING: failed to write to file '%s': %s, skip object dumping\n",
425 size_t *p_obj_buf_sz)
428 void *obj_buf = NULL;
429 int err, nr_cpus_avail;
430 unsigned int kernel_version;
431 char linux_version_code_str[64];
435 char *
kbuild_dir = NULL, *kbuild_include_opts = NULL,
436 *perf_bpf_include_opts = NULL;
438 char *command_echo = NULL, *command_out;
439 char *perf_include_dir = system_path(PERF_INCLUDE_DIR);
441 if (path[0] !=
'-' && realpath(path, abspath) == NULL) {
443 pr_err(
"ERROR: problems with path %s: %s\n",
444 path, str_error_r(err, serr,
sizeof(serr)));
452 "clang", clang_path);
455 "ERROR:\tunable to find clang.\n" 456 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n" 457 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
469 snprintf(nr_cpus_avail_str,
sizeof(nr_cpus_avail_str),
"%d",
475 snprintf(linux_version_code_str,
sizeof(linux_version_code_str),
476 "0x%x", kernel_version);
477 if (asprintf(&perf_bpf_include_opts,
"-I%s/bpf", perf_include_dir) < 0)
484 force_set_env(
"PERF_BPF_INC_OPTIONS", perf_bpf_include_opts);
493 (path[0] ==
'-') ? path : abspath);
495 pr_debug(
"llvm compiling command template: %s\n",
template);
497 if (asprintf(&command_echo,
"echo -n \"%s\"",
template) < 0)
504 pr_debug(
"llvm compiling command : %s\n", command_out);
508 pr_err(
"ERROR:\tunable to compile %s\n", path);
509 pr_err(
"Hint:\tCheck error message shown above.\n");
510 pr_err(
"Hint:\tYou can also pre-compile it into .o using:\n");
511 pr_err(
" \t\tclang -target bpf -O2 -c %s\n", path);
512 pr_err(
" \twith proper -I and -D options.\n");
519 free(kbuild_include_opts);
520 free(perf_bpf_include_opts);
521 free(perf_include_dir);
526 *p_obj_buf = obj_buf;
529 *p_obj_buf_sz = obj_buf_sz;
534 free(kbuild_include_opts);
536 free(perf_bpf_include_opts);
537 free(perf_include_dir);
int fetch_kernel_version(unsigned int *puint, char *str, size_t str_size)
const char * clang_bpf_cmd_template
static void version_notice(void)
int perf_config_bool(const char *name, const char *value)
#define CLANG_BPF_CMD_DEFAULT_TEMPLATE
int perf_llvm_config(const char *var, const char *value)
static void force_set_env(const char *var, const char *value)
static int search_program(const char *def, const char *name, char *output)
#define pr_debug(fmt,...)
static int detect_kbuild_dir(char **kbuild_dir)
static const char * kinc_fetch_script
void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
int llvm__get_nr_cpus(void)
static int read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
static struct @9 output[OUTPUT_TYPE_MAX]
int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz)
int llvm__search_clang(void)
void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
#define pr_warning(fmt,...)