Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

BCC and libbpf: build your own eBPF tools

By updated

Improve this page

Two libraries dominate the writing of eBPF tools: BCC, built for fast prototyping, and libbpf, built for shipping a portable binary. Choosing the right one from the start avoids rewriting everything once the tool is in service. This lesson is optional for an administrator: writing and shipping an eBPF tool is a tool developer's gesture, like CO-RE and BTF. If you only want to use the existing BCC tools (execsnoop, opensnoop, biolatency), the dedicated section of the bpftrace lesson is enough. Here, we show the BCC workflow, then the libbpf workflow with its generated skeleton, and give a clear decision rule. The outputs come from an Ubuntu 24.04 VM (kernel 6.8, BCC 0.29.1).

What you will learn

  • Position BCC (prototyping) against libbpf (production)
  • Understand the libbpf workflow: compiled object, skeleton, loader
  • Decide which library to adopt according to your need
  • Find the libbpf-tools, the portable version of the BCC tools
  • Avoid starting over from scratch by choosing wrong at the outset

Prerequisites

  • A Linux VM with the eBPF toolchain (see first program).
  • The bpfcc-tools packages (BCC tools) and libbpf-dev (libbpf library), plus bpftool.
  • Reading C: the excerpts in this lesson are C, and libbpf loading is written in C (or Rust, Go). Without this background, read the lesson for the decision rule and skip the code part.

When should you choose BCC?

BCC (BPF Compiler Collection) is a library that lets you write the eBPF program in C, embedded in a Python (or C++) script that drives its compilation, loading and display. Its asset is immediacy. You write, you run, it works. It is the ideal tool to learn and to answer a one-off question quickly or prototype a tool.

The bpfcc-tools package also ships dozens of ready-made diagnostics, installed under the name <tool>-bpfcc on Debian and Ubuntu: execsnoop, opensnoop, biolatency, tcplife, runqlat. Their usage, with the outputs and the reading, is described in the section "Diagnosing with the ready-made BCC tools" of the bpftrace lesson. This lesson looks at the other use of BCC: writing your own tool.

How does libbpf compile once and ship a binary?

libbpf is the official C library of the kernel project. With it, you write the eBPF program in C, compile it once into an object, and a loader program (in C, Rust, Go...) embeds it. Combined with CO-RE, it produces a portable binary that runs on different kernels without recompilation, as detailed in CO-RE and BTF.

The comfort of libbpf comes from a generator: from the compiled object, bpftool produces a skeleton, a C header that cleanly exposes the maps, the programs and the attachments. We generate the object then its skeleton:

Terminal window
clang -O2 -g -target bpf -c ok.bpf.c -o ok.bpf.o
sudo bpftool gen skeleton ok.bpf.o > ok.skel.h

Real excerpt of the generated skeleton:

/* THIS FILE IS AUTOGENERATED BY BPFTOOL! */
struct ok_bpf {
struct bpf_object_skeleton *skeleton;
struct bpf_object *obj;
struct {
struct bpf_map *counter;
} maps;
struct {
struct bpf_program *deref_with_check;
} progs;
struct {
struct bpf_link *deref_with_check;
} links;

The loader includes this file and handles named fields (obj->maps.counter, obj->progs.deref_with_check) instead of strings and manual lookups. The loading code becomes short and safe. This is the model of modern eBPF tools (Cilium, Tetragon, the bcc/libbpf-tools suite).

bpftrace, BCC or libbpf: which decision rule?

Three tools let you write eBPF, from the simplest to the most complete. The following table sets them apart at a glance, adding bpftrace, which answers a good part of the needs without writing a program.

CriterionbpftraceBCClibbpf + CO-RE
GoalOne-off question, one-linerLearn, prototype a toolShip a production tool
Do you write C?No (dedicated language)Yes (C in Python)Yes (C + loader)
Compilationat each runon each target, at each launchonly once
Dependencies on the targetbpftraceclang + kernel headersnone (just BTF)
Portability across kernelsrecompiles locallyrecompiles locallyportable binary (CO-RE)
When to use itquick investigationlearning, prototypedeployment at scale

The rule fits in one sentence: bpftrace for an immediate answer, BCC to explore and learn, libbpf to ship. You move naturally from one to the other along the life cycle of a tool: an idea tested in bpftrace, prototyped in BCC, then rewritten in libbpf the day it must be deployed everywhere.

Security and points of vigilance

The risks do not change with the chosen library: they are those of any kernel program loading.

  • Loading remains privileged. Whatever the library, you need root: measured in the lab, an account holding CAP_BPF + CAP_PERFMON does not run the BCC tools (see bpftrace). These capabilities remain the path of agents designed for them, not replayed here. Reserve these tools for administrators.
  • Do not embed clang in production for BCC. If a BCC tool must run on many machines, that is the signal to move to libbpf: you avoid installing a compiler on every server.
  • Check that BTF is present for CO-RE. libbpf + CO-RE requires /sys/kernel/btf/vmlinux on the target. On an old kernel that lacks it, plan for an external BTF or recompilation.
  • Pin what must survive. A libbpf tool can pin its maps in /sys/fs/bpf so that they persist; this is data sharing to protect.

Key takeaways

  • BCC writes the program in C inside a Python script, compiles on the target, and suits the prototype; its ready-made tools are in the bpftrace lesson.
  • libbpf compiles only once and, with CO-RE, produces a portable binary with no dependencies on the targets.
  • The skeleton generated by bpftool makes the libbpf loading code short and safe.
  • Rule: BCC to explore, libbpf to ship; you often move from one to the other.
  • The libbpf-tools offer ready-made diagnostics, but in a portable and lightweight version.
  • In every case, loading eBPF remains a privileged operation.

Knowledge check

Check that the essentials of this lesson are in place. The questions only cover what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer