llvm-utils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
  3. * Copyright (C) 2015, Huawei Inc.
  4. */
  5. #include <stdio.h>
  6. #include "util.h"
  7. #include "debug.h"
  8. #include "llvm-utils.h"
  9. #include "cache.h"
  10. #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
  11. "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
  12. "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
  13. "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
  14. "-Wno-unused-value -Wno-pointer-sign " \
  15. "-working-directory $WORKING_DIR " \
  16. "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
  17. struct llvm_param llvm_param = {
  18. .clang_path = "clang",
  19. .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
  20. .clang_opt = NULL,
  21. .kbuild_dir = NULL,
  22. .kbuild_opts = NULL,
  23. .user_set_param = false,
  24. };
  25. int perf_llvm_config(const char *var, const char *value)
  26. {
  27. if (prefixcmp(var, "llvm."))
  28. return 0;
  29. var += sizeof("llvm.") - 1;
  30. if (!strcmp(var, "clang-path"))
  31. llvm_param.clang_path = strdup(value);
  32. else if (!strcmp(var, "clang-bpf-cmd-template"))
  33. llvm_param.clang_bpf_cmd_template = strdup(value);
  34. else if (!strcmp(var, "clang-opt"))
  35. llvm_param.clang_opt = strdup(value);
  36. else if (!strcmp(var, "kbuild-dir"))
  37. llvm_param.kbuild_dir = strdup(value);
  38. else if (!strcmp(var, "kbuild-opts"))
  39. llvm_param.kbuild_opts = strdup(value);
  40. else
  41. return -1;
  42. llvm_param.user_set_param = true;
  43. return 0;
  44. }
  45. static int
  46. search_program(const char *def, const char *name,
  47. char *output)
  48. {
  49. char *env, *path, *tmp = NULL;
  50. char buf[PATH_MAX];
  51. int ret;
  52. output[0] = '\0';
  53. if (def && def[0] != '\0') {
  54. if (def[0] == '/') {
  55. if (access(def, F_OK) == 0) {
  56. strlcpy(output, def, PATH_MAX);
  57. return 0;
  58. }
  59. } else if (def[0] != '\0')
  60. name = def;
  61. }
  62. env = getenv("PATH");
  63. if (!env)
  64. return -1;
  65. env = strdup(env);
  66. if (!env)
  67. return -1;
  68. ret = -ENOENT;
  69. path = strtok_r(env, ":", &tmp);
  70. while (path) {
  71. scnprintf(buf, sizeof(buf), "%s/%s", path, name);
  72. if (access(buf, F_OK) == 0) {
  73. strlcpy(output, buf, PATH_MAX);
  74. ret = 0;
  75. break;
  76. }
  77. path = strtok_r(NULL, ":", &tmp);
  78. }
  79. free(env);
  80. return ret;
  81. }
  82. #define READ_SIZE 4096
  83. static int
  84. read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
  85. {
  86. int err = 0;
  87. void *buf = NULL;
  88. FILE *file = NULL;
  89. size_t read_sz = 0, buf_sz = 0;
  90. file = popen(cmd, "r");
  91. if (!file) {
  92. pr_err("ERROR: unable to popen cmd: %s\n",
  93. strerror(errno));
  94. return -EINVAL;
  95. }
  96. while (!feof(file) && !ferror(file)) {
  97. /*
  98. * Make buf_sz always have obe byte extra space so we
  99. * can put '\0' there.
  100. */
  101. if (buf_sz - read_sz < READ_SIZE + 1) {
  102. void *new_buf;
  103. buf_sz = read_sz + READ_SIZE + 1;
  104. new_buf = realloc(buf, buf_sz);
  105. if (!new_buf) {
  106. pr_err("ERROR: failed to realloc memory\n");
  107. err = -ENOMEM;
  108. goto errout;
  109. }
  110. buf = new_buf;
  111. }
  112. read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
  113. }
  114. if (buf_sz - read_sz < 1) {
  115. pr_err("ERROR: internal error\n");
  116. err = -EINVAL;
  117. goto errout;
  118. }
  119. if (ferror(file)) {
  120. pr_err("ERROR: error occurred when reading from pipe: %s\n",
  121. strerror(errno));
  122. err = -EIO;
  123. goto errout;
  124. }
  125. err = WEXITSTATUS(pclose(file));
  126. file = NULL;
  127. if (err) {
  128. err = -EINVAL;
  129. goto errout;
  130. }
  131. /*
  132. * If buf is string, give it terminal '\0' to make our life
  133. * easier. If buf is not string, that '\0' is out of space
  134. * indicated by read_sz so caller won't even notice it.
  135. */
  136. ((char *)buf)[read_sz] = '\0';
  137. if (!p_buf)
  138. free(buf);
  139. else
  140. *p_buf = buf;
  141. if (p_read_sz)
  142. *p_read_sz = read_sz;
  143. return 0;
  144. errout:
  145. if (file)
  146. pclose(file);
  147. free(buf);
  148. if (p_buf)
  149. *p_buf = NULL;
  150. if (p_read_sz)
  151. *p_read_sz = 0;
  152. return err;
  153. }
  154. static inline void
  155. force_set_env(const char *var, const char *value)
  156. {
  157. if (value) {
  158. setenv(var, value, 1);
  159. pr_debug("set env: %s=%s\n", var, value);
  160. } else {
  161. unsetenv(var);
  162. pr_debug("unset env: %s\n", var);
  163. }
  164. }
  165. static void
  166. version_notice(void)
  167. {
  168. pr_err(
  169. " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
  170. " \tYou may want to try git trunk:\n"
  171. " \t\tgit clone http://llvm.org/git/llvm.git\n"
  172. " \t\t and\n"
  173. " \t\tgit clone http://llvm.org/git/clang.git\n\n"
  174. " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
  175. " \tdebian/ubuntu:\n"
  176. " \t\thttp://llvm.org/apt\n\n"
  177. " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
  178. " \toption in [llvm] section of ~/.perfconfig to:\n\n"
  179. " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
  180. " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
  181. " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
  182. " \t(Replace /path/to/llc with path to your llc)\n\n"
  183. );
  184. }
  185. static int detect_kbuild_dir(char **kbuild_dir)
  186. {
  187. const char *test_dir = llvm_param.kbuild_dir;
  188. const char *prefix_dir = "";
  189. const char *suffix_dir = "";
  190. char *autoconf_path;
  191. int err;
  192. if (!test_dir) {
  193. /* _UTSNAME_LENGTH is 65 */
  194. char release[128];
  195. err = fetch_kernel_version(NULL, release,
  196. sizeof(release));
  197. if (err)
  198. return -EINVAL;
  199. test_dir = release;
  200. prefix_dir = "/lib/modules/";
  201. suffix_dir = "/build";
  202. }
  203. err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
  204. prefix_dir, test_dir, suffix_dir);
  205. if (err < 0)
  206. return -ENOMEM;
  207. if (access(autoconf_path, R_OK) == 0) {
  208. free(autoconf_path);
  209. err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
  210. suffix_dir);
  211. if (err < 0)
  212. return -ENOMEM;
  213. return 0;
  214. }
  215. free(autoconf_path);
  216. return -ENOENT;
  217. }
  218. static const char *kinc_fetch_script =
  219. "#!/usr/bin/env sh\n"
  220. "if ! test -d \"$KBUILD_DIR\"\n"
  221. "then\n"
  222. " exit 1\n"
  223. "fi\n"
  224. "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
  225. "then\n"
  226. " exit 1\n"
  227. "fi\n"
  228. "TMPDIR=`mktemp -d`\n"
  229. "if test -z \"$TMPDIR\"\n"
  230. "then\n"
  231. " exit 1\n"
  232. "fi\n"
  233. "cat << EOF > $TMPDIR/Makefile\n"
  234. "obj-y := dummy.o\n"
  235. "\\$(obj)/%.o: \\$(src)/%.c\n"
  236. "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
  237. "EOF\n"
  238. "touch $TMPDIR/dummy.c\n"
  239. "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
  240. "RET=$?\n"
  241. "rm -rf $TMPDIR\n"
  242. "exit $RET\n";
  243. static inline void
  244. get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
  245. {
  246. int err;
  247. if (!kbuild_dir || !kbuild_include_opts)
  248. return;
  249. *kbuild_dir = NULL;
  250. *kbuild_include_opts = NULL;
  251. if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
  252. pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
  253. pr_debug("Skip kbuild options detection.\n");
  254. return;
  255. }
  256. err = detect_kbuild_dir(kbuild_dir);
  257. if (err) {
  258. pr_warning(
  259. "WARNING:\tunable to get correct kernel building directory.\n"
  260. "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
  261. " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
  262. " \tdetection.\n\n");
  263. return;
  264. }
  265. pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
  266. force_set_env("KBUILD_DIR", *kbuild_dir);
  267. force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
  268. err = read_from_pipe(kinc_fetch_script,
  269. (void **)kbuild_include_opts,
  270. NULL);
  271. if (err) {
  272. pr_warning(
  273. "WARNING:\tunable to get kernel include directories from '%s'\n"
  274. "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
  275. " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
  276. " \toption in [llvm] to \"\" to suppress this detection.\n\n",
  277. *kbuild_dir);
  278. free(*kbuild_dir);
  279. *kbuild_dir = NULL;
  280. return;
  281. }
  282. pr_debug("include option is set to %s\n", *kbuild_include_opts);
  283. }
  284. int llvm__compile_bpf(const char *path, void **p_obj_buf,
  285. size_t *p_obj_buf_sz)
  286. {
  287. size_t obj_buf_sz;
  288. void *obj_buf = NULL;
  289. int err, nr_cpus_avail;
  290. unsigned int kernel_version;
  291. char linux_version_code_str[64];
  292. const char *clang_opt = llvm_param.clang_opt;
  293. char clang_path[PATH_MAX], nr_cpus_avail_str[64];
  294. char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
  295. const char *template = llvm_param.clang_bpf_cmd_template;
  296. if (!template)
  297. template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
  298. err = search_program(llvm_param.clang_path,
  299. "clang", clang_path);
  300. if (err) {
  301. pr_err(
  302. "ERROR:\tunable to find clang.\n"
  303. "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
  304. " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
  305. version_notice();
  306. return -ENOENT;
  307. }
  308. /*
  309. * This is an optional work. Even it fail we can continue our
  310. * work. Needn't to check error return.
  311. */
  312. get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
  313. nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
  314. if (nr_cpus_avail <= 0) {
  315. pr_err(
  316. "WARNING:\tunable to get available CPUs in this system: %s\n"
  317. " \tUse 128 instead.\n", strerror(errno));
  318. nr_cpus_avail = 128;
  319. }
  320. snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
  321. nr_cpus_avail);
  322. if (fetch_kernel_version(&kernel_version, NULL, 0))
  323. kernel_version = 0;
  324. snprintf(linux_version_code_str, sizeof(linux_version_code_str),
  325. "0x%x", kernel_version);
  326. force_set_env("NR_CPUS", nr_cpus_avail_str);
  327. force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
  328. force_set_env("CLANG_EXEC", clang_path);
  329. force_set_env("CLANG_OPTIONS", clang_opt);
  330. force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
  331. force_set_env("WORKING_DIR", kbuild_dir ? : ".");
  332. /*
  333. * Since we may reset clang's working dir, path of source file
  334. * should be transferred into absolute path, except we want
  335. * stdin to be source file (testing).
  336. */
  337. force_set_env("CLANG_SOURCE",
  338. (path[0] == '-') ? path :
  339. make_nonrelative_path(path));
  340. pr_debug("llvm compiling command template: %s\n", template);
  341. err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
  342. if (err) {
  343. pr_err("ERROR:\tunable to compile %s\n", path);
  344. pr_err("Hint:\tCheck error message shown above.\n");
  345. pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
  346. pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
  347. pr_err(" \twith proper -I and -D options.\n");
  348. goto errout;
  349. }
  350. free(kbuild_dir);
  351. free(kbuild_include_opts);
  352. if (!p_obj_buf)
  353. free(obj_buf);
  354. else
  355. *p_obj_buf = obj_buf;
  356. if (p_obj_buf_sz)
  357. *p_obj_buf_sz = obj_buf_sz;
  358. return 0;
  359. errout:
  360. free(kbuild_dir);
  361. free(kbuild_include_opts);
  362. free(obj_buf);
  363. if (p_obj_buf)
  364. *p_obj_buf = NULL;
  365. if (p_obj_buf_sz)
  366. *p_obj_buf_sz = 0;
  367. return err;
  368. }
  369. int llvm__search_clang(void)
  370. {
  371. char clang_path[PATH_MAX];
  372. return search_program(llvm_param.clang_path, "clang", clang_path);
  373. }