From nobody Wed Jul 06 11:03:58 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 E2B351D00A17; Wed, 6 Jul 2022 11:03:58 +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 4LdGrG4xnPz3N62; Wed, 6 Jul 2022 11:03:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657105438; 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=VKEpXdjoA92LXrvUJDrg2d7IlVEMMtwlvOH/+XxNUPo=; b=ypsydWdCUQRJZC8N145SNDPwijk5n5uqhqbEXtaOkb7BBqIZSdGt2WX/ENul/jITWj5OQG yPNDCgWlb/jk/ZAx2U1kOvnqh5Uvl33Lr+w7a+TJIoDCv6CKZOfgB9RU3zLeys0c6kZG3H 6ENtBiDJmx0x6WKASJZvQsjl1U5w0yc13dFXowlN7kjGTHEyJtH4K2cQJWI+6V+KpWsjjn /0ejwWb/s2KFlXlWDYbluDIRZuoCsDzRld+H6tVje8VpOpjs5ArgifJ7nJuk9fIKDRDKW8 GOYe1vTDQ0exDx6wFqXc/IYwwvq8yBMlBbuj/ZX6anFLLPfMVHGfJXk/enEjKg== 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 7C22B1951E; Wed, 6 Jul 2022 11:03:58 +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 266B3wx0096375; Wed, 6 Jul 2022 11:03:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 266B3wr6096374; Wed, 6 Jul 2022 11:03:58 GMT (envelope-from git) Date: Wed, 6 Jul 2022 11:03:58 GMT Message-Id: <202207061103.266B3wr6096374@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: b756b92a3ad8 - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: b756b92a3ad8ed99fae86fa313dcac31f4719ad6 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1657105438; 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=VKEpXdjoA92LXrvUJDrg2d7IlVEMMtwlvOH/+XxNUPo=; b=MuHjlp+GK3d3eFDxZcaFUB7YiQa0bmTGuBVzV41LrIHFVl12BMaBjrSBpCaSgpgD+FhpdU qTvAQS/XcOgzVnEcCRtSv/ZXe+hkLIKX7V1AVW/Uhi3rKuHw4pC1zMIi8m11J9OfrCRMl0 +0mSqFLrXO6tel1CFh82iZ0Ow/dGQZ+oNTqF3Oxmyhal7yTur9Fxzs01czyuifIoLRm8fo JISaRWs3EWMsQe60bDRgP52uo1AaZbzmdEJrOExI/DAPrD7w8pUOVSZ+lgpn1ZLqgLN0G1 mU5NW7njQthHqzwvmLotfE1Drb+oIiZkl4u/VFq1IT+rzX47LWfQwtbcbCQcAw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1657105438; a=rsa-sha256; cv=none; b=sKYwignDgIzvR6IBV6wnu88u3YSc+Cx7aad8fQubks7yH19/cMLqvISVAyQteY1r+0tGNU q309NX1Fj4ABUlxvvab3Lpa54vu8ovxVILHLb8VjaKpgi48ok37LYismx53m6NRNVKLKk4 ouGG86Ot4KZphX7PP5UZ90tEis6q0C+jOiBDS3RpfG6PjkwSrjZnEVqy18pTrSm1AeNrVb 4fqHh205smjoQh48nOhOuQwUpWCnwISGsdobhTqomRWJtBO7CXq3oVLKR7poJXXQmYPUyE 74mx5VtSEdM8pId1izAHPfnmdsJwwhxFHXw5OlzXU5X60h7JhlU2XS8WcT9x0Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=b756b92a3ad8ed99fae86fa313dcac31f4719ad6 commit b756b92a3ad8ed99fae86fa313dcac31f4719ad6 Author: Dmitry Chagin AuthorDate: 2022-06-22 10:58:53 +0000 Commit: Dmitry Chagin CommitDate: 2022-07-06 11:02:12 +0000 libsysdecode: Add preliminary support for decoding Linux syscalls Differential revision: https://reviews.freebsd.org/D35354 MFC after: 2 weeks (cherry picked from commit b69ae1a34c6f918118693490f18a81ecd7163f83) --- 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 111cff4815c0..b1e7d1fb526c 100644 --- a/lib/libsysdecode/sysdecode.h +++ b/lib/libsysdecode/sysdecode.h @@ -136,4 +136,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__ */