Skip to content

Add libbpf-bootstrap-android to directly support amd64 cross-compile to Android aarch64 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
45 changes: 45 additions & 0 deletions .github/docker/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
ARG VERSION="22.04"
FROM ubuntu:${VERSION}

ARG LLVM_VERSION="14"
ENV LLVM_VERSION=$LLVM_VERSION

ARG SHORTNAME="jammy"
ENV SHORTNAME=$SHORTNAME

RUN apt-get update && apt-get install -y curl gnupg
RUN if [ "${LLVM_VERSION}" = "17" ]; \
then \
echo "\n\
deb http://apt.llvm.org/${SHORTNAME}/ llvm-toolchain-${SHORTNAME} main\n\
deb-src http://apt.llvm.org/${SHORTNAME}/ llvm-toolchain-${SHORTNAME} main\n\
" >> /etc/apt/sources.list;\
else \
echo "\n\
deb http://apt.llvm.org/${SHORTNAME}/ llvm-toolchain-${SHORTNAME}-${LLVM_VERSION} main\n\
deb-src http://apt.llvm.org/${SHORTNAME}/ llvm-toolchain-${SHORTNAME}-${LLVM_VERSION} main\n\
" >> /etc/apt/sources.list; \
fi
RUN curl -L https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -

ARG DEBIAN_FRONTEND="noninteractive"
ENV TZ="Etc/UTC"

RUN apt-get update && apt-get install -y \
libelf-dev \
zlib1g-dev \
libbfd-dev \
clang-${LLVM_VERSION} \
libclang-${LLVM_VERSION}-dev \
libclang-common-${LLVM_VERSION}-dev \
libclang1-${LLVM_VERSION} \
llvm-${LLVM_VERSION} \
llvm-${LLVM_VERSION}-dev \
llvm-${LLVM_VERSION}-runtime \
libllvm${LLVM_VERSION} \
make pkg-config \
rustc cargo rustfmt \
sudo \
&& apt-get -y clean

RUN ln -s /usr/bin/clang-${LLVM_VERSION} /usr/bin/clang && ln -s /usr/bin/llvm-strip-${LLVM_VERSION} /usr/bin/llvm-strip
55 changes: 55 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: libbpf-bootstrap build

on:
push:
branches:
- master
pull_request:

jobs:
build_libbpf_bootstrap:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
llvm: [11, 12, 13, 14, 15, 16, 17]
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build container (20.04)
if: ${{ matrix.llvm < 14 }}
uses: docker/build-push-action@v3
with:
push: false
build-args: |
LLVM_VERSION=${{ matrix.llvm }}
VERSION=20.04
SHORTNAME=focal
file: ./.github/docker/Dockerfile.ubuntu
tags: build_container
- name: Build container (22.04)
if: ${{ matrix.llvm >= 14 }}
uses: docker/build-push-action@v3
with:
push: false
build-args: |
LLVM_VERSION=${{ matrix.llvm }}
VERSION=22.04
SHORTNAME=jammy
file: ./.github/docker/Dockerfile.ubuntu
tags: build_container
- name: Build examples/c
run: |
docker run \
-v $(pwd):/libbpf-bootstrap \
build_container \
/bin/bash -c \
'cd /libbpf-bootstrap/examples/c && make -j`nproc`'
- name: Build examples/rust
run: |
docker run \
-v $(pwd):/libbpf-bootstrap \
build_container \
/bin/bash -c \
'cd /libbpf-bootstrap/examples/rust && cargo build'
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/build
/examples/c/build
/examples/c/.xmake
/examples/.vscode
/.idea
/.clang-format
/.vscode

9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "libbpf"]
path = libbpf
url = https://github.com/libbpf/libbpf.git
[submodule "bpftool"]
path = bpftool
url = https://github.com/libbpf/bpftool
[submodule "blazesym"]
path = blazesym
url = https://github.com/libbpf/blazesym.git
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2020, Andrii Nakryiko
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# libbpf-bootstrap-android
# libbpf-bootstrap-android
---
layout: post
title: libbpf-bootstrap-android
date: 2023-9-14 16:18
category: Framework
author: JiaHuann
tags: [Android, ebpf-framework, cross-compile]
summary: For amd64 aarch machine to directly using libbpf-bootstrap framwork to write ebpf programs both kernel and user space.And you don't need to have `CONFIG_BTF_DEBUG_INFO` support by using externel BTF file in BTFhubForAndroid instead.For this goal,I did some changes on libbpf-bootstrap,and have done some embedded features.Wish this can help you sth.Best regards.
---

# Libbpf-bootstrap for android
> For amd64 aarch machine to directly using libbpf-bootstrap framwork to write ebpf programs both kernel and user space.And you don't need to have `CONFIG_BTF_DEBUG_INFO` support by using externel BTF file in BTFhubForAndroid instead.For this goal,I did some changes on libbpf-bootstrap,and have done some embedded features.Wish this can help you sth.Best regards.
## upstream:
- https://github.com/libbpf/libbpf-bootstrap
- https://github.com/SeeFlowerX/BTFHubForAndroid

## prerare
- Android NDK
- gcc-aarch64-linux-gnu
- Android phone with root and unlock BL

## usage
- deps
```shell
https://github.com/JiaHuann/libbpf-bootstrap-android
cd libbpf-bootstrap-android
sudo apt install gcc-aarch64-linux-gnu llvm clang
wget https://dl.google.com/android/repository/android-ndk-r25c-linux.zip?hl=zh-cn android-ndk-r25c-linux.zip
unzip android-ndk-r25c-linux
```
- set NDK env to `$PATH`
```shell
vim ~/.bashrc
export your android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin to PATH
```

- compile and push to phone
```
cd examples/c && make

adb devices
adb remount # may fail, you can leave with issure.
adb push examples/c/btf_file/* example/c/your_executable_file /data/local/tmp
adb shell #into android kernel shell
```
- in android shell
```shell
cd /data/local/tmp && chmod +x your_excutable_file
./run
Congratulations!
```


# feature
- libelf,libz on aarcj64 embedded.
- x86 -> android13(aarch64) cross-compile you can directly write ebpf both kernel-program and user-program on x86 machine.
- FlameGraph embedded to monitor performance.
- For None BTF/vmlinux android-kernel(CONFIG_DEBUG_INFO_BTF not set) support.
- adb debug (after compiling&linking, directly push into android-phone) embedded.

# RoadMap:
- change the static link to ShareLib.
- Measure the eBPF program's overhead by FlameGraph.
- Add more Android features into support.
- Test for lower Android version.
2 changes: 2 additions & 0 deletions blazesym/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.x86_64-unknown-linux-gnu]
runner = "sudo --preserve-env"
1 change: 1 addition & 0 deletions blazesym/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data/vmlinux-5.17.12-100.fc34.x86_64.xz filter=lfs diff=lfs merge=lfs -text
9 changes: 9 additions & 0 deletions blazesym/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/data/*.bin
/data/*.gsym
/data/*.so
/data/*.zip
/data/kallsyms
/data/vmlinux-5.17.12-100.fc34.x86_64*
/data/zip-dir
/target
Cargo.lock
3 changes: 3 additions & 0 deletions blazesym/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
blank_lines_upper_bound = 2
imports_granularity = "item"
trailing_semicolon = false
25 changes: 25 additions & 0 deletions blazesym/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
0.2.0-alpha.2
-------------
- Added `extern "C"` guards in `blazesym.h` header for easy of use from C++ code
- Added unused member variable to `blaze_user_addr_meta_unknown` type for
compliance with C standard, stating undefined behavior for empty structs
- Changed `blaze_inspect_elf_src::path` type to `*const _`
- Fixed incorrect `NULL` checks when working with `blaze_symbolize_src_kernel`
objects
- Switched away from using Git LFS for large benchmark files towards
on-demand downloading from a different repository, controlled by
`generate-bench-files` feature


0.2.0-alpha.1
-------------
- Removed no longer necessary `base_address` member from various types
- Renamed `SymInfo::address` member to `addr`
- Fixed incorrect allocation size calculation in C API
- Fixed file offset lookup potentially reporting subtly wrong offsets on
certain ELF file segment constellations


0.2.0-alpha.0
-------------
- Initial documented release
79 changes: 79 additions & 0 deletions blazesym/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[package]
name = "blazesym"
description = "blazesym is a library that can be used for address symbolization and more."
version = "0.2.0-alpha.2"
edition = "2021"
rust-version = "1.63"
authors = ["Daniel Müller <[email protected]>", "Kui-Feng <[email protected]>"]
license-file = "LICENSE"
repository = "https://github.com/libbpf/blazesym"
readme = "README.md"
categories = [
"algorithms",
"api-bindings",
"development-tools::debugging",
"os::unix-apis",
"value-formatting",
]
keywords = [
"dwarf",
"elf",
"gsym",
"stacktrace",
"tracing",
]
exclude = ["data/dwarf-example", "data/kallsyms.xz"]
autobenches = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "blazesym"
crate-type = ["cdylib", "rlib", "staticlib"]

[features]
default = ["lru"]
# Enable this feature to re-generate the library's C header file. An
# up-to-date version of this header should already be available in the
# include/ directory, so this feature is only necessary when APIs are
# changed.
generate-c-header = ["cbindgen", "which"]
# Enable this feature to opt in to the generation of test files. Having test
# files created is necessary for running tests.
generate-test-files = ["xz2", "zip"]
# Enable this feature to opt in to the generation of benchmark files.
# This feature is required for some of the benchmarks. Note that git-lfs
# needs to be installed in this case.
generate-bench-files = ["reqwest", "xz2"]
# Disable generation of test files. This feature takes preference over
# `generate-test-files`.
dont-generate-test-files = []
# Enable code paths requiring a nightly toolchain. This feature is only meant to
# be used for testing and benchmarking purposes, not for the core library, which
# is expected to work on stable.
nightly = []

[[bench]]
name = "main"
harness = false

[dependencies]
libc = "0.2.137"
log = {version = "0.4.17", optional = true}
lru = {version = "0.10", optional = true}

[dev-dependencies]
anyhow = "1.0.71"
blazesym = {path = ".", features = ["generate-test-files", "log"]}
criterion = "0.4"
env_logger = "0.10"
tempfile = "3.4"
test-log = "0.2"

[build-dependencies]
cbindgen = {version = "0.24", optional = true}
libc = "0.2.137"
reqwest = {version = "0.11.18", optional = true, features = ["blocking"]}
xz2 = {version = "0.1.7", optional = true}
which = {version = "4.4.0", optional = true}
zip = {version = "0.6.4", optional = true, default-features = false}
29 changes: 29 additions & 0 deletions blazesym/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2022, Kuifeng Lee
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading