git: b69ae1a34c6f - main - libsysdecode: Add preliminary support for decoding Linux syscalls
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 22 Jun 2022 11:40:57 UTC
The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=b69ae1a34c6f918118693490f18a81ecd7163f83 commit b69ae1a34c6f918118693490f18a81ecd7163f83 Author: Dmitry Chagin <dchagin@FreeBSD.org> AuthorDate: 2022-06-22 10:58:53 +0000 Commit: Dmitry Chagin <dchagin@FreeBSD.org> CommitDate: 2022-06-22 10:58:53 +0000 libsysdecode: Add preliminary support for decoding Linux syscalls Differential revision: https://reviews.freebsd.org/D35354 MFC after: 2 weeks --- lib/libsysdecode/Makefile | 12 +++-- lib/libsysdecode/linux.c | 106 +++++++++++++++++++++++++++++++++++++++ lib/libsysdecode/mklinuxtables | 109 +++++++++++++++++++++++++++++++++++++++++ lib/libsysdecode/sysdecode.h | 8 +++ 4 files changed, 232 insertions(+), 3 deletions(-) diff --git a/lib/libsysdecode/Makefile b/lib/libsysdecode/Makefile index 7f8175e7bd53..6fb32caebcb1 100644 --- a/lib/libsysdecode/Makefile +++ b/lib/libsysdecode/Makefile @@ -5,6 +5,10 @@ LIB= sysdecode SRCS= errno.c flags.c ioctl.c signal.c syscallnames.c utrace.c support.c +.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \ + ${MACHINE_CPUARCH} == "i386" +SRCS+= linux.c +.endif INCS= sysdecode.h CFLAGS+= -I${.OBJDIR} @@ -106,7 +110,7 @@ MLINKS+=sysdecode_mask.3 sysdecode_accessmode.3 \ sysdecode_mask.3 sysdecode_wait4_options.3 \ sysdecode_mask.3 sysdecode_wait6_options.3 -CLEANFILES= ioctl.c ioctl.c.tmp tables.h +CLEANFILES= ioctl.c ioctl.c.tmp tables.h tables_linux.h .if defined(COMPAT_32BIT) CPP+= -m32 @@ -121,9 +125,11 @@ CFLAGS.gcc.ioctl.c+= -Wno-redundant-decls CFLAGS.gcc+= ${CFLAGS.gcc.${.IMPSRC}} -DEPENDOBJS+= tables.h +DEPENDOBJS+= tables.h tables_linux.h tables.h: mktables sh ${.CURDIR}/mktables ${SYSROOT:U${DESTDIR}}${INCLUDEDIR} ${.TARGET} +tables_linux.h: mklinuxtables + sh ${.CURDIR}/mklinuxtables ${SRCTOP}/sys ${.TARGET} # mkioctls runs find(1) for headers so needs to rebuild every time. This used # to be a hack only done in buildworld. @@ -139,7 +145,7 @@ ioctl.c: ioctl.c.tmp mv -f ${.TARGET}.tmp ${.TARGET}; \ fi -beforedepend: ioctl.c tables.h +beforedepend: ioctl.c tables.h tables_linux.h HAS_TESTS= SUBDIR.${MK_TESTS}+= tests diff --git a/lib/libsysdecode/linux.c b/lib/libsysdecode/linux.c new file mode 100644 index 000000000000..68b6c3fa9472 --- /dev/null +++ b/lib/libsysdecode/linux.c @@ -0,0 +1,106 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2022 Dmitry Chagin <dchagin@FreeBSD.orf> + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/types.h> +#include <sys/proc.h> +#include <stdbool.h> +#include <stdio.h> +#include <sysdecode.h> + +#include "support.h" + +#ifdef __aarch64__ +#include <arm64/linux/linux.h> +#elif __i386__ +#include <i386/linux/linux.h> +#elif __amd64__ +#ifdef COMPAT_32BIT +#include <amd64/linux32/linux.h> +#else +#include <amd64/linux/linux.h> +#endif +#else +#error "Unsupported Linux arch" +#endif + +#include <compat/linux/linux.h> +#include <compat/linux/linux_timer.h> + +#define X(a,b) { a, #b }, +#define XEND { 0, NULL } + +#define TABLE_START(n) static struct name_table n[] = { +#define TABLE_ENTRY X +#define TABLE_END XEND }; + +#include "tables_linux.h" + +#undef TABLE_START +#undef TABLE_ENTRY +#undef TABLE_END + +void +sysdecode_linux_clockid(FILE *fp, clockid_t which) +{ + const char *str; + clockid_t ci; + pid_t pid; + + if (which >= 0) { + str = lookup_value(clockids, which); + if (str == NULL) + fprintf(fp, "UNKNOWN(%d)", which); + else + fputs(str, fp); + return; + } + if ((which & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK) { + fputs("INVALID PERTHREAD|CLOCKFD", fp); + goto pidp; + } + ci = LINUX_CPUCLOCK_WHICH(which); + if (LINUX_CPUCLOCK_PERTHREAD(which) == true) + fputs("THREAD|", fp); + else + fputs("PROCESS|", fp); + str = lookup_value(clockcpuids, ci); + if (str != NULL) + fputs(str, fp); + else { + if (ci == LINUX_CLOCKFD) + fputs("CLOCKFD", fp); + else + fprintf(fp, "UNKNOWN(%d)", which); + } + +pidp: + pid = LINUX_CPUCLOCK_ID(which); + fprintf(fp, "(%d)", pid); +} diff --git a/lib/libsysdecode/mklinuxtables b/lib/libsysdecode/mklinuxtables new file mode 100644 index 000000000000..62f0d9329c94 --- /dev/null +++ b/lib/libsysdecode/mklinuxtables @@ -0,0 +1,109 @@ +#!/bin/sh +# +# Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. 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. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. +# +# $FreeBSD$ +# +# Generates tables_linux.h +# + +set -e + +LC_ALL=C; export LC_ALL + +if [ -z "$1" ] +then + echo "usage: sh $0 include-dir [output-file]" + exit 1 +fi +include_dir=$1 +if [ -n "$2" ]; then + output_file="$2" + output_tmp=$(mktemp -u) + exec > "$output_tmp" +fi + +all_headers= +# +# Generate a table C #definitions. The including file can define the +# TABLE_NAME(n), TABLE_ENTRY(x), and TABLE_END macros to define what +# the tables map to. +# +gen_table() +{ + local name grep file excl filter + name=$1 + grep=$2 + file=$3 + excl=$4 + + if [ -z "$excl" ]; then + filter="cat" + else + filter="egrep -v" + fi + cat <<_EOF_ +TABLE_START(${name}) +_EOF_ + if [ -e "${include_dir}/${file}" ]; then + all_headers="${all_headers:+${all_headers} }${file}" + egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ + $include_dir/$file | ${filter} ${excl} | \ + awk '{ for (i = 1; i <= NF; i++) \ + if ($i ~ /define/) \ + break; \ + ++i; \ + sub(/LINUX_/, "", $i); \ + printf "TABLE_ENTRY(LINUX_%s, %s)\n", $i, $i }' + fi +cat <<_EOF_ +TABLE_END + +_EOF_ +} + +cat <<_EOF_ +/* This file is auto-generated. */ + +_EOF_ + +gen_table "clockids" "LINUX_CLOCK_[A-Z_]+[[:space:]]+[0-9]+" "compat/linux/linux_timer.h" +gen_table "clockcpuids" "LINUX_CPUCLOCK_[A-Z_]+[[:space:]]+[0-9]+" "compat/linux/linux_timer.h" "_MASK|_MAX" + +# Generate a .depend file for our output file +if [ -n "$output_file" ]; then + depend_tmp=$(mktemp -u) + { + echo "$output_file: \\" + echo "$all_headers" | tr ' ' '\n' | sort -u | + sed -e "s,^, $include_dir/," -e 's,$, \\,' + echo + } > "$depend_tmp" + if cmp -s "$output_tmp" "$output_file"; then + rm -f "$output_tmp" "$depend_tmp" + else + mv -f "$depend_tmp" ".depend.${output_file}" + mv -f "$output_tmp" "$output_file" + fi +fi diff --git a/lib/libsysdecode/sysdecode.h b/lib/libsysdecode/sysdecode.h index 3de661bd7ad1..683d344ac0ca 100644 --- a/lib/libsysdecode/sysdecode.h +++ b/lib/libsysdecode/sysdecode.h @@ -134,4 +134,12 @@ bool sysdecode_wait6_options(FILE *_fp, int _options, int *_rem); const char *sysdecode_whence(int _whence); bool sysdecode_shmflags(FILE *_fp, int _flags, int *_rem); +#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__) + +#define SYSDECODE_HAVE_LINUX + +void sysdecode_linux_clockid(FILE *_fp, clockid_t _which); + +#endif /* __i386__ || __amd64__ || __aarch64__ */ + #endif /* !__SYSDECODE_H__ */