[PATCH v1] print the raw kernel version when using -Xraw
Dmitry V. Levin
ldv at altlinux.org
Tue Dec 11 01:10:30 UTC 2018
On Sun, Dec 09, 2018 at 07:44:02AM -0800, shankarapailoor wrote:
> From 295a8bb4474ed0125e993537dbd2cde9518ae7b6 Mon Sep 17 00:00:00 2001
> From: Shankara Pailoor <shankarapailoor at gmail.com>
> Date: Sun, 9 Dec 2018 07:41:43 -0800
> Subject: [PATCH v1] bpf: change kern_version field to properly handle -Xraw
> and -Xverbose print_kernel_version.c: add function to print kernel version
> defs.h: Likewise Makefile.am: Likewise tests/kern_version-Xabbrev.c: New test
> tests/kern_version-Xraw.c: New test tests/kern_version-Xverbose.c: New test
> tests/kernel_version.c: New test tests/pure_executables.list: Likewise
> tests/gen_tests.in: Likewise tests/.gitignore: Likewise
How do you read this? :)
> ---
> 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
.git/rebase-apply/patch:111: trailing whitespace.
tprintf("KERNEL_VERSION(%lu, %lu, %lu)",
.git/rebase-apply/patch:145: trailing whitespace.
kernel_version-Xabbrev -a16 -Xabbrev -v -e trace=bpf
error: 2 lines add whitespace errors.
> diff --git a/Makefile.am b/Makefile.am
> index cfcbe7c9..3e29f9a6 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -245,6 +245,7 @@ strace_SOURCES = \
> prctl.c \
> print_aio_sigset.c \
> print_dev_t.c \
> + print_kernel_version.c \
> print_group_req.c \
> print_fields.h \
> print_ifindex.c \
Please keep the list sorted.
> diff --git a/bpf.c b/bpf.c
> index e5dc4eeb..d8f39441 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);
> -
> + tprintf(", kern_version=");
> + print_kernel_version(attr.kern_version);
Please use tprints for printing formatless strings.
> diff --git a/tests/.gitignore b/tests/.gitignore
> index 58729054..3184532c 100644
> --- a/tests/.gitignore
> +++ b/tests/.gitignore
> @@ -192,6 +192,10 @@ is_linux_mips_n64
> kcmp
> kcmp-y
> kern_features
> +kernel_version
> +kernel_version-Xraw
> +kernel_version-Xverbose
> +kernel_version-Xabbrev
> kexec_file_load
> kexec_load
> keyctl
Please keep this list sorted.
> diff --git a/tests/gen_tests.in b/tests/gen_tests.in
> index 7374add8..1b97cdfa 100644
> --- a/tests/gen_tests.in
> +++ b/tests/gen_tests.in
> @@ -181,6 +181,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-Xraw -a16 -Xraw -v -e trace=bpf
> +kernel_version-Xverbose -a16 -Xverbose -v -e trace=bpf
> +kernel_version-Xabbrev -a16 -Xabbrev -v -e trace=bpf
> kern_features -a16
> kexec_file_load -s9
> kexec_load -s9
Likewise.
> diff --git a/tests/pure_executables.list b/tests/pure_executables.list
> index a4e9020a..2577597b 100755
> --- a/tests/pure_executables.list
> +++ b/tests/pure_executables.list
> @@ -153,6 +153,10 @@ ipc_shm-Xverbose
> kcmp
> kcmp-y
> kern_features
> +kernel_version
> +kernel_version-Xraw
> +kernel_version-Xverbose
> +kernel_version-Xabbrev
> kexec_file_load
> kexec_load
> keyctl
Likewise.
> --- /dev/null
> +++ b/tests/kernel_version.c
> @@ -0,0 +1,118 @@
> +/*
> + * Check kernel_version decoding.
It's kernel version, not kernel_version.
> + *
> + * Copyright (c) 2015-2017 Dmitry V. Levin <ldv at altlinux.org>
> + * Copyright (c) 2015-2018 The strace developers.
"The strace developers" includes me, so the first line is redundant,
no need to drag it into new code.
> + * 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"
Please ad an empty line after the copyright header.
> +
> +#include <stddef.h>
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +#include <asm/unistd.h>
> +#include "scno.h"
> +
> +#ifdef HAVE_LINUX_BPF_H
> +# include <linux/bpf.h>
> +#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()
My compiler complains about this when -Wold-style-definition is used
(this option is configured to be enabled by default when you use fresh
gcc).
$ echo 'void print_bpf_attr(){}' |gcc -Wold-style-definition -c -xc - -o/dev/null
<stdin>: In function ‘print_bpf_attr’:
<stdin>:1:6: warning: old-style function definition [-Wold-style-definition]
> +{
> +#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(", %lu) = %s\n", sizeof(prog), errstr);
On 32-bit architectures this leads to
tests/kernel_version.c:115:14: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'unsigned int' [-Wformat=]
I think you can safely assume that unsigned int is enough for sizeof(prog).
--
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20181211/21770767/attachment.bin>
More information about the Strace-devel
mailing list