-
Notifications
You must be signed in to change notification settings - Fork 263
/
CMakeLists.txt
350 lines (302 loc) · 12.4 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#
#
# Procmon-for-Linux
#
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
#################################################################################
#
# CMakeLists.txt
#
# Build script
#
#################################################################################
#################################################################################
# NOTE: #
# Please compile in a build directory rather than the source directory. #
# From Procmon/linux: #
# #
# $ mkdir build; cd build #
# $ cmake .. #
# $ make #
# #
# If you later change this CMakeLists.txt file, remake the build directory. #
# #
# $ cd .. #
# $ rm -rf build #
# $ mkdir build; cd build #
# $ cmake .. #
# $ make #
# #
#################################################################################
cmake_minimum_required (VERSION 3.14)
#
# Set the project version number.
#
if (DEFINED ENV{VERSION})
project (procmon VERSION $ENV{VERSION})
else()
project (procmon VERSION 0.0.0)
endif()
set(PROJECT_VERSION_TWEAK 0)
file(READ "dist/changelog" CHANGE_LOG)
#
# make ncurses a requirement
#
find_package(Curses REQUIRED)
set(LD "/usr/bin/ld")
#
# enable Debug while pre-release; re-enable it post-release to add symbols to binary
#
#set(CMAKE_BUILD_TYPE Debug)
#option(DEBUG_K "Enter debug mode" On)
#
# package name
#
set(PACKAGE_NAME "procmon")
#
# MAN page
#
set(PROCMON_COMPRESS_MAN "procmon.1.gz")
add_custom_target(procmonManPageCompress ALL
DEPENDS ${PROJECT_BINARY_DIR}/${PROCMON_COMPRESS_MAN}
)
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/${PROCMON_COMPRESS_MAN}
COMMAND gzip -9n -f -c "${CMAKE_SOURCE_DIR}/procmon.1" > ${PROJECT_BINARY_DIR}/${PROCMON_COMPRESS_MAN}
COMMENT "Compressing Procmon man page"
DEPENDS "${CMAKE_SOURCE_DIR}/procmon.1"
)
#
# Change log
#
set(PROCMON_COMPRESS_CHANGELOG "changelog.gz")
add_custom_target(procmonChangelogCompress ALL
DEPENDS ${PROJECT_BINARY_DIR}/${PROCMON_COMPRESS_CHANGELOG}
)
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/${PROCMON_COMPRESS_CHANGELOG}
COMMAND cp "${CMAKE_SOURCE_DIR}/dist/changelog" ${PROJECT_BINARY_DIR} && gzip -f -c "${CMAKE_SOURCE_DIR}/dist/changelog" > ${PROJECT_BINARY_DIR}/${PROCMON_COMPRESS_CHANGELOG}
COMMENT "Compressing changelog"
DEPENDS "${CMAKE_SOURCE_DIR}/dist/changelog"
)
#
# Configure files
#
configure_file ("${PROJECT_SOURCE_DIR}/src/version.h.in"
"${PROJECT_BINARY_DIR}/src/version.h"
)
configure_file(dist/DEBIAN.in/control.in DEBIANcontrol)
configure_file(dist/SPECS.in/spec.in SPECS.spec)
include(FetchContent)
#
# Include Sqlite3 amalgamation.
#
add_library(sqlite3-static STATIC
"${CMAKE_SOURCE_DIR}/vendor/sqlite3/sqlite3.c"
)
set_target_properties(sqlite3-static
PROPERTIES
INTERFACE_LINK_LIBRARIES "dl;pthread"
)
#
# Fetch Catch2 testing framework.
#
FetchContent_Declare(Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.7.2
)
FetchContent_MakeAvailable(Catch2)
#
# Set compiler flags.
#
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-omit-frame-pointer -DELPP_THREAD_SAFE")
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED True)
#
# set paths
#
set (PROCMON_TRACER_SRC "${CMAKE_SOURCE_DIR}/src/tracer/ebpf")
set (PROCMON_LOGGING_SRC "${CMAKE_SOURCE_DIR}/src/logging")
set (PROCMON_COMMON_SRC "${CMAKE_SOURCE_DIR}/src/common")
set (PROCMON_STORAGE_SRC "${CMAKE_SOURCE_DIR}/src/storage")
set (PROCMON_CONFIG_SRC "${CMAKE_SOURCE_DIR}/src/configuration")
set (PROCMON_DISPLAY_SRC "${CMAKE_SOURCE_DIR}/src/display")
set (PROCMON_EBPF_SRC "${CMAKE_SOURCE_DIR}/src/tracer/ebpf/kern")
set (PROCMON_SYM_SRC "${CMAKE_SOURCE_DIR}/src/sym")
add_executable(procmon
src/procmon.cpp
src/installer.cpp
"${PROCMON_TRACER_SRC}/ebpf_tracer_engine.cpp"
"${PROCMON_TRACER_SRC}/syscall_schema.cpp"
"${PROCMON_LOGGING_SRC}/easylogging++.cc"
"${PROCMON_COMMON_SRC}/cli_utils.cpp"
"${PROCMON_COMMON_SRC}/printable.cpp"
"${PROCMON_STORAGE_SRC}/sqlite3_storage_engine.cpp"
"${PROCMON_STORAGE_SRC}/storage_proxy.cpp"
"${PROCMON_STORAGE_SRC}/mock_storage_engine.cpp"
"${PROCMON_CONFIG_SRC}/procmon_configuration.cpp"
"${PROCMON_DISPLAY_SRC}/column.cpp"
"${PROCMON_DISPLAY_SRC}/event_formatter.cpp"
"${PROCMON_DISPLAY_SRC}/headless.cpp"
"${PROCMON_DISPLAY_SRC}/kill_event_formatter.cpp"
"${PROCMON_DISPLAY_SRC}/screen.cpp"
"${PROCMON_SYM_SRC}/bcc_proc.cpp"
"${PROCMON_SYM_SRC}/bcc_syms.cc"
"${PROCMON_SYM_SRC}/bcc_elf.cpp"
"${PROCMON_SYM_SRC}/bcc_perf_map.cpp"
"${PROCMON_SYM_SRC}/bcc_zip.cpp"
procmonEBPFkern4.17-5.1.o.o
procmonEBPFkern5.2.o.o
procmonEBPFkern5.3-5.5.o.o
procmonEBPFkern5.6-.o.o
procmonEBPFkern4.17-5.1_core.o.o
procmonEBPFkern5.2_core.o.o
procmonEBPFkern5.3-5.5_core.o.o
procmonEBPFkern5.6-_core.o.o
)
target_include_directories(procmon PUBLIC
/opt/sysinternalsEBPF/libbpf
#"/usr/include/bpf"
"${PROJECT_BINARY_DIR}/src/"
"${CMAKE_SOURCE_DIR}/vendor/sqlite3"
"/opt/sysinternalsEBPF/ebpfKern"
"${PROCMON_SYM_SRC}"
${CURSES_INCLUDE_DIR}
)
target_link_libraries(procmon
sysinternalsEBPF
sqlite3-static
${CURSES_LIBRARIES}
-lpanel
elf
stdc++fs
)
set(PACKED_BINARY_FILES
procmonEBPFkern4.17-5.1.o
procmonEBPFkern5.2.o
procmonEBPFkern5.3-5.5.o
procmonEBPFkern5.6-.o
procmonEBPFkern4.17-5.1_core.o
procmonEBPFkern5.2_core.o
procmonEBPFkern5.3-5.5_core.o
procmonEBPFkern5.6-_core.o
)
foreach(BIN_FILE IN LISTS PACKED_BINARY_FILES)
add_custom_command(OUTPUT "${BIN_FILE}.o"
COMMAND "${LD}" -r -b binary -o "${BIN_FILE}.o" "${BIN_FILE}"
COMMENT "Packing ${BIN_FILE} into ${BIN_FILE}.o"
DEPENDS ${PROJECT_BINARY_DIR}/${BIN_FILE}
)
endforeach(BIN_FILE)
#
# Make package(s)
#
add_custom_target(deb
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"
DEPENDS "${CMAKE_SOURCE_DIR}/dist" "${PROJECT_BINARY_DIR}/procmon"
)
add_custom_target(rpm
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"
DEPENDS "${CMAKE_SOURCE_DIR}/dist" "${PROJECT_BINARY_DIR}/procmon"
)
#
# EBPF
#
# This section makes the EBPF programs
#
set(EBPF_DEPENDS
"${PROCMON_EBPF_SRC}/procmonGenericEntry_rawtp.c"
"${PROCMON_EBPF_SRC}/procmonGenericExit_rawtp.c"
)
set(EBPF_PROGS
procmonEBPFkern4.17-5.1
procmonEBPFkern5.2
procmonEBPFkern5.3-5.5
procmonEBPFkern5.6-
)
#
# set binaries and options for clang and llc
#
set(CLANG "clang")
set(LLC "llc")
set(CLANG_OPTIONS -Wno-unused-value
-Wno-pointer-sign
-Wno-compare-distinct-pointer-types
-Wno-gnu-variable-sized-type-not-at-end
-Wno-address-of-packed-member
-Wno-tautological-compare
-Wno-unknown-warning-option
-g
)
set(CLANG_DEFINES -D __KERNEL__
-D __BPF_TRACING__
-D __TARGET_ARCH_x86
-D __linux__
)
if (DEBUG_K)
message("Using DEBUG_K Option...")
list(APPEND CLANG_DEFINES -DDEBUG_K)
endif()
set(CLANG_INCLUDES
-I "/usr/include"
-I "/usr/include/x86_64-linux-gnu"
-I "${CMAKE_SOURCE_DIR}"
-I "/opt/sysinternalsEBPF/ebpfKern"
-I "/opt/sysinternalsEBPF/libbpf"
-I "${CMAKE_BINARY_DIR}"
)
set(EBPF_CORE_PROG_SUFFIX "_core")
#
# function to make ebpf programs
#
function(build_ebpf ebpfsrc suffix)
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/${ebpfsrc}${suffix}.o
COMMAND "${CLANG}" -nostdinc -isystem `gcc -print-file-name=include` ${CLANG_INCLUDES} ${CLANG_DEFINES} -O2 ${CLANG_OPTIONS} -target bpf -fno-stack-protector -c "${PROCMON_EBPF_SRC}/${ebpfsrc}.c" -o "${PROJECT_BINARY_DIR}/${ebpfsrc}${suffix}.o"
COMMENT "Building EBPF object ${ebpfsrc}${suffix}.o"
DEPENDS ${PROCMON_EBPF_SRC}/${ebpfsrc}.c ${EBPF_DEPENDS}
)
endfunction()
#
# Loop for all ebpf programs
#
foreach(EBPF_PROG IN LISTS EBPF_PROGS)
#
# add custom target to build all ebpf programs with 'all'
#
add_custom_target(${EBPF_PROG} ALL
DEPENDS "${PROCMON_EBPF_SRC}/${EBPF_PROG}.c"
COMMENT "Building EBPF program ${EBPF_PROG}.c"
)
#
# first build NON CORE program
#
list(REMOVE_ITEM CLANG_DEFINES -DEBPF_CO_RE)
build_ebpf(${EBPF_PROG} "")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${EBPF_PROG}.o)
#
# next build CORE program
#
list(APPEND CLANG_DEFINES -DEBPF_CO_RE)
build_ebpf(${EBPF_PROG} ${EBPF_CORE_PROG_SUFFIX})
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${EBPF_PROG}${EBPF_CORE_PROG_SUFFIX}.o)
endforeach(EBPF_PROG)
# Define the parse_syscalls target
add_executable(getsyscalls getsyscalls/getsyscalls.cpp)
# Add a custom target to run parse_syscalls
add_custom_target(run_getsyscalls
COMMAND ${CMAKE_SOURCE_DIR}/getsyscalls/getsyscalls.sh 6.7
DEPENDS getsyscalls
COMMENT "Running getsyscalls"
)
# Add dependencies to ensure parse_syscalls runs before other targets
add_dependencies(procmon run_getsyscalls)