Skip to content

Commit ec32a73

Browse files
authored
Add resource tracking to ProcDump (#219)
* Initial check-point for restrack support * Restrack check-point * Check-point * check-point * check-point * check-point * check-point * Required updates for new images * Required updates for new images * build infra updates * ZeroMemory * remove restrack integration test since it fails in unprivileged containers * Add syslog * Fix enabling of syslog * Update license information and fix when symbol resolution fails * Fetch call stack in the entry rather than exit * Refactor * PR feedback * Add changelog entry * Remove stress-ng timing issue
1 parent c57b95b commit ec32a73

74 files changed

Lines changed: 139528 additions & 654 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM mcr.microsoft.com/cbl-mariner/base/core:2.0
2+
3+
# install all needed packages for builds
4+
RUN yum install -y ca-certificates \
5+
git \
6+
gdb \
7+
zlib-devel \
8+
gcc \
9+
rpm-build \
10+
make \
11+
curl \
12+
libcurl-devel \
13+
libicu-devel \
14+
libunwind-devel \
15+
nmap \
16+
wget \
17+
clang \
18+
glibc-devel \
19+
kernel-headers-5.15.125.1-2.cm2.noarch \
20+
binutils \
21+
lsb-release \
22+
cmake \
23+
bpftool \
24+
libbpf-devel
25+
26+
# install JQ since it doesn't have a .rpm package
27+
RUN curl https://stedolan.github.io/jq/download/linux64/jq > /usr/bin/jq && chmod +x /usr/bin/jq
28+
29+
# install .net core 6 for ESRP signing and integration tests
30+
RUN yum install -y dotnet-sdk-6.0
31+
32+
# Update packages to latest
33+
RUN yum update -y

.devcontainer/Dockerfile_Rocky

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ RUN yum install git \
2323
nmap \
2424
wget \
2525
clang \
26-
redhat-lsb
27-
28-
# Make sure gcc-10 is enabled
29-
RUN echo "source scl_source enable gcc-toolset-10" > /etc/profile.d/gcc.sh
26+
redhat-lsb \
27+
cmake \
28+
elfutils-libelf-devel \
29+
libbpf-devel \
30+
bpftool
3031

3132
# install JQ since it doesn't have a .rpm package
3233
RUN curl https://stedolan.github.io/jq/download/linux64/jq > /usr/bin/jq && chmod +x /usr/bin/jq

.devcontainer/Dockerfile_Ubuntu

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,30 @@ RUN apt upgrade -y \
2424
gdb \
2525
zlib1g-dev \
2626
stress-ng \
27-
gcc-10 \
28-
g++-10 \
29-
cpp-10 \
3027
wget \
3128
dpkg-dev \
3229
fakeroot \
3330
lsb-release \
3431
gettext \
3532
liblocale-gettext-perl \
33+
pax \
34+
cmake \
35+
libelf-dev \
3636
clang \
37-
pax
37+
clang-12 \
38+
llvm \
39+
build-essential \
40+
libbpf-dev
3841

39-
# prefer gcc-10 over the already installed version
40-
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
42+
# Set preference to clang-12
43+
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-12 100
44+
45+
# Build and install bpftool
46+
RUN rm -rf /usr/sbin/bpftool
47+
RUN git clone --recurse-submodules https://github.com/libbpf/bpftool.git
48+
WORKDIR bpftool/src
49+
RUN make install
50+
RUN ln -s /usr/local/sbin/bpftool /usr/sbin/bpftool
4151

4252
# install debbuild
4353
RUN wget https://github.com/debbuild/debbuild/releases/download/22.02.1/debbuild_22.02.1-0ubuntu20.04_all.deb \
@@ -50,7 +60,4 @@ RUN rm packages-microsoft-prod.deb
5060
RUN apt -y update && apt-get install -y dotnet-runtime-6.0
5161
RUN apt-get install -y dotnet-sdk-6.0
5262

53-
# Make sure we use gcc-10 when building
54-
ENV CC=gcc-10
55-
56-
WORKDIR /azp
63+
WORKDIR /azp

BUILD.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Containerized Builds
22
The Dockerfiles in this repo (located under the `.devcontainer` directory) are the same Dockerfiles that are used on the backend build systems when a PR is built as part of the PR checks. This provides an easy and convenient way to ensure that any changes being made can be built using the same backend infrastructure.
33

4-
There are two Dockerfiles available:
4+
There are three Dockerfiles available:
55

66
- `Dockerfile_Ubuntu` (default)
77
- `Dockerfile_Rocky`
8+
- `Dockerfile_AzureLinux`
89

910
There are two primary ways to build using containers:
1011

@@ -16,25 +17,22 @@ For more information about VS Code Dev Containers please see - https://code.visu
1617

1718
To build inside the container:
1819
```sh
20+
mkdir build
21+
cd build
22+
cmake ..
1923
make
20-
make install
2124
```
2225
# Local Builds
2326
## Prerequisites
24-
- clang v10+
25-
- gcc v10+
26-
- zlib
27-
- cmake 3.10+
28-
2927
### Ubuntu
3028
```
3129
sudo apt update
32-
sudo apt -y install gcc make clang gdb zlib1g-dev cmake
30+
sudo apt -y install gcc cmake make clang clang-12 gdb zlib-devel libelf-dev build-essential libbpf-dev linux-tools-common linux-tools-$(uname -r)
3331
```
3432

3533
### Rocky Linux
3634
```
37-
sudo yum install gcc make clang gdb zlib-devel cmake
35+
sudo yum install gcc make cmake clang gdb zlib-devel elfutils-libelf-devel libbpf-devel bpftool
3836
```
3937

4038
## Build
@@ -48,6 +46,12 @@ make
4846
# Building Packages
4947
The distribution packages for Procdump for Linux are constructed utilizing `dpkg-deb` for Debian targets and `rpmbuild` for Fedora targets.
5048

49+
Create a deb package:
5150
```sh
52-
make packages
53-
```
51+
make deb
52+
```
53+
54+
Create an rpm package:
55+
```sh
56+
make rpm
57+
```

CMakeLists.txt

Lines changed: 105 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ endif()
3939
set(PROJECT_VERSION_TWEAK 0)
4040
file(READ "dist/changelog" CHANGE_LOG)
4141

42+
#
43+
# enable Debug while pre-release; disable Debug in post-release
44+
#
45+
#set(CMAKE_BUILD_TYPE Debug)
46+
#option(DEBUG_K "Enter debug mode" On)
47+
4248
#
4349
# package name
4450
#
@@ -83,6 +89,9 @@ set(procdump_INC ${CMAKE_SOURCE_DIR}/include)
8389
set(procdump_SRC ${CMAKE_SOURCE_DIR}/src)
8490
set(procdump_Test ${CMAKE_SOURCE_DIR}/tests/integration)
8591
set(LD "/usr/bin/ld")
92+
set(libbpf_SOURCE_DIR ${CMAKE_BINARY_DIR}/libbpf/src/libbpf)
93+
set(procdump_ebpf_SOURCE_DIR ${CMAKE_SOURCE_DIR}/ebpf)
94+
set(sym_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sym)
8695

8796
#
8897
# Configure files
@@ -92,10 +101,14 @@ configure_file(dist/DEBIAN.in/control.in DEBIANcontrol)
92101
configure_file(dist/SPECS.in/spec.in SPECS.spec)
93102

94103
#
95-
# Make procdump profiler
104+
# Compiler
96105
#
97106
set(CMAKE_CXX_COMPILER "clang++")
98107

108+
#
109+
# Make procdump profiler
110+
#
111+
99112
# Figure out which architecture we are building for
100113
if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
101114
set(CLRHOSTDEF -DHOST_AMD64 -DHOST_64BIT)
@@ -147,31 +160,39 @@ add_custom_command(OUTPUT ProcDumpProfiler.o
147160
#
148161
# Make ProcDump
149162
#
150-
SET (CMAKE_C_COMPILER "clang")
151163
add_executable(procdump
152-
${procdump_SRC}/CoreDumpWriter.c
153-
${procdump_SRC}/DotnetHelpers.c
154-
${procdump_SRC}/Events.c
155-
${procdump_SRC}/GenHelpers.c
156-
${procdump_SRC}/Handle.c
157-
${procdump_SRC}/Logging.c
158-
${procdump_SRC}/Monitor.c
159-
${procdump_SRC}/Procdump.c
160-
${procdump_SRC}/ProcDumpConfiguration.c
161-
${procdump_SRC}/Process.c
162-
${procdump_SRC}/ProfilerHelpers.c
164+
${procdump_SRC}/CoreDumpWriter.cpp
165+
${procdump_SRC}/DotnetHelpers.cpp
166+
${procdump_SRC}/Events.cpp
167+
${procdump_SRC}/GenHelpers.cpp
168+
${procdump_SRC}/Handle.cpp
169+
${procdump_SRC}/Logging.cpp
170+
${procdump_SRC}/Monitor.cpp
171+
${procdump_SRC}/Procdump.cpp
172+
${procdump_SRC}/ProcDumpConfiguration.cpp
173+
${procdump_SRC}/Process.cpp
174+
${procdump_SRC}/ProfilerHelpers.cpp
175+
${procdump_SRC}/Restrack.cpp
176+
${sym_SOURCE_DIR}/bcc_proc.cpp
177+
${sym_SOURCE_DIR}/bcc_syms.cc
178+
${sym_SOURCE_DIR}/bcc_elf.cpp
179+
${sym_SOURCE_DIR}/bcc_perf_map.cpp
180+
${sym_SOURCE_DIR}/bcc_zip.cpp
163181
${PROJECT_BINARY_DIR}/ProcDumpProfiler.o
164182
)
165183

166-
target_compile_options(procdump PRIVATE -pthread -std=gnu99 -fstack-protector-all -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -O2 -Werror)
184+
target_compile_options(procdump PRIVATE -g -pthread -fstack-protector-all -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -Werror -D_GNU_SOURCE -std=c++11 -O2)
167185

168186
target_include_directories(procdump PUBLIC
169187
${procdump_INC}
170188
${PROJECT_BINARY_DIR}
171189
/usr/include
190+
${sym_SOURCE_DIR}
191+
${procdump_ebpf_SOURCE_DIR}
172192
)
173193

174-
target_link_libraries(procdump pthread)
194+
add_dependencies(procdump libbpf procdump_ebpf)
195+
target_link_libraries(procdump ${libbpf_SOURCE_DIR}/src/libbpf.a elf z pthread)
175196

176197
#
177198
# Copy integration test directory
@@ -189,7 +210,7 @@ add_executable(ProcDumpTestApplication
189210
${procdump_Test}/ProcDumpTestApplication.c
190211
)
191212

192-
target_compile_options(ProcDumpTestApplication PRIVATE -pthread -std=gnu99 -fstack-protector-all -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -Werror)
213+
target_compile_options(ProcDumpTestApplication PRIVATE -g -pthread -std=gnu99 -fstack-protector-all -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -Werror -O2)
193214

194215
target_include_directories(ProcDumpTestApplication PUBLIC
195216
/usr/include
@@ -200,7 +221,72 @@ target_link_libraries(ProcDumpTestApplication pthread)
200221
#
201222
# Make package(s)
202223
#
203-
add_custom_target(packages
204-
COMMAND "${CMAKE_SOURCE_DIR}/makePackages.sh" "${CMAKE_SOURCE_DIR}" "${PROJECT_BINARY_DIR}" "${PACKAGE_NAME}" "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" "0"
224+
add_custom_target(deb
225+
COMMAND "${CMAKE_SOURCE_DIR}/makePackages.sh" "${CMAKE_SOURCE_DIR}" "${PROJECT_BINARY_DIR}" "${PACKAGE_NAME}" "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" "0" "deb"
226+
DEPENDS "${CMAKE_SOURCE_DIR}/dist" "${PROJECT_BINARY_DIR}/procdump"
227+
)
228+
229+
add_custom_target(rpm
230+
COMMAND "${CMAKE_SOURCE_DIR}/makePackages.sh" "${CMAKE_SOURCE_DIR}" "${PROJECT_BINARY_DIR}" "${PACKAGE_NAME}" "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" "0" "rpm"
205231
DEPENDS "${CMAKE_SOURCE_DIR}/dist" "${PROJECT_BINARY_DIR}/procdump"
206-
)
232+
)
233+
234+
#
235+
# Make ProcDump eBPF program
236+
#
237+
238+
# Fetch libbpf
239+
include(ExternalProject)
240+
241+
ExternalProject_Add(libbpf
242+
GIT_REPOSITORY https://github.com/libbpf/libbpf.git
243+
GIT_TAG v1.2.2
244+
PREFIX ./libbpf
245+
CONFIGURE_COMMAND ""
246+
BUILD_COMMAND cd ../libbpf/src && bash -c "CFLAGS=\"-g -O2 -Werror -Wall -fPIC\" make"
247+
INSTALL_COMMAND ""
248+
)
249+
250+
# set binaries and options for clang and llc
251+
set(CLANG "clang")
252+
set(LLC "llc")
253+
set(CLANG_OPTIONS -Wno-unused-value
254+
-Wno-pointer-sign
255+
-Wno-compare-distinct-pointer-types
256+
-Wno-gnu-variable-sized-type-not-at-end
257+
-Wno-address-of-packed-member
258+
-Wno-tautological-compare
259+
-Wno-unknown-warning-option
260+
-g
261+
)
262+
set(CLANG_DEFINES -D __KERNEL__
263+
-D __BPF_TRACING__
264+
-D __TARGET_ARCH_x86
265+
-D __linux__
266+
)
267+
if (DEBUG_K)
268+
message("Using DEBUG_K Option...")
269+
list(APPEND CLANG_DEFINES -DDEBUG_K)
270+
endif()
271+
272+
set(CLANG_INCLUDES
273+
-I "/usr/include"
274+
-I "/usr/include/x86_64-linux-gnu"
275+
-I "${CMAKE_SOURCE_DIR}"
276+
-I "${CMAKE_BINARY_DIR}"
277+
-I "${libbpf_SOURCE_DIR}/src"
278+
)
279+
280+
add_custom_target(procdump_ebpf
281+
DEPENDS procdump_ebpf.o
282+
)
283+
284+
add_dependencies(procdump_ebpf libbpf)
285+
286+
add_custom_command(OUTPUT procdump_ebpf.o
287+
COMMAND "${CLANG}" -nostdinc -isystem `gcc -print-file-name=include` ${CLANG_INCLUDES} ${CLANG_DEFINES} -O2 ${CLANG_OPTIONS} -target bpf -fno-stack-protector -c "${procdump_ebpf_SOURCE_DIR}/procdump_ebpf.c" -o "procdump_ebpf.o" && bpftool gen object procdump.ebpf.o procdump_ebpf.o && bpftool gen skeleton "procdump.ebpf.o" name "procdump_ebpf" > "procdump_ebpf.skel.h"
288+
COMMENT "Building EBPF object procdump_ebpf.o"
289+
DEPENDS ${procdump_ebpf_SOURCE_DIR}/procdump_ebpf.c
290+
)
291+
292+
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES procdump.ebpf.o)

0 commit comments

Comments
 (0)