Skip to content

Commit 1b16fff

Browse files
committed
perf llvm-utils: Add bpf include path to clang command line
We'll start putting headers for helpers to be used in eBPF proggies in there: # perf trace -v --no-syscalls -e empty.c |& grep "llvm compiling command : " llvm compiling command : /usr/lib64/ccache/clang -D__KERNEL__ -D__NR_CPUS__=4 -DLINUX_VERSION_CODE=0x41100 -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include -I/home/acme/git/linux/arch/x86/include -I./arch/x86/include/generated -I/home/acme/git/linux/include -I./include -I/home/acme/git/linux/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -I./include/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h -I/home/acme/lib/include/perf/bpf -Wno-unused-value -Wno-pointer-sign -working-directory /lib/modules/4.17.0-rc3-00034-gf4ef6a438cee/build -c /home/acme/bpf/empty.c -target bpf -O2 -o - # Notice the "-I/home/acme/lib/include/perf/bpf" Cc: Adrian Hunter <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: https://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent d8ed87b commit 1b16fff

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

tools/perf/Makefile.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ endif
885885

886886
# Among the variables below, these:
887887
# perfexecdir
888+
# perf_include_dir
888889
# template_dir
889890
# mandir
890891
# infodir
@@ -904,6 +905,7 @@ bindir = $(abspath $(prefix)/$(bindir_relative))
904905
mandir = share/man
905906
infodir = share/info
906907
perfexecdir = libexec/perf-core
908+
perf_include_dir = lib/include/perf
907909
sharedir = $(prefix)/share
908910
template_dir = share/perf-core/templates
909911
STRACE_GROUPS_DIR = share/perf-core/strace/groups
@@ -934,6 +936,7 @@ bindir_SQ = $(subst ','\'',$(bindir))
934936
mandir_SQ = $(subst ','\'',$(mandir))
935937
infodir_SQ = $(subst ','\'',$(infodir))
936938
perfexecdir_SQ = $(subst ','\'',$(perfexecdir))
939+
perf_include_dir_SQ = $(subst ','\'',$(perf_include_dir))
937940
template_dir_SQ = $(subst ','\'',$(template_dir))
938941
htmldir_SQ = $(subst ','\'',$(htmldir))
939942
tipdir_SQ = $(subst ','\'',$(tipdir))
@@ -944,14 +947,17 @@ srcdir_SQ = $(subst ','\'',$(srcdir))
944947

945948
ifneq ($(filter /%,$(firstword $(perfexecdir))),)
946949
perfexec_instdir = $(perfexecdir)
950+
perf_include_instdir = $(perf_include_dir)
947951
STRACE_GROUPS_INSTDIR = $(STRACE_GROUPS_DIR)
948952
tip_instdir = $(tipdir)
949953
else
950954
perfexec_instdir = $(prefix)/$(perfexecdir)
955+
perf_include_instdir = $(prefix)/$(perf_include_dir)
951956
STRACE_GROUPS_INSTDIR = $(prefix)/$(STRACE_GROUPS_DIR)
952957
tip_instdir = $(prefix)/$(tipdir)
953958
endif
954959
perfexec_instdir_SQ = $(subst ','\'',$(perfexec_instdir))
960+
perf_include_instdir_SQ = $(subst ','\'',$(perf_include_instdir))
955961
STRACE_GROUPS_INSTDIR_SQ = $(subst ','\'',$(STRACE_GROUPS_INSTDIR))
956962
tip_instdir_SQ = $(subst ','\'',$(tip_instdir))
957963

@@ -999,6 +1005,7 @@ $(call detected_var,ETC_PERFCONFIG_SQ)
9991005
$(call detected_var,STRACE_GROUPS_DIR_SQ)
10001006
$(call detected_var,prefix_SQ)
10011007
$(call detected_var,perfexecdir_SQ)
1008+
$(call detected_var,perf_include_dir_SQ)
10021009
$(call detected_var,tipdir_SQ)
10031010
$(call detected_var,srcdir_SQ)
10041011
$(call detected_var,LIBDIR)

tools/perf/Makefile.perf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ ifndef NO_JVMTI
767767
endif
768768
$(call QUIET_INSTALL, libexec) \
769769
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
770+
ifndef NO_LIBBPF
771+
$(call QUIET_INSTALL, lib) \
772+
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perf_include_instdir_SQ)/bpf'
773+
endif
770774
$(call QUIET_INSTALL, perf-archive) \
771775
$(INSTALL) $(OUTPUT)perf-archive -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
772776
$(call QUIET_INSTALL, perf-with-kcore) \

tools/perf/util/Build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ libperf-y += perf-hooks.o
152152
libperf-$(CONFIG_CXX) += c++/
153153

154154
CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
155+
CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
156+
155157
# avoid compiler warnings in 32-bit mode
156158
CFLAGS_genelf_debug.o += -Wno-packed
157159

tools/perf/util/llvm-utils.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
#include "config.h"
1515
#include "util.h"
1616
#include <sys/wait.h>
17+
#include <subcmd/exec-cmd.h>
1718

1819
#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
1920
"$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
2021
"-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
21-
"$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
22+
"$CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS " \
2223
"-Wno-unused-value -Wno-pointer-sign " \
2324
"-working-directory $WORKING_DIR " \
2425
"-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
@@ -212,7 +213,7 @@ version_notice(void)
212213
" \t\thttp://llvm.org/apt\n\n"
213214
" \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
214215
" \toption in [llvm] section of ~/.perfconfig to:\n\n"
215-
" \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
216+
" \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS \\\n"
216217
" \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
217218
" \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
218219
" \t(Replace /path/to/llc with path to your llc)\n\n"
@@ -431,9 +432,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
431432
const char *clang_opt = llvm_param.clang_opt;
432433
char clang_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
433434
char serr[STRERR_BUFSIZE];
434-
char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
435+
char *kbuild_dir = NULL, *kbuild_include_opts = NULL,
436+
*perf_bpf_include_opts = NULL;
435437
const char *template = llvm_param.clang_bpf_cmd_template;
436-
char *command_echo, *command_out;
438+
char *command_echo = NULL, *command_out;
439+
char *perf_include_dir = system_path(PERF_INCLUDE_DIR);
437440

438441
if (path[0] != '-' && realpath(path, abspath) == NULL) {
439442
err = errno;
@@ -471,12 +474,14 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
471474

472475
snprintf(linux_version_code_str, sizeof(linux_version_code_str),
473476
"0x%x", kernel_version);
474-
477+
if (asprintf(&perf_bpf_include_opts, "-I%s/bpf", perf_include_dir) < 0)
478+
goto errout;
475479
force_set_env("NR_CPUS", nr_cpus_avail_str);
476480
force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
477481
force_set_env("CLANG_EXEC", clang_path);
478482
force_set_env("CLANG_OPTIONS", clang_opt);
479483
force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
484+
force_set_env("PERF_BPF_INC_OPTIONS", perf_bpf_include_opts);
480485
force_set_env("WORKING_DIR", kbuild_dir ? : ".");
481486

482487
/*
@@ -512,6 +517,8 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
512517
free(command_out);
513518
free(kbuild_dir);
514519
free(kbuild_include_opts);
520+
free(perf_bpf_include_opts);
521+
free(perf_include_dir);
515522

516523
if (!p_obj_buf)
517524
free(obj_buf);
@@ -526,6 +533,8 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
526533
free(kbuild_dir);
527534
free(kbuild_include_opts);
528535
free(obj_buf);
536+
free(perf_bpf_include_opts);
537+
free(perf_include_dir);
529538
if (p_obj_buf)
530539
*p_obj_buf = NULL;
531540
if (p_obj_buf_sz)

0 commit comments

Comments
 (0)