xref: /aosp_15_r20/external/bpftool/README.md (revision 858ea5e570667251cdc31d3fe7b846b591105938)
1*858ea5e5SAndroid Build Coastguard Worker<a href=".github/assets/">
2*858ea5e5SAndroid Build Coastguard Worker  <div>
3*858ea5e5SAndroid Build Coastguard Worker    <img src=".github/assets/bpftool_horizontal_color.svg"
4*858ea5e5SAndroid Build Coastguard Worker         alt="bpftool logo: Hannah the Honeyguide"
5*858ea5e5SAndroid Build Coastguard Worker         width=500px; />
6*858ea5e5SAndroid Build Coastguard Worker  </div>
7*858ea5e5SAndroid Build Coastguard Worker</a>
8*858ea5e5SAndroid Build Coastguard Worker
9*858ea5e5SAndroid Build Coastguard Worker[![License BSD 2-Clause][bsd-badge]](LICENSE.BSD-2-Clause)
10*858ea5e5SAndroid Build Coastguard Worker[![License GPL 2.0][gpl-badge]](LICENSE.GPL-2.0)
11*858ea5e5SAndroid Build Coastguard Worker[![Build status][build-badge]][build]
12*858ea5e5SAndroid Build Coastguard Worker
13*858ea5e5SAndroid Build Coastguard Worker[bsd-badge]: https://img.shields.io/badge/License-BSD_2--Clause-blue.svg
14*858ea5e5SAndroid Build Coastguard Worker[gpl-badge]: https://img.shields.io/badge/License-GPL_2.0-blue.svg
15*858ea5e5SAndroid Build Coastguard Worker[build-badge]: https://github.com/libbpf/bpftool/actions/workflows/build.yaml/badge.svg
16*858ea5e5SAndroid Build Coastguard Worker[build]: https://github.com/libbpf/bpftool/actions/workflows/build.yaml
17*858ea5e5SAndroid Build Coastguard Worker
18*858ea5e5SAndroid Build Coastguard WorkerThis is the official home for bpftool. _Please use this Github repository for
19*858ea5e5SAndroid Build Coastguard Workerbuilding and packaging bpftool and when using it in your projects through Git
20*858ea5e5SAndroid Build Coastguard Workersubmodule._
21*858ea5e5SAndroid Build Coastguard Worker
22*858ea5e5SAndroid Build Coastguard WorkerThe _authoritative source code_ of bpftool is developed as part of the
23*858ea5e5SAndroid Build Coastguard Worker[bpf-next Linux source tree][bpf-next] under [the `tools/bpf/bpftool`
24*858ea5e5SAndroid Build Coastguard Workersubdirectory][tools-bpf-bpftool] and is periodically synced to
25*858ea5e5SAndroid Build Coastguard Worker<https://github.com/libbpf/bpftool> on Github. As such, all changes for bpftool
26*858ea5e5SAndroid Build Coastguard Worker**should be sent to the [BPF mailing list][bpf-ml]**, **please don't open PRs
27*858ea5e5SAndroid Build Coastguard Workerhere** unless you are changing some Github-specific components.
28*858ea5e5SAndroid Build Coastguard Worker
29*858ea5e5SAndroid Build Coastguard Worker[bpf-next]: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git
30*858ea5e5SAndroid Build Coastguard Worker[tools-bpf-bpftool]: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/bpf/bpftool
31*858ea5e5SAndroid Build Coastguard Worker[bpf-ml]: http://vger.kernel.org/vger-lists.html#bpf
32*858ea5e5SAndroid Build Coastguard Worker
33*858ea5e5SAndroid Build Coastguard WorkerQuestions on bpftool and BPF general usage
34*858ea5e5SAndroid Build Coastguard Worker------------------------------------------
35*858ea5e5SAndroid Build Coastguard Worker
36*858ea5e5SAndroid Build Coastguard WorkerCheck out [the manual pages](docs) for documentation about bpftool. A number of
37*858ea5e5SAndroid Build Coastguard Workerexample invocations are also displayed in [this blog
38*858ea5e5SAndroid Build Coastguard Workerpost](https://qmonnet.github.io/whirl-offload/2021/09/23/bpftool-features-thread/).
39*858ea5e5SAndroid Build Coastguard Worker
40*858ea5e5SAndroid Build Coastguard WorkerAll general BPF questions, including kernel functionality, bpftool features and
41*858ea5e5SAndroid Build Coastguard Workerusage, should be sent to [email protected] mailing list. You can subscribe to
42*858ea5e5SAndroid Build Coastguard Workerit [here][bpf-ml] and search its archives [here][lore].
43*858ea5e5SAndroid Build Coastguard Worker
44*858ea5e5SAndroid Build Coastguard WorkerThe mailing list is monitored by many more people than this repo and they will
45*858ea5e5SAndroid Build Coastguard Workerhappily try to help you with whatever issue you encounter. This repository's
46*858ea5e5SAndroid Build Coastguard WorkerPRs and issues should be opened only for dealing with issues related to
47*858ea5e5SAndroid Build Coastguard Workercomponents specific to the bpftool mirror repository (such as the
48*858ea5e5SAndroid Build Coastguard Workersynchronization script or the CI workflows). The project maintainers also use
49*858ea5e5SAndroid Build Coastguard WorkerGitHub issues as a generic tracker for bpftool, but issues should first be
50*858ea5e5SAndroid Build Coastguard Workerreported on the mailing list nonetheless.
51*858ea5e5SAndroid Build Coastguard Worker
52*858ea5e5SAndroid Build Coastguard Worker[lore]: https://lore.kernel.org/bpf/
53*858ea5e5SAndroid Build Coastguard Worker
54*858ea5e5SAndroid Build Coastguard WorkerDependencies
55*858ea5e5SAndroid Build Coastguard Worker------------
56*858ea5e5SAndroid Build Coastguard Worker
57*858ea5e5SAndroid Build Coastguard WorkerRequired:
58*858ea5e5SAndroid Build Coastguard Worker
59*858ea5e5SAndroid Build Coastguard Worker- libelf
60*858ea5e5SAndroid Build Coastguard Worker- zlib
61*858ea5e5SAndroid Build Coastguard Worker
62*858ea5e5SAndroid Build Coastguard WorkerOptional:
63*858ea5e5SAndroid Build Coastguard Worker
64*858ea5e5SAndroid Build Coastguard Worker- libbfd (for dumping JIT-compiled program instructions)
65*858ea5e5SAndroid Build Coastguard Worker- libcap (for better feature probing)
66*858ea5e5SAndroid Build Coastguard Worker- kernel BTF information (for profiling programs or showing PIDs of processes
67*858ea5e5SAndroid Build Coastguard Worker  referencing BPF objects)
68*858ea5e5SAndroid Build Coastguard Worker- clang/LLVM (idem)
69*858ea5e5SAndroid Build Coastguard Worker
70*858ea5e5SAndroid Build Coastguard WorkerBuild
71*858ea5e5SAndroid Build Coastguard Worker-----
72*858ea5e5SAndroid Build Coastguard Worker
73*858ea5e5SAndroid Build Coastguard Worker### Initialize libbpf submodule
74*858ea5e5SAndroid Build Coastguard Worker
75*858ea5e5SAndroid Build Coastguard WorkerThis repository uses libbpf as a submodule. You can initialize it when cloning
76*858ea5e5SAndroid Build Coastguard Workerbpftool:
77*858ea5e5SAndroid Build Coastguard Worker
78*858ea5e5SAndroid Build Coastguard Worker```console
79*858ea5e5SAndroid Build Coastguard Worker$ git clone --recurse-submodules https://github.com/libbpf/bpftool.git
80*858ea5e5SAndroid Build Coastguard Worker```
81*858ea5e5SAndroid Build Coastguard Worker
82*858ea5e5SAndroid Build Coastguard WorkerAlternatively, if you have already cloned the repository, you can initialize
83*858ea5e5SAndroid Build Coastguard Workerthe submodule by running the following command from within the repository:
84*858ea5e5SAndroid Build Coastguard Worker
85*858ea5e5SAndroid Build Coastguard Worker```console
86*858ea5e5SAndroid Build Coastguard Worker$ git submodule update --init
87*858ea5e5SAndroid Build Coastguard Worker```
88*858ea5e5SAndroid Build Coastguard Worker
89*858ea5e5SAndroid Build Coastguard Worker### Build bpftool
90*858ea5e5SAndroid Build Coastguard Worker
91*858ea5e5SAndroid Build Coastguard WorkerTo build bpftool:
92*858ea5e5SAndroid Build Coastguard Worker
93*858ea5e5SAndroid Build Coastguard Worker```console
94*858ea5e5SAndroid Build Coastguard Worker$ cd src
95*858ea5e5SAndroid Build Coastguard Worker$ make
96*858ea5e5SAndroid Build Coastguard Worker```
97*858ea5e5SAndroid Build Coastguard Worker
98*858ea5e5SAndroid Build Coastguard WorkerTo build and install bpftool on the system:
99*858ea5e5SAndroid Build Coastguard Worker
100*858ea5e5SAndroid Build Coastguard Worker```console
101*858ea5e5SAndroid Build Coastguard Worker$ cd src
102*858ea5e5SAndroid Build Coastguard Worker# make install
103*858ea5e5SAndroid Build Coastguard Worker```
104*858ea5e5SAndroid Build Coastguard Worker
105*858ea5e5SAndroid Build Coastguard WorkerBuilding bpftool in a separate directory is supported via the `OUTPUT` variable:
106*858ea5e5SAndroid Build Coastguard Worker
107*858ea5e5SAndroid Build Coastguard Worker```console
108*858ea5e5SAndroid Build Coastguard Worker$ mkdir /tmp/bpftool
109*858ea5e5SAndroid Build Coastguard Worker$ cd src
110*858ea5e5SAndroid Build Coastguard Worker$ OUTPUT=/tmp/bpftool make
111*858ea5e5SAndroid Build Coastguard Worker```
112*858ea5e5SAndroid Build Coastguard Worker
113*858ea5e5SAndroid Build Coastguard WorkerMost of the output is suppressed by default, but detailed building logs can be
114*858ea5e5SAndroid Build Coastguard Workerdisplayed by passing `V=1`:
115*858ea5e5SAndroid Build Coastguard Worker
116*858ea5e5SAndroid Build Coastguard Worker```console
117*858ea5e5SAndroid Build Coastguard Worker$ cd src
118*858ea5e5SAndroid Build Coastguard Worker$ make V=1
119*858ea5e5SAndroid Build Coastguard Worker```
120*858ea5e5SAndroid Build Coastguard Worker
121*858ea5e5SAndroid Build Coastguard WorkerAdditional compilation flags can be passed to the command line if required. For
122*858ea5e5SAndroid Build Coastguard Workerexample, we can create a static build with the following commands:
123*858ea5e5SAndroid Build Coastguard Worker
124*858ea5e5SAndroid Build Coastguard Worker```console
125*858ea5e5SAndroid Build Coastguard Worker$ cd src
126*858ea5e5SAndroid Build Coastguard Worker$ EXTRA_CFLAGS=--static make
127*858ea5e5SAndroid Build Coastguard Worker```
128*858ea5e5SAndroid Build Coastguard Worker
129*858ea5e5SAndroid Build Coastguard WorkerNote that to use the LLVM disassembler with static builds, we need a static
130*858ea5e5SAndroid Build Coastguard Workerversion of the LLVM library installed on the system:
131*858ea5e5SAndroid Build Coastguard Worker
132*858ea5e5SAndroid Build Coastguard Worker1.  Download a precompiled LLVM release or build it locally.
133*858ea5e5SAndroid Build Coastguard Worker
134*858ea5e5SAndroid Build Coastguard Worker    - Download the appropriate
135*858ea5e5SAndroid Build Coastguard Worker      [release of LLVM](https://releases.llvm.org/download.html) for your
136*858ea5e5SAndroid Build Coastguard Worker      platform, for example on x86_64 with LLVM 15.0.0:
137*858ea5e5SAndroid Build Coastguard Worker
138*858ea5e5SAndroid Build Coastguard Worker      ```console
139*858ea5e5SAndroid Build Coastguard Worker      $ curl -LO https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.0/clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4.tar.xz
140*858ea5e5SAndroid Build Coastguard Worker      $ tar xvf clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4.tar.xz
141*858ea5e5SAndroid Build Coastguard Worker      $ mv clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4 llvm_build
142*858ea5e5SAndroid Build Coastguard Worker      ```
143*858ea5e5SAndroid Build Coastguard Worker
144*858ea5e5SAndroid Build Coastguard Worker    - Alternatively, clone and build the LLVM libraries locally.
145*858ea5e5SAndroid Build Coastguard Worker
146*858ea5e5SAndroid Build Coastguard Worker      ```console
147*858ea5e5SAndroid Build Coastguard Worker      $ git clone https://github.com/llvm/llvm-project.git
148*858ea5e5SAndroid Build Coastguard Worker      $ mkdir llvm_build
149*858ea5e5SAndroid Build Coastguard Worker      $ cmake -S llvm-project/llvm -B llvm_build -DCMAKE_BUILD_TYPE=Release
150*858ea5e5SAndroid Build Coastguard Worker      $ make -j -C llvm_build llvm-config llvm-libraries
151*858ea5e5SAndroid Build Coastguard Worker      ```
152*858ea5e5SAndroid Build Coastguard Worker
153*858ea5e5SAndroid Build Coastguard Worker2.  Build bpftool with `EXTRA_CFLAGS` set to `--static`, and by passing the
154*858ea5e5SAndroid Build Coastguard Worker    path to the relevant `llvm-config`.
155*858ea5e5SAndroid Build Coastguard Worker
156*858ea5e5SAndroid Build Coastguard Worker    ```console
157*858ea5e5SAndroid Build Coastguard Worker    $ cd bpftool
158*858ea5e5SAndroid Build Coastguard Worker    $ LLVM_CONFIG=../../llvm_build/bin/llvm-config EXTRA_CFLAGS=--static make -j -C src
159*858ea5e5SAndroid Build Coastguard Worker    ```
160*858ea5e5SAndroid Build Coastguard Worker
161*858ea5e5SAndroid Build Coastguard Worker### Build bpftool's man pages
162*858ea5e5SAndroid Build Coastguard Worker
163*858ea5e5SAndroid Build Coastguard WorkerThe man pages for bpftool can be built with:
164*858ea5e5SAndroid Build Coastguard Worker
165*858ea5e5SAndroid Build Coastguard Worker```console
166*858ea5e5SAndroid Build Coastguard Worker$ cd docs
167*858ea5e5SAndroid Build Coastguard Worker$ make
168*858ea5e5SAndroid Build Coastguard Worker```
169*858ea5e5SAndroid Build Coastguard Worker
170*858ea5e5SAndroid Build Coastguard WorkerThey can be installed on the system with:
171*858ea5e5SAndroid Build Coastguard Worker
172*858ea5e5SAndroid Build Coastguard Worker```console
173*858ea5e5SAndroid Build Coastguard Worker$ cd docs
174*858ea5e5SAndroid Build Coastguard Worker# make install
175*858ea5e5SAndroid Build Coastguard Worker```
176*858ea5e5SAndroid Build Coastguard Worker
177*858ea5e5SAndroid Build Coastguard Workerbpf-next to GitHub sync
178*858ea5e5SAndroid Build Coastguard Worker-----------------------
179*858ea5e5SAndroid Build Coastguard Worker
180*858ea5e5SAndroid Build Coastguard WorkerThis repository mirrors [bpf-next Linux source tree's
181*858ea5e5SAndroid Build Coastguard Worker`tools/bpf/bpftool`][tools-bpf-bpftool] directory, plus its few dependencies
182*858ea5e5SAndroid Build Coastguard Workerfrom under `kernel/bpf/`, and its supporting header files. Some of these header
183*858ea5e5SAndroid Build Coastguard Workerfiles, `include/linux/*.h` on the current repository, are reduced versions of
184*858ea5e5SAndroid Build Coastguard Workertheir counterpart files at [bpf-next][bpf-next]'s `tools/include/linux/*.h` to
185*858ea5e5SAndroid Build Coastguard Workermake compilation successful.
186*858ea5e5SAndroid Build Coastguard Worker
187*858ea5e5SAndroid Build Coastguard WorkerSynchronization between the two repositories happens every few weeks or so.
188*858ea5e5SAndroid Build Coastguard WorkerGiven that bpftool remains aligned on libbpf's version, its repository tends to
189*858ea5e5SAndroid Build Coastguard Workerfollow libbpf's. When the libbpf repo syncs up with bpf-next, bpftool's repo
190*858ea5e5SAndroid Build Coastguard Workerusually follows within the next few days.
191*858ea5e5SAndroid Build Coastguard Worker
192*858ea5e5SAndroid Build Coastguard WorkerThe synchronization process is semi-automated: the script in
193*858ea5e5SAndroid Build Coastguard Worker`scripts/sync-kernel.sh` cherry-picks, adjusts and commits all changes from
194*858ea5e5SAndroid Build Coastguard Worker`bpf-next` to a local version of the bpftool repository. However, maintainers
195*858ea5e5SAndroid Build Coastguard Workerrun this script manually and then create a pull request to merge the resulting
196*858ea5e5SAndroid Build Coastguard Workercommits.
197*858ea5e5SAndroid Build Coastguard Worker
198*858ea5e5SAndroid Build Coastguard WorkerTake a look at [the script](scripts/sync-kernel.sh) for the technical details of the process. See also the documentation in [the accompanying README.md](scripts#sync-kernelsh)
199*858ea5e5SAndroid Build Coastguard Worker
200*858ea5e5SAndroid Build Coastguard WorkerLicense
201*858ea5e5SAndroid Build Coastguard Worker-------
202*858ea5e5SAndroid Build Coastguard Worker
203*858ea5e5SAndroid Build Coastguard WorkerThis work is dual-licensed under the GNU GPL v2.0 (only) license and the BSD
204*858ea5e5SAndroid Build Coastguard Worker2-clause license. You can select either of them if you reuse this work.
205*858ea5e5SAndroid Build Coastguard Worker
206*858ea5e5SAndroid Build Coastguard Worker`SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)`
207