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