Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
4 * Copyright (C) 2015, Huawei Inc.
5 */
6
7#include <errno.h>
8#include <limits.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <linux/err.h>
12#include "debug.h"
13#include "llvm-utils.h"
14#include "config.h"
15#include "util.h"
16#include <sys/wait.h>
17
18#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
19 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
20 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
21 "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
22 "-Wno-unused-value -Wno-pointer-sign " \
23 "-working-directory $WORKING_DIR " \
24 "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
25
26struct llvm_param llvm_param = {
27 .clang_path = "clang",
28 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
29 .clang_opt = NULL,
30 .kbuild_dir = NULL,
31 .kbuild_opts = NULL,
32 .user_set_param = false,
33};
34
35int perf_llvm_config(const char *var, const char *value)
36{
37 if (!strstarts(var, "llvm."))
38 return 0;
39 var += sizeof("llvm.") - 1;
40
41 if (!strcmp(var, "clang-path"))
42 llvm_param.clang_path = strdup(value);
43 else if (!strcmp(var, "clang-bpf-cmd-template"))
44 llvm_param.clang_bpf_cmd_template = strdup(value);
45 else if (!strcmp(var, "clang-opt"))
46 llvm_param.clang_opt = strdup(value);
47 else if (!strcmp(var, "kbuild-dir"))
48 llvm_param.kbuild_dir = strdup(value);
49 else if (!strcmp(var, "kbuild-opts"))
50 llvm_param.kbuild_opts = strdup(value);
51 else if (!strcmp(var, "dump-obj"))
52 llvm_param.dump_obj = !!perf_config_bool(var, value);
53 else {
54 pr_debug("Invalid LLVM config option: %s\n", value);
55 return -1;
56 }
57 llvm_param.user_set_param = true;
58 return 0;
59}
60
61static int
62search_program(const char *def, const char *name,
63 char *output)
64{
65 char *env, *path, *tmp = NULL;
66 char buf[PATH_MAX];
67 int ret;
68
69 output[0] = '\0';
70 if (def && def[0] != '\0') {
71 if (def[0] == '/') {
72 if (access(def, F_OK) == 0) {
73 strlcpy(output, def, PATH_MAX);
74 return 0;
75 }
76 } else if (def[0] != '\0')
77 name = def;
78 }
79
80 env = getenv("PATH");
81 if (!env)
82 return -1;
83 env = strdup(env);
84 if (!env)
85 return -1;
86
87 ret = -ENOENT;
88 path = strtok_r(env, ":", &tmp);
89 while (path) {
90 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
91 if (access(buf, F_OK) == 0) {
92 strlcpy(output, buf, PATH_MAX);
93 ret = 0;
94 break;
95 }
96 path = strtok_r(NULL, ":", &tmp);
97 }
98
99 free(env);
100 return ret;
101}
102
103#define READ_SIZE 4096
104static int
105read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
106{
107 int err = 0;
108 void *buf = NULL;
109 FILE *file = NULL;
110 size_t read_sz = 0, buf_sz = 0;
111 char serr[STRERR_BUFSIZE];
112
113 file = popen(cmd, "r");
114 if (!file) {
115 pr_err("ERROR: unable to popen cmd: %s\n",
116 str_error_r(errno, serr, sizeof(serr)));
117 return -EINVAL;
118 }
119
120 while (!feof(file) && !ferror(file)) {
121 /*
122 * Make buf_sz always have obe byte extra space so we
123 * can put '\0' there.
124 */
125 if (buf_sz - read_sz < READ_SIZE + 1) {
126 void *new_buf;
127
128 buf_sz = read_sz + READ_SIZE + 1;
129 new_buf = realloc(buf, buf_sz);
130
131 if (!new_buf) {
132 pr_err("ERROR: failed to realloc memory\n");
133 err = -ENOMEM;
134 goto errout;
135 }
136
137 buf = new_buf;
138 }
139 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
140 }
141
142 if (buf_sz - read_sz < 1) {
143 pr_err("ERROR: internal error\n");
144 err = -EINVAL;
145 goto errout;
146 }
147
148 if (ferror(file)) {
149 pr_err("ERROR: error occurred when reading from pipe: %s\n",
150 str_error_r(errno, serr, sizeof(serr)));
151 err = -EIO;
152 goto errout;
153 }
154
155 err = WEXITSTATUS(pclose(file));
156 file = NULL;
157 if (err) {
158 err = -EINVAL;
159 goto errout;
160 }
161
162 /*
163 * If buf is string, give it terminal '\0' to make our life
164 * easier. If buf is not string, that '\0' is out of space
165 * indicated by read_sz so caller won't even notice it.
166 */
167 ((char *)buf)[read_sz] = '\0';
168
169 if (!p_buf)
170 free(buf);
171 else
172 *p_buf = buf;
173
174 if (p_read_sz)
175 *p_read_sz = read_sz;
176 return 0;
177
178errout:
179 if (file)
180 pclose(file);
181 free(buf);
182 if (p_buf)
183 *p_buf = NULL;
184 if (p_read_sz)
185 *p_read_sz = 0;
186 return err;
187}
188
189static inline void
190force_set_env(const char *var, const char *value)
191{
192 if (value) {
193 setenv(var, value, 1);
194 pr_debug("set env: %s=%s\n", var, value);
195 } else {
196 unsetenv(var);
197 pr_debug("unset env: %s\n", var);
198 }
199}
200
201static void
202version_notice(void)
203{
204 pr_err(
205" \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
206" \tYou may want to try git trunk:\n"
207" \t\tgit clone http://llvm.org/git/llvm.git\n"
208" \t\t and\n"
209" \t\tgit clone http://llvm.org/git/clang.git\n\n"
210" \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
211" \tdebian/ubuntu:\n"
212" \t\thttp://llvm.org/apt\n\n"
213" \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
214" \toption in [llvm] section of ~/.perfconfig to:\n\n"
215" \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
216" \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
217" \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
218" \t(Replace /path/to/llc with path to your llc)\n\n"
219);
220}
221
222static int detect_kbuild_dir(char **kbuild_dir)
223{
224 const char *test_dir = llvm_param.kbuild_dir;
225 const char *prefix_dir = "";
226 const char *suffix_dir = "";
227
228 char *autoconf_path;
229
230 int err;
231
232 if (!test_dir) {
233 /* _UTSNAME_LENGTH is 65 */
234 char release[128];
235
236 err = fetch_kernel_version(NULL, release,
237 sizeof(release));
238 if (err)
239 return -EINVAL;
240
241 test_dir = release;
242 prefix_dir = "/lib/modules/";
243 suffix_dir = "/build";
244 }
245
246 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
247 prefix_dir, test_dir, suffix_dir);
248 if (err < 0)
249 return -ENOMEM;
250
251 if (access(autoconf_path, R_OK) == 0) {
252 free(autoconf_path);
253
254 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
255 suffix_dir);
256 if (err < 0)
257 return -ENOMEM;
258 return 0;
259 }
260 free(autoconf_path);
261 return -ENOENT;
262}
263
264static const char *kinc_fetch_script =
265"#!/usr/bin/env sh\n"
266"if ! test -d \"$KBUILD_DIR\"\n"
267"then\n"
268" exit -1\n"
269"fi\n"
270"if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
271"then\n"
272" exit -1\n"
273"fi\n"
274"TMPDIR=`mktemp -d`\n"
275"if test -z \"$TMPDIR\"\n"
276"then\n"
277" exit -1\n"
278"fi\n"
279"cat << EOF > $TMPDIR/Makefile\n"
280"obj-y := dummy.o\n"
281"\\$(obj)/%.o: \\$(src)/%.c\n"
282"\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
283"EOF\n"
284"touch $TMPDIR/dummy.c\n"
285"make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
286"RET=$?\n"
287"rm -rf $TMPDIR\n"
288"exit $RET\n";
289
290void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
291{
292 static char *saved_kbuild_dir;
293 static char *saved_kbuild_include_opts;
294 int err;
295
296 if (!kbuild_dir || !kbuild_include_opts)
297 return;
298
299 *kbuild_dir = NULL;
300 *kbuild_include_opts = NULL;
301
302 if (saved_kbuild_dir && saved_kbuild_include_opts &&
303 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
304 *kbuild_dir = strdup(saved_kbuild_dir);
305 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
306
307 if (*kbuild_dir && *kbuild_include_opts)
308 return;
309
310 zfree(kbuild_dir);
311 zfree(kbuild_include_opts);
312 /*
313 * Don't fall through: it may breaks saved_kbuild_dir and
314 * saved_kbuild_include_opts if detect them again when
315 * memory is low.
316 */
317 return;
318 }
319
320 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
321 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
322 pr_debug("Skip kbuild options detection.\n");
323 goto errout;
324 }
325
326 err = detect_kbuild_dir(kbuild_dir);
327 if (err) {
328 pr_warning(
329"WARNING:\tunable to get correct kernel building directory.\n"
330"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
331" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
332" \tdetection.\n\n");
333 goto errout;
334 }
335
336 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
337 force_set_env("KBUILD_DIR", *kbuild_dir);
338 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
339 err = read_from_pipe(kinc_fetch_script,
340 (void **)kbuild_include_opts,
341 NULL);
342 if (err) {
343 pr_warning(
344"WARNING:\tunable to get kernel include directories from '%s'\n"
345"Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
346" \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
347" \toption in [llvm] to \"\" to suppress this detection.\n\n",
348 *kbuild_dir);
349
350 free(*kbuild_dir);
351 *kbuild_dir = NULL;
352 goto errout;
353 }
354
355 pr_debug("include option is set to %s\n", *kbuild_include_opts);
356
357 saved_kbuild_dir = strdup(*kbuild_dir);
358 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
359
360 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
361 zfree(&saved_kbuild_dir);
362 zfree(&saved_kbuild_include_opts);
363 }
364 return;
365errout:
366 saved_kbuild_dir = ERR_PTR(-EINVAL);
367 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
368}
369
370int llvm__get_nr_cpus(void)
371{
372 static int nr_cpus_avail = 0;
373 char serr[STRERR_BUFSIZE];
374
375 if (nr_cpus_avail > 0)
376 return nr_cpus_avail;
377
378 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
379 if (nr_cpus_avail <= 0) {
380 pr_err(
381"WARNING:\tunable to get available CPUs in this system: %s\n"
382" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
383 nr_cpus_avail = 128;
384 }
385 return nr_cpus_avail;
386}
387
388void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
389{
390 char *obj_path = strdup(path);
391 FILE *fp;
392 char *p;
393
394 if (!obj_path) {
395 pr_warning("WARNING: Not enough memory, skip object dumping\n");
396 return;
397 }
398
399 p = strrchr(obj_path, '.');
400 if (!p || (strcmp(p, ".c") != 0)) {
401 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
402 obj_path);
403 goto out;
404 }
405
406 p[1] = 'o';
407 fp = fopen(obj_path, "wb");
408 if (!fp) {
409 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
410 obj_path, strerror(errno));
411 goto out;
412 }
413
414 pr_info("LLVM: dumping %s\n", obj_path);
415 if (fwrite(obj_buf, size, 1, fp) != 1)
416 pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n",
417 obj_path, strerror(errno));
418 fclose(fp);
419out:
420 free(obj_path);
421}
422
423int llvm__compile_bpf(const char *path, void **p_obj_buf,
424 size_t *p_obj_buf_sz)
425{
426 size_t obj_buf_sz;
427 void *obj_buf = NULL;
428 int err, nr_cpus_avail;
429 unsigned int kernel_version;
430 char linux_version_code_str[64];
431 const char *clang_opt = llvm_param.clang_opt;
432 char clang_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
433 char serr[STRERR_BUFSIZE];
434 char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
435 const char *template = llvm_param.clang_bpf_cmd_template;
436 char *command_echo, *command_out;
437
438 if (path[0] != '-' && realpath(path, abspath) == NULL) {
439 err = errno;
440 pr_err("ERROR: problems with path %s: %s\n",
441 path, str_error_r(err, serr, sizeof(serr)));
442 return -err;
443 }
444
445 if (!template)
446 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
447
448 err = search_program(llvm_param.clang_path,
449 "clang", clang_path);
450 if (err) {
451 pr_err(
452"ERROR:\tunable to find clang.\n"
453"Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
454" \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
455 version_notice();
456 return -ENOENT;
457 }
458
459 /*
460 * This is an optional work. Even it fail we can continue our
461 * work. Needn't to check error return.
462 */
463 llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
464
465 nr_cpus_avail = llvm__get_nr_cpus();
466 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
467 nr_cpus_avail);
468
469 if (fetch_kernel_version(&kernel_version, NULL, 0))
470 kernel_version = 0;
471
472 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
473 "0x%x", kernel_version);
474
475 force_set_env("NR_CPUS", nr_cpus_avail_str);
476 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
477 force_set_env("CLANG_EXEC", clang_path);
478 force_set_env("CLANG_OPTIONS", clang_opt);
479 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
480 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
481
482 /*
483 * Since we may reset clang's working dir, path of source file
484 * should be transferred into absolute path, except we want
485 * stdin to be source file (testing).
486 */
487 force_set_env("CLANG_SOURCE",
488 (path[0] == '-') ? path : abspath);
489
490 pr_debug("llvm compiling command template: %s\n", template);
491
492 if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
493 goto errout;
494
495 err = read_from_pipe(command_echo, (void **) &command_out, NULL);
496 if (err)
497 goto errout;
498
499 pr_debug("llvm compiling command : %s\n", command_out);
500
501 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
502 if (err) {
503 pr_err("ERROR:\tunable to compile %s\n", path);
504 pr_err("Hint:\tCheck error message shown above.\n");
505 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
506 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
507 pr_err(" \twith proper -I and -D options.\n");
508 goto errout;
509 }
510
511 free(command_echo);
512 free(command_out);
513 free(kbuild_dir);
514 free(kbuild_include_opts);
515
516 if (!p_obj_buf)
517 free(obj_buf);
518 else
519 *p_obj_buf = obj_buf;
520
521 if (p_obj_buf_sz)
522 *p_obj_buf_sz = obj_buf_sz;
523 return 0;
524errout:
525 free(command_echo);
526 free(kbuild_dir);
527 free(kbuild_include_opts);
528 free(obj_buf);
529 if (p_obj_buf)
530 *p_obj_buf = NULL;
531 if (p_obj_buf_sz)
532 *p_obj_buf_sz = 0;
533 return err;
534}
535
536int llvm__search_clang(void)
537{
538 char clang_path[PATH_MAX];
539
540 return search_program(llvm_param.clang_path, "clang", clang_path);
541}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
4 * Copyright (C) 2015, Huawei Inc.
5 */
6
7#include <errno.h>
8#include <limits.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <linux/err.h>
13#include <linux/string.h>
14#include <linux/zalloc.h>
15#include "debug.h"
16#include "llvm-utils.h"
17#include "config.h"
18#include "util.h"
19#include <sys/wait.h>
20#include <subcmd/exec-cmd.h>
21
22#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
23 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
24 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
25 "$CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS " \
26 "-Wno-unused-value -Wno-pointer-sign " \
27 "-working-directory $WORKING_DIR " \
28 "-c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -g -O2 -o - $LLVM_OPTIONS_PIPE"
29
30struct llvm_param llvm_param = {
31 .clang_path = "clang",
32 .llc_path = "llc",
33 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
34 .clang_opt = NULL,
35 .opts = NULL,
36 .kbuild_dir = NULL,
37 .kbuild_opts = NULL,
38 .user_set_param = false,
39};
40
41static void version_notice(void);
42
43int perf_llvm_config(const char *var, const char *value)
44{
45 if (!strstarts(var, "llvm."))
46 return 0;
47 var += sizeof("llvm.") - 1;
48
49 if (!strcmp(var, "clang-path"))
50 llvm_param.clang_path = strdup(value);
51 else if (!strcmp(var, "clang-bpf-cmd-template"))
52 llvm_param.clang_bpf_cmd_template = strdup(value);
53 else if (!strcmp(var, "clang-opt"))
54 llvm_param.clang_opt = strdup(value);
55 else if (!strcmp(var, "kbuild-dir"))
56 llvm_param.kbuild_dir = strdup(value);
57 else if (!strcmp(var, "kbuild-opts"))
58 llvm_param.kbuild_opts = strdup(value);
59 else if (!strcmp(var, "dump-obj"))
60 llvm_param.dump_obj = !!perf_config_bool(var, value);
61 else if (!strcmp(var, "opts"))
62 llvm_param.opts = strdup(value);
63 else {
64 pr_debug("Invalid LLVM config option: %s\n", value);
65 return -1;
66 }
67 llvm_param.user_set_param = true;
68 return 0;
69}
70
71static int
72search_program(const char *def, const char *name,
73 char *output)
74{
75 char *env, *path, *tmp = NULL;
76 char buf[PATH_MAX];
77 int ret;
78
79 output[0] = '\0';
80 if (def && def[0] != '\0') {
81 if (def[0] == '/') {
82 if (access(def, F_OK) == 0) {
83 strlcpy(output, def, PATH_MAX);
84 return 0;
85 }
86 } else if (def[0] != '\0')
87 name = def;
88 }
89
90 env = getenv("PATH");
91 if (!env)
92 return -1;
93 env = strdup(env);
94 if (!env)
95 return -1;
96
97 ret = -ENOENT;
98 path = strtok_r(env, ":", &tmp);
99 while (path) {
100 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
101 if (access(buf, F_OK) == 0) {
102 strlcpy(output, buf, PATH_MAX);
103 ret = 0;
104 break;
105 }
106 path = strtok_r(NULL, ":", &tmp);
107 }
108
109 free(env);
110 return ret;
111}
112
113static int search_program_and_warn(const char *def, const char *name,
114 char *output)
115{
116 int ret = search_program(def, name, output);
117
118 if (ret) {
119 pr_err("ERROR:\tunable to find %s.\n"
120 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
121 " \tand '%s-path' option in [llvm] section of ~/.perfconfig.\n",
122 name, name);
123 version_notice();
124 }
125 return ret;
126}
127
128#define READ_SIZE 4096
129static int
130read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
131{
132 int err = 0;
133 void *buf = NULL;
134 FILE *file = NULL;
135 size_t read_sz = 0, buf_sz = 0;
136 char serr[STRERR_BUFSIZE];
137
138 file = popen(cmd, "r");
139 if (!file) {
140 pr_err("ERROR: unable to popen cmd: %s\n",
141 str_error_r(errno, serr, sizeof(serr)));
142 return -EINVAL;
143 }
144
145 while (!feof(file) && !ferror(file)) {
146 /*
147 * Make buf_sz always have obe byte extra space so we
148 * can put '\0' there.
149 */
150 if (buf_sz - read_sz < READ_SIZE + 1) {
151 void *new_buf;
152
153 buf_sz = read_sz + READ_SIZE + 1;
154 new_buf = realloc(buf, buf_sz);
155
156 if (!new_buf) {
157 pr_err("ERROR: failed to realloc memory\n");
158 err = -ENOMEM;
159 goto errout;
160 }
161
162 buf = new_buf;
163 }
164 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
165 }
166
167 if (buf_sz - read_sz < 1) {
168 pr_err("ERROR: internal error\n");
169 err = -EINVAL;
170 goto errout;
171 }
172
173 if (ferror(file)) {
174 pr_err("ERROR: error occurred when reading from pipe: %s\n",
175 str_error_r(errno, serr, sizeof(serr)));
176 err = -EIO;
177 goto errout;
178 }
179
180 err = WEXITSTATUS(pclose(file));
181 file = NULL;
182 if (err) {
183 err = -EINVAL;
184 goto errout;
185 }
186
187 /*
188 * If buf is string, give it terminal '\0' to make our life
189 * easier. If buf is not string, that '\0' is out of space
190 * indicated by read_sz so caller won't even notice it.
191 */
192 ((char *)buf)[read_sz] = '\0';
193
194 if (!p_buf)
195 free(buf);
196 else
197 *p_buf = buf;
198
199 if (p_read_sz)
200 *p_read_sz = read_sz;
201 return 0;
202
203errout:
204 if (file)
205 pclose(file);
206 free(buf);
207 if (p_buf)
208 *p_buf = NULL;
209 if (p_read_sz)
210 *p_read_sz = 0;
211 return err;
212}
213
214static inline void
215force_set_env(const char *var, const char *value)
216{
217 if (value) {
218 setenv(var, value, 1);
219 pr_debug("set env: %s=%s\n", var, value);
220 } else {
221 unsetenv(var);
222 pr_debug("unset env: %s\n", var);
223 }
224}
225
226static void
227version_notice(void)
228{
229 pr_err(
230" \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
231" \tYou may want to try git trunk:\n"
232" \t\tgit clone http://llvm.org/git/llvm.git\n"
233" \t\t and\n"
234" \t\tgit clone http://llvm.org/git/clang.git\n\n"
235" \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
236" \tdebian/ubuntu:\n"
237" \t\thttps://apt.llvm.org/\n\n"
238" \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
239" \toption in [llvm] section of ~/.perfconfig to:\n\n"
240" \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS \\\n"
241" \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
242" \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
243" \t(Replace /path/to/llc with path to your llc)\n\n"
244);
245}
246
247static int detect_kbuild_dir(char **kbuild_dir)
248{
249 const char *test_dir = llvm_param.kbuild_dir;
250 const char *prefix_dir = "";
251 const char *suffix_dir = "";
252
253 /* _UTSNAME_LENGTH is 65 */
254 char release[128];
255
256 char *autoconf_path;
257
258 int err;
259
260 if (!test_dir) {
261 err = fetch_kernel_version(NULL, release,
262 sizeof(release));
263 if (err)
264 return -EINVAL;
265
266 test_dir = release;
267 prefix_dir = "/lib/modules/";
268 suffix_dir = "/build";
269 }
270
271 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
272 prefix_dir, test_dir, suffix_dir);
273 if (err < 0)
274 return -ENOMEM;
275
276 if (access(autoconf_path, R_OK) == 0) {
277 free(autoconf_path);
278
279 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
280 suffix_dir);
281 if (err < 0)
282 return -ENOMEM;
283 return 0;
284 }
285 pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n",
286 __func__, autoconf_path);
287 free(autoconf_path);
288 return -ENOENT;
289}
290
291static const char *kinc_fetch_script =
292"#!/usr/bin/env sh\n"
293"if ! test -d \"$KBUILD_DIR\"\n"
294"then\n"
295" exit 1\n"
296"fi\n"
297"if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
298"then\n"
299" exit 1\n"
300"fi\n"
301"TMPDIR=`mktemp -d`\n"
302"if test -z \"$TMPDIR\"\n"
303"then\n"
304" exit 1\n"
305"fi\n"
306"cat << EOF > $TMPDIR/Makefile\n"
307"obj-y := dummy.o\n"
308"\\$(obj)/%.o: \\$(src)/%.c\n"
309"\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
310"\t\\$(CC) -c -o \\$@ \\$<\n"
311"EOF\n"
312"touch $TMPDIR/dummy.c\n"
313"make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
314"RET=$?\n"
315"rm -rf $TMPDIR\n"
316"exit $RET\n";
317
318void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
319{
320 static char *saved_kbuild_dir;
321 static char *saved_kbuild_include_opts;
322 int err;
323
324 if (!kbuild_dir || !kbuild_include_opts)
325 return;
326
327 *kbuild_dir = NULL;
328 *kbuild_include_opts = NULL;
329
330 if (saved_kbuild_dir && saved_kbuild_include_opts &&
331 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
332 *kbuild_dir = strdup(saved_kbuild_dir);
333 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
334
335 if (*kbuild_dir && *kbuild_include_opts)
336 return;
337
338 zfree(kbuild_dir);
339 zfree(kbuild_include_opts);
340 /*
341 * Don't fall through: it may breaks saved_kbuild_dir and
342 * saved_kbuild_include_opts if detect them again when
343 * memory is low.
344 */
345 return;
346 }
347
348 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
349 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
350 pr_debug("Skip kbuild options detection.\n");
351 goto errout;
352 }
353
354 err = detect_kbuild_dir(kbuild_dir);
355 if (err) {
356 pr_warning(
357"WARNING:\tunable to get correct kernel building directory.\n"
358"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
359" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
360" \tdetection.\n\n");
361 goto errout;
362 }
363
364 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
365 force_set_env("KBUILD_DIR", *kbuild_dir);
366 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
367 err = read_from_pipe(kinc_fetch_script,
368 (void **)kbuild_include_opts,
369 NULL);
370 if (err) {
371 pr_warning(
372"WARNING:\tunable to get kernel include directories from '%s'\n"
373"Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
374" \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
375" \toption in [llvm] to \"\" to suppress this detection.\n\n",
376 *kbuild_dir);
377
378 zfree(kbuild_dir);
379 goto errout;
380 }
381
382 pr_debug("include option is set to %s\n", *kbuild_include_opts);
383
384 saved_kbuild_dir = strdup(*kbuild_dir);
385 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
386
387 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
388 zfree(&saved_kbuild_dir);
389 zfree(&saved_kbuild_include_opts);
390 }
391 return;
392errout:
393 saved_kbuild_dir = ERR_PTR(-EINVAL);
394 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
395}
396
397int llvm__get_nr_cpus(void)
398{
399 static int nr_cpus_avail = 0;
400 char serr[STRERR_BUFSIZE];
401
402 if (nr_cpus_avail > 0)
403 return nr_cpus_avail;
404
405 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
406 if (nr_cpus_avail <= 0) {
407 pr_err(
408"WARNING:\tunable to get available CPUs in this system: %s\n"
409" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
410 nr_cpus_avail = 128;
411 }
412 return nr_cpus_avail;
413}
414
415void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
416{
417 char *obj_path = strdup(path);
418 FILE *fp;
419 char *p;
420
421 if (!obj_path) {
422 pr_warning("WARNING: Not enough memory, skip object dumping\n");
423 return;
424 }
425
426 p = strrchr(obj_path, '.');
427 if (!p || (strcmp(p, ".c") != 0)) {
428 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
429 obj_path);
430 goto out;
431 }
432
433 p[1] = 'o';
434 fp = fopen(obj_path, "wb");
435 if (!fp) {
436 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
437 obj_path, strerror(errno));
438 goto out;
439 }
440
441 pr_debug("LLVM: dumping %s\n", obj_path);
442 if (fwrite(obj_buf, size, 1, fp) != 1)
443 pr_debug("WARNING: failed to write to file '%s': %s, skip object dumping\n", obj_path, strerror(errno));
444 fclose(fp);
445out:
446 free(obj_path);
447}
448
449int llvm__compile_bpf(const char *path, void **p_obj_buf,
450 size_t *p_obj_buf_sz)
451{
452 size_t obj_buf_sz;
453 void *obj_buf = NULL;
454 int err, nr_cpus_avail;
455 unsigned int kernel_version;
456 char linux_version_code_str[64];
457 const char *clang_opt = llvm_param.clang_opt;
458 char clang_path[PATH_MAX], llc_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
459 char serr[STRERR_BUFSIZE];
460 char *kbuild_dir = NULL, *kbuild_include_opts = NULL,
461 *perf_bpf_include_opts = NULL;
462 const char *template = llvm_param.clang_bpf_cmd_template;
463 char *pipe_template = NULL;
464 const char *opts = llvm_param.opts;
465 char *command_echo = NULL, *command_out;
466 char *libbpf_include_dir = system_path(LIBBPF_INCLUDE_DIR);
467
468 if (path[0] != '-' && realpath(path, abspath) == NULL) {
469 err = errno;
470 pr_err("ERROR: problems with path %s: %s\n",
471 path, str_error_r(err, serr, sizeof(serr)));
472 return -err;
473 }
474
475 if (!template)
476 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
477
478 err = search_program_and_warn(llvm_param.clang_path,
479 "clang", clang_path);
480 if (err)
481 return -ENOENT;
482
483 /*
484 * This is an optional work. Even it fail we can continue our
485 * work. Needn't check error return.
486 */
487 llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
488
489 nr_cpus_avail = llvm__get_nr_cpus();
490 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
491 nr_cpus_avail);
492
493 if (fetch_kernel_version(&kernel_version, NULL, 0))
494 kernel_version = 0;
495
496 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
497 "0x%x", kernel_version);
498 if (asprintf(&perf_bpf_include_opts, "-I%s/", libbpf_include_dir) < 0)
499 goto errout;
500 force_set_env("NR_CPUS", nr_cpus_avail_str);
501 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
502 force_set_env("CLANG_EXEC", clang_path);
503 force_set_env("CLANG_OPTIONS", clang_opt);
504 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
505 force_set_env("PERF_BPF_INC_OPTIONS", perf_bpf_include_opts);
506 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
507
508 if (opts) {
509 err = search_program_and_warn(llvm_param.llc_path, "llc", llc_path);
510 if (err)
511 goto errout;
512
513 err = -ENOMEM;
514 if (asprintf(&pipe_template, "%s -emit-llvm | %s -march=bpf %s -filetype=obj -o -",
515 template, llc_path, opts) < 0) {
516 pr_err("ERROR:\tnot enough memory to setup command line\n");
517 goto errout;
518 }
519
520 template = pipe_template;
521
522 }
523
524 /*
525 * Since we may reset clang's working dir, path of source file
526 * should be transferred into absolute path, except we want
527 * stdin to be source file (testing).
528 */
529 force_set_env("CLANG_SOURCE",
530 (path[0] == '-') ? path : abspath);
531
532 pr_debug("llvm compiling command template: %s\n", template);
533
534 err = -ENOMEM;
535 if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
536 goto errout;
537
538 err = read_from_pipe(command_echo, (void **) &command_out, NULL);
539 if (err)
540 goto errout;
541
542 pr_debug("llvm compiling command : %s\n", command_out);
543
544 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
545 if (err) {
546 pr_err("ERROR:\tunable to compile %s\n", path);
547 pr_err("Hint:\tCheck error message shown above.\n");
548 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
549 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
550 pr_err(" \twith proper -I and -D options.\n");
551 goto errout;
552 }
553
554 free(command_echo);
555 free(command_out);
556 free(kbuild_dir);
557 free(kbuild_include_opts);
558 free(perf_bpf_include_opts);
559 free(libbpf_include_dir);
560
561 if (!p_obj_buf)
562 free(obj_buf);
563 else
564 *p_obj_buf = obj_buf;
565
566 if (p_obj_buf_sz)
567 *p_obj_buf_sz = obj_buf_sz;
568 return 0;
569errout:
570 free(command_echo);
571 free(kbuild_dir);
572 free(kbuild_include_opts);
573 free(obj_buf);
574 free(perf_bpf_include_opts);
575 free(libbpf_include_dir);
576 free(pipe_template);
577 if (p_obj_buf)
578 *p_obj_buf = NULL;
579 if (p_obj_buf_sz)
580 *p_obj_buf_sz = 0;
581 return err;
582}
583
584int llvm__search_clang(void)
585{
586 char clang_path[PATH_MAX];
587
588 return search_program_and_warn(llvm_param.clang_path, "clang", clang_path);
589}