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