Skip to content

Commit

Permalink
Makefile: Add libbpf-uapi target
Browse files Browse the repository at this point in the history
Signed-off-by: Tao Chen <[email protected]>
  • Loading branch information
chentao-kernel committed Mar 20, 2024
1 parent 80b1e03 commit 55f5c20
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
21 changes: 11 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ GIT_COMIID := $(shell git rev-parse --short HEAD)
GIT_TAG := $(shell git describe --tags --abbrev=0)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
CLANG_FMT := clang-format-14
ARCH := $(shell uname -m | sed 's/x86_64/x86/g; s/aarch64/arm64/g')
ARCH_GO := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')

RELEASE_VERSION := $(GIT_BRANCH)_$(GIT_COMIID)
RELEASE_TIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
Expand All @@ -28,12 +30,11 @@ EXTRA_CGO_LDFLAGS := -L$(abspath lib/libbpf/lib/lib64) -lbpf \
TARGET ?= spycat
EBPF_SRC := $(PWD)/pkg/ebpf
APP_SRC := $(PWD)/cmd/spycat/main.go
LIBBPF := $(PWD)/lib/libbpf
LIBBCC := $(PWD)/lib/bcc
LIBBPF_SRC := $(PWD)/lib/libbpf
LIBBCC_SRC := $(PWD)/lib/bcc
INCLUDE := -I$(PWD)/pkg/ebpf/headers

CLANG_COMPILE := $(CLANG) $(CFLAGS) $(INCLUDE) -target bpf -D__TARGET_ARCH_x86 $(BPFAPI)

CLANG_COMPILE := $(CLANG) $(CFLAGS) $(INCLUDE) -target bpf -D__TARGET_ARCH_$(ARCH) $(BPFAPI)

.PHONY: all generate

Expand All @@ -42,19 +43,19 @@ generate: export BPF_CFLAGS := $(CFLAGS)
generate:
go generate $(EBPF_SRC)/uprobe/uprobe.go


libbpf:
make -C $(LIBBPF)
make -C $(LIBBCC)
make -C $(LIBBPF_SRC) libbpf
make -C $(LIBBCC_SRC)

ebpf.o: libbpf
$(CLANG_COMPILE) -c $(EBPF_SRC)/cpu/offcpu/offcpu.bpf.c -o $(EBPF_SRC)/cpu/offcpu/offcpu.bpf.o
$(CLANG_COMPILE) -c $(EBPF_SRC)/cpu/oncpu/oncpu.bpf.c -o $(EBPF_SRC)/cpu/oncpu/oncpu.bpf.o

all: generate libbpf ebpf.o
all: generate ebpf.o
@echo "go build $(TARGET)"
CGO_CFLAGS="$(EXTRA_CGO_CFLAGS)" \
CGO_LDFLAGS="$(EXTRA_CGO_LDFLAGS)" \
GOOS=linux GOARCH=$(ARCH_GO) \
go build -ldflags "-linkmode external -extldflags '-static' -X 'main.version=$(RELEASE_VERSION)' \
-X 'main.commitId=$(RELEASE_COMMIT)' -X 'main.releaseTime=$(RELEASE_TIME)' \
-X 'main.goVersion=$(RELEASE_GOVERSION)' -X 'main.author=$(RELEASE_AUTHOR)'" -o $(TARGET) $(APP_SRC)
Expand Down Expand Up @@ -98,5 +99,5 @@ lint-check:

clean:
find . -name "*.o" | xargs rm -f
#make -C $(LIBBPF) clean
#make -C $(LIBBCC) clean
#make -C $(LIBBPF_SRC) clean
#make -C $(LIBBCC_SRC) clean
19 changes: 16 additions & 3 deletions lib/libbpf/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
LIBBPF_VERSION ?= v0.8.1
LIBBPF_SRC = $(CURDIR)/src/src
LIBBPF_DESTDIR = $(CURDIR)/lib/include

.PHONY: libbpf
libbpf:
.PHONY: $(LIBBPF_SRC)
$(LIBBPF_SRC):
mkdir -p $(LIBBPF_DESTDIR)
test -d src || git clone http://github.com/libbpf/libbpf src
cd src && git checkout $(LIBBPF_VERSION)

# libbpf uapi
.PHONY: libbpf-uapi
libbpf-uapi: $(LIBBPF_SRC)
# UAPI headers can be installed by a different package so they're not installed
# in by (libbpf) install rule.
UAPIDIR=$(LIBBPF_DESTDIR) \
$(MAKE) -C $(LIBBPF_SRC) install_uapi_headers

.PHONY: libbpf
libbpf: libbpf-uapi
PREFIX=$(shell pwd)/lib make -C src/src -j16 install
cd src/src && make install

.PHONY: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions pkg/ebpf/cpu/offcpu/offcpu.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#ifndef __OFFCPUTIME_H
#define __OFFCPUTIME_H
#include "vmlinux.h"

#define TASK_COMM_LEN 16

Expand Down
21 changes: 21 additions & 0 deletions pkg/ebpf/cpu/oncpu/oncpu.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
#ifndef __ONCPU_H
#define __ONCPU_H

#define PERF_MAX_STACK_DEPTH 127
#define PROFILE_MAPS_SIZE 16384

typedef signed char __s8;
typedef unsigned char __u8;
typedef short int __s16;
typedef short unsigned int __u16;
typedef int __s32;
typedef unsigned int __u32;
typedef long long int __s64;
typedef long long unsigned int __u64;
typedef __s8 s8;
typedef __u8 u8;
typedef __s16 s16;
typedef __u16 u16;
typedef __s32 s32;
typedef __u32 u32;
typedef __s64 s64;
typedef __u64 u64;

struct profile_key_t {
__u32 pid;
__s64 kern_stack;
Expand Down Expand Up @@ -38,3 +57,5 @@ struct trace_info {
__s64 user_stack;
char comm[16];
};

#endif /* __ONCPU_H */

0 comments on commit 55f5c20

Please sign in to comment.