blob: 8ee25bea1b24a1be8730fec4b2146f6d0a008c21 [file] [log] [blame]
Wang Nanaa61fd02015-07-21 11:13:34 +00001/*
2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
4 */
5
6#include <stdio.h>
Wang Nand325d782015-07-14 06:40:02 +00007#include <sys/utsname.h>
Wang Nanaa61fd02015-07-21 11:13:34 +00008#include "util.h"
9#include "debug.h"
10#include "llvm-utils.h"
11#include "cache.h"
12
13#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
Wang Nan59f41af2015-11-04 11:20:04 +000014 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
Wang Nan4a4f66a2015-11-04 11:20:05 +000015 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
Wang Nan59f41af2015-11-04 11:20:04 +000016 "$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 -"
Wang Nanaa61fd02015-07-21 11:13:34 +000020
21struct llvm_param llvm_param = {
22 .clang_path = "clang",
23 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
24 .clang_opt = NULL,
25 .kbuild_dir = NULL,
26 .kbuild_opts = NULL,
Wang Nan9bc898c2015-07-08 10:04:02 +000027 .user_set_param = false,
Wang Nanaa61fd02015-07-21 11:13:34 +000028};
29
30int perf_llvm_config(const char *var, const char *value)
31{
32 if (prefixcmp(var, "llvm."))
33 return 0;
34 var += sizeof("llvm.") - 1;
35
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
47 return -1;
Wang Nan9bc898c2015-07-08 10:04:02 +000048 llvm_param.user_set_param = true;
Wang Nanaa61fd02015-07-21 11:13:34 +000049 return 0;
50}
Wang Nan4cea3a92015-06-11 10:31:09 +000051
52static int
53search_program(const char *def, const char *name,
54 char *output)
55{
56 char *env, *path, *tmp = NULL;
57 char buf[PATH_MAX];
58 int ret;
59
60 output[0] = '\0';
61 if (def && def[0] != '\0') {
62 if (def[0] == '/') {
63 if (access(def, F_OK) == 0) {
64 strlcpy(output, def, PATH_MAX);
65 return 0;
66 }
67 } else if (def[0] != '\0')
68 name = def;
69 }
70
71 env = getenv("PATH");
72 if (!env)
73 return -1;
74 env = strdup(env);
75 if (!env)
76 return -1;
77
78 ret = -ENOENT;
79 path = strtok_r(env, ":", &tmp);
80 while (path) {
81 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
82 if (access(buf, F_OK) == 0) {
83 strlcpy(output, buf, PATH_MAX);
84 ret = 0;
85 break;
86 }
87 path = strtok_r(NULL, ":", &tmp);
88 }
89
90 free(env);
91 return ret;
92}
93
94#define READ_SIZE 4096
95static int
96read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
97{
98 int err = 0;
99 void *buf = NULL;
100 FILE *file = NULL;
101 size_t read_sz = 0, buf_sz = 0;
102
103 file = popen(cmd, "r");
104 if (!file) {
105 pr_err("ERROR: unable to popen cmd: %s\n",
106 strerror(errno));
107 return -EINVAL;
108 }
109
110 while (!feof(file) && !ferror(file)) {
111 /*
112 * Make buf_sz always have obe byte extra space so we
113 * can put '\0' there.
114 */
115 if (buf_sz - read_sz < READ_SIZE + 1) {
116 void *new_buf;
117
118 buf_sz = read_sz + READ_SIZE + 1;
119 new_buf = realloc(buf, buf_sz);
120
121 if (!new_buf) {
122 pr_err("ERROR: failed to realloc memory\n");
123 err = -ENOMEM;
124 goto errout;
125 }
126
127 buf = new_buf;
128 }
129 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
130 }
131
132 if (buf_sz - read_sz < 1) {
133 pr_err("ERROR: internal error\n");
134 err = -EINVAL;
135 goto errout;
136 }
137
138 if (ferror(file)) {
139 pr_err("ERROR: error occurred when reading from pipe: %s\n",
140 strerror(errno));
141 err = -EIO;
142 goto errout;
143 }
144
145 err = WEXITSTATUS(pclose(file));
146 file = NULL;
147 if (err) {
148 err = -EINVAL;
149 goto errout;
150 }
151
152 /*
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.
156 */
157 ((char *)buf)[read_sz] = '\0';
158
159 if (!p_buf)
160 free(buf);
161 else
162 *p_buf = buf;
163
164 if (p_read_sz)
165 *p_read_sz = read_sz;
166 return 0;
167
168errout:
169 if (file)
170 pclose(file);
171 free(buf);
172 if (p_buf)
173 *p_buf = NULL;
174 if (p_read_sz)
175 *p_read_sz = 0;
176 return err;
177}
178
179static inline void
180force_set_env(const char *var, const char *value)
181{
182 if (value) {
183 setenv(var, value, 1);
184 pr_debug("set env: %s=%s\n", var, value);
185 } else {
186 unsetenv(var);
187 pr_debug("unset env: %s\n", var);
188 }
189}
190
191static void
192version_notice(void)
193{
194 pr_err(
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"
198" \t\t and\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"
209);
210}
211
Wang Nand325d782015-07-14 06:40:02 +0000212static int detect_kbuild_dir(char **kbuild_dir)
213{
214 const char *test_dir = llvm_param.kbuild_dir;
215 const char *prefix_dir = "";
216 const char *suffix_dir = "";
217
218 char *autoconf_path;
219 struct utsname utsname;
220
221 int err;
222
223 if (!test_dir) {
224 err = uname(&utsname);
225 if (err) {
226 pr_warning("uname failed: %s\n", strerror(errno));
227 return -EINVAL;
228 }
229
230 test_dir = utsname.release;
231 prefix_dir = "/lib/modules/";
232 suffix_dir = "/build";
233 }
234
235 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
236 prefix_dir, test_dir, suffix_dir);
237 if (err < 0)
238 return -ENOMEM;
239
240 if (access(autoconf_path, R_OK) == 0) {
241 free(autoconf_path);
242
243 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
244 suffix_dir);
245 if (err < 0)
246 return -ENOMEM;
247 return 0;
248 }
249 free(autoconf_path);
250 return -ENOENT;
251}
252
Wang Nan0c6d18b2015-06-11 11:25:49 +0000253static const char *kinc_fetch_script =
254"#!/usr/bin/env sh\n"
255"if ! test -d \"$KBUILD_DIR\"\n"
256"then\n"
257" exit -1\n"
258"fi\n"
259"if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
260"then\n"
261" exit -1\n"
262"fi\n"
263"TMPDIR=`mktemp -d`\n"
264"if test -z \"$TMPDIR\"\n"
265"then\n"
266" exit -1\n"
267"fi\n"
268"cat << EOF > $TMPDIR/Makefile\n"
269"obj-y := dummy.o\n"
270"\\$(obj)/%.o: \\$(src)/%.c\n"
271"\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
272"EOF\n"
273"touch $TMPDIR/dummy.c\n"
274"make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
275"RET=$?\n"
276"rm -rf $TMPDIR\n"
277"exit $RET\n";
278
Wang Nand325d782015-07-14 06:40:02 +0000279static inline void
Wang Nan0c6d18b2015-06-11 11:25:49 +0000280get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
Wang Nand325d782015-07-14 06:40:02 +0000281{
282 int err;
283
Wang Nan0c6d18b2015-06-11 11:25:49 +0000284 if (!kbuild_dir || !kbuild_include_opts)
Wang Nand325d782015-07-14 06:40:02 +0000285 return;
286
287 *kbuild_dir = NULL;
Wang Nan0c6d18b2015-06-11 11:25:49 +0000288 *kbuild_include_opts = NULL;
Wang Nand325d782015-07-14 06:40:02 +0000289
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");
293 return;
294 }
295
296 err = detect_kbuild_dir(kbuild_dir);
297 if (err) {
298 pr_warning(
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");
303 return;
304 }
Wang Nan0c6d18b2015-06-11 11:25:49 +0000305
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,
311 NULL);
312 if (err) {
313 pr_warning(
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",
318 *kbuild_dir);
319
320 free(*kbuild_dir);
321 *kbuild_dir = NULL;
322 return;
323 }
324
325 pr_debug("include option is set to %s\n", *kbuild_include_opts);
Wang Nand325d782015-07-14 06:40:02 +0000326}
327
Wang Nan4a4f66a2015-11-04 11:20:05 +0000328static unsigned long
329fetch_kernel_version(void)
330{
331 struct utsname utsname;
332 int version, patchlevel, sublevel, err;
333
334 if (uname(&utsname))
335 return 0;
336
337 err = sscanf(utsname.release, "%d.%d.%d",
338 &version, &patchlevel, &sublevel);
339
340 if (err != 3) {
341 pr_debug("Unablt to get kernel version from uname '%s'\n",
342 utsname.release);
343 return 0;
344 }
345
346 return (version << 16) + (patchlevel << 8) + sublevel;
347}
348
Wang Nan4cea3a92015-06-11 10:31:09 +0000349int llvm__compile_bpf(const char *path, void **p_obj_buf,
350 size_t *p_obj_buf_sz)
351{
Wang Nan59f41af2015-11-04 11:20:04 +0000352 int err, nr_cpus_avail;
353 char clang_path[PATH_MAX], nr_cpus_avail_str[64];
Wang Nan4a4f66a2015-11-04 11:20:05 +0000354 char linux_version_code_str[64];
Wang Nan4cea3a92015-06-11 10:31:09 +0000355 const char *clang_opt = llvm_param.clang_opt;
356 const char *template = llvm_param.clang_bpf_cmd_template;
Wang Nan0c6d18b2015-06-11 11:25:49 +0000357 char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
Wang Nan4cea3a92015-06-11 10:31:09 +0000358 void *obj_buf = NULL;
359 size_t obj_buf_sz;
360
361 if (!template)
362 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
363
364 err = search_program(llvm_param.clang_path,
365 "clang", clang_path);
366 if (err) {
367 pr_err(
368"ERROR:\tunable to find clang.\n"
369"Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
370" \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
371 version_notice();
372 return -ENOENT;
373 }
374
Wang Nand325d782015-07-14 06:40:02 +0000375 /*
376 * This is an optional work. Even it fail we can continue our
377 * work. Needn't to check error return.
378 */
Wang Nan0c6d18b2015-06-11 11:25:49 +0000379 get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
Wang Nand325d782015-07-14 06:40:02 +0000380
Wang Nan59f41af2015-11-04 11:20:04 +0000381 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
382 if (nr_cpus_avail <= 0) {
383 pr_err(
384"WARNING:\tunable to get available CPUs in this system: %s\n"
385" \tUse 128 instead.\n", strerror(errno));
386 nr_cpus_avail = 128;
387 }
388 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
389 nr_cpus_avail);
390
Wang Nan4a4f66a2015-11-04 11:20:05 +0000391 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
392 "0x%lx", fetch_kernel_version());
393
Wang Nan59f41af2015-11-04 11:20:04 +0000394 force_set_env("NR_CPUS", nr_cpus_avail_str);
Wang Nan4a4f66a2015-11-04 11:20:05 +0000395 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
Wang Nan4cea3a92015-06-11 10:31:09 +0000396 force_set_env("CLANG_EXEC", clang_path);
397 force_set_env("CLANG_OPTIONS", clang_opt);
Wang Nan0c6d18b2015-06-11 11:25:49 +0000398 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
Wang Nand325d782015-07-14 06:40:02 +0000399 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
Wang Nan4cea3a92015-06-11 10:31:09 +0000400
401 /*
402 * Since we may reset clang's working dir, path of source file
403 * should be transferred into absolute path, except we want
404 * stdin to be source file (testing).
405 */
406 force_set_env("CLANG_SOURCE",
407 (path[0] == '-') ? path :
408 make_nonrelative_path(path));
409
410 pr_debug("llvm compiling command template: %s\n", template);
411 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
412 if (err) {
413 pr_err("ERROR:\tunable to compile %s\n", path);
414 pr_err("Hint:\tCheck error message shown above.\n");
415 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
416 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
417 pr_err(" \twith proper -I and -D options.\n");
418 goto errout;
419 }
420
Wang Nand325d782015-07-14 06:40:02 +0000421 free(kbuild_dir);
Wang Nan0c6d18b2015-06-11 11:25:49 +0000422 free(kbuild_include_opts);
Wang Nan4cea3a92015-06-11 10:31:09 +0000423 if (!p_obj_buf)
424 free(obj_buf);
425 else
426 *p_obj_buf = obj_buf;
427
428 if (p_obj_buf_sz)
429 *p_obj_buf_sz = obj_buf_sz;
430 return 0;
431errout:
Wang Nand325d782015-07-14 06:40:02 +0000432 free(kbuild_dir);
Wang Nan0c6d18b2015-06-11 11:25:49 +0000433 free(kbuild_include_opts);
Wang Nan4cea3a92015-06-11 10:31:09 +0000434 free(obj_buf);
435 if (p_obj_buf)
436 *p_obj_buf = NULL;
437 if (p_obj_buf_sz)
438 *p_obj_buf_sz = 0;
439 return err;
440}
Wang Nan9bc898c2015-07-08 10:04:02 +0000441
442int llvm__search_clang(void)
443{
444 char clang_path[PATH_MAX];
445
446 return search_program(llvm_param.clang_path, "clang", clang_path);
447}