From nobody Wed Jun 22 11:40:57 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id D5727871759; Wed, 22 Jun 2022 11:40:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LShKP5Kpyz4skG; Wed, 22 Jun 2022 11:40:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655898057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CsPpEGEOI7MD5vakH6B0UmiCPRIB5yNQ+zAwYs9fKVY=; b=Bh1qCqmrcXh7O0C/6y4KQx4TCw7QwUWPrwMZ79p8Q2a50jGgMhim1+F12moYWZ7DnyxMlt +BPZrTZBo/VPSLyBNAtQmnzMospnXpW3GfqJ6QDJdrPMvAxn/OimItRo3IqUPRPcXzGGmF n2SlWngqD9BVd42LB2lAT1WS8UVC0vle6VLTNVEEmf1vvo0QgpWtV0iRVhG6mx70KR8/q6 NYKKhGAhFBPPXNkzjLdi34E8GjdsM3fV+OfR7ngQ2HV44bMsDmzNzcEnd8aDvxWH0Bs3Sq JQjiio/MycW0SREJBGGYTs0NxmteVhTBZxl0IUf2oadTWQPYQcpLUFs4AU3HrQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9565C2578B; Wed, 22 Jun 2022 11:40:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25MBevkU023609; Wed, 22 Jun 2022 11:40:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25MBev0P023608; Wed, 22 Jun 2022 11:40:57 GMT (envelope-from git) Date: Wed, 22 Jun 2022 11:40:57 GMT Message-Id: <202206221140.25MBev0P023608@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dmitry Chagin Subject: git: b69ae1a34c6f - main - libsysdecode: Add preliminary support for decoding Linux syscalls List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b69ae1a34c6f918118693490f18a81ecd7163f83 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655898057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CsPpEGEOI7MD5vakH6B0UmiCPRIB5yNQ+zAwYs9fKVY=; b=qfzEVFtbRE2oxVHQr1l+L5ZG87ZjQKdMAPFVBamUffpQ+sNNBRIOuxB1GSRCUkJgbFPr6m KxLXLbOShAjrah7lZVeLYBLp/ZysvsC/H27ZO9TlZU5n6jBqRQpvCAt26tkbYtH1LK/G67 6Cso4QLA3jqkq7gg7HaohSZEIfOBJwaeLuP+lT/ZyRBx8rYnwmzHy/IrMsssQQYKTrkIwH opKZCCWnrfuNMW0C1XW17YiL1IeMZKVoldbBEekGlsYOjQIK62KsK6HWwYpHjy1XyQ5uEF RtpdsnFOsribQXa3r+TNc7oKzE5Jku7vO4qTff0ISzXknsYbSui1TYyWTJ+DaA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1655898057; a=rsa-sha256; cv=none; b=nGGUuEGQbaOTPjGPUpzzC6SWy2V294Xm8TZB0TFcKdPUdssdMFDiuMXccRXF+qy2MzHzwS Nqt0jRCB6/eRjcK5sGxcrSzkKp59scBzVQcRWlmuaLEO9UEacceBxmtKgiYo7tt6tt06a0 WJ5i7mAy966yQXaN3+M5OADBnFqs6rF/CRAcg4M60BU3Pot1a18ZaAono4E8IJCQta2Qjz HnJknmwV97ldxAhz8+1kuCCXrVzuqySuudupc694/Rf11uf82pg6zqF1Y1lEIlkcXdNxd7 +uFHgf1eeZGQNN+qGMUlBMy13tc9jj+4MkOeRaznEi7FzW/baS1ZbSh+IH+cDw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=b69ae1a34c6f918118693490f18a81ecd7163f83 commit b69ae1a34c6f918118693490f18a81ecd7163f83 Author: Dmitry Chagin AuthorDate: 2022-06-22 10:58:53 +0000 Commit: Dmitry Chagin 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 + * + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include "support.h" + +#ifdef __aarch64__ +#include +#elif __i386__ +#include +#elif __amd64__ +#ifdef COMPAT_32BIT +#include +#else +#include +#endif +#else +#error "Unsupported Linux arch" +#endif + +#include +#include + +#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" . 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__ */