Skip to content

Commit

Permalink
build: gzip+base64 ebpf progs
Browse files Browse the repository at this point in the history
  • Loading branch information
apetruhin committed Nov 26, 2024
1 parent 55183ef commit 5430687
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
24 changes: 12 additions & 12 deletions ebpftracer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.13
FROM alpine:3.14

RUN apk add llvm clang libbpf-dev linux-headers

Expand All @@ -14,20 +14,20 @@ RUN clang -g -O2 -target bpf -D__KERNEL_FROM=420 -D__TARGET_ARCH_arm64 -c ebpf.c
RUN clang -g -O2 -target bpf -D__KERNEL_FROM=506 -D__TARGET_ARCH_arm64 -c ebpf.c -o ebpf506arm64.o && llvm-strip --strip-debug ebpf506arm64.o
RUN clang -g -O2 -target bpf -D__KERNEL_FROM=512 -D__TARGET_ARCH_arm64 -c ebpf.c -o ebpf512arm64.o && llvm-strip --strip-debug ebpf512arm64.o

RUN echo -en '// generated - do not edit\npackage ebpftracer\n\nvar ebpfProg = map[string][]struct {\n' > ebpf.go \
&& echo -en '\tv string\n' >> ebpf.go \
&& echo -en '\tp []byte\n' >> ebpf.go \
RUN echo -en '// generated - do not edit\npackage ebpftracer\n\nvar ebpfProgs = map[string][]struct {\n' > ebpf.go \
&& echo -en '\tversion string\n' >> ebpf.go \
&& echo -en '\tprog []byte\n' >> ebpf.go \
&& echo -en '}{\n' >> ebpf.go \
&& echo -en '\t"amd64": {\n' >> ebpf.go \
&& echo -en '\t\t{"v5.12", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf512x86.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"v5.6", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf506x86.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"v4.20", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf420x86.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"v4.16", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf416x86.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"5.12", []byte("' >> ebpf.go && gzip -c ebpf512x86.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"5.6", []byte("' >> ebpf.go && gzip -c ebpf506x86.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"4.20", []byte("' >> ebpf.go && gzip -c ebpf420x86.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"4.16", []byte("' >> ebpf.go && gzip -c ebpf416x86.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t},\n'>> ebpf.go \
&& echo -en '\t"arm64": {\n' >> ebpf.go \
&& echo -en '\t\t{"v5.12", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf512arm64.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"v5.6", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf506arm64.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"v4.20", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf420arm64.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"v4.16", []byte("' >> ebpf.go && hexdump -v -e '"\x" 1/1 "%02x"' ebpf416arm64.o >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"5.12", []byte("' >> ebpf.go && gzip -c ebpf512arm64.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"5.6", []byte("' >> ebpf.go && gzip -c ebpf506arm64.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"4.20", []byte("' >> ebpf.go && gzip -c ebpf420arm64.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t\t{"4.16", []byte("' >> ebpf.go && gzip -c ebpf416arm64.o | base64 -w0 >> ebpf.go && echo '")},' >> ebpf.go \
&& echo -en '\t},\n'>> ebpf.go \
&& echo -en '}\n'>> ebpf.go
22 changes: 11 additions & 11 deletions ebpftracer/ebpf.go

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions ebpftracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package ebpftracer

import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -188,7 +191,7 @@ type perfMap struct {
}

func (t *Tracer) ebpf(ch chan<- Event) error {
if _, ok := ebpfProg[runtime.GOARCH]; !ok {
if _, ok := ebpfProgs[runtime.GOARCH]; !ok {
return fmt.Errorf("unsupported architecture: %s", runtime.GOARCH)
}
kv := common.GetKernelVersion()
Expand All @@ -206,11 +209,15 @@ func (t *Tracer) ebpf(ch chan<- Event) error {
_, debugFsErr := os.Stat("/sys/kernel/debug/tracing")
_, traceFsErr := os.Stat("/sys/kernel/tracing")

if debugFsErr != nil && traceFsErr != nil {
return fmt.Errorf("kernel tracing is not available: debugfs or tracefs must be mounted")
reader, err := gzip.NewReader(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(prog)))
if err != nil {
return fmt.Errorf("invalid program encoding: %w", err)
}

collectionSpec, err := ebpf.LoadCollectionSpecFromReader(bytes.NewReader(prg))
prog, err = io.ReadAll(reader)
if err != nil {
return fmt.Errorf("failed to ungzip program: %w", err)
}
collectionSpec, err := ebpf.LoadCollectionSpecFromReader(bytes.NewReader(prog))
if err != nil {
return fmt.Errorf("failed to load collection spec: %w", err)
}
Expand Down

0 comments on commit 5430687

Please sign in to comment.