1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2OUTPUT := $(abspath .output) 3CLANG ?= clang 4LLVM_STRIP ?= llvm-strip 5BPFTOOL_SRC := $(abspath ./bpftool/src) 6BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool) 7BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool 8LIBBPF_SRC := $(abspath ../src/cc/libbpf/src) 9LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a) 10LIBBLAZESYM_SRC := $(abspath blazesym/target/release/libblazesym.a) 11INCLUDES := -I$(OUTPUT) -I../src/cc/libbpf/include/uapi 12CFLAGS := -g -O2 -Wall 13BPFCFLAGS := -g -O2 -Wall 14INSTALL ?= install 15prefix ?= /usr/local 16ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' \ 17 | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/' \ 18 | sed 's/riscv64/riscv/' | sed 's/loongarch.*/loongarch/') 19BTFHUB_ARCHIVE ?= $(abspath btfhub-archive) 20ifeq ($(ARCH),x86) 21CARGO ?= $(shell which cargo) 22ifeq ($(strip $(CARGO)),) 23USE_BLAZESYM ?= 0 24else 25USE_BLAZESYM ?= 1 26endif 27endif 28 29ifeq ($(wildcard $(ARCH)/),) 30$(error Architecture $(ARCH) is not supported yet. Please open an issue) 31endif 32 33BZ_APPS = \ 34 memleak \ 35 opensnoop \ 36 # 37 38APPS = \ 39 bashreadline \ 40 bindsnoop \ 41 biolatency \ 42 biopattern \ 43 biosnoop \ 44 biostacks \ 45 biotop \ 46 bitesize \ 47 cachestat \ 48 capable \ 49 cpudist \ 50 cpufreq \ 51 drsnoop \ 52 execsnoop \ 53 exitsnoop \ 54 filelife \ 55 filetop \ 56 fsdist \ 57 fsslower \ 58 funclatency \ 59 gethostlatency \ 60 hardirqs \ 61 javagc \ 62 klockstat \ 63 ksnoop \ 64 llcstat \ 65 mdflush \ 66 mountsnoop \ 67 numamove \ 68 offcputime \ 69 oomkill \ 70 readahead \ 71 runqlat \ 72 runqlen \ 73 runqslower \ 74 sigsnoop \ 75 slabratetop \ 76 softirqs \ 77 solisten \ 78 statsnoop \ 79 syscount \ 80 tcptracer \ 81 tcpconnect \ 82 tcpconnlat \ 83 tcplife \ 84 tcppktlat \ 85 tcprtt \ 86 tcpstates \ 87 tcpsynbl \ 88 tcptop \ 89 vfsstat \ 90 wakeuptime \ 91 $(BZ_APPS) \ 92 # 93 94# export variables that are used in Makefile.btfgen as well. 95export OUTPUT BPFTOOL ARCH BTFHUB_ARCHIVE APPS 96 97FSDIST_ALIASES = btrfsdist ext4dist nfsdist xfsdist 98FSSLOWER_ALIASES = btrfsslower ext4slower nfsslower xfsslower 99SIGSNOOP_ALIAS = killsnoop 100APP_ALIASES = $(FSDIST_ALIASES) $(FSSLOWER_ALIASES) ${SIGSNOOP_ALIAS} 101 102COMMON_OBJ = \ 103 $(OUTPUT)/trace_helpers.o \ 104 $(OUTPUT)/syscall_helpers.o \ 105 $(OUTPUT)/errno_helpers.o \ 106 $(OUTPUT)/map_helpers.o \ 107 $(OUTPUT)/uprobe_helpers.o \ 108 $(OUTPUT)/btf_helpers.o \ 109 $(OUTPUT)/compat.o \ 110 $(if $(ENABLE_MIN_CORE_BTFS),$(OUTPUT)/min_core_btf_tar.o) \ 111 # 112 113ifeq ($(USE_BLAZESYM),1) 114COMMON_OBJ += \ 115 $(OUTPUT)/libblazesym.a \ 116 $(OUTPUT)/blazesym.h \ 117 # 118endif 119 120define allow-override 121 $(if $(or $(findstring environment,$(origin $(1))),\ 122 $(findstring command line,$(origin $(1)))),,\ 123 $(eval $(1) = $(2))) 124endef 125 126$(call allow-override,CC,$(CROSS_COMPILE)cc) 127$(call allow-override,LD,$(CROSS_COMPILE)ld) 128 129.PHONY: all 130all: $(APPS) $(APP_ALIASES) 131 132ifeq ($(V),1) 133Q = 134msg = 135else 136Q = @ 137msg = @printf ' %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))"; 138MAKEFLAGS += --no-print-directory 139endif 140 141ifneq ($(EXTRA_CFLAGS),) 142CFLAGS += $(EXTRA_CFLAGS) 143endif 144ifneq ($(EXTRA_LDFLAGS),) 145LDFLAGS += $(EXTRA_LDFLAGS) 146endif 147ifeq ($(USE_BLAZESYM),1) 148CFLAGS += -DUSE_BLAZESYM=1 149endif 150 151ifeq ($(USE_BLAZESYM),1) 152LDFLAGS += $(OUTPUT)/libblazesym.a -lrt -lpthread -ldl 153endif 154 155.PHONY: clean 156clean: 157 $(call msg,CLEAN) 158 $(Q)rm -rf $(OUTPUT) $(APPS) $(APP_ALIASES) 159 160$(LIBBLAZESYM_SRC):: 161 $(Q)cd blazesym && cargo build --release --features=cheader 162 163$(OUTPUT)/libblazesym.a: $(LIBBLAZESYM_SRC) | $(OUTPUT) 164 $(call msg,LIB,$@) 165 $(Q)cp $(LIBBLAZESYM_SRC) $@ 166 167$(OUTPUT)/blazesym.h: $(LIBBLAZESYM_SRC) | $(OUTPUT) 168 $(call msg,INC,$@) 169 $(Q)cp blazesym/target/release/blazesym.h $@ 170 171$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT): 172 $(call msg,MKDIR,$@) 173 $(Q)mkdir -p $@ 174 175$(BPFTOOL): | $(BPFTOOL_OUTPUT) 176 $(call msg,BPFTOOL,$@) 177 $(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap 178 179$(APPS): %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT) 180 $(call msg,BINARY,$@) 181 $(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) -lelf -lz -o $@ 182 183ifeq ($(USE_BLAZESYM),1) 184$(patsubst %,$(OUTPUT)/%.o,$(BZ_APPS)): $(OUTPUT)/blazesym.h 185endif 186 187$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h 188 189$(OUTPUT)/%.o: %.c $(wildcard %.h) $(LIBBPF_OBJ) | $(OUTPUT) 190 $(call msg,CC,$@) 191 $(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@ 192 193$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL) 194 $(call msg,GEN-SKEL,$@) 195 $(Q)$(BPFTOOL) gen skeleton $< > $@ 196 197$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(ARCH)/vmlinux.h | $(OUTPUT) 198 $(call msg,BPF,$@) 199 $(Q)$(CLANG) $(BPFCFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) \ 200 -I$(ARCH)/ $(INCLUDES) -c $(filter %.c,$^) -o $@ && \ 201 $(LLVM_STRIP) -g $@ 202 203btfhub-archive: force 204 $(call msg,GIT,$@) 205 $(Q)[ -d "$(BTFHUB_ARCHIVE)" ] || git clone -q https://github.com/aquasecurity/btfhub-archive/ $(BTFHUB_ARCHIVE) 206 $(Q)cd $(BTFHUB_ARCHIVE) && git pull 207 208ifdef ENABLE_MIN_CORE_BTFS 209$(OUTPUT)/min_core_btf_tar.o: $(patsubst %,$(OUTPUT)/%.bpf.o,$(APPS)) btfhub-archive | bpftool 210 $(Q)$(MAKE) -f Makefile.btfgen 211endif 212 213# Build libbpf.a 214$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch]) | $(OUTPUT)/libbpf 215 $(call msg,LIB,$@) 216 $(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1 \ 217 OBJDIR=$(dir $@)libbpf DESTDIR=$(dir $@) \ 218 INCLUDEDIR= LIBDIR= UAPIDIR= \ 219 install 220 221$(FSSLOWER_ALIASES): fsslower 222 $(call msg,SYMLINK,$@) 223 $(Q)ln -f -s $^ $@ 224 225$(FSDIST_ALIASES): fsdist 226 $(call msg,SYMLINK,$@) 227 $(Q)ln -f -s $^ $@ 228 229$(SIGSNOOP_ALIAS): sigsnoop 230 $(call msg,SYMLINK,$@) 231 $(Q)ln -f -s $^ $@ 232 233install: $(APPS) $(APP_ALIASES) 234 $(call msg, INSTALL libbpf-tools) 235 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/bin 236 $(Q)$(INSTALL) $(APPS) $(DESTDIR)$(prefix)/bin 237 $(Q)cp -a $(APP_ALIASES) $(DESTDIR)$(prefix)/bin 238 239.PHONY: force 240force: 241 242# delete failed targets 243.DELETE_ON_ERROR: 244# keep intermediate (.skel.h, .bpf.o, etc) targets 245.SECONDARY: 246