From bf195a8455c282095a441f8faab18d3eb6b46372 Mon Sep 17 00:00:00 2001 From: Shankara Pailoor Date: Sun, 9 Dec 2018 07:41:43 -0800 Subject: [PATCH] bpf: honor xlat styles when printing kernel version * print_kernel_version.c: New file. * Makefile.am (strace_SOURCES): Add it. * defs.h (print_kernel_version): New prototype. * bpf.c (BEGIN_BPF_CMD_DECODER(BPF_PROG_LOAD)): Use print_kernel_version. * tests/kernel_version.c: New file. * tests/kernel_version-Xabbrev.c: Likewise. * tests/kernel_version-Xraw.c: Likewise. * tests/kernel_version-Xverbose.c: Likewise. * tests/gen_tests.in (kernel_version, kernel_version-Xabbrev, kernel_version-Xraw, kernel_version-Xverbose): New tests. * tests/pure_executables.list: Add kernel_version, kernel_version-Xabbrev, kernel_version-Xraw, and kernel_version-Xverbose. * tests/.gitignore: Likewise. Co-Authored-by: Dmitry V. Levin --- Makefile.am | 1 + bpf.c | 7 +- defs.h | 1 + print_kernel_version.c | 51 ++++++++++++++ tests/.gitignore | 4 ++ tests/gen_tests.in | 4 ++ tests/kernel_version-Xabbrev.c | 1 + tests/kernel_version-Xraw.c | 2 + tests/kernel_version-Xverbose.c | 2 + tests/kernel_version.c | 118 ++++++++++++++++++++++++++++++++ tests/pure_executables.list | 4 ++ 11 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 print_kernel_version.c create mode 100644 tests/kernel_version-Xabbrev.c create mode 100644 tests/kernel_version-Xraw.c create mode 100644 tests/kernel_version-Xverbose.c create mode 100644 tests/kernel_version.c diff --git a/Makefile.am b/Makefile.am index cfcbe7c9..2dd2f5f2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -249,6 +249,7 @@ strace_SOURCES = \ print_fields.h \ print_ifindex.c \ print_instruction_pointer.c \ + print_kernel_version.c \ print_mac.c \ print_mq_attr.c \ print_msgbuf.c \ diff --git a/bpf.c b/bpf.c index e5dc4eeb..bfe4c0c8 100644 --- a/bpf.c +++ b/bpf.c @@ -292,11 +292,8 @@ BEGIN_BPF_CMD_DECODER(BPF_PROG_LOAD) /* kern_version field was added in Linux commit v4.1-rc1~84^2~50. */ if (len <= offsetof(struct BPF_PROG_LOAD_struct, kern_version)) break; - tprintf(", kern_version=KERNEL_VERSION(%u, %u, %u)", - attr.kern_version >> 16, - (attr.kern_version >> 8) & 0xFF, - attr.kern_version & 0xFF); - + tprints(", kern_version="); + print_kernel_version(attr.kern_version); /* prog_flags field was added in Linux commit v4.12-rc2~34^2~29^2~2. */ if (len <= offsetof(struct BPF_PROG_LOAD_struct, prog_flags)) break; diff --git a/defs.h b/defs.h index 25c9f4db..d8434095 100644 --- a/defs.h +++ b/defs.h @@ -814,6 +814,7 @@ extern void print_symbolic_mode_t(unsigned int); extern void print_numeric_umode_t(unsigned short); extern void print_numeric_long_umask(unsigned long); extern void print_dev_t(unsigned long long dev); +extern void print_kernel_version(unsigned long version); extern void print_abnormal_hi(kernel_ulong_t); extern bool print_int32_array_member(struct tcb *, void *elem_buf, diff --git a/print_kernel_version.c b/print_kernel_version.c new file mode 100644 index 00000000..5faedd95 --- /dev/null +++ b/print_kernel_version.c @@ -0,0 +1,51 @@ +/* + * Kernel version printing routine. + * + * Copyright (c) 2018 The strace developers. + * 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. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "defs.h" + +void +print_kernel_version(const unsigned long version) +{ + if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) + tprintf("%#lx", version); + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) + return; + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE) + tprints(" /* "); + + tprintf("KERNEL_VERSION(%lu, %lu, %lu)", + version >> 16, + (version >> 8) & 0xFF, + version & 0xFF); + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE) + tprints(" */"); +} diff --git a/tests/.gitignore b/tests/.gitignore index c8609c39..c9dbabb8 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -198,6 +198,10 @@ is_linux_mips_n64 kcmp kcmp-y kern_features +kernel_version +kernel_version-Xabbrev +kernel_version-Xraw +kernel_version-Xverbose kexec_file_load kexec_load keyctl diff --git a/tests/gen_tests.in b/tests/gen_tests.in index f50b53a8..33e76077 100644 --- a/tests/gen_tests.in +++ b/tests/gen_tests.in @@ -187,6 +187,10 @@ ipc_shm-Xraw +ipc.sh -Xraw -a19 ipc_shm-Xverbose +ipc.sh -Xverbose -a36 kcmp -a22 kcmp-y -a22 -y -e trace=kcmp +kernel_version -a16 -v -e trace=bpf +kernel_version-Xabbrev -a16 -Xabbrev -v -e trace=bpf +kernel_version-Xraw -a16 -Xraw -v -e trace=bpf +kernel_version-Xverbose -a16 -Xverbose -v -e trace=bpf kern_features -a16 kexec_file_load -s9 kexec_load -s9 diff --git a/tests/kernel_version-Xabbrev.c b/tests/kernel_version-Xabbrev.c new file mode 100644 index 00000000..2d6da358 --- /dev/null +++ b/tests/kernel_version-Xabbrev.c @@ -0,0 +1 @@ +#include "kernel_version.c" diff --git a/tests/kernel_version-Xraw.c b/tests/kernel_version-Xraw.c new file mode 100644 index 00000000..cf27608b --- /dev/null +++ b/tests/kernel_version-Xraw.c @@ -0,0 +1,2 @@ +#define XLAT_RAW 1 +#include "kernel_version.c" diff --git a/tests/kernel_version-Xverbose.c b/tests/kernel_version-Xverbose.c new file mode 100644 index 00000000..cc6eb1b7 --- /dev/null +++ b/tests/kernel_version-Xverbose.c @@ -0,0 +1,2 @@ +#define XLAT_VERBOSE 1 +#include "kernel_version.c" diff --git a/tests/kernel_version.c b/tests/kernel_version.c new file mode 100644 index 00000000..e7caeaab --- /dev/null +++ b/tests/kernel_version.c @@ -0,0 +1,118 @@ +/* + * Check kernel version decoding. + * + * Copyright (c) 2015-2018 The strace developers. + * 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. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "tests.h" + +#include +#include +#include +#include +#include + +#include +#include "scno.h" + +#ifdef HAVE_LINUX_BPF_H +# include +#endif + +#include "bpf_attr.h" +#include "print_fields.h" + +#include "xlat.h" +#include "xlat/bpf_commands.h" + +#define CMD_STR(x) #x +static const char *errstr; + +static void +print_bpf_attr(void) +{ +#if XLAT_RAW + printf("{prog_type=0x15" +#else + printf("{prog_type=0x15 /* BPF_PROG_TYPE_??? */" +#endif + ", insn_cnt=3134983661" + ", insns=NULL" + ", license=NULL" + ", log_level=24" + ", log_size=3141592653" + ", log_buf=NULL" +#if XLAT_RAW + ", kern_version=0xcafef00d" +#elif XLAT_VERBOSE + ", kern_version=0xcafef00d" + " /* KERNEL_VERSION(51966, 240, 13) */" +#else + ", kern_version=KERNEL_VERSION(51966, 240, 13)" +#endif + ", prog_flags=0" + ", prog_name=\"\"" + ", prog_ifindex=0" + ", expected_attach_type=" +#if XLAT_RAW + "0}" +#elif XLAT_VERBOSE + "0 /* BPF_CGROUP_INET_INGRESS */}" +#else /* XLAT_ABBREV */ + "BPF_CGROUP_INET_INGRESS}" +#endif + ); +} + +int +main(void) +{ + long ret; + struct BPF_PROG_LOAD_struct prog = { + .prog_type = 21, + .insn_cnt = 0xbadc0ded, + .insns = 0, + .license = 0, + .log_level = 24, + .log_size = 3141592653U, + .log_buf = 0, + .kern_version = 0xcafef00d, + .prog_flags = 0, + }; + ret = syscall(__NR_bpf, BPF_PROG_LOAD, &prog, sizeof(prog)); + errstr = sprintrc(ret); +#if XLAT_RAW + printf("bpf(%#x, ", BPF_PROG_LOAD); +#elif XLAT_VERBOSE + printf("bpf(%#x /* %s */, ", BPF_PROG_LOAD, CMD_STR(BPF_PROG_LOAD)); +#else + printf("bpf(%s, ", CMD_STR(BPF_PROG_LOAD)); +#endif + print_bpf_attr(); + printf(", %u) = %s\n", (unsigned int) sizeof(prog), errstr); + puts("+++ exited with 0 +++"); + return 0; +} diff --git a/tests/pure_executables.list b/tests/pure_executables.list index c6c1b0b5..c1ac75ed 100755 --- a/tests/pure_executables.list +++ b/tests/pure_executables.list @@ -159,6 +159,10 @@ ipc_shm-Xverbose kcmp kcmp-y kern_features +kernel_version +kernel_version-Xabbrev +kernel_version-Xraw +kernel_version-Xverbose kexec_file_load kexec_load keyctl -- 2.40.0