Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
411c0ec
Documentation
LICENSES
arch
block
certs
crypto
drivers
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
accounting
arch
bpf
build
cgroup
debugging
firewire
firmware
gpio
hv
iio
include
io_uring
kvm
laptop
leds
lib
memory-model
nfsd
objtool
pci
pcmcia
perf
Documentation
arch
bench
examples
include
jvmti
pmu-events
python
scripts
tests
trace
ui
util
c++
Build
clang-c.h
clang-test.cpp
clang.cpp
clang.h
cs-etm-decoder
include
intel-pt-decoder
libunwind
scripting-engines
Build
PERF-VERSION-GEN
affinity.c
affinity.h
annotate.c
annotate.h
archinsn.h
arm-spe-pkt-decoder.c
arm-spe-pkt-decoder.h
arm-spe.c
arm-spe.h
auxtrace.c
auxtrace.h
block-info.c
block-info.h
block-range.c
block-range.h
bpf-event.c
bpf-event.h
bpf-loader.c
bpf-loader.h
bpf-prologue.c
bpf-prologue.h
bpf_map.c
bpf_map.h
branch.c
branch.h
build-id.c
build-id.h
cache.h
cacheline.c
cacheline.h
call-path.c
call-path.h
callchain.c
callchain.h
cap.c
cap.h
cgroup.c
cgroup.h
cloexec.c
cloexec.h
color.c
color.h
color_config.c
comm.c
comm.h
compress.h
config.c
config.h
copyfile.c
copyfile.h
counts.c
counts.h
cpu-set-sched.h
cpumap.c
cpumap.h
cputopo.c
cputopo.h
cs-etm.c
cs-etm.h
data-convert-bt.c
data-convert-bt.h
data-convert.h
data.c
data.h
db-export.c
db-export.h
debug.c
debug.h
demangle-java.c
demangle-java.h
demangle-rust.c
demangle-rust.h
dso.c
dso.h
dsos.c
dsos.h
dump-insn.c
dump-insn.h
dwarf-aux.c
dwarf-aux.h
dwarf-regs.c
env.c
env.h
event.c
event.h
events_stats.h
evlist.c
evlist.h
evsel.c
evsel.h
evsel_config.h
evsel_fprintf.c
evsel_fprintf.h
evswitch.c
evswitch.h
expr.h
expr.y
find-map.c
fncache.c
fncache.h
genelf.c
genelf.h
genelf_debug.c
generate-cmdlist.sh
get_current_dir_name.c
get_current_dir_name.h
group.h
header.c
header.h
help-unknown-cmd.c
help-unknown-cmd.h
hist.c
hist.h
intel-bts.c
intel-bts.h
intel-pt.c
intel-pt.h
intlist.c
intlist.h
jit.h
jitdump.c
jitdump.h
kvm-stat.h
levenshtein.c
levenshtein.h
llvm-utils.c
llvm-utils.h
lzma.c
machine.c
machine.h
map.c
map.h
map_symbol.h
maps.h
mem-events.c
mem-events.h
mem2node.c
mem2node.h
memswap.c
memswap.h
metricgroup.c
metricgroup.h
mmap.c
mmap.h
namespaces.c
namespaces.h
ordered-events.c
ordered-events.h
parse-branch-options.c
parse-branch-options.h
parse-events.c
parse-events.h
parse-events.l
parse-events.y
parse-regs-options.c
parse-regs-options.h
path.c
path.h
perf-hooks-list.h
perf-hooks.c
perf-hooks.h
perf_event_attr_fprintf.c
perf_regs.c
perf_regs.h
pmu.c
pmu.h
pmu.l
pmu.y
print_binary.c
print_binary.h
probe-event.c
probe-event.h
probe-file.c
probe-file.h
probe-finder.c
probe-finder.h
pstack.c
pstack.h
python-ext-sources
python.c
rb_resort.h
rblist.c
rblist.h
record.c
record.h
rlimit.c
rlimit.h
rwsem.c
rwsem.h
s390-cpumcf-kernel.h
s390-cpumsf-kernel.h
s390-cpumsf.c
s390-cpumsf.h
s390-sample-raw.c
sample-raw.c
sample-raw.h
session.c
session.h
setns.c
setup.py
smt.c
smt.h
sort.c
sort.h
spark.c
spark.h
srccode.c
srccode.h
srcline.c
srcline.h
stat-display.c
stat-shadow.c
stat.c
stat.h
strbuf.c
strbuf.h
strfilter.c
strfilter.h
string.c
string2.h
strlist.c
strlist.h
svghelper.c
svghelper.h
symbol-elf.c
symbol-minimal.c
symbol.c
symbol.h
symbol_conf.h
symbol_fprintf.c
symsrc.h
synthetic-events.c
synthetic-events.h
syscalltbl.c
syscalltbl.h
target.c
target.h
term.c
term.h
thread-stack.c
thread-stack.h
thread.c
thread.h
thread_map.c
thread_map.h
time-utils.c
time-utils.h
tool.h
top.c
top.h
trace-event-info.c
trace-event-parse.c
trace-event-read.c
trace-event-scripting.c
trace-event.c
trace-event.h
trigger.h
tsc.c
tsc.h
units.c
units.h
unwind-libdw.c
unwind-libdw.h
unwind-libunwind-local.c
unwind-libunwind.c
unwind.h
usage.c
util.c
util.h
values.c
values.h
vdso.c
vdso.h
xyarray.c
zlib.c
zstd.c
.gitignore
Build
CREDITS
MANIFEST
Makefile
Makefile.config
Makefile.perf
builtin-annotate.c
builtin-bench.c
builtin-buildid-cache.c
builtin-buildid-list.c
builtin-c2c.c
builtin-config.c
builtin-data.c
builtin-diff.c
builtin-evlist.c
builtin-ftrace.c
builtin-help.c
builtin-inject.c
builtin-kallsyms.c
builtin-kmem.c
builtin-kvm.c
builtin-list.c
builtin-lock.c
builtin-mem.c
builtin-probe.c
builtin-record.c
builtin-report.c
builtin-sched.c
builtin-script.c
builtin-stat.c
builtin-timechart.c
builtin-top.c
builtin-trace.c
builtin-version.c
builtin.h
check-headers.sh
command-list.txt
design.txt
perf-archive.sh
perf-completion.sh
perf-read-vdso.c
perf-sys.h
perf-with-kcore.sh
perf.c
perf.h
power
scripts
spi
testing
thermal
time
usb
virtio
vm
wmi
Makefile
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
tools
/
perf
/
util
/
c++
/
clang.cpp
Blame
Blame
Latest commit
History
History
216 lines (183 loc) · 5.45 KB
Breadcrumbs
linux
/
tools
/
perf
/
util
/
c++
/
clang.cpp
Top
File metadata and controls
Code
Blame
216 lines (183 loc) · 5.45 KB
Raw
// SPDX-License-Identifier: GPL-2.0 /* * llvm C frontend for perf. Support dynamically compile C file * * Inspired by clang example code: * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp * * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com> * Copyright (C) 2016 Huawei Inc. */ #include "clang/Basic/Version.h" #include "clang/CodeGen/CodeGenAction.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Tooling/Tooling.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/Option/Option.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" #include <memory> #include "clang.h" #include "clang-c.h" namespace perf { static std::unique_ptr<llvm::LLVMContext> LLVMCtx; using namespace clang; static CompilerInvocation * createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path, DiagnosticsEngine& Diags) { llvm::opt::ArgStringList CCArgs { "-cc1", "-triple", "bpf-pc-linux", "-fsyntax-only", "-ferror-limit", "19", "-fmessage-length", "127", "-O2", "-nostdsysteminc", "-nobuiltininc", "-vectorize-loops", "-vectorize-slp", "-Wno-unused-value", "-Wno-pointer-sign", "-x", "c"}; CCArgs.append(CFlags.begin(), CFlags.end()); CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs); FrontendOptions& Opts = CI->getFrontendOpts(); Opts.Inputs.clear(); Opts.Inputs.emplace_back(Path, FrontendOptions::getInputKindForExtension("c")); return CI; } static std::unique_ptr<llvm::Module> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS) { CompilerInstance Clang; Clang.createDiagnostics(); #if CLANG_VERSION_MAJOR < 9 Clang.setVirtualFileSystem(&*VFS); #else Clang.createFileManager(&*VFS); #endif #if CLANG_VERSION_MAJOR < 4 IntrusiveRefCntPtr<CompilerInvocation> CI = createCompilerInvocation(std::move(CFlags), Path, Clang.getDiagnostics()); Clang.setInvocation(&*CI); #else std::shared_ptr<CompilerInvocation> CI( createCompilerInvocation(std::move(CFlags), Path, Clang.getDiagnostics())); Clang.setInvocation(CI); #endif std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx)); if (!Clang.ExecuteAction(*Act)) return std::unique_ptr<llvm::Module>(nullptr); return Act->takeModule(); } std::unique_ptr<llvm::Module> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Name, StringRef Content) { using namespace vfs; llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS( new OverlayFileSystem(getRealFileSystem())); llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS( new InMemoryFileSystem(true)); /* * pushOverlay helps setting working dir for MemFS. Must call * before addFile. */ OverlayFS->pushOverlay(MemFS); MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); return getModuleFromSource(std::move(CFlags), Name, OverlayFS); } std::unique_ptr<llvm::Module> getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path) { IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem()); return getModuleFromSource(std::move(CFlags), Path, VFS); } std::unique_ptr<llvm::SmallVectorImpl<char>> getBPFObjectFromModule(llvm::Module *Module) { using namespace llvm; std::string TargetTriple("bpf-pc-linux"); std::string Error; const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error); if (!Target) { llvm::errs() << Error; return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr); } llvm::TargetOptions Opt; TargetMachine *TargetMachine = Target->createTargetMachine(TargetTriple, "generic", "", Opt, Reloc::Static); Module->setDataLayout(TargetMachine->createDataLayout()); Module->setTargetTriple(TargetTriple); std::unique_ptr<SmallVectorImpl<char>> Buffer(new SmallVector<char, 0>()); raw_svector_ostream ostream(*Buffer); legacy::PassManager PM; bool NotAdded; #if CLANG_VERSION_MAJOR < 7 NotAdded = TargetMachine->addPassesToEmitFile(PM, ostream, TargetMachine::CGFT_ObjectFile); #else NotAdded = TargetMachine->addPassesToEmitFile(PM, ostream, nullptr, TargetMachine::CGFT_ObjectFile); #endif if (NotAdded) { llvm::errs() << "TargetMachine can't emit a file of this type\n"; return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr); } PM.run(*Module); return Buffer; } } extern "C" { void perf_clang__init(void) { perf::LLVMCtx.reset(new llvm::LLVMContext()); LLVMInitializeBPFTargetInfo(); LLVMInitializeBPFTarget(); LLVMInitializeBPFTargetMC(); LLVMInitializeBPFAsmPrinter(); } void perf_clang__cleanup(void) { perf::LLVMCtx.reset(nullptr); llvm::llvm_shutdown(); } int perf_clang__compile_bpf(const char *filename, void **p_obj_buf, size_t *p_obj_buf_sz) { using namespace perf; if (!p_obj_buf || !p_obj_buf_sz) return -EINVAL; llvm::opt::ArgStringList CFlags; auto M = getModuleFromSource(std::move(CFlags), filename); if (!M) return -EINVAL; auto O = getBPFObjectFromModule(&*M); if (!O) return -EINVAL; size_t size = O->size_in_bytes(); void *buffer; buffer = malloc(size); if (!buffer) return -ENOMEM; memcpy(buffer, O->data(), size); *p_obj_buf = buffer; *p_obj_buf_sz = size; return 0; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
You can’t perform that action at this time.