Linux Perf
clang-test.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 #include "clang.h"
3 #include "clang-c.h"
4 #include "llvm/IR/Function.h"
5 #include "llvm/IR/LLVMContext.h"
6 
7 #include <util-cxx.h>
8 #include <tests/llvm.h>
9 #include <string>
10 
12 public:
15 };
16 
17 static std::unique_ptr<llvm::Module>
19 {
20  unsigned int kernel_version;
21 
22  if (fetch_kernel_version(&kernel_version, NULL, 0))
23  return std::unique_ptr<llvm::Module>(nullptr);
24 
25  std::string cflag_kver("-DLINUX_VERSION_CODE=" +
26  std::to_string(kernel_version));
27 
28  std::unique_ptr<llvm::Module> M =
29  perf::getModuleFromSource({cflag_kver.c_str()},
30  "perf-test.c",
32  return M;
33 }
34 
35 extern "C" {
37 {
38  perf_clang_scope _scope;
39 
40  auto M = __test__clang_to_IR();
41  if (!M)
42  return -1;
43  for (llvm::Function& F : *M)
44  if (F.getName() == "bpf_func__SyS_epoll_pwait")
45  return 0;
46  return -1;
47 }
48 
50 {
51  perf_clang_scope _scope;
52 
53  auto M = __test__clang_to_IR();
54  if (!M)
55  return -1;
56 
57  auto Buffer = perf::getBPFObjectFromModule(&*M);
58  if (!Buffer)
59  return -1;
60  return 0;
61 }
62 
63 }
int fetch_kernel_version(unsigned int *puint, char *str, size_t str_size)
Definition: util.c:447
int test__clang_to_obj(void)
Definition: clang-test.cpp:49
void perf_clang__cleanup(void)
Definition: clang.cpp:171
#define M(path, c, e)
Definition: kmod-path.c:51
const char test_llvm__bpf_base_prog[]
Definition: llvm-src-base.c:2
static std::unique_ptr< llvm::Module > __test__clang_to_IR(void)
Definition: clang-test.cpp:18
int test__clang_to_IR(void)
Definition: clang-test.cpp:36
static std::unique_ptr< llvm::Module > getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path, IntrusiveRefCntPtr< vfs::FileSystem > VFS)
Definition: clang.cpp:68
std::unique_ptr< llvm::SmallVectorImpl< char > > getBPFObjectFromModule(llvm::Module *Module)
Definition: clang.cpp:124
void perf_clang__init(void)
Definition: clang.cpp:162