llvm-utils.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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__ $CLANG_OPTIONS " \
  12. "$KERNEL_INC_OPTIONS -Wno-unused-value " \
  13. "-Wno-pointer-sign -working-directory " \
  14. "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
  15. struct llvm_param llvm_param = {
  16. .clang_path = "clang",
  17. .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
  18. .clang_opt = NULL,
  19. .kbuild_dir = NULL,
  20. .kbuild_opts = NULL,
  21. };
  22. int perf_llvm_config(const char *var, const char *value)
  23. {
  24. if (prefixcmp(var, "llvm."))
  25. return 0;
  26. var += sizeof("llvm.") - 1;
  27. if (!strcmp(var, "clang-path"))
  28. llvm_param.clang_path = strdup(value);
  29. else if (!strcmp(var, "clang-bpf-cmd-template"))
  30. llvm_param.clang_bpf_cmd_template = strdup(value);
  31. else if (!strcmp(var, "clang-opt"))
  32. llvm_param.clang_opt = strdup(value);
  33. else if (!strcmp(var, "kbuild-dir"))
  34. llvm_param.kbuild_dir = strdup(value);
  35. else if (!strcmp(var, "kbuild-opts"))
  36. llvm_param.kbuild_opts = strdup(value);
  37. else
  38. return -1;
  39. return 0;
  40. }
  41. static int
  42. search_program(const char *def, const char *name,
  43. char *output)
  44. {
  45. char *env, *path, *tmp = NULL;
  46. char buf[PATH_MAX];
  47. int ret;
  48. output[0] = '\0';
  49. if (def && def[0] != '\0') {
  50. if (def[0] == '/') {
  51. if (access(def, F_OK) == 0) {
  52. strlcpy(output, def, PATH_MAX);
  53. return 0;
  54. }
  55. } else if (def[0] != '\0')
  56. name = def;
  57. }
  58. env = getenv("PATH");
  59. if (!env)
  60. return -1;
  61. env = strdup(env);
  62. if (!env)
  63. return -1;
  64. ret = -ENOENT;
  65. path = strtok_r(env, ":", &tmp);
  66. while (path) {
  67. scnprintf(buf, sizeof(buf), "%s/%s", path, name);
  68. if (access(buf, F_OK) == 0) {
  69. strlcpy(output, buf, PATH_MAX);
  70. ret = 0;
  71. break;
  72. }
  73. path = strtok_r(NULL, ":", &tmp);
  74. }
  75. free(env);
  76. return ret;
  77. }
  78. #define READ_SIZE 4096
  79. static int
  80. read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
  81. {
  82. int err = 0;
  83. void *buf = NULL;
  84. FILE *file = NULL;
  85. size_t read_sz = 0, buf_sz = 0;
  86. file = popen(cmd, "r");
  87. if (!file) {
  88. pr_err("ERROR: unable to popen cmd: %s\n",
  89. strerror(errno));
  90. return -EINVAL;
  91. }
  92. while (!feof(file) && !ferror(file)) {
  93. /*
  94. * Make buf_sz always have obe byte extra space so we
  95. * can put '\0' there.
  96. */
  97. if (buf_sz - read_sz < READ_SIZE + 1) {
  98. void *new_buf;
  99. buf_sz = read_sz + READ_SIZE + 1;
  100. new_buf = realloc(buf, buf_sz);
  101. if (!new_buf) {
  102. pr_err("ERROR: failed to realloc memory\n");
  103. err = -ENOMEM;
  104. goto errout;
  105. }
  106. buf = new_buf;
  107. }
  108. read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
  109. }
  110. if (buf_sz - read_sz < 1) {
  111. pr_err("ERROR: internal error\n");
  112. err = -EINVAL;
  113. goto errout;
  114. }
  115. if (ferror(file)) {
  116. pr_err("ERROR: error occurred when reading from pipe: %s\n",
  117. strerror(errno));
  118. err = -EIO;
  119. goto errout;
  120. }
  121. err = WEXITSTATUS(pclose(file));
  122. file = NULL;
  123. if (err) {
  124. err = -EINVAL;
  125. goto errout;
  126. }
  127. /*
  128. * If buf is string, give it terminal '\0' to make our life
  129. * easier. If buf is not string, that '\0' is out of space
  130. * indicated by read_sz so caller won't even notice it.
  131. */
  132. ((char *)buf)[read_sz] = '\0';
  133. if (!p_buf)
  134. free(buf);
  135. else
  136. *p_buf = buf;
  137. if (p_read_sz)
  138. *p_read_sz = read_sz;
  139. return 0;
  140. errout:
  141. if (file)
  142. pclose(file);
  143. free(buf);
  144. if (p_buf)
  145. *p_buf = NULL;
  146. if (p_read_sz)
  147. *p_read_sz = 0;
  148. return err;
  149. }
  150. static inline void
  151. force_set_env(const char *var, const char *value)
  152. {
  153. if (value) {
  154. setenv(var, value, 1);
  155. pr_debug("set env: %s=%s\n", var, value);
  156. } else {
  157. unsetenv(var);
  158. pr_debug("unset env: %s\n", var);
  159. }
  160. }
  161. static void
  162. version_notice(void)
  163. {
  164. pr_err(
  165. " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
  166. " \tYou may want to try git trunk:\n"
  167. " \t\tgit clone http://llvm.org/git/llvm.git\n"
  168. " \t\t and\n"
  169. " \t\tgit clone http://llvm.org/git/clang.git\n\n"
  170. " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
  171. " \tdebian/ubuntu:\n"
  172. " \t\thttp://llvm.org/apt\n\n"
  173. " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
  174. " \toption in [llvm] section of ~/.perfconfig to:\n\n"
  175. " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
  176. " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
  177. " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
  178. " \t(Replace /path/to/llc with path to your llc)\n\n"
  179. );
  180. }
  181. int llvm__compile_bpf(const char *path, void **p_obj_buf,
  182. size_t *p_obj_buf_sz)
  183. {
  184. int err;
  185. char clang_path[PATH_MAX];
  186. const char *clang_opt = llvm_param.clang_opt;
  187. const char *template = llvm_param.clang_bpf_cmd_template;
  188. void *obj_buf = NULL;
  189. size_t obj_buf_sz;
  190. if (!template)
  191. template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
  192. err = search_program(llvm_param.clang_path,
  193. "clang", clang_path);
  194. if (err) {
  195. pr_err(
  196. "ERROR:\tunable to find clang.\n"
  197. "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
  198. " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
  199. version_notice();
  200. return -ENOENT;
  201. }
  202. force_set_env("CLANG_EXEC", clang_path);
  203. force_set_env("CLANG_OPTIONS", clang_opt);
  204. force_set_env("KERNEL_INC_OPTIONS", NULL);
  205. force_set_env("WORKING_DIR", ".");
  206. /*
  207. * Since we may reset clang's working dir, path of source file
  208. * should be transferred into absolute path, except we want
  209. * stdin to be source file (testing).
  210. */
  211. force_set_env("CLANG_SOURCE",
  212. (path[0] == '-') ? path :
  213. make_nonrelative_path(path));
  214. pr_debug("llvm compiling command template: %s\n", template);
  215. err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
  216. if (err) {
  217. pr_err("ERROR:\tunable to compile %s\n", path);
  218. pr_err("Hint:\tCheck error message shown above.\n");
  219. pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
  220. pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
  221. pr_err(" \twith proper -I and -D options.\n");
  222. goto errout;
  223. }
  224. if (!p_obj_buf)
  225. free(obj_buf);
  226. else
  227. *p_obj_buf = obj_buf;
  228. if (p_obj_buf_sz)
  229. *p_obj_buf_sz = obj_buf_sz;
  230. return 0;
  231. errout:
  232. free(obj_buf);
  233. if (p_obj_buf)
  234. *p_obj_buf = NULL;
  235. if (p_obj_buf_sz)
  236. *p_obj_buf_sz = 0;
  237. return err;
  238. }