diff --git a/Makefile b/Makefile index b6d890e..eac7b2e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ -.PHONY: all clean build ebpf go-build vmlinux build-libbpf test +.PHONY: all clean build ebpf ebpf-debug go-build vmlinux build-libbpf test debug + +EBPF_CFLAGS ?= # Default target all: vmlinux ebpf go-build @@ -15,7 +17,14 @@ vmlinux: # eBPF compilation ebpf: vmlinux - clang -g -O2 -D__TARGET_ARCH_x86 -I./bpf -I./dest/libbpf/usr/include -idirafter /usr/include/x86_64-linux-gnu -c bpf/php.bpf.c -target bpf -o bpf/php.bpf.o + clang -g -O2 -D__TARGET_ARCH_x86 $(EBPF_CFLAGS) -I./bpf -I./dest/libbpf/usr/include -idirafter /usr/include/x86_64-linux-gnu -c bpf/php.bpf.c -target bpf -o bpf/php.bpf.o + +# eBPF compilation with debug (enables bpf_trace_printk) +ebpf-debug: + $(MAKE) ebpf EBPF_CFLAGS=-DDEBUG + +# Debug build (eBPF with -DDEBUG + go binary) +debug: ebpf-debug go-build # Go build go-build: diff --git a/bpf/php.bpf.c b/bpf/php.bpf.c index fb888f8..9f05ab0 100644 --- a/bpf/php.bpf.c +++ b/bpf/php.bpf.c @@ -4,6 +4,16 @@ #define MAX_STR_LEN 512 +#ifdef DEBUG +#define debug_printk(fmt, ...) \ + ({ \ + static const char ____fmt[] = fmt; \ + bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \ + }) +#else +#define debug_printk(fmt, ...) ((void)0) +#endif + char filename[MAX_STR_LEN]; @@ -19,8 +29,7 @@ int BPF_USDT(compile_file_return, char *arg0, char *arg1) { u64 ts = bpf_ktime_get_ns(); - static const char fmtstr[] = "compile file return: %s, %s\n"; - bpf_trace_printk(fmtstr, sizeof(fmtstr), arg0, arg1); + debug_printk("compile file return: %s, %s\n", arg0, arg1); bpf_probe_read_user_str(&filename, sizeof(filename), arg0);