Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ IF (CAPIO_LOG)
execute_process(
COMMAND bash "${PROJECT_SOURCE_DIR}/scripts/gen_syscallnames.sh"
"${PROJECT_BINARY_DIR}/include/syscall/syscallnames.h"
${CMAKE_CXX_COMPILER}
)
ELSE (CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "Capio logger enabled in release mode. skipping compilation of CAPIO logger")
Expand Down
92 changes: 54 additions & 38 deletions scripts/gen_syscallnames.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
#!/bin/bash

if [ -z "${1}" ]; then
echo "Usage: $(basename "${BASH_SOURCE[0]}") OUTPUT_PATH"
exit
OUTPUT_PATH="${1}"
PROVIDED_COMPILER="${2}"

if [ -z "${OUTPUT_PATH}" ]; then
echo "Usage: $(basename "${BASH_SOURCE[0]}") OUTPUT_PATH [COMPILER_PATH]"
exit 1
fi

CC="${PROVIDED_COMPILER:-$(command -v gcc || command -v clang)}"

if [ -z "$CC" ]; then
echo "Error: No compiler found." >&2
exit 1
fi

ARCH=$(uname -m | rev | cut -c 1-2 | rev)
INPUT=$(find /usr/include -name "unistd_${ARCH}.h")
DESTINATION="${1}"

# Generate default destination file
mkdir -p "$(dirname "${DESTINATION}")"
echo 'auto sys_num_to_string(int sysnum) { return "Table not created"; }' > "${DESTINATION}"

# Parse syscall names
echo "Parsing ${INPUT}"
DATA="auto sys_num_to_string(int sysnum) {
switch (sysnum) {" > "${DESTINATION}"

while IFS= read -r line
do
if [[ "$(echo "$line" | grep '#define __NR_')" != "" ]]
then
SYSCALL=$(echo "$line" | cut -d ' ' -f 2)
SYSCALL=${SYSCALL#"__NR_"}
SYSCALLNO=$(echo "$line" | cut -d ' ' -f 3)
DATA="$DATA
case $SYSCALLNO:
return \"$SYSCALL\";" >> "${DESTINATION}"
fi
done < "${INPUT}"

# Add default branch
DATA="$DATA
default:
return \"Unknown\";
};
};"

# Print content to the destination file
echo "${DATA}" > "${DESTINATION}"
OUTPUT_DIRECTORY=$(dirname $OUTPUT_PATH)

echo "Generating syscall table using $CC..."
echo "Output directory: $OUTPUT_DIRECTORY"
echo "Output path: $OUTPUT_PATH"

mkdir -p "$OUTPUT_DIRECTORY"

# Create a temporary C file to resolve macro values
TEMP_C=$(mktemp -t syscall_gen.XXXXXX.c)
echo "#include <asm/unistd.h>" > "$TEMP_C"
$CC -E -dM "$TEMP_C" | grep "#define __NR_" | awk '{print $2}' | sort | uniq | while read -r line; do
echo "VAL_$line=$line" >> "$TEMP_C"
done

# Run preprocessor on our temp file to get the resolved integers
RESOLVED_DEFS=$($CC -E -P "$TEMP_C" | grep "VAL___NR_")
rm "$TEMP_C"

{
echo "#include <string>"
echo ""
echo "inline std::string sys_num_to_string(int sysnum) {"
echo " switch (sysnum) {"

echo "$RESOLVED_DEFS" | while IFS='=' read -r label value; do
name=${label#VAL___NR_}
if [[ "$name" == "syscalls" || "$name" == "ia32_syscalls" || "$name" == "x32_syscalls" ]]; then
continue
fi
if [[ "$value" =~ ^[0-9]+$ ]]; then
echo " case $value: return \"$name\";"
fi
done | sort -n -u -k2

echo " default: return \"Unknown (\" + std::to_string(sysnum) + \")\";"
echo " }"
echo "}"
} > "${OUTPUT_PATH}"

echo "Done! Generated $(grep "case" "$OUTPUT_PATH" | wc -l) syscalls."
Loading