Skip to content

Commit

Permalink
Merge pull request linuxkerneltravel#764 from ziyangfu/develop
Browse files Browse the repository at this point in the history
MagicEyes: [new feature] cross compile arm64 BPF programs in x64 platform
  • Loading branch information
chenamy2017 authored Apr 22, 2024
2 parents 7c2ffdd + 71d8cc7 commit 77aff6e
Show file tree
Hide file tree
Showing 59 changed files with 8,971 additions and 37 deletions.
41 changes: 30 additions & 11 deletions MagicEyes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# -----------------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.10)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)

set(PROJECT_NAME MagicEyes)
project(${PROJECT_NAME})
Expand Down Expand Up @@ -45,6 +45,20 @@ elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "riscv64")
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "loongarch64")
set(ARCH "loongarch")
endif()

#lht增加:不同编译环境下,构建libbpf和bpftool的编译选项
set(CROSS_COMPILE "")
set(CC ${CMAKE_C_COMPILER})

#lht增加:cmake -Dcross_compilation_arm64=ON .. 来配置交叉编译arm64的环境
if(DEFINED TARCH AND TARCH STREQUAL arm64 OR TARCH STREQUAL aarch64)
message(STATUS "Cross-compilation for arm64 is enabled.")
set(ARCH "arm64")
include(${PROJECT_SOURCE_DIR}/platforms/aarch64/arm64_linux_setup.cmake)
else()
endif()


# 判断CPU架构
# if (NOT DEFINED OS_ARCH)
# string(TOLOWER ${CMAKE_HOST_SYSTEM_PROCESSOR} OS_ARCH)
Expand Down Expand Up @@ -89,13 +103,15 @@ ExternalProject_Add(libbpf
SOURCE_DIR ${CMAKE_SOURCE_DIR}/libbpf/src
CONFIGURE_COMMAND ""
BUILD_COMMAND make
BUILD_STATIC_ONLY=1
OBJDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf
DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf
INCLUDEDIR=
LIBDIR=
UAPIDIR=
install install_uapi_headers
ARCH=${ARCH}
CC=${CC}
BUILD_STATIC_ONLY=1
OBJDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf
DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/libbpf
INCLUDEDIR=
LIBDIR=
UAPIDIR=
install install_uapi_headers
BUILD_IN_SOURCE TRUE
INSTALL_COMMAND ""
STEP_TARGETS build
Expand All @@ -105,8 +121,11 @@ ExternalProject_Add(bpftool
PREFIX bpftool
SOURCE_DIR ${CMAKE_SOURCE_DIR}/bpftool/src
CONFIGURE_COMMAND ""
BUILD_COMMAND make bootstrap
OUTPUT=${CMAKE_CURRENT_BINARY_DIR}/bpftool/
BUILD_COMMAND make
ARCH=${ARCH}
CROSS_COMPILE=${CROSS_COMPILE}
bootstrap
OUTPUT=${CMAKE_CURRENT_BINARY_DIR}/bpftool/
BUILD_IN_SOURCE TRUE
INSTALL_COMMAND ""
STEP_TARGETS build
Expand Down
39 changes: 28 additions & 11 deletions MagicEyes/README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
# MagicEyes

## 1. 简介

一款用于Linux内核的可观测性定制工具,覆盖CPU、内存、网络、文件、虚拟化等子系统。

母项目直达:[lmp](https://github.com/linuxkerneltravel/lmp)

## 2. 架构

![basic_arch](./docs/introduction/images_dir/basic_arch.png)

## 3. 目前已有的工具

- 文件系统部分
- [x] fs_watcher
- [x] fs_watcher

- 内存部分
- [x] mem_watcher
- [x] mem_watcher

- 网络部分
- [x] net_watcher
- [x] net_watcher

- CPU部分
- [x] cpu_watcher
- [x] proc_image
- [x] cpu_watcher
- [x] proc_image

- 虚拟化部分
- [x] kvm_watcher
- [x] kvm_watcher

- 系统诊断与调优
- [x] stack_analyzer
- [x] stack_analyzer


## 4. 编译安装

```bash
git clone git clone --recurse-submodules <magic_eyes_address>
git clone --recurse-submodules <magic_eyes_address>
mkdir build && cd build
# 编译所有工具
cmake -DBUILD_ALL=ON -D -DCMAKE_INSTALL_PREFIX=<install_dir> ..
# 编译单独某个工具,如 fs_watcher
# -------------------------------------------------------
# 若想要编译所有工具
cmake -DBUILD_ALL=ON -DCMAKE_INSTALL_PREFIX=<install_dir> ..
# 若想要编译单独某个工具,如 fs_watcher
cmake -DBUILD_FS_WATCHER=ON ..
# 若想在x64平台交叉编译出arm64平台的程序(TARCH 即 target arch)
cmake -DBUILD_ALL=ON -DTARCH=arm64 ..
# -------------------------------------------------------
make
make install
```
32 changes: 31 additions & 1 deletion MagicEyes/platforms/aarch64/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
ARM64平台交叉编译工具链
# ARM64平台交叉编译工具链

## How to install aarch64-linux-gnu- compilation tools for cross-compilation?

1. install the Ubuntu toolchain gcc-aarch64-linux-gnu GNU C compiler for the arm64 architecture

``` sudo apt-get install gcc-aarch64-linux-gnu ```



2. export the environment variable ARCH and CROSS_COMPILE

``` export ARCH=arm64 && export CROSS_COMPILE=/usr/bin/aarch64-linux-gnu- ```



3. After done before installation, cmake will automatically detect the cross-compilation tools.




## What is the arm64_linux_setup.cmake?

This file is a cmake module used to set the arm64 cross-compilation environment in the cmake project.




## What is the dependency directory?

The dependency directory is used to store the dependent libraries for arm64 cross-compilation.
33 changes: 33 additions & 0 deletions MagicEyes/platforms/aarch64/arm64_linux_setup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(CMAKE_SYSTEM_NAME Linux) #设置目标系统名字
set(CMAKE_SYSTEM_PROCESSOR arm64) #设置目标处理器架构

#设置libbpf和bpftool的make编译选项
set(CROSS_COMPILE "aarch64-linux-gnu-")

#检查交叉编译链是否存在于系统环境变量中,不存在报错结束
find_program(CMAKE_C_COMPILE_PATH NAMES ${CMAKE_C_COMPILER} PATHS $ENV{PATH})
find_program(CMAKE_CXX_COMPILE_PATH NAMES ${CMAKE_CXX_COMPILER} PATHS $ENV{PATH})
if(NOT CMAKE_C_COMPILE_PATH OR NOT CMAKE_CXX_COMPILE_PATH)
message(FATAL_ERROR "Can't find aarch64 compiler, please add it to system environment variable!")
endif()

# 指定交叉编译链
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
message(STATUS "The C compiler identification is ${CMAKE_C_COMPILER} now")
message(STATUS "The CXX compiler identification is ${CMAKE_CXX_COMPILER} now")

set(CC ${CMAKE_C_COMPILER})


#引入libbpf的依赖库zlib, elf
set(ZLIB_ARM64_DIR "${PROJECT_SOURCE_DIR}/platforms/aarch64/dependency/zlib-1.3.1/lib")
set(ELF_ARM64_DIR "${PROJECT_SOURCE_DIR}/platforms/aarch64/dependency/elfutils-0.189/lib")
set(ZLIB_SOURCE_DIR ${PROJECT_SOURCE_DIR}/platforms/aarch64/dependency/zlib-1.3.1/)
set(ELF_SOURCE_DIR ${PROJECT_SOURCE_DIR}/platforms/aarch64/dependency/elfutils-0.189/)
include_directories(
${ZLIB_SOURCE_DIR}/include
${ELF_SOURCE_DIR}/include)

message(STATUS "Arm64 cross-compilation set-up run successfully")
#bpf.c 使用clang编译,.c使用gcc编译
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/sh
#
# Script to make an offline archive for debugging with libdwfl-based tools.
#
# make-debug-archive ARCHIVE {options}
# make-debug-archive --kernel [--force] [RELEASE]
#
# Valid options are those listed under 'Input selection options'
# by running /home/jerry/Downloads/toolchain/elfutils-0.189/_install/bin/eu-unstrip --help.
#
# The archive installed by --kernel be used automatically by -K.
# An offline archive can be used via -e in any tool that accepts those options.
#

UNSTRIP=${UNSTRIP:-/home/jerry/Downloads/toolchain/elfutils-0.189/_install/bin/eu-unstrip}
AR=${AR:-/home/jerry/Downloads/toolchain/elfutils-0.189/_install/bin/eu-ar}
SUDO=${SUDO:-/usr/bin/sudo}

LS=/bin/ls
RM=/bin/rm
MV=/bin/mv
MKDIR=/bin/mkdir
XARGS=/usr/bin/xargs

outdir=${TMPDIR:-/tmp}/debugar$$

usage()
{
echo "Usage: $0 ARCHIVE {options}"
echo " or: $0 --kernel [--sudo] [--force] [RELEASE]"
echo
echo "Valid options are listed under 'Input selection options'"
echo "when running: $UNSTRIP --help"
echo
echo "The --kernel form updates the file used by -K if the"
echo "kernel installation has changed, or always with --force."
echo "With --sudo, touches the installed file via $SUDO."
}

fatal_usage()
{
usage >&2
exit 2
}

script_version()
{
echo "`basename $0` (elfutils) 0.189"
echo "Copyright (C) 2007 Red Hat, Inc."
echo "This is free software; see the source for copying conditions."
echo "There is NO warranty; not even for MERCHANTABILITY or"
echo "FITNESS FOR A PARTICULAR PURPOSE."
echo "Written by Roland McGrath."
}

sudo=
kernel=no
force_kernel=no
while [ $# -gt 0 ]; do
case "x$1" in
x--help) usage; exit 0 ;;
x--version) script_version; exit 0 ;;
x--kernel) kernel=yes ;;
x--force) force_kernel=yes ;;
x--sudo) sudo=$SUDO ;;
*) break ;;
esac
shift
done

if [ $kernel = no ] && [ $force_kernel = yes -o -n "$sudo" ]; then
usage
fi

if [ $kernel = yes ]; then
if [ $# -eq 0 ]; then
release=`uname -r`
elif [ $# -eq 1 ]; then
release=$1
else
fatal_usage
fi

dir=/usr/lib/debug/lib/modules/$release
archive=$dir/debug.a
dep=/lib/modules/$release/modules.dep

if [ ! -d $dir ]; then
echo >&2 "$0: $dir not installed"
exit 1
fi

# Without --force, bail if the kernel installation is not newer.
# This file is normally touched by installing new kernels or modules.
if [ $force_kernel = no -a "$archive" -nt "$dep" ]; then
exit 0
fi

# We have to kill the old one first, because our own -K would use it.
[ ! -e "$archive" ] || $sudo $RM -f "$archive" || exit

set "$archive" "-K$release"
fi

if [ $# -lt 2 ]; then
fatal_usage
fi

archive="$1"
shift

case "$archive" in
/*) ;;
*) archive="`/bin/pwd`/$archive" ;;
esac

if [ -z "$sudo" ]; then
new_archive="$archive.new"
else
new_archive="$outdir.a"
fi

$RM -f "$new_archive" || exit

trap '$RM -rf "$outdir" "$new_archive"' 0 1 2 15

$MKDIR "$outdir" &&
$UNSTRIP -d "$outdir" -m -a -R "$@" &&
(cd "$outdir" && $LS | $XARGS $AR cq "$new_archive") &&
$sudo $MV -f "$new_archive" "$archive"

exit
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 77aff6e

Please sign in to comment.