llvm.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. #include <bpf/libbpf.h>
  3. #include <util/llvm-utils.h>
  4. #include <util/cache.h>
  5. #include "tests.h"
  6. #include "debug.h"
  7. static int perf_config_cb(const char *var, const char *val,
  8. void *arg __maybe_unused)
  9. {
  10. return perf_default_config(var, val, arg);
  11. }
  12. /*
  13. * Randomly give it a "version" section since we don't really load it
  14. * into kernel
  15. */
  16. static const char test_bpf_prog[] =
  17. "__attribute__((section(\"do_fork\"), used)) "
  18. "int fork(void *ctx) {return 0;} "
  19. "char _license[] __attribute__((section(\"license\"), used)) = \"GPL\";"
  20. "int _version __attribute__((section(\"version\"), used)) = 0x40100;";
  21. #ifdef HAVE_LIBBPF_SUPPORT
  22. static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
  23. {
  24. struct bpf_object *obj;
  25. obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
  26. if (!obj)
  27. return -1;
  28. bpf_object__close(obj);
  29. return 0;
  30. }
  31. #else
  32. static int test__bpf_parsing(void *obj_buf __maybe_unused,
  33. size_t obj_buf_sz __maybe_unused)
  34. {
  35. fprintf(stderr, " (skip bpf parsing)");
  36. return 0;
  37. }
  38. #endif
  39. int test__llvm(void)
  40. {
  41. char *tmpl_new, *clang_opt_new;
  42. void *obj_buf;
  43. size_t obj_buf_sz;
  44. int err, old_verbose;
  45. perf_config(perf_config_cb, NULL);
  46. /*
  47. * Skip this test if user's .perfconfig doesn't set [llvm] section
  48. * and clang is not found in $PATH, and this is not perf test -v
  49. */
  50. if (verbose == 0 && !llvm_param.user_set_param && llvm__search_clang()) {
  51. fprintf(stderr, " (no clang, try 'perf test -v LLVM')");
  52. return TEST_SKIP;
  53. }
  54. old_verbose = verbose;
  55. /*
  56. * llvm is verbosity when error. Suppress all error output if
  57. * not 'perf test -v'.
  58. */
  59. if (verbose == 0)
  60. verbose = -1;
  61. if (!llvm_param.clang_bpf_cmd_template)
  62. return -1;
  63. if (!llvm_param.clang_opt)
  64. llvm_param.clang_opt = strdup("");
  65. err = asprintf(&tmpl_new, "echo '%s' | %s", test_bpf_prog,
  66. llvm_param.clang_bpf_cmd_template);
  67. if (err < 0)
  68. return -1;
  69. err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
  70. if (err < 0)
  71. return -1;
  72. llvm_param.clang_bpf_cmd_template = tmpl_new;
  73. llvm_param.clang_opt = clang_opt_new;
  74. err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz);
  75. verbose = old_verbose;
  76. if (err) {
  77. if (!verbose)
  78. fprintf(stderr, " (use -v to see error message)");
  79. return -1;
  80. }
  81. err = test__bpf_parsing(obj_buf, obj_buf_sz);
  82. free(obj_buf);
  83. return err;
  84. }