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